:root {
  /* Dark theme matching the Digital Opus Tools hub brand
     (digitalopusadsolutions.com), so this product looks consistent once
     hosted alongside it. Worksheet OUTPUT content (.sheet and everything
     inside it) intentionally stays light/white regardless — see the
     .sheet rule below — since printed worksheets need readable text on
     white paper, not a dark-mode PDF. */
  color-scheme: dark;
  --ink: #f4f7f4;
  --paper: #1b211b;
  --paper-hover: #222a22;
  --muted: #aab4aa;
  --line: #343e34;
  --accent: #8ab681;
  --accent-dark: #6e9a66;
  --bg: #111511;
}

* { box-sizing: border-box; }

.hidden { display: none !important; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
}

.app {
  display: grid;
  grid-template-columns: 300px 1fr;
  min-height: 100vh;
}

/* ---------- Grade-level picker — same visible-card pattern as Teacher
   Tools' resource picker, so a student lands on their own grade level
   instead of a mixed-band subject list. ---------- */
#picker-screen, #grade-picker-screen, #subject-picker-screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 60px 20px;
}

.picker-header { text-align: center; margin-bottom: 6px; max-width: 900px; }
.picker-header h1 { font-size: 26px; margin: 0 0 6px; }
.picker-header .sub { color: var(--muted); margin: 0 0 34px; }
.picker-header .back-btn { padding: 0 0 10px; }

#picker-screen > .hint { color: var(--muted); font-size: 14px; margin: -20px 0 30px; }

#library-search-wrap { width: 100%; max-width: 420px; margin-bottom: 30px; }
#library-search-wrap .search-box {
  width: 100%;
  padding: 11px 16px;
  font-size: 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  box-sizing: border-box;
}
#library-search-wrap .search-box:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(138, 182, 129, 0.45); }

#library-search-results { width: 100%; max-width: 900px; margin-bottom: 30px; }
#library-search-results .picker-card p { color: var(--muted); }

.no-results { color: var(--muted); font-size: 14px; text-align: center; margin-top: -14px; margin-bottom: 30px; }
.search-count { color: var(--muted); font-size: 13px; text-align: center; margin-top: -14px; margin-bottom: 20px; }

#subject-sections { width: 100%; max-width: 900px; }

.picker-section { width: 100%; max-width: 900px; margin-bottom: 32px; }
.picker-section:last-child { margin-bottom: 0; }
.picker-section h3 {
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 14px;
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 6px;
}
.picker-section h3::before { content: "▾"; font-size: 10px; }
.picker-section.collapsed h3::before { content: "▸"; }
.picker-section.collapsed .picker-cards { display: none; }
.picker-section.collapsed .picker-section-sub { display: none; }

.picker-section-sub { color: var(--muted); font-size: 13px; margin: -6px 0 14px; }

.picker-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 18px;
  width: 100%;
}

button.picker-card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 24px 20px;
  text-align: left;
  cursor: pointer;
  font: inherit;
  color: var(--ink);
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
  transition: transform .15s, border-color .15s;
}
button.picker-card:hover,
button.picker-card:focus-visible {
  transform: translateY(-3px);
  border-color: var(--accent);
  outline: none;
}
button.picker-card:focus-visible { box-shadow: 0 0 0 3px rgba(138, 182, 129, 0.45); }
button.picker-card .picker-icon { font-size: 26px; }
button.picker-card h2 { font-size: 16px; margin: 0; }
button.picker-card p { font-size: 13px; color: var(--muted); margin: 0; line-height: 1.4; }

.back-btn {
  width: auto;
  display: inline-block;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  padding: 0 0 14px;
  cursor: pointer;
  text-align: left;
}
.back-btn:hover { color: var(--accent-dark); text-decoration: underline; }

/* ---------- Controls panel ---------- */
.controls {
  background: var(--paper);
  border-right: 1px solid var(--line);
  padding: 20px;
  overflow-y: auto;
  position: sticky;
  top: 0;
  align-self: start;
  max-height: 100vh;
}

.controls h1 {
  font-size: 18px;
  margin: 0 0 16px;
}

.hint {
  font-size: 13px;
  color: var(--muted);
  margin: 0 0 14px;
}

.field {
  margin-bottom: 14px;
}

.field label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 4px;
}

.field select,
.field input[type="text"],
.field input[type="number"] {
  width: 100%;
  padding: 7px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 14px;
  background: var(--paper);
  color: var(--ink);
}

.opts {
  border: 1px solid var(--line);
  border-radius: 8px;
  margin: 0 0 16px;
  padding: 10px 12px 14px;
  max-height: 260px;
  overflow-y: auto;
}

.opts.hidden { display: none; }

.opts legend {
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  padding: 0 4px;
}

.check {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  margin: 6px 0;
  font-weight: 400;
  color: var(--ink);
}

.check input { margin: 0; }

.badge-unlimited {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 2px 7px;
  border-radius: 10px;
  color: var(--accent-dark);
  background: rgba(138, 182, 129, 0.18);
  border: 1px solid var(--accent);
  white-space: nowrap;
}

.topic-row {
  padding: 4px 0 8px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 2px;
}
.topic-row:last-child { border-bottom: none; }
.topic-row-actions {
  display: flex; align-items: center; gap: 10px; margin: 2px 0 0 24px;
}
.topic-info-link { font-size: 12px; color: var(--muted); text-decoration: none; }
.topic-info-link:hover { color: var(--accent); text-decoration: underline; }
.topic-open-practice { padding: 3px 9px; font-size: 12px; }

hr {
  border: none;
  border-top: 1px solid var(--line);
  margin: 16px 0;
}

.btn {
  display: block;
  width: 100%;
  padding: 9px 10px;
  margin-bottom: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.btn:hover { background: var(--paper-hover); }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn.primary:hover { background: var(--accent-dark); }

/* ---------- Worksheet display ---------- */
.sheet-wrap {
  padding: 32px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  align-items: center;
}

.sheet {
  /* Worksheet output always simulates white paper, independent of the
     site's dark chrome theme — nested rules that read --ink/--paper/
     --muted/--line/--accent/--accent-dark inherit these light values
     through the CSS custom property cascade. */
  --ink: #1f2430;
  --paper: #ffffff;
  --muted: #6b7280;
  --line: #d8dce3;
  --accent: #2f6f4f;
  --accent-dark: #21503a;
  --bg: #eef1f5;
  background: var(--paper);
  color: var(--ink);
  width: 100%;
  max-width: 850px;
  padding: 40px 48px;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08), 0 8px 24px rgba(0,0,0,0.06);
}

.answerkey-only {
  border: 2px dashed var(--line);
}

.sheet-header h2 {
  margin: 0 0 10px;
  font-size: 24px;
}

.name-line {
  display: flex;
  gap: 28px;
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 24px;
  flex-wrap: wrap;
}

.problems {
  margin: 0;
  padding-left: 0;
  list-style: none;
  display: grid;
  gap: 14px 24px;
}

.problems.cols-1 { grid-template-columns: 1fr; }
.problems.cols-2 { grid-template-columns: 1fr 1fr; }
.problems.cols-3 { grid-template-columns: 1fr 1fr 1fr; }

.problem {
  font-size: 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: baseline;
}

/* Preserves literal newlines/indentation in question text (used by the
   Programming generator's code snippets) without needing raw HTML in
   the text itself — a plain "\n" renders correctly here AND on the
   static topic pages' escaped sample-item text (see .sample-q in
   topic-page.css), unlike a <br> tag would. */
.problem .q { white-space: pre-wrap; font-family: inherit; }

.problem .num {
  color: var(--muted);
  font-weight: 600;
  min-width: 22px;
}

.problem .answer {
  color: var(--accent-dark);
  font-weight: 700;
  margin-left: 6px;
}

.problem .answer.hidden-answer { display: none; }

/* "Add space to show work" toggle — a blank line under the question,
   forced onto its own row inside the .problem flex layout via
   flex-basis: 100%, so num/question stay on the first line unchanged
   when the toggle is off. */
.problem .workspace-line {
  flex-basis: 100%;
  height: 26px;
  margin: 2px 0 2px 30px;
  border-bottom: 1px dashed #99a399;
}

/* ---------- Walkthroughs ---------- */
.walkthrough-only {
  border: 2px dashed var(--line);
}

.walkthrough-list {
  margin: 0;
  padding-left: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.wt-item {
  border-bottom: 1px solid var(--line);
  padding-bottom: 14px;
}

.wt-item:last-child { border-bottom: none; }

.wt-q {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 6px;
  display: flex;
  gap: 8px;
  white-space: pre-wrap;
}

.wt-q .num { color: var(--muted); min-width: 22px; }

.wt-steps {
  margin: 4px 0 6px 30px;
  padding-left: 18px;
  font-size: 13px;
  color: var(--ink);
  line-height: 1.5;
}

.wt-steps li { margin-bottom: 2px; }

.wt-answer {
  margin-left: 30px;
  font-size: 13px;
  font-weight: 700;
  color: var(--accent-dark);
}

/* ---------- Interactive self-check quiz — lives in the sidebar, right
   below the Start Quiz button, so it's immediately visible instead of
   appearing far below a long generated worksheet output. ---------- */
.quiz-wrap { width: 100%; margin-top: 16px; }
.quiz-runner {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.quiz-progress { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin: 0; }
.quiz-question { font-size: 15px; font-weight: 700; line-height: 1.4; white-space: pre-wrap; }
.quiz-scratch {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  font-size: 14px;
  resize: vertical;
}
.quiz-answer { border-top: 1px solid var(--line); padding-top: 14px; display: flex; flex-direction: column; gap: 10px; }
.quiz-answer-label { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; margin: 0; }
.quiz-answer-text { font-size: 16px; font-weight: 700; color: var(--accent-dark); margin: 0; }
.quiz-selfmark { display: flex; flex-direction: column; gap: 8px; }
.quiz-selfmark .btn { width: 100%; padding: 8px 10px; margin: 0; font-size: 13px; }
.quiz-mark-correct { border-color: var(--accent); color: var(--accent-dark); }
.quiz-mark-wrong { border-color: #b91c1c; color: #b91c1c; }
.quiz-summary { align-items: center; text-align: center; }
.quiz-summary h3 { margin: 0; font-size: 17px; }

.flashcard-runner {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.flashcard-progress { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin: 0; }
.flashcard {
  width: 100%;
  min-height: 140px;
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  white-space: pre-wrap;
  background: var(--paper);
  border: 2px solid var(--accent-dark);
  border-radius: 12px;
  color: var(--ink);
  font: inherit;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.4;
  cursor: pointer;
}
.flashcard.is-flipped { border-color: var(--accent); color: var(--accent-dark); }
.flashcard-controls { display: flex; gap: 8px; width: 100%; }
.flashcard-controls .btn { flex: 1; padding: 8px 6px; font-size: 13px; }
.quiz-score { font-size: 22px; font-weight: 800; color: var(--accent-dark); margin: 0; }
.quiz-summary .btn { width: 100%; padding: 8px 10px; margin: 0 0 6px; }

/* ---------- Mobile / small screens ---------- */
@media (max-width: 768px) {
  .app {
    grid-template-columns: 1fr;
  }

  .controls {
    border-right: none;
    border-bottom: 1px solid var(--line);
    position: static;
    overflow-y: visible;
    max-height: none;
  }

  .sheet-wrap {
    padding: 16px;
    gap: 16px;
  }

  #picker-screen,
  #grade-picker-screen,
  #subject-picker-screen {
    padding: 32px 16px;
  }

  .problems.cols-2,
  .problems.cols-3 {
    grid-template-columns: 1fr;
  }
}

/* ---------- Print ---------- */
@media print {
  .no-print { display: none !important; }

  .app {
    display: block;
  }

  .sheet-wrap {
    padding: 0;
    gap: 0;
  }

  .sheet {
    box-shadow: none;
    border-radius: 0;
    max-width: 100%;
    width: 100%;
    padding: 0;
    page-break-after: always;
  }

  body.printing-worksheet .answerkey-only,
  body.printing-worksheet .walkthrough-only { display: none; }
  body.printing-answers .sheet:not(.answerkey-only) { display: none; }
  body.printing-answers .answerkey-only { border: none; }
  body.printing-walkthroughs .sheet:not(.walkthrough-only) { display: none; }
  body.printing-walkthroughs .walkthrough-only { border: none; }
}
