/* Palette comes from byte-design.css (loaded first); these keep the app's own variable names. Theme: data-theme on <html> or system. */
:root {
  --bg: var(--byte-bg); --surface: var(--byte-card); --surface-2: var(--byte-pill); --text: var(--byte-fg); --muted: var(--byte-muted);
  --border: var(--byte-border); --accent: var(--byte-accent); --accent-text: var(--byte-on-accent); --danger: var(--byte-danger); --ok: var(--byte-done);
  --user: var(--byte-pill);
  --user: color-mix(in srgb, var(--byte-accent) 16%, var(--byte-card)); --radius: var(--byte-radius-l); --focus: var(--byte-accent);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { font-size: 100%; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 1rem/1.5 var(--byte-font-ui); }
:root {
  --seg-a-end: env(viewport-segment-right 0 0, 50vw);
  --seg-b-start: env(viewport-segment-left 1 0, 50vw);
  --hinge: calc(var(--seg-b-start) - var(--seg-a-end));
  --layout-top: 3.5rem; --split: 40; --gutter: 0.75rem; --rail-w: 18rem; --target: 2.75rem;
}
:focus-visible { outline: 2px solid var(--byte-accent); outline-offset: 2px; box-shadow: var(--byte-focus); }
.sr-only, .skip:not(:focus) { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip:focus { position: fixed; top: 8px; left: 8px; z-index: 50; background: var(--surface); padding: 8px 12px; border-radius: 6px; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--border); background: var(--surface-2); border-radius: var(--byte-radius); padding: 0.5rem 0.875rem; font-weight: 600; color: var(--byte-head); min-height: var(--target); min-width: var(--target); touch-action: manipulation; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.55; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; }
.btn { display: inline-flex; align-items: center; min-height: var(--target); padding: 0.375rem 0.75rem; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); color: var(--accent); text-decoration: none; font-size: 0.9rem; }
.error { color: var(--danger); min-height: 1.5em; margin: 0; }
.login-status { color: var(--muted); min-height: 1.5em; margin: 0; font-size: 0.875rem; }
.hint { color: var(--muted); }

.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { box-sizing: border-box; width: min(380px, 100%); max-width: 100%; min-width: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; display: grid; gap: 12px; }
.login-card h1 { margin: 0 0 4px; font-size: 1.4rem; color: var(--byte-head); }
.login-card { box-shadow: var(--byte-shadow); }
.login-card label { font-size: 0.8125rem; font-weight: 600; color: var(--muted); }
.login-card input, .composer textarea { box-sizing: border-box; min-width: 0; width: 100%; padding: 10px 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--byte-radius); }
.login-card input:focus, .composer textarea:focus { border-color: var(--accent); }
.login-card input::placeholder, .composer textarea::placeholder { color: var(--muted); }

/* ---------- shell ---------- */
.app { display: flex; flex-direction: column; height: 100dvh; padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left); }
.top { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; padding: 0.375rem 0.75rem; border-bottom: 1px solid var(--border); background: var(--byte-bar); }
.top h1 { font-size: 1.1rem; margin: 0; color: var(--byte-head); letter-spacing: 0.02em; }
.top-actions { display: flex; gap: 0.375rem; flex-wrap: wrap; justify-content: flex-end; }
#swap { display: none; }
[data-layout="tabletop"] #swap { display: inline-block; }
.layout { flex: 1; min-height: 0; display: grid; gap: 0; padding: 0.75rem; position: relative; }
.chat, .panel, .rail { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden; }
.chat { grid-area: chat; container: chat / inline-size; }
.panel { grid-area: media; container: media / inline-size; }
.rail { grid-area: rail; margin-inline-start: 0.75rem; }
.splitter { grid-area: split; display: none; position: relative; cursor: col-resize; touch-action: none; background: transparent; border: 0; }
.splitter::before { content: ""; position: absolute; inset: 0.5rem 0.2rem; border-radius: 999px; background: var(--border); }
.splitter::after { content: ""; position: absolute; inset: 0 -1rem; } /* enlarges the touch target to >= 44px */
.splitter:hover::before, .splitter:focus-visible::before, .splitter.drag::before { background: var(--accent); }
.log-wrap { position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column; }
.jump-latest { position: absolute; right: 0.75rem; bottom: 0.75rem; z-index: 3; background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; box-shadow: var(--byte-shadow); border-radius: var(--byte-radius-pill); padding-inline: 1rem; }
.jump-latest:focus-visible { outline: 2px solid var(--byte-accent); outline-offset: 2px; box-shadow: var(--byte-shadow), var(--byte-focus); }
.log { flex: 1; min-height: 0; padding: 0.75rem; overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 0.625rem; }
.composer { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: end; gap: 0.5rem; padding: 0.75rem; padding-bottom: max(0.75rem, env(safe-area-inset-bottom)); border-top: 1px solid var(--border); background: var(--surface); }
.composer textarea { resize: none; min-height: var(--target); max-height: 8rem; field-sizing: content; }
.composer-buttons { display: flex; gap: 0.5rem; }
.composer textarea { grid-column: 2; }
.composer-buttons { grid-column: 3; flex-wrap: wrap; justify-content: flex-end; }
.voice-group { display: inline-flex; gap: 0.5rem; }
.voice-status { grid-column: 1 / -1; margin: 0; min-height: 1.25rem; font-size: 0.875rem; color: var(--muted); }
.voice-status:empty { display: none; }
.voice-status.error { color: var(--danger); }
button.mic.recording { background: var(--danger); color: #fff; border-color: var(--danger); animation: mic-pulse 1.2s ease-in-out infinite; }
button.mic.busy { opacity: 0.7; }
@keyframes mic-pulse { 50% { box-shadow: 0 0 0 6px rgba(179, 38, 30, 0.25); } }

/* ---------- banner, mini-player, bottom switch ---------- */
.confirm-banner { display: flex; align-items: center; gap: 0.75rem; padding: 0.625rem 0.75rem; background: var(--accent); color: var(--accent-text); }
.confirm-banner-text { margin: 0; flex: 1; font-weight: 700; font-size: 1.05rem; overflow-wrap: anywhere; }
.confirm-banner button { background: var(--accent-text); color: var(--accent); border-color: var(--accent-text); min-height: 3rem; }
.miniplayer { display: flex; align-items: center; gap: 0.5rem; padding: 0.375rem 0.75rem; border-top: 1px solid var(--border); background: var(--surface); }
.mp-info { flex: 1; min-width: 0; display: grid; }
.mp-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.9rem; }
.mp-info input[type="range"] { width: 100%; min-height: var(--target); margin: 0; accent-color: var(--accent); }
.tabs { display: none; grid-template-columns: 1fr 1fr; gap: 0.25rem; padding: 0.375rem 0.75rem; padding-bottom: max(0.375rem, env(safe-area-inset-bottom)); border-top: 1px solid var(--border); background: var(--surface); }
.tabs button { position: relative; min-height: 3rem; font-weight: 600; }
.tabs button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.dot { position: absolute; top: 0.5rem; margin-inline-start: 0.375rem; width: 0.75rem; height: 0.75rem; border-radius: 50%; background: var(--danger); border: 2px solid var(--surface); }
.rail-list { list-style: none; margin: 0; padding: 0.5rem 0.75rem; overflow-y: auto; display: grid; gap: 0.375rem; align-content: start; }
.rail-list li { display: grid; padding: 0.375rem 0.5rem; border: 1px solid var(--border); border-radius: var(--byte-radius); overflow-wrap: anywhere; }
.rail-list li.bad { border-color: var(--danger); }
.rail-list small { color: var(--muted); }
.panel-body { flex: 1; min-height: 0; }

/* ---------- COMPACT: one pane + bottom switch ---------- */
[data-layout="compact"] .layout { grid-template: "main" minmax(0, 1fr) / minmax(0, 1fr); padding: 0.5rem; }
[data-layout="compact"] .chat, [data-layout="compact"] .panel { grid-area: main; }
[data-layout="compact"][data-view="chat"] .panel, [data-layout="compact"][data-view="media"] .chat { display: none; }
[data-layout="compact"] .tabs { display: grid; }
[data-layout="compact"] .composer { padding-bottom: 0.75rem; }
[data-layout="compact"] .msg { max-width: 94%; }

/* ---------- MEDIUM + EXPANDED: two panes (draggable split), optional rail ---------- */
[data-layout="medium"] .layout, [data-layout="expanded"] .layout {
  grid-template: "chat split media" minmax(0, 1fr) / calc((100% - var(--gutter)) * var(--split) / 100) var(--gutter) minmax(0, 1fr);
}
[data-layout="expanded"][data-rail-on="1"] .layout {
  grid-template: "chat split media rail" minmax(0, 1fr) / calc((100% - var(--gutter) - var(--rail-w) - 0.75rem) * var(--split) / 100) var(--gutter) minmax(0, 1fr) var(--rail-w);
}
[data-layout="medium"] .splitter, [data-layout="expanded"] .splitter { display: block; }
[data-layout="medium"] .composer, [data-layout="expanded"] .composer { padding-bottom: max(0.75rem, env(safe-area-inset-bottom)); }
[data-layout]:not([data-layout="compact"]) .miniplayer { padding-bottom: max(0.375rem, env(safe-area-inset-bottom)); }

/* ---------- BOOK: vertical hinge, chat left, media right, nothing under the hinge ---------- */
[data-layout="book"] .app { padding-left: 0; padding-right: 0; }
[data-layout="book"] .layout { padding: 0; grid-template: "chat . media" minmax(0, 1fr) / var(--seg-a-end) var(--hinge) minmax(0, 1fr); }
[data-layout="book"] .chat { margin: 0.75rem 1rem 0.75rem 0.75rem; }
[data-layout="book"] .panel { margin: 0.75rem 0.75rem 0.75rem 1rem; }
[data-layout="book"] .top { padding-inline: 0.75rem; }
[data-layout="book"] .miniplayer { padding-inline-start: calc(var(--seg-b-start) + 0.75rem); }

/* ---------- TABLETOP (horizontal hinge): chat top / media bottom, or the reverse ---------- */
[data-layout="tabletop"] .layout { padding: 0; grid-template-columns: minmax(0, 1fr); grid-template-areas: none;
  grid-template-rows: calc(var(--seg-a-end) - var(--layout-top)) var(--hinge) minmax(0, 1fr); }
[data-layout="tabletop"] .chat { grid-area: auto; grid-row: 1; grid-column: 1; margin: 0.5rem 0.75rem; }
[data-layout="tabletop"] .panel { grid-area: auto; grid-row: 3; grid-column: 1; margin: 0.5rem 0.75rem; }
[data-layout="tabletop"][data-order="media-first"] .chat { grid-row: 3; }
[data-layout="tabletop"][data-order="media-first"] .panel { grid-row: 1; }
[data-layout="tabletop"] .composer { padding-bottom: max(0.75rem, env(safe-area-inset-bottom)); }

/* ---------- container-driven details (independent of any fold API) ---------- */
@container media (min-width: 34rem) { .grid { grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr)); } }
@container chat (min-width: 38rem) { .msg { max-width: 80%; } }
[data-tiny] .top h1 { font-size: 1rem; }
[data-tiny] .composer-buttons button { padding-inline: 0.5rem; }
.msg { max-width: 92%; padding: 8px 12px; border-radius: var(--radius); overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--surface-2); }
.msg.error { align-self: stretch; max-width: none; color: var(--danger); border: 1px solid var(--danger); background: transparent; }
.md p, .md ul, .md ol, .md pre, .md h3, .md h4, .md h5, .md h6 { margin: 0 0 8px; }
.md > :last-child { margin-bottom: 0; }
.md pre { background: var(--surface-2); padding: 8px; border-radius: 6px; overflow-x: auto; }
.md code { background: var(--surface-2); font-family: var(--byte-font-mono); padding: 0 4px; border-radius: 4px; }
.md a { color: var(--byte-link); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0; }
.chip { font-size: 0.8rem; padding: 2px 10px; border-radius: var(--byte-radius-pill); border: 1px solid var(--border); background: var(--surface-2); color: var(--muted); }
.chip.ok { color: var(--ok); border-color: var(--ok); }
.chip.fail { color: var(--danger); border-color: var(--danger); }
.chip.write::before { content: "write \00b7  "; }

.confirm { border: 3px solid var(--accent); border-radius: var(--byte-radius-l); padding: 0.75rem 0.875rem; margin: 0.375rem 0; background: var(--surface); }
.confirm:not(.done) { position: sticky; bottom: 0; z-index: 2; box-shadow: 0 -0.25rem 1rem rgba(0, 0, 0, 0.25); }
.confirm .row button { flex: 1 1 8rem; min-height: 3rem; font-size: 1.05rem; }
.confirm h3 { margin: 0 0 4px; font-size: 1rem; }
.confirm p { margin: 0 0 8px; white-space: pre-wrap; }
.confirm pre { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 0.85rem; background: var(--bg); padding: 6px; border-radius: 6px; }
.confirm .ptext-stats { margin: 0 0 4px; font-size: 0.85rem; color: var(--muted); }
.confirm .ptext-stats.warn { color: var(--text); font-weight: 600; }
.confirm .ptext-body { max-height: 40vh; overflow: auto; margin: 0 0 8px; border: 1px solid var(--border); }
.confirm .hl-url { background: #ffe08a; color: #3a2a00; text-decoration: underline; border-radius: 3px; }
.confirm .hl-na { background: #ffc9c9; color: #5a0000; border-radius: 3px; }
.confirm .hl-zw { background: #c9d7ff; color: #00205a; border-radius: 3px; font-size: 0.75rem; }
.confirm .row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.confirm .status { margin-top: 8px; color: var(--muted); }
.confirm.done { border-color: var(--border); }

.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; border-bottom: 1px solid var(--border); background: var(--byte-bar); }
.panel-head h2 { margin: 0; font-size: 1rem; }
.panel-body { padding: 12px; display: grid; gap: 14px; overflow-y: auto; }
.pitem { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; background: var(--byte-card); }
.pitem h3, .panel-head h2, .grid-title { color: var(--byte-head); }
.pitem h3 { margin: 0 0 4px; font-size: 1rem; }
.pitem h3, .grid-title { overflow-wrap: anywhere; }
.pitem .cap { margin: 0 0 8px; white-space: pre-wrap; overflow-wrap: anywhere; color: var(--muted); }
.pitem ul { margin: 0 0 8px; padding-left: 18px; }
.news-storage-notice { margin: 0 0 8px; padding: 8px 10px; border: 1px solid color-mix(in srgb, var(--byte-warn) 50%, var(--byte-border)); border-inline-start: 3px solid var(--byte-warn); background: color-mix(in srgb, var(--byte-warn) 8%, var(--byte-card)); }
.pitem li small { display: block; color: var(--muted); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr)); gap: 0.5rem; }
.grid button { padding: 0; min-height: 0; border-radius: 0.5rem; overflow: hidden; aspect-ratio: 1; background: var(--surface-2); }
.grid img { width: 100%; height: 100%; object-fit: cover; display: block; }
.grid-title { margin: 0 0 6px; font-size: 1rem; }
.player audio { width: 100%; }
.queue { list-style: none; padding: 0; margin: 8px 0 0; display: grid; gap: 4px; }
.queue button { width: 100%; text-align: left; background: transparent; }
.player audio { min-height: var(--target); }
.queue button[aria-current="true"] { border-color: var(--accent); font-weight: 600; }

.lightbox { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.92); display: grid; grid-template: minmax(0, 1fr) / auto minmax(0, 1fr) auto; align-items: center; gap: 0.5rem; padding: max(1rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right)) max(1rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left)); z-index: 40; }
.lb-fig { margin: 0; text-align: center; min-width: 0; height: 100%; display: grid; grid-template-rows: minmax(0, 1fr) auto; }
.lb-fig img { width: 100%; height: 100%; min-height: 0; object-fit: contain; }
.lb-fig figcaption { color: #e8ebf2; margin-top: 0.5rem; overflow-wrap: anywhere; padding-inline: 0.5rem; }
.lb-btn { background: #242a36; color: #e8ebf2; border-color: #4a5468; min-width: var(--target); }
/* On a folded display the viewer takes the larger segment, never the hinge */
[data-layout="book"][data-big="0"] .lightbox { inset: 0 auto 0 0; width: var(--seg-a-end); }
[data-layout="book"][data-big="1"] .lightbox { inset: 0 0 0 var(--seg-b-start); }
[data-layout="tabletop"][data-big="0"] .lightbox { inset: 0 0 auto 0; height: var(--seg-a-end); }
[data-layout="tabletop"][data-big="1"] .lightbox { inset: var(--seg-b-start) 0 0 0; }
.lb-close { position: absolute; top: max(0.75rem, env(safe-area-inset-top)); right: max(0.75rem, env(safe-area-inset-right)); z-index: 1; }

/* ---------- map + reader panel items ---------- */
.map { margin: 0; display: grid; gap: 0.375rem; }
.map-stage { position: relative; width: 100%; aspect-ratio: 4 / 3; overflow: hidden; border: 1px solid var(--border); border-radius: var(--byte-radius); background: #e6e2d8; isolation: isolate; }
.map-stage.no-tiles { background: repeating-linear-gradient(0deg, transparent 0 63px, rgba(0, 0, 0, 0.12) 63px 64px), repeating-linear-gradient(90deg, transparent 0 63px, rgba(0, 0, 0, 0.12) 63px 64px), #e6e2d8; }
.map-tiles, .map-overlay { position: absolute; inset: 0; width: 100%; height: 100%; }
.map-tiles img { position: absolute; display: block; max-width: none; user-select: none; -webkit-user-drag: none; }
.map-overlay { pointer-events: none; }
.map-route { stroke: #1a5fd4; stroke-width: 4; stroke-linejoin: round; stroke-linecap: round; opacity: 0.9; }
.map-marker circle { fill: #d93025; stroke: #fff; stroke-width: 2; }
.map-marker-n { fill: #fff; font: 700 9px system-ui, sans-serif; }
.map-attrib { position: absolute; right: 0; bottom: 0; z-index: 2; padding: 0.125rem 0.375rem; font-size: 0.7rem; line-height: 1.3; background: rgba(255, 255, 255, 0.85); color: #222; border-top-left-radius: 0.375rem; }
.map-attrib a { color: #0b4fb3; }
.map-bar { display: flex; align-items: center; gap: 0.375rem; }
.map-zoom { color: var(--muted); font-size: 0.85rem; }
.map-load { position: absolute; left: 50%; top: 50%; z-index: 2; transform: translate(-50%, -50%); min-height: 2.75rem; padding: 0 1.25rem; }
.map-note { margin: 0; font-size: 0.85rem; color: var(--muted); }
.map-legend { margin: 0; padding-left: 1.25rem; font-size: 0.9rem; overflow-wrap: anywhere; }
.reader-pos { margin: 0 0 4px; font-size: 0.85rem; color: var(--muted); }
.reader-bar { width: 100%; height: 0.5rem; accent-color: var(--accent); }
.reader-text { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 22rem; overflow-y: auto; margin: 0.5rem 0; padding: 0.625rem 0.75rem; background: var(--surface); border: 1px solid var(--border); border-radius: 0.5rem; font: 1.05rem/1.65 var(--byte-font-read); }
.reader-nav { display: flex; gap: 0.5rem; }
.reader-nav button { flex: 1 1 0; min-height: 3rem; }
.reader-status { min-height: 0; }

@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; transition: none !important; animation: none !important; } }

/* write proposals in the Results panel */
.proposals { display: flex; flex-direction: column; gap: 0.5rem; max-height: 65vh; overflow: auto; }
.proposals:empty { display: none; }
.proposals .confirm { margin: 0.375rem 0; position: static; box-shadow: none; max-height: none; }
.proposal[data-state="saved"] { border-color: var(--ok, #2e7d32); }
.proposal[data-state="error"] { border-color: var(--danger, #c62828); }
.proposal .proposal-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0 0; }
.proposal .proposal-actions:empty { display: none; }
.proposal .proposal-actions .btn, .proposal .proposal-actions button { min-height: 2.75rem; }
.proposal .proposal-body { max-height: 45vh; overflow: auto; }
.proposed { margin: 0.25rem 0; }
.proposed button { min-height: 2.75rem; }
@media (max-width: 768px) { button, .btn, input:not([type="range"]), textarea { min-height: var(--byte-touch); } }
