:root {
  color-scheme: light;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  background: #f4f8f7;
  color: #132329;
  --bg: #f4f8f7;
  --panel: #ffffff;
  --panel-tint: #eef8f6;
  --border: #cfe2df;
  --border-strong: #64b8c7;
  --text: #132329;
  --muted: #5c7280;
  --muted-strong: #2f4853;
  --blue: #126f9f;
  --blue-dark: #0b587f;
  --green: #16835f;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(135deg, rgba(22, 131, 95, 0.12), transparent 34rem),
    linear-gradient(315deg, rgba(18, 111, 159, 0.1), transparent 32rem),
    var(--bg);
  color: var(--text);
}

.shell {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 32px 0;
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) minmax(320px, 2fr);
  gap: 24px;
  align-items: end;
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 18px 42px rgba(19, 35, 41, 0.08);
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(2rem, 4vw, 3.4rem);
  line-height: 1;
  color: var(--text);
}

.toolbar h1 {
  width: fit-content;
  background: linear-gradient(90deg, var(--green), var(--blue));
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}

.controls {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 12px;
  align-items: end;
}

label,
.field {
  display: grid;
  gap: 6px;
  color: var(--muted-strong);
  font-size: 0.9rem;
  font-weight: 700;
}

select,
input,
button {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
}

select,
input {
  padding: 8px 10px;
  background: #ffffff;
  color: var(--text);
}

select:focus,
input:focus,
button:focus-visible {
  outline: 2px solid rgba(18, 111, 159, 0.24);
  outline-offset: 2px;
}

.topic-field {
  position: relative;
}

.field-label {
  line-height: 1.2;
}

.select-button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 10px;
  border-color: var(--border);
  background: #ffffff;
  color: var(--text);
  font-weight: 400;
  text-align: left;
  box-shadow: none;
}

.select-button:hover {
  border-color: var(--border-strong);
  background: #ffffff;
  transform: none;
}

.select-button span:first-child {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topic-menu {
  position: absolute;
  z-index: 20;
  top: calc(100% + 6px);
  left: 0;
  width: 100%;
  max-height: 220px;
  overflow-y: auto;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #ffffff;
  box-shadow: 0 18px 42px rgba(19, 35, 41, 0.16);
}

.topic-option {
  display: flex;
  grid-template-columns: none;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  padding: 5px 4px;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
}

.topic-option input {
  width: 16px;
  min-height: 16px;
  margin: 0;
  accent-color: var(--blue);
}

.empty-options {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.count-control {
  position: relative;
  display: block;
}

.count-control input {
  appearance: textfield;
  padding-right: 92px;
}

.count-control input::-webkit-inner-spin-button,
.count-control input::-webkit-outer-spin-button {
  margin: 0;
  appearance: none;
}

.count-unit {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
  pointer-events: none;
  white-space: nowrap;
}

button {
  border-color: var(--blue);
  background: var(--blue);
  color: #ffffff;
  font-weight: 800;
  cursor: pointer;
  transition:
    background 160ms ease,
    border-color 160ms ease,
    transform 160ms ease;
}

button:hover {
  border-color: var(--blue-dark);
  background: var(--blue-dark);
  transform: translateY(-1px);
}

.secondary-button {
  border-color: var(--border-strong);
  background: #ffffff;
  color: var(--blue-dark);
}

.secondary-button:hover {
  background: var(--panel-tint);
}

button:disabled {
  border-color: #9eb4bd;
  background: #9eb4bd;
  color: #ffffff;
  cursor: wait;
  transform: none;
}

.status-row {
  min-height: 54px;
  display: flex;
  align-items: center;
  color: var(--muted);
}

.status-row p {
  margin: 0;
}

.worksheet-output {
  display: grid;
  justify-items: center;
}

.worksheet-page {
  width: min(8.5in, 100%);
  min-height: 11in;
  padding: 0.65in;
  border: 1px solid #d2d8df;
  background: #ffffff;
  box-shadow: 0 18px 40px rgba(19, 35, 41, 0.12);
  color: #111827;
}

.worksheet-header {
  border-bottom: 2px solid #111827;
  padding-bottom: 14px;
  margin-bottom: 24px;
}

.worksheet-header h2,
.answer-key-toggle {
  margin: 0;
  color: #111827;
}

.worksheet-header h2 {
  font-size: 1.8rem;
  line-height: 1.15;
}

.worksheet-header p {
  margin: 8px 0 0;
  color: #4b5563;
  font-size: 0.92rem;
}

.problem-list,
.answer-key ol {
  margin: 0;
  padding-left: 28px;
}

.problem-list {
  display: grid;
  gap: 22px;
  font-size: 1rem;
  line-height: 1.55;
}

.problem-list li {
  padding-left: 6px;
}

.math-fraction {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 1.4em;
  margin: 0 0.14em;
  line-height: 1.05;
  vertical-align: middle;
}

.math-fraction-numerator,
.math-fraction-denominator {
  display: block;
  padding: 0 0.18em;
  text-align: center;
}

.math-fraction-numerator {
  border-bottom: 1px solid currentColor;
}

.answer-key {
  margin-top: 42px;
  padding-top: 18px;
  border-top: 1px solid #cfd7df;
}

.answer-key-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
  width: auto;
  min-height: 48px;
  padding: 0;
  border: 0;
  background: transparent;
  color: #111827;
  font-size: 1.15rem;
  font-weight: 800;
  text-align: left;
  box-shadow: none;
}

.answer-key-toggle:hover {
  background: transparent;
  color: var(--blue-dark);
  transform: none;
}

.answer-key-icon {
  display: inline-grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: 2px solid var(--blue);
  border-radius: 999px;
  color: var(--blue-dark);
  font-size: 1.25rem;
  font-weight: 900;
  transition: transform 160ms ease;
}

.answer-key-toggle[aria-expanded="true"] .answer-key-icon {
  transform: rotate(180deg);
}

.answer-key-content {
  display: grid;
  gap: 8px;
  margin-top: 14px;
}

.answer-key-content[hidden] {
  display: none;
}

.answer-key ol {
  display: grid;
  gap: 8px;
  color: #263445;
  line-height: 1.45;
}

.print-footer {
  display: none;
}

@media (max-width: 860px) {
  .toolbar,
  .controls {
    grid-template-columns: 1fr;
  }

  .toolbar {
    align-items: stretch;
  }
}

@media print {
  @page {
    size: letter;
    margin: 0;
  }

  body {
    background: #ffffff;
  }

  .shell {
    width: 100%;
    padding: 0;
  }

  .toolbar,
  .status-row {
    display: none;
  }

  .worksheet-page {
    position: relative;
    width: 100%;
    min-height: auto;
    padding: 0.5in 0.5in 0.75in;
    border: 0;
    box-shadow: none;
  }

  .answer-key {
    break-before: page;
    page-break-before: always;
    margin-top: 0;
    padding-top: 0;
    border-top: 0;
  }

  .answer-key-toggle {
    min-height: auto;
    margin-bottom: 14px;
    font-size: 1.15rem;
    pointer-events: none;
  }

  .answer-key-icon {
    display: none;
  }

  .answer-key-content,
  .answer-key-content[hidden] {
    display: grid;
  }

  .print-footer {
    position: fixed;
    right: 0.5in;
    bottom: 0.25in;
    left: 0.5in;
    display: block;
    color: #4b5563;
    font-size: 0.78rem;
    text-align: center;
  }
}
