/* ==========================================================================
   Kobyx Mesh — dashboard design system
   Tokens first, then layout, then components. Light and dark are both
   deliberately chosen sets, not an automatic inversion.
   ========================================================================== */

:root {
  color-scheme: light;

  /* surfaces & ink */
  --bg:            #f7f8fa;
  --surface:       #ffffff;
  --surface-2:     #f2f4f7;
  --surface-3:     #eaecf0;
  --border:        #e4e7ec;
  --border-strong: #d0d5dd;
  --text:          #101828;
  --text-2:        #475467;
  --muted:         #667085;

  /* brand / interactive */
  --accent:        #2a78d6;
  --accent-hover:  #1c5cab;
  --accent-soft:   #eaf1fc;
  --accent-ink:    #1c5cab;
  --focus:         #2a78d6;

  /* data series — validated (see docs/ARCHITECTURE.md) */
  --series-1:      #2a78d6;   /* issued  */
  --series-2:      #e34948;   /* refused */
  --grid:          #eaecf0;
  --axis-text:     #667085;

  /* status */
  --ok:            #0ca30c;
  --ok-soft:       #e7f6e7;
  --warn:          #b26a00;
  --warn-soft:     #fdf3e2;
  --bad:           #d03b3b;
  --bad-soft:      #fdecec;

  /* shape */
  --r-sm: 6px;
  --r:    9px;
  --r-lg: 14px;
  --shadow-sm: 0 1px 2px rgb(16 24 40 / 0.05);
  --shadow:    0 1px 3px rgb(16 24 40 / 0.08), 0 1px 2px rgb(16 24 40 / 0.04);
  --shadow-lg: 0 12px 32px rgb(16 24 40 / 0.12);

  --sidebar-w: 236px;
  --control-h: 38px;

  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg:            #0c0e12;
    --surface:       #151920;
    --surface-2:     #1b2029;
    --surface-3:     #232936;
    --border:        #232a35;
    --border-strong: #333c4a;
    --text:          #e9edf5;
    --text-2:        #b3bdcd;
    --muted:         #8d97a8;

    --accent:        #3987e5;
    --accent-hover:  #5598e7;
    --accent-soft:   #16243a;
    --accent-ink:    #86b6ef;

    --series-1:      #3987e5;
    --series-2:      #e66767;
    --grid:          #232a35;
    --axis-text:     #8d97a8;

    --ok:            #35c58a;
    --ok-soft:       #10251d;
    --warn:          #e0a233;
    --warn-soft:     #2a2113;
    --bad:           #f0645f;
    --bad-soft:      #2d1618;

    --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
    --shadow:    0 1px 3px rgb(0 0 0 / 0.5);
    --shadow-lg: 0 12px 32px rgb(0 0 0 / 0.6);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg:            #0c0e12;
  --surface:       #151920;
  --surface-2:     #1b2029;
  --surface-3:     #232936;
  --border:        #232a35;
  --border-strong: #333c4a;
  --text:          #e9edf5;
  --text-2:        #b3bdcd;
  --muted:         #8d97a8;

  --accent:        #3987e5;
  --accent-hover:  #5598e7;
  --accent-soft:   #16243a;
  --accent-ink:    #86b6ef;

  --series-1:      #3987e5;
  --series-2:      #e66767;
  --grid:          #232a35;
  --axis-text:     #8d97a8;

  --ok:            #35c58a;
  --ok-soft:       #10251d;
  --warn:          #e0a233;
  --warn-soft:     #2a2113;
  --bad:           #f0645f;
  --bad-soft:      #2d1618;

  --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
  --shadow:    0 1px 3px rgb(0 0 0 / 0.5);
  --shadow-lg: 0 12px 32px rgb(0 0 0 / 0.6);
}

/* --------------------------------------------------------------- base ---- */
* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -0.011em; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

code, .mono { font-family: var(--mono); font-size: 0.875em; }

.hidden { display: none !important; }
.muted  { color: var(--muted); }
.nowrap { white-space: nowrap; }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/* ------------------------------------------------------------- sign in ---- */
.signin-wrap {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.signin {
  width: 100%;
  max-width: 392px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  padding: 32px;
}

.signin .brand { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.signin h1 { font-size: 20px; }
.signin .sub { color: var(--muted); font-size: 13.5px; margin: 0 0 24px; }
.signin .field { margin-bottom: 14px; }
.signin .btn { width: 100%; justify-content: center; margin-top: 6px; }

/* --------------------------------------------------------------- shell ---- */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100dvh; }

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 18px 12px;
  position: sticky;
  top: 0;
  height: 100dvh;
  overflow-y: auto;
}

.sidebar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 10px 18px;
}
.sidebar .brand-name { font-size: 15px; font-weight: 650; letter-spacing: -0.015em; }
.sidebar .brand-sub  { font-size: 11.5px; color: var(--muted); }

.logo {
  width: 30px; height: 30px; flex: none;
  border-radius: 8px;
  background: linear-gradient(140deg, var(--accent), var(--accent-hover));
  display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 13px;
  box-shadow: var(--shadow-sm);
}

.nav-label {
  font-size: 10.5px; font-weight: 650; letter-spacing: 0.07em;
  text-transform: uppercase; color: var(--muted);
  padding: 14px 10px 6px;
}

.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border-radius: var(--r-sm);
  color: var(--text-2);
  font-weight: 500;
  cursor: pointer;
  border: 0; background: none; width: 100%; text-align: left;
  font-size: 13.5px;
  font-family: inherit;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-item[aria-current="page"] { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.nav-item .ico { width: 16px; height: 16px; flex: none; opacity: 0.85; }
.nav-item .count {
  margin-left: auto; font-size: 11.5px; font-variant-numeric: tabular-nums;
  background: var(--surface-3); color: var(--text-2);
  border-radius: 999px; padding: 1px 7px; font-weight: 600;
}

.sidebar-foot { margin-top: auto; padding-top: 14px; border-top: 1px solid var(--border); }
.sidebar-foot .who { display: flex; align-items: center; gap: 9px; padding: 6px 10px; }
.avatar {
  width: 26px; height: 26px; border-radius: 50%; flex: none;
  background: var(--surface-3); color: var(--text-2);
  display: grid; place-items: center; font-size: 11px; font-weight: 700;
}

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

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 20;
}
.topbar h1 { font-size: 18px; }
.topbar .page-sub { font-size: 13px; color: var(--muted); margin-top: 1px; }
.topbar .spacer { flex: 1; }

.content { padding: 24px 28px 56px; max-width: 1320px; width: 100%; }

.menu-btn { display: none; }

/* -------------------------------------------------------------- cards ----- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  margin-bottom: 20px;
  overflow: hidden;
}
.card-head {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.card-head h2 { font-size: 14.5px; }
.card-head .sub { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.card-head .spacer { flex: 1; }
.card-body { padding: 20px; }
.card-body.flush { padding: 0; }

/* ---------------------------------------------------------- stat tiles ---- */
.stat-grid {
  display: grid; gap: 16px; margin-bottom: 20px;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  padding: 16px 18px;
}
.stat .label {
  font-size: 12px; color: var(--muted); font-weight: 550;
  display: flex; align-items: center; gap: 6px;
}
.stat .value {
  font-size: 30px; font-weight: 640; letter-spacing: -0.025em;
  margin-top: 8px; font-variant-numeric: tabular-nums; line-height: 1.1;
}
.stat .foot { font-size: 12px; color: var(--muted); margin-top: 5px; }

/* ------------------------------------------------------------- tables ----- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
thead th {
  text-align: left; font-size: 11.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted);
  padding: 10px 16px; background: var(--surface-2);
  border-bottom: 1px solid var(--border); white-space: nowrap;
}
tbody td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
td.actions { text-align: right; white-space: nowrap; }
td.actions .btn + .btn { margin-left: 6px; }
.cell-title { font-weight: 550; }
.cell-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* ------------------------------------------------------------ controls ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: var(--control-h); padding: 0 14px;
  border-radius: var(--r-sm); border: 1px solid transparent;
  background: var(--accent); color: #fff;
  font: 550 13.5px/1 var(--sans); cursor: pointer;
  white-space: nowrap; transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.btn:hover:not(:disabled) { background: var(--accent-hover); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn.secondary { background: var(--surface); color: var(--text-2); border-color: var(--border-strong); }
.btn.secondary:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

.btn.ghost { background: transparent; color: var(--text-2); border-color: transparent; }
.btn.ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

.btn.danger { background: transparent; color: var(--bad); border-color: var(--border-strong); }
.btn.danger:hover:not(:disabled) { background: var(--bad-soft); border-color: var(--bad); }

.btn.sm { height: 30px; padding: 0 10px; font-size: 12.5px; border-radius: var(--r-sm); }
.btn.icon { width: 30px; padding: 0; }

/* ---------------------------------------------------------------- form ---- */
/* One control height everywhere; width comes from the grid cell, never from
   a per-field size attribute. That is what keeps the fields even. */
.form-grid {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: 16px;
}
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field.span-2  { grid-column: span 2; }
.field.span-3  { grid-column: span 3; }
.field.span-4  { grid-column: span 4; }
.field.span-6  { grid-column: span 6; }
.field.span-12 { grid-column: span 12; }

label {
  font-size: 12.5px; font-weight: 550; color: var(--text-2);
  display: flex; align-items: center; gap: 6px;
}
.hint { font-size: 12px; color: var(--muted); }

input[type="text"], input[type="password"], input[type="number"],
input[type="datetime-local"], select, textarea {
  width: 100%;
  height: var(--control-h);
  padding: 0 11px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  font: 400 13.5px var(--sans);
  transition: border-color 0.12s, box-shadow 0.12s;
}
textarea { height: auto; padding: 9px 11px; resize: vertical; }
input::placeholder { color: var(--muted); }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
                    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) center, calc(100% - 11px) center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 32px;
}

.form-actions {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--border);
}

.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch .track {
  width: 36px; height: 20px; border-radius: 999px;
  background: var(--surface-3); border: 1px solid var(--border-strong);
  position: relative; transition: background 0.15s, border-color 0.15s;
}
.switch .track::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--surface); box-shadow: var(--shadow-sm);
  transition: transform 0.15s;
}
.switch input:checked + .track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .track::after { transform: translateX(16px); }

/* --------------------------------------------------------------- pills ---- */
.pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 9px; border-radius: 999px;
  font-size: 11.5px; font-weight: 600; white-space: nowrap;
  border: 1px solid transparent;
}
.pill .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: none; }
.pill.ok   { color: var(--ok);   background: var(--ok-soft);   border-color: color-mix(in srgb, var(--ok) 28%, transparent); }
.pill.warn { color: var(--warn); background: var(--warn-soft); border-color: color-mix(in srgb, var(--warn) 28%, transparent); }
.pill.bad  { color: var(--bad);  background: var(--bad-soft);  border-color: color-mix(in srgb, var(--bad) 28%, transparent); }
.pill.neutral { color: var(--text-2); background: var(--surface-2); border-color: var(--border-strong); }

/* ------------------------------------------------------------ copy row ---- */
.copy-row {
  display: flex; align-items: stretch; gap: 0;
  border: 1px solid var(--border-strong); border-radius: var(--r-sm);
  background: var(--surface-2); overflow: hidden;
}
.copy-row code {
  flex: 1; min-width: 0; padding: 10px 12px;
  overflow-x: auto; white-space: nowrap; color: var(--text);
  font-size: 12.5px; line-height: 1.5;
}
.copy-row .btn { border-radius: 0; border: 0; border-left: 1px solid var(--border-strong); height: auto; }

/* ----------------------------------------------------------- callouts ----- */
.callout {
  display: flex; gap: 11px;
  padding: 13px 15px; border-radius: var(--r);
  border: 1px solid var(--border); background: var(--surface-2);
  font-size: 13px; color: var(--text-2);
}
.callout .ico { flex: none; width: 17px; height: 17px; margin-top: 1px; }
.callout strong { color: var(--text); font-weight: 600; }
.callout.info { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 25%, transparent); }
.callout.warn { background: var(--warn-soft);   border-color: color-mix(in srgb, var(--warn) 28%, transparent); }
.callout + .callout { margin-top: 12px; }

/* -------------------------------------------------------------- steps ----- */
.steps { counter-reset: step; display: flex; flex-direction: column; gap: 0; }
.step { display: grid; grid-template-columns: 32px 1fr; gap: 16px; padding-bottom: 26px; position: relative; }
.step:last-child { padding-bottom: 0; }
.step::before {
  counter-increment: step; content: counter(step);
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent-ink);
  display: grid; place-items: center; font-weight: 650; font-size: 13px;
  border: 1px solid color-mix(in srgb, var(--accent) 28%, transparent);
}
/* connector line between steps */
.step:not(:last-child)::after {
  content: ""; position: absolute; left: 15px; top: 34px; bottom: 6px;
  width: 1px; background: var(--border);
}
.step h3 { font-size: 14px; margin-bottom: 5px; }
.step p  { margin: 0 0 10px; color: var(--text-2); font-size: 13.5px; }
.step > div > *:last-child { margin-bottom: 0; }

/* --------------------------------------------------------- empty state ---- */
.empty { padding: 44px 24px; text-align: center; }
.empty .ico { width: 38px; height: 38px; color: var(--muted); opacity: 0.55; margin-bottom: 12px; }
.empty h3 { font-size: 14.5px; margin-bottom: 5px; }
.empty p { color: var(--muted); font-size: 13px; margin: 0 auto 16px; max-width: 42ch; }

/* ---------------------------------------------------------------- toast --- */
.toasts {
  position: fixed; right: 20px; bottom: 20px; z-index: 100;
  display: flex; flex-direction: column; gap: 10px; max-width: 380px;
}
.toast {
  display: flex; gap: 10px; align-items: flex-start;
  background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--r); box-shadow: var(--shadow-lg);
  padding: 12px 14px; font-size: 13px;
  animation: toast-in 0.18s ease-out;
}
.toast.ok  { border-left-color: var(--ok); }
.toast.bad { border-left-color: var(--bad); }
.toast .msg { flex: 1; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------------------------------------------------------------- modal --- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 90;
  background: rgb(16 24 40 / 0.5);
  display: grid; place-items: center; padding: 24px;
  animation: fade 0.12s ease-out;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-lg); box-shadow: var(--shadow-lg);
  width: 100%; max-width: 460px; padding: 24px;
}
.modal h3 { font-size: 16px; margin-bottom: 8px; }
.modal p { color: var(--text-2); font-size: 13.5px; margin: 0 0 18px; }
.modal .row { display: flex; gap: 10px; justify-content: flex-end; }

/* --------------------------------------------------------------- charts --- */
.chart-grid { display: grid; gap: 20px; grid-template-columns: 1.55fr 1fr; }
@media (max-width: 1080px) { .chart-grid { grid-template-columns: 1fr; } }

.chart { width: 100%; display: block; overflow: visible; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .axis-text { fill: var(--axis-text); font-size: 11px; font-family: var(--sans); }
.chart .axis-text.value { font-variant-numeric: tabular-nums; }
.chart .bar-label { fill: var(--text-2); font-size: 11.5px; font-weight: 600;
                    font-family: var(--sans); font-variant-numeric: tabular-nums; }
.chart .s1 { fill: var(--series-1); }
.chart .s2 { fill: var(--series-2); }
.chart .hover-band { fill: var(--text); opacity: 0; }
.chart .hover-band.on { opacity: 0.055; }

.legend { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; font-size: 12.5px; color: var(--text-2); }
.legend .key { display: inline-flex; align-items: center; gap: 7px; }
.legend .swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.legend .swatch.s1 { background: var(--series-1); }
.legend .swatch.s2 { background: var(--series-2); }

.chart-tip {
  position: fixed; z-index: 60; pointer-events: none;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--r-sm); box-shadow: var(--shadow-lg);
  padding: 9px 11px; font-size: 12.5px; min-width: 150px;
  transition: opacity 0.08s;
}
.chart-tip .t-title { font-weight: 650; margin-bottom: 6px; font-size: 12.5px; }
.chart-tip .t-row { display: flex; align-items: center; gap: 8px; margin-top: 3px; }
.chart-tip .t-row .swatch { width: 9px; height: 9px; border-radius: 2px; flex: none; }
.chart-tip .t-row .t-val { margin-left: auto; font-weight: 650; font-variant-numeric: tabular-nums; }

.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-sm); padding: 2px; }
.seg button {
  border: 0; background: none; color: var(--muted); cursor: pointer;
  font: 550 12.5px var(--sans); padding: 5px 11px; border-radius: 5px;
}
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }

/* ------------------------------------------------------------ responsive -- */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; z-index: 50; left: 0; top: 0; width: 260px;
    transform: translateX(-100%); transition: transform 0.2s;
    box-shadow: var(--shadow-lg);
  }
  .app.nav-open .sidebar { transform: none; }
  .menu-btn { display: inline-flex; }
  .content, .topbar { padding-left: 16px; padding-right: 16px; }
  .form-grid > .field { grid-column: span 12 !important; }
}
