:root {
  color-scheme: light;
  --bg: #f7f6f2;
  --surface: #ffffff;
  --surface-soft: #f1f3ed;
  --ink: #24231f;
  --muted: #6f716b;
  --line: #dddcd4;
  --accent: #2e6f76;
  --accent-strong: #1f555b;
  --accent-soft: #d9ecec;
  --gold: #a56d1d;
  --red: #ad3e3a;
  --red-soft: #f6e2df;
  --green: #28734b;
  --green-soft: #deeee3;
  --blue-soft: #e3e9f5;
  --shadow: 0 18px 48px rgba(37, 35, 29, 0.12);
  --radius: 8px;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: 288px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 22px 22px 28px;
  border-right: 1px solid var(--line);
  background: #fbfaf6;
}

.sidebar::-webkit-scrollbar {
  width: 10px;
}

.sidebar::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar::-webkit-scrollbar-thumb {
  border: 3px solid #fbfaf6;
  border-radius: 999px;
  background: #cbc9bf;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: var(--radius);
  background: var(--accent);
  color: white;
  font-weight: 800;
  letter-spacing: 0;
}

.brand h1 {
  margin: 0;
  font-size: 18px;
  line-height: 1.2;
}

.brand p,
.muted-text,
.empty-state p,
.keyword-help p,
.editor-section-head p {
  color: var(--muted);
}

.brand p {
  margin: 4px 0 0;
  font-size: 13px;
}

.main-nav {
  display: grid;
  gap: 8px;
}

.nav-button {
  width: 100%;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 11px 12px;
  text-align: left;
  color: var(--ink);
  background: transparent;
}

.nav-button.active,
.nav-button:hover {
  border-color: var(--accent-soft);
  background: var(--accent-soft);
}

.side-panel {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.panel-title,
.section-label,
.field-label {
  display: block;
  margin-bottom: 8px;
  color: #565851;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
}

.stat-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin: 0;
}

.stat-list div {
  padding: 10px;
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.stat-list dt {
  color: var(--muted);
  font-size: 12px;
}

.stat-list dd {
  margin: 2px 0 0;
  font-size: 24px;
  font-weight: 750;
}

.side-actions {
  display: grid;
  gap: 8px;
  margin-top: auto;
}

.sync-panel {
  display: grid;
  gap: 10px;
}

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

.sync-actions .secondary-button {
  padding-inline: 8px;
}

.sync-status {
  min-height: 38px;
  padding: 9px 10px;
  border-radius: var(--radius);
  color: var(--muted);
  background: var(--surface-soft);
  font-size: 12px;
  line-height: 1.45;
}

.sync-status.ok {
  color: var(--green);
  background: var(--green-soft);
}

.sync-status.warn {
  color: #886118;
  background: #f8e8c8;
}

.sync-status.error {
  color: var(--red);
  background: var(--red-soft);
}

.workspace {
  min-width: 0;
  padding: 34px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.workspace-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 24px;
}

.workspace-header h2,
.dialog-head h2,
.empty-state h3 {
  margin: 0;
}

.workspace-header h2 {
  font-size: clamp(26px, 4vw, 42px);
  line-height: 1.05;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--gold);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0;
}

.setup-grid {
  display: grid;
  grid-template-columns: 1.2fr repeat(3, minmax(140px, 0.5fr));
  gap: 14px;
  margin-bottom: 16px;
}

.setup-section,
.chapter-picker,
.session-surface,
.library-toolbar,
.review-queues,
.library-chapter,
.queue-section {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.setup-section {
  padding: 14px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  padding: 4px;
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.segment {
  border: 0;
  border-radius: 6px;
  padding: 9px 10px;
  color: var(--muted);
  background: transparent;
}

.segment.active {
  color: white;
  background: var(--accent);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 11px;
  background: #fff;
  color: var(--ink);
  outline: none;
}

textarea {
  min-height: 110px;
  resize: vertical;
  line-height: 1.65;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(46, 111, 118, 0.16);
}

.chapter-picker {
  padding: 14px;
}

.chapter-picker-head,
.session-actions,
.library-toolbar,
.chapter-head,
.card-row-head,
.quiz-field-head,
.editor-section-head,
.dialog-actions,
.result-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.inline-actions {
  display: flex;
  gap: 10px;
}

.chapter-options {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.chapter-option {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.chapter-option input {
  width: auto;
}

.session-actions {
  justify-content: flex-start;
  margin: 16px 0;
}

.primary-button,
.secondary-button,
.text-button,
.icon-button {
  border-radius: var(--radius);
  border: 1px solid transparent;
}

.primary-button {
  padding: 11px 16px;
  color: #fff;
  background: var(--accent);
}

.primary-button:hover {
  background: var(--accent-strong);
}

.secondary-button {
  padding: 10px 13px;
  border-color: var(--line);
  color: var(--ink);
  background: var(--surface);
}

.secondary-button:hover {
  border-color: var(--accent);
}

.text-button {
  padding: 4px 0;
  color: var(--accent);
  background: transparent;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  font-size: 24px;
  color: var(--muted);
  background: transparent;
}

.danger-soft {
  color: var(--red);
  border-color: #efcbc7;
  background: var(--red-soft);
}

.session-surface {
  min-height: 430px;
  padding: 20px;
}

.empty-state {
  display: grid;
  place-content: center;
  min-height: 320px;
  text-align: center;
}

.study-card,
.quiz-card,
.result-panel,
.library-card,
.queue-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.study-card,
.quiz-card {
  padding: 22px;
}

.card-kicker {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
}

.chapter-tag,
.period-tag,
.status-pill,
.score-pill,
.source-pill {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 12px;
  font-weight: 700;
}

.chapter-tag {
  color: #314046;
  background: rgba(46, 111, 118, 0.12);
}

.period-tag {
  color: #fff;
  background: var(--accent);
}

.period-tag.modern {
  background: #6b4f8a;
}

.period-tag.unknown {
  color: #5c4b20;
  background: #f1e6c9;
}

.input-hint {
  margin: 7px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.status-pill {
  color: #5c4b20;
  background: #f1e6c9;
}

.status-pill.mastered {
  color: var(--green);
  background: var(--green-soft);
}

.status-pill.fuzzy {
  color: #886118;
  background: #f8e8c8;
}

.status-pill.forgot {
  color: var(--red);
  background: var(--red-soft);
}

.source-pill {
  color: #405479;
  background: var(--blue-soft);
}

.term-title {
  margin: 0 0 18px;
  font-size: clamp(30px, 7vw, 64px);
  line-height: 1.05;
}

.field-list {
  display: grid;
  gap: 12px;
}

.field-block {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfaf7;
}

.field-block h3 {
  margin: 0 0 8px;
  font-size: 15px;
}

.field-block p {
  margin: 0;
  line-height: 1.72;
}

.session-nav {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 18px;
}

.session-progress {
  color: var(--muted);
  margin-right: auto;
}

.quiz-inputs {
  display: grid;
  gap: 14px;
  margin-top: 18px;
}

.quiz-field {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfaf7;
}

.quiz-field h3 {
  margin: 0;
  font-size: 16px;
}

.quiz-field .keyword-count {
  color: var(--muted);
  font-size: 12px;
}

.result-panel {
  margin-top: 18px;
  padding: 16px;
}

.score-pill {
  color: #fff;
  background: var(--accent);
}

.field-result {
  padding: 14px 0;
  border-top: 1px solid var(--line);
}

.field-result:first-of-type {
  border-top: 0;
}

.keyword-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.keyword-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 8px;
  border-radius: 999px;
  font-size: 12px;
  background: var(--surface-soft);
}

.keyword-chip.hit {
  color: var(--green);
  background: var(--green-soft);
}

.keyword-chip.miss {
  color: var(--red);
  background: var(--red-soft);
}

.standard-answer {
  margin-top: 10px;
  padding: 12px;
  border-radius: var(--radius);
  background: #fbfaf7;
  line-height: 1.68;
}

.library-toolbar {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) 180px 160px auto;
  padding: 12px;
  margin-bottom: 16px;
}

.library-list,
.review-queues {
  display: grid;
  gap: 16px;
}

.library-chapter,
.queue-section {
  overflow: hidden;
}

.chapter-head,
.queue-head {
  padding: 14px 16px;
  background: #f8f7f1;
  border-bottom: 1px solid var(--line);
}

.chapter-head h3,
.queue-head h3 {
  margin: 0;
  font-size: 18px;
}

.chapter-head span,
.queue-head span {
  color: var(--muted);
  font-size: 13px;
}

.library-card,
.queue-card {
  margin: 12px;
  padding: 14px;
}

.card-row-head {
  align-items: flex-start;
}

.card-row-head h4 {
  margin: 0;
  font-size: 20px;
}

.card-summary {
  margin: 10px 0 0;
  color: var(--muted);
  line-height: 1.62;
}

.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.card-actions {
  display: flex;
  gap: 8px;
}

.card-dialog {
  width: min(1120px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  border: 0;
  border-radius: var(--radius);
  padding: 0;
  box-shadow: var(--shadow);
}

.card-dialog::backdrop {
  background: rgba(18, 23, 24, 0.38);
}

.dialog-shell {
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 32px);
}

.dialog-head,
.dialog-actions {
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
}

.dialog-actions {
  border-top: 1px solid var(--line);
  border-bottom: 0;
}

.editor-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 18px;
  padding: 20px;
  overflow: auto;
}

.editor-main {
  display: grid;
  gap: 14px;
}

.two-col {
  display: grid;
  grid-template-columns: 160px minmax(0, 1fr);
  gap: 12px;
}

.editor-section-head {
  align-items: flex-end;
  padding-top: 6px;
}

.editor-section-head h3,
.keyword-help h3 {
  margin: 0 0 4px;
}

.editor-section-head p,
.keyword-help p {
  margin: 0;
  font-size: 13px;
  line-height: 1.55;
}

.field-editor-list {
  display: grid;
  gap: 14px;
}

.field-editor {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfaf7;
}

.field-editor-top {
  display: grid;
  grid-template-columns: minmax(180px, 0.4fr) auto auto;
  gap: 10px;
  align-items: end;
  margin-bottom: 10px;
}

.keyword-editor {
  margin-top: 10px;
  padding: 10px;
  border: 1px dashed #c8cac0;
  border-radius: var(--radius);
  background: var(--surface);
}

.keyword-editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.keyword-input-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.keyword-help {
  position: sticky;
  top: 0;
  align-self: start;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #f8f7f1;
}

.example-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 14px 0;
}

.example-chips span {
  padding: 5px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-size: 12px;
}

.spacer {
  flex: 1;
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  max-width: min(420px, calc(100vw - 40px));
  padding: 12px 14px;
  border-radius: var(--radius);
  color: #fff;
  background: #24231f;
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.empty-line {
  color: var(--muted);
  padding: 16px;
}

@media (max-width: 920px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    height: auto;
    max-height: none;
    overflow: visible;
  }

  .workspace {
    padding: 22px;
  }

  .setup-grid,
  .library-toolbar,
  .editor-grid,
  .two-col {
    grid-template-columns: 1fr;
  }

  .workspace-header,
  .chapter-picker-head,
  .session-actions,
  .dialog-actions,
  .field-editor-top {
    align-items: stretch;
    flex-direction: column;
  }

  .field-editor-top {
    display: grid;
    grid-template-columns: 1fr;
  }

  .keyword-help {
    position: static;
  }
}
