:root {
  /* LevelMaster brand: red #e30613, navy #1e163e (from the logo) */
  --brand-red: #e30613;
  --brand-navy: #1e163e;
  --bg: #f7f6f3;
  --panel: #ffffff;
  --line: #e4e1da;
  --fg: #22303a;
  --muted: #6e7a84;
  --accent: var(--brand-red);
  --accent-soft: #fbe9ea;
  --link: #2b4a7a;
  --warn-soft: #fbf0e0;
  --warn: #a8732a;
  --ok-soft: #e6f2ea;
  --ok: #3f7d59;
  --user-bubble: #eceaf3;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
/* Dark palette: applied by the toggle (html[data-theme]) or, before the script runs, by the system setting. */
:root[data-theme="dark"] {
  --bg: #171b1f; --panel: #1f252b; --line: #2d353d; --fg: #e6e9ec; --muted: #98a3ac;
  --accent: #ed1c24; --accent-soft: #3a2426; --link: #9bb8dc; --warn-soft: #3a3024; --warn: #e0b36a;
  --ok-soft: #223b2c; --ok: #8fcaa5; --user-bubble: #2a3238;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171b1f; --panel: #1f252b; --line: #2d353d; --fg: #e6e9ec; --muted: #98a3ac;
    --accent: #ed1c24; --accent-soft: #3a2426; --link: #9bb8dc; --warn-soft: #3a3024; --warn: #e0b36a;
    --ok-soft: #223b2c; --ok: #8fcaa5; --user-bubble: #2a3238;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 16px/1.5 var(--font); background: var(--bg); color: var(--fg); }
a { color: var(--link); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

/* ---------- generic ---------- */
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 8px 14px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { filter: brightness(.92); }
.btn.danger { color: #a33; }
.btn:disabled { opacity: .5; cursor: default; }
.field { display: grid; gap: 4px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--muted); }
.field input, .field select { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 10px 12px; width: 100%; }
.error { color: #a33; font-size: 14px; min-height: 1.2em; }
.muted { color: var(--muted); font-size: 13px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 500; }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; background: var(--accent-soft); color: var(--accent); }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
code { background: var(--accent-soft); padding: 1px 5px; border-radius: 5px; font-size: 13px; }

/* ---------- centred auth pages ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth .card { width: 100%; max-width: 380px; }
.auth h1 { font-size: 20px; margin: 0 0 4px; }
.brand { font-weight: 600; letter-spacing: .2px; }
.brand span { color: var(--accent); }
.logo { height: 36px; width: auto; display: block; }
.logo.dark { display: none; }
:root[data-theme="dark"] .logo.light { display: none; }
:root[data-theme="dark"] .logo.dark { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .logo.light { display: none; }
  :root:not([data-theme="light"]) .logo.dark { display: block; }
}
.auth .logo { height: 40px; margin: 0 auto 14px; }
.theme-toggle { font-size: 16px; line-height: 1; padding: 7px 10px; }

/* ---------- app shell ---------- */
.shell { height: 100%; display: grid; grid-template-rows: auto 1fr auto; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
.topbar .grow { flex: 1; }
.topbar select { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 6px 10px; min-width: 0; max-width: 50vw; }
.thread { overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: min(760px, 100%); width: fit-content; min-width: 0; padding: 10px 14px; border-radius: var(--radius); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user-bubble); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg .tools { display: block; margin-top: 6px; font-size: 12px; color: var(--muted); }
.msg.toolnote { background: none; border: 0; padding: 0 6px; }
.msg.toolnote .tools { margin: 0; }
.msg.thinking { color: var(--muted); font-style: italic; }
.confirm { align-self: flex-start; max-width: min(760px, 100%); background: var(--warn-soft); border: 1px solid var(--warn); border-radius: var(--radius); padding: 14px; }
.confirm h4 { margin: 0 0 6px; color: var(--warn); font-size: 14px; }
.confirm pre { white-space: pre-wrap; margin: 0 0 10px; font-family: var(--font); }
.confirm .actions { display: flex; gap: 8px; }
.composer { display: flex; flex-direction: column; gap: 6px; padding: 10px 12px; border-top: 1px solid var(--line); background: var(--panel); padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.composer .row-inputs { display: flex; gap: 8px; align-items: flex-end; }
.composer .attach { padding: 8px 10px; cursor: pointer; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips:empty { display: none; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 2px 10px; font-size: 13px; }
.chip button { border: 0; background: none; color: inherit; padding: 0; font-size: 12px; }
.dropzone { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(227,6,19,.08); border: 3px dashed var(--accent); font-size: 20px; color: var(--accent); z-index: 20; pointer-events: none; }
.composer textarea { flex: 1; resize: none; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; background: var(--bg); min-height: 44px; max-height: 160px; }
.empty { margin: auto; text-align: center; color: var(--muted); max-width: 420px; }
.empty h2 { font-weight: 500; font-size: 18px; color: var(--fg); margin: 0 0 6px; }

/* ---------- drawer (history) ---------- */
.drawer { position: fixed; inset: 0 auto 0 0; width: min(320px, 85vw); background: var(--panel); border-right: 1px solid var(--line); transform: translateX(-100%); transition: transform .2s; z-index: 10; display: flex; flex-direction: column; }
.drawer.open { transform: none; }
.drawer header { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.drawer ul { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
.drawer footer { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; padding: 12px 14px; border-top: 1px solid var(--line); padding-bottom: max(12px, env(safe-area-inset-bottom)); }
.drawer footer .muted { flex-basis: 100%; }
.drawer li button { width: 100%; text-align: left; background: none; border: 0; padding: 8px 10px; border-radius: 8px; }
.drawer li button:hover, .drawer li button.active { background: var(--accent-soft); }
.drawer li small { display: block; color: var(--muted); }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.25); z-index: 9; }

/* ---------- admin ---------- */
.page { max-width: 960px; margin: 0 auto; padding: 16px; }
.page.wide { max-width: 1120px; }
.page h2 { font-size: 18px; margin: 24px 0 10px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; }
.row .field { margin: 0; flex: 1 1 160px; }
.invite-link { word-break: break-all; }

/* Soft chart colours for people in the usage chart (same in day and night). */
:root { --c1: #9ec3e6; --c2: #f2b8a2; --c3: #b4d9a9; --c4: #d4bde3; --c5: #f3d98d; --c6: #a6d8d3; --c7: #f1bfd3; --c8: #c9c1a2; --c9: #b9c4ee; --bad: #b4413c; --bad-soft: #f8e3e1; }
:root[data-theme="dark"] { --bad: #f09a92; --bad-soft: #3d2423; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --bad: #f09a92; --bad-soft: #3d2423; } }

.admin-head { display: flex; align-items: center; gap: 12px; }
.admin-head .grow, .toolbar .grow { flex: 1; }
.admin-title { font-size: 22px; margin: 8px 0 4px; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); overflow-x: auto; margin: 8px 0 16px; scrollbar-width: none; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 10px 14px; color: var(--muted); white-space: nowrap; margin-bottom: -1px; }
.tabs button[aria-selected="true"] { color: var(--fg); border-bottom-color: var(--accent); font-weight: 600; }
.tabs button:hover { color: var(--fg); }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; min-width: 0; }
.tile[role="button"] { cursor: pointer; }
.tile[role="button"]:hover { border-color: var(--muted); }
.tile.attn { border-color: var(--warn); background: linear-gradient(var(--warn-soft), var(--panel) 70%); }
.tile .label { font-size: 13px; color: var(--muted); }
.tile .value { font-size: 26px; font-weight: 600; margin: 2px 0; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.tile .sub { font-size: 13px; color: var(--muted); margin-top: 6px; overflow-wrap: anywhere; }
.bar { height: 8px; background: var(--line); border-radius: 999px; overflow: hidden; margin-top: 8px; }
.bar > span { display: block; height: 100%; background: var(--ok); border-radius: 999px; }
.bar.warn > span { background: var(--warn); }

.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.card-head h3, .help h3 { font-size: 16px; margin: 0; }
.help h3 { margin-bottom: 8px; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar.tight { margin: 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--panel); }
.seg button { border: 0; background: none; padding: 6px 12px; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--fg); font-weight: 600; }
.btn.small, select.small { padding: 4px 10px; font-size: 13px; border-radius: 8px; }
select.small { border: 1px solid var(--line); background: var(--panel); }
.scroll { overflow-x: auto; }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.nowrap { white-space: nowrap; }
td.wrap { overflow-wrap: anywhere; min-width: 220px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
a.btn { display: inline-block; text-decoration: none; color: inherit; }
.block { display: block; margin-top: 4px; }
.pill.bad { background: var(--bad-soft); color: var(--bad); }

.chart svg { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.empty-chart { padding: 40px 0; text-align: center; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--muted); margin-top: 8px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; margin-right: 6px; vertical-align: -1px; }

.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; font-size: 14px; }
.kv > * { min-width: 0; overflow-wrap: anywhere; }
.meter + .meter { margin-top: 12px; }
.meter-label { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; flex-wrap: wrap; }
.check-out { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; white-space: pre-wrap; font-size: 13px; margin: 8px 0 0; overflow-wrap: anywhere; }
.check-out.ok { border-color: var(--ok); }
.check-out.bad { border-color: var(--bad); }
.notice { border-color: var(--ok); }
.help-table td:first-child { min-width: 170px; }
.help ol { padding-left: 20px; margin: 0 0 8px; }
.help li { margin: 4px 0; }
@media (max-width: 560px) {
  .page { padding: 12px; }
  .tile .value { font-size: 22px; }
  .card { padding: 14px; }
}
