:root {
  color-scheme: light;
  --brand: #4f46e5;
  --brand-strong: #4338ca;
  --brand-soft: #eef2ff;
  --canvas: #eef1f6;
  --surface: #ffffff;
  --text: #172033;
  --muted: #64748b;
  --border: #dce2ea;
  --danger: #b91c1c;
  --warning: #b45309;
  --radius: 12px;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  color: var(--text);
  background: var(--canvas);
}

* {
  box-sizing: border-box;
}

html {
  min-width: 0;
  background: var(--canvas);
}

body {
  min-width: 0;
  margin: 0;
  background: var(--canvas);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button,
input,
select {
  font: inherit;
}

button,
select,
input[type="checkbox"] {
  cursor: pointer;
}

button:disabled,
input:disabled {
  cursor: not-allowed;
}

:focus-visible {
  outline: 3px solid #312e81;
  outline-offset: 3px;
}

.app-bar {
  position: sticky;
  z-index: 20;
  top: 0;
  display: flex;
  min-height: 68px;
  align-items: center;
  gap: 18px;
  padding: 10px 24px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.97);
}

.brand {
  flex: 0 0 auto;
  color: var(--brand);
  font-size: 20px;
  font-weight: 800;
  letter-spacing: -0.02em;
  text-decoration: none;
}

.app-heading {
  min-width: 0;
}

.app-heading h1,
.app-heading p,
#selectionSummary {
  margin: 0;
}

.app-heading h1 {
  font-size: 18px;
  line-height: 1.3;
}

.app-heading p {
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#selectionSummary {
  margin-left: auto;
  color: #475569;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

.button,
.drawer-toggle {
  min-height: 40px;
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 700;
}

.button-primary {
  border-color: var(--brand);
  background: var(--brand);
  color: #fff;
}

.button-primary:hover:not(:disabled) {
  border-color: var(--brand-strong);
  background: var(--brand-strong);
}

.button-primary:disabled {
  border-color: #a5b4fc;
  background: #a5b4fc;
}

.button-secondary,
.drawer-toggle {
  border-color: #c7d2fe;
  background: var(--surface);
  color: var(--brand-strong);
}

.ticket-view {
  display: grid;
  min-height: calc(100vh - 68px);
  place-items: start center;
  padding: clamp(48px, 9vh, 96px) 20px;
}

.ticket-card {
  width: min(100%, 520px);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: clamp(24px, 5vw, 42px);
  background: var(--surface);
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.08);
}

.ticket-card h2 {
  margin: 0 0 8px;
  font-size: clamp(24px, 4vw, 30px);
  line-height: 1.25;
  letter-spacing: -0.025em;
}

.ticket-help,
.field-help {
  color: var(--muted);
}

.ticket-help {
  margin: 0 0 26px;
}

.ticket-code-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 700;
}

.ticket-entry-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
}

.ticket-code-slots {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr)) auto repeat(4, minmax(0, 1fr));
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.ticket-code-slot {
  width: 100%;
  min-width: 0;
  height: 52px;
  border: 1px solid #b8c2d1;
  border-radius: 10px;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 24px;
  font-weight: 700;
  text-align: center;
  text-transform: uppercase;
}

.ticket-code-slot:hover {
  border-color: #8f9bad;
  background: #f8faff;
}

.ticket-code-slot:focus {
  border-color: var(--brand);
  outline: 2px solid var(--brand-strong);
  outline-offset: 2px;
  box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.13);
}

.ticket-code-divider {
  color: var(--muted);
  font-size: 24px;
  font-weight: 800;
}

.field-help {
  margin: 8px 0 0;
  font-size: 13px;
}

.ticket-status {
  min-height: 24px;
  margin: 16px 0 0;
  font-weight: 600;
}

.ticket-status[data-type="error"] {
  color: var(--danger);
}

.ticket-status[data-type="loading"] {
  color: var(--brand-strong);
}

.ticket-actions {
  display: flex;
  gap: 16px;
}

.text-button {
  border: 0;
  padding: 4px 0;
  background: transparent;
  color: var(--brand-strong);
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.workbench {
  min-width: 0;
  min-height: calc(100vh - 68px);
  padding: 16px;
}

.drawer-bar {
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
}

.drawer-toggle {
  flex: 1 1 0;
}

.drawer-backdrop {
  position: fixed;
  z-index: 35;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  padding: 0;
  background: rgba(15, 23, 42, 0.42);
}

.drawer-shell {
  position: fixed;
  z-index: 40;
  top: 0;
  bottom: 0;
  display: none;
  width: min(360px, calc(100vw - 32px));
  grid-template-rows: auto minmax(0, 1fr);
  background: var(--surface);
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.24);
}

.drawer-shell.is-open {
  display: grid;
}

.filter-drawer {
  left: 0;
}

.question-drawer {
  right: 0;
}

.drawer-header {
  display: flex;
  min-height: 58px;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}

.drawer-header h2 {
  margin: 0;
  font-size: 16px;
}

.drawer-header button {
  min-height: 38px;
  border: 1px solid #c7d2fe;
  border-radius: 8px;
  padding: 6px 12px;
  background: #fff;
  color: var(--brand-strong);
  font-weight: 700;
}

.side-panel,
.paper-preview {
  min-width: 0;
}

.side-panel {
  display: block;
  align-self: start;
  max-height: none;
  height: 100%;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}

.filter-controls,
.paper-controls,
.question-list {
  padding: 18px;
}

.paper-controls {
  border-top: 1px solid var(--border);
}

.filter-controls h2,
.paper-controls h2,
.question-list h2 {
  margin: 0 0 16px;
  font-size: 16px;
}

.filter-controls > label,
.paper-controls > label {
  display: grid;
  gap: 6px;
  margin-top: 14px;
  color: #475569;
  font-size: 13px;
  font-weight: 650;
}

.paper-controls > label:has(input[type="checkbox"]) {
  grid-template-columns: auto 1fr;
  align-items: center;
}

.filter-controls select,
.paper-controls select,
.paper-controls input:not([type="checkbox"]) {
  width: 100%;
  min-width: 0;
  min-height: 40px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  padding: 7px 9px;
  background: #fff;
  color: var(--text);
  font-size: 14px;
}

.paper-preview {
  overflow: hidden;
  padding: clamp(12px, 3vw, 30px);
  border: 1px solid #d9dee7;
  border-radius: var(--radius);
  background: #dfe4ec;
}

.paper {
  width: min(100%, 210mm);
  aspect-ratio: 210 / 297;
  margin: 0 auto 24px;
  overflow: hidden;
  overflow-wrap: anywhere;
  border: 1px solid #d6dae1;
  padding: clamp(22px, 5vw, 58px);
  background: #fff;
  box-shadow: 0 12px 30px rgba(15, 23, 42, 0.13);
  color: #111827;
}

.paper-page {
  position: relative;
  break-after: page;
}

.paper-page:last-child {
  break-after: auto;
}

.paper-page-number {
  position: absolute;
  right: 0;
  bottom: 18px;
  left: 0;
  color: #64748b;
  font-size: 12px;
  text-align: center;
}

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

.paper-header h1 {
  margin: 0 0 14px;
  font-family: "Noto Serif SC", "Songti SC", SimSun, serif;
  font-size: clamp(22px, 3vw, 30px);
}

.identity-fields {
  margin: 0;
  font-size: 14px;
}

.paper-questions {
  min-width: 0;
}

.paper-columns {
  display: grid;
  min-width: 0;
  grid-template-columns: minmax(0, 1fr);
  gap: 28px;
}

.paper-columns-2 .paper-columns {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.paper-column {
  min-width: 0;
}

.paper-question,
.choice-options {
  break-inside: avoid;
}

.paper-question {
  margin: 0 0 22px;
}

.paper-question h3 {
  margin: 0 0 10px;
  font-size: 15px;
  font-weight: 500;
  line-height: 1.75;
}

.question-number {
  font-weight: 800;
}

.choice-options {
  display: grid;
  gap: 7px;
  margin: 0;
  padding-left: 24px;
  list-style: none;
}

.option-label {
  font-weight: 700;
}

.answer-blank {
  display: inline-block;
  width: 5.5em;
  max-width: 100%;
  margin: 0 0.25em;
  border-bottom: 1px solid #111827;
}

.paper-density-loose .paper-question {
  margin-bottom: 30px;
}

.paper-density-compact .paper-question {
  margin-bottom: 14px;
}

.paper-answers {
  margin-top: 30px;
  border-top: 1px solid #94a3b8;
  padding-top: 18px;
}

.paper-answers h2 {
  font-size: 18px;
}

.paper-answer {
  overflow-wrap: anywhere;
}

.question-list h2 {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.question-list h2 span {
  color: var(--brand-strong);
  font-size: 13px;
}

.question-list > button {
  border: 0;
  padding: 5px 0;
  background: transparent;
  color: var(--brand-strong);
  font-size: 13px;
  font-weight: 700;
}

.group-select {
  float: right;
  width: 18px;
  height: 18px;
  margin: 7px 0 10px 10px;
  accent-color: var(--brand);
}

.question-list-item {
  clear: both;
  min-width: 0;
  margin-top: 12px;
  border-top: 1px solid #edf0f4;
  padding-top: 12px;
}

.question-list-item > label {
  display: grid;
  min-width: 0;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 9px;
  align-items: start;
}

.question-list-item input[type="checkbox"] {
  width: 18px;
  height: 18px;
  margin: 2px 0 0;
  accent-color: var(--brand);
}

.question-list-stem,
.question-list-meta,
.incomplete-reason {
  min-width: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.question-list-stem {
  display: -webkit-box;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  font-size: 14px;
  line-height: 1.55;
}

.question-list-meta,
.incomplete-reason {
  margin: 7px 0 0 27px;
  font-size: 12px;
}

.question-list-meta {
  color: var(--muted);
}

.incomplete-reason {
  color: var(--warning);
  font-weight: 700;
}

.question-list-item.is-incomplete {
  background: #fffbeb;
}

.question-list-item > button {
  margin: 8px 0 0 7px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  background: #fff;
  color: #475569;
  font-size: 12px;
}

.empty-state,
.empty-paper {
  color: var(--muted);
  text-align: center;
}

.print-only {
  display: none;
}

@page {
  size: A4;
  margin: 14mm 13mm 16mm;
}

[hidden] {
  display: none !important;
}

@media (min-width: 1280px) {
  .workbench {
    display: grid;
    grid-template-columns: 260px minmax(560px, 1fr) 300px;
    grid-template-areas: "filter preview question";
    gap: 16px;
    align-items: start;
  }

  .drawer-bar {
    display: none;
  }

  .drawer-shell,
  .drawer-shell.is-open {
    position: static;
    display: contents;
    width: auto;
  }

  .drawer-header,
  .drawer-backdrop {
    display: none !important;
  }

  .side-panel {
    position: sticky;
    top: 84px;
    height: auto;
    max-height: calc(100vh - 100px);
  }

  #filterPanel { grid-area: filter; }
  #questionPanel { grid-area: question; }
  .paper-preview { grid-area: preview; }
}

@media screen and (max-width: 720px) {
  .app-bar {
    position: static;
    flex-wrap: wrap;
    gap: 8px 12px;
    padding: 10px 14px;
  }

  .app-heading {
    flex: 1 1 180px;
  }

  #selectionSummary {
    order: 3;
    width: 100%;
    margin-left: 0;
  }

  .ticket-entry-row {
    grid-template-columns: minmax(0, 1fr);
  }

  .ticket-code-slots {
    grid-template-columns: repeat(4, minmax(0, 1fr)) auto repeat(4, minmax(0, 1fr));
    gap: 4px;
  }

  .ticket-code-slot {
    font-size: 20px;
  }

  .workbench {
    padding: 10px;
  }

  .paper-preview {
    padding: 8px;
  }

  .paper {
    min-height: 0;
    aspect-ratio: auto;
    padding: 18px 14px;
  }

  .paper-columns-2 .paper-columns {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}

@media print {
  .no-print,
  #ticketView,
  #workbench {
    display: none !important;
  }

  #printRoot {
    display: block !important;
  }

  .paper-page {
    width: 184mm;
    height: 267mm;
    aspect-ratio: auto;
    border: 0;
    padding: 0;
    padding-bottom: 12mm;
    box-shadow: none;
    break-after: page;
  }

  .paper-page:last-child {
    break-after: auto;
  }

  .question-block,
  .option-list,
  .answer-row {
    break-inside: avoid;
  }

  .section-heading {
    break-after: avoid;
  }
}
