:root {
  --ink: #2c2117;
  --muted: #7b6b5d;
  --paper: #fff8ec;
  --paper-deep: #efe0c4;
  --line: #c2a36f;
  --primary: #683b22;
  --red: #b3261e;
  --stop-red: #ff3b30;
  --gray: #6f6b66;
  --bg: #ead9bd;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: radial-gradient(circle at top left, #fff7e8, var(--bg));
  font-family: 'Noto Serif TC', 'Songti TC', 'PMingLiU', serif;
}

a { color: inherit; text-decoration: none; }
button, input, textarea, select { font: inherit; }

.card {
  background: rgba(255, 248, 236, 0.94);
  border: 1px solid rgba(104, 59, 34, 0.18);
  border-radius: 24px;
  box-shadow: 0 24px 80px rgba(74, 45, 18, 0.15);
  padding: 26px;
}

.home-page { display: grid; place-items: center; }
.home-shell { width: min(1080px, calc(100vw - 32px)); display: grid; grid-template-columns: 1.2fr .8fr; gap: 24px; }
.hero { min-height: 420px; display: flex; flex-direction: column; justify-content: space-between; background: linear-gradient(135deg, rgba(255,255,255,.7), rgba(240,219,181,.75)); }
.eyebrow { margin: 0 0 10px; color: #8b4d2d; letter-spacing: .16em; font-size: .86rem; }
h1, h2, h3 { margin-top: 0; }
.hero h1 { font-size: clamp(2.5rem, 6vw, 4.6rem); line-height: 1.08; }
.lead { font-size: 1.12rem; line-height: 1.9; }

.button {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 10px 18px;
  background: #fffaf2;
  color: var(--ink);
  cursor: pointer;
}
.button.primary { background: var(--primary); border-color: var(--primary); color: #fff8ec; }
.button.ghost { background: rgba(255,255,255,.55); }
.button:disabled { opacity: .45; cursor: not-allowed; }
.button.small { padding: 7px 12px; font-size: .9rem; }
.button.tiny { padding: 5px 10px; font-size: .84rem; }
.button.danger { border-color: var(--red); color: var(--red); background: #fff2ef; }
.actions, .topbar nav, .practice-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.form, .stack-form { display: grid; gap: 14px; }
.form > a.button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  text-decoration: none;
  box-sizing: border-box;
}
label { display: grid; gap: 8px; }
input, textarea, select {
  width: 100%;
  border: 1px solid rgba(104,59,34,.25);
  border-radius: 14px;
  padding: 12px 14px;
  background: #fffdf8;
  color: var(--ink);
  box-sizing: border-box;
}
select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 36px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8' fill='none'%3E%3Cpath d='M1 1.5L6 6.5L11 1.5' stroke='%23683b22' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  background-size: 12px 8px;
  cursor: pointer;
}
textarea { resize: vertical; line-height: 1.75; }
.hint, .muted, .message { color: var(--muted); }
.message { min-height: 1.4em; }

.topbar {
  height: 68px;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 5;
  background: rgba(255,248,236,.86);
  border-bottom: 1px solid rgba(104,59,34,.14);
  backdrop-filter: blur(12px);
}
.brand { font-weight: 800; font-size: 1.2rem; }
.user-label { color: var(--muted); }

.app-shell { height: calc(100vh - 68px); padding: 20px; display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 20px; }
.side-panel, .practice-area { min-height: 0; }
.text-list, .list-box { display: grid; gap: 10px; }
.text-item, .list-row {
  width: 100%;
  border: 1px solid rgba(104,59,34,.14);
  border-radius: 16px;
  background: rgba(255,255,255,.56);
  padding: 14px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  text-align: left;
}
.text-item { cursor: pointer; }
.text-item.active {
  border-color: rgba(104,59,34,.32);
  background: rgba(255,244,223,.82);
  box-shadow: inset 0 0 0 1px rgba(104,59,34,.12);
}
.text-item span, .list-row span { display: block; margin-top: 4px; color: var(--muted); font-size: .9rem; }
.legend { margin-top: 18px; display: grid; gap: 8px; color: var(--muted); }
.dot { display: inline-block; width: .8em; height: .8em; border-radius: 50%; margin-right: 6px; vertical-align: -1px; }
.dot.red { border: 2.5px solid var(--stop-red); }
.dot.gray { border: 2.5px solid var(--gray); }

.practice-area { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; gap: 14px; }
.practice-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 18px; }
.scroll-frame {
  min-height: 0;
  height: 100%;
  overflow: auto;
  border: 1px solid rgba(104,59,34,.22);
  border-radius: 20px;
  background:
    linear-gradient(rgba(255,248,236,.86), rgba(255,248,236,.86)),
    repeating-linear-gradient(to left, transparent 0 48px, rgba(104,59,34,.08) 49px 50px);
  padding: 26px;
}
.reader {
  box-sizing: border-box;
  writing-mode: horizontal-tb;
  height: 100%;
  max-height: 100%;
  min-height: 0;
  width: max-content;
  min-width: 100%;
  display: flex;
  flex-direction: row-reverse;
  justify-content: flex-start;
  align-items: stretch;
  gap: 28px;
  padding: 4px;
}
.empty-state {
  display: grid;
  place-items: center;
  width: 100%;
  min-width: 100%;
  color: var(--muted);
  writing-mode: horizontal-tb;
}
.reader-block {
  writing-mode: vertical-rl;
  text-orientation: upright;
  height: 100%;
  max-height: 100%;
  display: block;
  line-height: 1;
  white-space: normal;
}
.note-pair {
  writing-mode: horizontal-tb;
  display: inline-flex;
  flex-direction: row-reverse;
  justify-content: flex-start;
  align-items: flex-start;
  width: 2.35rem;
  gap: 1px;
  vertical-align: top;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  border-inline: 1px solid rgba(104,59,34,.14);
}
.note-line {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.token {
  position: relative;
  display: inline-grid;
  place-items: center;
  vertical-align: top;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  padding: 0;
}
.token:hover { background: rgba(179,38,30,.08); }
.main-token { width: 2.35rem; height: 2.35rem; font-size: 2.15rem; line-height: 1; }
.note-token { width: 1.12rem; height: 1.12rem; font-size: .92rem; line-height: 1; }
.token::before, .token::after { content: ''; position: absolute; border-radius: 50%; pointer-events: none; opacity: 0; box-sizing: border-box; }
.token.student-stop::after { opacity: 1; width: .5em; height: .5em; border: 2.5px solid var(--stop-red); right: -.02em; bottom: .02em; }
.token.answer-stop::before { opacity: 1; width: .56em; height: .56em; border: 2.5px solid var(--gray); right: -.08em; bottom: -.04em; }
.token.extra-stop::after { background: rgba(255, 59, 48, .1); }
.token.missing-stop::before { background: rgba(111, 107, 102, .1); }
.result-panel { min-height: 92px; }
.score-card { display: inline-flex; align-items: baseline; gap: 10px; padding: 12px 18px; border-radius: 16px; background: #fff4df; border: 1px solid rgba(104,59,34,.15); }
.score-card strong { font-size: 2rem; }
.stats { margin-top: 10px; display: flex; gap: 10px; flex-wrap: wrap; }
.stats span { padding: 7px 12px; border-radius: 999px; background: rgba(255,255,255,.62); }

.teacher-shell {
  width: min(1680px, calc(100vw - 32px));
  height: calc(100vh - 68px);
  margin: 0 auto;
  padding: 16px 0;
  display: grid;
  min-height: 0;
  overflow: hidden;
}

.teacher-dashboard {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 14px;
  padding: 18px 20px;
  min-height: 0;
  height: 100%;
}

.dashboard-toolbar {
  display: grid;
  grid-template-columns: auto minmax(320px, 520px);
  align-items: end;
  justify-content: space-between;
  gap: 16px 24px;
}

.dashboard-header { min-width: 0; }
.dashboard-header .eyebrow { margin-bottom: 4px; }
.dashboard-header h1 {
  margin: 0;
  font-size: clamp(1.55rem, 2.4vw, 2.15rem);
  line-height: 1.2;
}

.admin-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  padding: 6px;
  border: 1px solid rgba(104,59,34,.16);
  border-radius: 999px;
  background: rgba(255,255,255,.48);
}

.tab-button {
  border: 0;
  border-radius: 999px;
  padding: 10px 14px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-weight: 700;
  letter-spacing: .04em;
  transition: background .2s ease, color .2s ease, box-shadow .2s ease;
  white-space: nowrap;
}

.tab-button:hover { background: rgba(104,59,34,.08); color: var(--primary); }
.tab-button.active { background: var(--primary); color: #fff8ec; box-shadow: 0 10px 24px rgba(104,59,34,.22); }

.tab-panel {
  display: grid;
  min-width: 0;
  min-height: 0;
}

.tab-panel.active { height: 100%; }
.tab-panel[hidden] { display: none; }

.panel-workspace {
  display: grid;
  grid-template-columns: minmax(340px, 1.05fr) minmax(0, 1.2fr);
  gap: 16px;
  min-height: 0;
  height: 100%;
}

.panel-workspace.records-workspace {
  grid-template-columns: 1fr;
}

.panel-controls {
  display: grid;
  gap: 12px;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  align-content: start;
}

.panel-list-pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
  min-width: 0;
  min-height: 0;
  padding: 14px;
  border: 1px solid rgba(104,59,34,.14);
  border-radius: 20px;
  background: rgba(255,255,255,.42);
}

.panel-list-pane.full {
  height: 100%;
  grid-template-rows: auto auto minmax(0, 1fr);
}

.list-pane-header h2,
.list-pane-header h3 {
  margin: 0 0 4px;
}

.list-pane-header .muted { margin: 0; }

.student-list-pane {
  grid-template-rows: auto auto minmax(0, 1fr) auto;
}

.student-list-toolbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.student-filter {
  display: flex;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
}

.student-filter label {
  gap: 6px;
  min-width: 140px;
}

.student-filter label span {
  color: var(--muted);
  font-size: .86rem;
}

.student-filter input[type="search"],
.record-filter input[type="search"],
.record-filter input[type="date"] {
  padding: 10px 14px;
  border-radius: 14px;
}

.student-filter select,
.record-filter select {
  padding: 10px 36px 10px 14px;
  border-radius: 14px;
}

.record-filter select,
.student-filter select {
  min-width: 160px;
}

.sort-button {
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  padding: 0;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.sort-button:hover { color: var(--primary); }
.sort-button.active { color: var(--primary); }

.table-pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding-top: 10px;
}

.pagination-meta {
  color: var(--muted);
  font-size: .9rem;
}

.pagination-size {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: .9rem;
}

.pagination-size select {
  width: auto;
  padding: 8px 32px 8px 12px;
  border-radius: 14px;
}

.record-toolbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.record-filter {
  display: flex;
  align-items: end;
  gap: 10px;
  flex-wrap: wrap;
}

.record-filter label {
  gap: 6px;
  min-width: 140px;
}

.record-filter label span {
  color: var(--muted);
  font-size: .86rem;
}

.panel-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border: 1px solid rgba(104,59,34,.12);
  border-radius: 16px;
  background: linear-gradient(135deg, rgba(255,255,255,.6), rgba(255,244,223,.58));
}

.panel-heading.compact h2 { margin-bottom: 4px; font-size: 1.2rem; }
.panel-heading.compact .muted { margin: 0; font-size: .92rem; line-height: 1.5; }

.admin-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 12px;
  align-items: stretch;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.admin-subcard {
  min-width: 0;
  padding: 14px;
  border: 1px solid rgba(104,59,34,.14);
  border-radius: 16px;
  background: rgba(255,255,255,.56);
}

.admin-subcard h3 { margin-bottom: 2px; font-size: 1.05rem; }
.admin-subcard .button.primary { width: 100%; justify-content: center; }
.admin-subcard.stack-form { gap: 10px; }

.text-editor-card textarea {
  min-height: 240px;
  max-height: 42vh;
}

.text-editor-card label span {
  color: var(--muted);
  font-size: .86rem;
}

.text-normalize-progress {
  display: grid;
  gap: 6px;
}

.progress-track {
  height: 8px;
  border-radius: 999px;
  background: rgba(104,59,34,.12);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--primary), #c98b4d);
  transition: width .12s ease;
}

.progress-label {
  color: var(--muted);
  font-size: .84rem;
}

.scrollable-list {
  min-height: 0;
  overflow: auto;
  align-content: start;
  padding-right: 2px;
  gap: 8px;
}

.scrollable-list .list-row {
  padding: 12px 14px;
  border-radius: 14px;
}

.record-table-wrap {
  min-height: 0;
  overflow: auto;
}

.record-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .92rem;
}

.record-table th,
.record-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid rgba(104,59,34,.12);
  vertical-align: middle;
  white-space: nowrap;
}

.record-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: rgba(255,248,236,.96);
  color: var(--muted);
  font-weight: 700;
}

.record-table tbody tr:hover {
  background: rgba(255,255,255,.55);
}

.record-table .record-title-cell {
  white-space: normal;
  min-width: 140px;
}

.record-table .num-cell {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.record-table .check-cell {
  width: 36px;
  text-align: center;
}

.record-table .check-cell input {
  width: auto;
  accent-color: var(--primary);
}

.student-row { align-items: flex-start; }
.student-info { display: grid; gap: 2px; }

.admin-card { min-width: 0; }
.wide-card { grid-column: 1 / -1; }
.preview-box {
  max-height: 160px;
  overflow: auto;
  border-radius: 14px;
  background: #2b241d;
  color: #fff4e0;
  padding: 12px;
  margin: 0;
  font-size: .86rem;
}

.modal {
  border: 0;
  padding: 0;
  background: transparent;
  max-width: min(440px, calc(100vw - 32px));
}

.record-review-modal {
  max-width: min(920px, calc(100vw - 32px));
  width: min(920px, calc(100vw - 32px));
}

.record-review-card {
  display: grid;
  gap: 12px;
}

.record-review-legend {
  margin-top: 0;
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.record-review-frame {
  height: min(58vh, 520px);
  min-height: 260px;
}

.modal::backdrop {
  background: rgba(44, 33, 23, 0.42);
  backdrop-filter: blur(4px);
}

.modal-card {
  margin: 0;
  padding: 22px;
  border: 1px solid rgba(104,59,34,.18);
  border-radius: 22px;
  background: rgba(255, 248, 236, 0.98);
  box-shadow: 0 24px 80px rgba(74, 45, 18, 0.22);
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.45rem;
}

.modal-card .message { min-height: 1.4em; }

@media (max-width: 1100px) {
  .dashboard-toolbar { grid-template-columns: 1fr; align-items: stretch; }
  .panel-workspace { grid-template-columns: 1fr; }
  .panel-controls { overflow: visible; }
  .panel-list-pane { min-height: 320px; }
  .teacher-shell { height: auto; min-height: calc(100vh - 68px); overflow: visible; }
  .teacher-dashboard { height: auto; }
  .tab-panel.active { height: auto; }
  .panel-workspace { height: auto; }
}

@media (max-width: 920px) {
  .home-shell, .app-shell { grid-template-columns: 1fr; height: auto; }
  .app-shell { padding: 12px; gap: 12px; }
  .teacher-shell { width: min(100% - 20px, 1680px); padding-block: 12px 20px; }
  .admin-tabs, .admin-grid, .field-grid { grid-template-columns: 1fr; }
  .admin-tabs { border-radius: 24px; }
  .panel-heading, .dashboard-toolbar, .record-toolbar { flex-direction: column; align-items: stretch; }
  .record-filter { align-items: stretch; }
  .record-filter label { min-width: 0; }
  .student-filter { align-items: stretch; }
  .student-filter label { min-width: 0; }
  .practice-header { flex-direction: column; }
  .practice-area { gap: 10px; }
  .scroll-frame {
    min-height: 180px;
    height: calc(100dvh - 200px);
    max-height: calc(100dvh - 140px);
  }
  .reader,
  .reader-block {
    height: 100%;
    max-height: 100%;
  }
  .main-token { width: 2.08rem; height: 2.08rem; font-size: 1.86rem; }
  .note-pair { width: 2.08rem; }
  .note-token { width: 1.05rem; height: 1.05rem; font-size: .88rem; }
  .record-table th,
  .record-table td {
    padding: 8px 10px;
  }
}

@media (max-width: 920px) and (max-height: 700px) {
  .scroll-frame {
    height: calc(100dvh - 160px);
    max-height: calc(100dvh - 120px);
  }
}
