/* ---------- Theme tokens ---------- */
:root {
  color-scheme: light;
  --bg: #F5F6F8;
  --surface: #FFFFFF;
  --surface-2: #F1F3F6;
  --surface-3: #E9ECF1;
  --border: #E3E6EB;
  --border-dim: #EEF0F3;
  --text: #0F172A;
  --text-muted: #64748B;
  --text-faint: #94A3B8;

  --red: #DC2626;
  --red-hover: #B91C1C;
  --red-text: #B91C1C;
  --blue: #2563EB;
  --success: #16A34A;
  --warning: #D97706;
  --error: #DC2626;
  --info: #2563EB;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 4px 16px rgba(15, 23, 42, 0.05);
  --shadow-lg: 0 10px 40px rgba(15, 23, 42, 0.10);
  --ring: color-mix(in srgb, var(--red) 22%, transparent);

  --radius: 12px;
  --radius-sm: 8px;

  --font-head: 'Anton', sans-serif;
  --font-body: 'Inter', 'Noto Sans Thai', system-ui, sans-serif;
  --logo: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='2.5' y='3.5' width='19' height='13' rx='2'/%3E%3Cpath d='M8 21h8M12 16.5V21'/%3E%3Cpath d='M6.5 8l2.5 2-2.5 2M11 12h3.5'/%3E%3C/svg%3E");
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0A0C10;
    --surface: #12151B;
    --surface-2: #181C23;
    --surface-3: #20252E;
    --border: #252A33;
    --border-dim: #1C2028;
    --text: #E8EAEE;
    --text-muted: #8C96A5;
    --text-faint: #5C6573;
    --red: #EF4444;
    --red-hover: #DC2626;
    --red-text: #F87171;
    --blue: #3B82F6;
    --success: #22C55E;
    --warning: #F59E0B;
    --error: #EF4444;
    --info: #60A5FA;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.25);
    --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.5);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0A0C10;
  --surface: #12151B;
  --surface-2: #181C23;
  --surface-3: #20252E;
  --border: #252A33;
  --border-dim: #1C2028;
  --text: #E8EAEE;
  --text-muted: #8C96A5;
  --text-faint: #5C6573;
  --red: #EF4444;
  --red-hover: #DC2626;
  --red-text: #F87171;
  --blue: #3B82F6;
  --success: #22C55E;
  --warning: #F59E0B;
  --error: #EF4444;
  --info: #60A5FA;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.25);
  --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.5);
}

/* ---------- Base ---------- */
* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { background: var(--bg); }
body {
  color: var(--text);
  font-family: var(--font-body);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  transition: background-color 200ms, color 200ms;
}

a { color: var(--blue); text-decoration: none; font-weight: 500; }
a:hover { text-decoration: underline; }

::selection { background: color-mix(in srgb, var(--red) 25%, transparent); }

/* ---------- Layout ---------- */
.app { display: flex; min-height: 100vh; }

.sidebar {
  width: 236px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 22px 14px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-head);
  font-size: 22px;
  letter-spacing: 1px;
  color: var(--text);
  padding: 0 10px 22px;
  text-transform: uppercase;
}
.brand::before {
  content: "";
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--logo) center / 68% no-repeat, linear-gradient(135deg, var(--red), color-mix(in srgb, var(--red) 55%, #7f1d1d));
  box-shadow: 0 4px 12px color-mix(in srgb, var(--red) 35%, transparent);
  flex-shrink: 0;
}

.role-badge {
  margin: 0 6px 14px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 8px;
}
.role-badge .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); box-shadow: 0 0 0 3px color-mix(in srgb, var(--success) 20%, transparent); }
.role-badge strong { color: var(--text); font-weight: 600; }

.nav-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.9px; color: var(--text-faint); padding: 10px 12px 6px; font-weight: 600; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  font-weight: 500;
  transition: background 160ms, color 160ms, transform 220ms cubic-bezier(0.2, 0.8, 0.2, 1);
  user-select: none;
}
.nav-item span:first-child { width: 18px; text-align: center; font-size: 15px; transition: transform 220ms cubic-bezier(0.2, 0.8, 0.2, 1); }
.nav-item:hover { background: var(--surface-2); color: var(--text); transform: translateX(3px); }
.nav-item:hover span:first-child { transform: scale(1.18); }
.nav-item:active { transform: translateX(3px) scale(0.98); }
.nav-item.active {
  background: color-mix(in srgb, var(--red) 10%, transparent);
  color: var(--red-text);
  box-shadow: inset 3px 0 0 var(--red);
}

.sidebar-foot { display: flex; flex-direction: column; gap: 6px; padding-top: 12px; border-top: 1px solid var(--border); }

.main { flex: 1; min-width: 0; padding: 36px 40px; max-width: 1280px; position: relative; }

/* ---------- Account button (top-right) ---------- */
.page-title { padding-right: 220px; }
.user-chip {
  position: absolute; top: 30px; right: 40px; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: 5px 5px 5px 14px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow-sm);
  color: var(--text); font: inherit; cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.15s;
}
.user-chip:hover { border-color: var(--surface-3); box-shadow: var(--shadow); transform: translateY(-1px); }
.user-chip:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }
.user-chip.active { border-color: var(--red); }
.user-chip .who { text-align: right; line-height: 1.2; }
.user-chip .who b { display: block; font-size: 13px; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-chip .who span { font-size: 11px; color: var(--text-muted); }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; flex: none;
  display: grid; place-items: center;
  background: var(--red); color: #fff; font-weight: 700; font-size: 15px;
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 3px var(--border);
}
.avatar.lg { width: 72px; height: 72px; font-size: 30px; box-shadow: 0 0 0 3px var(--surface), 0 0 0 4px var(--border); }
img.avatar { object-fit: cover; background: var(--surface-2); }
.avatar-edit { position: relative; padding: 0; border: 0; background: none; border-radius: 50%; cursor: pointer; flex: none; }
.avatar-edit:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }
.avatar-edit:hover .avatar { filter: brightness(0.85); }
.avatar-cam {
  position: absolute; right: -2px; bottom: -2px; width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center; font-size: 13px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); box-shadow: var(--shadow-sm);
}
.profile-head { display: flex; align-items: center; gap: 18px; margin-bottom: 28px; }


/* ---------- Typography ---------- */
h1, h2, h3, h4 { font-family: var(--font-body); font-weight: 700; letter-spacing: -0.02em; color: var(--text); }
h1 { font-size: 28px; line-height: 1.2; }
h2 { font-size: 22px; line-height: 1.25; }
h3 { font-size: 16px; line-height: 1.3; letter-spacing: -0.01em; }
h4 { font-size: 15px; line-height: 1.3; letter-spacing: -0.01em; }

.page-title { margin-bottom: 28px; }
.page-title .sub { font-size: 14px; color: var(--text-muted); margin-top: 4px; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow-sm);
}
.card.elevated { box-shadow: var(--shadow); }
.card.red { box-shadow: inset 3px 0 0 var(--red), var(--shadow-sm); }
.card.blue { box-shadow: inset 3px 0 0 var(--blue), var(--shadow-sm); }

/* ---------- Stat cards ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; margin-bottom: 32px; }
.stat-card {
  position: relative;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
  transition: transform 160ms, box-shadow 160ms;
}
.stat-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.stat-card::after {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 3px;
  background: var(--accent, var(--border));
}
.stat-card:has(.value.red) { --accent: var(--red); }
.stat-card:has(.value.blue) { --accent: var(--blue); }
.stat-card:has(.value.green) { --accent: var(--success); }
.stat-card:has(.value.amber) { --accent: var(--warning); }
.stat-card .label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--text-muted); font-weight: 600; }
.stat-card .value { font-family: var(--font-mono); font-size: 34px; font-weight: 700; margin-top: 6px; letter-spacing: -0.02em; }
.stat-card .value.red { color: var(--red); }
.stat-card .value.blue { color: var(--blue); }
.stat-card .value.green { color: var(--success); }
.stat-card .value.amber { color: var(--warning); }

/* ---------- Chips ---------- */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  white-space: nowrap;
  --c: var(--text-muted);
  color: var(--c);
  background: color-mix(in srgb, var(--c) 13%, transparent);
}
.chip::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.chip.success, .chip.low { --c: var(--success); }
.chip.warning, .chip.high { --c: var(--warning); }
.chip.error, .chip.critical { --c: var(--red-text); }
.chip.info, .chip.medium { --c: var(--info); }
.chip.muted { --c: var(--text-muted); background: var(--surface-2); border: 1px solid var(--border); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; margin: -4px -6px; }
table { width: 100%; border-collapse: separate; border-spacing: 0; }
th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  font-weight: 600;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td { padding: 11px 12px; border-bottom: 1px solid var(--border-dim); font-size: 13px; }
tbody tr:last-child td { border-bottom: none; }
tbody tr { transition: background 120ms; }
tbody tr:hover td { background: var(--surface-2); }
.mono { font-family: var(--font-mono); font-size: 12px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: var(--font-body);
  font-weight: 600;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  isolation: isolate;
  transition:
    transform 220ms cubic-bezier(0.2, 0.8, 0.2, 1),
    box-shadow 220ms ease,
    background 180ms ease,
    border-color 180ms ease,
    color 180ms ease;
}
.btn:hover:not(:disabled) { transform: translateY(-2px); }
.btn:active:not(:disabled) { transform: translateY(0) scale(0.96); transition-duration: 90ms; }

/* Light sweep across solid buttons on hover */
.btn.primary::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: linear-gradient(115deg, transparent 30%, rgba(255, 255, 255, 0.35) 50%, transparent 70%);
  transform: translateX(-120%);
  transition: transform 650ms ease;
  pointer-events: none;
}
.btn.primary:hover:not(:disabled)::before { transform: translateX(120%); }

/* Click ripple (span injected by js/buttons.js) */
.btn-ripple {
  position: absolute;
  border-radius: 50%;
  pointer-events: none;
  background: currentColor;
  opacity: 0.25;
  transform: scale(0);
  animation: btn-ripple 550ms ease-out forwards;
}
@keyframes btn-ripple { to { transform: scale(1); opacity: 0; } }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }
.btn.primary {
  background: var(--red);
  color: #fff;
  padding: 0 18px;
  font-size: 14px;
  height: 38px;
  box-shadow: 0 1px 2px color-mix(in srgb, var(--red) 30%, transparent), inset 0 1px 0 rgba(255, 255, 255, 0.15);
}
.btn.primary:hover { background: var(--red-hover); box-shadow: 0 6px 18px color-mix(in srgb, var(--red) 30%, transparent); }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); padding: 0 16px; font-size: 14px; height: 38px; box-shadow: var(--shadow-sm); }
.btn.secondary:hover { background: var(--surface-2); border-color: var(--surface-3); }
.btn.ghost { background: transparent; color: var(--text-muted); padding: 6px 12px; }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.sm { padding: 0 10px; font-size: 12px; height: 30px; }
.btn.ghost.danger { color: var(--red-text); }
.btn.ghost.danger:hover { background: color-mix(in srgb, var(--red) 12%, transparent); color: var(--red-text); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.theme-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-muted);
  font: 500 13px var(--font-body);
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: background 180ms, color 180ms, transform 220ms cubic-bezier(0.2, 0.8, 0.2, 1), box-shadow 220ms;
}
.theme-toggle:hover { color: var(--text); background: var(--surface-3); transform: translateY(-2px); box-shadow: var(--shadow); }
.theme-toggle:active { transform: scale(0.95); transition-duration: 90ms; }
.theme-toggle:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--ring); }
.theme-toggle.floating { position: fixed; top: 16px; right: 16px; z-index: 10; box-shadow: var(--shadow); background: var(--surface); }

/* ---------- Inputs ---------- */
.input {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 0 12px;
  height: 40px;
  font-family: var(--font-body);
  font-size: 14px;
  width: 100%;
  transition: border-color 140ms, box-shadow 140ms;
}
.input::placeholder { color: var(--text-faint); }
.input:hover { border-color: var(--surface-3); }
.input:focus { outline: none; border-color: var(--red); box-shadow: 0 0 0 4px var(--ring); }
select.input { cursor: pointer; }
label { display: block; font-size: 13px; font-weight: 600; color: var(--text); margin-bottom: 6px; }

/* ---------- Login ---------- */
.login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px 16px;
  background:
    radial-gradient(800px 400px at 15% -10%, color-mix(in srgb, var(--red) 14%, transparent), transparent 70%),
    radial-gradient(700px 400px at 110% 110%, color-mix(in srgb, var(--blue) 10%, transparent), transparent 70%),
    var(--bg);
}
.login-card {
  width: 380px;
  max-width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 36px 32px;
  box-shadow: var(--shadow-lg);
}
.login-card h1 {
  display: flex;
  align-items: center;
  gap: 12px;
  font-family: var(--font-head);
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-size: 30px;
  margin-bottom: 6px;
}
.login-card h1::before {
  content: "";
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--logo) center / 68% no-repeat, linear-gradient(135deg, var(--red), color-mix(in srgb, var(--red) 55%, #7f1d1d));
  box-shadow: 0 6px 16px color-mix(in srgb, var(--red) 35%, transparent);
  flex-shrink: 0;
}
.login-card .sub { color: var(--text-muted); font-size: 14px; margin-bottom: 28px; }
.field { margin-bottom: 18px; }

/* ---------- Risk overview ---------- */
.risk-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.risk-cell {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  text-align: center;
  box-shadow: var(--shadow-sm);
}
.risk-cell .n { font-family: var(--font-mono); font-size: 30px; font-weight: 700; }
.risk-cell .t { font-size: 11px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--text-muted); font-weight: 600; margin-top: 2px; }

/* ---------- Sections ---------- */
.section { margin-bottom: 32px; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.section-head h3 { font-size: 16px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.grid-2 > * { min-width: 0; }

.empty { color: var(--text-muted); text-align: center; padding: 36px 16px; font-size: 13px; }

.error-banner {
  background: color-mix(in srgb, var(--red) 10%, transparent);
  color: var(--red-text);
  border: 1px solid color-mix(in srgb, var(--red) 28%, transparent);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  margin-bottom: 16px;
  font-size: 13px;
}

.ok-banner {
  background: color-mix(in srgb, var(--success) 12%, transparent);
  color: var(--success);
  border: 1px solid color-mix(in srgb, var(--success) 30%, transparent);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  margin-bottom: 16px;
  font-size: 13px;
}
.sim-intro { display: flex; gap: 24px; justify-content: space-between; align-items: flex-start; margin-bottom: 20px; flex-wrap: wrap; }
.sim-intro > div:first-child { flex: 1 1 420px; min-width: 0; }
.sim-intro p { color: var(--text-muted); font-size: 14px; line-height: 1.6; margin: 0 0 12px; }
.sim-check { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; color: var(--text); cursor: pointer; }
.sim-check input { margin-top: 3px; }
.sim-stats { margin-top: 10px; font-size: 12px; color: var(--text-muted); }
.sim-actions { display: flex; flex-direction: column; gap: 8px; align-items: stretch; min-width: 220px; }
.sim-result { margin-bottom: 20px; }
.sim-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.sim-card { position: relative; display: flex; flex-direction: column; gap: 8px; }
.sim-card h4 { padding-right: 32px; }
.sim-card p { color: var(--text-muted); font-size: 13px; line-height: 1.5; margin: 0; flex: 1; }
.sim-card .btn { align-self: flex-start; }
.sim-n { position: absolute; top: 14px; right: 14px; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center;
  font-size: 11px; font-weight: 700; background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); }
.sim-expect { font-size: 12px; background: var(--surface-2); border-radius: var(--radius-sm); padding: 8px 10px; }
.sim-expect span { display: block; font-size: 11px; color: var(--text-muted); margin-bottom: 2px; }
.line-dot { display: inline-block; font-size: 10px; font-weight: 800; color: #fff; background: #06C755; border-radius: 6px; padding: 2px 6px; margin-right: 6px; vertical-align: 2px; }
.line-steps { margin: 0; padding-left: 20px; font-size: 14px; display: grid; gap: 12px; }
.line-steps .hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.line-code { font-size: 30px; font-weight: 700; letter-spacing: 6px; margin-top: 6px; padding: 8px 14px; display: inline-block;
  background: var(--surface-2); border: 1px dashed var(--border); border-radius: var(--radius-sm); user-select: all; }
.profile-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 20px; align-items: start; }
.profile-grid > * { min-width: 0; }
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 8px 12px; font-size: 14px; }
.kv dt { color: var(--text-muted); }
.kv dd { margin: 0; word-break: break-all; }
.globe-row { display: grid; grid-template-columns: minmax(0, 2fr) minmax(220px, 1fr); gap: 20px; align-items: start; }
@media (max-width: 900px) { .globe-row { grid-template-columns: 1fr; } }

.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar .input { width: auto; min-width: 180px; flex: 1 1 180px; max-width: 260px; }

/* ---------- Data-viz tokens (validated: categorical slots 1-3, ordinal blue ramp) ---------- */
:root {
  --viz-1: #2a78d6;        /* success / accent */
  --viz-2: #eb6834;        /* failed */
  --viz-3: #1baf7a;        /* other events (below 3:1 → always labeled + table view) */
  --viz-ord-1: #86b6ef; --viz-ord-2: #3987e5; --viz-ord-3: #1c5cab; --viz-ord-4: #0d366b;
  --viz-grid: #ECEEF2;
  --viz-axis: #CBD2DC;
  --viz-muted: #8A93A3;
  --viz-spark: #B9C2CF;
  --status-good: #0ca30c; --status-warning: #fab219; --status-serious: #ec835a; --status-critical: #d03b3b;
  --delta-good: #006300; --delta-bad: #c02c2c;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --viz-1: #3987e5; --viz-2: #d95926; --viz-3: #199e70;
    --viz-ord-1: #184f95; --viz-ord-2: #256abf; --viz-ord-3: #5598e7; --viz-ord-4: #9ec5f4;
    --viz-grid: #20252E; --viz-axis: #313845; --viz-muted: #7F8999; --viz-spark: #3A4252;
    --delta-good: #0ca30c; --delta-bad: #ef5b5b;
  }
}
:root[data-theme="dark"] {
  --viz-1: #3987e5; --viz-2: #d95926; --viz-3: #199e70;
  --viz-ord-1: #184f95; --viz-ord-2: #256abf; --viz-ord-3: #5598e7; --viz-ord-4: #9ec5f4;
  --viz-grid: #20252E; --viz-axis: #313845; --viz-muted: #7F8999; --viz-spark: #3A4252;
  --delta-good: #0ca30c; --delta-bad: #ef5b5b;
}

/* ---------- Analytics dashboard ---------- */
.dash { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: 18px; align-items: start; }
.dash-main { display: grid; gap: 16px; min-width: 0; transition: opacity 200ms; }
.dash-main.loading { opacity: 0.55; }
.dash-filters { position: sticky; top: 20px; display: flex; flex-direction: column; gap: 14px; }
.dash-filters h3 { display: flex; align-items: center; gap: 8px; font-size: 15px; padding-bottom: 10px; border-bottom: 1px solid var(--border); }
.dash-filters label { font-size: 12px; color: var(--text-muted); font-weight: 600; margin-bottom: 4px; }
.dash-filters .input { height: 36px; font-size: 13px; }
.dash-filters .meta { font-size: 12px; color: var(--text-faint); line-height: 1.5; }

.kpi-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.kpi {
  position: relative; padding: 16px 18px 14px; display: grid; gap: 4px 12px;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas: "label label" "value spark" "delta delta";
}
.kpi .k-label { grid-area: label; font-size: 13px; color: var(--text-muted); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi .k-value { grid-area: value; font-size: 28px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.15; color: var(--text); align-self: end; }
.kpi .k-delta { grid-area: delta; font-size: 12px; color: var(--text-muted); }
.kpi .k-delta b { font-weight: 700; }
.kpi .k-delta .good { color: var(--delta-good); }
.kpi .k-delta .bad { color: var(--delta-bad); }
.kpi .k-spark { grid-area: spark; align-self: end; }

.viz-grid { display: grid; gap: 16px; grid-template-columns: repeat(12, minmax(0, 1fr)); }
.span-3 { grid-column: span 3; } .span-4 { grid-column: span 4; } .span-5 { grid-column: span 5; }
.span-6 { grid-column: span 6; } .span-7 { grid-column: span 7; } .span-8 { grid-column: span 8; } .span-12 { grid-column: span 12; }

.viz-card { position: relative; padding: 16px 18px; min-width: 0; display: flex; flex-direction: column; gap: 12px; }
.viz-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.viz-head h3 { font-size: 15px; }
.viz-head .sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.viz-toggle { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; flex-shrink: 0; }
.viz-toggle button { border: 0; background: transparent; color: var(--text-muted); font: 600 11px var(--font-body); padding: 4px 8px; cursor: pointer; }
.viz-toggle button.on { background: var(--surface-2); color: var(--text); }
.viz-empty { color: var(--text-muted); font-size: 13px; text-align: center; padding: 28px 8px; }
.viz-table { width: 100%; font-size: 12px; }
.viz-table td, .viz-table th { padding: 6px 8px; }
.viz-table td.num { text-align: right; font-variant-numeric: tabular-nums; }

.viz-tip {
  position: absolute; z-index: 30; pointer-events: none;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  padding: 8px 10px; font-size: 12px; line-height: 1.5; white-space: nowrap;
  transform: translate(-50%, calc(-100% - 10px));
}
.viz-tip .t { color: var(--text-muted); font-size: 11px; margin-bottom: 2px; }
.viz-tip .r { display: flex; align-items: center; gap: 6px; }
.viz-tip .r b { margin-left: auto; padding-left: 12px; font-variant-numeric: tabular-nums; }
.sw { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; display: inline-block; }
.sw.line { height: 3px; border-radius: 2px; width: 14px; }

.donut-wrap { display: flex; flex-direction: column; align-items: stretch; gap: 16px; }
.donut-wrap svg { flex-shrink: 0; align-self: center; }
.legend { display: grid; gap: 8px; min-width: 0; font-size: 13px; }
.legend .li { display: grid; grid-template-columns: 12px minmax(0, 1fr) auto auto; align-items: center; gap: 8px; }
.legend .li .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.legend .li .v { font-variant-numeric: tabular-nums; color: var(--text); font-weight: 600; }
.legend .li .p { font-variant-numeric: tabular-nums; color: var(--text-muted); width: 46px; text-align: right; }
.legend .li.dim { opacity: 0.45; }

.hbars { display: grid; gap: 10px; }
.hbar { display: grid; grid-template-columns: minmax(0, 1fr); gap: 4px; cursor: default; }
.hbar .top { display: flex; justify-content: space-between; gap: 8px; font-size: 12.5px; }
.hbar .top .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.hbar .top .v { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--text); }
.hbar .track { height: 10px; position: relative; }
.hbar .fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 0 4px 4px 0; transition: width 400ms cubic-bezier(0.2, 0.8, 0.2, 1); }
.hbar:hover .fill { filter: brightness(1.1); }
.hbar .meta { font-size: 11px; color: var(--text-muted); }

.line-legend { display: flex; gap: 16px; font-size: 12px; color: var(--text-muted); }
.line-legend span { display: inline-flex; align-items: center; gap: 6px; }
.linechart text { font-family: var(--font-body); }

.risk-rows { display: grid; gap: 14px; }
.risk-row { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; grid-template-areas: "rl n p" "bar bar bar"; align-items: center; gap: 6px 12px; font-size: 13px; }
.risk-row .rl { grid-area: rl; display: flex; align-items: center; gap: 6px; font-weight: 600; color: var(--text); }
.risk-row .rl i { font-style: normal; font-size: 12px; width: 16px; text-align: center; }
.risk-row .n { grid-area: n; }
.risk-row .p { grid-area: p; min-width: 44px; }
.risk-row .bar { grid-area: bar; height: 10px; background: var(--surface-2); border-radius: 0 4px 4px 0; position: relative; }
.risk-row .bar > span { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 0 4px 4px 0; transition: width 400ms; }
.risk-row .n { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.risk-row .p { text-align: right; color: var(--text-muted); font-variant-numeric: tabular-nums; }

/* Analysis modal */
.modal-back { position: fixed; inset: 0; z-index: 50; background: rgba(8, 10, 14, 0.55); display: flex; justify-content: flex-end; animation: fade-in 160ms ease; }
.modal-panel { width: min(640px, 100%); height: 100%; overflow-y: auto; background: var(--bg); border-left: 1px solid var(--border); padding: 24px; display: grid; gap: 16px; align-content: start; animation: slide-in 260ms cubic-bezier(0.2, 0.8, 0.2, 1); }
@keyframes fade-in { from { opacity: 0; } }
@keyframes slide-in { from { transform: translateX(40px); opacity: 0; } }
.verdict { border-radius: var(--radius); padding: 16px 18px; border: 1px solid var(--border); background: var(--surface); box-shadow: inset 4px 0 0 var(--vc, var(--text-muted)); }
.verdict .vr { display: flex; align-items: center; gap: 8px; font-size: 12px; font-weight: 700; letter-spacing: 0.6px; text-transform: uppercase; color: var(--text-muted); }
.verdict .vt { font-size: 16px; font-weight: 700; margin-top: 6px; color: var(--text); }
.seq { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.seq .ph { padding: 6px 10px; border-radius: 8px; background: var(--surface); border: 1px solid var(--border); font-size: 12.5px; font-weight: 600; }
.seq .ph small { color: var(--text-muted); font-weight: 500; margin-left: 4px; }
.seq .arrow { color: var(--text-faint); }
.mini-stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.mini-stats .card { padding: 12px 14px; }
.mini-stats .l { font-size: 11px; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.6px; }
.mini-stats .v { font-size: 20px; font-weight: 700; margin-top: 2px; }
.timeline { display: grid; gap: 0; font-size: 12.5px; }
.timeline .ev { display: grid; grid-template-columns: 190px 14px minmax(0, 1fr); gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--border-dim); align-items: center; }
.timeline .ev .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-faint); box-shadow: 0 0 0 2px var(--bg); }

@media (max-width: 1200px) {
  .span-3, .span-4, .span-5 { grid-column: span 6; }
  .span-7, .span-8 { grid-column: span 12; }
}
@media (max-width: 1000px) {
  .dash { grid-template-columns: minmax(0, 1fr); }
  .dash-filters { position: static; display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); align-items: end; }
  .dash-filters h3, .dash-filters .meta { grid-column: 1 / -1; }
  .kpi-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 640px) {
  .span-3, .span-4, .span-5, .span-6 { grid-column: span 12; }
  .kpi .k-spark { display: none; }
  .kpi .k-value { font-size: 24px; }
  .timeline .ev { grid-template-columns: 1fr; gap: 2px; }
  .timeline .ev .dot { display: none; }
  .mini-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  .hbar .fill, .risk-row .bar > span, .modal-back, .modal-panel { transition: none; animation: none; }
  .btn, .theme-toggle, .nav-item, .nav-item span:first-child { transition: none; }
  .btn:hover:not(:disabled), .theme-toggle:hover, .nav-item:hover, .nav-item:hover span:first-child { transform: none; }
  .btn.primary::before, .btn-ripple { display: none; }
}

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .grid-2 { grid-template-columns: 1fr; }
  .main { padding: 28px 24px; }
  .user-chip { top: 22px; right: 24px; padding: 4px; }
  .user-chip .who { display: none; }
  .page-title { padding-right: 56px; }
}

@media (max-width: 760px) {
  .app { flex-direction: column; }
  .sidebar {
    width: 100%;
    height: auto;
    flex-direction: row;
    align-items: center;
    gap: 4px;
    padding: 10px 16px;
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    scrollbar-width: none;
    z-index: 5;
  }
  .sidebar::-webkit-scrollbar { display: none; }
  .brand { padding: 0 8px 0 0; font-size: 18px; white-space: nowrap; }
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .stat-card { padding: 14px 16px; }
  .stat-card .value { font-size: 28px; }
  .theme-toggle { white-space: nowrap; }
  .brand::before { width: 22px; height: 22px; border-radius: 6px; }
  .role-badge, .nav-label, .sidebar .spacer { display: none; }
  .nav-item { white-space: nowrap; padding: 8px 10px; }
  .nav-item.active { box-shadow: inset 0 -2px 0 var(--red); }
  .sidebar-foot { flex-direction: row; border-top: none; padding-top: 0; margin-left: auto; }
  .main { padding: 24px 16px; }
  .risk-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .toolbar .input { max-width: none; }
  h1 { font-size: 24px; }
}
