/* Mobile first: the base layout is for phones; from 900 px the panel stays fixed on the left. */
:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1d2330;
  --muted: #6b7385;
  --border: #e2e5eb;
  /* The collection's colors (_control/ui.json) override these four */
  --brand-accent: #2f6fde;
  --brand-accent-text: #ffffff;
  --brand-accent-dark: #5b8ff0;
  --brand-accent-text-dark: #0d1117;
  --accent: var(--brand-accent);
  --accent-text: var(--brand-accent-text);
  --user-bg: color-mix(in srgb, var(--accent) 12%, var(--panel));
  --code-bg: #f0f2f5;
  --hover: #eef1f5;
  --ok: #2e8b57;
  --warn: #c27c0e;
  --err: #c0392b;
  --radius: 12px;
  --gutter: 16px;
  --shadow: 0 8px 30px rgba(15, 20, 30, 0.18);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15181e;
    --panel: #1d2129;
    --text: #e4e7ee;
    --muted: #949bab;
    --border: #2d323d;
    --accent: var(--brand-accent-dark);
    --accent-text: var(--brand-accent-text-dark);
    --user-bg: color-mix(in srgb, var(--accent) 22%, var(--panel));
    --code-bg: #262b35;
    --hover: #262b35;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
h2 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
[hidden] { display: none !important; }

button { font: inherit; cursor: pointer; border-radius: 10px; border: none; min-height: 44px; }
button:disabled { opacity: 0.5; cursor: default; }
button:focus-visible, select:focus-visible, textarea:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.primary { background: var(--accent); color: var(--accent-text); padding: 10px 14px; }
.secondary { background: transparent; color: var(--text); border: 1px solid var(--border); padding: 8px 12px; }
.wide { width: 100%; }
.icon-btn {
  width: 44px; height: 44px; display: inline-grid; place-items: center;
  background: transparent; color: var(--text); flex-shrink: 0;
}
.icon-btn:hover { background: var(--hover); }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

select, textarea {
  width: 100%;
  font: inherit;
  font-size: 16px; /* avoids the iOS auto-zoom on focus */
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}

/* ---------------------------------------------------------- layout */
.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto 1fr;
  height: 100vh;
  height: 100dvh;
}
.topbar {
  display: flex; align-items: center; gap: 4px;
  padding: 4px 8px;
  padding-top: max(4px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.topbar-title {
  min-width: 0; margin: 0; font-size: 1rem; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center;
}

.drawer {
  position: fixed; inset: 0 auto 0 0; z-index: 20;
  width: min(86vw, 340px);
  display: flex; flex-direction: column; gap: 20px;
  padding: var(--gutter);
  padding-top: max(var(--gutter), env(safe-area-inset-top));
  padding-bottom: max(var(--gutter), env(safe-area-inset-bottom));
  background: var(--panel);
  overflow-y: auto;
  transform: translateX(-100%);
  transition: transform 0.22s ease;
  box-shadow: none;
}
.drawer.open { transform: none; box-shadow: var(--shadow); }
.drawer-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.brand { font-weight: 700; font-size: 1.1rem; line-height: 1.25; }
.brand-block { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-icon { width: 28px; height: 28px; border-radius: 7px; flex-shrink: 0; object-fit: contain; }
.brand-icon.large { width: 40px; height: 40px; border-radius: 10px; }
.brand-icon.hero { width: 64px; height: 64px; border-radius: 16px; margin-bottom: 8px; }
.topbar-brand { flex: 1; min-width: 0; display: flex; align-items: center; justify-content: center; gap: 8px; }
.notice { color: var(--muted); margin: 0; padding-top: 12px; border-top: 1px solid var(--border); }
.footer-notice {
  margin: 0; padding: 0 var(--gutter) max(8px, env(safe-area-inset-bottom));
  background: var(--panel); color: var(--muted); font-size: 0.72rem; line-height: 1.35; text-align: center;
}
.backdrop { position: fixed; inset: 0; z-index: 10; background: rgba(0, 0, 0, 0.35); }
@media (prefers-reduced-motion: reduce) { .drawer { transition: none; } }

.main { display: flex; flex-direction: column; min-height: 0; }
.messages { flex: 1; overflow-y: auto; padding: var(--gutter); scroll-behavior: smooth; }
.empty { text-align: center; margin-top: 14vh; padding: 0 8px; }
.empty h2 { font-size: 1.25rem; text-transform: none; letter-spacing: 0; color: var(--text); }
.empty > p { max-width: 560px; margin-left: auto; margin-right: auto; }
.suggestions { display: flex; flex-direction: column; gap: 8px; max-width: 560px; margin: 8px auto 0; }
.suggestion {
  text-align: left; padding: 10px 14px; background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.suggestion:hover { border-color: var(--accent); background: var(--user-bg); }
.grow { flex: 1; }

/* ---------------------------------------------------------- panel */
.panel select { margin-bottom: 4px; }
.buttons { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.source-card { border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; margin-bottom: 8px; }
.source-card b { display: block; margin-bottom: 2px; }
.findings summary { cursor: pointer; color: var(--muted); margin-top: 4px; }
.findings ul { margin: 6px 0 0; padding-left: 18px; max-height: 40vh; overflow-y: auto; }
.findings li { margin-bottom: 4px; overflow-wrap: anywhere; }
.findings li b { display: inline; font-weight: 600; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.dot.ok { background: var(--ok); } .dot.running { background: var(--warn); } .dot.error { background: var(--err); }
.meter { height: 6px; border-radius: 3px; background: var(--border); overflow: hidden; margin-top: 4px; }
.meter span { display: block; height: 100%; background: var(--accent); }
.meter.high span { background: var(--warn); }

.conv-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.conv-item { display: flex; align-items: center; border-radius: 10px; }
.conv-item.active { background: var(--user-bg); }
.conv-item:not(.active):hover { background: var(--hover); }
.conv-open {
  flex: 1; min-width: 0; text-align: left; background: transparent; color: var(--text);
  padding: 8px 10px; display: flex; flex-direction: column;
}
.conv-open .t { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-open .d { font-size: 0.75rem; color: var(--muted); }
.conv-del { color: var(--muted); }
.conv-del svg { width: 18px; height: 18px; }

/* ---------------------------------------------------------- messages */
.msg { margin: 0 auto 18px; max-width: 860px; }
.bubble { padding: 10px 14px; border-radius: var(--radius); background: var(--panel); border: 1px solid var(--border); overflow-x: auto; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user-bg); border: none; margin-left: auto; max-width: 88%; width: fit-content; white-space: pre-wrap; }
.bubble p { margin: 0 0 8px; } .bubble p:last-child { margin-bottom: 0; }
.bubble pre { background: var(--code-bg); padding: 10px; border-radius: 8px; overflow-x: auto; }
.bubble code { background: var(--code-bg); padding: 1px 4px; border-radius: 4px; font-size: 0.9em; }
.bubble pre code { padding: 0; background: none; }
.bubble table { border-collapse: collapse; margin: 8px 0; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 { font-size: 1rem; margin: 12px 0 6px; text-transform: none; letter-spacing: 0; color: var(--text); }
.bubble ul, .bubble ol { padding-left: 22px; margin: 6px 0; }
.bubble .answer { font-size: 1.05rem; line-height: 1.45; }
.bubble .label { display: flex; align-items: center; gap: 6px; font-size: 0.75rem; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase; color: var(--accent); margin-bottom: 2px; }
.bubble .icon { font-size: 1.1rem; line-height: 1; letter-spacing: 0; }
.bubble .basis { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--border); font-size: 0.92rem; color: var(--muted); }
.bubble .basis summary { cursor: pointer; min-height: 32px; display: flex; align-items: center; }
.bubble .basis summary .label { margin: 0; }
.bubble .basis ul, .bubble .basis ol { margin: 4px 0; }
.bubble .conclusion { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--border); font-size: 0.95rem; }
.cite { color: var(--accent); font-weight: 600; font-size: 0.85em; }
.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; font-size: 0.78rem; color: var(--muted); margin-top: 4px; }
.meta button { min-height: 32px; padding: 2px 8px; background: transparent; color: var(--muted); border: 1px solid var(--border); font-size: 0.78rem; }
.error { color: var(--err); }

.rate { display: inline-flex; gap: 2px; }
.meta .rate-btn { width: 32px; padding: 0; display: inline-grid; place-items: center; border: none; }
.rate-btn svg { width: 17px; height: 17px; }
.rate-btn.down svg { transform: rotate(180deg); }
.rate-btn[aria-pressed="true"] { color: var(--accent); background: var(--user-bg); }
.rate-btn.down[aria-pressed="true"] { color: var(--err); }
.feedback-form { margin-top: 8px; padding: 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel); }
.feedback-form p { margin: 0 0 8px; }
.feedback-form textarea { margin: 8px 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { min-height: 34px; padding: 4px 12px; border-radius: 999px; background: transparent; color: var(--text); border: 1px solid var(--border); font-size: 0.85rem; }
.chip[aria-pressed="true"] { background: var(--user-bg); border-color: var(--accent); }
.feedback-form .row { display: flex; justify-content: flex-end; gap: 8px; }

.sources { margin-top: 8px; font-size: 0.85rem; }
.sources summary { cursor: pointer; color: var(--muted); padding: 6px 0; min-height: 32px; }
.sources ol { margin: 6px 0 0; padding-left: 22px; }
.sources li { margin-bottom: 4px; overflow-wrap: anywhere; }
.sources a { color: var(--accent); text-decoration: none; }
.sources a:hover { text-decoration: underline; }

/* ---------------------------------------------------------- user context (ui.json context_fields) */
.context-bar {
  display: flex; gap: 8px; overflow-x: auto; padding: 8px var(--gutter) 0;
  background: var(--panel); border-top: 1px solid var(--border);
}
.context-bar + .composer { border-top: none; }
.ctx { display: flex; flex-direction: column; flex: 1 1 0; min-width: 140px; max-width: 320px; }
.ctx span { font-size: 0.72rem; color: var(--muted); padding: 0 2px 2px; }
.ctx select { padding: 6px 10px; min-height: 38px; background: var(--bg); }
.ctx select.set { border-color: var(--accent); background: var(--user-bg); }

/* ---------------------------------------------------------- composer */
.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px var(--gutter);
  border-top: 1px solid var(--border);
  background: var(--panel);
}
.composer textarea { resize: none; max-height: 40vh; line-height: 1.4; }
.send-btn { width: 46px; height: 46px; flex-shrink: 0; display: grid; place-items: center; background: var(--accent); color: var(--accent-text); }
.send-btn.stop { background: var(--err); }
.send-btn.stop svg { fill: currentColor; stroke: none; }

.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* ---------------------------------------------------------- desktop */
@media (min-width: 900px) {
  .app { grid-template-columns: 300px 1fr; grid-template-rows: 1fr; }
  .topbar { display: none; }
  .drawer {
    position: static; width: auto; transform: none; box-shadow: none;
    border-right: 1px solid var(--border); grid-row: 1; grid-column: 1;
  }
  .main { grid-row: 1; grid-column: 2; }
  #close-drawer, .backdrop { display: none !important; }
  .messages { padding: 24px max(24px, calc((100% - 860px) / 2)); }
  .composer { padding: 14px max(24px, calc((100% - 860px) / 2)) 8px; }
  .footer-notice { padding: 0 max(24px, calc((100% - 860px) / 2)) 12px; }
  .context-bar { padding: 10px max(24px, calc((100% - 860px) / 2)) 0; }
  .suggestions { display: grid; grid-template-columns: 1fr 1fr; }
  button { min-height: 38px; }
  .icon-btn { width: 38px; height: 38px; }
}

/* ---------------------------------------------------------- administration panel (/admin) */
body.admin { overflow-y: auto; overscroll-behavior: auto; }
.admin-bar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px var(--gutter); padding-top: max(10px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.admin-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.link-btn { display: inline-flex; align-items: center; min-height: 44px; border-radius: 10px; text-decoration: none; }
.admin-main { padding: var(--gutter); max-width: 1200px; margin: 0 auto; }
.admin-grid { display: grid; gap: var(--gutter); grid-template-columns: 1fr; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: var(--gutter); min-width: 0; }
.card h3 { font-size: 0.9rem; margin: 14px 0 6px; }
.card p { margin: 6px 0; }
.gate { max-width: 440px; margin: 10vh auto 0; }
.gate .row { display: flex; gap: 8px; }
input[type="password"] {
  flex: 1; min-width: 0; font: inherit; font-size: 16px; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
}
input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
ul.plain { list-style: none; margin: 0; padding: 0; }
ul.plain li { margin-bottom: 6px; overflow-wrap: anywhere; }
.doc-list { max-height: 50vh; overflow-y: auto; }
@media (min-width: 900px) {
  .admin-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .wide-card { grid-column: 1 / -1; }
  .link-btn { min-height: 38px; }
}
