/* ============================================================
   Design tokens
   ============================================================ */
:root {
  --paper:      #F1F4F7;
  --paper-card: #FFFFFF;
  --ink:        #003661;
  --ink-soft:   #4F6C85;
  --rule:       #CDD9E2;
  --rule-red:   #C41230;

  --help:       #1D6FA5;
  --help-bg:    #E2EEF6;
  --core:       #003661;
  --core-bg:    #E1E8ED;
  --extension:  #C41230;
  --extension-bg: #F7E1E5;

  --radius: 10px;
  --shadow: 0 1px 0 rgba(0,54,97,0.06), 0 6px 16px -10px rgba(0,54,97,0.35);

  --font-display: "Space Grotesk", sans-serif;
  --font-body: "IBM Plex Sans", sans-serif;
  --font-mono: "IBM Plex Mono", monospace;
}

* { box-sizing: border-box; }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

body {
  margin: 0;
  background: var(--paper);
  background-image:
    linear-gradient(var(--rule) 1px, transparent 1px),
    linear-gradient(90deg, var(--rule) 1px, transparent 1px);
  background-size: 28px 28px;
  background-attachment: fixed;
  color: var(--ink);
  font-family: var(--font-body);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.page {
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 24px 64px;
}

/* ============================================================
   Masthead — ruled exercise-book page header
   ============================================================ */
.masthead {
  position: relative;
  background: var(--paper-card);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-top: 32px;
  padding: 28px 28px 28px 44px;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  flex-wrap: wrap;
  overflow: hidden;
}

.masthead-rule {
  position: absolute;
  left: 26px;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--rule-red);
  opacity: 0.55;
}

.masthead-inner { max-width: 620px; }

.masthead-actions {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}

.masthead-buttons {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.site-switch-link {
  font-family: var(--font-mono);
  font-size: 12.5px;
  font-weight: 600;
  color: var(--help);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 0.12s ease;
}
.site-switch-link:hover { border-color: var(--help); }

@media (max-width: 760px) {
  .masthead-actions { align-items: flex-start; width: 100%; }
  .masthead-buttons { width: 100%; }
}

.eyebrow {
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--core);
  margin: 0 0 10px;
}

.masthead h1 {
  font-family: var(--font-display);
  font-size: clamp(28px, 4vw, 40px);
  font-weight: 700;
  margin: 0 0 10px;
  letter-spacing: -0.01em;
}

.lede {
  margin: 0;
  color: var(--ink-soft);
  font-size: 15px;
  max-width: 52ch;
}

/* ============================================================
   Buttons
   ============================================================ */
.btn {
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 14px;
  border-radius: 999px;
  padding: 11px 20px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn:focus-visible {
  outline: 3px solid var(--help);
  outline-offset: 2px;
}

.btn-primary {
  background: var(--ink);
  color: var(--paper-card);
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.btn-primary:hover { background: #00294D; }
.btn-icon { font-size: 16px; }

.btn-secondary {
  background: var(--core-bg);
  color: var(--core);
  border-color: color-mix(in srgb, var(--core) 30%, transparent);
}
.btn-secondary:hover { background: #CFE0EC; }

.btn-ghost {
  background: transparent;
  color: var(--ink-soft);
  border-color: var(--rule);
}
.btn-ghost:hover { background: rgba(30,42,56,0.05); }

/* ============================================================
   Grid of 4 quadrants
   ============================================================ */
.grid {
  margin-top: 28px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

@media (max-width: 760px) {
  .grid { grid-template-columns: 1fr; }
  .masthead { flex-direction: column; align-items: stretch; }
}

.card {
  background: var(--paper-card);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-number {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--ink-soft);
  letter-spacing: 0.06em;
}

.level-pill {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--core-bg);
  color: var(--core);
}
.level-pill.level-help { background: var(--help-bg); color: var(--help); }
.level-pill.level-core { background: var(--core-bg); color: var(--core); }
.level-pill.level-extension { background: var(--extension-bg); color: var(--extension); }

/* ---------- controls ---------- */
.controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.controls .field:nth-child(2) { grid-column: 1 / -1; }

.field { display: flex; flex-direction: column; gap: 6px; }

.field-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

select {
  font-family: var(--font-body);
  font-size: 13.5px;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 8px;
  padding: 9px 10px;
  cursor: pointer;
}
select:focus-visible {
  outline: 3px solid var(--help);
  outline-offset: 1px;
}
select:disabled { color: var(--ink-soft); cursor: not-allowed; }

/* ---------- question panel ---------- */
.question-panel {
  background: var(--paper);
  border: 1px dashed var(--rule);
  border-radius: 8px;
  padding: 16px;
  min-height: 88px;
}

.code-tag {
  margin: 0 0 8px;
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--core);
  letter-spacing: 0.02em;
}

.question-text {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 16px;
  line-height: 1.6;
  color: var(--ink);
}

/* ---------- fraction rendering ---------- */
.frac {
  display: inline-flex;
  flex-direction: column;
  vertical-align: middle;
  text-align: center;
  margin: 0 3px;
  font-size: 0.85em;
  line-height: 1.1;
}
.frac .num { border-bottom: 1.5px solid var(--ink); padding: 0 3px 1px; }
.frac .den { padding: 1px 3px 0; }

/* ---------- mini table (probability two-way tables) ---------- */
.mini-table {
  border-collapse: collapse;
  margin: 10px 0;
  font-family: var(--font-mono);
  font-size: 13px;
}
.mini-table th, .mini-table td {
  border: 1px solid var(--rule);
  padding: 5px 10px;
  text-align: center;
}
.mini-table th { background: var(--core-bg); color: var(--core); }

/* ---------- actions ---------- */
.card-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.answer-panel {
  background: var(--extension-bg);
  border-left: 3px solid var(--extension);
  border-radius: 0 8px 8px 0;
  padding: 12px 14px;
}
.answer-label {
  margin: 0 0 4px;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--extension);
}
.answer-text {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 14.5px;
  color: var(--ink);
}

/* ============================================================
   Worksheet view — clean Q1–Q4 overlay for display / printing
   ============================================================ */
#worksheet-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 28, 36, 0.55);
  z-index: 100;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 20px;
  overflow-y: auto;
}
#worksheet-overlay[hidden] { display: none; }
#worksheet-overlay:not([hidden]) { display: flex; }

.worksheet-sheet {
  background: var(--paper-card);
  border-radius: var(--radius);
  box-shadow: 0 20px 50px -20px rgba(0,0,0,0.5);
  width: 100%;
  max-width: 900px;
  padding: 24px;
}

.worksheet-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--rule);
}

.worksheet-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
  color: var(--ink-soft);
  cursor: pointer;
}
.worksheet-toggle input { width: 16px; height: 16px; cursor: pointer; }

.worksheet-bar-actions { display: flex; gap: 10px; }

.worksheet-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
}

@media (max-width: 640px) {
  .worksheet-grid { grid-template-columns: 1fr; }
}

.worksheet-cell {
  border: 1px solid var(--rule);
  border-radius: 8px;
  padding: 18px;
  background: var(--paper);
  min-height: 120px;
}

.worksheet-q-label {
  margin: 0 0 10px;
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 13px;
  letter-spacing: 0.06em;
  color: var(--core);
}

.worksheet-q-text {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 16px;
  line-height: 1.6;
  color: var(--ink);
}

.worksheet-a-text {
  margin: 14px 0 0;
  padding-top: 12px;
  border-top: 1px dashed var(--rule);
  font-family: var(--font-mono);
  font-size: 14px;
  color: var(--extension);
}

.worksheet-work-space {
  margin: 14px 0 0;
  padding-top: 10px;
  border-top: 1px dashed var(--rule);
  min-height: 140px;
  background-image: repeating-linear-gradient(
    to bottom,
    transparent,
    transparent 27px,
    var(--rule) 28px
  );
}

.worksheet-work-label {
  margin: 0 0 4px;
  font-family: var(--font-mono);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

/* ---------- print: show only the worksheet sheet ---------- */
@media print {
  body * { visibility: hidden; }
  #worksheet-overlay, #worksheet-overlay * { visibility: visible; }
  #worksheet-overlay {
    position: absolute;
    inset: 0;
    background: #fff;
    padding: 0;
    display: block;
  }
  .worksheet-sheet { box-shadow: none; max-width: 100%; }
  .worksheet-bar { display: none; }
  body { background: #fff; }
}

/* ============================================================
   Footer
   ============================================================ */
.site-footer {
  margin-top: 32px;
  padding: 18px 4px 0;
  font-size: 13px;
  color: var(--ink-soft);
  border-top: 1px solid var(--rule);
}
.site-footer a { color: var(--core); }
