/* ═══════════════════════════════════════════════════════
   RAIA · Reflecting Tomorrow · v1.2.5
   Design: Deep Midnight Navy, Slate Silver, Electric Blue
   Type: Syne (heading), DM Sans (body), JetBrains Mono (code)
   ═══════════════════════════════════════════════════════ */

/* ─── Reset ─── */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

/* ─── Tokens ─── */
:root {
    --font-heading: 'Syne', 'DM Sans', system-ui, sans-serif;
    --font-body: 'DM Sans', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    --font-mono: 'JetBrains Mono', ui-monospace, 'SFMono-Regular', Consolas, monospace;

    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-6: 24px;
    --space-8: 32px;
    --space-12: 48px;

    --radius-sm: 8px;
    --radius-md: 14px;
    --radius-lg: 20px;
    --radius-pill: 999px;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.45);

    --control-size: 44px;
    --header-h: 64px;
    --content-max: 960px;

    --text-xs: 0.75rem;
    --text-sm: 0.875rem;
    --text-base: 1rem;
    --text-lg: 1.125rem;
    --text-xl: 1.375rem;
    --text-2xl: clamp(1.75rem, 3.2vw, 2.25rem);

    --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
}

:root[data-theme="dark"],
:root:not([data-theme]) {
    color-scheme: dark;
    --bg: #020617;
    --bg-raised: #0b1222;
    --bg-inset: #040a18;
    --surface: #0f172a;
    --surface-hover: #15203a;
    --border: #1e293b;
    --border-strong: #334155;
    --text: #e2e8f0;
    --text-soft: #94a3b8;
    --text-faint: #64748b;
    --accent: #3b82f6;
    --accent-strong: #2563eb;
    --accent-ink: #93c5fd;
    --on-accent: #ffffff;
    --accent-soft: rgba(59, 130, 246, 0.14);
    --danger: #f87171;
    --danger-soft: rgba(248, 113, 113, 0.12);
    --success: #4ade80;
    --focus: #93c5fd;
    --header-bg: rgba(2, 6, 23, 0.82);
    --user-bubble: #1e3a8a;
    --user-text: #eff6ff;
    --code-head: #0b1222;
}

:root[data-theme="light"] {
    color-scheme: light;
    --bg: #f1f5f9;
    --bg-raised: #ffffff;
    --bg-inset: #e2e8f0;
    --surface: #ffffff;
    --surface-hover: #f1f5f9;
    --border: #cbd5e1;
    --border-strong: #94a3b8;
    --text: #0f172a;
    --text-soft: #334155;
    --text-faint: #64748b;
    --accent: #2563eb;
    --accent-strong: #1d4ed8;
    --accent-ink: #1d4ed8;
    --on-accent: #ffffff;
    --accent-soft: rgba(37, 99, 235, 0.1);
    --danger: #b91c1c;
    --danger-soft: rgba(185, 28, 28, 0.08);
    --success: #15803d;
    --focus: #1d4ed8;
    --header-bg: rgba(241, 245, 249, 0.86);
    --user-bubble: #dbeafe;
    --user-text: #0f172a;
    --code-head: #e2e8f0;
    --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.1);
    --shadow-lg: 0 24px 60px rgba(15, 23, 42, 0.18);
}

/* ─── Base ─── */
html {
    height: 100%;
    scroll-behavior: smooth;
}

body {
    min-height: 100%;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    font-family: var(--font-body);
    font-size: var(--text-base);
    line-height: 1.6;
    color: var(--text);
    background: var(--bg);
    overflow: hidden;
}

h1, h2, h3 {
    font-family: var(--font-heading);
    font-weight: 700;
    line-height: 1.2;
    letter-spacing: -0.01em;
}

a {
    color: var(--accent-ink);
    text-underline-offset: 3px;
}

a:hover { color: var(--text); }

button,
input,
select,
textarea {
    font: inherit;
    color: inherit;
}

button { cursor: pointer; background: none; border: 0; }

button:disabled,
[aria-disabled="true"] {
    cursor: not-allowed;
}

:focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 2px;
    border-radius: 6px;
}

.icon {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.icon-sm { width: 16px; height: 16px; }

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.skip-link {
    position: absolute;
    top: 8px;
    left: 8px;
    z-index: 100;
    padding: 8px 14px;
    border-radius: var(--radius-pill);
    background: var(--accent);
    color: var(--on-accent);
    transform: translateY(-160%);
    transition: transform 0.2s var(--ease);
}

.skip-link:focus { transform: translateY(0); }

/* ─── Header ─── */
.app-header {
    position: sticky;
    top: 0;
    z-index: 20;
    height: var(--header-h);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: 0 clamp(var(--space-3), 3vw, var(--space-6));
    background: var(--header-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
}

.brand {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-width: 0;
    color: var(--text);
    text-decoration: none;
}

.brand:hover { color: var(--text); }

.brand-logo {
    width: auto;
    height: 28px;
    flex-shrink: 0;
}

.brand-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.brand-name {
    font-family: var(--font-heading);
    font-weight: 700;
    font-size: var(--text-lg);
    letter-spacing: 0.04em;
    line-height: 1.1;
}

.brand-tagline {
    font-size: var(--text-xs);
    color: var(--text-soft);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.version-badge {
    margin-left: var(--space-1);
    padding: 2px 8px;
    border-radius: var(--radius-pill);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--accent-ink);
    background: var(--accent-soft);
    border: 1px solid var(--border);
}

.nav {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.nav-link {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--control-size);
    padding: 0 var(--space-4);
    border-radius: var(--radius-pill);
    color: var(--text-soft);
    text-decoration: none;
    font-weight: 500;
    transition: background 0.2s var(--ease), color 0.2s var(--ease);
}

.nav-link:hover {
    background: var(--surface-hover);
    color: var(--text);
}

.nav-link[aria-current="page"] {
    background: var(--accent-soft);
    color: var(--accent-ink);
}

.icon-btn {
    display: inline-grid;
    place-items: center;
    width: var(--control-size);
    height: var(--control-size);
    border-radius: 50%;
    color: var(--text-soft);
    transition: background 0.2s var(--ease), color 0.2s var(--ease);
}

.icon-btn:hover {
    background: var(--surface-hover);
    color: var(--text);
}

.support-link {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--control-size);
    padding: 0 var(--space-4);
    border-radius: var(--radius-pill);
    border: 1px solid var(--border-strong);
    color: var(--text);
    text-decoration: none;
    font-size: var(--text-sm);
    font-weight: 500;
    transition: border-color 0.2s var(--ease), background 0.2s var(--ease);
}

.support-link:hover {
    border-color: var(--accent);
    background: var(--accent-soft);
    color: var(--text);
}

/* ─── Views ─── */
.app-main {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    position: relative;
}

.view {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

/* ─── Chat view ─── */
.chat-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
}

.chat-inner {
    width: 100%;
    max-width: var(--content-max);
    margin: 0 auto;
    padding: var(--space-8) clamp(var(--space-4), 3vw, var(--space-6)) var(--space-6);
    display: flex;
    flex-direction: column;
    gap: var(--space-6);
    min-height: 100%;
}

.welcome {
    margin: auto 0;
    padding: var(--space-8) 0;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-4);
    max-width: 640px;
}

.welcome-title {
    font-size: var(--text-2xl);
}

.welcome-desc {
    color: var(--text-soft);
    max-width: 56ch;
}

.chips {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin-top: var(--space-2);
}

.chip {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--control-size);
    padding: 0 var(--space-4);
    border-radius: var(--radius-pill);
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text);
    font-size: var(--text-sm);
    transition: border-color 0.2s var(--ease), background 0.2s var(--ease);
}

.chip:hover {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.chip .icon { color: var(--accent-ink); }

.notice {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--space-4);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-strong);
    background: var(--surface);
}

.notice-body { flex: 1; min-width: 0; }

.notice-title {
    font-family: var(--font-heading);
    font-weight: 600;
    font-size: var(--text-base);
    margin-bottom: 2px;
}

.notice-text {
    font-size: var(--text-sm);
    color: var(--text-soft);
}

.notice-warn { border-color: var(--accent); background: var(--accent-soft); }
.notice-warn .icon { color: var(--accent-ink); }

.notice-error { border-color: var(--danger); background: var(--danger-soft); }
.notice-error .icon { color: var(--danger); }

.notice-action {
    margin-top: var(--space-3);
}

/* Messages */
.messages {
    display: flex;
    flex-direction: column;
    gap: var(--space-6);
}

.msg {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    max-width: 100%;
}

/* Entrance motion for new messages is handled by GSAP in script.js */

@keyframes msg-in {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}

.msg-user {
    align-items: flex-end;
}

.msg-user .msg-body {
    max-width: min(640px, 86%);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-lg) var(--radius-lg) 6px var(--radius-lg);
    background: var(--user-bubble);
    color: var(--user-text);
    white-space: pre-wrap;
    word-break: break-word;
}

.msg-meta {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-xs);
    color: var(--text-faint);
}

.msg-assistant .msg-body {
    width: 100%;
    line-height: 1.7;
    word-break: break-word;
}

.msg-assistant .msg-body > :first-child { margin-top: 0; }
.msg-assistant .msg-body > :last-child { margin-bottom: 0; }

.msg-body p { margin: var(--space-3) 0; }

.msg-body h1,
.msg-body h2,
.msg-body h3,
.msg-body h4 {
    margin: var(--space-6) 0 var(--space-3);
}

.msg-body h1 { font-size: var(--text-xl); }
.msg-body h2 { font-size: var(--text-lg); }
.msg-body h3,
.msg-body h4 { font-size: var(--text-base); }

.msg-body ul,
.msg-body ol {
    margin: var(--space-3) 0;
    padding-left: var(--space-6);
}

.msg-body li + li { margin-top: var(--space-1); }

.msg-body blockquote {
    margin: var(--space-4) 0;
    padding: var(--space-2) var(--space-4);
    border-left: 3px solid var(--accent);
    color: var(--text-soft);
    background: var(--accent-soft);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.msg-body table {
    display: block;
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    border-collapse: collapse;
    margin: var(--space-4) 0;
    font-size: var(--text-sm);
}

.msg-body th,
.msg-body td {
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
}

.msg-body th { background: var(--surface); }

.msg-body hr {
    border: 0;
    border-top: 1px solid var(--border);
    margin: var(--space-6) 0;
}

.msg-body :not(pre) > code {
    font-family: var(--font-mono);
    font-size: 0.875em;
    padding: 0.15em 0.4em;
    border-radius: 6px;
    background: var(--surface);
    border: 1px solid var(--border);
    word-break: break-word;
}

.msg-body a {
    color: var(--accent-ink);
    text-decoration: underline;
}

/* Code blocks */
.code-block {
    margin: var(--space-4) 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    overflow: hidden;
    background: var(--bg-inset);
}

.code-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    min-height: 40px;
    padding: 0 var(--space-3) 0 var(--space-4);
    background: var(--code-head);
    border-bottom: 1px solid var(--border);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--text-soft);
}

.code-copy {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 32px;
    padding: 0 var(--space-3);
    border-radius: var(--radius-pill);
    color: var(--text-soft);
    font-size: var(--text-xs);
    transition: background 0.2s var(--ease), color 0.2s var(--ease);
}

.code-copy:hover {
    background: var(--surface-hover);
    color: var(--text);
}

.code-block pre {
    margin: 0;
    padding: var(--space-4);
    overflow-x: auto;
    font-family: var(--font-mono);
    font-size: 0.8125rem;
    line-height: 1.6;
    color: var(--text);
    tab-size: 4;
}

.code-block code {
    font-family: inherit;
    white-space: pre;
}

/* KaTeX */
.katex { font-size: 1.05em; }

.katex-display {
    margin: var(--space-4) 0;
    padding: var(--space-1) 0;
    overflow-x: auto;
    overflow-y: hidden;
}

/* Message actions */
.msg-actions {
    display: flex;
    align-items: center;
    gap: var(--space-1);
}

.msg-action {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 32px;
    padding: 0 var(--space-3);
    border-radius: var(--radius-pill);
    font-size: var(--text-xs);
    color: var(--text-faint);
    transition: background 0.2s var(--ease), color 0.2s var(--ease);
}

.msg-action:hover {
    background: var(--surface-hover);
    color: var(--text);
}

/* Typing */
.typing {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    color: var(--text-soft);
    font-size: var(--text-sm);
}

.typing-dots {
    display: inline-flex;
    gap: 4px;
}

.typing-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--accent);
    animation: blink 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes blink {
    0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
    40% { opacity: 1; transform: translateY(-3px); }
}

/* Composer dock */
.dock {
    position: relative;
    padding: var(--space-3) clamp(var(--space-3), 3vw, var(--space-6)) var(--space-4);
    background: linear-gradient(to top, var(--bg) 70%, transparent);
}

.dock-inner {
    width: 100%;
    max-width: var(--content-max);
    margin: 0 auto;
}

.composer {
    display: flex;
    align-items: flex-end;
    gap: var(--space-3);
}

.pill {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    min-height: 52px;
    padding: 6px 6px 6px var(--space-6);
    border-radius: var(--radius-pill);
    border: 1px solid var(--border-strong);
    background: var(--surface);
    box-shadow: var(--shadow-sm);
    transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

.pill:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 4px var(--accent-soft);
}

.pill textarea {
    flex: 1;
    min-width: 0;
    resize: none;
    border: 0;
    outline: none;
    background: transparent;
    padding: 12px 0;
    max-height: 180px;
    line-height: 1.5;
    font-size: var(--text-base);
}

.pill textarea::placeholder { color: var(--text-faint); }

.pill textarea:focus-visible { outline: none; }

.send-btn {
    flex-shrink: 0;
    display: inline-grid;
    place-items: center;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: var(--accent);
    color: var(--on-accent);
    box-shadow: var(--shadow-md);
    transition: background 0.2s var(--ease), transform 0.15s var(--ease), opacity 0.2s var(--ease);
}

.send-btn:hover { background: var(--accent-strong); }
.send-btn:active { transform: scale(0.96); }

.send-btn[aria-disabled="true"] {
    opacity: 0.5;
}

.send-btn[data-mode="stop"] {
    background: var(--danger);
    color: #fff;
}

.send-btn .spinner {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.35);
    border-top-color: #fff;
    animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.dock-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2) var(--space-4);
    margin-top: var(--space-2);
    padding: 0 var(--space-4);
    font-size: var(--text-xs);
    color: var(--text-faint);
}

.kbd {
    display: inline-block;
    padding: 0 6px;
    border-radius: 6px;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    font-family: var(--font-mono);
    font-size: 0.6875rem;
    color: var(--text-soft);
}

.jump-btn {
    position: absolute;
    left: 50%;
    bottom: 110px;
    transform: translateX(-50%);
    z-index: 5;
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--control-size);
    padding: 0 var(--space-4);
    border-radius: var(--radius-pill);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    box-shadow: var(--shadow-md);
    color: var(--text);
    font-size: var(--text-sm);
}

/* ─── Settings view ─── */
.settings-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.settings-inner {
    width: 100%;
    max-width: 720px;
    margin: 0 auto;
    padding: var(--space-8) clamp(var(--space-4), 3vw, var(--space-6)) var(--space-12);
    display: flex;
    flex-direction: column;
    gap: var(--space-6);
}

.settings-head h1 {
    font-size: var(--text-2xl);
    margin-bottom: var(--space-2);
}

.settings-head p {
    color: var(--text-soft);
}

.card {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    padding: var(--space-6);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    background: var(--bg-raised);
}

.card-title {
    font-family: var(--font-heading);
    font-size: var(--text-lg);
    font-weight: 600;
}

.grid-2 {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-4);
}

.field {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    min-width: 0;
}

.field label,
.field .label {
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-soft);
}

.input,
.field input[type="text"],
.field input[type="password"],
.field input[type="search"],
.field input:not([type]),
.field select {
    width: 100%;
    min-height: var(--control-size);
    padding: 0 var(--space-4);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text);
    transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

.field input::placeholder { color: var(--text-faint); }

.field input:focus,
.field select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 4px var(--accent-soft);
}

.input-wrap {
    display: flex;
    gap: var(--space-2);
}

.input-wrap input { flex: 1; }

.hint {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2) var(--space-4);
    font-size: var(--text-xs);
    color: var(--text-faint);
}

.hint a { font-size: var(--text-xs); }

.checkbox {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-height: 32px;
    cursor: pointer;
    color: var(--text-soft);
}

.checkbox input {
    width: 18px;
    height: 18px;
    accent-color: var(--accent);
}

.actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: var(--control-size);
    padding: 0 var(--space-6);
    border-radius: var(--radius-pill);
    font-weight: 600;
    font-size: var(--text-sm);
    border: 1px solid transparent;
    transition: background 0.2s var(--ease), border-color 0.2s var(--ease), opacity 0.2s var(--ease);
}

.btn-primary {
    background: var(--accent);
    color: var(--on-accent);
}

.btn-primary:hover { background: var(--accent-strong); }

.btn-secondary {
    background: transparent;
    border-color: var(--border-strong);
    color: var(--text);
}

.btn-secondary:hover {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.btn-danger {
    background: transparent;
    border-color: var(--danger);
    color: var(--danger);
}

.btn-danger:hover { background: var(--danger-soft); }

.btn:disabled { opacity: 0.55; }

.btn-block { width: 100%; }

.status {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--control-size);
    padding: 0 var(--space-4);
    border-radius: var(--radius-pill);
    background: var(--surface);
    border: 1px solid var(--border);
    font-size: var(--text-sm);
    color: var(--text-soft);
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-faint);
}

.status[data-state="on"] .status-dot { background: var(--success); }
.status[data-state="on"] { color: var(--text); }

details.advanced {
    border-top: 1px solid var(--border);
    padding-top: var(--space-4);
}

details.advanced summary {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--control-size);
    cursor: pointer;
    font-weight: 600;
    font-size: var(--text-sm);
    color: var(--text-soft);
    list-style: none;
}

details.advanced summary::-webkit-details-marker { display: none; }

details.advanced[open] summary .icon { transform: rotate(180deg); }

details.advanced summary .icon { transition: transform 0.2s var(--ease); }

details.advanced .advanced-body {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    padding-top: var(--space-4);
}

.range-row {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-4);
}

.range-group {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.range-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: var(--text-sm);
    color: var(--text-soft);
    font-weight: 500;
}

.range-value {
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--accent-ink);
    padding: 2px 8px;
    border-radius: var(--radius-pill);
    background: var(--accent-soft);
}

input[type="range"] {
    width: 100%;
    min-height: var(--control-size);
    accent-color: var(--accent);
    cursor: pointer;
}

.settings-foot {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    gap: var(--space-4);
}

/* ─── Drawer ─── */
.drawer-overlay {
    position: fixed;
    inset: 0;
    z-index: 40;
    background: rgba(2, 6, 23, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s var(--ease);
}

.drawer-overlay.open {
    opacity: 1;
    pointer-events: auto;
}

.drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    width: min(380px, 100%);
    display: flex;
    flex-direction: column;
    background: var(--bg-raised);
    border-left: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
    transform: translateX(100%);
    visibility: hidden;
    transition: transform 0.3s var(--ease), visibility 0.3s;
}

.drawer.open {
    transform: translateX(0);
    visibility: visible;
}

.drawer-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-4) var(--space-4) var(--space-6);
    border-bottom: 1px solid var(--border);
}

.drawer-head h2 {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-lg);
}

.drawer-body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: var(--space-3);
}

.history-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}

.history-item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-1) var(--space-1) var(--space-1) var(--space-3);
    border-radius: var(--radius-md);
    transition: background 0.2s var(--ease);
}

.history-item:hover,
.history-item.active {
    background: var(--surface-hover);
}

.history-item.active { box-shadow: inset 3px 0 0 var(--accent); }

.history-open {
    flex: 1;
    min-width: 0;
    text-align: left;
    padding: var(--space-2) 0;
    min-height: var(--control-size);
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.history-title {
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-date {
    font-size: var(--text-xs);
    color: var(--text-faint);
}

.history-empty {
    padding: var(--space-8) var(--space-4);
    text-align: center;
    color: var(--text-soft);
    font-size: var(--text-sm);
}

.drawer-foot {
    padding: var(--space-3) var(--space-4);
    border-top: 1px solid var(--border);
}

/* ─── Toasts ─── */
.toasts {
    position: fixed;
    left: 50%;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 96px);
    transform: translateX(-50%);
    z-index: 70;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-2);
    width: min(92vw, 460px);
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-pill);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    box-shadow: var(--shadow-md);
    font-size: var(--text-sm);
    color: var(--text);
}

.toast-error { border-color: var(--danger); }
.toast-success { border-color: var(--success); }

@keyframes toast-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: none; }
}

/* ─── Small screens ─── */
@media (min-width: 640px) {
    .grid-2 { grid-template-columns: 1fr 1fr; }
    .range-row { grid-template-columns: 1fr 1fr; }
    .field-wide { grid-column: 1 / -1; }
}

@media (max-width: 639px) {
    .brand-tagline,
    .version-badge { display: none; }

    /* Keyboard hint is desktop-only */
    .hint-keys { display: none; }

    /* Header on phones: brand shrinks, actions keep 44px touch targets */
    .app-header {
        gap: var(--space-2);
        padding: 0 var(--space-3);
    }

    .brand {
        flex: 1 1 auto;
        min-width: 0;
    }

    .brand-text { min-width: 0; }

    .brand-name { white-space: nowrap; }

    .brand-logo {
        height: 24px;
    }

    .nav {
        flex: 0 0 auto;
        gap: 2px;
    }

    .nav-link .nav-label,
    .support-link .support-label { display: none; }

    /* Support moves to the settings page on phones. Chat stays in the nav (also reachable from the logo) */
    .support-link { display: none; }

    .nav-link,
    .icon-btn {
        width: var(--control-size);
        height: var(--control-size);
        padding: 0;
        justify-content: center;
    }

    .chat-inner { padding-top: var(--space-6); }

    .msg-user .msg-body { max-width: 92%; }

    .jump-btn { bottom: 104px; }

    .pill { padding-left: var(--space-4); }
}

/* ═══════════════════════════════════════════════════════
   v1.2.4 additions
   ═══════════════════════════════════════════════════════ */

/* Active nav link, set by script.js with a class as well as aria-current */
.nav-link.is-active {
    background: var(--accent-soft);
    color: var(--accent-ink);
}

/* URL input (custom endpoint) uses the same look as the other text fields */
.field input[type="url"] {
    width: 100%;
    min-height: var(--control-size);
    padding: 0 var(--space-4);
    border-radius: var(--radius-md);
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text);
    transition: border-color 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

.field input[type="url"]::placeholder { color: var(--text-faint); }

.field input[type="url"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 4px var(--accent-soft);
}

/* Model picker: a large, readable select on phones */
.model-select {
    min-height: 48px;
    font-family: var(--font-mono);
    font-size: var(--text-sm);
}

.field-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2) var(--space-3);
}

.field-head > label { margin-right: auto; }

.field-sub {
    margin-top: var(--space-2);
}

.field-title {
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--text);
    margin-top: var(--space-2);
}

.btn-sm {
    min-height: 36px;
    padding: 0 var(--space-4);
    font-size: var(--text-xs);
}

.custom-form { align-items: start; }

/* Sliders: larger touch target and a thumb that responds to a drag */
input[type="range"].range-input {
    -webkit-appearance: none;
    appearance: none;
    height: 44px;
    padding: 0;
    background: transparent;
}

input[type="range"].range-input::-webkit-slider-runnable-track {
    height: 6px;
    border-radius: var(--radius-pill);
    background: var(--border-strong);
}

input[type="range"].range-input::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 26px;
    height: 26px;
    margin-top: -10px;
    border-radius: 50%;
    border: 3px solid var(--bg-raised);
    background: var(--accent);
    box-shadow: var(--shadow-sm);
    transition: transform 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

input[type="range"].range-input::-moz-range-track {
    height: 6px;
    border-radius: var(--radius-pill);
    background: var(--border-strong);
}

input[type="range"].range-input::-moz-range-thumb {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    border: 3px solid var(--bg-raised);
    background: var(--accent);
    box-shadow: var(--shadow-sm);
    transition: transform 0.2s var(--ease), box-shadow 0.2s var(--ease);
}

input[type="range"].range-input:active::-webkit-slider-thumb {
    transform: scale(1.15);
    box-shadow: 0 0 0 6px var(--accent-soft);
}

input[type="range"].range-input:active::-moz-range-thumb {
    transform: scale(1.15);
    box-shadow: 0 0 0 6px var(--accent-soft);
}

.range-scale {
    display: flex;
    justify-content: space-between;
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--text-faint);
}

/* Free quota note below the composer */
.quota-note {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2) var(--space-3);
    margin-top: var(--space-2);
    padding: var(--space-2) var(--space-4);
    border-radius: var(--radius-pill);
    border: 1px solid var(--border);
    background: var(--surface);
    font-size: var(--text-xs);
    color: var(--text-soft);
}

.quota-btn {
    min-height: 32px;
    padding: 0 var(--space-3);
    border-radius: var(--radius-pill);
    border: 1px solid var(--accent);
    color: var(--accent-ink);
    font-size: var(--text-xs);
    font-weight: 600;
    transition: background 0.2s var(--ease);
}

.quota-btn:hover { background: var(--accent-soft); }

/* Active provider and model, shown next to the character count */
.active-model {
    min-width: 0;
    max-width: 70%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-family: var(--font-mono);
    color: var(--text-soft);
}

/* Provider line under each assistant message */
.msg-provider {
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--text-faint);
    margin-top: var(--space-1);
}

.msg-provider.is-backup {
    color: var(--accent-ink);
}

/* Call-to-action inside an error notice */
.msg-action.msg-action-cta {
    align-self: flex-start;
    min-height: 36px;
    margin-top: var(--space-3);
    padding: 0 var(--space-4);
    border-radius: var(--radius-pill);
    background: var(--accent);
    color: var(--on-accent);
    font-size: var(--text-sm);
    font-weight: 600;
}

.msg-action.msg-action-cta:hover {
    background: var(--accent-strong);
    color: var(--on-accent);
}

/* Custom API list */
.custom-list {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.custom-item {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: var(--surface);
}

.custom-item.is-active {
    border-color: var(--accent);
    box-shadow: inset 3px 0 0 var(--accent);
}

.custom-info {
    flex: 1 1 180px;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.custom-info strong {
    font-size: var(--text-sm);
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.custom-meta {
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--text-faint);
    overflow-wrap: anywhere;
}

.custom-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-1);
}

.custom-btn {
    min-height: 36px;
    padding: 0 var(--space-3);
    border-radius: var(--radius-pill);
    border: 1px solid var(--border-strong);
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--text);
    transition: background 0.2s var(--ease), border-color 0.2s var(--ease);
}

.custom-btn:hover {
    border-color: var(--accent);
    background: var(--accent-soft);
}

.custom-btn[data-custom-action="delete"] {
    color: var(--danger);
    border-color: var(--danger);
}

.custom-btn[data-custom-action="delete"]:hover {
    background: var(--danger-soft);
}

.custom-empty {
    font-size: var(--text-sm);
    color: var(--text-faint);
}

/* Items marked data-hide-touch are hidden on touch screens */
@media (hover: none) and (pointer: coarse) {
    [data-hide-touch] { display: none !important; }
}

/* Phone header: compact nav buttons so the brand (logo and name) is not clipped */
@media (max-width: 639px) {
    .nav { gap: 4px; }

    .nav-link,
    .icon-btn {
        width: 40px;
        height: 40px;
    }

}

/* ═══════════════════════════════════════════════════════
   Settings panel (opens over the chat)
   ═══════════════════════════════════════════════════════ */
.nav-link[aria-expanded="true"] {
    background: var(--accent-soft);
    color: var(--accent-ink);
}

.settings-overlay {
    position: fixed;
    inset: 0;
    z-index: 40;
    background: rgba(2, 6, 23, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s var(--ease);
}

.settings-overlay.open {
    opacity: 1;
    pointer-events: auto;
}

.settings-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    width: min(720px, 100%);
    display: flex;
    flex-direction: column;
    background: var(--bg);
    border-left: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
    transform: translateX(100%);
    visibility: hidden;
    transition: transform 0.3s var(--ease), visibility 0.3s;
}

.settings-panel.open {
    transform: translateX(0);
    visibility: visible;
}

.settings-panel .settings-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.panel-head {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-4) var(--space-4) var(--space-6);
    border-bottom: 1px solid var(--border);
    background: var(--bg-raised);
}

.panel-title {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-lg);
}

/* ═══════════════════════════════════════════════════════
   v1.2.5
   ═══════════════════════════════════════════════════════ */

/* Tables in replies: scroll sideways in their own box, columns keep their width */
.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: var(--space-3) 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
}

.table-wrap table {
    width: max-content;
    min-width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: var(--text-sm);
}

.table-wrap th,
.table-wrap td {
    padding: var(--space-2) var(--space-3);
    text-align: left;
    vertical-align: top;
    line-height: 1.5;
    max-width: 34ch;
    white-space: normal;
    overflow-wrap: break-word;
    border-bottom: 1px solid var(--border);
}

.table-wrap th {
    white-space: nowrap;
    font-weight: 600;
    background: var(--bg-raised);
}

.table-wrap tr:last-child td { border-bottom: 0; }

/* A reply's paragraphs, blocks, and tables fade in one after another */
@keyframes msgReveal {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}

.msg-reveal {
    animation: msgReveal 0.45s var(--ease) both;
    animation-delay: var(--reveal-delay, 0ms);
}

/* Model picker button next to the message field */
.model-picker-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
    max-width: 70%;
    min-height: 32px;
    padding: 0 var(--space-3);
    border-radius: var(--radius-pill);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-soft);
    font-size: var(--text-xs);
    transition: border-color 0.2s var(--ease);
}

.model-picker-btn:hover,
.model-picker-btn[aria-expanded="true"] { border-color: var(--accent); }

.model-picker-btn .active-model { max-width: 100%; }

.model-picker-btn .chevron {
    width: 12px;
    height: 12px;
    flex-shrink: 0;
}

/* Scroll Down button: a small dot marks replies that arrived while the user was reading above */
.jump-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent);
}

.jump-dot[hidden] { display: none; }

/* Model sheet (bottom drawer) */
.sheet-overlay {
    position: fixed;
    inset: 0;
    z-index: 45;
    background: rgba(2, 6, 23, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s var(--ease);
}

.sheet-overlay.open {
    opacity: 1;
    pointer-events: auto;
}

.sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 55;
    max-height: 78vh;
    display: flex;
    flex-direction: column;
    background: var(--bg-raised);
    border-top: 1px solid var(--border);
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    box-shadow: var(--shadow-lg);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    transform: translateY(105%);
    visibility: hidden;
    transition: transform 0.32s var(--ease), visibility 0.32s;
}

.sheet.open {
    transform: translateY(0);
    visibility: visible;
}

.sheet.is-dragging { transition: none; }

.sheet-grip {
    width: 56px;
    height: 24px;
    margin: 4px auto 0;
    cursor: grab;
    touch-action: none;
    position: relative;
}

.sheet-grip::after {
    content: "";
    position: absolute;
    left: 50%;
    top: 10px;
    width: 40px;
    height: 5px;
    transform: translateX(-50%);
    border-radius: var(--radius-pill);
    background: var(--border-strong);
}

.sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-1) var(--space-4) var(--space-3) var(--space-5);
    touch-action: none;
}

.sheet-title {
    font-size: var(--text-base);
    font-weight: 600;
}

.model-list {
    list-style: none;
    margin: 0;
    padding: 0 var(--space-3) var(--space-4);
    overflow-y: auto;
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}

.model-option {
    width: 100%;
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: 56px;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    border: 1px solid transparent;
    text-align: left;
    color: var(--text);
    transition: background 0.2s var(--ease), border-color 0.2s var(--ease);
}

.model-option:hover { background: var(--surface); }

.model-option[aria-selected="true"] {
    background: var(--accent-soft);
    border-color: var(--accent);
}

.model-option-text {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.model-option-name {
    font-size: var(--text-sm);
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.model-option-id {
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--text-faint);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.model-option-badge {
    flex-shrink: 0;
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--accent-ink);
}

/* Slider states: hover, focus, active, and dragging */
input[type="range"].range-input:hover::-webkit-slider-thumb { transform: scale(1.08); }
input[type="range"].range-input:hover::-moz-range-thumb { transform: scale(1.08); }

input[type="range"].range-input:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 4px var(--accent-soft); }
input[type="range"].range-input:focus-visible::-moz-range-thumb { box-shadow: 0 0 0 4px var(--accent-soft); }

input[type="range"].range-input.is-dragging::-webkit-slider-thumb {
    transform: scale(1.15);
    box-shadow: 0 0 0 6px var(--accent-soft);
    transition: none;
}

input[type="range"].range-input.is-dragging::-moz-range-thumb {
    transform: scale(1.15);
    box-shadow: 0 0 0 6px var(--accent-soft);
    transition: none;
}

/* ─── Motion and contrast preferences ─── */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

@media (prefers-contrast: more) {
    :root { --border-strong: currentColor; }
}
