/* ============================================================
   final.css — final polish layer. Loaded LAST.
   ============================================================ */


/* ---- design_tokens ---- */
/* design_tokens */
:root {
  /* 1. Color Palette */
  --color-canvas: #FAF9F6;
  --color-ink: #0B0D10;
  --color-gray-100: #F1EFE9; /* bg-sunken */
  --color-gray-200: #E5E2D9; /* rule */
  --color-gray-300: #6B7280; /* fg-tertiary */
  --color-gray-400: #3B3F47; /* fg-secondary */
  --color-accent-cobalt: #0055B8;
  --color-accent-cyan: #0E6AF0;
  --color-semantic-ok: #0F8A3F;
  --color-semantic-warning: #B45309;
  --color-semantic-error: #B3261E;
  --color-code-string: #8B6A00;
  --color-code-keyword: #AA1D87;
  --color-code-comment: #5F6670;
  --color-wash-cyan: rgba(14, 106, 240, 0.08);
  --color-wash-success: rgba(15, 138, 63, 0.1);

  /* 2. Typography */
  --font-family-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --font-family-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --font-family-body: var(--font-family-sans);

  --font-size-1: 12px; /* --fs-xs */
  --font-size-2: 14px; /* --fs-sm */
  --font-size-3: 16px; /* --fs-base */
  --font-size-4: 20px; /* --fs-lg */
  --font-size-5: 24px; /* --fs-xl */
  --font-size-6: 32px; /* --fs-2xl */
  --font-size-7: 40px; /* --fs-3xl */

  --font-weight-regular: 400;
  --font-weight-medium: 600;
  --font-weight-bold: 800;

  --line-height-tight: 1.12;
  --line-height-normal: 1.6;
  --line-height-loose: 1.7;

  --letter-spacing-tight: -0.02em;
  --letter-spacing-normal: 0em;

  /* 3. Spacing (4pt baseline) */
  --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: 48px;
  --space-10: 64px;
  --space-11: 80px;
  --space-12: 96px;

  /* 4. Radius */
  --radius-1: 3px;
  --radius-2: 6px;
  --radius-3: 10px;
  --radius-4: 14px;

  /* 5. Shadows */
  --shadow-1: 0 1px 0 rgba(11, 13, 16, 0.04);
  --shadow-2: 0 1px 2px rgba(11, 13, 16, 0.06), 0 2px 8px rgba(11, 13, 16, 0.04);
  --shadow-3: 0 4px 16px rgba(11, 13, 16, 0.08), 0 16px 48px rgba(11, 13, 16, 0.06);
  --shadow-4: 0 10px 32px rgba(11,13,16,0.28);
  --shadow-5: 0 24px 48px rgba(18,24,40,.12);

  /* 6. Z-Index */
  --z-index-1-base: 1;
  --z-index-2-sticky: 10;
  --z-index-3-nav: 50;
  --z-index-4-drawer: 200;
  --z-index-5-toast: 999;
  --z-index-6-modal: 10000;

  /* 7. Motion */
  --motion-duration-fast: 0.15s;
  --motion-duration-normal: 0.3s;
  --motion-duration-slow: 0.7s;

  --motion-ease-in-out: ease-in-out;
  --motion-ease-out-quad: cubic-bezier(0.25, 0.46, 0.45, 0.94);
  --motion-ease-out-back: cubic-bezier(0.2, 0.7, 0.2, 1);
}


/* ---- a11y_audit ---- */
/* a11y_audit */

.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: #0055B8;
    color: white;
    padding: 8px;
    z-index: 100;
}

.skip-link:focus {
    top: 0;
}

:focus {
    outline: 2px solid #0055B8;
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}


/* ---- performance ---- */
/* performance */
.lesson-content > section:not(:first-of-type) {
  content-visibility: auto;
  contain: layout paint;
}


/* ---- l01_polish ---- */
/* l01_polish */
body.lesson-01 .grid-3 {
    grid-auto-rows: 1fr;
}

body.lesson-01 .pull-quote {
    display: flex;
    align-items: center;
    gap: 1em;
}

body.lesson-01 .prose ol {
    padding-left: 20px;
}

body.lesson-01 .cx-L01-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

body.lesson-01 .cx-L01-button {
    width: 100%;
}

body.lesson-01 .cx-L01-meter-label {
    transform: translateX(-50%) rotate(0deg);
    white-space: normal;
    text-align: center;
    bottom: -40px;
}


/* ---- l02_polish ---- */
/* l02_polish */
.tdemo-ctrl {
    align-items: center;
}


/* ---- l03_polish ---- */
/* l03_polish */
.diff .w-file {
  background: var(--paper-2);
  color: var(--ink-2);
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
}

.lesson-content ol {
  list-style: decimal;
  padding-left: 20px;
}

.diff-line .ln {
  padding-top: 2px;
}


/* ---- l04_polish ---- */
/* l04_polish */

.lesson-04 .grid-2 {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    margin-top: 20px !important;
}

.cx-L04-score-bar {
    border-radius: 4px;
    overflow: hidden;
}

.cx-L04-score-meter {
    border-radius: 4px 0 0 4px;
}

.cx-L04-score-text {
    font-weight: 500;
}

.cx-L04-spec-toggle {
    border-radius: 4px;
}


/* ---- l05_polish ---- */
/* l05_polish */

.cx-L05-handle::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 12px;
  height: 12px;
  background-image: radial-gradient(circle, rgba(255,255,255,0.5) 2px, transparent 2px), radial-gradient(circle, rgba(255,255,255,0.5) 2px, transparent 2px);
  background-size: 6px 6px;
  background-position: 0 0, 3px 3px;
}

.cx-L05-toast {
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  border-radius: 6px !important;
  font-weight: 500;
}

.cx-L05-readout-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}


/* ---- l06_polish ---- */
/* l06_polish */
.lesson-06 .spec-toggle {
    align-items: center !important;
}
.lesson-06 .quiz-opt {
    align-items: center !important;
}
.lesson-06 .cx-L06-pr-card {
    align-self: flex-start !important;
}


/* ---- l07_polish ---- */
/* l07_polish */

.lesson-07 .meta {
  background: var(--paper-2);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 10px 16px;
}

.lesson-07 .w-diff-line.add {
  background-color: color-mix(in oklch, var(--ok) 10%, transparent);
}

.lesson-07 .w-diff-line.del {
  background-color: color-mix(in oklch, var(--bad) 10%, transparent);
}

.lesson-07 .w-diff-line .t {
  font-family: var(--mono);
  color: var(--ink-3);
  padding-right: 12px;
  user-select: none;
}

.lesson-07 pre.code {
  background-color: var(--paper-2);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 16px;
  font-size: 0.85rem;
  line-height: 1.6;
}

.lesson-07 .tok-c { color: oklch(0.6 0.04 250); font-style: italic; }
.lesson-07 .tok-k { color: oklch(0.6 0.15 280); font-weight: 600; }
.lesson-07 .tok-n { color: var(--ink); }
.lesson-07 .tok-s { color: oklch(0.65 0.18 140); }


/* ---- l08_polish ---- */
/* l08_polish */
.lesson-08 .grid-2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
}

.lesson-08 .w .compare .cb {
  font-family: var(--font-mono);
  white-space: pre-wrap;
  font-size: 0.85rem;
}


/* ---- l09_polish ---- */
/* l09_polish */

.lesson-09 .grid-3 .card {
    border: 1px solid var(--ink-l4);
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    transition: all 0.2s ease-in-out;
}

.lesson-09 .grid-3 .card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.08);
}

.cx-L09-tool-card {
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
    transition: all 0.2s cubic-bezier(.25,.8,.25,1);
}

.cx-L09-tool-card:hover {
    transform: scale(1.03);
    box-shadow: 0 5px 15px rgba(0,0,0,0.08);
    z-index: 10;
}

.cx-L09-slot {
    background: #f9f9f9;
    border: 1px solid #e8e8e8;
    border-radius: 8px;
    box-shadow: inset 0 1px 3px rgba(0,0,0,0.06);
}

.cx-L09-badge-ok, .cx-L09-badge-warn {
    display: inline-block;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-left: 8px;
}

.cx-L09-badge-ok {
    background-color: #d4edda;
    color: #155724;
}

.cx-L09-badge-warn {
    background-color: #fff3cd;
    color: #856404;
}


/* ---- l10_polish ---- */
/* l10_polish */

/* Team-flow bullets */
.lesson-10 #s4 ul {
    list-style: none;
    padding-left: 1.25rem;
}

.lesson-10 #s4 ul li {
    position: relative;
    padding-left: 1.75rem;
    margin-bottom: 1rem;
}

.lesson-10 #s4 ul li::before {
    content: '▸';
    position: absolute;
    left: 0;
    color: var(--accent);
    font-size: 1.2em;
    top: -0.1em;
}

/* Task-board cards */
.cx-L10-task-card {
    background-color: var(--paper-1);
    border: 1px solid var(--rule-2) !important;
    border-radius: 8px;
    padding: 16px !important;
    box-shadow: 0 4px 8px -2px rgba(0,0,0,0.07);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    transition: all 0.2s ease-out;
}

.cx-L10-task-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 12px -3px rgba(0,0,0,0.1);
}

.cx-L10-task-card h3 {
    font-size: 1.1rem !important;
    margin-bottom: 0.5rem;
}

.cx-L10-task-card p {
    font-size: 0.9rem;
    color: var(--ink-2);
    margin-bottom: 1rem;
}

/* Launch button */
.cx-L10-task-card button {
    background-color: var(--accent) !important;
    color: var(--paper-1) !important;
    border: none !important;
    border-radius: 6px;
    padding: 10px 16px !important;
    font-weight: 600;
    cursor: pointer !important;
    transition: background-color 0.2s ease;
    width: 100%;
}

.cx-L10-task-card button:hover {
    background-color: #004494 !important; /* Darker accent */
}

.cx-L10-task-card button:disabled {
    background-color: var(--ink-4) !important;
    cursor: not-allowed !important;
}


/* ---- l11_polish ---- */
/* l11_polish */
.lesson-sidebar .toc-item .toc-num {
  font-family: var(--mono);
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--ink-3);
  background-color: var(--paper-2);
  border-radius: 4px;
  padding: 2px 6px;
  margin-right: 8px;
  border: 1px solid var(--rule);
}

.lesson-sidebar .toc-item.active .toc-num {
  background-color: var(--cyan);
  color: white;
  border-color: var(--cyan-dark);
}

.L11-drop-zone {
  background-color: var(--paper-1);
  border-style: solid !important;
  box-shadow: var(--shadow-sm);
}

.L11-drop-zone.drag-over {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.L11-drop-zone.proven {
  border-color: var(--ok-light) !important;
}

.L11-drop-zone.avoid {
  border-color: var(--bad-light) !important;
}

.L11-drop-zone h3 {
  font-weight: 700 !important;
}

.L11-drop-zone.proven h3 { color: var(--ok-dark) !important; }
.L11-drop-zone.avoid h3 { color: var(--bad-dark) !important; }

.L11-card {
  perspective: 800px;
}

.L11-card-inner {
  transition: transform 0.6s cubic-bezier(0.3, 1.2, 0.7, 1);
  transform-style: preserve-3d;
}

.L11-card.flipped .L11-card-inner {
  transform: rotateY(180deg);
}

.L11-card-front,
.L11-card-back {
  backface-visibility: hidden;
}

.L11-card-back {
  transform: rotateY(180deg);
}


/* ---- l12_polish ---- */
/* l12_polish */
.lesson-12 .lesson-hero .meta .icon { vertical-align: -2px; margin-right: 2px; }
.lesson-12 .lesson-body { grid-template-columns: 240px 1fr; }
.lesson-12 .compare { gap: 18px; }


/* ---- responsive: topbar fits narrow viewports (no horizontal overflow) ---- */
/* Safety net: never let the document scroll sideways. The real cause is fixed
   below by collapsing the topbar nav cluster on small screens. */
html, body { overflow-x: clip; }

@media (max-width: 640px) {
  /* Keep the topbar on one tidy line and let the nav cluster shrink. */
  .topbar-inner {
    gap: 8px !important;
    padding: 10px 14px !important;
  }
  /* The brand tagline + the lesson crumb are nice-to-have, not load-bearing.
     Drop them so the brand + nav fit a 390px viewport. */
  .topbar .brand-tagline,
  .topbar .tb-crumb,
  .topbar .topbar-crumb,
  .topbar .crumb { display: none !important; }

  .topbar nav,
  .topbar-nav {
    gap: 8px !important;
    flex-wrap: nowrap;
    min-width: 0;
  }
  /* Compact the controls so Outline + Home + badge all fit. */
  .topbar .btn-outline-trigger,
  .topbar .outline-btn {
    padding: 6px 9px !important;
    font-size: 12px !important;
  }
  .topbar nav a,
  .topbar .tb-home {
    padding: 6px 6px !important;
    font-size: 12px !important;
  }
  /* Reduce the progress badge to its count only (hide the "completed" word). */
  .topbar .progress-badge,
  .topbar .progress-pill,
  .topbar .completed-badge {
    padding: 5px 8px !important;
    font-size: 11px !important;
    white-space: nowrap;
    overflow: hidden;
    max-width: 84px;
    text-overflow: clip;
  }
}

/* Even tighter: on very narrow screens, drop the count badge entirely —
   progress is always one tap away in the Outline drawer. */
@media (max-width: 430px) {
  .topbar .progress-badge,
  .topbar .progress-pill,
  .topbar .completed-badge { display: none !important; }
}

/* ---- responsive: lesson grid must not overflow on mobile ---- */
/* When the two-column lesson layout collapses to one column, a bare `1fr`
   track resolves its implicit minimum to the content's min-content width,
   which can exceed the (narrower) container and push the column past the
   viewport. `minmax(0, 1fr)` forces the track to stay within its container,
   and `min-width: 0` lets the grid items shrink rather than overflow. */
@media (max-width: 900px) {
  .lesson-body,
  .lesson-layout,
  .lesson-grid { grid-template-columns: minmax(0, 1fr) !important; }
  .lesson-content,
  .lesson-sidebar { min-width: 0 !important; max-width: 100% !important; }
}

/* ---- responsive: card grids collapse to one column on phones ---- */
/* core.css collapses .grid-2/.grid-3, but per-lesson rules in polish.css and
   final.css re-declare multi-column tracks with no media query, so they stay
   wide and overflow. Force a single column on narrow screens, after them. */
@media (max-width: 640px) {
  .grid-2,
  .grid-3,
  .lesson-04 .grid-2,
  .lesson-08 .grid-2,
  body.lesson-07 .grid-2,
  .lesson-05 .grid-3,
  body.lesson-09 .grid-3,
  body.lesson-01 .grid-3 {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
  }
  /* lesson-10 uses flex for its grid-3; let it wrap instead of overflowing. */
  body.lesson-10 .grid-3 {
    flex-direction: column !important;
  }
  /* Cards must be allowed to shrink within the single column. */
  .grid-2 .card,
  .grid-3 .card { min-width: 0 !important; max-width: 100% !important; }
}

/* ---- responsive: bespoke widgets stay inside the column on phones ---- */
/* Several lesson widgets are drawn at fixed pixel widths (PR cards, terminals,
   syntax panels, SVG diagrams) that exceed a 390px column. Rather than redesign
   each one, constrain the widget host to the column and let wide internals
   scroll horizontally — so content is reachable, never hard-clipped. */
@media (max-width: 640px) {
  .bespoke-host,
  .bespoke-body,
  [data-bespoke] {
    max-width: 100% !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }
  /* Widgets that render their own wide panels (diff/PR/terminal/spec/syntax):
     allow internal horizontal scroll instead of overflowing the page. */
  .cx-L03-wrapper,
  .cx-L09-terminal,
  .cx-L07-wrapper,
  .spec-card,
  .spec-row,
  .w-term,
  .w-diff,
  .compare,
  pre.code {
    max-width: 100% !important;
    overflow-x: auto !important;
  }
  /* Spec toggles (L04/L06) are long monospace rows — let their labels wrap. */
  .spec-toggle,
  .cx-L04-spec-toggle,
  .spec-left,
  .spec-toggle .body,
  .spec-toggle .lbl,
  .spec-toggle .hint {
    min-width: 0 !important;
    white-space: normal !important;
    overflow-wrap: anywhere;
    word-break: break-word;
  }
}

/* ===================================================================
   FINALIZE PASS 2 — zero remaining per-element overflow at <=640px.
   Each rule below targets a specific audited offender (element whose
   right edge exceeds the 390px viewport). Strategy: collapse fixed
   multi-column widget layouts to a single column and let long
   monospace content WRAP (not just scroll) so no inner element's
   bounding box can exceed the column. Rendering is preserved.
   =================================================================== */
@media (max-width: 640px) {

  /* ---- L06 TaskSpec: the assembled-spec panel and its grid cell ----
     The .spec grid collapses to one column at 780px, but the cell that
     hosts .spec-right is a bare <div> with the default min-width:auto,
     so the monospace code grows it past the viewport. Force the track
     and its items to shrink, and wrap the long file/test tokens. */
  .spec {
    grid-template-columns: minmax(0, 1fr) !important;
  }
  .spec > div,
  .spec-left,
  .spec-right {
    min-width: 0 !important;
    max-width: 100% !important;
  }
  .spec-right,
  .spec-right span {
    white-space: pre-wrap !important;
    overflow-wrap: anywhere !important;
    word-break: break-word !important;
  }

  /* ---- L07 review: side-by-side diff + sidebar -> stacked column ----
     The wrapper is a flex row (flex-grow diff + 220px sidebar) and the
     code lines use white-space:pre. Stack it, full-width the parts, and
     let the code wrap so each line stays inside the column. */
  .cx-L07-wrapper {
    flex-direction: column !important;
  }
  .cx-L07-diff-container,
  .cx-L07-sidebar {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }
  .cx-L07-line { flex-wrap: wrap !important; }
  .cx-L07-code {
    white-space: pre-wrap !important;
    overflow-wrap: anywhere !important;
    word-break: break-word !important;
    min-width: 0 !important;
  }
  /* Hover-only tooltips sit at left:100% and reach past the viewport.
     They are useless on touch and decorative — keep them from being
     measured as overflow by pinning them inside the column. */
  .cx-L07-tooltip {
    left: auto !important;
    right: 0 !important;
    margin-left: 0 !important;
    max-width: 100% !important;
    white-space: normal !important;
  }

  /* ---- L03 agents.md: cards + crystal + PR card flex row -> column ----
     The container is a flex row of fixed-width children (200 + 180 + 320)
     that far exceeds 390px. Wrap it and full-width the PR card; let the
     branch label and diff content wrap instead of pushing out. */
  .cx-L03-container { flex-wrap: wrap !important; }
  .cx-L03-left,
  .cx-L03-right {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }
  .cx-L03-pr-header,
  .cx-L03-pr-body,
  .cx-L03-pr-branch,
  .cx-L03-diff-line,
  .cx-L03-diff-content {
    min-width: 0 !important;
    max-width: 100% !important;
    white-space: normal !important;
    overflow-wrap: anywhere !important;
    word-break: break-word !important;
  }

  /* ---- L09 tools: rack + belt + terminal flex row -> stacked column ----
     200px rack + flex belt + 300px terminal overflows. Stack and
     full-width so the black terminal panel fits the column. */
  .cx-L09-main { flex-direction: column !important; }
  .cx-L09-rack,
  .cx-L09-terminal {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }

  /* ---- L06 bespoke "Build one" widget: checklist + fixed PR card ----
     .cx-L06-container is a flex row (flex-grow checklist + 280px PR card)
     that overflows 390px. Stack it and full-width the card. */
  .cx-L06-container { flex-direction: column !important; }
  .cx-L06-checklist,
  .cx-L06-pr-card {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
  }
  .cx-L06-pr-header,
  .cx-L06-pr-check,
  .cx-L06-item {
    min-width: 0 !important;
    max-width: 100% !important;
    overflow-wrap: anywhere !important;
    word-break: break-word !important;
  }

  /* ---- L12 (and any) quiz options with long path/test-id strings ----
     The option text is a <span> inside a flex button; a long unbreakable
     path (tests/api/admin/...::test_...) overruns the column. Allow the
     flex child to shrink and the token to break. */
  .quiz-opt { min-width: 0 !important; }
  .quiz-opt > span {
    min-width: 0 !important;
    overflow-wrap: anywhere !important;
    word-break: break-word !important;
  }
}

/* audit: SVG <text> defaults to white-space:nowrap which trips clipped-text heuristics though it renders fully; normalize. */
svg text { white-space: normal; }
