/* ========== 主题变量 ========== */
:root {
  --bg: #f4f5fb;
  --bg-soft: #eceef7;
  --card: #ffffff;
  --card-soft: #f8f9ff;
  --text: #1c1e2a;
  --muted: #6b7187;
  --border: #e2e4f0;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --accent-2: #7c3aed;
  --green: #16a34a;
  --green-soft: #e6f7ec;
  --amber: #d97706;
  --amber-soft: #fdf1dc;
  --red: #dc2626;
  --red-soft: #fdeaea;
  --shadow: 0 8px 30px rgba(30, 34, 60, .12);
  --shadow-sm: 0 2px 8px rgba(30, 34, 60, .08);
  --radius: 16px;
  --radius-lg: 22px;
  --nav-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
[data-theme="dark"] {
  --bg: #12141c;
  --bg-soft: #1a1d2a;
  --card: #1e2230;
  --card-soft: #232738;
  --text: #e7e9f2;
  --muted: #98a0b8;
  --border: #2c3145;
  --accent: #818cf8;
  --accent-soft: #232747;
  --accent-2: #a78bfa;
  --green: #4ade80;
  --green-soft: #1a3324;
  --amber: #fbbf24;
  --amber-soft: #382d12;
  --red: #f87171;
  --red-soft: #3a1c1c;
  --shadow: 0 8px 30px rgba(0, 0, 0, .45);
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, .3);
}

/* ========== 基础 ========== */
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font-family: inherit; border: none; background: none; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

.app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 640px;
  margin: 0 auto;
}

/* ========== 顶部栏 ========== */
.app-header {
  background: var(--bg-soft);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top, 0px);
}
.header-inner {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand-logo {
  width: 30px; height: 30px; border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-size: 16px;
}
.brand-title { font-size: 17px; font-weight: 700; letter-spacing: .5px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; background: var(--card); border: 1px solid var(--border);
  color: var(--text);
}

/* ========== 主体 ========== */
.app-main { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 12px 14px calc(var(--nav-h) + var(--safe-b) + 16px); }
.tab-panel { animation: fadeIn .2s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ========== 闪卡页 ========== */
.flash-toolbar { margin-bottom: 10px; }
.seg {
  display: flex; background: var(--bg-soft); border: 1px solid var(--border);
  border-radius: 12px; padding: 3px; margin-bottom: 10px;
}
.seg-btn {
  flex: 1; padding: 8px 0; border-radius: 9px; font-size: 14px; font-weight: 600; color: var(--muted);
}
.seg-btn.active { background: var(--card); color: var(--accent); box-shadow: var(--shadow-sm); }

.cat-scroll {
  display: flex; gap: 8px; overflow-x: auto; padding: 2px;
  scrollbar-width: none; -ms-overflow-style: none;
}
.cat-scroll::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0; padding: 6px 14px; border-radius: 999px;
  background: var(--card); border: 1px solid var(--border);
  font-size: 13px; color: var(--muted);
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.flash-progress { display: flex; align-items: center; gap: 10px; margin: 12px 2px 14px; }
.fp-track { flex: 1; height: 6px; background: var(--bg-soft); border-radius: 3px; overflow: hidden; }
.fp-bar { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), var(--accent-2)); border-radius: 3px; transition: width .3s ease; }
.fp-text { font-size: 12px; color: var(--muted); white-space: nowrap; }

/* 卡片 */
.card-stage { height: 52vh; perspective: 1400px; margin-bottom: 14px; }
.flashcard { width: 100%; height: 100%; cursor: pointer; }
.fc-inner {
  position: relative; width: 100%; height: 100%;
  transform-style: preserve-3d; transition: transform .55s cubic-bezier(.4,.1,.2,1);
}
.flashcard.flipped .fc-inner { transform: rotateY(180deg); }
.fc-face {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  border-radius: var(--radius-lg); background: var(--card); border: 1px solid var(--border);
  box-shadow: var(--shadow); overflow-y: auto; overflow-x: hidden; padding: 22px;
  display: flex; flex-direction: column;
}
.fc-back { transform: rotateY(180deg); }

.fc-front { justify-content: center; align-items: center; text-align: center; gap: 14px; }
.fc-title { font-size: 26px; font-weight: 800; line-height: 1.3; word-break: break-word; }
.fc-cats { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.fc-cat {
  font-size: 11px; padding: 3px 10px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); font-weight: 600;
}
.fc-cat.sub { background: var(--bg-soft); color: var(--muted); }
.fc-teaser { font-size: 14px; color: var(--muted); max-width: 88%; }
.fc-hint { font-size: 12px; color: var(--muted); opacity: .75; margin-top: 8px; }

.fc-back-foot {
  margin-top: auto; padding-top: 14px; display: flex; align-items: center; justify-content: space-between;
  border-top: 1px dashed var(--border);
}
.fc-back-foot .mini-btn {
  font-size: 13px; color: var(--accent); background: var(--accent-soft);
  padding: 7px 14px; border-radius: 9px; font-weight: 600;
}

/* 操作按钮 */
.flash-actions { display: flex; gap: 10px; }
.act-btn {
  flex: 1; padding: 14px 0; border-radius: 14px; font-size: 16px; font-weight: 700;
  color: #fff; box-shadow: var(--shadow-sm); transition: transform .08s ease;
}
.act-btn:active { transform: scale(.96); }
.act-forgot { background: var(--red); }
.act-unsure { background: var(--amber); }
.act-known { background: var(--green); }

/* AI 模式按钮 */
.ai-row { display: flex; gap: 10px; margin-top: 10px; }
.ai-btn {
  flex: 1; padding: 12px 0; border-radius: 13px; font-size: 14px; font-weight: 700;
  background: var(--card); border: 1px solid var(--border); color: var(--text);
  box-shadow: var(--shadow-sm); transition: transform .08s ease;
}
.ai-btn:active { transform: scale(.97); }
.ai-btn:first-child { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }

/* AI 对话面板 */
.ai-panel {
  position: fixed; inset: 0; z-index: 60; background: var(--bg);
  display: flex; flex-direction: column; max-width: 640px; margin: 0 auto;
  height: 100vh; height: 100dvh;
}
.ai-head { flex: 1; display: flex; align-items: center; gap: 8px; min-width: 0; }
.ai-badge {
  flex-shrink: 0; font-size: 11px; font-weight: 700; padding: 3px 9px; border-radius: 999px;
  background: var(--accent); color: #fff;
}
.ai-badge.teach { background: var(--green); }
.ai-subj { font-size: 14px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.ai-messages { flex: 1; overflow-y: auto; padding: 14px 14px 6px; display: flex; flex-direction: column; gap: 10px; }
.msg { display: flex; gap: 8px; }
.msg .avatar {
  width: 30px; height: 30px; border-radius: 9px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 15px;
}
.msg.ai .avatar { background: var(--accent-soft); }
.msg.user .avatar { background: var(--green-soft); }
.msg .bubble {
  max-width: 82%; padding: 10px 13px; border-radius: 13px; font-size: 14px; line-height: 1.65;
  white-space: pre-wrap; word-break: break-word;
}
.msg.ai .bubble { background: var(--card); border: 1px solid var(--border); border-top-left-radius: 4px; }
.msg.user { flex-direction: row-reverse; }
.msg.user .bubble { background: var(--accent); color: #fff; border-top-right-radius: 4px; }
.msg .bubble.error { color: var(--red); background: var(--red-soft); border-color: var(--red); }
.msg.thinking .bubble::after {
  content: "…"; animation: blink 1s infinite;
}
@keyframes blink { 50% { opacity: .25; } }

.ai-hint { padding: 0 16px 6px; font-size: 12px; color: var(--muted); text-align: center; }
.ai-input-bar {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px;
  padding-bottom: calc(10px + var(--safe-b));
  background: var(--card); border-top: 1px solid var(--border);
}
.ai-mic, .ai-finish, .ai-send {
  flex-shrink: 0; height: 42px; border-radius: 12px; font-size: 14px; font-weight: 700;
}
.ai-mic { width: 44px; background: var(--bg-soft); border: 1px solid var(--border); }
.ai-mic.listening { background: var(--red); color: #fff; border-color: var(--red); }
.ai-finish { padding: 0 14px; background: var(--green-soft); color: var(--green); border: 1px solid var(--green); }
.ai-input {
  flex: 1; min-height: 42px; max-height: 120px; resize: none;
  padding: 11px 13px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--bg-soft); color: var(--text); font-size: 14px; line-height: 1.4;
  outline: none; font-family: inherit;
}
.ai-input:focus { border-color: var(--accent); background: var(--card); }
.ai-send { padding: 0 18px; background: var(--accent); color: #fff; }
.ai-send:disabled { opacity: .5; }

/* AI 气泡内 Markdown 紧凑排版 */
.msg .bubble.markdown-body { white-space: normal; font-size: 14px; }
.msg .bubble.markdown-body p { margin: 4px 0; }
.msg .bubble.markdown-body h1,
.msg .bubble.markdown-body h2,
.msg .bubble.markdown-body h3,
.msg .bubble.markdown-body h4 { font-size: 15px; margin: 8px 0 4px; padding: 0; border: none; }
.msg .bubble.markdown-body h2 { border-left: 3px solid var(--accent); padding-left: 6px; }
.msg .bubble.markdown-body ul, .msg .bubble.markdown-body ol { margin: 4px 0; padding-left: 18px; }
.msg .bubble.markdown-body li { margin: 2px 0; }
.msg .bubble.markdown-body pre { margin: 6px 0; padding: 8px 10px; font-size: 12px; }
.msg .bubble.markdown-body table { font-size: 12px; margin: 6px 0; }
.msg .bubble.markdown-body blockquote { margin: 6px 0; padding: 4px 10px; }
.msg.user .bubble { white-space: pre-wrap; }

/* 空状态 */
.flash-empty { text-align: center; padding: 40px 0; }
.empty-emoji { font-size: 44px; }
.empty-title { font-size: 18px; font-weight: 700; margin: 10px 0 4px; }
.empty-sub { font-size: 14px; color: var(--muted); margin-bottom: 18px; }
.ghost-btn {
  padding: 11px 22px; border-radius: 12px; font-size: 15px; font-weight: 600;
  background: var(--accent-soft); color: var(--accent);
}

/* ========== 手册页 ========== */
.search-wrap { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 4px 0 10px; }
.search-input {
  width: 100%; padding: 12px 16px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--card); color: var(--text); font-size: 15px; outline: none;
}
.search-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search-input::placeholder { color: var(--muted); }

.tree-group { margin-bottom: 14px; }
.tree-group-head {
  display: flex; align-items: center; gap: 8px; padding: 8px 4px;
  font-size: 15px; font-weight: 700;
}
.tree-group-head .count { font-size: 12px; color: var(--muted); font-weight: 500; margin-left: auto; }
.tree-card {
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border: 1px solid var(--border); border-radius: 12px;
  padding: 11px 14px; margin-bottom: 8px; cursor: pointer;
}
.tree-card:active { background: var(--accent-soft); }
.tree-card .tc-icon {
  width: 30px; height: 30px; border-radius: 8px; flex-shrink: 0;
  background: var(--accent-soft); color: var(--accent);
  display: flex; align-items: center; justify-content: center; font-size: 14px;
}
.tree-card .tc-main { flex: 1; min-width: 0; }
.tree-card .tc-title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tree-card .tc-sub { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tree-card .tc-fav { font-size: 13px; color: var(--amber); }
.tree-empty { text-align: center; color: var(--muted); padding: 40px 0; font-size: 14px; }

/* ========== 收藏页 ========== */
.list-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 4px 10px; font-size: 15px; font-weight: 700; }
.list-count { font-size: 13px; color: var(--muted); font-weight: 500; }

/* ========== 统计页 ========== */
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }
.stat-box {
  background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 14px;
}
.stat-box .num { font-size: 26px; font-weight: 800; color: var(--accent); }
.stat-box .lbl { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stats-head { font-size: 15px; font-weight: 700; margin: 16px 0 10px; }
.cat-progress { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 4px 14px; margin-bottom: 8px; }
.cat-row { display: flex; align-items: center; gap: 10px; padding: 9px 0; }
.cat-row .c-name { width: 40%; font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cat-row .c-track { flex: 1; height: 6px; background: var(--bg-soft); border-radius: 3px; overflow: hidden; }
.cat-row .c-fill { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); border-radius: 3px; }
.cat-row .c-num { width: 70px; text-align: right; font-size: 11px; color: var(--muted); }
.danger-zone { margin-top: 20px; text-align: center; }
.danger-btn {
  padding: 10px 20px; border-radius: 12px; font-size: 14px; font-weight: 600;
  color: var(--red); background: var(--red-soft); border: 1px solid var(--red);
}

/* ========== 底部导航 ========== */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  max-width: 640px; margin: 0 auto;
  display: flex; background: var(--card); border-top: 1px solid var(--border);
  padding-bottom: var(--safe-b);
}
.nav-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 6px; color: var(--muted);
}
.nav-btn.active { color: var(--accent); }
.nav-icon { font-size: 20px; line-height: 1; }
.nav-label { font-size: 11px; }

/* ========== 阅读器 ========== */
.reader {
  position: fixed; inset: 0; z-index: 50; background: var(--bg);
  display: flex; flex-direction: column; max-width: 640px; margin: 0 auto;
  height: 100vh; height: 100dvh;
}
.reader-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: var(--card); border-bottom: 1px solid var(--border);
}
.reader-back { font-size: 26px; line-height: 1; }
.reader-title {
  flex: 1; font-size: 15px; font-weight: 700;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.reader-fav { color: var(--amber); }
.reader-content { flex: 1; overflow-y: auto; padding: 18px 18px 30px; }
.reader-nav {
  display: flex; gap: 10px; padding: 10px 14px calc(14px + var(--safe-b));
  background: var(--card); border-top: 1px solid var(--border);
}
.reader-nav button {
  flex: 1; padding: 11px 0; border-radius: 11px; font-size: 14px; font-weight: 600;
  background: var(--accent-soft); color: var(--accent);
}
.reader-nav button:disabled { opacity: .35; }
.reader-ai {
  display: flex; gap: 10px; padding: 8px 14px 4px; background: var(--card);
  border-top: 1px solid var(--border);
}
.reader-ai button {
  flex: 1; padding: 10px 0; border-radius: 11px; font-size: 13px; font-weight: 700;
  background: var(--accent-soft); color: var(--accent); border: 1px solid var(--accent);
}
.reader-ai button:last-child { background: var(--green-soft); color: var(--green); border-color: var(--green); }

/* ========== Markdown 内容样式 ========== */
.markdown-body { font-size: 15px; word-break: break-word; }
.markdown-body h1 { font-size: 22px; margin: 4px 0 12px; }
.markdown-body h2 { font-size: 18px; margin: 20px 0 8px; padding-left: 10px; border-left: 4px solid var(--accent); }
.markdown-body h3 { font-size: 16px; margin: 16px 0 6px; }
.markdown-body h4 { font-size: 15px; margin: 14px 0 6px; }
.markdown-body p { margin: 8px 0; }
.markdown-body ul, .markdown-body ol { margin: 8px 0; padding-left: 22px; }
.markdown-body li { margin: 4px 0; }
.markdown-body strong { color: var(--accent); font-weight: 700; }
.markdown-body a { color: var(--accent-2); text-decoration: none; border-bottom: 1px dashed var(--accent-2); }
.markdown-body hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.markdown-body blockquote {
  margin: 10px 0; padding: 8px 14px; border-left: 4px solid var(--accent);
  background: var(--accent-soft); border-radius: 0 10px 10px 0; color: var(--muted);
}
.markdown-body code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .86em; background: var(--bg-soft); border: 1px solid var(--border);
  padding: 2px 6px; border-radius: 6px;
}
.markdown-body pre {
  background: #0f1420; color: #e6edf3; border-radius: 12px; padding: 14px; margin: 12px 0;
  overflow-x: auto; -webkit-overflow-scrolling: touch;
}
[data-theme="dark"] .markdown-body pre { background: #0b0e16; border: 1px solid var(--border); }
.markdown-body pre code { background: none; border: none; padding: 0; font-size: 13px; line-height: 1.55; color: inherit; }
.markdown-body table {
  width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 13px;
  display: block; overflow-x: auto;
}
.markdown-body th, .markdown-body td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; }
.markdown-body th { background: var(--bg-soft); font-weight: 700; white-space: nowrap; }
.markdown-body img { max-width: 100%; border-radius: 10px; }
.markdown-body sup { font-size: 11px; color: var(--muted); }
.markdown-body input[type="checkbox"] { margin-right: 6px; }

/* 阅读器内的表格更紧凑 */
.markdown-body > table { display: table; }

@media (min-width: 641px) {
  .app { border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
  .reader { border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
}
