:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #1c2430;
  --muted: #5b6676;
  --line: #dde2e8;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --error: #b3261e;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c;
    --card: #1b2129;
    --text: #e6eaf0;
    --muted: #9aa5b4;
    --line: #2d3640;
    --accent: #6ea2f5;
    --accent-text: #0d1117;
    --error: #ff8a80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}

.bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--card);
}

.brand { font-weight: 600; }
.who { color: var(--muted); }

main {
  max-width: 880px;
  margin: 24px auto;
  padding: 0 16px;
  display: grid;
  gap: 16px;
}

#app { display: grid; gap: 16px; }
#app[hidden], [hidden] { display: none !important; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 20px;
}

.narrow { max-width: 440px; width: 100%; margin: 0 auto; }

h1 { font-size: 1.3rem; margin: 0 0 8px; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }

form, .stack { display: grid; gap: 12px; }
label { display: grid; gap: 4px; font-weight: 500; }

input {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  width: 100%;
}

button {
  font: inherit;
  padding: 7px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--card);
  color: var(--text);
  cursor: pointer;
}

button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.link { border: none; background: none; color: var(--accent); padding: 0 4px; }
button:disabled { opacity: 0.6; cursor: default; }

.hint { color: var(--muted); font-weight: 400; font-size: 0.92em; }
.error { color: var(--error); margin: 0; }
.mono { font-family: ui-monospace, Consolas, monospace; font-size: 0.9em; overflow-wrap: anywhere; }

.facts {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 16px;
  margin: 0 0 12px;
}
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }

.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }

.list { list-style: none; padding: 0; margin: 0 0 12px; display: grid; gap: 8px; }
.list li { padding: 8px 0; border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }

details { margin-top: 8px; }
summary { cursor: pointer; color: var(--accent); }
details form { margin-top: 12px; }

.switch { display: flex; gap: 8px; align-items: center; }
.switch input { width: auto; }

/* ---------- tabs ---------- */
.bar { gap: 16px; }
.tabs { display: flex; gap: 4px; margin-right: auto; margin-left: 16px; }
.tab { border: none; background: none; padding: 6px 12px; border-radius: 6px; color: var(--muted); }
.tab.active { background: var(--bg); color: var(--text); font-weight: 600; }

main:has(#assistant-view:not([hidden])) { max-width: 1200px; }
.settings { display: grid; gap: 16px; }

/* ---------- assistant ---------- */
.assistant-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 16px;
  min-height: calc(100vh - 120px);
}
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}
.sidebar h3 { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 12px 0 0; }
.list.compact { margin: 0; gap: 2px; }
.list.compact li { display: flex; align-items: center; gap: 4px; padding: 4px 0; border: none; }
.list.compact .title { flex: 1; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 2px 0; color: var(--text); }
.list.compact .remove { color: var(--muted); }

.chat {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}
.conversation { flex: 1; display: flex; flex-direction: column; gap: 16px; overflow-y: auto; max-height: calc(100vh - 300px); }
.message { max-width: 100%; }
.message.user .body {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  white-space: pre-wrap;
  margin-left: auto;
  width: fit-content;
  max-width: 80%;
}
.message.assistant .body { overflow-wrap: anywhere; }
.message.assistant .body p { margin: 0 0 8px; }
.message.failed .body { color: var(--error); }
.message .hint { margin: 4px 0 0; }

.table-wrap { overflow-x: auto; margin: 8px 0 12px; }
.table-wrap table { border-collapse: collapse; font-size: 0.92em; }
.table-wrap th, .table-wrap td { border-bottom: 1px solid var(--line); padding: 4px 10px; text-align: left; white-space: nowrap; }
.table-wrap th { color: var(--muted); font-weight: 600; }
.chart { margin: 0 0 8px; max-width: 640px; }
.chart svg { width: 100%; height: auto; background: #ffffff; border-radius: 6px; }

.composer { display: grid; gap: 8px; }
.composer textarea {
  font: inherit;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  resize: vertical;
  width: 100%;
}
.composer select { font: inherit; padding: 6px; border-radius: 6px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
label.inline { display: flex; align-items: center; gap: 8px; font-weight: 400; }
.spacer { flex: 1; }
.notice { margin: 0; padding: 8px 12px; border-radius: 6px; background: var(--bg); border: 1px solid var(--line); }

@media (max-width: 760px) {
  .assistant-layout { grid-template-columns: 1fr; }
  .sidebar { order: 2; }
  .conversation { max-height: none; }
  .tabs { margin-left: 0; }
  #who-email { display: none; }
  .bar { padding: 10px 16px; gap: 8px; }
}

/* ---------- terms, licences ---------- */
dialog.terms {
  max-width: 720px;
  width: calc(100% - 32px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  padding: 20px;
}
dialog.terms::backdrop { background: rgb(0 0 0 / 0.4); }
.terms .summary { background: var(--bg); border-radius: 6px; padding: 10px 12px; }
.terms .summary p, .terms-body p { margin: 0 0 8px; }
.terms-body { max-height: 50vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 6px; padding: 8px 12px; font-size: 0.92em; }
.terms-body h3 { font-size: 1rem; margin: 12px 0 4px; }
#licensees input { width: 7em; padding: 4px 6px; }
#admin h3 { margin: 16px 0 4px; font-size: 1rem; }
/* Grid items default to min-width: auto, so a wide table would widen the page; let it scroll inside its card. */
.settings > .card, main > .card { min-width: 0; }

/* ---------- alerts ---------- */
#alerts td { vertical-align: top; }
#alerts .actions { white-space: nowrap; }
#alerts .attention { color: var(--error); white-space: normal; max-width: 260px; }
.card > .row h2 { margin: 0; }
fieldset { border: 1px solid var(--line); border-radius: 6px; padding: 12px; margin: 0; }
textarea { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); color: var(--text); width: 100%; }
select { font: inherit; padding: 6px; border-radius: 6px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
#outbox pre { white-space: pre-wrap; font: inherit; background: var(--bg); padding: 8px; border-radius: 6px; }
.chip { display: inline-flex; gap: 2px; align-items: center; border: 1px solid var(--line); border-radius: 999px; padding: 0 6px; margin: 2px; font-size: 0.9em; }
