#app { height: 100%; }

/* 侧栏铺满，内容区做成一张浮起的面板（inset 布局） */
.shell {
  display: grid;
  grid-template-columns: var(--sidebar) minmax(0, 1fr);
  /* 手机浏览器地址栏会收起/展开：100vh 是最高的那种情况，会让底部内容滚不出来。
     dvh 跟着可见高度走，老浏览器退回 100vh。 */
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--sidebar-bg);
}

/* ===== 左侧栏 ===== */
.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100vh;
  background: var(--sidebar-bg);
}
.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  height: var(--topbar);
  flex: none;
  padding: 0 16px;
  min-width: 0;
}
.brand-mark {
  width: 24px;
  height: 24px;
  flex: none;
  border-radius: 7px;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--accent);
}
.brand-mark svg { width: 13px; height: 13px; }
.brand-name {
  font-size: 13.5px;
  font-weight: 600;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-sub {
  margin-left: auto;
  flex: none;
  font-family: var(--mono);
  font-size: 9.5px;
  line-height: 15px;
  color: var(--text-mute);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0 4px;
}

.side-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 2px 10px 14px;
  display: flex;
  flex-direction: column;
}
.new-q {
  flex: none;
  height: 32px;
  margin: 2px 0 4px;
  justify-content: center;
  font-size: 12.5px;
  border-radius: var(--radius-sm);
  gap: 7px;
}
.new-q svg { width: 14px; height: 14px; }

/* 侧栏导航分六组：学习 / 积累 / 知识库 / 实验 / 数据 / 系统。
   前两组平铺（每天都点），后四组可折叠（偶尔翻），折叠状态记在 localStorage。
   组之间的间距从 14 收到 10、组标题不再额外占边距，这样展开任意一组时
   「系统」那一组仍然完整可见，不用滚来滚去找「设置」。三组都展开还是要滚，
   那时候内容本来就超过一屏，`.side-body` 自己会滚。 */
.nav { display: flex; flex-direction: column; gap: 10px; margin-top: 4px; }
.nav-group { display: flex; flex-direction: column; gap: 1px; }
.nav-label {
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-mute);
  padding: 8px 8px 5px;
}
.nav a {
  display: flex;
  align-items: center;
  gap: 9px;
  /* 30px 而不是 32px：六组展开一组时，条目加组标题加底部账户区刚好比一屏
     少一点点，不用滚就能看到最下面的「设置」。再矮就挤手了，32 又会溢出。 */
  height: 30px;
  padding: 0 8px;
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 13px;
  transition: background var(--dur-snap) var(--ease-snap),
              color var(--dur-snap) var(--ease-snap);
}
.nav a:hover { background: var(--bg-hover); color: var(--text); }
.nav a.active {
  background: var(--nav-active);
  color: var(--text-strong);
  font-weight: 500;
}
.nav a.active .nav-icon { color: var(--accent-2); opacity: 1; }
.nav-icon {
  width: 15px; height: 15px; flex: none;
  display: inline-flex; align-items: center; justify-content: center;
  opacity: 0.72;
  transition: color var(--dur-snap) var(--ease-snap), opacity var(--dur-snap) var(--ease-snap);
}
.nav-icon svg { width: 15px; height: 15px; }
/* 左侧导航项右上角的数字角标：例如「复习」待复习 n 条。
   内联在那一行最右侧（margin-left:auto），数字≥100 时显示 99+。 */
.nav-count {
  margin-left: auto;
  flex: none;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  line-height: 1;
  font-weight: 600;
  color: var(--text-strong);
  background: var(--accent-2);
  box-shadow: 0 0 0 2px var(--sidebar-bg, var(--bg));
}
/* 换算行：角标横向换行时那一行会显得多出一截，这里让它不换行 */
.nav-count[hidden] { display: none; }
/* 可折叠分组的标题沿用分组标题的字号与颜色，只在左边多一个箭头。
   和「学习 / 积累 / 系统」保持同一条基线，六个组标题看起来是一层的。 */
.nav-label-sub {
  color: var(--text-mute);
  opacity: 0.9;
}

/* —— 可折叠分组（知识库 / 实验 / 数据） ——
   点标题先展开它下面的子项，再点收起。收起状态记在 localStorage，
   下次进来还是同一个状态；进到子页时自动展开。 */
.nav-group-collapsible { margin-top: 0; }
.nav-group-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 8px 8px 5px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text-mute);
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  text-align: left;
  cursor: pointer;
  transition: background var(--dur-snap) var(--ease-snap), color var(--dur-snap) var(--ease-snap);
}
.nav-group-toggle:hover { background: var(--bg-hover); color: var(--text); }
.nav-caret {
  width: 12px; height: 12px;
  flex: none;
  transition: transform var(--dur-snap) var(--ease-snap);
}
.nav-group-collapsible[data-open="1"] .nav-caret { transform: rotate(90deg); }
.nav-sub-items {
  display: none;
  flex-direction: column;
  gap: 1px;
}
.nav-group-collapsible[data-open="1"] .nav-sub-items {
  display: flex;
  /* 展开时把子项整体往右推一格，一眼看出它们属于「知识库」 */
  padding-left: 7px;
}
/* 收起时如果有子页正在显示，父标题高亮：用户不会以为走错了页面 */
.nav-group-collapsible[data-active="1"][data-open="0"] > .nav-group-toggle {
  color: var(--accent-2);
}

.sidebar-foot {
  margin-top: auto;
  flex: none;
  border-top: 1px solid var(--divider);
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.mini-countdown {
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.55;
  color: var(--text-mute);
  padding: 0 2px;
}
.mini-countdown b {
  color: var(--text-strong);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.foot-row { display: flex; gap: 6px; }
.foot-row .btn { flex: 1; min-width: 0; height: 28px; padding: 0 6px; font-size: 12px; }

/* ===== 右侧主面板 ===== */
.main {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  margin: 8px;
  overflow: hidden;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-1);
}
.topbar {
  flex: none;
  z-index: 5;
  height: var(--topbar);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px 0 10px;
  border-bottom: 1px solid var(--divider);
  background: var(--bg);
}
.crumb {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--text-dim);
  min-width: 0;
  white-space: nowrap;
}
.crumb-root { color: var(--text-mute); }
.crumb-sep { color: var(--text-mute); opacity: 0.5; }
.crumb b { color: var(--text-strong); font-weight: var(--heading-weight); font-size: 13px; }
.topbar-right { display: flex; align-items: center; gap: 6px; margin-left: auto; min-width: 0; }

/* 右上角「AI 正在做什么」：一个会呼吸的点 + 任务标题。
   有任务时才是亮色，空闲时压暗，不抢视线；标题超长就省略，
   完整标题放在 title 里，鼠标悬停能看全。 */
.ai-activity {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  max-width: 260px;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--text-dim);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
  flex: none;
  min-width: 0;
}
.ai-activity:hover { border-color: var(--accent-line); color: var(--text); }
.ai-activity.idle { opacity: .72; }
.ai-activity.busy {
  border-color: var(--accent-line);
  color: var(--text-strong);
  background: var(--accent-soft);
}
.ai-activity .ai-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-mute);
  flex: none;
}
.ai-activity.busy .ai-dot { background: var(--accent-2); }
.ai-activity .ai-dot.pulse {
  animation: ai-pulse 1.4s var(--ease) infinite;
}
@keyframes ai-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(167, 139, 250, 0.55); opacity: 1; }
  50% { box-shadow: 0 0 0 5px rgba(167, 139, 250, 0); opacity: .7; }
}
.ai-activity-text {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ai-activity-more {
  flex: none;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--accent-2);
}
/* 右上角「自动后台 AI」总开关：关掉后所有后台自动调用停，只剩手动操作 */
.ai-auto {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex: none;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--text-dim);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
}
.ai-auto:hover { border-color: var(--accent-line); color: var(--text); }
.ai-auto .ai-auto-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--success, #34d399);
  flex: none;
}
.ai-auto.off { color: var(--text-mute); background: transparent; border-style: dashed; }
.ai-auto.off .ai-auto-dot { background: var(--text-mute); }
.ai-auto.off .ai-auto-text { text-decoration: line-through; }
.ai-auto-text { white-space: nowrap; }
/* 已经跑了多久：毫秒级刷新，等宽数字避免宽度跳动把标题挤来挤去 */
.ai-activity .ai-elapsed {
  flex: none;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--accent-2);
  font-variant-numeric: tabular-nums;
  opacity: .9;
}
@media (prefers-reduced-motion: reduce) {
  .ai-activity .ai-dot.pulse { animation: none; }
}
@media (max-width: 1100px) {
  .ai-activity { max-width: 170px; }
  .ai-activity .ai-elapsed { display: none; }
}
@media (max-width: 820px) {
  /* 手机上顶栏本来就挤，只留一个跳动的小点表示 AI 在忙 */
  .ai-activity { max-width: none; padding: 4px 8px; }
  .ai-activity .ai-activity-text, .ai-activity .ai-activity-more, .ai-activity .ai-elapsed { display: none; }
  .ai-activity.idle { display: none; }
}

.view {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 28px 32px 72px;
  scroll-behavior: smooth;
  /* 滑到列表尽头不要再把整页往下拽（安卓会触发下拉刷新，等于重新加载） */
  overscroll-behavior-y: contain;
  /* 底部内边距的数值单独留一份变量：
     悬浮翻页条（.pager-pill）用 bottom: calc(14px - var(--view-pad-bottom))
     把它抵消掉，才能真正贴到视口底部，而不是被这层内边距顶上来 70 多像素。
     以前是写死 14px，结果翻页条停在离屏幕底 95px 的位置（用户说「位置不好」）。 */
  --view-pad-bottom: 72px;
}
/* 内容按一列居中，宽屏下不会被拉得太宽 */
.view > * {
  width: 100%;
  max-width: var(--content-max);
  margin-left: auto;
  margin-right: auto;
}
/* 悬浮按钮不属于正文列：它按视口定位，不能被上面的宽度规则撑开 */
.view > .save-dock {
  width: auto;
  max-width: none;
  margin-left: 0;
  margin-right: 0;
}

body[data-locked="1"] .nav,
body[data-locked="1"] .sidebar-foot,
body[data-locked="1"] .new-q,
body[data-locked="1"] .topbar-right { display: none; }
body[data-locked="1"] .view { display: flex; align-items: flex-start; justify-content: center; }

/* ===== 收起侧栏（手动 + 窄屏自动） ===== */
.shell[data-collapsed="1"] { --sidebar: 68px; }
.shell[data-collapsed="1"] .brand { justify-content: center; padding: 0; }
.shell[data-collapsed="1"] .brand-name,
.shell[data-collapsed="1"] .brand-sub,
.shell[data-collapsed="1"] .nav-label,
.shell[data-collapsed="1"] .sidebar-foot { display: none; }
.shell[data-collapsed="1"] .side-body { padding: 2px 8px 14px; }
.shell[data-collapsed="1"] .nav a { justify-content: center; padding: 0; }
.shell[data-collapsed="1"] .nav a > span:not(.nav-icon) { display: none; }
.shell[data-collapsed="1"] .new-q { justify-content: center; padding: 0; }
.shell[data-collapsed="1"] .new-q > span { display: none; }

@media (max-width: 1080px) {
  .shell { --sidebar: 68px; }
  .brand { justify-content: center; padding: 0; }
  .brand-name, .brand-sub, .nav-label, .sidebar-foot { display: none; }
  .side-body { padding: 2px 8px 14px; }
  .nav a { justify-content: center; padding: 0; }
  .nav a > span:not(.nav-icon) { display: none; }
  .new-q { justify-content: center; padding: 0; }
  .new-q > span { display: none; }
  .view { padding: 22px 20px 52px; --view-pad-bottom: 52px; }
  .topbar { padding: 0 10px; }
}

@media (max-width: 720px) {
  .main { margin: 0; border-radius: 0; border: none; }
  .topbar-right .badge { display: none; }
  .view { padding: 18px 14px 48px; --view-pad-bottom: 48px; }
}

/* ===== 手机端：侧栏变抽屉，主内容占满整屏 ===== */
.nav-backdrop { display: none; }

@media (max-width: 820px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .main { margin: 0; border: 0; border-radius: 0; box-shadow: none; }
  .topbar { padding: 0 10px 0 6px; gap: 8px; }
  .topbar-right .badge { display: none; }
  .crumb { overflow: hidden; text-overflow: ellipsis; }
  .search-btn span, .search-btn kbd { display: none; }
  /* 手机上新增错题最常见（拍张照就行），顶栏留一个只有「＋」的按钮，不用先去开抽屉 */
  .topbar-right #addQuestion { width: 40px; min-height: 40px; padding: 0; gap: 0; }
  .topbar-right #addQuestion .lbl { display: none; }
  .topbar-right #addQuestion > span[aria-hidden] { font-size: 17px; line-height: 1; }
  /* 打字背单词是外站跳转，手机上也收成一个图标，别把顶栏挤爆 */
  .topbar-right #typewordsLink { width: 40px; min-height: 40px; padding: 0; gap: 0; }
  .topbar-right #typewordsLink .lbl { display: none; }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 70;
    width: min(276px, 84vw);
    height: 100vh;
    height: 100dvh;
    padding-bottom: env(safe-area-inset-bottom);
    border-right: 1px solid var(--border);
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.42);
    transform: translateX(-102%);
    transition: transform 0.24s var(--ease-out);
    will-change: transform;
  }
  .shell[data-nav="1"] .sidebar { transform: none; }
  /* 抽屉里恢复文字标签，图标栏只适合桌面 */
  .brand { justify-content: flex-start; padding: 0 16px; }
  .brand-name { display: block; }
  .brand-sub { display: inline-block; }
  .side-body { padding: 4px 10px calc(14px + env(safe-area-inset-bottom)); }
  .new-q { height: 40px; padding: 0 14px; justify-content: center; font-size: 13px; }
  .new-q > span { display: inline; }
  .nav-label { display: block; }
  .nav a { height: 40px; padding: 0 10px; justify-content: flex-start; gap: 10px; font-size: 14px; }
  .nav a > span:not(.nav-icon) { display: inline; }
  .nav-icon, .nav-icon svg { width: 17px; height: 17px; }
  .sidebar-foot { display: flex; padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
  /* 桌面手动收起的状态在手机上不能生效，否则抽屉里只剩图标 */
  .shell[data-collapsed="1"] .brand { justify-content: flex-start; padding: 0 16px; }
  .shell[data-collapsed="1"] .brand-name { display: block; }
  .shell[data-collapsed="1"] .brand-sub { display: inline-block; }
  .shell[data-collapsed="1"] .side-body { padding: 4px 10px calc(14px + env(safe-area-inset-bottom)); }
  .shell[data-collapsed="1"] .new-q { justify-content: center; padding: 0 14px; }
  .shell[data-collapsed="1"] .new-q > span { display: inline; }
  .shell[data-collapsed="1"] .nav-label { display: block; }
  .shell[data-collapsed="1"] .nav a { justify-content: flex-start; padding: 0 10px; }
  .shell[data-collapsed="1"] .nav a > span:not(.nav-icon) { display: inline; }
  .shell[data-collapsed="1"] .sidebar-foot { display: flex; }

  .nav-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 65;
    background: rgba(0, 0, 0, 0.52);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.22s var(--ease-out);
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
  }
  .shell[data-nav="1"] .nav-backdrop { opacity: 1; pointer-events: auto; }

  .view { padding: 20px 16px calc(56px + env(safe-area-inset-bottom)); --view-pad-bottom: 56px; }
}

@media (max-width: 420px) {
  .view { padding: 16px 12px calc(52px + env(safe-area-inset-bottom)); --view-pad-bottom: 52px; }
  .crumb-root, .crumb-sep { display: none; }
}
