/* ===== Appearance controls (theme / accent / chat width) ===== */
.appearance-options {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 12px;
}

.appearance-options button {
    flex: 1;
    min-width: 72px;
    padding: 8px 10px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    font-size: var(--text-xs);
    cursor: pointer;
    transition: background-color 0.2s, border-color 0.2s, color 0.2s;
}

.appearance-options button:hover {
    color: var(--text-primary);
    border-color: var(--accent);
}

.appearance-options button.active {
    background: var(--accent-subtle);
    border-color: var(--accent);
    color: var(--accent);
}

/* Theme buttons preview their own palette: a small dot split between the
   theme's background and its accent. Colors are hardcoded per theme because
   each button shows a palette other than the active one. */
#themeOptions button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    /* The generic .appearance-options rule sets min-width: 72px, which lets a
       button shrink below its content — so a long label ("Parchment") plus the
       swatch dot spilled outside the button. fit-content keeps each button at
       least as wide as its own label+dot; flex:1 still grows them to fill the
       row, and flex-wrap drops overflow onto the next line. */
    min-width: fit-content;
    white-space: nowrap;
}

#themeOptions button::before {
    content: '';
    flex: 0 0 auto;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 1px solid rgba(128, 128, 128, 0.45);
    background: linear-gradient(135deg, var(--swatch-bg) 50%, var(--swatch-accent) 50%);
}

#themeOptions button[data-theme-name="parchment"] { --swatch-bg: #f4ede0; --swatch-accent: #9c4a2f; }
#themeOptions button[data-theme-name="alabaster"] { --swatch-bg: #f5f6f8; --swatch-accent: #3b5bdb; }
#themeOptions button[data-theme-name="camellia"]  { --swatch-bg: #f8f1f3; --swatch-accent: #a83258; }
#themeOptions button[data-theme-name="celadon"]   { --swatch-bg: #eef3ef; --swatch-accent: #2d6a4f; }
#themeOptions button[data-theme-name="espresso"]  { --swatch-bg: #1b1714; --swatch-accent: #d98a4b; }
#themeOptions button[data-theme-name="nocturne"]  { --swatch-bg: #181b24; --swatch-accent: #7b84f7; }
#themeOptions button[data-theme-name="verdant"]   { --swatch-bg: #161f19; --swatch-accent: #57b889; }
#themeOptions button[data-theme-name="abyssal"]   { --swatch-bg: #101b24; --swatch-accent: #3bbbc9; }

/* Files-in-context: number input + trailing label (FC-03b) */
.active-file-turns-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}
.active-file-turns-row input[type="number"] {
    width: 72px;
}

/* ===== Reset & Base ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

::selection {
    background: var(--accent-subtle);
    color: var(--text-primary);
}

body {
    font-family: var(--font-main);
    background-color: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    height: 100vh; /* fallback for browsers without dvh */
    height: 100dvh; /* dynamic viewport: accounts for mobile browser chrome */
    overflow: hidden;
}

/* Ensure the HTML5 `hidden` attribute always wins over component display rules */
[hidden] {
    display: none !important;
}

/* Keyboard focus ring — shows only for keyboard navigation, not mouse clicks.
   :where() keeps specificity low so components can still opt out if needed. */
:where(button, a, input, select, textarea, [tabindex]):focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ===== Form controls =====
   ONE field appearance for the whole app. Before this, the same look was
   written out three times — `.modal-body input`, `.settings-section
   input/select/textarea`, `.key-popover-input` — each scoped to a container,
   so a field rendered anywhere else (the per-model param cards, a popover at
   body level) fell back to the browser default: white, square, Arial.

   `:where()` pins the specificity at (0,0,1), the lowest possible. Every
   context rule in the later files therefore wins by ordinary document order
   and only has to state what it actually changes — which in practice is
   spacing and width, not appearance. Add a field anywhere and it is already
   right; override only when that context genuinely differs.

   Note the file order IS the cascade (see CLAUDE.md): base.css loads second,
   so these are the defaults everything else refines. Width is deliberately
   absent — `.param-row-inline` wants 150px and `.key-popover-input` wants
   `flex: 1`, so full-width stays a context decision. */
:where(button) {
    font-family: var(--font-main);
}

input:where(:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"])),
select,
textarea {
    padding: 10px 12px;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-family: var(--font-main);
    font-size: var(--text-sm);
    transition: border-color 0.2s;
}

input:where(:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"])):focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: var(--accent);
}

input:where(:disabled),
select:where(:disabled),
textarea:where(:disabled) {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Checkboxes are DRAWN, not tinted. `accent-color` only recolors the OS
   widget: it keeps its native shape and border, which never matched our cards
   and fields. This replaces five near-duplicate rules that had drifted to
   three different sizes (15/16/18px) plus three sites with no styling at all.
   One 16px box everywhere, built from the same tokens as the fields above. */
input[type="checkbox"] {
    appearance: none;
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    flex-shrink: 0;
    margin: 0;
    display: inline-grid;
    place-content: center;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    transition: background-color 0.15s, border-color 0.15s;
}

/* Guarded rather than set on the rule above: a bare `cursor: pointer` there
   would outrank the generic :where(:disabled) rule and show a pointer on
   disabled boxes. Leaving disabled cursors to the low-specificity default also
   lets a context override it (the file rows use `progress` while saving). */
input[type="checkbox"]:not(:disabled) {
    cursor: pointer;
}

/* The tick is a clip-pathed box filled via inset box-shadow, so it inherits a
   single color and needs no SVG asset or extra element. */
input[type="checkbox"]::before {
    content: '';
    width: 10px;
    height: 10px;
    transform: scale(0);
    transition: transform 0.12s ease-out;
    box-shadow: inset 1em 1em var(--on-accent);
    clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
}

input[type="checkbox"]:checked {
    background-color: var(--accent);
    border-color: var(--accent);
}

input[type="checkbox"]:checked::before {
    transform: scale(1);
}

input[type="checkbox"]:not(:disabled):hover {
    border-color: var(--accent);
}

/* Sliders, same treatment: the appearance lived in `.palette-row` and
   `.param-group` only, so the per-model param cards got a native track. */
input:where([type="range"]) {
    height: 6px;
    border-radius: 3px;
    background: var(--bg-tertiary);
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}

/* Vendor thumb pseudo-elements must each get their OWN rule — a browser drops
   the whole rule when any selector in a comma-joined list is unrecognized. */
input:where([type="range"])::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    transition: transform 0.1s ease;
}

input:where([type="range"])::-webkit-slider-thumb:hover {
    transform: scale(1.1);
}

input:where([type="range"])::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: none;
}

/* Respect users who prefer reduced motion: near-instant transitions/animations. */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
        scroll-behavior: auto !important;
    }
}

/* ===== App Layout ===== */
.app-container {
    display: flex;
    height: 100vh; /* fallback */
    height: 100dvh;
}

/* ===== Sidebar ===== */
.sidebar {
    /* Cap at 85vw so a wide desktop width can't overflow a phone's drawer. */
    width: min(var(--sidebar-width), 85vw);
    background-color: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    position: fixed;
    left: calc(-1 * var(--sidebar-width));
    top: 0;
    height: 100vh; /* fallback */
    height: 100dvh;
    z-index: 100;
    box-shadow: var(--shadow-lg); /* lifts the off-canvas drawer (mobile); removed on desktop */
    transition: left 0.3s ease;
    overflow-y: auto;
}

.sidebar.open {
    left: 0;
}

/* Sidebar resize handle — only meaningful on desktop, where the sidebar is
   an in-flow column. Hidden on mobile (sidebar is an off-canvas overlay). */
.sidebar-resize-handle {
    display: none;
}

@media (min-width: 768px) {
    .sidebar {
        border-right: none;
    }

    .sidebar-resize-handle {
        display: block;
        flex: 0 0 5px;
        margin-left: -2px;
        align-self: stretch;
        cursor: col-resize;
        position: relative;
        z-index: 50;
        touch-action: none;
    }

    .sidebar-resize-handle::before {
        content: '';
        position: absolute;
        top: 0;
        bottom: 0;
        left: 2px;
        width: 1px;
        background: var(--border-color);
        transition: background-color 0.15s ease, width 0.15s ease, left 0.15s ease;
    }

    .sidebar-resize-handle:hover::before,
    .sidebar-resize-handle.dragging::before {
        background: var(--accent);
        width: 3px;
        left: 1px;
    }
}

.close-sidebar {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    transition: background-color 0.2s, color 0.2s;
}

.close-sidebar:hover {
    background-color: var(--bg-tertiary);
    color: var(--text-primary);
}

/* ===== Section rail (WR-07, enriched UIP-03) ===== */
.rail {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    user-select: none;
}

.rail-top {
    padding: 10px 8px 6px;
    flex: 0 0 auto;
}

.rail-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 4px 4px 10px;
    gap: 6px;
    min-height: 42px;
}

.rail-brand {
    font-family: var(--font-brand);
    font-style: italic;
    font-size: 24px;
    font-weight: 500;
    letter-spacing: -0.015em;
    line-height: 1.1;
    color: var(--text-primary);
    user-select: none;
}

.rail-header-actions {
    display: flex;
    align-items: center;
    gap: 2px;
}

.rail-icon-btn {
    width: 36px;
    height: 36px;
    display: grid;
    place-items: center;
    padding: 0;
    border: none;
    border-radius: 9px;
    background: none;
    cursor: pointer;
    color: var(--text-secondary);
    transition: background-color 0.15s, color 0.15s;
}

.rail-new-btn {
    color: var(--accent);
}

.rail-new-btn:hover {
    background-color: var(--accent-subtle);
    color: var(--accent);
}

.rail-new-btn svg {
    width: 25px;
    height: 25px;
    display: block;
}

.rail-header .close-sidebar {
    font-size: 19px;
    line-height: 1;
    margin-left: 0;
    padding: 0;
}

.rail-nav {
    display: flex;
    flex-direction: column;
    gap: 1px;
    padding: 0 8px;
    flex: 0 0 auto;
}

.rail-item {
    display: flex;
    align-items: center;
    gap: 11px;
    height: 36px;
    padding: 0 10px;
    border: none;
    border-radius: 9px;
    background: none;
    color: var(--text-secondary);
    font-family: var(--font-main);
    font-size: 13.5px;
    font-weight: 500;
    cursor: pointer;
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    transition: background-color 0.15s, color 0.15s;
}

.rail-item span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.rail-item:hover {
    background-color: var(--bg-tertiary);
    color: var(--text-primary);
}

.rail-item.active {
    background-color: var(--accent-subtle);
    color: var(--accent);
}

.rail-icon {
    width: 18px;
    height: 18px;
    flex: 0 0 18px;
}

/* Recent chats block */
.rail-recent {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    margin-top: 8px;
    border-top: 1px solid var(--border-color);
    padding-top: 8px;
}

.rail-recent[hidden] {
    display: none;
}

.rail-recent-title {
    padding: 2px 18px 6px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-muted);
    flex: 0 0 auto;
}

.rail-recent-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 3px 8px 8px;
}

.rail-recent-scroll::-webkit-scrollbar {
    width: 8px;
}

.rail-recent-scroll::-webkit-scrollbar-track {
    background: var(--bg-secondary);
}

.rail-recent-scroll::-webkit-scrollbar-thumb {
    background: transparent;
    border-radius: 999px;
    border: 2px solid var(--bg-secondary);
    min-height: 24px;
}

.rail-recent-scroll:hover::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
}

.rail-recent-scroll::-webkit-scrollbar-thumb:hover {
    background: var(--scrollbar-thumb-hover);
}

.rail-recent-scroll::-webkit-scrollbar-button,
.rail-recent-scroll::-webkit-scrollbar-button:single-button:vertical:decrement,
.rail-recent-scroll::-webkit-scrollbar-button:single-button:vertical:increment {
    display: none;
    width: 0;
    height: 0;
}

.rail-recent-group-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px 3px;
    font-size: 11.5px;
    font-weight: 600;
    color: var(--text-secondary);
}

.rail-recent-group-icon {
    width: 14px;
    height: 14px;
    flex: 0 0 14px;
    color: var(--text-muted);
}

.rail-recent-group-head span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.rail-recent-project-sub {
    padding: 5px 10px 2px 28px;
    font-size: 11px;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.rail-chat-row {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    min-height: 34px;
    padding: 5px 10px 5px 28px;
    border: none;
    border-radius: 8px;
    background: none;
    color: var(--text-secondary);
    font-family: var(--font-main);
    text-align: left;
    cursor: pointer;
    position: relative;
    transition: background-color 0.12s, color 0.12s;
}

[data-recent-style="flat"] .rail-chat-row {
    padding: 6px 10px 6px 14px;
    align-items: flex-start;
}

.rail-chat-row:hover {
    background-color: var(--bg-tertiary);
    color: var(--text-primary);
}

.rail-chat-row.open {
    background-color: color-mix(in srgb, var(--accent) 10%, transparent);
    color: var(--text-primary);
}

:where(:root:not([data-theme])) .rail-chat-row.open,
[data-theme="alabaster"] .rail-chat-row.open,
[data-theme="camellia"] .rail-chat-row.open,
[data-theme="celadon"] .rail-chat-row.open {
    background-color: color-mix(in srgb, var(--accent) 7%, transparent);
}

.rail-chat-row.open:hover {
    background-color: var(--bg-tertiary);
}

.rail-chat-row.open::before {
    content: '';
    position: absolute;
    left: 16px;
    top: 7px;
    bottom: 7px;
    width: 2px;
    border-radius: 2px;
    background: var(--accent);
}

[data-recent-style="flat"] .rail-chat-row.open::before {
    left: 4px;
}

.rail-chat-ava {
    width: 18px;
    height: 18px;
    border-radius: 5px;
    flex: 0 0 18px;
    display: grid;
    place-items: center;
    overflow: hidden;
    background-color: var(--bg-tertiary);
}

[data-recent-style="flat"] .rail-chat-ava {
    margin-top: 1px;
}

.rail-chat-ava img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.rail-chat-ava .avatar-emoji {
    font-size: 12px;
    line-height: 1;
    display: block;
}

.rail-chat-body {
    min-width: 0;
    flex: 1;
    display: block;
}

.rail-chat-title {
    display: block;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.25;
}

.rail-chat-ctx {
    display: none;
    font-size: 11px;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 1px;
}

[data-recent-style="flat"] .rail-chat-ctx {
    display: block;
}

/* Bottom footer (Profile + Settings) */
.rail-footer {
    margin-top: auto;
    flex: 0 0 auto;
    border-top: 1px solid var(--border-color);
    padding: 6px 8px 8px;
    display: flex;
    flex-direction: column;
    gap: 1px;
}

/* Phone drawer touch targets & comfortable drawer width */
@media (max-width: 767px) {
    .sidebar {
        width: min(max(var(--sidebar-width), 260px), 85vw);
        left: calc(-1 * min(max(var(--sidebar-width), 260px), 85vw));
    }

    .rail-icon-btn {
        width: 44px;
        height: 44px;
    }

    .rail-item {
        height: 44px;
    }

    .rail-chat-row {
        min-height: 44px;
    }
}

/* Font buttons preview their own typeface in Appearance settings (UIP-01). */
#fontFamilyOptions button {
    min-width: fit-content;
    white-space: nowrap;
}
#fontFamilyOptions button[data-font-family="inter"]          { font-family: 'Inter', sans-serif; }
#fontFamilyOptions button[data-font-family="jakarta"]        { font-family: 'Plus Jakarta Sans', sans-serif; }
#fontFamilyOptions button[data-font-family="newsreader"]     { font-family: 'Newsreader', Georgia, serif; font-size: 13px; }
#fontFamilyOptions button[data-font-family="source-serif"]   { font-family: 'Source Serif 4', Georgia, serif; font-size: 13px; }
#fontFamilyOptions button[data-font-family="atkinson"]       { font-family: 'Atkinson Hyperlegible', sans-serif; }
#fontFamilyOptions button[data-font-family="jetbrains-mono"] { font-family: 'JetBrains Mono', monospace; }
#fontFamilyOptions button[data-font-family="system"]         { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif; }

