/* Semantic paper & ink tokens — Light (warm notebook sheet) and Dark (drafting slate) */
:root {
  --desk-bg: #EFECE5;
  --paper-bg: #FBF9F4;
  --paper-line: rgba(74, 114, 168, 0.11);
  --margin-line: rgba(196, 88, 88, 0.24);
  --bar-bg: rgba(251, 249, 244, 0.78);

  --ink-primary: #1B2432;      /* 14.5:1 on #FBF9F4 */
  --ink-secondary: #3B4657;    /* 8.8:1 on #FBF9F4 */
  --ink-muted: #5C6675;        /* 5.5:1 on #FBF9F4 */
  --ink-blue: #164C96;         /* 8.1:1 on #FBF9F4 (fountain pen blue) */
  --ink-red: #B23322;          /* 6.1:1 on #FBF9F4 (margin/correction ink) */
  --ink-rule: rgba(27, 36, 50, 0.24);
  --ink-rule-soft: rgba(27, 36, 50, 0.14);

  --sheet-shadow: 0 2px 10px rgba(27, 36, 50, 0.06), 0 18px 40px rgba(27, 36, 50, 0.05);

  --font-hand: 'Kalam', 'Segoe Print', 'Bradley Hand', cursive, sans-serif;
  --font-display: 'Caveat', 'Kalam', cursive, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --desk-bg: #111317;
    --paper-bg: #181B20;
    --paper-line: rgba(140, 170, 215, 0.08);
    --margin-line: rgba(224, 112, 112, 0.2);
    --bar-bg: rgba(24, 27, 32, 0.8);

    --ink-primary: #EFECE3;    /* 14.2:1 on #181B20 */
    --ink-secondary: #C8C5BC;  /* 9.6:1 on #181B20 */
    --ink-muted: #989791;      /* 5.6:1 on #181B20 */
    --ink-blue: #7AB4FF;       /* 8.2:1 on #181B20 */
    --ink-red: #FF7B6B;        /* 6.8:1 on #181B20 */
    --ink-rule: rgba(239, 236, 227, 0.26);
    --ink-rule-soft: rgba(239, 236, 227, 0.14);

    --sheet-shadow: 0 4px 24px rgba(0, 0, 0, 0.45);
  }
}

*, *::before, *::after {
  box-sizing: border-box;
}

html {
  font-size: 100%;
  background: var(--desk-bg);
  color: var(--ink-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  scroll-padding-top: 4.75rem;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-hand);
  font-size: 1.08rem;
  line-height: 1.72;
  padding-bottom: max(3.5rem, env(safe-area-inset-bottom));
}

/* Floating translucent hand-drawn utility bar (Apple material + fluid spring ring) */
.floating-bar {
  position: sticky;
  top: 0;
  z-index: 40;
  background: var(--bar-bg);
  backdrop-filter: blur(18px) saturate(160%);
  -webkit-backdrop-filter: blur(18px) saturate(160%);
  border-bottom: 1px solid transparent;
  transition: border-color 180ms ease;
}

.floating-bar.is-scrolled {
  border-bottom-color: var(--ink-rule-soft);
}

.bar-inner {
  max-width: 820px;
  margin: 0 auto;
  padding: 0.45rem max(1rem, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.jump-nav {
  flex: 1;
  min-width: 0;
}

.nav-track {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.15rem;
  overflow-x: auto;
  scrollbar-width: none;
  max-width: 100%;
  padding: 0.15rem 0.2rem;
}

.nav-track::-webkit-scrollbar {
  display: none;
}

.nav-ring {
  position: absolute;
  top: 50%;
  left: 0;
  height: 32px;
  margin-top: -16px;
  opacity: 0;
  pointer-events: none;
  will-change: transform, width;
}

.nav-ring svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}

.nav-ring path {
  fill: none;
  stroke: var(--ink-blue);
  stroke-width: 2.1;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.nav-link {
  position: relative;
  z-index: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 42px;
  min-height: 36px;
  padding: 0 0.55rem;
  font-family: var(--font-display);
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--ink-muted);
  text-decoration: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: color 140ms ease, transform 95ms ease-out;
}

.nav-link:hover,
.nav-link.is-active {
  color: var(--ink-blue);
}

.nav-link:active {
  transform: scale(0.93);
}

.sketch-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 36px;
  padding: 0.15rem 0.85rem;
  background: transparent;
  color: var(--ink-primary);
  font-family: var(--font-hand);
  font-size: 0.95rem;
  font-weight: 700;
  border: 1.7px solid var(--ink-rule);
  border-radius: 255px 18px 225px 18px / 18px 225px 18px 255px;
  cursor: pointer;
  flex-shrink: 0;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: color 120ms ease, border-color 120ms ease, transform 95ms ease-out;
}

.sketch-btn:hover {
  color: var(--ink-blue);
  border-color: var(--ink-blue);
}

.sketch-btn:active {
  transform: scale(0.95);
}

:focus-visible {
  outline: 2.5px solid var(--ink-blue);
  outline-offset: 2px;
  border-radius: 4px;
}

/* Single continuous written notebook sheet — ZERO secluded sub-boxes or cards */
.sheet {
  position: relative;
  max-width: 820px;
  margin: 1.5rem auto 0;
  padding: 2.75rem 3.25rem 3.5rem 4.75rem;
  background-color: var(--paper-bg);
  background-image:
    linear-gradient(to right, transparent 3.45rem, var(--margin-line) 3.45rem, var(--margin-line) calc(3.45rem + 1.5px), transparent calc(3.45rem + 1.5px)),
    repeating-linear-gradient(to bottom, transparent 0, transparent 2.05rem, var(--paper-line) 2.05rem, var(--paper-line) calc(2.05rem + 1px));
  background-position: 0 0, 0 1.45rem;
  box-shadow: var(--sheet-shadow);
  border-radius: 3px;
}

/* Top assignment heading directly on the page */
.sheet-heading {
  margin-bottom: 1.75rem;
}

.heading-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.sheet-heading h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(2rem, 4.2vw, 2.75rem);
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.01em;
  color: var(--ink-primary);
}

.mm-mark {
  font-family: var(--font-display);
  font-size: 1.45rem;
  font-weight: 700;
  color: var(--ink-red);
  white-space: nowrap;
}

.rule-double {
  display: block;
  width: 100%;
  height: 13px;
  margin-top: 0.35rem;
}

.rule-double path {
  fill: none;
  stroke: var(--ink-primary);
  stroke-width: 2.1;
  stroke-linecap: round;
  opacity: 0.8;
}

.rule-double path.thin {
  stroke-width: 1.2;
  opacity: 0.55;
}

/* Flowing written question + solution entries directly on the sheet */
.q-entry {
  padding: 0.5rem 0 0.75rem;
}

.question-text {
  margin: 0 0 0.75rem;
  font-weight: 700;
  font-size: 1.12rem;
  line-height: 1.6;
  color: var(--ink-primary);
}

.q-num {
  color: var(--ink-red);
  font-family: var(--font-display);
  font-size: 1.45rem;
  font-weight: 700;
  margin-right: 0.3rem;
}

.question-text code,
.solution-body code {
  font-family: var(--font-hand);
  font-weight: 700;
  color: var(--ink-blue);
  background: none;
  padding: 0;
}

.solution-body {
  color: var(--ink-secondary);
}

.solution-body p {
  margin: 0 0 0.45rem;
}

.solution-body strong {
  color: var(--ink-primary);
  font-weight: 700;
}

.sol-lead {
  font-family: var(--font-display);
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--ink-blue);
  margin-bottom: 0.3rem !important;
  text-decoration: underline;
  text-decoration-style: wavy;
  text-decoration-thickness: 1.3px;
  text-underline-offset: 3px;
}

.math-line {
  padding-left: 1.35rem;
  color: var(--ink-primary);
  font-variant-numeric: tabular-nums;
}

.aside-note {
  font-size: 0.96rem;
  color: var(--ink-muted);
  font-style: italic;
}

/* Final answer underlined naturally with pen stroke (no box!) */
.final-ans {
  margin-top: 0.75rem !important;
  margin-bottom: 0 !important;
  color: var(--ink-primary);
  font-size: 1.1rem;
}

.ans-tag {
  font-family: var(--font-display);
  font-size: 1.42rem;
  font-weight: 700;
  color: var(--ink-blue);
  margin-right: 0.35rem;
}

.ink-underline {
  text-decoration: underline;
  text-decoration-color: var(--ink-blue);
  text-decoration-thickness: 2px;
  text-underline-offset: 4px;
}

/* Subtle single pen-stroke divider between questions */
.ink-divider {
  display: block;
  width: 100%;
  height: 10px;
  margin: 1.25rem 0 1.15rem;
}

.ink-divider path {
  fill: none;
  stroke: var(--ink-rule);
  stroke-width: 1.4;
  stroke-linecap: round;
  stroke-dasharray: 10 6 24 5 14 7;
}

/* Hand-ruled pen tables directly on the sheet (transparent, no boxes/fills) */
.table-scroll {
  overflow-x: auto;
  margin: 0.65rem 0 0.85rem;
}

.ink-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 1rem;
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
  text-align: left;
  background: transparent;
}

.ink-table th {
  padding: 0.4rem 0.65rem;
  font-family: var(--font-display);
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--ink-primary);
  border-bottom: 2px solid var(--ink-primary);
  border-right: 1px dashed var(--ink-rule);
}

.ink-table td {
  padding: 0.35rem 0.65rem;
  color: var(--ink-secondary);
  border-bottom: 1px solid var(--ink-rule-soft);
  border-right: 1px dashed var(--ink-rule);
}

.ink-table th:last-child,
.ink-table td:last-child {
  border-right: none;
}

.ink-table tbody tr:last-child td {
  border-bottom: none;
}

.ink-table tr.ink-mark td {
  color: var(--ink-blue);
  font-weight: 700;
}

.ink-table tr.ink-alert td {
  color: var(--ink-red);
  font-weight: 700;
}

/* Hand-drawn inline graph for Q4 — sits directly on the paper */
.sketch-graph {
  margin: 0.9rem 0 0.85rem;
  padding: 0;
}

.sketch-graph svg {
  width: 100%;
  max-width: 600px;
  height: auto;
  display: block;
}

.g-axis {
  fill: none;
  stroke: var(--ink-primary);
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.g-thresh {
  fill: none;
  stroke: var(--ink-muted);
  stroke-width: 1.3;
  stroke-dasharray: 6 5;
  stroke-linecap: round;
}

.g-tick {
  stroke: var(--ink-primary);
  stroke-width: 1.5;
  stroke-linecap: round;
}

.g-curve {
  fill: none;
  stroke: var(--ink-blue);
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.g-drop {
  fill: none;
  stroke: var(--ink-red);
  stroke-width: 2.1;
  stroke-dasharray: 5 4;
  stroke-linecap: round;
}

.g-dot {
  fill: var(--paper-bg);
  stroke: var(--ink-blue);
  stroke-width: 2.1;
}

.g-dot-warn {
  fill: var(--ink-red);
}

.g-dot-final {
  fill: var(--ink-blue);
}

.g-circle {
  fill: none;
  stroke: var(--ink-blue);
  stroke-width: 1.7;
  stroke-linecap: round;
}

.g-label {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 700;
  fill: var(--ink-primary);
}

.g-note {
  font-family: var(--font-hand);
  font-size: 12px;
  fill: var(--ink-muted);
}

.g-tick-lbl {
  font-family: var(--font-hand);
  font-size: 12.5px;
  fill: var(--ink-secondary);
}

.g-warn {
  font-family: var(--font-display);
  font-size: 15.5px;
  font-weight: 700;
  fill: var(--ink-red);
}

.g-final {
  font-family: var(--font-display);
  font-size: 16.5px;
  font-weight: 700;
  fill: var(--ink-blue);
}

/* Responsive mobile view — keep continuous sheet comfortable */
@media (max-width: 680px) {
  body {
    font-size: 1.02rem;
  }

  .sheet {
    margin-top: 0;
    padding: 1.5rem 1.25rem 2.75rem 1.4rem;
    background-image:
      repeating-linear-gradient(to bottom, transparent 0, transparent 2.05rem, var(--paper-line) 2.05rem, var(--paper-line) calc(2.05rem + 1px));
    background-position: 0 0.7rem;
    box-shadow: none;
    border-radius: 0;
  }

  .math-line {
    padding-left: 0.75rem;
  }

  .nav-link {
    min-width: 44px;
    min-height: 40px;
  }

  .sketch-btn {
    min-height: 40px;
  }
}

/* Accessibility: Reduced motion, Reduced transparency, Increased contrast */
@media (prefers-reduced-motion: reduce) {
  .nav-link,
  .sketch-btn,
  .floating-bar {
    transition: none;
  }
  .nav-link:active,
  .sketch-btn:active {
    transform: none;
  }
}

@media (prefers-reduced-transparency: reduce) {
  .floating-bar {
    background: var(--paper-bg);
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    border-bottom-color: var(--ink-rule);
  }
}

@media (prefers-contrast: more) {
  :root {
    --ink-secondary: #1B2432;
    --ink-muted: #2B3647;
    --ink-rule: rgba(27, 36, 50, 0.55);
  }
  @media (prefers-color-scheme: dark) {
    :root {
      --ink-secondary: #EFECE3;
      --ink-muted: #DAD7CE;
      --ink-rule: rgba(239, 236, 227, 0.55);
    }
  }
}

/* Print — crisp written submission sheet on white paper */
@media print {
  :root {
    --desk-bg: #FFFFFF;
    --paper-bg: #FFFFFF;
    --paper-line: transparent;
    --margin-line: rgba(180, 70, 70, 0.25);
    --ink-primary: #111111;
    --ink-secondary: #222222;
    --ink-muted: #444444;
    --ink-blue: #113B7A;
    --ink-red: #9A2618;
    --ink-rule: rgba(0, 0, 0, 0.35);
    --ink-rule-soft: rgba(0, 0, 0, 0.18);
  }

  .floating-bar {
    display: none !important;
  }

  .sheet {
    margin: 0;
    max-width: 100%;
    padding: 0.4in 0.5in 0.5in 0.75in;
    box-shadow: none;
    background-image: linear-gradient(to right, transparent 0.55in, var(--margin-line) 0.55in, var(--margin-line) calc(0.55in + 1px), transparent calc(0.55in + 1px));
  }

  .q-entry {
    break-inside: avoid;
    page-break-inside: avoid;
  }
}
