/* ---------- Tokens ---------- */
:root {
    --bg: #EEF0F2;
    --surface: #FFFFFF;
    --ink: #171A1F;
    --muted: #6A7280;
    --line: #D9DDE3;
    --accent: #0F6E73;
    --accent-ink: #FFFFFF;
    --danger: #B3372B;
    --ok: #1F8A5B;
    --shadow: 0 1px 2px rgba(20, 24, 31, .04), 0 16px 36px -14px rgba(20, 24, 31, .2);

    /* VS Code Light+ */
    --code-bg: #ffffff; --code-head: #f3f3f3; --code-muted: #6b6b6b; --code-line: #d9dde3;
    --c-text: #000000; --c-comment: #008000; --c-keyword: #0000ff; --c-ctrl: #af00db;
    --c-string: #a31515; --c-number: #098658; --c-func: #795e26; --c-type: #267f99;
    --c-var: #001080; --c-const: #0070c1; --c-attr: #0451a5; --c-cssprop: #e50000;
    --c-tag: #800000; --c-tagpunct: #800000; --c-selector: #800000; --c-regexp: #811f3f;
    --c-add-bg: rgba(9, 134, 88, .12); --c-del-bg: rgba(163, 21, 21, .1);
    --mono: "Cascadia Code", "Cascadia Mono", Consolas, "SF Mono", Menlo, ui-monospace, monospace;
    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #111418;
        --surface: #181C21;
        --ink: #E9ECEF;
        --muted: #8B94A1;
        --line: #272C33;
        --accent: #59C9C1;
        --accent-ink: #0B1F1E;
        --danger: #F08A7E;
        --ok: #5CCB98;
        --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 18px 40px -16px rgba(0, 0, 0, .65);

        /* VS Code Dark+ */
        --code-bg: #1e1e1e; --code-head: #252526; --code-muted: #9d9d9d; --code-line: #333333;
        --c-text: #d4d4d4; --c-comment: #6a9955; --c-keyword: #569cd6; --c-ctrl: #c586c0;
        --c-string: #ce9178; --c-number: #b5cea8; --c-func: #dcdcaa; --c-type: #4ec9b0;
        --c-var: #9cdcfe; --c-const: #4fc1ff; --c-attr: #9cdcfe; --c-cssprop: #9cdcfe;
        --c-tag: #569cd6; --c-tagpunct: #808080; --c-selector: #d7ba7d; --c-regexp: #d16969;
        --c-add-bg: rgba(155, 185, 85, .15); --c-del-bg: rgba(255, 0, 0, .15);
    }
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    min-height: 100dvh;
    background: var(--bg);
    color: var(--ink);
    font: 16px/1.55 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; letter-spacing: -.01em; }

/* ---------- Buttons & fields ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    height: 42px; padding: 0 18px;
    border: 1px solid transparent; border-radius: 12px;
    background: var(--accent); color: var(--accent-ink);
    font-weight: 600; cursor: pointer;
    transition: opacity .15s, transform .1s;
}
.btn:active:not(:disabled) { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.quiet { background: transparent; color: var(--ink); border-color: var(--line); }
.btn.quiet:hover:not(:disabled) { background: var(--surface); }
.btn.small { height: 34px; padding: 0 14px; font-size: .875rem; font-weight: 500; }
.btn.block { width: 100%; }

.field { margin-bottom: 20px; }
.field label { display: block; margin-bottom: 8px; font-weight: 500; }
.field input {
    width: 100%; height: 46px; padding: 0 14px;
    border: 1px solid var(--line); border-radius: 12px;
    background: var(--surface); outline: 0;
    transition: border-color .15s, box-shadow .15s;
}
.field input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}
.hint { margin: 8px 0 0; font-size: .875rem; line-height: 1.5; color: var(--muted); }
.hint b { font-weight: 500; color: var(--ink); overflow-wrap: anywhere; }

.status { min-height: 1.5em; margin: 0 0 16px; font-size: .9rem; }
.status[data-s="ok"] { color: var(--ok); }
.status[data-s="error"] { color: var(--danger); }
.status[data-s="pending"] { color: var(--muted); }

.row { display: flex; gap: 10px; }
.row .btn { flex: 1; }

/* ---------- Sign in ---------- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.auth-panel { width: min(380px, 100%); }
.auth .brand { margin-bottom: 56px; }
.auth h1 { margin: 0 0 32px; font-size: 2.25rem; font-weight: 300; letter-spacing: -.04em; line-height: 1.1; }
.auth .lede { margin: -20px 0 32px; color: var(--muted); }

/* ---------- Header ---------- */
.bar {
    position: sticky; top: 0; z-index: 5;
    display: flex; align-items: center; justify-content: space-between;
    height: 60px; padding: 0 20px;
    background: color-mix(in srgb, var(--bg) 82%, transparent);
    -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
}
.bar-left { display: flex; align-items: center; gap: 8px; margin-left: -8px; }
.bar-right { display: flex; align-items: center; gap: 6px; }
.bar-right form { margin: 0; }

.dot { width: 8px; height: 8px; margin-right: 6px; border-radius: 50%; background: var(--muted); transition: background .3s; }
.dot[data-s="ok"] { background: var(--ok); }
.dot[data-s="error"] { background: var(--danger); }
.dot[data-s="pending"] { animation: pulse .8s ease-in-out infinite alternate; }

.select { position: relative; display: block; }
.select select {
    appearance: none; -webkit-appearance: none;
    max-width: min(46vw, 280px);
    padding: 8px 32px 8px 12px;
    border: 1px solid transparent; border-radius: 10px;
    background: transparent; font-weight: 500;
    text-overflow: ellipsis; cursor: pointer;
}
.select select:hover:not(:disabled) { border-color: var(--line); background: var(--surface); }
.select select:disabled { cursor: default; color: var(--muted); }
.select svg { position: absolute; right: 12px; top: 50%; translate: 0 -50%; color: var(--muted); pointer-events: none; }

.icon {
    display: grid; place-items: center; width: 38px; height: 38px;
    border: 0; border-radius: 10px; background: transparent; color: var(--muted); cursor: pointer;
    transition: background .15s, color .15s;
}
.icon:hover { background: var(--surface); color: var(--ink); }

/* ---------- Stage ---------- */
.stage { width: min(760px, 100% - 40px); margin: 0 auto; padding: 5vh 0 250px; }

.empty { padding-top: 12vh; }
.empty h1 {
    margin: 0 0 14px;
    font-size: clamp(2.25rem, 8vw, 4.25rem);
    font-weight: 300; letter-spacing: -.045em; line-height: 1.05;
    overflow-wrap: anywhere;
    animation: rise .7s cubic-bezier(.2, .7, .2, 1) both;
}
.empty p { margin: 0; max-width: 46ch; color: var(--muted); }
.empty .btn { margin-top: 24px; }

.q {
    margin: 0 0 28px;
    font-size: 1.35rem; font-weight: 500; letter-spacing: -.02em; line-height: 1.35;
    white-space: pre-wrap; overflow-wrap: anywhere;
}
.a { white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.7; }
.a[data-wait]:empty::before { content: attr(data-wait); color: var(--muted); }
.a.streaming::after {
    content: ""; display: inline-block; width: 8px; height: 1.05em; margin-left: 2px;
    vertical-align: -.15em; border-radius: 2px; background: var(--accent);
    animation: blink 1s steps(1) infinite;
}
.a code {
    padding: 2px 6px; border-radius: 6px;
    background: color-mix(in srgb, var(--line) 55%, transparent);
    font-family: var(--mono); font-size: .88em;
}

/* ---------- Code blocks ---------- */
.code {
    margin: 16px 0; overflow: hidden;
    border: 1px solid var(--code-line); border-radius: 12px; background: var(--code-bg);
    white-space: normal;
}
.code-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 5px 6px 5px 14px; background: var(--code-head);
    color: var(--code-muted); font-size: .75rem; line-height: 1.4;
}
.code-copy {
    padding: 4px 10px; border: 0; border-radius: 6px; background: transparent;
    color: inherit; font-size: .75rem; cursor: pointer; transition: background .15s, color .15s;
}
.code-copy:hover { background: color-mix(in srgb, var(--code-muted) 22%, transparent); color: var(--c-text); }
.code-copy.done { color: var(--ok); }
.a pre {
    margin: 0; padding: 14px 16px; overflow-x: auto;
    background: transparent; color: var(--c-text);
    font-family: var(--mono); font-size: .875rem; line-height: 1.6;
    white-space: pre; tab-size: 4;
}
.a pre code { padding: 0; background: none; border-radius: 0; font-size: inherit; color: inherit; }

/* VS Code token colours */
.hljs-comment, .hljs-quote { color: var(--c-comment); }
.hljs-keyword, .hljs-literal, .hljs-symbol, .hljs-bullet, .hljs-meta, .hljs-doctag,
.hljs-variable.language_, .hljs-template-tag { color: var(--c-keyword); }
.hljs-keyword.hljs-ctrl { color: var(--c-ctrl); }
.hljs-string, .hljs-meta .hljs-string, .hljs-code { color: var(--c-string); }
.hljs-number { color: var(--c-number); }
.hljs-title, .hljs-title.function_ { color: var(--c-func); }
.hljs-built_in, .hljs-type, .hljs-title.class_, .hljs-title.class_.inherited__ { color: var(--c-type); }
.hljs-variable, .hljs-params, .hljs-property, .hljs-template-variable { color: var(--c-var); }
.hljs-variable.constant_ { color: var(--c-const); }
.hljs-attr { color: var(--c-attr); }
.hljs-attribute { color: var(--c-cssprop); }
.hljs-tag { color: var(--c-tagpunct); }
.hljs-name { color: var(--c-tag); }
.hljs-selector-tag, .hljs-selector-id, .hljs-selector-class, .hljs-selector-attr, .hljs-selector-pseudo { color: var(--c-selector); }
.hljs-regexp { color: var(--c-regexp); }
.hljs-operator, .hljs-punctuation, .hljs-subst { color: var(--c-text); }
.hljs-section, .hljs-strong { font-weight: 700; }
.hljs-section { color: var(--c-keyword); }
.hljs-emphasis { font-style: italic; }
.hljs-link { color: var(--c-keyword); text-decoration: underline; }
.hljs-addition { color: var(--c-number); background: var(--c-add-bg); display: inline-block; width: 100%; }
.hljs-deletion { color: var(--c-string); background: var(--c-del-bg); display: inline-block; width: 100%; }

.a .err { color: var(--danger); }

/* ---------- Composer ---------- */
.dock { width: min(760px, 100%); margin: 0 auto; }
.composer-wrap {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 4;
    padding: 40px 20px calc(20px + env(safe-area-inset-bottom));
    background: linear-gradient(to top, var(--bg) 55%, transparent);
    pointer-events: none;
}
.composer {
    pointer-events: auto;
    display: flex; align-items: flex-end; gap: 8px;
    width: 100%; padding: 10px 10px 10px 22px;
    border: 1px solid var(--line); border-radius: 28px;
    background: var(--surface); box-shadow: var(--shadow);
    transition: border-color .2s;
}
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 60%, var(--line)); }
.composer textarea {
    flex: 1; min-width: 0; max-height: 40vh; padding: 9px 0;
    border: 0; outline: 0; background: transparent; resize: none; line-height: 1.5;
}
.composer textarea::placeholder { color: var(--muted); }

.send {
    flex: none; display: grid; place-items: center; width: 40px; height: 40px;
    border: 0; border-radius: 50%; background: var(--accent); color: var(--accent-ink); cursor: pointer;
    transition: opacity .2s, transform .1s;
}
.send:disabled { opacity: .28; cursor: default; }
.send:active:not(:disabled) { transform: scale(.93); }
.send[data-mode="send"] .i-stop, .send[data-mode="stop"] .i-send { display: none; }

/* ---------- Thread ---------- */
.turn + .turn { margin-top: 56px; }
.mem-line {
    display: flex; align-items: center; gap: 14px; margin: 44px 0;
    color: var(--muted); font-size: .75rem; letter-spacing: .03em;
}
.mem-line::before, .mem-line::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.foot { display: flex; align-items: center; gap: 14px; margin-top: 20px; }
.note { font-size: .85rem; color: var(--muted); }
.muted { color: var(--muted); }

/* ---------- System prompt ---------- */
.chip {
    pointer-events: auto;
    display: inline-flex; align-items: center; gap: 8px;
    height: 30px; margin: 0 0 10px 6px; padding: 0 13px 0 11px;
    border: 1px solid var(--line); border-radius: 999px;
    background: var(--surface); color: var(--muted);
    font-size: .8125rem; cursor: pointer;
    transition: color .15s, border-color .15s;
}
.chip:hover, .chip[aria-expanded="true"] { color: var(--ink); }
.chip i { width: 6px; height: 6px; border-radius: 50%; background: var(--line); transition: background .2s; }
.chip.on i { background: var(--accent); }
.sys {
    pointer-events: auto;
    margin: 0 0 10px; padding: 14px 16px 10px;
    border: 1px solid var(--line); border-radius: 20px;
    background: var(--surface); box-shadow: var(--shadow);
    animation: rise .25s cubic-bezier(.2, .7, .2, 1);
}
.sys textarea { display: block; width: 100%; min-height: 72px; max-height: 30vh; border: 0; outline: 0; background: transparent; resize: none; line-height: 1.5; }
.sys textarea::placeholder { color: var(--muted); }
.sys-foot { display: flex; justify-content: space-between; margin-top: 6px; font-size: .75rem; color: var(--muted); }

/* ---------- Settings sheet ---------- */
dialog { border: 0; padding: 0; background: var(--bg); color: var(--ink); }
#settings {
    margin: 0 0 0 auto; width: min(480px, 100vw); max-width: none;
    height: 100dvh; max-height: none;
    border-left: 1px solid var(--line); box-shadow: var(--shadow);
}
#settings[open] { animation: slide .3s cubic-bezier(.2, .7, .2, 1); }
#settings::backdrop, #history::backdrop { background: rgba(10, 14, 20, .38); -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); }

.sheet { display: flex; flex-direction: column; height: 100%; }
.sheet-head { flex: none; display: flex; align-items: center; justify-content: space-between; padding: 20px 24px 16px; }
.sheet-head h2 { margin: 0; font-size: 1.25rem; font-weight: 600; letter-spacing: -.02em; }
.sheet-body { flex: 1; overflow-y: auto; padding: 0 24px; overscroll-behavior: contain; }
.sheet-foot {
    flex: none; padding: 14px 24px calc(16px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line); background: var(--bg);
}
.sheet-foot .status { margin: 0 0 10px; min-height: 0; }
.sheet-foot .status:empty { display: none; }

/* thin banded section headers */
.band {
    display: flex; align-items: center; height: 32px; margin: 0 -24px; padding: 0 24px;
    border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
    background: color-mix(in srgb, var(--line) 38%, transparent);
    color: var(--muted); font-size: .6875rem; font-weight: 600; letter-spacing: .1em; text-transform: uppercase;
}
.sec { padding: 20px 0 26px; }
.sec .field { margin-bottom: 14px; }
.test-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.test-row .status { margin: 0; min-height: 0; }

.setting { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 16px 0; }
.setting:first-child { padding-top: 0; }
.setting + .setting { border-top: 1px solid var(--line); }
.setting-text { min-width: 0; }
.setting label, .setting .lbl { display: block; font-weight: 500; }
.setting-text p, .setting .sub { margin: 3px 0 0; font-size: .8125rem; line-height: 1.5; color: var(--muted); }
.setting.stack { display: block; }
.setting-top { display: flex; align-items: baseline; justify-content: space-between; }
.setting output { color: var(--accent); font-weight: 600; font-variant-numeric: tabular-nums; }
.scale { display: flex; justify-content: space-between; font-size: .75rem; color: var(--muted); }

.num {
    flex: none; width: 118px; height: 40px; padding: 0 12px; text-align: right;
    border: 1px solid var(--line); border-radius: 10px; background: var(--surface); outline: 0;
    transition: border-color .15s, box-shadow .15s;
}
.num:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.num::placeholder { color: var(--muted); }

.select.boxed { flex: none; }
.select.boxed select { max-width: none; min-width: 140px; height: 40px; border-color: var(--line); background: var(--surface); }

input[type="range"] { -webkit-appearance: none; appearance: none; display: block; width: 100%; height: 24px; margin: 10px 0 0; background: transparent; cursor: pointer; }
input[type="range"]::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: var(--line); }
input[type="range"]::-moz-range-track { height: 4px; border-radius: 2px; background: var(--line); }
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none; width: 18px; height: 18px; margin-top: -7px; border: 0; border-radius: 50%;
    background: var(--accent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 20%, transparent);
}
input[type="range"]::-moz-range-thumb { width: 18px; height: 18px; border: 0; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 20%, transparent); }

.seg { position: relative; flex: none; display: inline-flex; padding: 3px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.setting .seg label { padding: 6px 13px; border-radius: 9px; font-size: .875rem; color: var(--muted); cursor: pointer; transition: background .15s, color .15s; }
.seg input:checked + label { background: var(--accent); color: var(--accent-ink); }
.seg input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: 1px; }

.switch { position: relative; flex: none; display: inline-block; width: 44px; height: 26px; }
.switch input { position: absolute; inset: 0; z-index: 1; margin: 0; opacity: 0; cursor: pointer; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--line); transition: background .2s; pointer-events: none; }
.switch span::after {
    content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%;
    background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, .3); transition: translate .2s;
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { translate: 18px 0; }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.peek { margin-top: 4px; border-top: 1px solid var(--line); }
.peek summary { padding: 14px 0 4px; color: var(--muted); font-size: .875rem; cursor: pointer; }
.peek summary:hover { color: var(--ink); }
.peek-body {
    margin-top: 8px; padding: 12px 14px; max-height: 240px; overflow: auto;
    border: 1px solid var(--line); border-radius: 12px; background: var(--surface);
    font-size: .875rem; line-height: 1.55; white-space: pre-wrap; overflow-wrap: anywhere;
}

/* ---------- Motion ---------- */
@keyframes rise  { from { opacity: 0; translate: 0 10px; } to { opacity: 1; translate: 0 0; } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes pulse { from { opacity: .35; } to { opacity: 1; } }
@keyframes slideL { from { translate: -40px 0; opacity: 0; } to { translate: 0 0; opacity: 1; } }
@keyframes slide { from { translate: 40px 0; opacity: 0; } to { translate: 0 0; opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- Chat history sheet (opens from the left) ---------- */
#history {
    margin: 0 auto 0 0; width: min(380px, 100vw); max-width: none;
    height: 100dvh; max-height: none;
    border-right: 1px solid var(--line); box-shadow: var(--shadow);
}
#history[open] { animation: slideL .3s cubic-bezier(.2, .7, .2, 1); }

.hist-tools { flex: none; display: grid; gap: 10px; padding: 0 24px 14px; }
.hist-filter {
    width: 100%; height: 42px; padding: 0 14px;
    border: 1px solid var(--line); border-radius: 12px; background: var(--surface); outline: 0;
    transition: border-color .15s, box-shadow .15s;
}
.hist-filter:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.hist-filter::placeholder { color: var(--muted); }

.hist-list { flex: 1; overflow-y: auto; padding: 0 12px 24px; overscroll-behavior: contain; }
.hist-group { margin: 20px 14px 6px; color: var(--muted); font-size: .6875rem; font-weight: 600; letter-spacing: .1em; text-transform: uppercase; }
.hist-empty { padding: 32px 18px; color: var(--muted); font-size: .9rem; line-height: 1.5; text-align: center; }

.chat-row { position: relative; display: flex; align-items: center; border-radius: 12px; }
.chat-row:hover, .chat-row:focus-within, .chat-row.active { background: var(--surface); }
.chat-row.active::before { content: ""; position: absolute; left: 0; top: 11px; bottom: 11px; width: 3px; border-radius: 2px; background: var(--accent); }
.chat-open { flex: 1; min-width: 0; padding: 11px 14px; border: 0; border-radius: 12px; background: transparent; text-align: left; cursor: pointer; }
.chat-title { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-actions { flex: none; display: flex; gap: 2px; padding-right: 6px; opacity: 0; transition: opacity .15s; }
.chat-row:hover .chat-actions, .chat-row:focus-within .chat-actions { opacity: 1; }
@media (hover: none) { .chat-actions { opacity: .75; } }
.mini {
    display: grid; place-items: center; min-width: 30px; height: 30px; padding: 0 6px;
    border: 0; border-radius: 8px; background: transparent; color: var(--muted);
    font-size: .8125rem; cursor: pointer; transition: background .15s, color .15s;
}
.mini:hover { background: color-mix(in srgb, var(--line) 60%, transparent); color: var(--ink); }
.mini.armed { color: var(--danger); font-weight: 500; }
.chat-edit {
    flex: 1; min-width: 0; height: 38px; margin: 3px 6px; padding: 0 12px;
    border: 1px solid var(--accent); border-radius: 10px; background: var(--surface); outline: 0;
}

/* ---------- Memory meter ---------- */
.meter-wrap { padding: 2px 0 18px; }
.meter { height: 4px; overflow: hidden; border-radius: 2px; background: var(--line); }
.meter i { display: block; width: 0; height: 100%; border-radius: 2px; background: var(--accent); transition: width .3s; }
.meter-wrap .sub { margin: 8px 0 0; font-size: .8125rem; line-height: 1.5; color: var(--muted); }
.setting.ruled { border-top: 1px solid var(--line); }
#sumStatus { margin: 0; min-height: 0; }
#sumStatus:empty { display: none; }
