:root {
  --paper: #f8f4e9;
  --canvas: #e9e3d5;
  --ink: #282a25;
  --muted: #6f7169;
  --line: #d4ccbb;
  --red: #b44332;
  --red-deep: #8d3025;
  --sage: #426a58;
  --sage-soft: #e0e9df;
  --warning: #9b6726;
  --white: #fffdf7;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--canvas); color: var(--ink); font-family: "Noto Sans TC", sans-serif; }
button, input, select { font: inherit; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible, summary:focus-visible { outline: 3px solid color-mix(in srgb, var(--red) 45%, transparent); outline-offset: 2px; }

.app-shell { min-height: 100vh; }
.topbar { height: 72px; display: grid; grid-template-columns: 290px 1fr auto; align-items: center; background: var(--ink); color: var(--paper); padding: 0 22px; gap: 24px; }
.brand { display: flex; align-items: center; gap: 11px; color: inherit; text-decoration: none; }
.brand-mark { width: 38px; height: 42px; display: grid; place-items: center; background: var(--paper); color: var(--red); font-family: "Noto Serif TC", serif; font-weight: 900; font-size: 22px; clip-path: polygon(0 0, 88% 0, 100% 12%, 100% 100%, 0 100%); }
.brand strong, .brand small { display: block; }
.brand strong { font-family: "Noto Serif TC", serif; letter-spacing: .12em; }
.brand small { font-size: 10px; opacity: .58; letter-spacing: .09em; margin-top: 1px; }
.document-title { justify-self: center; display: flex; align-items: center; gap: 10px; }
.document-title label { font-size: 12px; color: #b9b7ae; }
.document-title input { width: 230px; border: 0; border-bottom: 1px solid #6d6d65; color: var(--paper); background: transparent; padding: 7px 3px; font-family: "Noto Serif TC", serif; font-weight: 700; }
.save-state { font-size: 11px; color: #aeb8ad; }
.save-state i { width: 6px; height: 6px; display: inline-block; border-radius: 50%; background: #71a17d; margin-right: 5px; }
.top-actions { display: flex; gap: 8px; }
.quiet-button, .ink-button { border: 1px solid #65665f; border-radius: 3px; padding: 9px 14px; color: var(--paper); background: transparent; font-size: 13px; }
.ink-button { color: var(--ink); background: var(--paper); border-color: var(--paper); font-weight: 700; }

.workspace { min-height: calc(100vh - 72px); display: grid; grid-template-columns: minmax(270px, 310px) minmax(480px, 1fr) minmax(300px, 360px); }
.settings-panel, .preview-panel { padding: 26px 22px; background: #f2ede1; }
.settings-panel { border-right: 1px solid var(--line); overflow-y: auto; max-height: calc(100vh - 72px); }
.preview-panel { border-left: 1px solid var(--line); background: #ddd6c7; max-height: calc(100vh - 72px); overflow-y: auto; }
.editor-panel { padding: 28px clamp(22px, 3vw, 44px); background: var(--paper); max-height: calc(100vh - 72px); overflow-y: auto; }
.eyebrow { display: block; color: var(--red); font-size: 10px; font-weight: 700; letter-spacing: .16em; margin-bottom: 5px; }
h1, h2, h3, p { margin-top: 0; }
h1, h2, h3 { font-family: "Noto Serif TC", serif; }
.section-heading h1 { font-size: 24px; line-height: 1.3; margin-bottom: 8px; }
.section-heading p { font-size: 12px; line-height: 1.65; color: var(--muted); }
.source-disclosure { margin: 17px 0 2px; padding: 10px 11px; border: 1px solid #cdbdaf; background: #f8eee7; }
.source-disclosure summary { display: flex; justify-content: space-between; gap: 8px; cursor: pointer; list-style: none; font-size: 10px; color: var(--muted); }
.source-disclosure summary::-webkit-details-marker { display: none; }
.source-disclosure summary strong { color: var(--red-deep); font-size: 11px; }
.source-disclosure p { margin: 10px 0 1px; padding-top: 9px; border-top: 1px solid #ddcec2; color: var(--muted); font-size: 10px; line-height: 1.6; }
.source-disclosure p b { color: var(--red); }
.field-group { margin-top: 24px; }
.field-label { display: flex; justify-content: space-between; align-items: center; font-size: 12px; font-weight: 700; margin-bottom: 9px; }
.field-label em { font-style: normal; font-size: 10px; color: var(--muted); font-weight: 500; }
.field-label button { border: 0; background: transparent; color: var(--red); font-size: 11px; }
.fixed-selection { display: flex; justify-content: space-between; padding: 11px 12px; border: 1px solid var(--line); background: rgba(255,255,255,.35); font-size: 12px; }
.fixed-selection span { color: var(--muted); }
.grade-selection { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 8px; }
.grade-selection label { color: var(--muted); font-weight: 500; }
.grade-selection select { border: 0; border-bottom: 1px solid var(--line); background: transparent; color: var(--ink); padding: 2px 4px 4px; font-weight: 700; }
.objective-search { position: relative; margin-bottom: 8px; }
.objective-search span { position: absolute; left: 10px; top: 7px; color: var(--muted); }
.objective-search input { width: 100%; border: 1px solid var(--line); background: var(--white); padding: 8px 9px 8px 29px; font-size: 12px; }
.objective-list { display: grid; gap: 5px; max-height: 215px; overflow-y: auto; padding-right: 3px; }
.objective-item { width: 100%; border: 1px solid transparent; background: transparent; padding: 9px 10px; text-align: left; color: var(--ink); display: grid; grid-template-columns: 18px 1fr; gap: 7px; border-radius: 2px; }
.objective-item:hover { background: rgba(255,255,255,.45); }
.objective-item.selected { border-color: color-mix(in srgb, var(--red) 40%, var(--line)); background: #f9eee8; }
.objective-item i { width: 15px; height: 15px; border: 1px solid #aaa596; display: grid; place-items: center; margin-top: 1px; font-style: normal; font-size: 10px; color: transparent; }
.objective-item.selected i { background: var(--red); border-color: var(--red); color: white; }
.objective-item strong { display: block; font-size: 12px; line-height: 1.35; }
.objective-item small { display: block; color: var(--muted); font-size: 9px; margin-top: 2px; }
.segmented { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid var(--line); }
.segmented button { border: 0; border-right: 1px solid var(--line); padding: 9px 3px; background: transparent; color: var(--muted); font-size: 12px; }
.segmented button:last-child { border-right: 0; }
.segmented button.active { background: var(--ink); color: var(--paper); }
.inline-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.inline-fields label { font-size: 11px; font-weight: 700; }
.inline-fields select { display: block; width: 100%; margin-top: 7px; border: 1px solid var(--line); background: var(--white); padding: 8px; font-size: 12px; color: var(--ink); }
.advanced-options { margin-top: 19px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 10px 0; font-size: 11px; }
.advanced-options summary { cursor: pointer; font-weight: 700; }
.advanced-options summary span { float: right; color: var(--muted); font-weight: 400; }
.check-row { display: block; margin-top: 10px; color: var(--muted); }
.generate-button { width: 100%; margin-top: 20px; padding: 12px 14px; display: flex; justify-content: space-between; border: 0; background: var(--red); color: white; font-weight: 700; box-shadow: 3px 3px 0 var(--red-deep); }
.generate-button small { font-weight: 400; opacity: .72; }
.generate-button:hover { transform: translate(-1px,-1px); box-shadow: 4px 4px 0 var(--red-deep); }
.generate-button:disabled { opacity: .55; cursor: wait; transform: none; box-shadow: 2px 2px 0 var(--red-deep); }

.editor-header, .preview-header { display: flex; align-items: flex-end; justify-content: space-between; border-bottom: 1px solid var(--line); padding-bottom: 16px; }
.editor-header h2, .preview-header h2 { font-size: 22px; margin: 0; }
.review-stats { min-width: 140px; font-size: 11px; color: var(--muted); text-align: right; }
.review-stats strong { color: var(--sage); font-size: 18px; font-family: "Noto Serif TC", serif; }
.progress { height: 3px; background: var(--line); margin-top: 6px; }
.progress i { display: block; height: 100%; background: var(--sage); width: 0; transition: width .4s ease; }
.empty-state { max-width: 390px; margin: clamp(70px, 12vh, 130px) auto; text-align: center; }
.paper-stack { width: 88px; height: 104px; display: grid; place-items: center; margin: 0 auto 30px; background: var(--white); border: 1px solid var(--line); box-shadow: -7px 7px 0 #e6dfd0, -8px 8px 0 var(--line); transform: rotate(2deg); }
.paper-stack span { font-family: "Noto Serif TC", serif; font-size: 23px; color: var(--red); }
.empty-state h3 { font-size: 20px; margin-bottom: 10px; }
.empty-state p { font-size: 12px; line-height: 1.7; color: var(--muted); }
.empty-state ul { list-style: none; padding: 0; margin: 24px auto 0; width: max-content; text-align: left; font-size: 11px; color: var(--muted); }
.empty-state li { margin: 7px 0; }
.empty-state li i { color: var(--sage); margin-right: 8px; }
.hidden { display: none !important; }

.generation-state { text-align: center; margin: 95px auto 0; }
.generation-orbit { width: 70px; height: 70px; margin: auto; border: 1px solid var(--line); border-radius: 50%; display: grid; place-items: center; position: relative; }
.generation-orbit span { position: absolute; inset: -3px; border-radius: 50%; border-top: 3px solid var(--red); animation: spin 1.2s linear infinite; }
.generation-orbit b { font-family: "Noto Serif TC", serif; color: var(--red); font-size: 22px; }
@keyframes spin { to { transform: rotate(360deg); } }
.generation-state h3 { margin: 24px 0 18px; }
.generation-state ol { list-style: none; display: flex; justify-content: center; padding: 0; gap: 25px; color: #aaa596; font-size: 11px; }
.generation-state li.done { color: var(--sage); }
.generation-state li.done::before { content: "✓ "; }

.question-list { margin-top: 22px; display: grid; gap: 13px; }
.question-card { border-top: 1px solid var(--line); background: rgba(255,255,255,.3); padding: 17px 16px 15px; position: relative; transition: background .2s ease; }
.question-card.approved { background: color-mix(in srgb, var(--sage-soft) 45%, transparent); }
.question-card.warning { background: #fbf1df; }
.question-meta { display: flex; align-items: center; gap: 7px; margin-bottom: 12px; font-size: 9px; color: var(--muted); }
.question-number { width: 25px; height: 25px; display: grid; place-items: center; background: var(--ink); color: var(--paper); font-family: "Noto Serif TC", serif; font-size: 12px; }
.status-badge { margin-left: auto; padding: 4px 7px; border: 1px solid #a9bdad; color: var(--sage); }
.warning .status-badge { border-color: #d5b47c; color: var(--warning); }
.question-body { font-family: "Noto Serif TC", serif; font-weight: 600; line-height: 1.75; font-size: 14px; }
.answer-line { margin-top: 10px; font-size: 11px; color: var(--muted); }
.answer-line b { color: var(--ink); }
.question-warning { color: var(--warning); font-size: 10px; border-left: 2px solid #c58a36; padding-left: 7px; margin-top: 10px; }
.question-actions { display: flex; gap: 4px; margin-top: 14px; }
.question-actions button { border: 1px solid var(--line); background: transparent; padding: 6px 9px; color: var(--muted); font-size: 10px; }
.question-actions .approve { margin-left: auto; color: var(--sage); border-color: #9ab2a1; font-weight: 700; }
.approved .question-actions .approve { background: var(--sage); color: white; }
.edit-area { display: grid; gap: 8px; margin-top: 12px; }
.edit-area textarea, .edit-area input { border: 1px solid var(--line); background: var(--white); padding: 8px; color: var(--ink); resize: vertical; font-size: 12px; }
.edit-area button { justify-self: start; border: 0; background: var(--ink); color: var(--paper); padding: 7px 12px; font-size: 10px; }

.icon-button { border: 1px solid #aaa393; background: transparent; color: var(--ink); width: 30px; height: 30px; }
.preview-tabs { display: grid; grid-template-columns: 1fr 1fr; margin-top: 15px; border-bottom: 1px solid #aaa393; }
.preview-tabs button { border: 0; background: transparent; padding: 8px; color: var(--muted); font-size: 11px; }
.preview-tabs button.active { color: var(--red); border-bottom: 2px solid var(--red); font-weight: 700; }
.page-stage { padding: 20px 6px; }
.paper-page { aspect-ratio: 210 / 297; background: var(--white); border: 1px solid #c9c0ae; padding: 9% 8% 7%; color: #2a2b27; position: relative; box-shadow: 0 8px 20px rgba(54,47,35,.10); }
.paper-kicker { color: var(--red); font-size: clamp(6px, .6vw, 9px); font-weight: 700; letter-spacing: .1em; }
.paper-kicker span { float: right; color: #77736a; letter-spacing: 0; font-family: "Noto Sans TC", sans-serif; font-weight: 500; }
.paper-page h3 { font-size: clamp(13px, 1.2vw, 18px); margin: 4px 0 9px; }
.student-meta { display: flex; justify-content: space-between; gap: 4px; font-size: clamp(5px, .48vw, 7px); }
.paper-rule { height: 2px; background: var(--ink); margin: 9px 0 11px; }
.preview-questions { display: grid; gap: clamp(7px, 1.1vw, 14px); font-family: "Noto Serif TC", serif; font-size: clamp(6px, .6vw, 9px); line-height: 1.55; }
.preview-question b { margin-right: 4px; }
.preview-question .preview-answer { color: var(--red); font-family: "Noto Sans TC", sans-serif; font-size: .9em; }
.answer-box { display: inline-block; width: 38px; border-bottom: 1px solid #777; margin-left: 4px; }
.preview-placeholder { text-align: center; color: #aaa596; margin-top: 38%; font-family: "Noto Sans TC", sans-serif; }
.paper-page footer { position: absolute; bottom: 4%; left: 8%; right: 8%; display: flex; justify-content: space-between; border-top: 1px solid #bbb5a8; padding-top: 5px; color: #8f8b82; font-size: clamp(5px, .45vw, 7px); }
.preview-meta { display: flex; justify-content: space-between; color: #69675f; font-size: 10px; padding: 0 6px 14px; }
.export-button { width: 100%; border: 0; padding: 11px; background: var(--ink); color: var(--paper); font-weight: 700; }
.export-button:disabled { opacity: .42; cursor: not-allowed; }
.toast { position: fixed; z-index: 20; bottom: 25px; left: 50%; transform: translate(-50%, 20px); background: var(--ink); color: var(--paper); padding: 10px 16px; font-size: 12px; opacity: 0; pointer-events: none; transition: .25s ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.export-dialog { width: min(470px, calc(100vw - 30px)); border: 0; background: var(--paper); color: var(--ink); padding: 30px; box-shadow: 0 25px 70px rgba(25,24,21,.35); }
.export-dialog::backdrop { background: rgba(29,29,26,.64); }
.dialog-close { position: absolute; right: 15px; top: 12px; border: 0; background: transparent; font-size: 22px; color: var(--muted); }
.export-dialog h2 { margin-bottom: 7px; font-size: 25px; }
.export-dialog > p { color: var(--muted); font-size: 12px; }
.export-summary { display: grid; grid-template-columns: repeat(3, 1fr); border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); margin: 22px 0; }
.export-summary span { padding: 13px 5px; border-right: 1px solid var(--line); }
.export-summary span:last-child { border-right: 0; }
.export-summary small, .export-summary strong { display: block; }
.export-summary small { color: var(--muted); font-size: 9px; }
.export-summary strong { font-family: "Noto Serif TC", serif; font-size: 14px; margin-top: 3px; }
.download-option { width: 100%; border: 1px solid var(--line); background: var(--white); padding: 12px; margin-top: 8px; display: flex; justify-content: space-between; align-items: center; text-align: left; }
.download-option b, .download-option small { display: block; }
.download-option small { color: var(--muted); margin-top: 2px; font-size: 9px; }
.download-option i { font-style: normal; color: var(--red); font-size: 11px; }
.source-summary { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; padding: 10px 12px; margin: -11px 0 18px; background: #eee7da; font-size: 10px; }
.source-summary b { color: var(--red-deep); }
.source-summary span { font-weight: 700; }
.source-summary small { grid-column: 2; color: var(--muted); }
.recent-list { display: grid; gap: 8px; margin-top: 20px; max-height: 55vh; overflow-y: auto; }
.recent-empty { padding: 28px 12px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); color: var(--muted); text-align: center; font-size: 12px; }
.recent-item { border-top: 1px solid var(--line); padding: 13px 2px 5px; display: grid; grid-template-columns: 1fr auto; gap: 8px 14px; }
.recent-item h3 { margin: 0; font-size: 14px; }
.recent-item p { margin: 3px 0 0; color: var(--muted); font-size: 9px; }
.recent-item .recent-status { align-self: start; padding: 3px 6px; border: 1px solid #a7b9aa; color: var(--sage); font-size: 9px; }
.recent-actions { grid-column: 1 / -1; display: flex; gap: 6px; }
.recent-actions button { border: 1px solid var(--line); background: transparent; padding: 5px 9px; color: var(--muted); font-size: 10px; }
.recent-actions button:first-child { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.grading-dialog { width: min(760px, calc(100vw - 24px)); max-height: 88vh; border: 0; padding: 30px; background: var(--paper); color: var(--ink); box-shadow: 0 25px 70px rgba(25,24,21,.35); }
.grading-dialog::backdrop { background: rgba(29,29,26,.64); }
.grading-heading { display: flex; justify-content: space-between; align-items: end; border-bottom: 1px solid var(--line); padding-bottom: 14px; }
.grading-heading h2 { margin: 0; font-size: 25px; }
.source-pill { padding: 5px 8px; border: 1px solid #a6b9a9; color: var(--sage); font-size: 9px; }
.grading-intro { color: var(--muted); font-size: 11px; margin: 13px 0 18px; }
.answer-entry { display: grid; gap: 8px; max-height: 46vh; overflow-y: auto; padding-right: 5px; }
.answer-row { display: grid; grid-template-columns: 30px 1fr minmax(120px, .35fr); align-items: center; gap: 10px; border-top: 1px solid var(--line); padding: 10px 3px; }
.answer-row > b { width: 25px; height: 25px; display: grid; place-items: center; background: var(--ink); color: var(--paper); font-family: "Noto Serif TC", serif; font-size: 11px; }
.answer-row label { font-family: "Noto Serif TC", serif; font-size: 11px; line-height: 1.5; }
.answer-row input { width: 100%; border: 1px solid var(--line); background: var(--white); padding: 8px; font-size: 12px; }
.answer-row.correct { background: color-mix(in srgb, var(--sage-soft) 55%, transparent); }
.answer-row.incorrect { background: #fae9e4; }
.answer-feedback { grid-column: 2 / -1; font-size: 9px; color: var(--red-deep); }
.grading-footer { display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--line); margin-top: 13px; padding-top: 13px; font-size: 10px; color: var(--muted); }
.grading-footer button, .retry-practice { border: 0; background: var(--red); color: white; padding: 9px 14px; font-weight: 700; font-size: 11px; }
.grading-result { margin-top: 18px; border-top: 2px solid var(--ink); padding-top: 16px; }
.score-line { display: flex; align-items: baseline; gap: 10px; }
.score-line strong { font-family: "Noto Serif TC", serif; color: var(--red); font-size: 40px; }
.score-line span { font-size: 11px; color: var(--muted); }
.weakness-list { margin: 12px 0 16px; display: grid; gap: 6px; }
.weakness-item { display: flex; justify-content: space-between; padding: 8px 10px; background: #eee7da; font-size: 10px; }
.weakness-item b { color: var(--red-deep); }

@media (max-width: 1050px) {
  .workspace { grid-template-columns: 280px 1fr; }
  .preview-panel { grid-column: 1 / -1; max-height: none; border-left: 0; border-top: 1px solid var(--line); }
  .page-stage { max-width: 390px; margin: auto; }
  .settings-panel, .editor-panel { max-height: calc(100vh - 72px); }
}
@media (max-width: 720px) {
  .topbar { height: auto; min-height: 64px; grid-template-columns: 1fr auto; padding: 10px 14px; }
  .document-title { grid-row: 2; grid-column: 1 / -1; justify-self: stretch; }
  .document-title input { flex: 1; }
  .quiet-button { display: none; }
  .workspace { display: block; }
  .settings-panel, .editor-panel, .preview-panel { max-height: none; border: 0; padding: 22px 18px; }
  .editor-panel { min-height: 500px; }
  .generation-state ol { flex-direction: column; gap: 7px; }
  .paper-page { max-width: 360px; margin: auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
