:root {
  color-scheme: light;
  --bg: #faf0df;
  --surface: #fff8ed;
  --surface-strong: #fffdf7;
  --line: #dec5a3;
  --text: #1f1d1a;
  --muted: #74614f;
  --accent: #a85f2c;
  --accent-strong: #2f241d;
  --accent-soft: #f5dfbf;
  --green: #a85f2c;
  --green-soft: #f5dfbf;
  --blue: #5a6570;
  --blue-soft: #e8eaeb;
  --amber: #c27a2c;
  --amber-soft: #f7e3bd;
  --red: #9a433b;
  --red-soft: #f3dfd9;
  --shadow: 0 12px 34px rgba(80, 50, 24, 0.08);
  --font-display: Charter, Georgia, "Songti SC", "Noto Serif CJK SC", serif;
  --font-body: Inter, "SF Pro Text", "Noto Sans SC", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-family: var(--font-body);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  text-rendering: optimizeLegibility;
}

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

.page {
  width: min(1440px, calc(100% - 32px));
  margin: 0 auto;
  padding: 20px 0 36px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: flex-end;
  margin-bottom: 14px;
}

.topbar h1,
.section-head h2,
.module-head h3,
.topic-card h4 {
  margin: 0;
  letter-spacing: 0;
}

.section-head h2 {
  font-family: var(--font-display);
  font-size: clamp(22px, 2vw, 30px);
  font-weight: 650;
  line-height: 1.08;
}

.section-head.compact h2 {
  font-size: clamp(20px, 1.8vw, 26px);
}

.topbar h1 {
  font-family: var(--font-display);
  font-size: clamp(28px, 3.6vw, 42px);
  font-weight: 650;
  line-height: 1.04;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 760;
  letter-spacing: 0;
  text-transform: uppercase;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.button,
.icon-button,
.text-button {
  border: 0;
  cursor: pointer;
}

.button {
  min-height: 38px;
  border-radius: 8px;
  padding: 0 13px;
  font-size: 14px;
  font-weight: 720;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: background 160ms ease, border-color 160ms ease, color 160ms ease;
}

.button.primary {
  color: white;
  background: var(--accent);
}

.button.ghost {
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
}

.button.primary:hover {
  background: #8f4a23;
}

.button.ghost:hover {
  background: var(--accent-soft);
  border-color: #cfb28b;
}

.button.compact {
  min-height: 34px;
  background: var(--accent);
  color: white;
}

.file-button {
  position: relative;
  overflow: hidden;
}

.file-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.error {
  margin: 14px 0;
  border: 1px solid var(--red);
  background: var(--red-soft);
  color: var(--red);
  border-radius: 8px;
  padding: 12px 14px;
  font-weight: 700;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.metric-card,
.panel,
.topic-area {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.metric-card {
  padding: 14px;
}

.metric-card span,
.metric-card small,
.topic-card p,
.topic-meta,
.session-item p,
.session-item small,
.module-head span {
  color: var(--muted);
}

.metric-card span {
  font-size: 13px;
  font-weight: 700;
}

.metric-card small {
  font-size: 13px;
}

.metric-card strong {
  display: block;
  margin: 5px 0 3px;
  font-family: var(--font-display);
  font-size: clamp(22px, 3vw, 28px);
  font-weight: 650;
  line-height: 1.08;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(320px, 0.8fr);
  gap: 16px;
  align-items: start;
}

.topic-area,
.panel {
  padding: 16px;
}

.topic-area,
.side-area,
.panel {
  min-width: 0;
}

.side-area {
  display: grid;
  gap: 16px;
}

.topic-explorer {
  display: grid;
  grid-template-columns: minmax(190px, 0.36fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
  min-width: 0;
}

.topic-jump-panel {
  display: grid;
  gap: 10px;
  position: sticky;
  top: 0;
}

.topic-jump-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
  padding: 9px;
}

.topic-jump-main,
.topic-jump-chapters button {
  width: 100%;
  border: 0;
  cursor: pointer;
  text-align: left;
}

.topic-jump-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-radius: 8px;
  background: var(--green-soft);
  color: var(--text);
  padding: 10px;
  font-weight: 780;
}

.topic-jump-main strong {
  color: var(--green);
  white-space: nowrap;
}

.topic-jump-chapters {
  display: grid;
  gap: 6px;
  margin-top: 8px;
}

.topic-jump-chapters button {
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
  padding: 7px 8px;
}

.topic-jump-main:hover,
.topic-jump-chapters button:hover {
  border-color: var(--line);
  background: var(--accent-soft);
  color: var(--text);
}

.topic-sections {
  min-width: 0;
}

.section-head,
.module-head,
.topic-main,
.topic-meta,
.topic-actions,
.session-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.section-head {
  margin-bottom: 16px;
}

.section-head.compact {
  margin-bottom: 12px;
}

.select,
.input,
.mini-select {
  border: 1px solid var(--line);
  background: var(--surface-strong);
  color: var(--text);
  border-radius: 8px;
}

.select,
.input {
  min-height: 42px;
  padding: 0 12px;
}

.mini-select {
  min-height: 34px;
  padding: 0 8px;
  max-width: 120px;
}

.module-block + .module-block {
  margin-top: 18px;
}

.module-head {
  padding: 11px 0;
  border-bottom: 1px solid var(--line);
}

.module-head h3 {
  font-size: 17px;
  font-weight: 760;
}

.chapter-title {
  margin: 18px 0 10px;
  font-weight: 780;
  color: var(--accent-strong);
}

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

.topic-card {
  border: 1px solid var(--line);
  background: var(--surface-strong);
  border-radius: 8px;
  padding: 12px;
  cursor: pointer;
  transition: background 160ms ease, border-color 160ms ease, transform 160ms ease;
}

.topic-card:hover,
.topic-card:focus-visible {
  background: #fffaf0;
  border-color: #cfae82;
  outline: 0;
  transform: translateY(-1px);
}

.topic-card h4 {
  font-size: 16px;
  font-weight: 760;
}

.topic-card p {
  margin: 6px 0 0;
  line-height: 1.5;
}

.topic-meta {
  margin: 14px 0;
  font-size: 13px;
}

.topic-card-badges {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
}

.topic-open-hint {
  border-radius: 999px;
  padding: 5px 9px;
  background: var(--amber-soft);
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.topic-open-hint.pending {
  background: rgba(115, 99, 78, 0.1);
  color: var(--muted);
}

.topic-actions {
  cursor: default;
}

.status-pill,
.streak {
  border-radius: 999px;
  padding: 5px 9px;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.status-idle .status-pill {
  color: #6b5b49;
  background: #f1e5d2;
}

.status-active .status-pill {
  color: var(--blue);
  background: var(--blue-soft);
}

.status-done .status-pill,
.streak {
  color: var(--green);
  background: var(--green-soft);
}

.status-review .status-pill {
  color: var(--amber);
  background: var(--amber-soft);
}

.week-labels,
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 6px;
}

.week-labels {
  color: var(--muted);
  font-size: 12px;
  text-align: center;
  margin-bottom: 6px;
}

.exam-calendar {
  display: grid;
  gap: 14px;
  max-height: 72vh;
  overflow: auto;
  padding-right: 4px;
}

.calendar-month {
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.calendar-month:first-child {
  border-top: 0;
  padding-top: 0;
}

.calendar-month h3 {
  margin: 0 0 10px;
  font-size: 15px;
}

.calendar-day {
  aspect-ratio: 1;
  border-radius: 8px;
  display: grid;
  place-items: center;
  font-size: 12px;
  background: #f2e6d2;
  color: #6b6258;
}

.calendar-day.blank {
  background: transparent;
}

.calendar-day.day-past {
  opacity: 0.42;
}

.calendar-day.day-after-exam {
  opacity: 0.34;
}

.calendar-day.day-today {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
  font-weight: 900;
}

.calendar-day.day-exam {
  background: var(--red);
  color: white;
  font-weight: 900;
}

.calendar-day.day-low {
  background: #ead5b6;
  color: var(--accent-strong);
}

.calendar-day.day-mid {
  background: #c9863c;
  color: white;
}

.calendar-day.day-high {
  background: var(--green);
  color: white;
}

.bar-chart {
  height: 138px;
  display: grid;
  grid-template-columns: repeat(14, minmax(10px, 1fr));
  gap: 7px;
  align-items: end;
}

.chart-item {
  height: 100%;
  display: grid;
  grid-template-rows: 1fr auto;
  gap: 6px;
  align-items: end;
}

.chart-item i {
  width: 100%;
  min-height: 6px;
  border-radius: 7px 7px 2px 2px;
  background: var(--accent);
}

.chart-item span {
  color: var(--muted);
  font-size: 10px;
  text-align: center;
  writing-mode: vertical-rl;
  justify-self: center;
  max-height: 34px;
}

.session-item {
  align-items: flex-start;
  padding: 12px 0;
  border-top: 1px solid var(--line);
}

.session-item:first-child {
  border-top: 0;
  padding-top: 0;
}

.session-item p,
.session-item small {
  margin: 5px 0 0;
  display: block;
  line-height: 1.45;
}

.text-button {
  color: var(--red);
  background: transparent;
  padding: 4px;
  font-weight: 700;
}

.empty {
  margin: 0;
  color: var(--muted);
}

.session-dialog {
  width: min(540px, calc(100% - 24px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
}

.topic-dialog {
  width: min(780px, calc(100% - 28px));
  max-height: min(86vh, 720px);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 22px 70px rgba(80, 50, 24, 0.2);
}

.topic-dialog::backdrop {
  background: rgba(31, 29, 26, 0.34);
}

.topic-detail-shell {
  display: grid;
  gap: 16px;
  padding: 18px;
  max-height: inherit;
  overflow: auto;
}

.topic-detail-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.topic-detail-head > div {
  min-width: 0;
}

.topic-detail-head h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(24px, 3vw, 34px);
  line-height: 1.08;
}

.topic-detail-essence {
  margin: 0;
  border-left: 4px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 8px;
  padding: 12px 14px;
  color: var(--accent-strong);
  font-size: 16px;
  font-weight: 760;
  line-height: 1.6;
}

.topic-detail-essence.pending {
  border-left-color: var(--muted);
  background: rgba(115, 99, 78, 0.1);
  color: var(--muted);
}

.topic-detail-grid {
  display: grid;
  grid-template-columns: 1.1fr 1fr;
  gap: 12px;
}

.topic-detail-section {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
  padding: 14px;
}

.topic-detail-recitation {
  grid-row: span 2;
}

.topic-detail-section h3 {
  margin: 0 0 10px;
  font-size: 15px;
}

.topic-detail-section ul {
  margin: 0;
  padding-left: 20px;
  color: var(--muted);
  line-height: 1.65;
}

.topic-detail-section li + li {
  margin-top: 7px;
}

.topic-detail-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.topic-detail-actions span {
  color: var(--muted);
  line-height: 1.5;
}

.session-dialog::backdrop {
  background: rgba(29, 39, 48, 0.32);
}

.session-form {
  padding: 18px;
  display: grid;
  gap: 14px;
}

.session-form label {
  display: grid;
  gap: 7px;
  color: var(--accent-strong);
  font-weight: 760;
}

.session-form textarea {
  min-height: 96px;
  padding: 10px 12px;
  resize: vertical;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 6px;
}

.icon-button {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--text);
  font-size: 24px;
  line-height: 1;
  transition: background 160ms ease, color 160ms ease;
}

.icon-button:hover:not(:disabled) {
  background: #ead8bd;
  color: var(--accent-strong);
}

.icon-button:disabled {
  background: #f6ecde;
  color: var(--muted);
  cursor: not-allowed;
  opacity: 0.45;
}

.calendar-nav {
  display: grid;
  grid-template-columns: 36px 1fr 36px;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.calendar-title {
  display: grid;
  justify-items: center;
  gap: 3px;
  min-width: 0;
  text-align: center;
}

.calendar-title strong {
  text-align: center;
  font-size: 16px;
}

.calendar-title span {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.2;
}

@media (min-width: 1081px) {
  body {
    overflow: hidden;
  }

  .page {
    display: flex;
    flex-direction: column;
    height: 100vh;
    padding-bottom: 28px;
  }

  .workspace {
    flex: 1;
    min-height: 0;
    align-items: stretch;
  }

  .topic-area {
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  #topicMap,
  .topic-explorer {
    min-height: 0;
  }

  #topicMap {
    flex: 1;
  }

  .topic-explorer {
    align-items: stretch;
    height: 100%;
  }

  .topic-jump-panel {
    max-height: 100%;
    overflow-y: auto;
    scrollbar-gutter: stable;
  }

  .topic-sections,
  .side-area {
    min-height: 0;
    overflow-y: auto;
    scrollbar-gutter: stable;
  }

  .side-area {
    align-content: start;
    padding-right: 2px;
  }
}

@media (max-width: 1080px) {
  body {
    overflow: auto;
  }

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

  .topic-explorer {
    display: block;
    height: auto;
  }

  .topic-jump-panel {
    display: flex;
    overflow-x: auto;
    overflow-y: visible;
    max-width: 100%;
    position: static;
    gap: 10px;
    margin-bottom: 16px;
    padding-bottom: 4px;
    scrollbar-gutter: auto;
  }

  .topic-jump-card {
    flex: 0 0 min(260px, 82vw);
    min-width: 0;
  }

  .topic-sections {
    overflow: visible;
  }

  .side-area {
    grid-template-columns: 1fr;
  }

  .exam-calendar {
    max-height: none;
  }
}

@media (max-width: 760px) {
  .page {
    width: min(100% - 20px, 640px);
    padding-top: 18px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .topbar-actions {
    width: 100%;
    justify-content: stretch;
  }

  .topbar-actions .button {
    flex: 1;
  }

  .metrics,
  .topic-grid,
  .side-area,
  .exam-calendar {
    grid-template-columns: 1fr;
  }

  .metric-card {
    padding: 14px;
  }

  .section-head,
  .topic-main,
  .topic-actions,
  .topic-detail-actions {
    align-items: flex-start;
    flex-direction: column;
  }

  .topic-detail-head {
    align-items: flex-start;
    flex-direction: row;
  }

  .topic-detail-head .icon-button {
    flex: 0 0 36px;
  }

  .select,
  .mini-select,
  .topic-actions .button,
  .topic-detail-actions .button {
    width: 100%;
  }

  .topic-card-badges {
    justify-content: flex-start;
  }

  .topic-dialog {
    width: 100%;
    max-width: none;
    max-height: 88vh;
    margin: auto 0 0;
    border-radius: 8px 8px 0 0;
  }

  .topic-detail-grid {
    grid-template-columns: 1fr;
  }

  .topic-detail-recitation {
    grid-row: auto;
  }

  .bar-chart {
    gap: 5px;
  }

  .session-dialog {
    width: 100%;
    max-width: none;
    margin: auto 0 0;
    border-radius: 8px 8px 0 0;
  }
}
