@layer reset, base, layout, components, utils;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  * { margin: 0; }
  html { -webkit-text-size-adjust: 100%; }
  body { -webkit-font-smoothing: antialiased; }
  img, svg { display: block; max-width: 100%; }
  button, input, select, textarea { font: inherit; color: inherit; }
  h1,h2,h3 { font-weight: 600; }
  a { color: var(--accent); text-decoration: none; }
  a:hover { text-decoration: underline; }
}

@layer base {
  body {
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--ink);
    line-height: 1.6;
    font-size: 16px;
    transition: background var(--d-base) var(--ease-standard),
                color var(--d-base) var(--ease-standard);
  }
  :is(h1,h2,h3) { font-family: var(--font-display); letter-spacing: -0.02em; line-height: 1.1; }
  .mono, code, kbd { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
  .num { font-variant-numeric: tabular-nums; }
  ::selection { background: var(--accent-weak); }
  :focus-visible { outline: none; box-shadow: var(--ring); border-radius: var(--r-sm); }
}

@layer layout {
  .wrap { max-width: var(--w-page); margin-inline: auto; padding-inline: var(--gutter); }
  .section { padding-block: var(--s9); }
  @media (max-width: 760px){ .section { padding-block: var(--s8); } }
  .eyebrow {
    font-family: var(--font-mono); font-size: 12px; font-weight: 600;
    letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted);
    margin-bottom: var(--s3);
  }
  .lede { font-size: 1.125rem; color: var(--text-2); max-width: 46ch; }
}

@layer components {
  /* ---- top nav ---- */
  .nav {
    position: sticky; top: 0; z-index: 50;
    backdrop-filter: saturate(1.4) blur(8px);
    background: color-mix(in srgb, var(--bg) 82%, transparent);
    border-bottom: 1px solid var(--border);
  }
  .nav-in { display: flex; align-items: center; gap: var(--s4); height: 56px; }
  .brand { display: flex; align-items: center; gap: var(--s2); font-family: var(--font-display); font-weight: 600; letter-spacing: -0.02em; }
  .brand .crown { color: var(--accent); }
  .nav-spacer { flex: 1; }
  .nav a.navlink { color: var(--text-2); font-size: 14px; }
  .nav a.navlink:hover { color: var(--ink); text-decoration: none; }
  .nav-dd { position: relative; display: inline-flex; }
  .nav-dd-btn { background: none; border: none; cursor: pointer; font: inherit; font-size: 14px; color: var(--text-2); padding: 0; }
  .nav-dd-btn:hover { color: var(--ink); }
  .nav-dd-menu { position: absolute; right: 0; top: calc(100% + 10px); min-width: 230px; display: none; z-index: 60;
    background: var(--bg); border: 1px solid var(--border-strong); border-radius: var(--r-md);
    box-shadow: var(--shadow-pop); padding: 6px; }
  .nav-dd:hover .nav-dd-menu, .nav-dd:focus-within .nav-dd-menu, .nav-dd.open .nav-dd-menu { display: block; }
  .nav-dd-menu a { display: block; padding: 8px 10px; border-radius: var(--r-sm); font-size: 13px;
    color: var(--text-2); white-space: nowrap; }
  .nav-dd-menu a b { color: var(--ink); font-weight: 600; }
  .nav-dd-menu a:hover { background: var(--surface-2); text-decoration: none; }
  .icon-btn {
    display: inline-grid; place-items: center; width: 36px; height: 36px;
    border: 1px solid var(--border); border-radius: var(--r-md);
    background: var(--surface); cursor: pointer; color: var(--text-2);
  }
  .icon-btn:hover { color: var(--ink); border-color: var(--border-strong); }

  /* ---- hero ---- */
  .hero { padding-block: var(--s8) var(--s9); }
  .hero-grid {
    display: grid; grid-template-columns: minmax(0,44fr) minmax(0,56fr);
    gap: var(--s7); align-items: start;
  }
  @media (max-width: 960px){
    .hero-grid { grid-template-columns: 1fr; gap: var(--s6); }
    .hero-copy { order: 2; }
    .hero-demo { order: 1; }
  }
  h1.title { font-size: clamp(2.25rem, 5vw, 3.25rem); margin-bottom: var(--s4); }
  h1.title .accent { color: var(--accent); }
  .hero .lede { margin-bottom: var(--s5); }
  .install {
    display: flex; align-items: center; gap: var(--s3);
    background: var(--surface-2); border: 1px solid var(--border);
    border-radius: var(--r-md); padding: var(--s3) var(--s4);
    font-family: var(--font-mono); font-size: 14px; margin-bottom: var(--s5);
  }
  .install .dollar { color: var(--muted); }
  .install code { flex: 1; }
  .cta-row { display: flex; gap: var(--s3); flex-wrap: wrap; }

  /* ---- buttons ---- */
  .btn {
    display: inline-flex; align-items: center; gap: var(--s2);
    height: 40px; padding-inline: var(--s4); border-radius: var(--r-md);
    font-weight: 600; font-size: 14px; cursor: pointer; border: 1px solid transparent;
    transition: background var(--d-fast) var(--ease-standard),
                border-color var(--d-fast) var(--ease-standard),
                transform var(--d-fast) var(--ease-standard);
  }
  .btn:hover { text-decoration: none; }
  .btn:active { transform: translateY(1px); }
  .btn-primary { background: var(--accent); color: #fff; }
  .btn-primary:hover { background: var(--accent-hover); }
  .btn-secondary { background: var(--surface); border-color: var(--border); color: var(--ink); }
  .btn-secondary:hover { border-color: var(--border-strong); }
  .btn-ghost { background: transparent; color: var(--text-2); }
  .btn-ghost:hover { background: var(--surface); color: var(--ink); }
  .btn-sm { height: 32px; font-size: 13px; padding-inline: var(--s3); }

  /* ---- cards ---- */
  .card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--r-lg); padding: var(--s5);
  }
  .card-head { font-family: var(--font-mono); font-size: 12px; font-weight: 600;
    letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); margin-bottom: var(--s4); }

  /* ---- demo shell ---- */
  .demo { position: relative; }
  .demo-toolbar { display: flex; align-items: center; gap: var(--s2); margin-bottom: var(--s4); flex-wrap: wrap; }
  .demo-tag {
    font-family: var(--font-mono); font-size: 11px; font-weight: 500; letter-spacing: .02em;
    color: var(--text-2); background: var(--surface-2); border-radius: var(--r-pill); padding: 3px 10px;
  }
  .demo-tag-lang { color: var(--accent); background: var(--accent-weak); }
  .engine-chip {
    margin-left: var(--s2); font-family: var(--font-mono); font-size: 12px; color: var(--muted);
    display: inline-flex; align-items: center; gap: 6px; align-self: center;
  }
  .engine-chip .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
  .engine-chip.ready .dot { background: var(--robust-fill); }
  .engine-chip.busy  .dot { background: var(--warn-fill); animation: pulse 1s infinite; }
  @keyframes pulse { 50% { opacity: .35; } }

  /* segmented control (Python / R) */
  .seg { display: inline-flex; background: var(--surface-2); border-radius: var(--r-md); padding: 3px; gap: 2px; }
  .seg button {
    border: 0; background: transparent; cursor: pointer; border-radius: 7px;
    padding: 5px 12px; font-family: var(--font-mono); font-size: 13px; color: var(--text-2);
    position: relative;
  }
  .seg button[aria-selected="true"] { background: var(--bg); color: var(--ink); box-shadow: var(--shadow-sm); }

  /* ---- leaderboard bar chart ---- */
  .board { position: relative; display: flex; flex-direction: column; gap: var(--s2);
    margin-block: var(--s4); padding-left: 24px; }
  .bar-row {
    display: grid; grid-template-columns: 180px 1fr 64px; align-items: center; gap: var(--s3);
    position: relative;
  }
  .bar-name { font-size: 14px; font-weight: 500; white-space: nowrap; min-width: 0;
    display: flex; align-items: center; gap: 6px; }
  .bar-name .rank { font-family: var(--font-mono); font-size: 12px; color: var(--muted); width: 16px; flex: none; }
  .bar-name .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
  .bar-track { position: relative; height: 28px; background: var(--surface-2); border-radius: var(--r-sm); overflow: hidden; }
  .bar-fill { position: absolute; inset-block: 0; left: 0; border-radius: var(--r-sm);
    background: var(--series-other);
    transition: width var(--d-slow) var(--ease-emphatic), background var(--d-moderate) var(--ease-standard); }
  .bar-row[data-role="winner"] .bar-fill { background: var(--series-winner); }
  .bar-row[data-role="rival"] .bar-fill {
    background: var(--series-rival);
    background-image: repeating-linear-gradient(45deg, rgba(255,255,255,.22) 0 6px, transparent 6px 12px);
  }
  .bar-row[data-role="winner"] .bar-fill::after {
    /* redundant (non-color) winner marker: inset outline */
    content: ""; position: absolute; inset: 0; border: 2px solid color-mix(in srgb, var(--series-winner) 60%, #000 10%);
    border-radius: var(--r-sm);
  }
  .bar-val { font-family: var(--font-mono); font-size: 13px; text-align: right; color: var(--text-2); }
  .win-pill {
    position: absolute; right: 70px; top: 50%; transform: translateY(-50%);
    background: var(--accent-weak); color: var(--accent); font-family: var(--font-mono);
    font-size: 10px; font-weight: 700; letter-spacing: .06em; padding: 2px 8px; border-radius: var(--r-pill);
    pointer-events: none;
  }
  .crown-float {
    position: absolute; left: 0; top: 0; z-index: 5; font-size: 22px; line-height: 1; color: var(--accent);
    width: 24px; text-align: center; will-change: transform; pointer-events: none;
    filter: drop-shadow(0 1px 1px color-mix(in srgb, var(--accent) 35%, transparent));
  }

  /* ---- slider ---- */
  .slider-wrap { margin-top: var(--s4); }
  .slider-row { display: flex; align-items: center; gap: var(--s3); }
  .stepper {
    width: 36px; height: 36px; flex: none; border-radius: var(--r-md); border: 1px solid var(--border);
    background: var(--surface); cursor: pointer; font-size: 18px; color: var(--text-2);
  }
  .stepper:hover { border-color: var(--border-strong); color: var(--ink); }
  .slider-track-wrap { position: relative; flex: 1; }
  input[type=range].slider { width: 100%; accent-color: var(--accent); height: 28px; }
  .kstar-tick {
    position: absolute; top: -4px; bottom: 16px; width: 0; border-left: 2px dashed var(--fragile);
    pointer-events: none;
  }
  .kstar-tick span {
    position: absolute; top: -18px; left: 50%; transform: translateX(-50%);
    font-family: var(--font-mono); font-size: 10px; color: var(--fragile); white-space: nowrap;
  }
  .slider-labels { display: flex; justify-content: space-between; font-family: var(--font-mono);
    font-size: 11px; color: var(--muted); margin-top: 2px; }
  .slider-readout { font-family: var(--font-mono); font-size: 13px; color: var(--text-2); min-height: 20px; }
  .slider-foot { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); margin-top: var(--s2); }
  .deleted-chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: var(--s2); min-height: 24px; }
  .chip {
    font-family: var(--font-mono); font-size: 11px; background: var(--surface-2);
    border: 1px solid var(--border); color: var(--text-2); padding: 2px 8px; border-radius: var(--r-pill);
  }

  /* ---- verdict panel ---- */
  .verdict { margin-top: var(--s5); padding: var(--s4); border-radius: var(--r-md);
    border: 1px solid var(--border); background: var(--bg); }
  .verdict-head { display: flex; align-items: center; gap: var(--s3); }
  .verdict-badge {
    display: inline-flex; align-items: center; gap: 6px; font-family: var(--font-mono);
    font-size: 12px; font-weight: 700; letter-spacing: .04em; padding: 3px 10px; border-radius: var(--r-pill);
  }
  .verdict[data-class="fragile"] .verdict-badge { background: color-mix(in srgb,var(--fragile-fill) 22%,transparent); color: var(--fragile); }
  .verdict[data-class="borderline"] .verdict-badge { background: var(--accent-weak); color: var(--accent); }
  .verdict[data-class="robust"] .verdict-badge { background: color-mix(in srgb,var(--robust-fill) 20%,transparent); color: var(--robust); }
  .verdict-kstar { margin-left: auto; font-family: var(--font-mono); font-weight: 500;
    font-size: clamp(1.5rem, 3vw, 2rem); line-height: 1; color: var(--ink); }
  .verdict-kstar small { font-size: 11px; color: var(--muted); display: block; text-align: right; letter-spacing: .04em; }
  .verdict-text { margin-top: var(--s3); font-size: 15px; }
  .verdict-text b { font-weight: 600; }
  .verdict-sub { margin-top: var(--s2); font-size: 13px; color: var(--muted); }

  /* ---- fragility meter ---- */
  /* Read-only fragility gauge — deliberately NOT slider-shaped (a caret, not a thumb). */
  .meter { margin-top: var(--s5); cursor: default; }
  .meter-bar { position: relative; height: 6px; border-radius: var(--r-pill);
    background: linear-gradient(90deg, var(--fragile-fill), var(--warn-fill) 48%, var(--robust-fill)); }
  .meter-bar::before, .meter-bar::after { position: absolute; top: 11px;
    font-family: var(--font-mono); font-size: 10px; letter-spacing: .02em; color: var(--muted); }
  .meter-bar::before { content: "fragile"; left: 0; }
  .meter-bar::after  { content: "robust"; right: 0; }
  .meter-marker { position: absolute; top: -9px; left: 0; width: 0; height: 0;
    border-left: 5px solid transparent; border-right: 5px solid transparent; border-top: 7px solid var(--ink);
    transform: translateX(-50%); transition: left var(--d-slow) var(--ease-emphatic); }
  .meter-caption { font-family: var(--font-mono); font-size: 11px; color: var(--muted); margin-top: 28px; }

  /* ---- share row ---- */
  .share-row { display: flex; gap: var(--s2); margin-top: var(--s4); flex-wrap: wrap; }

  /* ---- trust strip ---- */
  .trust { border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); }
  .trust-in { display: flex; gap: var(--s5); flex-wrap: wrap; align-items: center; padding-block: var(--s4);
    font-family: var(--font-mono); font-size: 13px; color: var(--muted); }
  .trust-in .badge { display: inline-flex; align-items: center; gap: 6px; }
  .trust-in .sep { opacity: .4; }

  /* ---- example cards ---- */
  .ex-filter-row { display: flex; align-items: center; gap: var(--s3); margin-bottom: var(--s4); flex-wrap: wrap; }
  .ex-filter-hint { font-size: 13px; }
  .ex-card.hidden { display: none; }
  .examples { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: var(--s4); }
  @media (max-width: 760px){ .examples { grid-template-columns: 1fr; } }
  .ex-card { position: relative; text-align: left; cursor: pointer; background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--r-md); padding: var(--s4); transition: border-color var(--d-fast); }
  .ex-card:hover { border-color: var(--accent); }
  .ex-card:focus-visible { outline: none; border-color: var(--accent); box-shadow: var(--ring); }
  .ex-card[aria-pressed="true"] { border-color: var(--accent); box-shadow: var(--ring); }
  .ex-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s2); margin-bottom: 4px; }
  .ex-head-right { display: inline-flex; align-items: center; gap: 6px; flex: none; }
  .ex-tag { font-family: var(--font-mono); font-size: 10px; font-weight: 600; letter-spacing: .03em;
    text-transform: uppercase; color: var(--muted); background: var(--surface-2);
    border-radius: var(--r-pill); padding: 2px 8px; white-space: nowrap; }
  .ex-tag-real { color: var(--robust); background: color-mix(in srgb, var(--robust) 14%, var(--bg)); }
  .ex-card h3 { font-size: 15px; margin: 0; }
  /* (i) info button + hover/click popover on real-data cards */
  .ex-info { width: 18px; height: 18px; flex: none; border-radius: 50%; border: 1px solid var(--border-strong);
    background: var(--bg); color: var(--muted); font-family: var(--font-mono); font-size: 11px; font-style: italic;
    font-weight: 700; line-height: 1; cursor: help; padding: 0; }
  .ex-info:hover, .ex-info:focus-visible { color: var(--accent); border-color: var(--accent); outline: none; }
  .ex-pop { display: none; position: absolute; z-index: 30; top: 44px; right: var(--s3); left: var(--s3);
    background: var(--bg); border: 1px solid var(--border-strong); border-radius: var(--r-md);
    box-shadow: var(--shadow-pop); padding: var(--s3) var(--s4); font-size: 12px; line-height: 1.55;
    color: var(--text-2); font-weight: 400; cursor: auto; }
  .ex-pop.open { display: block; }
  .ex-pop-src { display: block; margin-top: var(--s2); color: var(--muted); font-size: 11px; }
  .source-note { margin-top: var(--s3); font-size: 12px; color: var(--muted); line-height: 1.5; }
  .source-note a { color: var(--accent); }

  /* "Watch it flip" guided-demo button (sits by the slider) */
  .try-flip { display: inline-flex; align-items: center; flex: none; white-space: nowrap;
    font-family: var(--font-mono); font-size: 12px; font-weight: 600; color: var(--accent);
    background: var(--accent-weak); border: 1px solid transparent; border-radius: var(--r-pill);
    padding: 5px 14px; cursor: pointer; transition: border-color var(--d-fast), background var(--d-fast); }
  .try-flip:hover { border-color: var(--accent); }
  .try-flip:disabled { opacity: .55; cursor: default; }

  /* Anchor offset for the sticky nav. These are .section blocks with ~96px top padding,
     so a negative margin scrolls past most of that padding and lands the heading just
     below the nav (instead of leaving a big empty gap). */
  #your-data, #rigor, #quickstart { scroll-margin-top: -24px; }
  .ex-card p { font-size: 13px; color: var(--muted); }
  .ex-card .mini-meter { height: 6px; border-radius: var(--r-pill); margin-top: var(--s3);
    background: linear-gradient(90deg, var(--fragile-fill), var(--warn-fill) 48%, var(--robust-fill)); position: relative; }
  .ex-card .mini-meter::after { content:""; position:absolute; top:-2px; width:10px; height:10px; border-radius:50%;
    background: var(--bg); border: 2px solid var(--ink); left: var(--pos,50%); transform: translateX(-50%); }

  /* ---- textarea / editor ---- */
  .csv-head { display: flex; align-items: center; gap: var(--s3); margin-bottom: var(--s2); }
  .csv-head .eyebrow { margin: 0; }
  .csv-head #csv-upload-btn { margin-left: auto; }
  textarea.csv { width: 100%; min-height: 150px; resize: vertical; background: var(--surface-2);
    color: var(--ink); border: 1px solid var(--border); border-radius: var(--r-md);
    padding: var(--s3); font-family: var(--font-mono); font-size: 13px; line-height: 1.5;
    transition: border-color var(--d-fast), box-shadow var(--d-fast); }
  textarea.csv.drag { border-color: var(--accent); border-style: dashed;
    box-shadow: 0 0 0 3px var(--accent-weak); }
  .err-card { border-left: 3px solid var(--danger); background: color-mix(in srgb,var(--danger) 8%,var(--bg));
    padding: var(--s3) var(--s4); border-radius: var(--r-sm); margin-top: var(--s3); font-size: 14px; }

  /* ---- expanders ---- */
  details.exp { border-top: 1px solid var(--border); }
  details.exp summary { cursor: pointer; list-style: none; display: flex; align-items: center; gap: var(--s2);
    padding-block: var(--s3); font-weight: 500; }
  details.exp summary::-webkit-details-marker { display: none; }
  details.exp summary .chev { transition: transform var(--d-fast); color: var(--muted); }
  details.exp[open] summary .chev { transform: rotate(90deg); }
  details.exp .exp-body { padding-bottom: var(--s4); max-width: var(--w-read); color: var(--text-2); }

  pre.report { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-md);
    padding: var(--s4); overflow: auto; font-family: var(--font-mono); font-size: 12.5px; line-height: 1.5;
    max-height: 420px; white-space: pre; }
}

@layer utils {
  .hidden { display: none !important; }
  .sr-only { position:absolute; width:1px; height:1px; padding:0; margin:-1px; overflow:hidden;
    clip:rect(0 0 0 0); white-space:nowrap; border:0; }
  .muted { color: var(--muted); }
  .center { text-align: center; }
}

/* ---- reduced motion ---- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important;
    transition-duration: .001ms !important; scroll-behavior: auto !important; }
}
