/* Shared tokens + chat styles (adapted from chatone.html) */
:root {
    --bg: #F5F6F8; --surface: #FFFFFF; --side: #ECEFF3; --side-hover: #E1E6ED;
    --ink: #18202E; --muted: #5D6779; --line: #DCE1E8;
    --accent: #2C55C7; --accent-ink: #FFFFFF;
    --user: #E3E9F5; --think: #EEF1F6; --inline-code: #E7EBF2;
    --code: #151B28; --code-head: #1F2738; --code-ink: #E3E8F1;
    --danger: #B83A27; --danger-bg: #FBEDEA; --ok: #1F7A4A; --ok-bg: #E6F4EC;
    --font: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    color-scheme: light;
}
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #12161F; --surface: #1A1F2B; --side: #0E1219; --side-hover: #1C2230;
        --ink: #E4E9F2; --muted: #8E98AA; --line: #2A3141; --accent: #7D9CFF; --accent-ink: #0E1219;
        --user: #233050; --think: #181D29; --inline-code: #252C3B;
        --code: #0C1018; --code-head: #161C29; --danger: #F08C7A; --danger-bg: #3A1F1B; --ok: #6FCF97; --ok-bg: #16301F;
        color-scheme: dark;
    }
}
:root[data-theme="dark"] {
    --bg: #12161F; --surface: #1A1F2B; --side: #0E1219; --side-hover: #1C2230;
    --ink: #E4E9F2; --muted: #8E98AA; --line: #2A3141; --accent: #7D9CFF; --accent-ink: #0E1219;
    --user: #233050; --think: #181D29; --inline-code: #252C3B;
    --code: #0C1018; --code-head: #161C29; --danger: #F08C7A; --danger-bg: #3A1F1B; --ok: #6FCF97; --ok-bg: #16301F;
    color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: var(--font); font-size: 16px; line-height: 1.6; background: var(--bg); color: var(--ink); -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 272px 1fr; height: 100%; }
.sidebar { background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; padding: 14px 10px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 14px; font-weight: 600; min-width: 0; }
.brand span:last-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar { width: 30px; height: 30px; border-radius: 9px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 600; font-size: 14px; flex: none; overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.lg { width: 64px; height: 64px; border-radius: 18px; font-size: 26px; margin-bottom: 18px; }

.new-chat { display: flex; align-items: center; gap: 8px; width: 100%; padding: 10px 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; font-weight: 500; }
.new-chat:hover { border-color: var(--accent); }
.new-chat svg { width: 16px; height: 16px; }

.history-title { font-size: 13px; color: var(--muted); padding: 18px 10px 6px; }
.history { flex: 1; overflow-y: auto; min-height: 0; margin: 0; padding: 0; list-style: none; }
.history li { position: relative; }
.history .item { display: block; width: 100%; text-align: left; background: none; border: 0; border-radius: 8px; padding: 8px 34px 8px 10px; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history .item:hover { background: var(--side-hover); }
.history li.active .item { background: var(--side-hover); font-weight: 500; }
.history .del { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); width: 26px; height: 26px; border: 0; border-radius: 6px; background: none; color: var(--muted); display: none; place-items: center; }
.history li:hover .del, .history li.active .del, .history .del:focus-visible { display: grid; }
.history .del:hover { color: var(--danger); background: var(--surface); }
.history .del svg { width: 14px; height: 14px; }
.history-empty { font-size: 14px; color: var(--muted); padding: 6px 10px; }

.side-foot { border-top: 1px solid var(--line); padding-top: 10px; margin-top: 8px; }
.side-btn { display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 10px; border: 0; border-radius: 8px; background: none; font-size: 14px; text-align: left; }
.side-btn:hover { background: var(--side-hover); }
.side-btn svg { width: 17px; height: 17px; color: var(--muted); flex: none; }
.side-btn .who { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-btn .who b { font-weight: 500; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 8px 16px; border-bottom: 1px solid var(--line); background: var(--bg); min-height: 52px; }
.icon-btn { width: 36px; height: 36px; border: 0; border-radius: 8px; background: none; display: grid; place-items: center; flex: none; }
.icon-btn:hover { background: var(--side-hover); }
.icon-btn svg { width: 20px; height: 20px; }
#menuBtn { display: none; }
.topbar .title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }

.scroll { flex: 1; overflow-y: auto; min-height: 0; }
.thread { max-width: 780px; margin: 0 auto; padding: 32px 24px 24px; }

/* ---------- Empty state ---------- */
.empty { max-width: 640px; margin: 9vh auto 0; padding: 0 24px; }
.empty h1 { font-size: 30px; line-height: 1.2; font-weight: 600; margin: 0 0 8px; letter-spacing: -0.01em; }
.empty p { color: var(--muted); margin: 0 0 28px; }
.starters { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.starter { text-align: left; padding: 14px 16px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); font-size: 14px; line-height: 1.45; }
.starter:hover { border-color: var(--accent); }

/* ---------- Messages ---------- */
.msg { margin-bottom: 30px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .bubble { background: var(--user); padding: 10px 16px; border-radius: 18px 18px 4px 18px; max-width: 85%; white-space: pre-wrap; word-wrap: break-word; }
.msg.assistant { display: grid; grid-template-columns: 30px 1fr; gap: 14px; }
.msg.assistant .body { min-width: 0; }
.meta { display: flex; align-items: center; gap: 4px; margin-top: 8px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.act { border: 0; background: none; color: var(--muted); padding: 4px 8px; border-radius: 6px; font-size: 13px; }
.act:hover { background: var(--side-hover); color: var(--ink); }
.stopped-note { font-style: italic; margin-right: 6px; }
.error { background: var(--danger-bg); color: var(--danger); border-radius: 10px; padding: 10px 14px; font-size: 14px; margin-top: 12px; white-space: pre-wrap; word-wrap: break-word; }

.pending { display: flex; align-items: center; gap: 5px; padding: 8px 0 0; color: var(--muted); font-size: 13px; }
.pending span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.pending span:nth-child(2) { animation-delay: .15s; }
.pending span:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* ---------- Markdown ---------- */
.md { word-wrap: break-word; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 .85em; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.3; margin: 1.4em 0 .5em; font-weight: 600; }
.md h1 { font-size: 1.45em; } .md h2 { font-size: 1.25em; } .md h3 { font-size: 1.1em; } .md h4 { font-size: 1em; }
.md ul, .md ol { margin: 0 0 .85em; padding-left: 1.5em; }
.md li { margin: .25em 0; }
.md li > p { margin: 0 0 .4em; }
.md a { color: var(--accent); text-underline-offset: 2px; }
.md strong { font-weight: 600; }
.md blockquote { margin: 0 0 .85em; padding: .1em 1em; border-left: 3px solid var(--line); color: var(--muted); }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1.5em 0; }
.md :not(pre) > code { font-family: var(--mono); font-size: .88em; background: var(--inline-code); padding: .12em .38em; border-radius: 5px; }
.md img { max-width: 100%; border-radius: 8px; }
.table-wrap { overflow-x: auto; margin: 0 0 1em; border: 1px solid var(--line); border-radius: 10px; }
.md table { border-collapse: collapse; width: 100%; font-size: 14px; }
.md th, .md td { padding: 8px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.md th { background: var(--think); font-weight: 600; }
.md tr:last-child td { border-bottom: 0; }

.codeblock { margin: 0 0 1em; border-radius: 10px; overflow: hidden; background: var(--code); }
.code-head { display: flex; align-items: center; justify-content: space-between; background: var(--code-head); color: #9AA5B8; font-size: 12.5px; padding: 6px 8px 6px 14px; font-family: var(--mono); }
.copy-code { border: 0; background: none; color: #9AA5B8; font-size: 12.5px; font-family: var(--font); padding: 3px 8px; border-radius: 6px; }
.copy-code:hover { background: rgba(255,255,255,.08); color: #fff; }
.codeblock pre { margin: 0; padding: 14px 16px; overflow-x: auto; }
.codeblock code { font-family: var(--mono); font-size: 13.5px; line-height: 1.6; color: var(--code-ink); white-space: pre; background: none; padding: 0; }

.content.streaming > :last-child::after { content: ""; display: inline-block; width: .5em; height: 1.05em; margin-left: 2px; vertical-align: text-bottom; background: var(--accent); animation: caret 1s steps(2) infinite; }
.content.streaming > .codeblock:last-child::after, .content.streaming > .table-wrap:last-child::after { display: none; }
@keyframes caret { 50% { opacity: 0; } }

.hljs-comment, .hljs-quote { color: #6F7C93; font-style: italic; }
.hljs-keyword, .hljs-selector-tag, .hljs-literal, .hljs-doctag { color: #9DB4FF; }
.hljs-string, .hljs-regexp, .hljs-addition, .hljs-template-string { color: #A8D59B; }
.hljs-number, .hljs-symbol, .hljs-bullet { color: #F2B880; }
.hljs-title, .hljs-title.function_, .hljs-section { color: #F0D589; }
.hljs-title.class_, .hljs-type, .hljs-built_in { color: #7FD1D9; }
.hljs-attr, .hljs-attribute, .hljs-variable, .hljs-template-variable, .hljs-property { color: #E6A2C8; }
.hljs-meta { color: #C3A6F2; }
.hljs-tag, .hljs-name { color: #9DB4FF; }

/* ---------- Composer ---------- */
.composer-wrap { padding: 8px 24px calc(14px + env(safe-area-inset-bottom, 0px)); background: var(--bg); }
.composer { max-width: 780px; margin: 0 auto; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 12px 12px 10px 16px; box-shadow: 0 1px 2px rgba(20,30,50,.04), 0 6px 24px rgba(20,30,50,.06); display: flex; align-items: flex-end; gap: 10px; }
.composer:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; border: 0; outline: 0; resize: none; background: none; min-height: 26px; max-height: 240px; line-height: 1.55; padding: 5px 0; }
.send { width: 36px; height: 36px; border-radius: 50%; border: 0; flex: none; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; }
.send:disabled { opacity: .35; cursor: not-allowed; }
.send svg { width: 18px; height: 18px; }
.send.stop { background: var(--ink); color: var(--bg); }
.hint { text-align: center; font-size: 12px; color: var(--muted); margin: 8px auto 0; max-width: 780px; }

/* ---------- Dialog ---------- */
dialog { border: 1px solid var(--line); border-radius: 16px; padding: 0; width: min(440px, calc(100% - 32px)); background: var(--surface); color: var(--ink); }
dialog::backdrop { background: rgba(10,14,22,.45); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 14px 20px; border-bottom: 1px solid var(--line); }
.dlg-head h2 { font-size: 17px; margin: 0; font-weight: 600; }
.dlg-body { padding: 18px 20px; display: grid; gap: 12px; }
.dlg-body p { margin: 0; color: var(--muted); font-size: 14px; }
.dlg-body input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); outline: 0; }
.dlg-body input:focus { border-color: var(--accent); }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--line); }
.primary { border: 0; background: var(--accent); color: var(--accent-ink); border-radius: 10px; padding: 9px 18px; font-weight: 500; }
.ghost { border: 1px solid var(--line); background: none; border-radius: 10px; padding: 8px 14px; font-size: 14px; }
.ghost:hover { border-color: var(--accent); }

.toast { position: fixed; left: 50%; bottom: 120px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 8px; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 50; }
.toast.show { opacity: 1; }
.overlay { display: none; }

@media (max-width: 860px) {
    .app { grid-template-columns: 1fr; }
    .sidebar { position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 30; transform: translateX(-100%); transition: transform .22s ease; padding-top: calc(14px + env(safe-area-inset-top, 0px)); }
    .app.side-open .sidebar { transform: none; }
    .app.side-open .overlay { display: block; position: fixed; inset: 0; background: rgba(10,14,22,.4); z-index: 20; }
    .topbar { padding: calc(8px + env(safe-area-inset-top, 0px)) 12px 8px; }
    #menuBtn { display: grid; }
    .thread { padding: 20px 16px; }
    .composer-wrap { padding-left: 12px; padding-right: 12px; }
    .starters { grid-template-columns: 1fr; }
    .empty { margin-top: 5vh; padding: 0 16px; }
    .msg.user .bubble { max-width: 92%; }
    .msg.assistant { grid-template-columns: 1fr; gap: 8px; }
    .msg.assistant > .avatar { width: 26px; height: 26px; font-size: 12px; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
