/* ============ Base & Theme ============ */
:root {
  --bg: #f7f4ee;
  --bg-soft: #efeae0;
  --surface: #ffffff;
  --surface-2: #f0ede6;
  --text: #1a1a1a;
  --text-soft: #6b6b6b;
  --border: #e4dfd4;
  --accent: #2c5f4f;
  --accent-soft: #dce8e2;
  --danger: #b0453f;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --radius: 12px;
  --radius-sm: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}
body.night {
  --bg: #0e1110;
  --bg-soft: #161a18;
  --surface: #1a1f1d;
  --surface-2: #222825;
  --text: #e8ede9;
  --text-soft: #8a948f;
  --border: #2a322e;
  --accent: #8bc9a8;
  --accent-soft: #1f2e27;
  --shadow: 0 1px 3px rgba(0,0,0,0.4);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  transition: background 0.3s, color 0.3s;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input { font-family: inherit; }

/* ============ Layout ============ */
#app { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 50;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 22px 20px;
  border-bottom: 1px solid var(--border);
}
.brand-mark {
  width: 36px; height: 36px;
  background: var(--accent);
  color: var(--bg);
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  font-size: 18px;
}
.brand-text { font-weight: 600; font-size: 15px; }

.nav { flex: 1; padding: 14px 12px; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  text-align: left;
  color: var(--text-soft);
  font-size: 14px;
  transition: all 0.15s;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-ico { width: 20px; text-align: center; font-size: 16px; }

.sidebar-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
}
.avatar-btn { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  font-size: 14px;
  color: #fff;
  flex-shrink: 0;
}
.avatar-name { font-size: 13px; color: var(--text-soft); max-width: 100px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.theme-toggle {
  width: 34px; height: 34px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px;
  color: var(--text-soft);
  transition: all 0.15s;
}
.theme-toggle:hover { background: var(--surface-2); color: var(--text); }

.main {
  flex: 1;
  margin-left: 240px;
  padding: 32px 40px 60px;
  min-height: 100vh;
}
.view { display: none; animation: fade 0.25s ease; }
.view.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.view-header { margin-bottom: 28px; }
.view-header h2 { font-size: 24px; font-weight: 700; }
.view-desc { color: var(--text-soft); font-size: 14px; margin-top: 4px; }

/* ============ Home ============ */
.home-hero { margin-bottom: 32px; }
.hero-title { font-size: 30px; font-weight: 700; }
.hero-sub { color: var(--text-soft); margin-top: 6px; }
.feature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 18px;
}
.feature-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: var(--shadow);
}
.feature-card:hover { transform: translateY(-2px); border-color: var(--accent); box-shadow: 0 4px 12px rgba(0,0,0,0.08); }
.fc-ico {
  width: 44px; height: 44px;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
  margin-bottom: 14px;
}
.feature-card h3 { font-size: 17px; margin-bottom: 6px; }
.feature-card p { color: var(--text-soft); font-size: 13px; }

/* ============ Knowledge Points ============ */
.kp-layout { display: grid; grid-template-columns: 280px 1fr; gap: 20px; }
.kp-tree {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  max-height: 70vh;
  overflow-y: auto;
}
.tree-module { margin-bottom: 6px; }
.tree-module-title {
  font-weight: 600;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex; justify-content: space-between; align-items: center;
}
.tree-module-title:hover { background: var(--surface-2); }
.tree-module-title .arrow { transition: transform 0.2s; font-size: 12px; color: var(--text-soft); }
.tree-module.open .arrow { transform: rotate(90deg); }
.tree-lessons { display: none; padding-left: 10px; }
.tree-module.open .tree-lessons { display: block; }
.tree-lesson {
  padding: 7px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-soft);
  margin: 2px 0;
}
.tree-lesson:hover { background: var(--surface-2); color: var(--text); }
.tree-lesson.active { background: var(--accent-soft); color: var(--accent); font-weight: 500; }

.kp-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  min-height: 60vh;
}
.kp-content h3 { font-size: 18px; margin-bottom: 16px; padding-bottom: 12px; border-bottom: 1px solid var(--border); }
.kp-item {
  padding: 14px 16px;
  margin-bottom: 10px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  border-left: 3px solid var(--accent);
}
.kp-item .kp-num { font-weight: 600; color: var(--accent); margin-right: 6px; }
.kp-item .kp-blank {
  display: inline-block;
  min-width: 60px;
  border-bottom: 1px solid var(--accent);
  text-align: center;
  color: var(--accent);
  cursor: pointer;
  user-select: none;
  transition: all 0.15s;
}
.kp-item .kp-blank.filled { border-bottom-color: transparent; background: var(--accent-soft); padding: 0 6px; border-radius: 4px; }
.kp-item .kp-blank:hover { background: var(--accent-soft); }

.empty-hint {
  display: flex; align-items: center; justify-content: center;
  height: 300px;
  color: var(--text-soft);
  font-size: 14px;
}

/* ============ Cards ============ */
.cards-toolbar {
  display: flex; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; align-items: center;
}
.cards-toolbar select, .cards-toolbar .btn { height: 38px; }
.card-stage {
  perspective: 1200px;
  min-height: 320px;
  display: flex; align-items: center; justify-content: center;
}
.flashcard {
  width: 100%; max-width: 560px;
  min-height: 300px;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.5s;
  cursor: pointer;
}
.flashcard.flipped { transform: rotateY(180deg); }
.fc-face {
  position: absolute; inset: 0;
  backface-visibility: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 36px;
  display: flex; flex-direction: column; justify-content: center;
  box-shadow: var(--shadow);
}
.fc-back { transform: rotateY(180deg); }
.fc-face .fc-label { font-size: 12px; color: var(--text-soft); margin-bottom: 12px; letter-spacing: 1px; }
.fc-face .fc-text { font-size: 17px; line-height: 1.8; }
.fc-face .fc-text .blank { color: var(--accent); font-weight: 600; border-bottom: 2px solid var(--accent); padding: 0 4px; }
.cards-nav {
  display: flex; justify-content: center; align-items: center; gap: 16px;
  margin-top: 24px;
}
.cards-nav .card-progress { color: var(--text-soft); font-size: 13px; }

/* ============ Quiz ============ */
.quiz-setup { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 28px; }
.setup-row { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.setup-row label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-soft); }
select, .input {
  height: 40px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border 0.15s;
}
select:focus, .input:focus { border-color: var(--accent); }
.btn {
  height: 40px;
  padding: 0 20px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  transition: all 0.15s;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn-primary { background: var(--accent); color: var(--bg); }
.btn-primary:hover { opacity: 0.9; }
.btn-ghost { background: var(--surface-2); color: var(--text); }
.btn-ghost:hover { background: var(--border); }
.btn-sm { height: 32px; padding: 0 14px; font-size: 13px; }

.quiz-timer {
  position: sticky; top: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 20px;
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 20px;
  z-index: 10;
}
.timer-display { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--accent); }
.quiz-question {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px 26px;
  margin-bottom: 16px;
}
.q-num { font-weight: 700; color: var(--accent); margin-right: 8px; }
.q-text { font-size: 15px; margin-bottom: 16px; }
.q-options { display: flex; flex-direction: column; gap: 10px; }
.q-option {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s;
}
.q-option:hover { background: var(--surface-2); }
.q-option.selected { border-color: var(--accent); background: var(--accent-soft); }
.q-option.correct { border-color: #4caf50; background: rgba(76,175,80,0.1); }
.q-option.wrong { border-color: var(--danger); background: rgba(176,69,63,0.1); }
.q-option .opt-letter { font-weight: 700; color: var(--accent); min-width: 20px; }
.q-result { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); font-size: 14px; }
.q-result .correct-ans { color: #4caf50; font-weight: 600; }

/* ============ Daily / Wrong Paper ============ */
.daily-setup, .wrong-setup { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 28px; }
.topic-list { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 20px; }
.topic-chip {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.topic-chip:hover { background: var(--surface-2); }
.topic-chip.selected { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 500; }

.paper {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px;
  margin-top: 20px;
}
.paper-title { text-align: center; font-size: 22px; font-weight: 700; margin-bottom: 8px; }
.paper-meta { text-align: center; color: var(--text-soft); font-size: 13px; margin-bottom: 28px; }
.paper-section { margin-bottom: 28px; }
.paper-section h4 { font-size: 16px; margin-bottom: 14px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.paper-q { margin-bottom: 18px; }
.paper-q .pq-text { margin-bottom: 8px; }
.paper-q .pq-options { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 16px; font-size: 14px; }
.paper-actions { display: flex; gap: 12px; margin-top: 24px; justify-content: center; }

/* ============ Study Room ============ */
.studyroom-clock {
  display: flex; flex-direction: column; align-items: center;
  padding: 40px 0;
}
.flipclock {
  display: flex; gap: 12px;
  font-family: "SF Mono", "Menlo", monospace;
}
.flip-unit {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px 20px;
  min-width: 100px;
  text-align: center;
  box-shadow: var(--shadow);
}
.flip-unit .num { font-size: 48px; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
.flip-unit .label { font-size: 12px; color: var(--text-soft); margin-top: 6px; letter-spacing: 2px; }
.studyroom-tools {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 14px;
  margin-top: 30px;
}
.sr-tool {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s;
}
.sr-tool:hover { border-color: var(--accent); transform: translateY(-2px); }
.sr-tool .sr-ico { font-size: 24px; margin-bottom: 8px; }

/* ============ Records ============ */
.records-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 18px;
  margin-bottom: 24px;
}
.rec-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  cursor: pointer;
  transition: all 0.15s;
}
.rec-card:hover { border-color: var(--accent); }
.rec-card .rec-count { font-size: 32px; font-weight: 700; color: var(--accent); }
.rec-card .rec-label { color: var(--text-soft); font-size: 13px; margin-top: 4px; }
.records-detail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  min-height: 200px;
}
.records-detail h4 { margin-bottom: 14px; }
.rec-item {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  font-size: 14px;
}
.rec-item:last-child { border-bottom: none; }
.rec-item .rec-time { color: var(--text-soft); font-size: 12px; }
.rec-item .rec-actions { display: flex; gap: 8px; }

/* ============ Modal ============ */
.modal {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 100;
  align-items: center; justify-content: center;
}
.modal.show { display: flex; }
.modal-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 32px;
  width: 90%; max-width: 420px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal-box h3 { font-size: 20px; margin-bottom: 6px; }
.modal-sub { color: var(--text-soft); font-size: 13px; margin-bottom: 20px; }
.avatar-picker {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 10px;
  margin: 16px 0;
}
.avatar-opt {
  width: 100%; aspect-ratio: 1;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  color: #fff;
  cursor: pointer;
  border: 3px solid transparent;
  transition: all 0.15s;
  font-size: 18px;
}
.avatar-opt:hover { transform: scale(1.08); }
.avatar-opt.selected { border-color: var(--text); }
.modal-actions { display: flex; justify-content: flex-end; margin-top: 20px; }

/* ============ Toast ============ */
.toast {
  position: fixed;
  bottom: 30px; left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 12px 24px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  opacity: 0;
  transition: all 0.3s;
  z-index: 200;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ============ Print ============ */
@media print {
  html, body { background: #fff !important; color: #000 !important; }
  .sidebar, .view-header, .paper-actions, .cards-nav, .quiz-timer, .quiz-setup,
  .daily-setup, .wrong-setup, .records-cards, .records-detail, .theme-toggle,
  .avatar-btn, .toast, .modal { display: none !important; }
  .main { margin-left: 0; padding: 0; }
  .view { display: none !important; }
  .view.print-active { display: block !important; }
  .paper {
    border: none; box-shadow: none; padding: 0;
    background: #fff !important; color: #000 !important;
  }
  .paper * { color: #000 !important; background: transparent !important; }
  .paper-title { font-size: 20pt; margin-bottom: 6pt; }
  .paper-meta { font-size: 9pt; color: #555 !important; }
  .paper-section { break-inside: auto; }
  .paper-section h4 { font-size: 12pt; border-bottom: 1px solid #000; break-after: avoid; page-break-after: avoid; }
  .paper-q { break-inside: avoid; page-break-inside: avoid; margin-bottom: 12pt; }
  .paper-q .pq-options { font-size: 10.5pt; }
  .paper-q .pq-text { font-size: 11pt; }
  @page { margin: 16mm 14mm; }
}

/* ============ Responsive ============ */
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); transition: transform 0.3s; }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; padding: 20px; }
  .kp-layout { grid-template-columns: 1fr; }
  .records-cards { grid-template-columns: 1fr; }
  .paper-q .pq-options { grid-template-columns: 1fr; }
}
