:root {
  --ink: #1e293b;
  --ink-soft: #52606d;
  --line: #d8dadf;
  --line-soft: #ececef;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --bg: #f8fafc;
  --paper: #ffffff;
  --danger: #c0392b;
  --radius: 10px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Noto Sans KR', 'Malgun Gothic', sans-serif;
  font-weight: 500;
}

button {
  font-family: inherit;
}

/* ---------- 사이트 상단 내비게이션 (문제지 생성 페이지) ---------- */

.site-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  border-bottom: 1px solid var(--line-soft);
  background: var(--paper);
}

.site-logo {
  font-size: 18px;
  font-weight: 900;
  color: var(--ink);
  text-decoration: none;
}

.site-logo span {
  color: var(--accent);
}

.site-home-link {
  font-size: 14px;
  font-weight: 700;
  color: var(--ink-soft);
  text-decoration: none;
}

.site-home-link:hover {
  color: var(--accent);
}

/* ---------- 선택 화면 ---------- */

.selection-screen {
  max-width: 720px;
  margin: 0 auto;
  padding: 32px 20px 80px;
}

.app-header {
  text-align: center;
  margin-bottom: 28px;
}

.app-header h1 {
  font-size: 28px;
  font-weight: 900;
  margin: 0 0 6px;
}

.app-tagline {
  margin: 0;
  color: var(--ink-soft);
  font-size: 15px;
}

.step {
  background: var(--paper);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 14px;
}

.step h2 {
  font-size: 15px;
  font-weight: 700;
  margin: 0 0 12px;
  color: var(--ink);
}

.badge {
  display: inline-block;
  margin-left: 6px;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 999px;
  background: #f0f0f2;
  color: #9a9ea5;
  vertical-align: middle;
}

/* ---------- 학년 탭 ---------- */

.grade-tabs-row {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  margin-bottom: 10px;
  padding: 0 2px;
}

.grade-tab-compact {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 4px 2px 8px;
  font-size: 17px;
  font-weight: 700;
  color: var(--ink-soft);
  cursor: pointer;
}

.grade-tab-compact.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.grade-tab-compact.disabled {
  color: #c2c5cb;
  cursor: not-allowed;
}

/* ---------- 단원 / 세부 유형 드롭다운 ---------- */

.dropdown-row {
  display: flex;
  gap: 12px;
  margin-bottom: 8px;
}

.dropdown {
  position: relative;
  flex: 1;
  min-width: 0;
}

.dropdown-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
  text-align: left;
}

.dropdown-trigger span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dropdown-trigger:hover {
  border-color: var(--accent);
}

.chevron {
  flex-shrink: 0;
  color: #9a9ea5;
  transition: transform 0.15s ease;
}

.chevron.open {
  transform: rotate(180deg);
}

.dropdown-panel {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  z-index: 20;
  max-height: 320px;
  overflow-y: auto;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.14);
  padding: 8px;
}

.dropdown-empty {
  padding: 14px 10px;
  font-size: 13px;
  font-weight: 700;
  color: #9a9ea5;
}

.dropdown-group {
  padding: 6px 4px;
}

.dropdown-group.disabled {
  opacity: 0.6;
}

.dropdown-group-label {
  font-size: 12px;
  font-weight: 700;
  color: #9a9ea5;
  padding: 4px 8px;
}

.dropdown-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 10px;
  border: none;
  border-radius: 6px;
  background: none;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
}

.dropdown-item:hover {
  background: #f4f5f7;
}

.dropdown-item.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.dropdown-item.disabled {
  color: #c2c5cb;
  cursor: not-allowed;
}

.dropdown-item.disabled:hover {
  background: none;
}

.dropdown-item-divider {
  margin-bottom: 6px;
  padding-bottom: 15px;
  border-bottom: 1px solid var(--line-soft);
}

.current-selection-summary {
  margin: 0 0 14px;
  padding: 0 2px;
  font-size: 13px;
  font-weight: 700;
  color: var(--ink-soft);
  min-height: 1em;
}

@media screen and (max-width: 640px) {
  .dropdown-row {
    flex-direction: column;
  }
}

.answer-mode-radios {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.answer-mode-radio {
  flex: 1;
  min-width: 140px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 10px;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  font-size: 15px;
  font-weight: 700;
}

.answer-mode-radio.checked {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

.answer-mode-radio input {
  accent-color: var(--accent);
}

.sheet-count-control {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stepper-btn {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  border: 1.5px solid var(--line);
  background: var(--paper);
  font-size: 20px;
  font-weight: 700;
  cursor: pointer;
  color: var(--ink);
}

.stepper-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.sheet-count-input {
  width: 64px;
  height: 44px;
  text-align: center;
  font-size: 18px;
  font-weight: 700;
  border: 1.5px solid var(--line);
  border-radius: 8px;
}

.sheet-count-unit {
  font-size: 15px;
  color: var(--ink-soft);
  font-weight: 700;
}

.page-number-toggle label {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}

.page-number-toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.generate-bar {
  position: sticky;
  bottom: 14px;
  margin-top: 22px;
  text-align: center;
}

.error-message {
  color: var(--danger);
  font-weight: 700;
  font-size: 14px;
  margin: 0 0 10px;
}

.generate-button {
  width: 100%;
  padding: 16px;
  font-size: 18px;
  font-weight: 900;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 10px;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(29, 95, 214, 0.28);
}

.generate-button:hover {
  background: #164ec2;
}

/* ---------- 미리보기 화면 / 툴바 ---------- */

.preview-screen {
  min-height: 100vh;
}

.preview-toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

.toolbar-spacer {
  flex: 1;
}

.toolbar-btn {
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 700;
  border-radius: 8px;
  border: 1.5px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
}

.toolbar-btn.secondary {
  color: var(--ink-soft);
}

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

.toolbar-page-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink-soft);
}

#print-root {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  padding: 24px 0 60px;
}

/* ---------- A4 페이지(문제지/정답지) 공통 레이아웃 ---------- */
/* 화면과 인쇄물에서 동일한 크기/모양을 유지하기 위해 mm 단위로 고정한다. */

.a4-page {
  width: 210mm;
  height: 297mm;
  padding: 13mm 12mm 10mm;
  background: var(--paper);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
  display: flex;
  flex-direction: column;
}

.page-header {
  position: relative;
  text-align: center;
  padding-bottom: 4mm;
  margin-bottom: 4mm;
  border-bottom: 2px solid var(--ink);
}

.header-topline {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 1mm;
}

.qr-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.8mm;
}

.qr-block svg {
  display: block;
  width: 14mm;
  height: 14mm;
}

.qr-caption {
  font-size: 8px;
  font-weight: 700;
  color: #9a9ea5;
  white-space: nowrap;
}

.worksheet-title {
  margin: 0 0 2mm;
  font-size: 21px;
  font-weight: 900;
}

.name-line {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-size: 13px;
  font-weight: 700;
  padding-top: 2mm;
}

.name-blank {
  display: inline-block;
  width: 34mm;
  border-bottom: 1.4px solid var(--ink);
  height: 1em;
}

.problem-columns {
  flex: 1;
  display: flex;
  gap: 10mm;
  min-height: 0;
}

.problem-column {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.problem {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 1.5mm 0;
  border-bottom: 1px dashed var(--line);
  min-height: 0;
  overflow: hidden;
}

.problem-column .problem:last-child {
  border-bottom: none;
}

.problem-body {
  display: flex;
  align-items: baseline;
  gap: 7px;
}

.problem-number {
  font-size: 17px;
  font-weight: 900;
  flex-shrink: 0;
}

.equation {
  font-size: 19px;
}

.equation .katex {
  font-size: 1.12em;
}

.work-space {
  flex: 1;
}

.answer-line {
  display: flex;
  align-items: baseline;
  justify-content: flex-end;
  gap: 8px;
  padding-right: 2mm;
}

.answer-x {
  font-size: 16px;
}

.answer-blank {
  display: inline-block;
  width: 26mm;
  border-bottom: 1.4px solid var(--ink);
  height: 1em;
}

.options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5mm 3mm;
  margin-top: 2mm;
  padding-left: 1mm;
}

.option {
  display: flex;
  align-items: baseline;
  gap: 5px;
  font-size: 14px;
  min-width: 0;
}

.option .katex {
  font-size: 0.95em;
}

.option-mark {
  font-weight: 700;
  flex-shrink: 0;
}

.page-footer {
  text-align: center;
  padding-top: 1.5mm;
}

.page-number {
  font-size: 11px;
  color: #9a9ea5;
  font-weight: 700;
}

/* ---------- 정답지 ---------- */

.answer-page-header {
  border-bottom: 2px solid var(--ink);
}

.answer-blocks {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 8mm;
  min-height: 0;
}

.answer-block {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5mm;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.answer-block-title {
  font-size: 14px;
  font-weight: 900;
  margin: 0 0 3mm;
  padding-bottom: 2mm;
  border-bottom: 1px solid var(--line);
}

.answer-grid {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: min-content;
  gap: 2mm 4mm;
  align-content: start;
}

.answer-entry {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-size: 14px;
}

.answer-number {
  font-weight: 700;
  color: var(--ink-soft);
  flex-shrink: 0;
}

.answer-value .katex {
  font-size: 1em;
}

@media screen and (max-width: 900px) {
  .a4-page {
    width: 100%;
    height: auto;
    min-height: unset;
    box-shadow: none;
    border: 1px solid var(--line);
  }
  .problem-columns {
    flex-direction: column;
  }
  .answer-blocks {
    grid-template-columns: 1fr;
    grid-template-rows: none;
  }
}
