/* LeapX design tokens */
:root {
  /* Type */
  --font-sans: "Geist", ui-sans-serif, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, monospace;

  /* Neutrals — cool slightly-blue */
  --bg: oklch(0.985 0.003 250);
  --bg-elev: oklch(1 0 0);
  --bg-sunken: oklch(0.965 0.005 250);
  --line: oklch(0.91 0.006 250);
  --line-soft: oklch(0.945 0.005 250);
  --ink: oklch(0.18 0.01 250);
  --ink-2: oklch(0.36 0.012 250);
  --ink-3: oklch(0.54 0.012 250);
  --ink-4: oklch(0.7 0.012 250);

  /* Accent — refined amber/citrine */
  --accent: oklch(0.66 0.16 65);
  --accent-ink: oklch(0.28 0.08 65);
  --accent-soft: oklch(0.96 0.04 80);

  /* Semantic */
  --success: oklch(0.62 0.13 150);
  --success-soft: oklch(0.95 0.04 150);
  --danger: oklch(0.6 0.18 25);
  --danger-soft: oklch(0.96 0.04 25);
  --info: oklch(0.6 0.12 240);
  --info-soft: oklch(0.96 0.03 240);
  --warn: oklch(0.72 0.14 80);
  --warn-soft: oklch(0.97 0.04 80);

  /* Radii / shadow */
  --r-sm: 7px;
  --r-md: 12px;
  --r-lg: 16px;
  --r-xl: 22px;
  --shadow-sm: 0 1px 2px oklch(0.2 0.01 250 / 0.05);
  --shadow-md: 0 4px 14px oklch(0.2 0.01 250 / 0.06), 0 1px 2px oklch(0.2 0.01 250 / 0.04);

  /* Spacing scale — every UI metric should reference this so a single
     tweak propagates platform-wide. 4-based geometric scale. */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-7: 32px;
  --space-8: 40px;
  --space-9: 56px;

  /* Density (defaults — comfortable). Overridden by data-density="compact". */
  --row-h: 48px;
  --pad: var(--space-5);     /* 20px — card/page inner padding */
  --pad-sm: var(--space-3);  /* 12px — tight zones */
  --section-gap: var(--space-6);

  --max: 1440px;
}

[data-density="compact"] {
  --row-h: 36px;
  --pad: var(--space-3);
  --pad-sm: var(--space-2);
  --section-gap: var(--space-4);
}

[data-aesthetic="editorial"] {
  --pad: var(--space-7);     /* 32px — generous editorial whitespace */
  --row-h: 56px;
  --section-gap: var(--space-8);
}
[data-aesthetic="dense"] {
  --pad: var(--space-2);
  --row-h: 32px;
  --section-gap: var(--space-3);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "ss01", "cv11";
}
button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; padding: 0; }

/* App shell */
.app {
  display: grid;
  grid-template-columns: 264px 1fr;
  min-height: 100vh;
}
@media (max-width: 880px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}
.main {
  min-width: 0;
  background: var(--bg);
}

/* Generic */
.row { display: flex; align-items: center; }
.col { display: flex; flex-direction: column; }
.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }
.gap-5 { gap: var(--space-5); }
.gap-6 { gap: var(--space-6); }
.gap-8 { gap: var(--space-7); }
/* Section rhythm — apply to any direct vertical stack to get consistent gaps */
.stack > * + * { margin-top: var(--space-5); }
.stack-lg > * + * { margin-top: var(--space-7); }
.stack-sm > * + * { margin-top: var(--space-3); }
.grow { flex: 1; min-width: 0; }
.mono { font-family: var(--font-mono); font-feature-settings: "zero","ss01"; }
.muted { color: var(--ink-3); }
.dim { color: var(--ink-2); }
.tiny { font-size: 11px; letter-spacing: 0.04em; text-transform: uppercase; color: var(--ink-3); font-weight: 500; }
.h-rule { height: 1px; background: var(--line); }
.v-rule { width: 1px; background: var(--line); align-self: stretch; }

.card {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
}
.card-pad { padding: var(--pad); }
/* Default card padding when the card itself contains content directly */
.card.card-pad { padding: var(--pad); }
/* Card spacing between sibling cards inside a section */
.cards-grid { display: grid; gap: var(--space-4); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: var(--space-2);
  height: 36px; padding: 0 14px;
  border-radius: var(--r-sm);
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--ink);
  font-weight: 500;
  white-space: nowrap;
  transition: background 120ms, border-color 120ms, box-shadow 120ms;
}
.btn:hover { background: var(--bg-sunken); }
.btn-primary {
  background: var(--ink); color: var(--bg-elev); border-color: var(--ink);
}
.btn-primary:hover { background: oklch(0.28 0.012 250); }
.btn-accent {
  background: var(--accent); color: oklch(0.16 0.05 65); border-color: var(--accent);
}
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--bg-sunken); }
.btn-sm { height: 30px; padding: 0 10px; font-size: 12px; gap: var(--space-1); }

/* Badges */
.badge {
  display: inline-flex; align-items: center; gap: var(--space-1);
  height: 22px; padding: 0 8px;
  border-radius: 5px;
  font-size: 11px; font-weight: 500;
  background: var(--bg-sunken); color: var(--ink-2);
  border: 1px solid var(--line);
}
.badge.dot::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%;
  background: currentColor; display: inline-block;
}
.badge-success { background: var(--success-soft); color: var(--success); border-color: color-mix(in oklch, var(--success) 22%, transparent); }
.badge-danger { background: var(--danger-soft); color: var(--danger); border-color: color-mix(in oklch, var(--danger) 22%, transparent); }
.badge-info { background: var(--info-soft); color: var(--info); border-color: color-mix(in oklch, var(--info) 22%, transparent); }
.badge-warn { background: var(--warn-soft); color: oklch(0.45 0.12 80); border-color: color-mix(in oklch, var(--warn) 30%, transparent); }
.badge-accent { background: var(--accent-soft); color: var(--accent-ink); border-color: color-mix(in oklch, var(--accent) 30%, transparent); }
.badge-solid { background: var(--ink); color: var(--bg-elev); border-color: var(--ink); }

/* Inputs */
.input, .select, .textarea {
  height: 38px; padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  background: var(--bg-elev); color: var(--ink);
  outline: none;
  font-size: 13px;
  transition: border-color 120ms, box-shadow 120ms;
}
.input:focus, .select:focus, .textarea:focus {
  border-color: var(--ink-3);
  box-shadow: 0 0 0 3px oklch(0.85 0.012 250);
}
.textarea { height: auto; padding: var(--space-3); resize: vertical; line-height: 1.55; }
/* Field group — used by inline forms in platform admin */
.field-label { font-size: 11px; color: var(--ink-3); margin-bottom: var(--space-1); display: block; letter-spacing: 0.02em; }
.field { display: flex; flex-direction: column; gap: var(--space-1); }

/* Tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 14px 18px; }
th {
  font-size: 11px; font-weight: 500;
  color: var(--ink-3); text-transform: uppercase; letter-spacing: 0.04em;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  padding-top: 12px; padding-bottom: 12px;
}
tbody tr { border-bottom: 1px solid var(--line-soft); }
tbody tr:last-child { border-bottom: 0; }
tbody tr:hover { background: var(--bg-sunken); }
tbody td { vertical-align: middle; }

/* Topbar */
.topbar {
  height: 64px; padding: 0 var(--space-7);
  border-bottom: 1px solid var(--line);
  background: var(--bg-elev);
  display: flex; align-items: center; gap: var(--space-4);
  position: sticky; top: 0; z-index: 20;
}
.crumbs { color: var(--ink-3); font-size: 13px; display: flex; align-items: center; gap: var(--space-2); }
.crumbs b { color: var(--ink); font-weight: 500; }

/* Sidebar */
.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.brand {
  height: 64px; padding: 0 var(--space-5);
  display: flex; align-items: center; gap: var(--space-3);
  border-bottom: 1px solid var(--line);
}
.brand-name { font-weight: 600; letter-spacing: -0.01em; font-size: 15px; }
.brand-tag { font-size: 10px; color: var(--ink-3); margin-left: auto; font-family: var(--font-mono); }
.nav { flex: 1; padding: var(--space-4) var(--space-3); display: flex; flex-direction: column; gap: var(--space-1); overflow-y: auto; }
.nav-section {
  font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--ink-4); padding: var(--space-4) var(--space-3) var(--space-2);
  font-weight: 500;
}
.nav-section:first-child { padding-top: var(--space-1); }
.nav-item {
  display: flex; align-items: center; gap: var(--space-3);
  padding: 9px 12px;
  border-radius: var(--r-sm);
  color: var(--ink-2);
  cursor: pointer;
  font-weight: 450;
  font-size: 13px;
}
.nav-item:hover { background: var(--bg-sunken); color: var(--ink); }
.nav-item.active { background: var(--bg-sunken); color: var(--ink); font-weight: 500; }
.nav-item .count { margin-left: auto; font-size: 11px; color: var(--ink-4); font-family: var(--font-mono); }
.nav-item svg { color: var(--ink-3); flex-shrink: 0; }
.nav-item.active svg { color: var(--accent); }

/* Persona switcher */
.persona-card {
  margin: var(--space-3); padding: var(--space-4);
  border: 1px solid var(--line); border-radius: var(--r-md);
  background: var(--bg-sunken);
  display: flex; flex-direction: column; gap: var(--space-3);
}

/* Page */
.page { padding: var(--space-7) var(--space-8); max-width: var(--max); margin-inline: auto; }
.page-h { display: flex; align-items: end; gap: var(--space-4); margin-bottom: var(--space-2); flex-wrap: wrap; }
.page-h h1 { font-size: 24px; font-weight: 600; letter-spacing: -0.02em; margin: 0; line-height: 1.25; }
.page-sub { color: var(--ink-3); margin-bottom: var(--space-7); font-size: 13px; line-height: 1.55; }
/* Used by platform admin screens (no .page wrapper) */
.content { padding: var(--space-7) var(--space-8); max-width: var(--max); margin-inline: auto; }

/* Opt-in vertical rhythm — apply .stack on a section to get section-gap
   between its direct children. Default .page / .content don't auto-add
   that gap because individual components already manage their own
   margins inline; doubling them up causes oversized spacing. */

/* KPI tile */
.kpi {
  padding: var(--space-5) var(--space-5);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--bg-elev);
  display: flex; flex-direction: column; gap: var(--space-2);
}
.kpi .lbl { font-size: 11px; color: var(--ink-3); letter-spacing: 0.04em; text-transform: uppercase; font-weight: 500; }
.kpi .val { font-size: 28px; font-weight: 600; letter-spacing: -0.02em; font-family: var(--font-sans); font-feature-settings: "tnum"; line-height: 1.2; }
.kpi .delta { font-size: 12px; color: var(--success); display: flex; align-items: center; gap: var(--space-1); }
.kpi .delta.down { color: var(--danger); }

/* Bars */
.bar {
  height: 6px; background: var(--bg-sunken); border-radius: 999px; overflow: hidden;
}
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 999px; }

/* Avatar */
.av {
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--bg-sunken); border: 1px solid var(--line);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 600; color: var(--ink-2);
  flex-shrink: 0;
}
.av-stack { display: inline-flex; }
.av-stack .av { margin-left: -6px; }
.av-stack .av:first-child { margin-left: 0; }

/* Gate pipeline */
.pipeline { display: flex; align-items: stretch; gap: 0; }
.gate {
  flex: 1; min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  position: relative;
}
.gate:first-child { border-top-left-radius: var(--r-md); border-bottom-left-radius: var(--r-md); }
.gate:last-child { border-top-right-radius: var(--r-md); border-bottom-right-radius: var(--r-md); }
.gate + .gate { border-left: 0; }
.gate.passed { background: oklch(0.96 0.025 150); border-color: color-mix(in oklch, var(--success) 25%, var(--line)); }
.gate.current { background: var(--accent-soft); border-color: color-mix(in oklch, var(--accent) 35%, var(--line)); }
.gate.failed { background: var(--danger-soft); }
.gate .g-name { font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--ink-2); }
.gate .g-status { font-size: 12px; color: var(--ink-3); margin-top: 2px; }

/* Misc */
.kbd {
  font-family: var(--font-mono);
  font-size: 11px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
  background: var(--bg-elev);
  color: var(--ink-3);
}
a { color: var(--accent-ink); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Hash chain visual */
.chain-link {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 8px;
  border: 1px dashed color-mix(in oklch, var(--success) 35%, var(--line));
  background: color-mix(in oklch, var(--success-soft) 60%, var(--bg-elev));
  color: var(--success);
  border-radius: 999px;
  font-family: var(--font-mono);
  font-size: 11px;
}

/* Stripe placeholder */
.stripe {
  background: repeating-linear-gradient(
    -45deg,
    var(--bg-sunken) 0 8px,
    var(--bg-elev) 8px 16px
  );
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  display: flex; align-items: center; justify-content: center;
  color: var(--ink-3);
  font-family: var(--font-mono);
  font-size: 11px;
}

/* Pairwise */
.pw-card {
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  padding: 18px;
  background: var(--bg-elev);
  cursor: pointer;
  transition: transform 120ms, border-color 120ms, box-shadow 120ms;
}
.pw-card:hover { border-color: var(--ink-3); transform: translateY(-2px); box-shadow: var(--shadow-md); }
.pw-card.picked { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* Score bar */
.score-row { display: grid; grid-template-columns: 1fr 80px 100px; gap: 12px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line-soft); }
.score-row:last-child { border-bottom: 0; }

/* Audit */
.audit-row {
  display: grid;
  grid-template-columns: 130px 110px 1fr 200px 28px;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--line-soft);
  align-items: center;
  font-size: 13px;
}
.audit-row:hover { background: var(--bg-sunken); }

/* Mobile baseline (kept; the responsive block below tightens further) */
@media (max-width: 880px) {
  .page, .content { padding: var(--space-4); }
  .topbar { padding: 0 var(--space-4); height: 56px; }
  .brand { height: 56px; padding: 0 var(--space-4); }
  .page-h h1 { font-size: 20px; }
  th, td { padding: 10px 12px; }
}

/* ===========================================================================
   RTL support — applied when document.documentElement.dir == "rtl".
   Uses [dir="rtl"] selectors so we can keep all existing LTR rules intact
   and just flip what's strictly directional (sidebar position, paddings,
   text alignment, drawer slide direction).
   =========================================================================== */
[dir="rtl"] body { font-family: "Cairo", "Geist", ui-sans-serif, sans-serif; }
[dir="rtl"] .app { direction: rtl; }
[dir="rtl"] .audit-row { grid-template-columns: 130px 110px 1fr 200px 28px; }
[dir="rtl"] .score-row { grid-template-columns: 1fr 80px 100px; }
[dir="rtl"] .badge.dot { padding-inline-start: 8px; padding-inline-end: 8px; }
[dir="rtl"] .mono { direction: ltr; unicode-bidi: embed; display: inline-block; }
[dir="rtl"] table.table th,
[dir="rtl"] table.table td { text-align: right; }
[dir="rtl"] .crumbs { direction: rtl; }
[dir="rtl"] .nav-item .count { margin-inline-start: auto; }

/* ===========================================================================
   Responsive — drawer sidebar on mobile, fluid tables, modal width.
   Breakpoints: 640px (small phone), 880px (tablet/phone landscape).
   =========================================================================== */

/* Hamburger button — only shown on small screens via .mobile-only */
.hamburger {
  display: none;
  width: 36px; height: 36px;
  align-items: center; justify-content: center;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  color: var(--ink-2);
}
.hamburger:active { transform: scale(0.96); }

/* Override the existing 880px rule that just hid the sidebar — replace with
   a slide-over drawer. The sidebar becomes position:fixed off-screen by
   default, and slides in when .sidebar-open is toggled on the .app root. */
@media (max-width: 880px) {
  .app { grid-template-columns: 1fr; }

  .sidebar {
    display: flex !important;
    position: fixed;
    top: 0; bottom: 0;
    inset-inline-start: 0;
    width: 88vw;
    max-width: 320px;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 220ms ease;
    box-shadow: var(--shadow-md);
  }
  [dir="rtl"] .sidebar { transform: translateX(100%); }

  .app.sidebar-open .sidebar { transform: translateX(0); }
  .app.sidebar-open::before {
    content: "";
    position: fixed; inset: 0;
    background: oklch(0.2 0.01 250 / 0.45);
    z-index: 49;
  }

  .hamburger { display: inline-flex; }

  /* Fluid topbar — let crumbs wrap and trim margins */
  .topbar { padding: 0 var(--space-4); gap: var(--space-2); flex-wrap: wrap; }
  .crumbs { font-size: 13px; }

  /* Cards/tables breathe a bit less on phone */
  .page, .content { padding: var(--space-4) var(--space-4); }
  .card { border-radius: var(--r-md); }
}

/* Table horizontal scroll wrapper — every <table class="table"> in a small
   viewport should be horizontally scrollable so columns don't break. */
.table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

@media (max-width: 880px) {
  table.table { min-width: 560px; }
  table.table th, table.table td { padding: var(--space-3) var(--space-3); font-size: 13px; }

  /* Audit grid → stacked rows on phone-size */
  .audit-row {
    grid-template-columns: 1fr 1fr;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-4);
  }

  /* Score rows */
  .score-row { grid-template-columns: 1fr 60px 80px; }

  /* Buttons get bigger touch targets */
  .btn { min-height: 38px; }
  .btn-sm { min-height: 34px; }
}

@media (max-width: 640px) {
  /* Truly phone-sized */
  .page, .content { padding: var(--space-3); }
  .crumbs { font-size: 12px; }
  .crumbs b { font-size: 13px; }

  /* Login card full-width */
  .login-card { width: 100% !important; max-width: 100% !important; padding: 20px !important; }

  /* Quick-pick grid: one column on tiny screens */
  .login-quick-grid { grid-template-columns: 1fr !important; }

  /* Modal: full-width sheet */
  .modal { width: 100% !important; max-width: 100% !important; border-radius: 0 !important; }

  /* Hide tweaks panel — it's a dev affordance */
  .tweaks-panel { display: none !important; }
}

/* Focus rings — accessible without being too loud */
.btn:focus-visible,
.nav-item:focus-visible,
.input:focus-visible,
.select:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Skip-link for keyboard users */
.skip-link {
  position: absolute; left: -9999px; top: 8px;
  background: var(--ink); color: white;
  padding: 8px 14px; border-radius: var(--r-sm);
  z-index: 100;
}
.skip-link:focus { left: 8px; }

/* Locale switcher — compact, fits sidebar persona card */
.locale-switch {
  display: inline-flex;
  gap: 2px;
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 2px;
}
.locale-switch button {
  height: 22px; padding: 0 10px;
  font-size: 11px; font-weight: 500;
  color: var(--ink-3);
  border-radius: 4px;
}
.locale-switch button.active {
  background: var(--ink);
  color: var(--bg-elev);
}

/* Impersonation banner on mobile */
@media (max-width: 640px) {
  .imp-banner { flex-direction: column; align-items: flex-start; gap: 6px; padding: 8px 12px; }
}

/* ===========================================================================
   Spacing polish — extra section breathing room inside cards/tables.
   Applied last so it wins over earlier rules.
   =========================================================================== */

/* Page header rhythm — give title block real room above the content */
.page-h + * { margin-top: 0; }
.page-sub + * { margin-top: var(--space-2); }

/* Table-wrap inherits card border-radius so tables-inside-cards look clean */
.card.table-wrap { overflow: hidden; }
.card.table-wrap > table { border-radius: inherit; }

/* Modal */
.modal {
  padding: var(--space-7);
  border-radius: var(--r-lg);
}
.modal-h { margin-bottom: var(--space-5); }
.modal-actions { margin-top: var(--space-6); display: flex; gap: var(--space-2); justify-content: flex-end; }

/* Grid utility for any 2-3 column section */
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-4); }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--space-4); }
@media (max-width: 880px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; gap: var(--space-3); }
}

/* Sidebar feels more relaxed */
.sidebar { gap: 0; }
.sidebar .brand { letter-spacing: 0.01em; }

/* Page-h on mobile gets less margin so it doesn't dominate the viewport */
@media (max-width: 880px) {
  .page-h { margin-bottom: var(--space-1); }
  .page-sub { margin-bottom: var(--space-4); }
  .modal { padding: var(--space-5); }
}

/* Locale switcher buttons — bump touch target */
.locale-switch button { height: 26px; padding: 0 12px; }

/* When a card holds an inline form with .row gap-2 inside, ensure the
   row visually fits — flex-wrap was already set elsewhere; this keeps
   sub-elements aligned baseline-end. */
.card .row > .field { min-width: 0; }

/* Registration card — keep generous on desktop, tighten on mobile */
@media (max-width: 640px) {
  .register-card { padding: 24px !important; border-radius: 14px !important; }
}
