@layer reset, base, tokens, components, overrides;

/* ============ RESET ============ */
@layer reset {
  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  img, svg, video { max-width: 100%; display: block; }
  button, input, textarea, select { font: inherit; color: inherit; }
  input, textarea, select { font-size: 16px; }
}

/* ============ BASE ============ */
@layer base {
  html {
    font-optical-sizing: auto;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    font-feature-settings: "kern" 1, "liga" 1, "calt" 1;
    scroll-behavior: smooth;
  }
  body {
    background: var(--color-pit);
    color: var(--color-bone);
    font-family: var(--font-body);
    font-size: var(--text-base);
    font-weight: 400;
    line-height: 1.55;
    isolation: isolate;
  }
  h1, h2, h3 { text-wrap: balance; font-family: var(--font-display); }
  p, li { text-wrap: pretty; }
  ::selection { background: var(--color-flare); color: var(--color-pit); }
  :focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px var(--color-pit), 0 0 0 4px var(--color-flare);
    border-radius: var(--radius-xs);
  }
  .mono {
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    font-weight: 400;
    letter-spacing: 0.09em;
    line-height: 1.35;
  }
  .num { font-variant-numeric: tabular-nums; }
  .skip {
    position: absolute; left: 8px; top: -48px; z-index: 20;
    background: var(--color-flare); color: var(--color-pit);
    padding: 8px 16px; font-weight: 600; text-decoration: none;
    transition: top var(--dur-micro) var(--ease-mech);
  }
  .skip:focus-visible { top: 8px; }
  .visually-hidden {
    position: absolute; width: 1px; height: 1px;
    clip-path: inset(50%); overflow: hidden; white-space: nowrap;
  }
}

/* ============ COMPONENTS ============ */
@layer components {

  /* ---- top bar (task surface: conventional) ---- */
  .topbar {
    position: sticky; top: 0; z-index: 10;
    display: flex; align-items: center; gap: 24px;
    padding: 12px 24px;
    background: var(--color-pit);
    border-bottom: 1px solid var(--color-rule);
  }
  .wordmark {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: var(--text-base);
    letter-spacing: 0.02em;
    color: var(--color-bone);
    text-decoration: none;
  }
  .topbar-note { color: var(--color-ash); margin-inline: auto; }

  .btn {
    display: inline-flex; align-items: center; justify-content: center;
    min-height: 44px; padding: 10px 22px;
    font-weight: 600; font-size: var(--text-sm);
    text-decoration: none; cursor: pointer;
    border: 1px solid var(--color-rule);
    border-radius: var(--radius-xs);
    color: var(--color-bone);
    background: none;
    transition: background-color var(--dur-micro) var(--ease-snappy),
                color var(--dur-micro) var(--ease-snappy),
                border-color var(--dur-micro) var(--ease-snappy),
                transform var(--dur-micro) var(--ease-snappy);
  }
  .btn:active { transform: translate(1px, 2px); }
  .btn-flare {
    background: var(--color-flare);
    border-color: var(--color-flare);
    color: var(--color-pit);
  }
  .btn-flare:hover { background: var(--color-flare-deep); border-color: var(--color-flare-deep); }
  .btn-line:hover { border-color: var(--color-bone); background: var(--color-panel); }

  /* ---- shared section head (flush-left, never centered) ---- */
  .section-head {
    padding: 0 clamp(24px, 5vw, 80px);
    max-width: 900px;
  }
  .section-title {
    font-size: var(--text-xl);
    font-weight: 700;
    letter-spacing: -0.015em;
    line-height: 1.05;
  }
  .section-sub {
    margin-top: 14px;
    color: var(--color-ash);
    max-width: 52ch;
  }

  /* ---- hero: asymmetric 2:1, borders over shadows ---- */
  .hero {
    display: grid;
    grid-template-columns: 2fr 1fr;
    min-height: 88svh;
    border-bottom: 1px solid var(--color-rule);
  }
  .hero-lede {
    padding: clamp(48px, 9vh, 120px) clamp(24px, 5vw, 80px) clamp(48px, 8vh, 96px);
    display: flex; flex-direction: column; justify-content: center;
    border-right: 1px solid var(--color-rule);
  }
  .eyebrow { color: var(--color-ash); margin-bottom: 28px; }
  .hero-h1 {
    font-size: var(--text-hero);
    font-weight: 700;
    line-height: 0.97;
    letter-spacing: -0.025em;
    max-width: 12ch;
  }
  .hero-sub {
    margin-top: 30px;
    max-width: 46ch;
    font-size: var(--text-lg);
    line-height: 1.45;
    color: var(--color-ash);
  }
  .hero-cta { margin-top: 40px; display: flex; gap: 12px; flex-wrap: wrap; }

  .hero-rail { display: grid; grid-template-rows: repeat(4, 1fr); }
  .rail-cell {
    display: flex; flex-direction: column; justify-content: center; gap: 8px;
    padding: 24px clamp(20px, 2.5vw, 40px);
    border-bottom: 1px solid var(--color-rule);
  }
  .rail-cell:last-child { border-bottom: none; }
  .rail-label { color: var(--color-ash); }
  .rail-value {
    font-family: var(--font-display);
    font-weight: 300;
    font-size: var(--text-xl);
    line-height: 1;
    letter-spacing: -0.01em;
  }
  .rail-meta { color: var(--color-ash); }
  .live-dot {
    display: inline-block; width: 8px; height: 8px;
    border-radius: 999px; /* status-chip full-round exception */
    background: var(--color-flare);
    margin-right: 8px;
    animation: pulse 2.4s var(--ease-mech) infinite;
  }
  @keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

  /* ---- THE LADDER (signature) — left-flush type column ---- */
  .ladder-section { padding: 96px 0 64px; border-bottom: 1px solid var(--color-rule); }
  .ladder { list-style: none; margin-top: 48px; border-top: 1px solid var(--color-rule); }
  .rung {
    display: grid;
    grid-template-columns: minmax(56px, 8vw) 1fr auto;
    align-items: center;
    gap: clamp(12px, 3vw, 40px);
    padding: clamp(14px, 2.2vh, 26px) clamp(24px, 5vw, 80px);
    border-bottom: 1px solid var(--color-rule);
    transition: background-color var(--dur-state) var(--ease-mech);
  }
  .rung:hover { background: var(--color-panel); }
  .rung:hover .rung-name { transform: translateX(10px); }
  /* opacity state ladder (eightsleep-derived) — display names only, so
     small text keeps AA contrast; large-text floor is 3:1, lowest step stays above it */
  .rung .rung-name { opacity: 1; }
  .rung:nth-child(2) .rung-name { opacity: 0.8; }
  .rung:nth-child(3) .rung-name,
  .rung:nth-child(4) .rung-name { opacity: 0.58; }
  .rung:hover .rung-name { opacity: 1; }
  .rung-rank { color: var(--color-ash); }
  .rung:nth-child(1) .rung-rank { color: var(--color-flare); }
  .rung-name {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: var(--text-ladder);
    line-height: 0.95;
    letter-spacing: -0.02em;
    transition: transform var(--dur-state) var(--ease-snappy);
  }
  .rung-data { display: flex; flex-direction: column; gap: 6px; text-align: right; }
  .rung-streak { font-size: var(--text-sm); }
  .rung-streak b { font-weight: 600; }
  .rung-vitals { color: var(--color-ash); }
  .ladder-note { color: var(--color-ash); padding: 20px clamp(24px, 5vw, 80px) 0; }
  .rung.flipping { transition: transform var(--dur-reveal) var(--ease-mech); }

  /* ---- Gentleman's Rule trend — manual-figure chart ---- */
  .trend-section { padding: 88px 0; border-bottom: 1px solid var(--color-rule); }
  .trend-figure { padding: 48px clamp(24px, 5vw, 80px) 0; overflow-x: auto; }
  .trend-figure svg { width: 100%; min-width: 640px; height: auto; }
  .chart-grid line { stroke: var(--color-rule); stroke-width: 1; }
  .chart-baseline { stroke: var(--color-ash); stroke-width: 1; stroke-dasharray: 3 5; }
  .chart-label text, .chart-annot text {
    font-family: var(--font-mono);
    font-size: 13px;
    letter-spacing: 0.08em;
    fill: var(--color-ash);
  }
  .chart-line { fill: none; stroke-linejoin: round; stroke-linecap: round; }
  .chart-line-a { stroke: var(--color-flare); stroke-width: 3; }
  .chart-line-b { stroke: var(--color-bone); stroke-width: 2; }
  .chart-line-c { stroke: var(--color-ash); stroke-width: 2; }
  .chart-annot line { stroke: var(--color-ash); stroke-width: 1; }
  /* draw-on: data state change, mechanical */
  .chart-line { stroke-dasharray: 1; stroke-dashoffset: 1; }
  .drawn .chart-line { stroke-dashoffset: 0; transition: stroke-dashoffset 900ms var(--ease-mech); }
  .drawn .chart-line-b { transition-delay: 180ms; }
  .drawn .chart-line-c { transition-delay: 320ms; }
  .trend-figure figcaption { margin-top: 18px; color: var(--color-ash); }

  /* ---- Goals — bone block (alternation), bordered columns ---- */
  .goals-section {
    padding: 88px 0 96px;
    background: var(--color-bone);
    color: var(--color-pit);
  }
  .goals-section .section-sub { color: var(--color-rule); }
  .goals-grid {
    margin-top: 56px;
    display: grid;
    grid-template-columns: 1.25fr 1fr 0.85fr;
    border-block: 1px solid var(--color-rule);
  }
  .goal {
    padding: 40px clamp(20px, 3vw, 48px) 48px;
    border-right: 1px solid var(--color-rule);
    display: flex; flex-direction: column; gap: 16px;
  }
  .goal:last-child { border-right: none; }
  .goal-label { color: var(--color-rule); }
  .goal-value {
    font-family: var(--font-display);
    font-weight: 300;
    font-size: var(--text-stat);
    line-height: 0.9;
    letter-spacing: -0.02em;
  }
  .goal-unit {
    font-size: var(--text-lg);
    font-weight: 400;
    margin-left: 8px;
    color: var(--color-rule);
  }
  .goal-desc { max-width: 34ch; }
  .goal-meta { color: var(--color-rule); margin-top: auto; }
  .goal-meta a { color: var(--color-flare-deep); text-decoration: none; position: relative; }
  .goal-meta a::after {
    content: ""; position: absolute; left: 0; bottom: -3px;
    width: 100%; height: 2px; background: var(--color-flare-deep);
    transform: scaleX(0); transform-origin: left;
    transition: transform var(--dur-state) var(--ease-snappy);
  }
  .goal-meta a:hover::after { transform: scaleX(1); }

  /* ---- Photo wall — full-bleed horizontal strip ---- */
  .wall-section { padding: 88px 0 48px; border-bottom: 1px solid var(--color-rule); }
  .wall {
    margin-top: 48px;
    display: flex; gap: 16px;
    overflow-x: auto;
    padding: 4px clamp(24px, 5vw, 80px) 24px;
    scroll-snap-type: x proximity;
  }
  .frame {
    flex: 0 0 300px;
    min-height: 340px;
    scroll-snap-align: start;
    border: 1px solid var(--color-rule);
    border-radius: var(--radius-md);
    padding: 18px;
    display: flex; flex-direction: column; gap: 12px;
    background: var(--color-panel);
  }
  .frame:nth-child(odd) { min-height: 380px; }
  .frame-tag { color: var(--color-ash); }
  .frame-body {
    flex: 1;
    display: flex; align-items: end;
    color: var(--color-bone);
    background-size: cover; background-position: center;
    border-radius: var(--radius-xs);
  }
  .frame-open { border-style: dashed; background: none; }
  .frame-open .frame-body { color: var(--color-ash); }
  .frame-upload { cursor: pointer; }
  .frame-upload:hover { border-color: var(--color-flare); }
  .frame-upload:focus-within {
    box-shadow: 0 0 0 2px var(--color-pit), 0 0 0 4px var(--color-flare);
  }

  /* ---- Join — the one saturated flare moment ---- */
  .join-section { padding: 120px clamp(24px, 5vw, 80px) 140px; }
  .join-h2 {
    font-size: var(--text-stat);
    font-weight: 700;
    line-height: 0.95;
    letter-spacing: -0.025em;
    color: var(--color-flare);
    max-width: 10ch;
  }
  .join-sub { margin-top: 28px; max-width: 52ch; color: var(--color-ash); font-size: var(--text-lg); }
  .join-cta { margin-top: 44px; display: flex; gap: 12px; flex-wrap: wrap; }

  /* ---- footer with cropped ghost wordmark ---- */
  .footer {
    border-top: 1px solid var(--color-rule);
    padding: 32px clamp(24px, 5vw, 80px) 0;
    overflow: hidden;
  }
  .footer > .mono { color: var(--color-ash); }
  /* decorative cropped wordmark — CSS-generated so it's outside the a11y text tree */
  .footer-ghost::after {
    content: "TRENCHES";
    display: block;
    font-family: var(--font-display);
    font-weight: 700;
    font-size: clamp(6rem, 17vw, 16rem);
    line-height: 0.62;
    letter-spacing: 0.02em;
    color: var(--color-panel);
    margin-top: 28px;
    transform: translateY(8%);
    user-select: none;
  }

  /* ---- log dialog (conventional task surface) ---- */
  .log-dialog {
    margin: auto;
    width: min(480px, calc(100vw - 32px));
    background: var(--color-panel);
    color: var(--color-bone);
    border: 1px solid var(--color-rule);
    border-radius: var(--radius-md);
    padding: 0;
  }
  .log-dialog::backdrop { background: var(--color-shadow); }
  .log-dialog[open] { animation: dialog-in var(--dur-state) var(--ease-reveal); }
  @keyframes dialog-in {
    from { opacity: 0; transform: scale(0.96); }
    to { opacity: 1; transform: scale(1); }
  }
  .log-form { padding: 28px; display: flex; flex-direction: column; gap: 18px; }
  .log-title { font-size: var(--text-lg); font-weight: 700; }
  .log-hint { color: var(--color-ash); font-size: var(--text-sm); }
  .log-fieldset { border: 1px solid var(--color-rule); border-radius: var(--radius-xs); padding: 14px 16px; }
  .log-fieldset legend { color: var(--color-ash); padding: 0 6px; }
  .log-choice { display: flex; align-items: center; gap: 10px; padding: 6px 0; cursor: pointer; }
  .log-choice input { accent-color: var(--color-flare); width: 18px; height: 18px; }
  .log-note-label { color: var(--color-ash); }
  .log-note {
    background: var(--color-pit);
    border: 1px solid var(--color-rule);
    border-radius: var(--radius-xs);
    padding: 10px 12px;
    color: var(--color-bone);
    resize: vertical;
  }
  .log-note:focus-visible { box-shadow: 0 0 0 2px var(--color-panel), 0 0 0 4px var(--color-flare); }
  .log-actions { display: flex; justify-content: end; gap: 10px; }
  .log-confirm { color: var(--color-flare); }

  /* ---- entrance choreography: hard offset, snap (neo-brutalist) ---- */
  [data-enter] { opacity: 0; transform: translateY(14px); }
  .entered [data-enter] {
    opacity: 1; transform: none;
    transition: opacity var(--dur-reveal) var(--ease-reveal),
                transform var(--dur-reveal) var(--ease-reveal);
  }
  .entered [data-enter="2"] { transition-delay: 90ms; }
  .entered [data-enter="3"] { transition-delay: 170ms; }
  .entered [data-enter="4"] { transition-delay: 260ms; }
  .entered [data-enter="5"] { transition-delay: 330ms; }
  .entered [data-enter="6"] { transition-delay: 410ms; }

  /* ---- form/auth additions ---- */
  .log-pin { min-height: 44px; }
  .log-file { color: var(--color-ash); font-size: var(--text-sm); }
  .log-error { color: var(--color-flare); }
  .log-row { display: flex; gap: 12px; }
  .log-row > span { flex: 1; display: flex; flex-direction: column; gap: 6px; }
  .log-members { display: grid; grid-template-columns: 1fr 1fr; }
  .goal-add-btn {
    background: none; border: none; padding: 0; cursor: pointer;
    color: var(--color-flare-deep); letter-spacing: 0.09em;
    border-bottom: 2px solid var(--color-flare-deep);
  }
  button.frame { text-align: left; cursor: pointer; }
  .frame-photo { position: relative; overflow: hidden; }
  .frame-note {
    background: var(--color-pit); color: var(--color-bone);
    padding: 6px 10px; font-size: var(--text-sm);
    border-radius: var(--radius-xs);
    max-width: 100%;
  }
}

/* ============ OVERRIDES ============ */
@layer overrides {
  @media (max-width: 880px) {
    .hero { grid-template-columns: 1fr; min-height: 0; }
    .hero-lede { border-right: none; border-bottom: 1px solid var(--color-rule); }
    .hero-rail { grid-template-rows: none; grid-template-columns: 1fr 1fr; }
    .rail-cell { border-right: 1px solid var(--color-rule); }
    .rail-cell:nth-child(even) { border-right: none; }
    .topbar-note { display: none; }
    .goals-grid { grid-template-columns: 1fr; }
    .goal { border-right: none; border-bottom: 1px solid var(--color-rule); }
    .goal:last-child { border-bottom: none; }
    .rung { grid-template-columns: 40px 1fr; }
    .rung-data { grid-column: 2; text-align: left; }
  }
  @media (max-width: 520px) {
    .join-h2 { font-size: var(--text-hero); }
    .frame { flex-basis: 240px; min-height: 280px; }
  }
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
      animation-duration: 0.01ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: 0.01ms !important;
    }
    html { scroll-behavior: auto; }
    [data-enter] { opacity: 1; transform: none; }
    .chart-line { stroke-dasharray: none; stroke-dashoffset: 0; }
  }
}
