/* =============================================================================
   Theme tokens: Nord "Frost" palette. Light by default, dark via system
   preference or the Theme button. Worksheet pages are always black on white.
   ========================================================================== */

:root {
  --bg: #d8dee9;
  --bg-top: #e5e9f0;
  --panel: #eceff4;
  --tile: #ffffff;
  --tile-hover: #f3f6fa;
  --ink: #2e3440;
  --muted: #4c566a;
  --line: #c3cad6;
  --input-bg: #ffffff;
  --frame-a: #8fbcbb;
  --frame-b: #88c0d0;
  --head-ink: #242933;
  --accent: #5e81ac;
  --accent-ink: #ffffff;
  --primary: #5e81ac;
  --primary-ink: #ffffff;
  --error: #a3303a;
  --error-bg: #f7e1e3;
  --warn: #7d5700;
  --warn-bg: #fbf0d4;
  --tip-bg: #2e3440;
  --tip-ink: #eceff4;
  --shadow: 0 2px 6px rgba(46, 52, 64, .15), 0 8px 24px rgba(46, 52, 64, .08);
  --paper-shadow: 0 2px 4px rgba(46, 52, 64, .15), 0 10px 28px rgba(46, 52, 64, .12);
  --paper-answer: #b3261e;
  --mono: "Cascadia Mono", "Cascadia Code", Consolas, "SF Mono", Menlo, "DejaVu Sans Mono", monospace;
  --sans: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #2e3440;
    --bg-top: #272c36;
    --panel: #3b4252;
    --tile: #434c5e;
    --tile-hover: #4c566a;
    --ink: #eceff4;
    --muted: #b4bccb;
    --line: #56607a;
    --input-bg: #2e3440;
    --accent: #88c0d0;
    --accent-ink: #242933;
    --primary: #88c0d0;
    --primary-ink: #242933;
    --error: #f0a3aa;
    --error-bg: rgba(191, 97, 106, .22);
    --warn: #ebcb8b;
    --warn-bg: rgba(235, 203, 139, .16);
    --tip-bg: #eceff4;
    --tip-ink: #2e3440;
    --shadow: 0 2px 6px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .2);
    --paper-shadow: 0 2px 6px rgba(0, 0, 0, .35), 0 12px 32px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #2e3440;
  --bg-top: #272c36;
  --panel: #3b4252;
  --tile: #434c5e;
  --tile-hover: #4c566a;
  --ink: #eceff4;
  --muted: #b4bccb;
  --line: #56607a;
  --input-bg: #2e3440;
  --accent: #88c0d0;
  --accent-ink: #242933;
  --primary: #88c0d0;
  --primary-ink: #242933;
  --error: #f0a3aa;
  --error-bg: rgba(191, 97, 106, .22);
  --warn: #ebcb8b;
  --warn-bg: rgba(235, 203, 139, .16);
  --tip-bg: #eceff4;
  --tip-ink: #2e3440;
  --shadow: 0 2px 6px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .2);
  --paper-shadow: 0 2px 6px rgba(0, 0, 0, .35), 0 12px 32px rgba(0, 0, 0, .3);
  color-scheme: dark;
}

/* =============================================================================
   Base
   ========================================================================== */

* { box-sizing: border-box; }
html, body { margin: 0; }

body {
  font: 14px/1.45 var(--sans);
  color: var(--ink);
  background: var(--bg);
}

[hidden] { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); }

input[type="number"], input[type="text"], select {
  font: inherit;
  padding: 4px 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--ink);
}
input[type="number"] { width: 84px; }

a { color: var(--accent); }
label { cursor: pointer; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* =============================================================================
   Top bar
   ========================================================================== */

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 24px;
  padding: 14px 24px;
  background: var(--bg-top);
  border-bottom: 1px solid var(--line);
}
.brand h1 {
  margin: 0;
  font: 600 26px/1.15 var(--mono);
  letter-spacing: -.01em;
}
.brand p { margin: 2px 0 0; font-style: italic; color: var(--muted); }
.top-actions { display: flex; flex-wrap: wrap; gap: 10px; }

/* =============================================================================
   Buttons
   ========================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 9px 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--tile);
  color: var(--ink);
  font: 600 14px/1.2 var(--sans);
  text-decoration: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .12);
  cursor: pointer;
  transition: background-color .15s, transform .15s, box-shadow .15s;
}
.btn:hover:not(:disabled) { background: var(--tile-hover); transform: translateY(-1px); box-shadow: 0 3px 8px rgba(0, 0, 0, .18); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover:not(:disabled) { background: var(--primary); filter: brightness(1.08); }
.btn:disabled { opacity: .45; cursor: not-allowed; box-shadow: none; }

/* =============================================================================
   Windows (rounded panels with a Frost title bar)
   ========================================================================== */

.window {
  margin: 0 0 18px;
  padding: 0 4px 4px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--frame-a), var(--frame-b));
  box-shadow: var(--shadow);
}

.window-head {
  display: flex;
  align-items: center;
  gap: 9px;
  min-height: 40px;
  padding: 7px 8px 7px 10px;
  color: var(--head-ink);
}
.window-head h2 {
  margin: 0;
  font: 600 15px/1.2 var(--mono);
}
.wicon {
  width: 20px;
  height: 20px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.wicon circle { fill: currentColor; stroke: none; }
.wicon circle[r="4.5"] { fill: none; stroke: currentColor; }
.window-head .help { margin-left: auto; }

.window-body {
  padding: 14px;
  border-radius: 7px;
  background: var(--panel);
}

.head-note {
  margin-left: auto;
  font-size: 13px;
  font-weight: 600;
}
.head-note.stale {
  padding: 2px 10px;
  border-radius: 999px;
  background: #ebcb8b;
  color: #3b3000;
}
.pill:empty { display: none; }
.pill {
  margin-left: 6px;
  padding: 2px 9px;
  border-radius: 999px;
  background: rgba(36, 41, 51, .18);
}

/* =============================================================================
   App layout
   ========================================================================== */

.app {
  display: grid;
  grid-template-columns: minmax(340px, 400px) minmax(0, 1fr);
  align-items: start;
  gap: 24px;
  padding: 22px 24px;
}

.settings {
  position: sticky;
  top: 0;
  max-height: 100vh;
  overflow-y: auto;
  margin: -22px 0;
  padding: 22px 6px 0 2px;
}

.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; margin-bottom: 10px; }
.row:last-child { margin-bottom: 0; }
.row-label { margin-top: 10px; }
.row-label input[type="text"] { flex: 1; min-width: 0; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.field-label { margin-bottom: 4px; font-size: 12px; font-weight: 600; color: var(--muted); }

/* Segmented control for Format */
.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--input-bg);
}
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; padding: 4px 12px; border-radius: 6px; font-weight: 600; color: var(--muted); }
.segmented input:checked + span { background: var(--primary); color: var(--primary-ink); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Operation tiles */
fieldset.op {
  margin: 0 0 12px;
  padding: 0 12px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--tile);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .12);
  min-width: 0;
}
fieldset.op:last-child { margin-bottom: 0; }
fieldset.op.is-off { padding-bottom: 0; box-shadow: none; background: transparent; }
fieldset.op.has-error { border-color: var(--error); box-shadow: 0 0 0 1px var(--error); }

.op-head {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 0;
  float: left; /* lets the legend sit inside the tile */
  font-weight: 600;
  font-size: 15px;
}
.op-head label { flex: 1; }
.op-head + * { clear: both; }
.op-sym {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: linear-gradient(135deg, var(--frame-a), var(--frame-b));
  color: var(--head-ink);
  font: 700 16px/1 var(--sans);
}
.is-off .op-sym { opacity: .55; }

.op-body { display: grid; gap: 12px; }

table.digits { border-collapse: collapse; width: 100%; }
table.digits th, table.digits td { padding: 3px 6px 3px 0; text-align: left; font-weight: 400; white-space: nowrap; }
table.digits thead th { font-size: 12px; font-weight: 600; color: var(--muted); }
table.digits td:last-child { width: 30px; text-align: right; }

.add-operand {
  justify-self: start;
  padding: 6px 12px;
  border: 1px dashed var(--accent);
  border-radius: 7px;
  background: transparent;
  color: var(--accent);
  font: 600 13px/1.2 var(--sans);
  cursor: pointer;
}
.add-operand:hover:not(:disabled) { background: var(--tile-hover); }
.add-operand:disabled { border-color: var(--line); color: var(--muted); cursor: not-allowed; }

.remove-operand {
  width: 26px;
  height: 26px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font: 600 16px/1 var(--sans);
  cursor: pointer;
}
.remove-operand:hover { border-color: var(--error); color: var(--error); }

/* Sticky action bar */
.actions {
  position: sticky;
  bottom: 0;
  margin: 0 -6px 0 -2px;
  padding: 14px 6px 22px 2px;
  background: linear-gradient(to bottom, transparent, var(--bg) 18px);
}
#messages { max-height: 40vh; overflow-y: auto; }
.buttons { display: flex; gap: 10px; }
.buttons .btn { flex: 1; padding-block: 11px; font-size: 15px; }

/* Preview */
.preview { min-width: 0; }
.paper-well {
  padding: 24px;
  overflow-x: auto;
  background: var(--bg);
}

/* =============================================================================
   Help icons and tooltip
   ========================================================================== */

.help {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  flex: none;
  border: 1.5px solid currentColor;
  border-radius: 50%;
  color: var(--muted);
  font: 700 11px/1 var(--sans);
  text-decoration: none;
  vertical-align: 1px;
  cursor: help;
  opacity: .8;
}
.help:hover, .help:focus-visible { opacity: 1; background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.window-head .help { color: var(--head-ink); }
.window-head .help:hover, .window-head .help:focus-visible { background: var(--head-ink); border-color: var(--head-ink); color: var(--frame-b); }

.tooltip {
  position: fixed;
  z-index: 10;
  max-width: 290px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--tip-bg);
  color: var(--tip-ink);
  font-size: 13px;
  line-height: 1.45;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .28);
  pointer-events: none;
}
.tooltip-more { display: block; margin-top: 6px; font-size: 12px; opacity: .7; }

/* =============================================================================
   Validation and messages
   ========================================================================== */

.field-error { margin: 0; color: var(--error); font-size: 13px; }
.field-error:empty { display: none; }
.field-error span { display: block; }
.field-error span + span { margin-top: 4px; }
#err-count, #err-ops { margin: -4px 0 10px; }
#err-ops { margin: 10px 0 0; }

[aria-invalid="true"] { border-color: var(--error) !important; background: var(--error-bg); }
input[type="checkbox"][aria-invalid="true"] { outline: 2px solid var(--error); outline-offset: 1px; }

.msg { margin: 0 0 10px; padding: 9px 12px; border-radius: 8px; font-size: 13px; box-shadow: var(--shadow); }
.msg.error { background: var(--error-bg); color: var(--error); border: 1px solid var(--error); }
.msg.warning { background: var(--warn-bg); color: var(--warn); border: 1px solid var(--warn); }
.msg ul { margin: 4px 0 0; padding-left: 18px; }
.msg li + li { margin-top: 3px; }

button.link {
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  text-decoration: underline;
  cursor: pointer;
}

.noscript {
  margin: 0;
  padding: 16px 20px;
  background: var(--error-bg);
  color: var(--error);
  font-weight: 600;
  text-align: center;
}

/* =============================================================================
   Worksheet pages (always paper colours)
   ========================================================================== */

.sheet {
  width: 8.5in;
  height: 11in;
  padding: 0.5in;
  margin: 0 auto 24px;
  background: #fff;
  color: #000;
  border-radius: 3px;
  box-shadow: var(--paper-shadow);
}
.sheet:last-child { margin-bottom: 0; }

.page-inner {
  position: relative;
  width: 7.5in;
  overflow: hidden;
}

.sheet-head { padding-bottom: 0.3in; }
.sheet-title { margin: 0 0 0.15in; font: 700 18pt/1.2 "Segoe UI", Arial, Helvetica, sans-serif; }

.fields {
  display: flex;
  flex-wrap: wrap;
  gap: 0.15in 0.35in;
  font: 12pt/1.3 "Segoe UI", Arial, Helvetica, sans-serif;
}
.field { display: inline-flex; align-items: flex-end; gap: 0.08in; }
.field .line { display: inline-block; width: 1.2in; height: 1.1em; border-bottom: 1px solid #000; }
.field--name .line { width: 2.6in; }
.field--score .line { width: 0.7in; }

.grid { display: grid; align-items: start; }

.prob {
  display: flex;
  align-items: baseline;
  gap: 0.1in;
  width: max-content;
  white-space: nowrap;
  font-family: "Segoe UI", Arial, Helvetica, sans-serif;
  font-size: 16pt;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
  color: #000;
}
.prob.stacked { align-items: flex-start; }

.num-label { min-width: 2.4em; font-size: 11pt; color: #444; }
.prob.stacked .num-label { padding-top: 0.3em; }

.expr { display: inline-flex; align-items: baseline; gap: 0.35em; }

.blank {
  display: inline-block;
  height: 1.2em;
  border-bottom: 1.5px solid #000;
  text-align: center;
}

.stack {
  display: inline-grid;
  grid-template-columns: auto auto;
  column-gap: 0.3em;
  row-gap: 0.12em;
}
.stack .val, .stack .blank { justify-self: end; text-align: right; }
.stack .rule { grid-column: 1 / -1; border-top: 2px solid #000; margin: 0.05em 0 0.1em; }
.prob.stacked .blank {
  height: 1.3em;
  padding: 0 0.15em;
  border: 1.5px solid #000;
  border-radius: 3px;
}

.answer {
  color: var(--paper-answer);
  font-weight: 700;
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}

.page-foot {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  font: 9pt/1 "Segoe UI", Arial, Helvetica, sans-serif;
  color: #666;
}

/* Off-screen area used to measure problem sizes before paginating. */
#measure {
  position: absolute;
  top: 0;
  left: -10000px;
  visibility: hidden;
}

/* =============================================================================
   Instructions page
   ========================================================================== */

.doc-wrap {
  max-width: 900px;
  margin: 0 auto;
  padding: 24px 24px 48px;
}
.doc-intro { margin: 0 0 20px; font-size: 16px; }
.doc-intro .doc-note { display: block; margin-top: 4px; font-size: 13px; color: var(--muted); }

.toc-tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.toc-tiles a {
  display: block;
  height: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--tile);
  color: var(--ink);
  font-weight: 600;
  text-align: center;
  text-decoration: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .12);
  transition: background-color .15s, transform .15s;
}
.toc-tiles a:hover { background: var(--tile-hover); transform: translateY(-1px); }

.doc-section { scroll-margin-top: 16px; transition: box-shadow .2s; }
.doc-section:target { box-shadow: 0 0 0 3px var(--accent), var(--shadow); }
.doc-section .window-body { font-size: 15px; line-height: 1.6; }
.doc-section .window-body > :first-child { margin-top: 0; }
.doc-section .window-body > :last-child { margin-bottom: 0; }
.doc-section ul, .doc-section ol { padding-left: 22px; }
.doc-section li + li { margin-top: 4px; }

.doc-table { border-collapse: collapse; margin: 8px 0 12px; }
.doc-table th, .doc-table td { padding: 6px 16px 6px 0; border-bottom: 1px solid var(--line); text-align: left; }
.doc-table th { font-size: 13px; font-weight: 600; color: var(--muted); }

.doc-dl dt { margin-top: 14px; font-weight: 600; color: var(--error); }
.doc-dl dt:first-child { margin-top: 0; }
.doc-dl dd { margin: 2px 0 0; }

.example {
  padding: 1px 6px;
  border-radius: 5px;
  background: var(--tile);
  font-family: var(--mono);
  font-size: 14px;
}

/* =============================================================================
   Landing page
   ========================================================================== */

.landing {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 24px 16px;
}
.landing-window { width: 100%; max-width: 560px; margin: 0; }
.landing-body { padding: 36px 32px 28px; text-align: center; }
.landing-body h1 {
  margin: 0 0 10px;
  font: 600 28px/1.2 var(--mono);
  letter-spacing: -.01em;
}
.landing-lede { margin: 0 auto 28px; max-width: 42ch; font-size: 16px; color: var(--muted); }
.landing-cta {
  padding: 14px 30px;
  border-radius: 999px;
  font-size: 17px;
}
.landing-contact {
  margin: 30px 0 0;
  padding-top: 18px;
  border-top: 1px solid var(--line);
  font-size: 14px;
  color: var(--muted);
}
.landing-contact a { font-weight: 600; overflow-wrap: anywhere; }

/* =============================================================================
   Narrow screens
   ========================================================================== */

@media (max-width: 900px) {
  .app { grid-template-columns: minmax(0, 1fr); padding: 16px; }
  .settings { position: static; max-height: none; overflow: visible; margin: 0; padding: 0; }
  .actions { margin: 0; padding: 12px 0 16px; }
  .topbar { padding: 12px 16px; }
  .doc-wrap { padding: 16px 16px 40px; }
}

/* =============================================================================
   Print: only the paper pages
   ========================================================================== */

@page {
  size: letter;
  margin: 0.5in;
}

@media print {
  html, body { background: #fff !important; color: #000; }
  .topbar, .settings, .window-head, .no-print, .print-exclude, .tooltip, .noscript { display: none !important; }
  .app { display: block; padding: 0; }
  .window, .window-body, .paper-well {
    margin: 0;
    padding: 0;
    border-radius: 0;
    background: none;
    box-shadow: none;
    overflow: visible;
  }

  .sheet {
    width: auto;
    height: auto;
    margin: 0;
    padding: 0;
    border-radius: 0;
    box-shadow: none;
    page-break-after: always;
    break-after: page;
  }
  .sheet.is-last { page-break-after: auto; break-after: auto; }
  .sheet.first-answer { page-break-before: always; break-before: page; }
}
