/* =====================================================================
   SMS Gate · OpsDesk design system
   Dense-but-calm operational tooling. Tokens from SPEC §7.1 / §7.5.
   No CDN, no framework build. Bootstrap 5.3 class-compat layer below.
   ===================================================================== */

:root {
  /* colour roles (light) */
  --bg: #F5F7FB;
  --surface: #FFFFFF;
  --ink: #0F172A;        /* 17.85:1 on white */
  --ink-2: #475569;      /* 7.58:1 */
  --ink-3: #94A3B8;      /* 2.56:1 — decorative only */
  --accent: #2563EB;     /* 5.17:1 */
  --accent-strong: #1D4ED8; /* 6.70:1 */
  --accent-soft: #EAF0FF;
  --ok: #15803D;         /* 5.02:1 text-safe */
  --ok-bright: #16A34A;  /* 3.30:1 non-text only */
  --warn: #B45309;
  --danger: #B91C1C;     /* 6.47:1 */
  --line: #E2E8F0;

  /* type */
  --font-ui: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'Roboto Mono', 'SF Mono', ui-monospace, Menlo, monospace;
  --fs-xs: 12px; --fs-sm: 14px; --fs-md: 15px; --fs-lg: 18px; --fs-xl: 24px;
  --fs-mono: 13px;

  /* spacing (4-based) */
  --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px;
  --sp-5: 24px; --sp-6: 32px; --sp-7: 48px; --sp-8: 64px;

  /* radii */
  --r-sm: 6px; --r-md: 8px; --r-lg: 12px; --r-pill: 999px;

  /* elevation */
  --shadow-1: 0 1px 2px rgba(15, 23, 42, .06);
  --shadow-2: 0 4px 12px rgba(15, 23, 42, .10);
  --shadow-3: 0 12px 32px rgba(15, 23, 42, .16);

  /* motion */
  --t-fast: 150ms; --t-mid: 200ms; --t-slow: 300ms;
  --ease: cubic-bezier(.2, 0, 0, 1);
}

/* dark mode tokens (SPEC §7.5) */
[data-theme="dark"] {
  --bg: #0B1B33;
  --surface: #0E2238;
  --ink: #E2E8F0;
  --ink-2: #94A3B8;
  --ink-3: #64748B;
  --accent: #60A5FA;
  --accent-strong: #93C5FD;
  --accent-soft: rgba(96, 165, 250, .16);
  --ok: #4ADE80;
  --ok-bright: #4ADE80;
  --warn: #FBBF24;
  --danger: #F87171;
  --line: rgba(148, 163, 184, .18);
  --shadow-1: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-2: 0 4px 12px rgba(0, 0, 0, .5);
  --shadow-3: 0 12px 32px rgba(0, 0, 0, .6);
}

@media (prefers-reduced-motion: reduce) {
  :root { --t-fast: 0ms; --t-mid: 0ms; --t-slow: 0ms; }
  * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: var(--fs-md);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.mono { font-family: var(--font-mono); font-size: var(--fs-mono); }
.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;
}

/* visible focus everywhere (WCAG 2.2 AA) */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: var(--r-sm); }

/* ---------- app shell ---------- */
.app { display: flex; min-height: 100dvh; }

/* left rail for >=768 */
.rail { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: var(--sp-2) var(--sp-3);
  display: flex; align-items: center; gap: var(--sp-2);
  padding-top: calc(var(--sp-2) + env(safe-area-inset-top, 0));
}
.brand-mini { display: flex; align-items: center; gap: var(--sp-2); }
.brand-logo {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-size: 15px; flex: none;
}
.brand-name { font-weight: 700; font-size: var(--fs-md); }
.switcher {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px; border-radius: var(--r-md);
  background: var(--accent-soft); color: var(--accent-strong);
  font-weight: 600; font-size: var(--fs-sm); min-height: 40px;
  border: none; cursor: pointer;
}
.topbar .grow { flex: 1; }

.icon-btn {
  width: 44px; height: 44px; border-radius: var(--r-md);
  display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: none; color: var(--ink-2);
  cursor: pointer; font-size: 18px; flex: none;
}
.icon-btn:hover { background: var(--bg); }

.content { flex: 1; padding: var(--sp-3); max-width: 1200px; width: 100%; margin: 0 auto; }

/* ---------- bottom nav (mobile) ---------- */
.bottomnav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  background: var(--surface); border-top: 1px solid var(--line);
  display: flex; padding-bottom: env(safe-area-inset-bottom, 0);
}
.bottomnav button {
  flex: 1; min-width: 0; min-height: 56px;
  padding: 8px 2px 10px; border: none; background: none;
  color: var(--ink-3); font-family: var(--font-ui); font-size: 10.5px;
  cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.bottomnav button .ico { font-size: 19px; }
.bottomnav button[aria-current="page"] { color: var(--accent-strong); font-weight: 600; }

/* ---------- cards / panels ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-lg); padding: var(--sp-4);
  box-shadow: var(--shadow-1); margin-bottom: var(--sp-3);
}
.card h2 { font-size: var(--fs-lg); font-weight: 600; margin-bottom: var(--sp-3); }
.sec-label {
  font-size: 11px; font-weight: 700; letter-spacing: .06em;
  text-transform: uppercase; color: var(--ink-3);
  margin: var(--sp-3) 2px var(--sp-2);
}
.muted { color: var(--ink-2); }
.faint { color: var(--ink-3); font-size: var(--fs-xs); }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 10px 16px;
  border-radius: var(--r-md); border: 1px solid var(--line);
  background: var(--surface); color: var(--ink);
  font-family: var(--font-ui); font-size: var(--fs-sm); font-weight: 600;
  cursor: pointer; transition: background var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease);
}
.btn:hover { background: var(--bg); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); box-shadow: var(--shadow-1); }
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; }
.btn[disabled] { opacity: .55; cursor: not-allowed; }
.btn-sm { min-height: 36px; padding: 6px 12px; font-size: var(--fs-xs); }

/* ---------- forms ---------- */
.field { margin-bottom: var(--sp-3); }
.field > label, .lbl {
  display: block; font-size: var(--fs-xs); font-weight: 600;
  color: var(--ink-2); margin-bottom: 5px;
  text-transform: uppercase; letter-spacing: .02em;
}
input[type="text"], input[type="url"], input[type="password"], input[type="number"],
input[type="datetime-local"], select, textarea {
  width: 100%; font-family: var(--font-ui); font-size: var(--fs-md);
  color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--r-md);
  padding: 11px 12px; min-height: 44px;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { min-height: 110px; resize: vertical; line-height: 1.5; }
.hint { font-size: var(--fs-xs); color: var(--ink-3); margin-top: 4px; }
.err-text { font-size: var(--fs-xs); color: var(--danger); margin-top: 4px; }
input.invalid, select.invalid, textarea.invalid { border-color: var(--danger); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: var(--sp-2); }

/* recipient chips */
.chips {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-md); padding: 10px; min-height: 60px;
}
.chips:focus-within { border-color: var(--accent); }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--accent-soft); color: var(--accent-strong);
  border-radius: var(--r-pill); padding: 5px 8px 5px 12px;
  font-size: var(--fs-sm); font-weight: 600;
}
.chip.invalid { background: var(--danger); color: #fff; }
.chip button {
  border: none; background: none; color: inherit; cursor: pointer;
  font-size: 15px; line-height: 1; padding: 2px 4px; min-width: 24px; min-height: 24px;
}
.chips input {
  flex: 1; min-width: 130px; border: none; outline: none;
  font-family: var(--font-mono); font-size: var(--fs-md); padding: 4px; background: none; color: var(--ink);
}

/* segment counter */
.segrow { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; gap: 8px; }
.segcounter {
  font-family: var(--font-mono); font-size: var(--fs-mono); font-weight: 600;
  color: var(--accent-strong); background: var(--accent-soft);
  padding: 4px 10px; border-radius: var(--r-pill);
}
.segcounter.warn { color: var(--warn); background: #FFF9EC; border: 1px solid #FCD9A0; }
[data-theme="dark"] .segcounter.warn { background: rgba(251, 191, 36, .12); border-color: rgba(251, 191, 36, .3); }

/* segmented control (priority) */
.segctl { display: flex; gap: 6px; flex-wrap: wrap; }
.segctl button {
  flex: 1; min-width: 70px; min-height: 44px; padding: 8px 6px;
  border: 1px solid var(--line); border-radius: var(--r-md);
  background: var(--surface); color: var(--ink-2);
  font-family: var(--font-ui); font-size: var(--fs-xs); font-weight: 600; cursor: pointer;
}
.segctl button[aria-pressed="true"] {
  background: var(--accent-soft); color: var(--accent-strong); border-color: var(--accent);
}

/* switch */
.switch {
  position: relative; width: 48px; height: 28px; border-radius: var(--r-pill);
  background: var(--line); border: none; cursor: pointer; flex: none;
  transition: background var(--t-fast) var(--ease);
}
.switch::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px;
  border-radius: 50%; background: #fff; box-shadow: var(--shadow-1);
  transition: transform var(--t-fast) var(--ease);
}
.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"]::after { transform: translateX(20px); }

.switch-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px 0; border-top: 1px solid var(--line);
}
.switch-row:first-of-type { border-top: none; }

/* ---------- status pills / dots ---------- */
.pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: var(--r-pill);
  font-size: var(--fs-xs); font-weight: 600;
  background: var(--bg); border: 1px solid var(--line); color: var(--ink-2);
}
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.dot.ok { background: var(--ok); }
.dot.warn { background: #D97706; }
.dot.danger { background: var(--danger); }
.dot.grey { background: #CBD5E1; }
.dot.accent { background: var(--accent); }

/* rows */
.row {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px; border-bottom: 1px solid var(--line);
  min-height: 52px;
}
.row:last-child { border-bottom: none; }
.row .row-main { flex: 1; min-width: 0; }
.row .row-title { font-weight: 600; font-size: var(--fs-md); }
.row .row-sub { font-size: var(--fs-sm); color: var(--ink-2); margin-top: 2px; word-break: break-word; }
.row .row-meta { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 6px; font-size: var(--fs-xs); color: var(--ink-3); }

/* empty state */
.empty {
  text-align: center; padding: var(--sp-7) var(--sp-4); color: var(--ink-2);
}
.empty .big { font-size: 40px; margin-bottom: var(--sp-3); opacity: .5; }
.empty h3 { font-size: var(--fs-lg); color: var(--ink); margin-bottom: 6px; }

/* notice */
.notice {
  display: flex; gap: 10px; align-items: flex-start;
  border-radius: var(--r-md); padding: 10px 12px;
  font-size: var(--fs-xs); line-height: 1.45; margin-bottom: var(--sp-3);
}
.notice.info { background: var(--accent-soft); color: var(--accent-strong); border: 1px solid var(--accent); }
.notice.warn { background: #FFF7ED; color: var(--warn); border: 1px solid #FCD9A8; }
.notice.bad { background: #FEF1F1; color: var(--danger); border: 1px solid #F5B8B8; }
.notice.ok { background: #F0FDF4; color: var(--ok); border: 1px solid #BBF7D0; }
[data-theme="dark"] .notice.warn { background: rgba(251, 191, 36, .1); border-color: rgba(251, 191, 36, .3); }
[data-theme="dark"] .notice.bad { background: rgba(248, 113, 113, .12); border-color: rgba(248, 113, 113, .3); }
[data-theme="dark"] .notice.ok { background: rgba(74, 222, 128, .1); border-color: rgba(74, 222, 128, .3); }

/* toast */
.toasts { position: fixed; bottom: calc(72px + env(safe-area-inset-bottom, 0)); left: 12px; right: 12px; z-index: 60; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast {
  max-width: 520px; width: 100%; background: var(--ink); color: #fff;
  border-radius: var(--r-md); padding: 12px 16px; box-shadow: var(--shadow-3);
  font-size: var(--fs-sm); animation: slideup var(--t-mid) var(--ease);
}
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }
@keyframes slideup { from { transform: translateY(16px); opacity: 0; } to { transform: none; opacity: 1; } }

/* skeleton */
.skel { background: linear-gradient(90deg, var(--line) 25%, var(--bg) 37%, var(--line) 63%); background-size: 400% 100%; animation: shimmer 1.4s infinite; border-radius: var(--r-sm); height: 14px; }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

/* dense list skeleton rows */
.skel-row { display: flex; gap: 12px; padding: 12px; border-bottom: 1px solid var(--line); }

/* ---------- responsive: rail at >=768 ---------- */
@media (min-width: 768px) {
  .bottomnav { display: none; }
  .rail {
    display: flex; flex-direction: column;
    width: 212px; flex: none;
    background: var(--surface); border-right: 1px solid var(--line);
    padding: var(--sp-3) 10px;
    position: sticky; top: 0; height: 100dvh;
  }
  .rail .brand { display: flex; align-items: center; gap: 10px; padding: 6px 8px 16px; }
  .rail .brand b { font-size: var(--fs-md); }
  .rail .sec-label { margin: 14px 8px 6px; }
  .navitem {
    display: flex; align-items: center; gap: 10px; width: 100%;
    padding: 10px 8px; border-radius: var(--r-md); min-height: 44px;
    font-size: var(--fs-sm); color: var(--ink-2);
    background: none; border: none; cursor: pointer; text-align: left;
    font-family: var(--font-ui);
  }
  .navitem[aria-current="page"] { background: var(--accent-soft); color: var(--accent-strong); font-weight: 600; }
  .navitem:hover { background: var(--bg); }
  .rail .spacer { flex: 1; }
  .content { padding: var(--sp-5); }
  .main { padding-bottom: 0; }
  .app { padding-bottom: 0; }
}
@media (max-width: 767px) {
  .main { padding-bottom: 64px; }
  .content { padding-bottom: calc(var(--sp-6) + 64px); }
}

/* 1440: generous padding, content capped at 1200 (SPEC §6.3) */
@media (min-width: 1440px) {
  .content { max-width: 1200px; padding: var(--sp-6) var(--sp-7); }
  .rail { width: 236px; }
}

/* grid for desktop cards */
.grid-2 { display: grid; grid-template-columns: 1fr; gap: 0; }
@media (min-width: 1024px) {
  .grid-2 { grid-template-columns: 1fr 1fr; gap: var(--sp-4); }
}

/* offline banner */
.offline-banner {
  background: var(--warn); color: #fff; text-align: center;
  font-size: var(--fs-xs); font-weight: 600; padding: 6px 12px;
}

/* modal sheet */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(15, 23, 42, .5);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade var(--t-fast) var(--ease);
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.sheet {
  background: var(--surface); width: 100%; max-width: 620px;
  border-radius: var(--r-lg) var(--r-lg) 0 0; padding: var(--sp-5);
  max-height: 90dvh; overflow-y: auto;
  padding-bottom: calc(var(--sp-5) + env(safe-area-inset-bottom, 0));
  animation: slideup var(--t-mid) var(--ease);
}
@media (min-width: 768px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: var(--r-lg); }
}
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--sp-4); }
.sheet-head h2 { font-size: var(--fs-lg); font-weight: 700; }

/* hidden util */
.hidden { display: none !important; }
