:root {
  --border: #d9d9df;
  --accent: #2c5cc5;
  --bg-subtle: #f6f7f9;
  --bg: #fff;
  --text: #1b1d22;
  --text-muted: #666a73;
  --correct-bg: #d9f5e3;
  --correct-text: #1a7a3a;
  --incorrect-bg: #fbdcdc;
  --incorrect-text: #a91b1b;
  --chip-bg: #fff;
  --chip-active-bg: var(--accent);
  --panel-bg: #fff;
}

[data-theme="dark"] {
  --border: #383b42;
  --accent: #6d93ff;
  --bg-subtle: #22242a;
  --bg: #16171b;
  --text: #e7e8ea;
  --text-muted: #9a9ea8;
  --correct-bg: #123822;
  --correct-text: #6fe3a0;
  --incorrect-bg: #3a1616;
  --incorrect-text: #ff8a8a;
  --chip-bg: #22242a;
  --chip-active-bg: var(--accent);
  --panel-bg: #1d1f25;
}

* { box-sizing: border-box; }

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

.layout {
  display: flex;
  min-height: 100vh;
}

.filters {
  width: 340px;
  flex-shrink: 0;
  padding: 16px;
  background: var(--bg-subtle);
  border-right: 1px solid var(--border);
}

.filters-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.auth-error {
  max-width: 480px;
  margin: 80px auto;
  padding: 20px 24px;
  text-align: center;
  font-size: 14px;
  color: var(--text, #1b1d22);
}

.filters h2 {
  font-size: 15px;
  margin: 0 0 12px;
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  padding: 4px 8px;
  line-height: 1;
}

fieldset {
  border: none;
  padding: 0;
  margin: 0 0 14px;
  /* Fieldsets default to min-inline-size: min-content, which lets a long
     nowrap child (e.g. the "System" list's widest label) stretch the box
     past the 280px sidebar and out of line with its siblings. Pin it to 0
     so every filter box tracks the sidebar width instead. */
  min-width: 0;
}

legend {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 4px;
}

label {
  display: block;
  font-size: 13px;
  margin: 2px 0;
  color: var(--text);
}

input[type="number"] {
  width: 90px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.checkbox-row input[type="checkbox"] {
  margin: 0;
}

/* --- Progress filter dropdown (multi-select checkboxes) --- */
.dropdown-select {
  position: relative;
}

.dropdown-toggle {
  width: 100%;
  text-align: left;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0;
}

.dropdown-caret {
  color: var(--text-muted);
  font-size: 11px;
}

.dropdown-panel {
  position: absolute;
  z-index: 5;
  top: calc(100% + 4px);
  left: 0;
  width: 100%;
  box-sizing: border-box;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-bg);
  padding: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.dropdown-panel .checkbox-row {
  font-size: 13px;
  padding: 2px 0;
}

/* --- Destructive action button (Reset Progress) - visually distinct from
   the routine Clear Filters button so it isn't clicked by reflex. --- */
.danger-btn {
  width: 100%;
  color: var(--incorrect-text);
  border-color: var(--incorrect-text);
}

.danger-btn:hover {
  background: var(--incorrect-bg);
}

.reset-progress-warning {
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
}

button {
  font-size: 13px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
  margin-right: 6px;
  margin-bottom: 6px;
}

button.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

#clear-filters {
  width: 100%;
  margin-top: 6px;
}

.tool-buttons {
  margin-top: 10px;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}

.tool-buttons button {
  width: 100%;
}

/* --- Exam filter checkbox tree (NBME/CMS -> discipline -> form) ---
   A bordered box matching .chip-list, with nested checkbox rows. */
.exam-tree {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  max-height: 220px;
  overflow-y: auto;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px;
  background: var(--bg);
  scrollbar-width: thin;
}

.tree-row {
  display: flex;
  align-items: center;
  gap: 2px;
}

/* Fold caret: a clickable ▸ that rotates to ▾ when the group is expanded.
   .collapsed hides the group's children. Leaves use an invisible spacer of
   the same width so their checkboxes line up under their siblings'. */
.tree-caret {
  flex-shrink: 0;
  width: 14px;
  text-align: center;
  font-size: 10px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
  transition: transform 0.12s ease;
  transform: rotate(90deg);
}

.tree-node.collapsed > .tree-row > .tree-caret {
  transform: rotate(0deg);
}

.tree-caret-spacer {
  cursor: default;
  visibility: hidden;
}

.tree-node.collapsed > .tree-children {
  display: none;
}

.tree-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
  padding: 2px 0;
  margin: 0;
}

.tree-label input[type="checkbox"] {
  flex-shrink: 0;
  margin: 0;
  cursor: pointer;
}

/* Group name (NBME / CMS / discipline) - clicking it folds/unfolds the group.
   Bold to distinguish a foldable group from a selectable leaf form. */
.tree-group-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  padding: 2px 0;
}

.tree-children {
  margin-left: 16px;
}

.tree-node > .tree-children .tree-label {
  font-weight: 400;
}

/* --- System filter tree (System -> nested canonical Topics) ---
   Each System is a foldable row; its Topics render indented underneath.
   Selection highlights via .active rather than checkboxes, since only one
   System (or one Topic within a System) can be filtered at a time. */
.system-tree {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  max-height: 420px;
  overflow-y: auto;
  overflow-x: hidden;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px;
  background: var(--bg);
  scrollbar-width: thin;
}

.system-node.collapsed > .system-topics {
  display: none;
}

.system-row {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 4px;
  border-radius: 4px;
  cursor: pointer;
}

.system-row:hover {
  background: var(--bg-subtle);
}

.system-row.active {
  background: var(--chip-active-bg);
  color: #fff;
}

.system-caret {
  flex-shrink: 0;
  width: 14px;
  text-align: center;
  font-size: 10px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
  transition: transform 0.12s ease;
  transform: rotate(90deg);
}

.system-node.collapsed .system-caret {
  transform: rotate(0deg);
}

.system-row.active .system-caret {
  color: #fff;
}

.system-name {
  font-size: 13px;
  font-weight: 600;
  white-space: normal;
  word-break: break-word;
  flex: 1;
}

.system-topics {
  margin: 2px 0 6px 18px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.topic-row {
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  white-space: normal;
  word-break: break-word;
}

.topic-row:hover {
  background: var(--bg-subtle);
}

.topic-row.active {
  background: var(--chip-active-bg);
  color: #fff;
}

/* A leaf row that's just a checkbox + label (Subject/discipline rows, and
   Topic rows within the System tree) - reuses .system-row/.topic-row's box
   model but as a <label> wrapping the checkbox so clicking the text also
   toggles it. */
.checkbox-leaf-row {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.checkbox-leaf-row input[type="checkbox"] {
  flex-shrink: 0;
  cursor: pointer;
}

main {
  flex: 1;
  padding: 20px 28px;
  max-width: 900px;
}

.browse-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

#result-summary {
  color: var(--text-muted);
  font-size: 13px;
}

#browse-shuffle {
  flex-shrink: 0;
  margin: 0;
}

#browse-shuffle.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.question-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
  margin-bottom: 12px;
  background: var(--bg);
}

.question-card .meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.meta-actions {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 6px;
}

.q-reset {
  flex-shrink: 0;
  font-size: 11px;
  padding: 2px 8px;
  margin: 0;
  color: var(--text-muted);
}

.q-flag {
  flex-shrink: 0;
  font-size: 12px;
  padding: 2px 6px;
  margin: 0;
  line-height: 1;
  opacity: 0.35;
  filter: grayscale(1);
  background: transparent;
  border: 1px solid transparent;
}

.q-flag.active {
  opacity: 1;
  filter: none;
  border-color: var(--border);
}

.q-note-toggle {
  flex-shrink: 0;
  font-size: 13px;
  padding: 2px 6px;
  margin: 0;
  line-height: 1;
  opacity: 0.75;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
}

.q-note-toggle.has-note {
  opacity: 1;
  background: color-mix(in srgb, var(--accent) 15%, transparent);
  border-color: var(--accent);
}

.q-note-wrap {
  margin-bottom: 8px;
}

.q-note-text {
  width: 100%;
  min-height: 60px;
  font: inherit;
  font-size: 13px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-subtle);
  color: var(--text);
  resize: vertical;
}

#quiz-question .meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.pair-banner {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 8px 10px;
  margin-bottom: 10px;
}

.taxonomy-line {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
  display: none;
}

.taxonomy-line.revealed {
  display: block;
}

.question-card .stem,
#quiz-question .stem {
  font-size: 14px;
  margin-bottom: 8px;
  white-space: pre-wrap;
}

.question-card .choices {
  font-size: 13px;
  margin-bottom: 10px;
}

.choice-row {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  margin: 10px 0;
}

.question-card .choice,
#quiz-question .choice {
  cursor: pointer;
  border-radius: 4px;
  padding: 2px 4px;
  flex: 1;
}

.choice.selected {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-radius: 4px;
}

.choice.correct {
  font-weight: 600;
  color: var(--correct-text);
  background: var(--correct-bg);
}

.choice.incorrect {
  font-weight: 600;
  color: var(--incorrect-text);
  background: var(--incorrect-bg);
}

.choice.struck {
  text-decoration: line-through;
  color: var(--text-muted);
  opacity: 0.6;
}

.strike-toggle {
  flex-shrink: 0;
  border: 1px solid var(--border);
  background: var(--bg-subtle);
  color: var(--text-muted);
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 2px 0 0;
  outline: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.strike-toggle:focus,
.strike-toggle:focus-visible {
  outline: none;
  box-shadow: none;
}

.strike-toggle.active {
  background: var(--text-muted);
  color: var(--bg);
  border-color: var(--text-muted);
}

.figures {
  margin: 8px 0 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.q-figure {
  max-width: 100%;
  max-height: 420px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  cursor: zoom-in;
}

.q-figure.zoomed {
  max-height: none;
  max-width: none;
  cursor: zoom-out;
}

.question-card .explanation,
#quiz-question .explanation {
  font-size: 13px;
  background: var(--bg-subtle);
  padding: 8px 10px;
  border-radius: 6px;
  display: none;
  white-space: pre-wrap;
  line-height: 1.6;
}

.hidden { display: none; }
/* .side-panel/.calculator set display themselves, which (being later in the
   file and equal specificity) would otherwise beat plain .hidden and keep the
   panel visible after clicking close - these combined selectors outrank it. */
.side-panel.hidden,
.calculator.hidden,
.panel-backdrop.hidden { display: none; }

mark.user-hl {
  background: #ffe28a;
  color: #1b1d22;
  border-radius: 2px;
  padding: 0 1px;
  cursor: pointer;
}

.stem, .choices, .explanation {
  user-select: text;
}

#quiz-question .stem { font-size: 16px; margin-bottom: 14px; }
#quiz-question .choice { margin: 0; font-size: 14px; }
.quiz-controls { margin-top: 14px; }

.quiz-progress-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.quiz-timer {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 15px;
  padding: 2px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  color: var(--text);
  flex-shrink: 0;
}

.quiz-timer.overtime {
  color: var(--incorrect-text);
  border-color: var(--incorrect-text);
}

.test-timer-badge {
  font-size: 13px;
  opacity: 0.8;
}

/* --- Side panel (Lab Values) --- */
.panel-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 40;
}

.side-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(520px, 90vw);
  background: var(--panel-bg);
  color: var(--text);
  border-left: 1px solid var(--border);
  box-shadow: -4px 0 16px rgba(0, 0, 0, 0.15);
  z-index: 50;
  display: flex;
  flex-direction: column;
}

.side-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.side-panel-head h3 {
  margin: 0;
  font-size: 15px;
}

.side-panel-body {
  overflow-y: auto;
  padding: 12px 16px 24px;
}

.lab-section-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  text-transform: uppercase;
  margin: 16px 0 4px;
}

.lab-subsection-title {
  font-size: 12.5px;
  font-weight: 700;
  margin: 10px 0 2px;
}

table.lab-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
  margin-bottom: 4px;
}

table.lab-table td {
  padding: 3px 6px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

table.lab-table td:first-child {
  color: var(--text-muted);
  width: 46%;
}

/* --- Calculator --- */
.calculator {
  position: fixed;
  top: 70px;
  right: 24px;
  width: 240px;
  background: var(--panel-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  z-index: 60;
}

.calc-display {
  margin: 0 12px 10px;
  padding: 10px;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 6px;
  text-align: right;
  font-size: 22px;
  font-family: "SF Mono", Menlo, monospace;
  overflow-x: auto;
  white-space: nowrap;
}

.calc-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 0 12px 12px;
}

.calc-grid button {
  margin: 0;
  padding: 12px 0;
  font-size: 15px;
  text-align: center;
}

.calc-grid button.op {
  background: var(--bg-subtle);
  font-weight: 600;
}

.calc-grid button.equals {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.calc-grid button.wide {
  grid-column: span 2;
}

/* --- My Tests --- */
.test-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
  margin-bottom: 12px;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.test-card-info h3 {
  margin: 0 0 4px;
  font-size: 15px;
}

.test-card-meta {
  font-size: 12px;
  color: var(--text-muted);
}

.test-card-actions {
  flex-shrink: 0;
  display: flex;
  gap: 6px;
}

.test-card-delete {
  color: var(--incorrect-text);
}

.empty-state {
  color: var(--text-muted);
  font-size: 13px;
  padding: 20px 0;
}

/* --- Create Test panel --- */
#create-test-panel .side-panel-body label {
  margin-top: 12px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-muted);
}

#create-test-panel .side-panel-body label:first-child {
  margin-top: 0;
}

#create-test-name,
#create-test-count {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  margin-top: 4px;
}

.create-test-pool-info {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 6px;
}

#create-test-panel .checkbox-row {
  margin-top: 14px;
}

#create-test-submit {
  width: 100%;
  margin-top: 16px;
}

/* --- Create Step 2 Ratio Test panel --- */
.side-panel-wide {
  width: min(640px, 92vw);
}

.ratio-test-intro {
  font-size: 12.5px;
  color: var(--text-muted);
  line-height: 1.5;
  margin: 0 0 14px;
}

.ratio-test-actions {
  margin-top: 14px;
  display: flex;
  gap: 6px;
}

.ratio-test-actions button {
  flex: 1;
  margin: 0;
}

.ratio-test-preview {
  margin-top: 18px;
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

.ratio-test-preview h4 {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text-muted);
  margin: 14px 0 6px;
}

.ratio-test-preview h4:first-of-type {
  margin-top: 0;
}

.ratio-test-warnings {
  font-size: 12.5px;
  color: var(--incorrect-text);
}

.ratio-test-warnings:empty {
  display: none;
}

.ratio-test-warnings div {
  background: var(--incorrect-bg);
  border-radius: 6px;
  padding: 6px 8px;
  margin-bottom: 6px;
}

table.ratio-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
  margin-bottom: 6px;
}

table.ratio-table th {
  text-align: left;
  color: var(--text-muted);
  font-weight: 600;
  padding: 4px 6px;
  border-bottom: 1px solid var(--border);
}

table.ratio-table td {
  padding: 4px 6px;
  border-bottom: 1px solid var(--border);
}

table.ratio-table tr.shortfall td {
  color: var(--incorrect-text);
}

#ratio-test-start {
  width: 100%;
  margin-top: 16px;
}

/* --- Test Results / Performance --- */
.results-score {
  font-size: 28px;
  font-weight: 700;
  margin: 4px 0 20px;
}

.results-score .results-score-sub {
  font-size: 14px;
  font-weight: 400;
  color: var(--text-muted);
  margin-left: 8px;
}

.stats-section h3 {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text-muted);
  margin: 20px 0 8px;
}

.stats-section h3:first-of-type {
  margin-top: 0;
}

table.ratio-table td.pct-bar-cell {
  position: relative;
  min-width: 140px;
}

.pct-bar-track {
  background: var(--bg-subtle);
  border-radius: 4px;
  height: 14px;
  width: 100%;
  overflow: hidden;
}

.pct-bar-fill {
  height: 100%;
  background: var(--accent);
}

.pct-bar-fill.low { background: var(--incorrect-text); }
.pct-bar-fill.mid { background: #d9a441; }
.pct-bar-fill.high { background: var(--correct-text); }

.pct-label {
  font-size: 11.5px;
  color: var(--text-muted);
  margin-top: 2px;
  display: block;
}

.no-data-row td {
  color: var(--text-muted);
  font-style: italic;
}

.results-actions {
  margin-top: 24px;
  display: flex;
  gap: 8px;
}
