/* ============================================================
   DASHBOARD – CSS Custom Properties + Layout
   Compatible with Jinja2/Flask/Django
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@300;400;500;600&display=swap');

/* ── Tokens ─────────────────────────────────────────────── */
:root {
  --font: 'IBM Plex Sans', system-ui, sans-serif;

  /* Surfaces */
  --bg:               #f7f7f8;
  --surface:          #ffffff;
  --surface-hover:    #f4f4f6;
  --surface-active:   #ededf0;

  /* Sidebars */
  --sb1-bg:           transparent;          /* no custom background */
  --sb1-text:         #71717a;              /* readable on light bg */
  --sb1-text-active:  #18181b;
  --sb1-active-bg:    rgba(0,0,0,0.06);
  --sb1-hover-bg:     rgba(0,0,0,0.04);
  --sb1-border:       #e4e4e7;              /* visible separator on light bg */

  --sb2-bg:           #ffffff;
  --sb2-text:         #52525b;
  --sb2-text-active:  #18181b;
  --sb2-active-bg:    #ededf0;
  --sb2-hover-bg:     #f4f4f6;
  --sb2-border:       #e4e4e7;
  --sb2-section:      #a1a1aa;

  /* Topbar */
  --topbar-bg:        #ffffff;
  --topbar-border:    #e4e4e7;

  /* Text */
  --text:             #18181b;
  --text-muted:       #71717a;
  --text-subtle:      #a1a1aa;

  /* Accent */
  --accent:           oklch(0.52 0.18 245);
  --accent-light:     oklch(0.92 0.06 245);
  --accent-text:      #ffffff;

  /* Status */
  --green:            oklch(0.54 0.16 155);
  --red:              oklch(0.52 0.18 25);
  --amber:            oklch(0.66 0.16 75);

  /* Border & shadow */
  --border:           #e4e4e7;
  --ring:             rgba(37,99,235,0.25);
  --shadow-sm:        0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow:           0 4px 16px rgba(0,0,0,0.07);
  --shadow-lg:        0 8px 32px rgba(0,0,0,0.10);

  /* Sizes */
  --sb1-w:            60px;
  --sb2-w:            220px;
  --topbar-h:         52px;
  --drawer-w:         320px;
  --radius:           6px;
  --radius-lg:        10px;
}

/* Dark mode */
[data-theme="dark"] {
  --bg:               #0f0f13;
  --surface:          #1a1a21;
  --surface-hover:    #22222b;
  --surface-active:   #2a2a35;

  --sb1-bg:           transparent;
  --sb1-text:         #8888a0;
  --sb1-text-active:  #e4e4f0;
  --sb1-active-bg:    rgba(255,255,255,0.08);
  --sb1-hover-bg:     rgba(255,255,255,0.05);
  --sb1-border:       #2a2a38;              /* matches dark --border, visible on dark bg */

  --sb2-bg:           #13131a;
  --sb2-text:         #9090a8;   /* was #6e6e85 — lifted for WCAG AA */
  --sb2-text-active:  #e4e4f0;
  --sb2-active-bg:    #22222b;
  --sb2-hover-bg:     #1e1e27;
  --sb2-border:       #2a2a38;
  --sb2-section:      #5a5a78;   /* was #3f3f55 — lifted */

  --topbar-bg:        #13131a;
  --topbar-border:    #232330;

  --text:             #e4e4f0;
  --text-muted:       #6e6e85;
  --text-subtle:      #3f3f55;

  --accent:           oklch(0.60 0.18 245);
  --accent-light:     oklch(0.22 0.08 245);

  --border:           #232330;
  --ring:             rgba(96,165,250,0.25);
  --shadow-sm:        0 1px 3px rgba(0,0,0,0.3);
  --shadow:           0 4px 16px rgba(0,0,0,0.35);
  --shadow-lg:        0 8px 32px rgba(0,0,0,0.45);
}

/* ── Reset ───────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* Global focus-visible ring */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius);
}

/* Remove default outline when focus-visible is supported */
:focus:not(:focus-visible) { outline: none; }

html, body {
  height: 100%;
  font-family: var(--font);
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

/* ── App shell ───────────────────────────────────────────── */
.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Primary Sidebar (SB1) ───────────────────────────────── */
.sb1 {
  width: var(--sb1-w);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  border-right: 1px solid var(--sb1-border);
  z-index: 30;
  transition: width 0.22s ease;
  overflow: hidden;
}

.sb1.expanded {
  width: 200px;
}

.sb1-logo {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid var(--sb1-border);
  flex-shrink: 0;
}

.sb1-logo-icon {
  width: 28px;
  height: 28px;
  background: var(--accent);
  border-radius: 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.sb1-logo-icon svg { width: 16px; height: 16px; fill: white; }

.sb1-logo-label {
  color: var(--sb1-text-active);
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.01em;
  white-space: nowrap;
  margin-left: 10px;
  opacity: 0;
  transition: opacity 0.18s;
}

.sb1.expanded .sb1-logo-label { opacity: 1; }

.sb1-nav {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 10px 0;
  scrollbar-width: none;
}

.sb1-nav::-webkit-scrollbar { display: none; }

.sb1-group {
  margin-bottom: 2px;
}

.sb1-group-label {
  font-size: 9px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-subtle);
  padding: 12px 0 4px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  opacity: 0;
  transition: opacity 0.18s;
}

.sb1.expanded .sb1-group-label {
  opacity: 1;
  text-align: left;
  padding-left: 14px;
}

.sb1-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 10px;
  height: 38px;
  cursor: pointer;
  border-radius: var(--radius);
  margin: 1px 6px;
  color: var(--sb1-text);
  transition: background 0.12s, color 0.12s;
  white-space: nowrap;
  overflow: hidden;
  text-decoration: none;
  position: relative;
}

.sb1-item:hover  { background: var(--sb1-hover-bg); color: var(--sb1-text-active); }
.sb1-item.active { background: var(--sb1-active-bg); color: var(--sb1-text-active); box-shadow: inset 2px 0 0 var(--accent); }

.sb1-item:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.sb1-item-icon {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border-radius: 7px;
  transition: background 0.12s;
}

.sb1-item.active .sb1-item-icon {
  background: oklch(from var(--accent) l c h / 0.18);
  color: var(--accent);
}

.sb1-item-icon svg {
  width: 17px;
  height: 17px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sb1-item-label {
  font-size: 13px;
  font-weight: 400;
  opacity: 0;
  transition: opacity 0.18s;
}

.sb1.expanded .sb1-item-label { opacity: 1; }

.sb1-badge {
  margin-left: auto;
  background: var(--accent);
  color: white;
  font-size: 10px;
  font-weight: 600;
  padding: 1px 5px;
  border-radius: 20px;
  opacity: 0;
  transition: opacity 0.18s;
}

.sb1.expanded .sb1-badge { opacity: 1; }

.sb1-bottom {
  border-top: 1px solid var(--sb1-border);
  padding: 8px 0;
}

/* ── Secondary Sidebar (SB2) ─────────────────────────────── */
.sb2 {
  width: var(--sb2-w);
  background: var(--sb2-bg);
  border-right: 1px solid var(--sb2-border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 20;
  transition: width 0.22s ease, opacity 0.18s;
  overflow: hidden;
}

.sb2.collapsed {
  width: 0;
  opacity: 0;
  pointer-events: none;
}

.sb2-header {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  border-bottom: 1px solid var(--sb2-border);
  gap: 8px;
  flex-shrink: 0;
}

.sb2-header-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.sb2-header-close {
  margin-left: auto;
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
  cursor: pointer;
  border: none;
  background: none;
  color: var(--text-muted);
  transition: background 0.12s, color 0.12s;
}

.sb2-header-close:hover { background: var(--surface-hover); color: var(--text); }

.sb2-header-close svg {
  width: 14px;
  height: 14px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
}

.sb2-nav {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 10px 0;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.sb2-section {
  margin-bottom: 4px;
}

.sb2-section-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--sb2-section);
  padding: 10px 16px 4px;
  white-space: nowrap;
}

.sb2-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 0 10px;
  height: 34px;
  border-radius: var(--radius);
  margin: 1px 6px;
  cursor: pointer;
  color: var(--sb2-text);
  text-decoration: none;
  transition: background 0.12s, color 0.12s;
  white-space: nowrap;
}

.sb2-item:hover { background: var(--sb2-hover-bg); color: var(--sb2-text-active); }
.sb2-item.active {
  background: var(--sb2-active-bg);
  color: var(--sb2-text-active);
  font-weight: 500;
  box-shadow: inset 2px 0 0 var(--accent);
}

.sb2-item:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  border-radius: var(--radius);
}

.sb2-item-icon {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.sb2-item-icon svg {
  width: 15px;
  height: 15px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sb2-item-label { font-size: 13px; }

.sb2-item-badge {
  margin-left: auto;
  background: var(--accent-light);
  color: var(--accent);
  font-size: 10px;
  font-weight: 600;
  padding: 1px 5px;
  border-radius: 20px;
}

/* ── Main area ───────────────────────────────────────────── */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
  position: relative;
}

/* ── Topbar ──────────────────────────────────────────────── */
.topbar {
  height: var(--topbar-h);
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--topbar-border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 12px;
  flex-shrink: 0;
  z-index: 10;
}

.topbar-toggle {
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
  cursor: pointer;
  border: none;
  background: none;
  color: var(--text-muted);
  transition: background 0.12s, color 0.12s;
  flex-shrink: 0;
}

.topbar-toggle:hover { background: var(--surface-hover); color: var(--text); }

.topbar-toggle svg {
  width: 16px; height: 16px;
  stroke: currentColor; fill: none;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

.topbar-breadcrumb {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
}

.topbar-breadcrumb-sep { color: var(--border); }
.topbar-breadcrumb-current { color: var(--text); font-weight: 500; }

.topbar-search {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-hover);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0 12px;
  height: 32px;
  width: 220px;
  transition: border-color 0.12s, box-shadow 0.12s;
}

.topbar-search:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--ring);
}

.topbar-search svg {
  width: 14px; height: 14px;
  stroke: var(--text-muted); fill: none;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
  flex-shrink: 0;
}

.topbar-search input {
  border: none;
  background: transparent;
  outline: none;
  font-family: var(--font);
  font-size: 13px;
  color: var(--text);
  width: 100%;
}

.topbar-search input::placeholder { color: var(--text-subtle); }

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.topbar-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  cursor: pointer;
  border: none;
  background: none;
  color: var(--text-muted);
  transition: background 0.12s, color 0.12s;
  position: relative;
}

.topbar-btn:hover { background: var(--surface-hover); color: var(--text); }

.topbar-btn svg {
  width: 17px; height: 17px;
  stroke: currentColor; fill: none;
  stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round;
}

.topbar-btn-dot {
  position: absolute;
  top: 5px; right: 5px;
  width: 7px; height: 7px;
  background: var(--red);
  border-radius: 50%;
  border: 1.5px solid var(--topbar-bg);
}

.topbar-divider {
  width: 1px;
  height: 22px;
  background: var(--border);
  margin: 0 4px;
}

.topbar-user {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.12s;
}

.topbar-user:hover { background: var(--surface-hover); }

.topbar-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  color: white;
  flex-shrink: 0;
}

.topbar-user-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
}

/* ── Content ─────────────────────────────────────────────── */
.content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 28px 32px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.content-header {
  margin-bottom: 24px;
}

.content-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
}

.content-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 3px;
}

/* ── Stat cards ──────────────────────────────────────────── */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 28px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}

.stat-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.stat-value {
  font-size: 28px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.02em;
  margin-top: 6px;
  line-height: 1;
}

.stat-delta {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 12px;
  font-weight: 500;
  margin-top: 8px;
  padding: 2px 6px;
  border-radius: 20px;
}

.stat-delta.up { color: var(--green); background: oklch(0.95 0.04 155); }
.stat-delta.down { color: var(--red); background: oklch(0.95 0.04 25); }

[data-theme="dark"] .stat-delta.up { background: oklch(0.22 0.06 155); }
[data-theme="dark"] .stat-delta.down { background: oklch(0.22 0.06 25); }

/* ── Table ───────────────────────────────────────────────── */
.table-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  margin-bottom: 24px;
}

.table-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.table-title { font-size: 14px; font-weight: 600; color: var(--text); }
.table-subtitle { font-size: 12px; color: var(--text-muted); margin-top: 1px; }

table {
  width: 100%;
  border-collapse: collapse;
}

th {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  text-align: left;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

td {
  padding: 12px 20px;
  font-size: 13px;
  color: var(--text);
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tr:last-child td { border-bottom: none; }

tbody tr { transition: background 0.1s; }
tbody tr:hover { background: var(--surface-hover); }

/* ── Badges ──────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
}

.badge-green { color: var(--green); background: oklch(0.94 0.05 155); }
.badge-red   { color: var(--red);   background: oklch(0.94 0.05 25); }
.badge-amber { color: var(--amber); background: oklch(0.96 0.06 75); }
.badge-blue  { color: var(--accent); background: var(--accent-light); }

[data-theme="dark"] .badge-green { background: oklch(0.22 0.07 155); }
[data-theme="dark"] .badge-red   { background: oklch(0.22 0.07 25); }
[data-theme="dark"] .badge-amber { background: oklch(0.26 0.07 75); }
[data-theme="dark"] .badge-blue  { background: var(--accent-light); }

/* ── Buttons ─────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 14px;
  height: 32px;
  border-radius: var(--radius);
  font-family: var(--font);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: background 0.12s, opacity 0.12s;
}

.btn svg {
  width: 14px; height: 14px;
  stroke: currentColor; fill: none;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { opacity: 0.88; }

.btn-ghost {
  background: var(--surface-active);
  color: var(--text-muted);
  border: none;
}

.btn-ghost:hover { background: var(--surface-hover); color: var(--text); }

[data-theme="dark"] .btn-ghost {
  background: var(--surface-active);
  color: var(--text-muted);
}

[data-theme="dark"] .btn-ghost:hover {
  background: var(--surface-hover);
  color: var(--text);
}

/* ── Right Drawer ─────────────────────────────────────────── */
.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.3);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s;
}

.drawer-overlay.open { opacity: 1; pointer-events: all; }

.drawer {
  position: fixed;
  top: 0; right: 0;
  width: var(--drawer-w);
  height: 100%;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  z-index: 50;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.drawer.open { transform: translateX(0); }

.drawer-header {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  gap: 10px;
  flex-shrink: 0;
}

.drawer-title { font-size: 14px; font-weight: 600; color: var(--text); }

.drawer-close {
  margin-left: auto;
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 5px; cursor: pointer;
  color: var(--text-muted);
  transition: background 0.12s;
}

.drawer-close:hover { background: var(--surface-hover); color: var(--text); }

.drawer-close svg {
  width: 15px; height: 15px;
  stroke: currentColor; fill: none;
  stroke-width: 2.2; stroke-linecap: round;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px 18px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

/* Drawer content components */
.drawer-section { margin-bottom: 24px; }
.drawer-section-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.drawer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.drawer-row:last-child { border-bottom: none; }
.drawer-row-label { color: var(--text-muted); }
.drawer-row-value { color: var(--text); font-weight: 500; }

/* ── Form elements ───────────────────────────────────────── */
.form-group { margin-bottom: 20px; }

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.form-input {
  width: 100%;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font);
  font-size: 13px;
  outline: none;
  transition: border-color 0.12s, box-shadow 0.12s;
}

.form-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--ring);
}

.form-select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2371717a' stroke-width='2' stroke-linecap='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}

/* ── Toggle switch ───────────────────────────────────────── */
.toggle {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
  flex-shrink: 0;
}

.toggle input { display: none; }

.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 20px;
  cursor: pointer;
  transition: background 0.15s;
}

.toggle-slider::after {
  content: '';
  position: absolute;
  left: 3px; top: 3px;
  width: 14px; height: 14px;
  background: white;
  border-radius: 50%;
  transition: transform 0.15s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.toggle input:checked + .toggle-slider { background: var(--accent); }
.toggle input:checked + .toggle-slider::after { transform: translateX(16px); }

/* ── Settings rows ───────────────────────────────────────── */
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}

.settings-row:last-child { border-bottom: none; }
.settings-row-left {}
.settings-row-key { font-size: 13px; font-weight: 500; color: var(--text); }
.settings-row-desc { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* ── Chart placeholder ───────────────────────────────────── */
.chart-placeholder {
  background: repeating-linear-gradient(
    -45deg,
    transparent,
    transparent 6px,
    var(--surface-hover) 6px,
    var(--surface-hover) 7px
  );
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-subtle);
  font-family: monospace;
  font-size: 12px;
}

/* ── Utility ─────────────────────────────────────────────── */
.flex { display: flex; }
.items-center { align-items: center; }
.gap-2 { gap: 8px; }
.gap-3 { gap: 12px; }
.ml-auto { margin-left: auto; }
.mt-1 { margin-top: 4px; }
.mt-2 { margin-top: 8px; }
.mt-4 { margin-top: 16px; }
.mb-4 { margin-bottom: 16px; }
.text-muted { color: var(--text-muted); font-size: 12px; }

/* ── Classifier UI ───────────────────────────────────────── */
.clf-textarea {
  height: auto;
  padding: 10px;
  resize: vertical;
  font-family: var(--font);
  line-height: 1.5;
}

.clf-rules-textarea { min-height: 120px; }

/* Result card — code display */
.clf-code {
  font-family: 'IBM Plex Mono', 'Fira Mono', monospace;
  font-size: 20px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.02em;
  word-break: break-all;
}

/* Fields breakdown */
.clf-field-row {
  display: flex;
  align-items: baseline;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  gap: 16px;
}

.clf-field-row:last-child { border-bottom: none; }

.clf-field-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  flex-shrink: 0;
  min-width: 160px;
}

.clf-field-value {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
}

/* Section labels inside result card */
.clf-section-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.clf-error-title { color: var(--red); }

.clf-reasoning {
  font-size: 13px;
  color: var(--text);
  line-height: 1.65;
}

/* Markdown content inside .clf-reasoning */
.clf-reasoning p                     { margin: 0 0 8px; }
.clf-reasoning p:last-child          { margin-bottom: 0; }
.clf-reasoning strong                { font-weight: 600; }
.clf-reasoning em                    { font-style: italic; }
.clf-reasoning ul, .clf-reasoning ol { padding-left: 1.4em; margin: 4px 0 8px; }
.clf-reasoning li                    { margin: 2px 0; }
.clf-reasoning h1, .clf-reasoning h2, .clf-reasoning h3 {
  font-size: 13px; font-weight: 600; margin: 8px 0 4px;
}
.clf-reasoning code {
  font-family: 'IBM Plex Mono', monospace;
  font-size: .88em;
  background: var(--border);
  padding: 1px 4px;
  border-radius: 3px;
}

/* ── Evidence card ────────────────────────────────────────── */
.ev-group {
  border-top: 1px solid var(--border);
}
.ev-group:first-of-type { border-top: none; }
.ev-group > summary { list-style: none; }
.ev-group > summary::-webkit-details-marker { display: none; }

.ev-group-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}
.ev-group-header:hover { background: rgba(0,0,0,.02); }
.ev-group-title { flex: 1; }

.ev-chevron {
  width: 16px; height: 16px; flex-shrink: 0;
  stroke: var(--text-muted); fill: none; stroke-width: 2;
  transition: transform .2s;
}
details[open] > summary .ev-chevron { transform: rotate(180deg); }

.ev-docs-list {
  padding: 0 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.ev-empty {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  padding: 4px 0;
}

.ev-doc-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  background: var(--bg);
}

.ev-doc-desc {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 3px;
}
.ev-doc-type-tag {
  font-size: 11px;
  font-weight: 400;
  color: var(--text-muted);
  margin-left: 8px;
}

.ev-doc-code {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  color: var(--accent);
  margin-bottom: 7px;
  word-break: break-all;
}

.ev-doc-note {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  margin-bottom: 7px;
}

.ev-doc-badges {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.ev-doc-raw { margin-top: 6px; }
.ev-doc-raw > summary {
  list-style: none;
  cursor: pointer;
  font-size: 11px;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  user-select: none;
}
.ev-doc-raw > summary::-webkit-details-marker { display: none; }
.ev-doc-raw > summary::before {
  content: '▶';
  font-size: 8px;
  transition: transform .15s;
}
details[open].ev-doc-raw > summary::before { transform: rotate(90deg); }

.ev-doc-json {
  margin-top: 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  line-height: 1.55;
  overflow-x: auto;
  white-space: pre;
  color: var(--text);
  background: var(--bg-code, #f6f8fa);
  max-height: 320px;
  overflow-y: auto;
}

.clf-error-item {
  font-size: 12px;
  color: var(--red);
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
}

.clf-error-item:last-child { border-bottom: none; }

/* Config row (parameters + rules side by side) */
.clf-config-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 24px;
  align-items: start;
}

@media (max-width: 860px) {
  .clf-config-row { grid-template-columns: 1fr; }
}

/* Collapsible details */
.clf-details { margin-bottom: 0; }
.clf-details[open] .clf-chevron { transform: rotate(180deg); }
.clf-chevron {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  color: var(--text-muted);
  transition: transform 0.18s;
}

.clf-summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
}

.clf-summary::-webkit-details-marker { display: none; }

/* Parameter grid */
.clf-param-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 16px;
}

@media (max-width: 500px) {
  .clf-param-grid { grid-template-columns: 1fr; }
}

/* Loading spinner */
.clf-spinner {
  display: inline-block;
  width: 13px;
  height: 13px;
  border: 2px solid rgba(255,255,255,0.35);
  border-top-color: white;
  border-radius: 50%;
  animation: clf-spin 0.7s linear infinite;
  vertical-align: middle;
  margin-right: 6px;
}

@keyframes clf-spin { to { transform: rotate(360deg); } }

/* Status text */
.clf-status {
  font-size: 12px;
  color: var(--text-muted);
}

.clf-status-ok    { color: var(--green); }
.clf-status-error { color: var(--red); }
.clf-status-loading { color: var(--text-muted); }

/* ── Batch ───────────────────────────────────────────────── */

/* Import options row */
.batch-import-options {
  display: flex;
  gap: 28px;
  flex-wrap: wrap;
  align-items: flex-start;
}
.batch-option-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.batch-option-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.batch-delim-group {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
.batch-delim-btn {
  padding: 5px 12px;
  font-size: 12px;
  font-family: var(--font);
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text-muted);
  transition: border-color .12s, background .12s, color .12s;
  white-space: nowrap;
}
.batch-delim-btn:hover  { border-color: var(--accent); color: var(--text); }
.batch-delim-btn.active {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
  font-weight: 500;
}
.batch-toggle-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.batch-rows-input {
  width: 100px;
  padding: 5px 10px;
  font-size: 13px;
}

/* Column checkbox grid */
.batch-col-check-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 6px 12px;
  max-height: 200px;
  overflow-y: auto;
  padding: 2px 0 4px;
}
.batch-col-check {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text);
  cursor: pointer;
  min-width: 0;
}
.batch-col-check input[type="checkbox"] { flex-shrink: 0; cursor: pointer; }
.batch-col-check span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.batch-drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 48px 24px;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.batch-drop-zone:hover,
.batch-drop-zone.dragover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 5%, transparent);
}
.batch-drop-zone svg {
  width: 40px;
  height: 40px;
  display: block;
  margin: 0 auto 12px;
  stroke: var(--text-muted);
}
.batch-drop-zone-label {
  font-size: 14px;
  color: var(--text);
  margin-bottom: 4px;
}
.batch-drop-zone-sub {
  font-size: 12px;
  color: var(--text-muted);
}

.batch-file-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px;
  background: var(--surface-hover);
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 13px;
  color: var(--text);
}
.batch-file-chip svg {
  width: 14px;
  height: 14px;
  stroke: var(--text-muted);
  flex-shrink: 0;
}

.batch-select {
  cursor: pointer;
}

.batch-progress-wrap {
  margin-bottom: 8px;
}
.batch-progress-label {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
}
.batch-progress-bar {
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}
.batch-progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  transition: width .25s ease;
  width: 0%;
}

.batch-summary {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  font-size: 13px;
}
.batch-summary-item {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text-muted);
}
.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot-green { background: var(--green); }
.dot-red   { background: var(--red); }

/* Data table */
.data-table-wrap {
  overflow-x: auto;
  max-height: 500px;
  overflow-y: auto;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}
.data-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
  font-weight: 500;
  white-space: nowrap;
  color: var(--text-muted);
}
.data-table th.clf-col { color: var(--accent); }
.data-table td {
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
}
.data-table tr:last-child td { border-bottom: none; }
.data-table td.clf-col {
  font-family: 'IBM Plex Mono', monospace;
  color: var(--accent);
}
.data-table td.error-col {
  color: var(--red);
  font-size: 11px;
}
.data-table tr.row-error td {
  background: color-mix(in srgb, var(--red) 6%, transparent);
}

/* ── Batch row expand / detail panel ────────────────────── */
.batch-expand-th { width: 32px; padding: 0 !important; }

.batch-expand-cell {
  width: 32px;
  padding: 0 !important;
  text-align: center;
}

.batch-expand-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  width: 28px; height: 28px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 4px;
  font-size: 10px;
  transition: background .15s, color .15s, transform .2s;
}
.batch-expand-btn:hover { background: var(--border); color: var(--text); }
.batch-expand-btn.expanded { transform: rotate(90deg); color: var(--accent); }

.batch-detail-row > td {
  padding: 0 !important;
  background: var(--surface, #f9fafb);
  border-bottom: 2px solid var(--accent);
}

.batch-detail-panel {
  padding: 20px 24px 24px;
  border-top: 1px solid var(--border);
}

.batch-detail-badges {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.batch-detail-code {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: .02em;
  word-break: break-all;
  margin-bottom: 12px;
}

.batch-detail-fields { margin-bottom: 16px; }

.batch-detail-section {
  margin-bottom: 16px;
}

.batch-detail-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 20px 0;
}

/* Re-classify section */
.batch-reclass-toggle {
  gap: 6px;
  font-size: 13px;
}
.batch-reclass-toggle svg {
  width: 14px; height: 14px; flex-shrink: 0;
  stroke: currentColor; fill: none; stroke-width: 2;
}

.batch-reclass-form {
  margin-top: 14px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}

.batch-reclass-params {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 10px;
}

.batch-reclass-result-panel {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.batch-detail-section-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--text-muted);
  margin-bottom: 10px;
}

/* Re-classified row highlight */
.row-reclassified {
  box-shadow: inset 3px 0 0 var(--amber, #f59e0b);
}
.row-reclassified td:first-child { border-left: 3px solid var(--amber, #f59e0b); }

/* ── Rules page ──────────────────────────────────────────── */
.rules-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding-bottom: 48px;
}

.rules-loading, .rules-error {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}

.rules-spinner {
  width: 24px; height: 24px;
  stroke: var(--primary);
  stroke-width: 2;
  fill: none;
  stroke-linecap: round;
  animation: spin 1s linear infinite;
  display: inline-block;
  vertical-align: middle;
  margin-right: 8px;
}

.rules-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.rules-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
  flex-wrap: wrap;
}

.rules-card-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.rules-card-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.rules-card-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}

.rules-reset-btn {
  font-size: 12px;
  padding: 5px 10px;
}

.rules-save-btn {
  font-size: 12px;
  padding: 5px 10px;
}

.monaco-wrapper {
  width: 100%;
  height: 440px;
  border-top: 1px solid var(--border);
}

/* Markdown preview panel */
.rules-preview {
  padding: 20px 28px 28px;
  border-top: 1px solid var(--border);
  min-height: 440px;
  font-size: 13.5px;
  line-height: 1.7;
  color: var(--text);
}

.rules-preview h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  margin: 0 0 14px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.rules-preview h4 {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  margin: 20px 0 8px;
}

.rules-preview p { margin: 0 0 10px; }

.rules-preview ul, .rules-preview ol {
  margin: 0 0 10px;
  padding-left: 20px;
}

.rules-preview li { margin-bottom: 4px; }

.rules-preview code {
  font-family: 'IBM Plex Mono', 'Fira Mono', monospace;
  font-size: 12px;
  background: var(--surface-active);
  color: var(--primary);
  padding: 1px 5px;
  border-radius: 4px;
}

.rules-preview strong { font-weight: 600; }

.rules-preview blockquote {
  margin: 12px 0;
  padding: 10px 16px;
  border-left: 3px solid var(--amber, #f59e0b);
  background: color-mix(in srgb, var(--amber, #f59e0b) 8%, transparent);
  border-radius: 0 6px 6px 0;
  font-size: 13px;
  color: var(--text);
}

.rules-preview blockquote p { margin: 0; }

.rules-preview table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
  margin: 10px 0 16px;
}

.rules-preview th {
  text-align: left;
  padding: 6px 10px;
  background: var(--surface-active);
  font-weight: 600;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}

.rules-preview td {
  padding: 5px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.rules-preview tr:last-child td { border-bottom: none; }
.rules-preview tr:hover td { background: var(--surface-hover); }

.rules-preview hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 20px 0;
}

/* Preview button active state */
.rules-preview-btn.active {
  background: var(--surface-active);
  color: var(--primary);
}

/* Toast */
.rules-toast {
  position: fixed;
  bottom: 28px;
  right: 28px;
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .2s, transform .2s;
  pointer-events: none;
  z-index: 999;
}

.rules-toast-visible {
  opacity: 1;
  transform: translateY(0);
}

.rules-toast-success {
  background: var(--green, #22c55e);
  color: #fff;
}

.rules-toast-error {
  background: var(--red, #ef4444);
  color: #fff;
}

/* Confirmation dialog */
.rules-dialog-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: center;
}

.rules-dialog {
  background: var(--surface);
  border-radius: var(--radius-lg, 10px);
  padding: 28px 32px;
  max-width: 400px;
  width: 90%;
  box-shadow: var(--shadow-lg);
}

.rules-dialog-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  margin: 0 0 10px;
}

.rules-dialog-body {
  font-size: 13px;
  color: var(--text-muted);
  margin: 0 0 22px;
  line-height: 1.5;
}

.rules-dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.btn-danger {
  background: var(--red, #ef4444);
  color: #fff;
  border-color: transparent;
}
.btn-danger:hover { background: #dc2626; }

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 768px) {
  .sb2 { position: fixed; top: 0; left: var(--sb1-w); height: 100%; z-index: 25; }
  .sb2:not(.collapsed) { box-shadow: var(--shadow-lg); }
  .batch-reclass-params { grid-template-columns: 1fr; }
}
