/* ---- Themes ------------------------------------------------------------------
   Every color comes from these tokens. <html data-theme="..."> picks the preset;
   "system" follows the device's light/dark setting. */
:root, [data-theme="light"], [data-theme="system"] {
  --bg: #f4f6fa; --card: #ffffff; --ink: #111827; --muted: #64707f; --line: #e3e7ee;
  --brand: #1e4fd8; --brand-ink: #ffffff; --link: #1e4fd8; --soft: #eef3ff;
  --user: #1e4fd8; --user-ink: #ffffff;
  --warn-bg: #fff6e0; --warn-ink: #6b4a00; --err-bg: #fdecec; --err-ink: #8a1c1c;
  --ok-bg: #e6f6ec; --ok-ink: #14532d;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 2px 8px rgba(16, 24, 40, .05);
  color-scheme: light;
}
[data-theme="dark"] {
  --bg: #0d1117; --card: #161b22; --ink: #e6edf3; --muted: #8d97a3; --line: #272e38;
  --brand: #3f6ff0; --brand-ink: #ffffff; --link: #82a8ff; --soft: #1a2540;
  --user: #3f6ff0; --user-ink: #ffffff;
  --warn-bg: #3a2f12; --warn-ink: #f3d58a; --err-bg: #3b1717; --err-ink: #f5b5b5;
  --ok-bg: #12301f; --ok-ink: #b5ecc9;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  [data-theme="system"] {
    --bg: #0d1117; --card: #161b22; --ink: #e6edf3; --muted: #8d97a3; --line: #272e38;
    --brand: #3f6ff0; --brand-ink: #ffffff; --link: #82a8ff; --soft: #1a2540;
    --user: #3f6ff0; --user-ink: #ffffff;
    --warn-bg: #3a2f12; --warn-ink: #f3d58a; --err-bg: #3b1717; --err-ink: #f5b5b5;
    --ok-bg: #12301f; --ok-ink: #b5ecc9;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}
[data-theme="industrial"] {
  --bg: #16171a; --card: #202226; --ink: #ededed; --muted: #a2a4a8; --line: #33363b;
  --brand: #f28c28; --brand-ink: #17130e; --link: #ffaa55; --soft: #2d261d;
  --user: #3a3d43; --user-ink: #ffffff;
  --warn-bg: #3b2a10; --warn-ink: #ffd08a; --err-bg: #3d1a17; --err-ink: #ffb8ae;
  --ok-bg: #1d3020; --ok-ink: #bfe8c6;
  --shadow: 0 1px 2px rgba(0, 0, 0, .5);
  color-scheme: dark;
}
[data-theme="contrast"] {
  --bg: #000000; --card: #000000; --ink: #ffffff; --muted: #e2e2e2; --line: #ffffff;
  --brand: #ffd400; --brand-ink: #000000; --link: #ffd400; --soft: #000000;
  --user: #ffffff; --user-ink: #000000;
  --warn-bg: #000000; --warn-ink: #ffd400; --err-bg: #000000; --err-ink: #ff8080;
  --ok-bg: #000000; --ok-ink: #7dffa0;
  --shadow: none;
  color-scheme: dark;
}
[data-theme="contrast"] .msg, [data-theme="contrast"] .notice, [data-theme="contrast"] .error { border: 1px solid currentColor !important; }

/* ---- Base ------------------------------------------------------------------ */
:root { --radius: 16px; --radius-sm: 10px; }
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.55 "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1 { font-size: 1.15rem; margin: 0; letter-spacing: -.01em; }
h2 { letter-spacing: -.01em; }
a { color: var(--link); }
.muted { color: var(--muted); margin: .25rem 0 0; }
.small { font-size: .8rem; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
button {
  font: inherit; border: 0; border-radius: var(--radius-sm); padding: .7rem 1.15rem; cursor: pointer;
  background: var(--brand); color: var(--brand-ink); font-weight: 600; min-height: 44px;
  transition: filter .15s, background-color .15s;
}
button:hover { filter: brightness(1.08); }
button:disabled { opacity: .55; cursor: wait; }
:focus-visible { outline: 3px solid var(--brand); outline-offset: 2px; }
button.ghost, a.btn.ghost {
  background: transparent; color: var(--ink); border: 1px solid var(--line); padding: .45rem .85rem; min-height: 38px;
}
button.ghost:hover, a.btn.ghost:hover { background: var(--soft); filter: none; }
input, select, textarea {
  font: inherit; font-size: 16px; /* 16px stops iOS zoom-on-focus */
  padding: .6rem .75rem; border-radius: var(--radius-sm); border: 1px solid var(--line);
  background: var(--card); color: var(--ink); min-width: 0;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand); outline: none; box-shadow: 0 0 0 3px var(--soft); }

/* Logo / brand */
.brand { display: flex; align-items: center; gap: .65rem; color: inherit; text-decoration: none; }
.brand > span { display: grid; line-height: 1.2; }
.brand-name { font-weight: 700; font-size: 1.05rem; letter-spacing: -.01em; }
.brand-sub { margin: 0; }
.logo { display: inline-flex; flex: none; }
.logo-img { height: 40px; width: auto; }
.login .logo-img { height: 72px; }
/* Black-lettering logo on light themes, white-lettering logo on dark ones. */
.logo .on-dark { display: none; }
[data-theme="dark"] .logo .on-light, [data-theme="industrial"] .logo .on-light,
[data-theme="contrast"] .logo .on-light { display: none; }
[data-theme="dark"] .logo .on-dark, [data-theme="industrial"] .logo .on-dark,
[data-theme="contrast"] .logo .on-dark { display: block; }
@media (prefers-color-scheme: dark) {
  [data-theme="system"] .logo .on-light { display: none; }
  [data-theme="system"] .logo .on-dark { display: block; }
}

/* ---- Login ------------------------------------------------------------------ */
.login { min-height: 100%; display: grid; place-items: center; padding: 1rem; }
.card {
  width: 100%; max-width: 400px; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 1.5rem; display: grid; gap: .6rem; box-shadow: var(--shadow);
}
.card label { font-weight: 600; margin-top: .4rem; }
.login .brand { margin-bottom: .25rem; flex-direction: column; align-items: center; gap: .5rem; text-align: center; }
.login .brand + .muted { text-align: center; margin: 0 0 .5rem; text-wrap: balance; }
.error { color: var(--err-ink); background: var(--err-bg); padding: .6rem .8rem; border-radius: var(--radius-sm); margin: 0; }

/* ---- Chat ------------------------------------------------------------------ */
.app { height: 100%; height: 100dvh; display: flex; flex-direction: column; }
.bar {
  display: flex; justify-content: space-between; align-items: center; gap: 1rem;
  padding: calc(.7rem + env(safe-area-inset-top)) max(1rem, calc((100% - 1200px) / 2)) .7rem;
  border-bottom: 1px solid var(--line); background: var(--card);
}
.log {
  flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 1rem;
  padding: 1.25rem max(1rem, calc((100% - 1000px) / 2));
}
.msg { max-width: 92%; padding: .8rem 1rem; border-radius: var(--radius); overflow-wrap: anywhere; }
.msg p { margin: 0 0 .5rem; }
.msg > :last-child, .msg .body > :last-child { margin-bottom: 0; }
.msg.user {
  align-self: flex-end; background: var(--user); color: var(--user-ink);
  border-bottom-right-radius: 4px; white-space: pre-wrap;
}
.msg.bot {
  align-self: flex-start; background: var(--card); border: 1px solid var(--line);
  border-bottom-left-radius: 4px; box-shadow: var(--shadow);
}
.msg.pending { color: var(--muted); box-shadow: none; }
.msg.pending::after { content: ""; display: inline-block; width: 1.2em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }
.msg.pricing, .msg.missing, .msg.empty { background: var(--warn-bg); color: var(--warn-ink); border-color: transparent; box-shadow: none; }
.msg.error { background: var(--err-bg); color: var(--err-ink); border-color: transparent; box-shadow: none; }
.msg.empty a { color: inherit; font-weight: 600; }
.msg-title { font-weight: 700; }
.msg-head {
  display: flex; justify-content: space-between; align-items: center; gap: .5rem;
  margin: -.2rem 0 .4rem; font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em;
}
.copy {
  background: transparent; color: var(--muted); border: 1px solid var(--line); font-size: .75rem;
  font-weight: 600; padding: .2rem .55rem; min-height: 28px; text-transform: none; letter-spacing: 0;
}
.copy:hover { color: var(--ink); background: var(--soft); filter: none; }

/* Rendered answer text (built with DOM nodes in chat.js, never innerHTML) */
.body h3 { font-size: 1rem; margin: .6rem 0 .3rem; }
.body ul, .body ol { margin: .2rem 0 .6rem; padding-left: 1.3rem; }
.body li { margin: .15rem 0; }
.body li::marker { color: var(--muted); }
.cite {
  display: inline-block; font-size: .7rem; font-weight: 700; line-height: 1; vertical-align: .15em;
  padding: .2rem .35rem; margin: 0 .1rem; border-radius: 6px; background: var(--soft); color: var(--link); white-space: nowrap; flex: none;
  text-decoration: none;
}
.sources { margin-top: .75rem; padding-top: .6rem; border-top: 1px solid var(--line); font-size: .82rem; }
.sources-label { color: var(--muted); font-size: .72rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.sources ol { list-style: none; padding: 0; margin: .35rem 0 0; display: grid; gap: .3rem; }
.sources li { display: flex; flex-wrap: wrap; gap: .1rem .5rem; align-items: baseline; }
.sources li:target { background: var(--soft); border-radius: 6px; }
.src-file { color: var(--muted); font-size: .76rem; }

.chips { display: flex; flex-wrap: wrap; gap: .45rem; margin-top: .25rem; }
.chip {
  background: var(--soft); color: var(--ink); border: 1px solid var(--line); font-weight: 500; font-size: .87rem;
  padding: .4rem .8rem; min-height: 36px; border-radius: 999px;
}

.composer {
  background: var(--card); border-top: 1px solid var(--line); display: grid; gap: .5rem;
  padding: .7rem max(1rem, calc((100% - 1000px) / 2)) calc(.8rem + env(safe-area-inset-bottom));
}
.scope { display: flex; align-items: center; gap: .5rem; font-size: .85rem; }
.scope label { color: var(--muted); font-weight: 600; }
.picker { position: relative; flex: 1; max-width: 520px; display: flex; gap: .3rem; }
.picker input { flex: 1; min-width: 0; font-size: 14px; padding: .35rem .6rem; background: var(--bg); }
.picker-toggle { min-height: 34px; width: 38px; padding: 0; font-size: .9rem; }
.suggest { display: flex; flex-wrap: wrap; gap: .35rem; align-items: center; }
.suggest-label { font-size: .8rem; color: var(--muted); font-weight: 600; margin-right: .1rem; }
.suggest .chip { min-height: 30px; padding: .2rem .65rem; font-size: .8rem; }
.picker-list {
  position: absolute; bottom: calc(100% + .3rem); left: 0; right: 0; z-index: 20; margin: 0; padding: .3rem;
  list-style: none; max-height: 300px; overflow-y: auto; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius-sm); box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
}
.picker-list li { display: flex; justify-content: space-between; gap: .5rem; padding: .45rem .55rem; border-radius: 8px; cursor: pointer; font-size: .9rem; }
.picker-list li:hover, .picker-list li.active { background: var(--soft); }
.picker-list li.none { color: var(--muted); cursor: default; }
.doc-type { font-size: .7rem; font-weight: 700; color: var(--muted); border: 1px solid var(--line); border-radius: 5px; padding: 0 .3rem; align-self: center; }
.chip-btn { min-height: 32px; padding: .25rem .6rem; font-size: .8rem; }
.compose-row { display: flex; gap: .5rem; align-items: flex-end; }
.compose-row textarea { flex: 1; resize: none; max-height: 160px; background: var(--bg); padding: .7rem .85rem; }
@media (min-width: 700px) { .msg { max-width: 80%; } .msg.user { max-width: 65%; } }

/* ---- Account / admin pages --------------------------------------------------- */
.actions { display: flex; gap: .4rem; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.actions form { margin: 0; }
.btn {
  display: inline-flex; align-items: center; border-radius: var(--radius-sm); padding: .45rem .8rem; min-height: 38px;
  text-decoration: none; font-weight: 600;
}
button.danger { background: transparent; color: var(--err-ink); border: 1px solid var(--err-ink); padding: .45rem .8rem; min-height: 38px; }
.page { min-height: 100%; }
.content { padding: 1.25rem 1rem; display: grid; gap: 1rem; max-width: 1000px; margin: 0 auto; }
.content h2 { font-size: 1.05rem; margin: 0; }
.content .card { max-width: none; }
.stack { display: grid; gap: .6rem; }
.small-gap { gap: .4rem; }
.row { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.inline { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin: 0; }
.grid-form { display: grid; gap: .6rem; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); align-items: end; }
.grid-form label { display: grid; gap: .25rem; font-weight: 600; font-size: .9rem; }
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs a { padding: .6rem .9rem; text-decoration: none; color: var(--muted); border-bottom: 3px solid transparent; white-space: nowrap; }
.tabs a[aria-current="page"] { color: var(--ink); border-color: var(--brand); font-weight: 600; }
.notice { background: var(--warn-bg); color: var(--warn-ink); padding: .6rem .8rem; border-radius: var(--radius-sm); margin: 0; }
.notice.ok { background: var(--ok-bg); color: var(--ok-ink); }
.tag { background: var(--warn-bg); color: var(--warn-ink); border-radius: 6px; padding: .1rem .4rem; font-size: .8rem; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .6rem .5rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .78rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
details summary { cursor: pointer; color: var(--link); font-weight: 600; }
details[open] summary { margin-bottom: .5rem; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
code { font-size: .85em; background: var(--bg); padding: .1rem .3rem; border-radius: 4px; }

/* Theme picker */
.swatch { display: flex; height: 38px; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); }
.swatch i { flex: 1; }
.swatch[data-preview="system"] i:nth-child(1) { background: #f4f6fa; }
.swatch[data-preview="system"] i:nth-child(2) { background: #161b22; }
.swatch[data-preview="system"] i:nth-child(3) { background: #1e4fd8; }
.swatch[data-preview="light"] i:nth-child(1) { background: #f4f6fa; }
.swatch[data-preview="light"] i:nth-child(2) { background: #ffffff; }
.swatch[data-preview="light"] i:nth-child(3) { background: #1e4fd8; }
.swatch[data-preview="dark"] i:nth-child(1) { background: #0d1117; }
.swatch[data-preview="dark"] i:nth-child(2) { background: #161b22; }
.swatch[data-preview="dark"] i:nth-child(3) { background: #3f6ff0; }
.swatch[data-preview="industrial"] i:nth-child(1) { background: #16171a; }
.swatch[data-preview="industrial"] i:nth-child(2) { background: #202226; }
.swatch[data-preview="industrial"] i:nth-child(3) { background: #f28c28; }
.swatch[data-preview="contrast"] i:nth-child(1) { background: #000000; }
.swatch[data-preview="contrast"] i:nth-child(2) { background: #ffffff; }
.swatch[data-preview="contrast"] i:nth-child(3) { background: #ffd400; }

@media (max-width: 560px) {
  .bar { flex-wrap: wrap; }
  .brand-sub { display: none; }
}

/* Header: back link and theme menu */
a.btn.primary { background: var(--brand); color: var(--brand-ink); }
.menu { position: relative; }
.menu summary { list-style: none; cursor: pointer; color: var(--ink); border: 1px solid var(--line); }
.menu summary::-webkit-details-marker { display: none; }
.menu[open] summary { background: var(--soft); }
.menu > summary, .menu[open] > summary { margin: 0; }  /* the generic details spacing pushed it out of line */
.menu-list {
  position: absolute; right: 0; top: calc(100% + .35rem); z-index: 10; min-width: 220px; margin: 0;
  display: grid; gap: .15rem; padding: .35rem; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius-sm); box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
}
.menu-item {
  display: flex; align-items: center; gap: .6rem; width: 100%; text-align: left; background: transparent;
  color: var(--ink); font-weight: 500; padding: .45rem .55rem; min-height: 38px; border-radius: 8px;
}
.menu-item:hover { background: var(--soft); filter: none; }
.menu-item[aria-current="true"] { font-weight: 700; }
.menu-item[aria-current="true"]::after { content: "✓"; margin-left: auto; color: var(--link); }
.swatch.mini { height: 18px; width: 36px; flex: none; border-radius: 5px; }

/* Icon buttons in the header */
.icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon .dot { fill: currentColor; stroke: none; }
.icon { flex: none; }
a.btn.icon-btn, summary.btn.icon-btn, button.icon-btn {
  width: 42px; height: 42px; min-height: 42px; padding: 0; justify-content: center;
}
a.btn.icon-btn[aria-current="page"] { background: var(--soft); border-color: var(--brand); color: var(--link); }
@media (max-width: 560px) { .bar { flex-wrap: nowrap; } .brand-name { font-size: .95rem; } }
@media (max-width: 440px) {
  .brand > span { display: none; }  /* logo only, so the five icons fit on one row */
  .actions { gap: .3rem; }
}
.detail {
  margin-top: .5rem; padding-top: .5rem; border-top: 1px solid currentColor; font-size: .8rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre-wrap; opacity: .9;
}

/* Model API page */
.reveal { display: grid; gap: .3rem; }
.secret { display: block; padding: .6rem .75rem; border: 1px dashed var(--line); user-select: all; overflow-wrap: anywhere; font-size: .9rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

/* Sheet library */
.library-search { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.library-search input[type="search"] { flex: 1 1 280px; font-size: 16px; padding: .7rem .85rem; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius-sm); overflow: hidden; }
.seg-opt { margin: 0; cursor: pointer; }
.seg-opt input { position: absolute; opacity: 0; pointer-events: none; }
.seg-opt span { display: block; padding: .55rem .8rem; font-weight: 600; font-size: .9rem; color: var(--muted); }
.seg-opt input:checked + span { background: var(--soft); color: var(--ink); }
.seg-opt input:focus-visible + span { outline: 3px solid var(--brand); outline-offset: -3px; }
.results { list-style: none; margin: 0; padding: 0; display: grid; gap: .6rem; }
.result.card { padding: .9rem 1rem; gap: .4rem; }
.result-head { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.result-title { font-weight: 700; color: var(--ink); text-decoration: none; }
.result-title:hover { color: var(--link); text-decoration: underline; }
.excerpt { margin: 0; font-size: .88rem; color: var(--muted); overflow-wrap: anywhere; }
.excerpt mark { background: color-mix(in srgb, var(--brand) 22%, transparent); color: var(--ink); border-radius: 3px; padding: 0 .1rem; }
.result-actions { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; }
.result-actions .btn { min-height: 34px; padding: .3rem .7rem; font-size: .85rem; }
.result-actions .src-file { margin-left: auto; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .78rem; color: var(--muted); margin-left: auto; align-self: center; white-space: nowrap; }
.result-head .code { margin-left: 0; }
.tag.ocr { background: var(--soft); color: var(--link); }

/* Catalog tags and editing */
.tags { display: flex; flex-wrap: wrap; gap: .3rem; }
.chip-tag { font-size: .75rem; padding: .1rem .5rem; border-radius: 999px; background: var(--soft); color: var(--ink); border: 1px solid var(--line); }
.tag.edited { background: var(--ok-bg); color: var(--ok-ink); }
.row-actions { white-space: nowrap; }
.bulk { border: 1px solid var(--line); border-radius: var(--radius-sm); padding: .9rem; }
.bulk h3 { margin: 0; font-size: 1rem; }
.presets { display: flex; flex-wrap: wrap; gap: .4rem; margin: .25rem 0 .75rem; }
.preset { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 999px; background: var(--card); }
.preset a { padding: .35rem .75rem; color: var(--ink); text-decoration: none; font-weight: 600; font-size: .9rem; }
.preset.on { border-color: var(--brand); background: var(--soft); }
.preset.clear { padding: .35rem .75rem; color: var(--link); text-decoration: none; font-size: .9rem; }
.preset form { margin: 0; }
.preset-x { background: transparent; color: var(--muted); border: 0; min-height: 0; padding: 0 .6rem 0 0; font-size: 1.1rem; line-height: 1; }
.preset-save { margin: -.25rem 0 .75rem; }
.also-named { color: var(--muted); font-weight: 400; font-size: .9em; }
label.check { display: inline-flex; align-items: center; gap: .4rem; font-weight: 500; }
.row-actions form { display: inline; margin-left: .3rem; }
.row-actions .btn, .row-actions button { min-height: 34px; padding: .3rem .7rem; font-size: .85rem; }
.edit-form .hint { font-weight: 400; font-size: .78rem; color: var(--muted); }
.catalog-box { width: 100%; font-size: .85rem; resize: vertical; }
.facet-cols { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.facet-cols h3 { margin: 0 0 .3rem; text-transform: uppercase; letter-spacing: .04em; }
ul.plain { list-style: none; margin: 0; padding: 0; display: grid; gap: .25rem; }
details > summary { margin-bottom: .4rem; }
