/* FlexMessage design system — black / white with a single red accent (#cb0000).
   Tokens first; everything else derives from them. Dark mode inverts the neutrals only. */

:root {
  --bg: #ffffff;
  --bg-elev: #ffffff;
  --bg-muted: #f5f5f5;
  --fg: #111111;
  --fg-muted: #6b6b6b;
  --border: rgba(0, 0, 0, 0.10);
  --border-strong: rgba(0, 0, 0, 0.22);
  --accent: #cb0000;
  --accent-hover: #a80000;
  --accent-fg: #ffffff;
  --ok: #1a7f37;
  --ok-bg: #eaf5ec;
  --err: #cb0000;
  --err-bg: #fdecec;
  --info-bg: #f0f0f0;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 8px 24px rgba(0,0,0,.06);
  --radius: 12px;
  --radius-sm: 8px;
  --font: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sidebar-w: 240px;
  --topbar-h: 56px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f0f0f; --bg-elev: #171717; --bg-muted: #1d1d1d;
    --fg: #f2f2f2; --fg-muted: #9a9a9a;
    --border: rgba(255,255,255,.10); --border-strong: rgba(255,255,255,.24);
    --ok-bg: #12291a; --err-bg: #2b1212; --info-bg: #1d1d1d;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f0f0f; --bg-elev: #171717; --bg-muted: #1d1d1d;
  --fg: #f2f2f2; --fg-muted: #9a9a9a;
  --border: rgba(255,255,255,.10); --border-strong: rgba(255,255,255,.24);
  --ok-bg: #12291a; --err-bg: #2b1212; --info-bg: #1d1d1d;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
  color-scheme: dark;
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font-family: var(--font); font-size: 15px; line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.25; letter-spacing: -0.01em; }
h1 { font-size: 1.6rem; font-weight: 700; }
h2 { font-size: 1.1rem; font-weight: 600; }
p { margin: 0 0 1rem; }
a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
a:hover { color: var(--accent); }
.muted { color: var(--fg-muted); }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.truncate { max-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inline { display: inline; }
.skip { position: absolute; left: -999px; }
.skip:focus { left: 8px; top: 8px; background: var(--bg-elev); padding: 8px 12px; z-index: 100; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- app shell ---------- */
.shell { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  height: var(--topbar-h); display: flex; align-items: center; gap: 12px; padding: 0 16px;
  border-bottom: 1px solid var(--border); background: var(--bg-elev); position: sticky; top: 0; z-index: 20;
}
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.who { color: var(--fg-muted); font-size: .9rem; }
.brand { font-weight: 700; font-size: 1.05rem; text-decoration: none; letter-spacing: -0.01em; }
.brand:hover { color: inherit; }
.brand-accent { color: var(--accent); }
.brand-lg { font-size: 1.4rem; display: block; margin-bottom: 1.25rem; }
.body { display: flex; flex: 1; }
.sidebar {
  width: var(--sidebar-w); flex: none; padding: 16px 12px; border-right: 1px solid var(--border);
  background: var(--bg-elev); display: flex; flex-direction: column; gap: 2px;
  position: sticky; top: var(--topbar-h); height: calc(100vh - var(--topbar-h)); overflow-y: auto;
}
.nav-group { font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--fg-muted); padding: 14px 12px 6px; }
.nav-link {
  display: block; padding: 8px 12px; border-radius: var(--radius-sm); text-decoration: none; color: var(--fg);
  border-left: 3px solid transparent;
}
.nav-link:hover { background: var(--bg-muted); color: var(--fg); }
.nav-link.active { border-left-color: var(--accent); background: var(--bg-muted); font-weight: 600; }
.nav-footer { margin-top: auto; padding: 12px; }
.main { flex: 1; min-width: 0; padding: 28px 32px; max-width: 1200px; }
.page-head { margin-bottom: 20px; }
.page-narrow { max-width: 520px; }
.iconbtn { display: none; background: none; border: 0; color: inherit; padding: 6px; border-radius: var(--radius-sm); cursor: pointer; }
.iconbtn:hover { background: var(--bg-muted); }

@media (max-width: 860px) {
  .iconbtn { display: inline-flex; }
  .sidebar { position: fixed; left: 0; top: var(--topbar-h); transform: translateX(-100%); transition: transform .18s ease; z-index: 30; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .main { padding: 20px 16px; }
  .who { display: none; }
}

/* ---------- auth pages ---------- */
.auth { display: grid; place-items: center; min-height: 100vh; padding: 24px 16px; background: var(--bg-muted); }
.auth-card { width: 100%; max-width: 400px; background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius); padding: 32px; box-shadow: var(--shadow); }
.auth-title { font-size: 1.35rem; margin-bottom: 1rem; }

/* ---------- components ---------- */
.card { background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; margin-bottom: 20px; }
.stack { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: .85rem; font-weight: 600; }
.input {
  width: 100%; font: inherit; color: inherit; background: var(--bg); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); padding: 10px 12px; min-height: 42px;
}
.input:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px rgba(203,0,0,.15); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; font: inherit; font-weight: 600;
  padding: 10px 16px; min-height: 42px; border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
  background: var(--bg-elev); color: var(--fg); cursor: pointer; text-decoration: none;
}
.btn:hover { background: var(--bg-muted); color: var(--fg); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--accent-fg); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { min-height: 34px; padding: 6px 10px; font-size: .9rem; }
.btn-block { width: 100%; }
.actions { display: flex; gap: 10px; justify-content: flex-end; }
.alert { padding: 12px 14px; border-radius: var(--radius-sm); border: 1px solid var(--border); margin-bottom: 14px; }
.alert-error { background: var(--err-bg); border-color: rgba(203,0,0,.3); }
.alert-ok { background: var(--ok-bg); border-color: rgba(26,127,55,.3); }
.alert-info { background: var(--info-bg); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 20px; }
.stat { background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; display: flex; flex-direction: column; gap: 4px; }
.stat-label { font-size: .8rem; color: var(--fg-muted); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.stat-value { font-size: 1.7rem; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.table { width: 100%; border-collapse: collapse; font-size: .93rem; }
.table th, .table td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--fg-muted); font-weight: 600; }
.table tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
code, .mono { font-family: var(--mono); font-size: .9em; }

/* ---------- admin / list screens ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 16px; }
.toolbar .input { width: auto; min-width: 220px; }
.toolbar .spacer { flex: 1; }
.toolbar label.check { display: inline-flex; align-items: center; gap: 6px; color: var(--fg-muted); font-size: .9rem; }
.page-head.row { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.badge { display: inline-block; font-size: .72rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border-strong); color: var(--fg-muted); }
.badge-on { color: var(--ok); border-color: rgba(26,127,55,.4); background: var(--ok-bg); }
.badge-off { color: var(--fg-muted); background: var(--bg-muted); }
.badge-accent { color: var(--accent); border-color: rgba(203,0,0,.4); background: var(--err-bg); }
.pager { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 0 0; flex-wrap: wrap; }
.pager-links { display: inline-flex; align-items: center; gap: 10px; }
.table a { text-decoration: none; font-weight: 600; }
.table a:hover { text-decoration: underline; }
.table .actions-cell { white-space: nowrap; text-align: right; }
.table tr.clickable:hover td { background: var(--bg-muted); }
.row-view { display: inline-flex; align-items: center; gap: 6px; color: var(--fg-muted); font-size: .85rem; font-weight: 600; white-space: nowrap; }
.row-view svg { width: 16px; height: 16px; flex: none; }
.table tr.clickable:hover .row-view { color: var(--accent); }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; }
.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; }
.form-section { margin-bottom: 22px; }
.form-section > h2 { margin-bottom: 12px; }
.check-row { display: flex; flex-wrap: wrap; gap: 14px 22px; }
.check-row label { display: inline-flex; align-items: center; gap: 8px; }
select.input { appearance: auto; }
textarea.input { min-height: 96px; resize: vertical; font-family: var(--font); }
details.card > summary { cursor: pointer; font-weight: 600; }
.banner { background: var(--accent); color: var(--accent-fg); padding: 8px 16px; display: flex; gap: 12px; align-items: center; justify-content: center; font-size: .9rem; }
.banner a, .banner button { color: inherit; }
.banner .btn { border-color: rgba(255,255,255,.6); background: transparent; color: #fff; }
.keybox { font-family: var(--mono); font-size: 1rem; padding: 14px 16px; border: 1px dashed var(--accent); border-radius: var(--radius-sm); background: var(--err-bg); word-break: break-all; user-select: all; }
.btn-danger { color: var(--accent); border-color: rgba(203,0,0,.5); }
.btn-danger:hover { background: var(--err-bg); color: var(--accent); }
.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 18px; }
.kv dt { color: var(--fg-muted); font-size: .85rem; }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
pre.block { background: var(--bg-muted); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px; overflow: auto; font-size: .85rem; max-height: 420px; white-space: pre-wrap; }
.line-items .input { min-height: 36px; padding: 6px 10px; }
.empty { color: var(--fg-muted); padding: 24px; text-align: center; }

/* ---------- client portal ---------- */
.thread { display: flex; flex-direction: column; gap: 10px; padding: 8px 0; }
.bubble { max-width: 72%; padding: 10px 14px; border-radius: 16px; line-height: 1.4; white-space: pre-wrap; word-break: break-word; }
.bubble.out { align-self: flex-end; background: var(--fg); color: var(--bg); border-bottom-right-radius: 4px; }
.bubble.in { align-self: flex-start; background: var(--bg-muted); border-bottom-left-radius: 4px; }
.bubble time { display: block; font-size: .72rem; opacity: .65; margin-top: 4px; }
.compose { display: flex; gap: 10px; align-items: flex-end; margin-top: 14px; }
.compose textarea { flex: 1; min-height: 60px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.chip { font: inherit; font-size: .8rem; padding: 3px 10px; border-radius: 999px; border: 1px solid var(--border-strong); background: var(--bg-elev); color: var(--fg); cursor: pointer; }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.counter { font-size: .8rem; color: var(--fg-muted); text-align: right; }
.counter.over { color: var(--accent); font-weight: 600; }
.stat-sm .stat-value { font-size: 1.3rem; }
.mapping { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; align-items: center; }
@media (max-width: 860px) { .bubble { max-width: 90%; } .mapping { grid-template-columns: 1fr; } }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
