:root {
  --bg: #f6f7f9; --card: #ffffff; --text: #1b1f24; --muted: #5d6673; --line: #dfe3e8; --soft: #eef1f5;
  --accent: #2459d8; --accent-text: #ffffff; --danger: #b3261e; --ok: #1d7a46; --user: #e8eefc;
  --radius: 12px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101317; --card: #181c21; --text: #e8ebef; --muted: #9aa4b1; --line: #2a3038; --soft: #1f242b;
    --accent: #6c93ff; --accent-text: #0b1020; --danger: #ff8a80; --ok: #6fd39a; --user: #1d2a44;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
main { max-width: 760px; margin: 0 auto; padding: 24px 16px 64px; }
body.signed-in main { max-width: none; margin: 0; padding: 0; }
h1, h2, h3, h4, h5 { line-height: 1.25; margin: 0 0 8px; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.15rem; } h3, h4, h5 { font-size: 1rem; }
p { margin: 0 0 12px; }
.muted { color: var(--muted); }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---- shared controls ---- */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px; margin-bottom: 16px;
}
.auth { max-width: 420px; margin: 12vh auto 0; }
label { display: block; font-weight: 600; margin: 12px 0 6px; }
input[type=email], input[type=text], input[type=password], input:not([type]), textarea {
  width: 100%; padding: 10px 12px; font: inherit; color: inherit; background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px;
}
input.code { font-size: 1.5rem; letter-spacing: .35em; text-align: center; }
button, a.button {
  font: inherit; font-weight: 600; cursor: pointer; border-radius: 8px; padding: 10px 16px;
  border: 1px solid var(--accent); background: var(--accent); color: var(--accent-text); text-decoration: none;
  display: inline-block;
}
button.secondary, a.button.secondary { background: transparent; color: var(--text); border-color: var(--line); }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
button.link { background: none; border: none; color: var(--accent); padding: 4px 0; font-weight: 500; }
button.small { padding: 6px 10px; font-size: .9rem; }
button:disabled { opacity: .55; cursor: not-allowed; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 16px; }
.row.end { justify-content: flex-end; }
.row.flush { margin-top: 0; }
.row.between { justify-content: space-between; }
.error { color: var(--danger); margin: 10px 0 0; }
.notice { color: var(--ok); margin: 10px 0 0; }
.pill { display: inline-block; font-size: .8rem; padding: 2px 10px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); }
.pill.ok { color: var(--ok); border-color: var(--ok); }
.pill.warn { color: var(--danger); border-color: var(--danger); }
.pill.gap { margin-left: 8px; }
.checks { border: none; padding: 0; margin: 12px 0 0; }
.checks legend { padding: 0; }
.checks label { font-weight: 500; display: flex; gap: 8px; align-items: center; margin: 6px 0; }
ul.plain { list-style: none; padding: 0; margin: 0; }
ul.plain li { padding: 12px 0; border-top: 1px solid var(--line); display: flex; justify-content: space-between; gap: 12px; align-items: center; }
ul.plain li:first-child { border-top: none; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { text-align: left; padding: 8px 6px; border-top: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; border-top: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); color: var(--text); max-width: 380px; width: calc(100% - 32px); }
dialog::backdrop { background: rgba(0,0,0,.45); }

/* ---- the signed-in shell: top bar / two panels / composer ---- */
.shell { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; height: 100vh; height: 100dvh; }
header.top {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line);
  background: var(--card);
}
.brand { font-weight: 700; }
.status { font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.top-actions button { white-space: nowrap; }
.label-narrow { display: none; }
.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20; min-width: 180px;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 6px;
}
.menu button { display: block; width: 100%; text-align: left; background: none; border: none; color: var(--text); padding: 8px 10px; font-weight: 500; }
.menu button:hover { background: var(--soft); }
.menu .danger-text { color: var(--danger); }

.split { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.2fr); min-height: 0; }
.convo { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); }
.log { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 100%; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); border-radius: 12px 12px 4px 12px; padding: 8px 12px; max-width: 85%; }
.user-text { white-space: pre-wrap; }
.msg .about { font-size: .8rem; color: var(--muted); margin-bottom: 2px; }
.msg.assistant p:last-child { margin-bottom: 0; }
.msg.assistant ul, .msg.assistant ol { margin: 0 0 12px; padding-left: 22px; }
.msg.assistant pre { background: var(--soft); padding: 10px; border-radius: 8px; overflow-x: auto; }
.msg.assistant code { font-size: .9em; background: var(--soft); padding: 1px 4px; border-radius: 4px; }
.msg.assistant pre code { background: none; padding: 0; }
.msg.live { white-space: pre-wrap; }
.activity { font-size: .85rem; color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.error-line { color: var(--danger); font-size: .95rem; }
.log .card { padding: 12px 14px; margin: 0; max-width: 420px; }
.card-kind { font-size: .8rem; }
.card-title { font-weight: 600; }
.log .card .row { margin-top: 6px; }

.viewer { display: flex; flex-direction: column; min-height: 0; background: var(--card); }
.viewer-head { display: none; align-items: center; gap: 10px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.viewer-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tabs { display: flex; gap: 2px; overflow-x: auto; border-bottom: 1px solid var(--line); padding: 0 8px; flex: none; }
.viewer.empty .tabs { display: none; }
.tab { display: flex; align-items: center; border-bottom: 3px solid transparent; flex: none; max-width: 240px; }
.tab.active { border-bottom-color: var(--accent); }
.tab-btn { background: none; border: none; color: var(--muted); padding: 10px 6px 10px 10px; font-weight: 500; max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab.active .tab-btn { color: var(--text); }
.tab-close { background: none; border: none; color: var(--muted); padding: 4px 8px; font-weight: 400; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-right: 6px; vertical-align: middle; }
.pane { flex: 1; overflow-y: auto; padding: 20px; min-height: 0; }
.pane-empty { max-width: 360px; margin: 15vh auto 0; text-align: center; }
.doc h2 { font-size: 1.2rem; }
.meta { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 0 0 16px; color: var(--muted); font-size: .92rem; }
.meta dt { font-weight: 600; } .meta dd { margin: 0; overflow-wrap: anywhere; }
.body-text { white-space: pre-wrap; overflow-wrap: anywhere; border-top: 1px solid var(--line); padding-top: 14px; }

.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: 96px; z-index: 30; margin: 0;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 14px; box-shadow: var(--shadow); }
.toast.error { color: var(--danger); border-color: var(--danger); }
.toast.ok { color: var(--ok); }

.composer { border-top: 1px solid var(--line); background: var(--card); padding: 10px 16px 12px; }
.chip-row:empty { display: none; }
.chip-row { margin-bottom: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; font-size: .85rem; background: var(--soft); border: 1px solid var(--line); border-radius: 999px; padding: 2px 4px 2px 10px; max-width: 100%; }
.chip-x { background: none; border: none; color: var(--muted); padding: 0 6px; font-weight: 400; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; line-height: 1.4; max-height: 180px; }

ul.facts li.fact { align-items: flex-start; flex-wrap: wrap; }
.fact-main { flex: 1 1 260px; min-width: 0; overflow-wrap: anywhere; }
.fact-actions { justify-content: flex-end; }
.small-text { font-size: .82rem; }
.add-row select, .add-row input { width: auto; flex: 1 1 140px; padding: 8px 10px; font: inherit; color: inherit; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; }

/* medium screens: same layout, even split */
@media (max-width: 999px) { .split { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }

/* phones: the viewer becomes a full-screen next step above the composer */
@media (max-width: 699px) {
  .split { grid-template-columns: minmax(0, 1fr); }
  .convo { border-right: none; }
  .status { display: none; }
  .label-wide { display: none; }
  .label-narrow { display: inline; }
  header.top { padding: 8px 12px; }
  .viewer { display: none; }
  body.viewer-open .viewer { display: flex; position: fixed; inset: 0 0 auto 0; bottom: var(--composer-h, 72px); z-index: 10; }
  body.viewer-open .viewer-head { display: flex; }
  .msg.user { max-width: 92%; }
  .pane { padding: 16px; }
}
