/* ==========================================================================
   离散数学助手 · 移动端样式
   设计原则：
     · 移动优先，单栏；底部 3 个 tab，拇指可达
     · 触控目标 ≥ 44px，输入框字号 ≥ 16px（否则 iOS 会自动放大页面）
     · 用 100dvh 而不是 100vh，避免手机浏览器地址栏收起时布局跳动
     · 安全区适配（iPhone 刘海/home 指示条）
     · 表格与代码块允许横向滚动，绝不撑破页面
   ========================================================================== */

:root {
  --brand: #4f46e5;
  --brand-dark: #4338ca;
  --brand-soft: #eef2ff;
  --bg: #f8fafc;
  --card: #ffffff;
  --text: #0f172a;
  --text-2: #475569;
  --text-3: #94a3b8;
  --border: #e2e8f0;
  --danger: #dc2626;
  --ok: #16a34a;
  --radius: 14px;
  --doc-font-size: 16px;

  --topbar-h: 52px;
  --tabbar-h: 56px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Microsoft YaHei',
    'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: var(--doc-font-size);
  line-height: 1.7;
  overscroll-behavior-y: none;
  -webkit-text-size-adjust: 100%;
}

/* ---------- 顶部栏 ---------- */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 30;
  height: calc(var(--topbar-h) + var(--safe-top));
  padding: var(--safe-top) 12px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(135deg, #4f46e5, #6366f1);
  color: #fff;
  box-shadow: 0 1px 8px rgba(15, 23, 42, 0.12);
}

.topbar-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 600;
  min-width: 0;
}

.topbar-title .logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.22);
  font-size: 15px;
  font-weight: 700;
  flex: none;
}

#topbar-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: none;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.5);
  transition: background 0.2s;
}

.status-dot.ok {
  background: #4ade80;
  box-shadow: 0 0 0 3px rgba(74, 222, 128, 0.25);
}

.status-dot.bad {
  background: #f87171;
  box-shadow: 0 0 0 3px rgba(248, 113, 113, 0.25);
}

.icon-btn {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  font-size: 15px;
}

.icon-btn:active {
  background: rgba(255, 255, 255, 0.28);
}

/* ---------- 主内容区 ---------- */
.viewport {
  position: fixed;
  top: calc(var(--topbar-h) + var(--safe-top));
  left: 0;
  right: 0;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom));
  overflow: hidden;
}

.view {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 14px 20px;
}

.view[hidden] {
  display: none !important;
}

/* 阅读视图里操作条要盖住内容，留出底部空间 */
#view-reader {
  padding-bottom: 72px;
}

/* ---------- 目录 ---------- */
.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.search-box[hidden] {
  display: none !important;
}

#search-input {
  flex: 1;
  min-width: 0;
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
  font-size: 16px; /* 必须 ≥16px，否则 iOS 聚焦时会放大整页 */
  color: var(--text);
  outline: none;
}

#search-input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}

.text-btn {
  border: 0;
  background: transparent;
  color: var(--brand);
  font-size: 15px;
  padding: 8px 4px;
}

.chapter {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
  overflow: hidden;
}

.chapter-head {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 14px 14px;
  border: 0;
  background: transparent;
  text-align: left;
  font-size: 15px;
  color: var(--text);
  min-height: 52px;
}

.chapter-head:active {
  background: var(--brand-soft);
}

.chapter-code {
  flex: none;
  font-size: 12px;
  font-weight: 700;
  color: var(--brand);
  background: var(--brand-soft);
  border-radius: 6px;
  padding: 2px 7px;
  letter-spacing: 0.3px;
}

.chapter-title {
  flex: 1;
  font-weight: 600;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chapter-count {
  flex: none;
  font-size: 12px;
  color: var(--text-3);
}

.chapter-arrow {
  flex: none;
  font-size: 12px;
  color: var(--text-3);
  transition: transform 0.18s;
}

.chapter[data-open='true'] .chapter-arrow {
  transform: rotate(90deg);
}

.doc-list {
  border-top: 1px solid var(--border);
  display: none;
}

.chapter[data-open='true'] .doc-list {
  display: block;
}

.doc-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 13px 14px 13px 18px;
  border: 0;
  border-bottom: 1px solid #f1f5f9;
  background: transparent;
  text-align: left;
  font-size: 14.5px;
  color: var(--text-2);
  min-height: 48px;
}

.doc-item:last-child {
  border-bottom: 0;
}

.doc-item:active {
  background: var(--brand-soft);
}

.doc-item.is-active {
  color: var(--brand);
  background: var(--brand-soft);
  font-weight: 600;
}

.doc-badge {
  flex: none;
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 5px;
  background: #f1f5f9;
  color: var(--text-3);
}

.doc-badge.kp {
  background: var(--brand-soft);
  color: var(--brand);
}

.doc-item-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 搜索结果 */
.sr-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 9px;
}

.sr-item:active {
  background: var(--brand-soft);
}

.sr-path {
  font-size: 12px;
  color: var(--brand);
  margin-bottom: 4px;
}

.sr-snippet {
  font-size: 13.5px;
  color: var(--text-2);
  line-height: 1.6;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.sr-snippet mark {
  background: #fef08a;
  color: inherit;
  padding: 0 1px;
}

.sr-none {
  text-align: center;
  color: var(--text-3);
  font-size: 14px;
  padding: 40px 0;
}

/* ---------- 阅读 ---------- */
.reader-crumb {
  font-size: 12.5px;
  color: var(--text-3);
  margin-bottom: 10px;
  padding-left: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 15px 22px;
  font-size: var(--doc-font-size);
  line-height: 1.78;
  overflow-wrap: break-word;
  word-break: break-word;
}

.doc:empty {
  display: none;
}

/* 正文内的元素（渲染出来的是内联样式，这里补移动端约束） */
.doc h1 {
  font-size: 1.32em;
  line-height: 1.45;
  margin: 18px 0 10px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--brand-soft);
}

.doc h2 {
  font-size: 1.16em;
  margin: 18px 0 8px;
  color: var(--text);
}

.doc h3 {
  font-size: 1.05em;
  margin: 14px 0 6px;
}

.doc p {
  margin: 8px 0;
}

.doc ul,
.doc ol {
  padding-left: 20px;
  margin: 8px 0;
}

.doc li {
  margin: 4px 0;
}

.doc blockquote {
  margin: 10px 0;
}

/* 表格：允许横向滚动，不撑破屏幕 */
.doc table {
  display: block;
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-collapse: collapse;
  font-size: 0.92em;
}

.doc pre {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 8px;
  font-size: 0.86em;
}

.doc code {
  font-size: 0.92em;
}

/* KaTeX 公式允许横向滚动（长公式不换行） */
.doc .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 6px 0;
}

.doc .katex {
  font-size: 1.02em;
}

.reader-placeholder {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-3);
}

.reader-placeholder[hidden] {
  display: none !important;
}

.ph-emoji {
  font-size: 44px;
  margin-bottom: 10px;
}

.primary-btn {
  margin-top: 14px;
  height: 44px;
  padding: 0 22px;
  border: 0;
  border-radius: 12px;
  background: var(--brand);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
}

.primary-btn:active {
  background: var(--brand-dark);
}

.loading-box {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 40px 0;
  color: var(--text-3);
  font-size: 14px;
}

.loading-box[hidden] {
  display: none !important;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--brand);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  display: inline-block;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 阅读视图底部操作条 */
.reader-actions {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom));
  z-index: 25;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: linear-gradient(to top, var(--bg) 60%, rgba(248, 250, 252, 0));
}

.reader-actions[hidden] {
  display: none !important;
}

.action-btn {
  flex: 1;
  height: 44px;
  border: 0;
  border-radius: 12px;
  background: var(--brand);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.28);
}

.action-btn:active {
  background: var(--brand-dark);
}

.font-group {
  display: flex;
  gap: 8px;
}

.font-btn {
  height: 44px;
  min-width: 52px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
  color: var(--text-2);
  font-size: 14px;
  font-weight: 600;
}

.font-btn:active {
  background: var(--brand-soft);
  color: var(--brand);
}

/* ---------- 问答 ---------- */
#view-chat {
  padding-bottom: 8px;
}

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-bottom: 8px;
}

.msg {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.msg.user {
  flex-direction: row-reverse;
}

.avatar {
  flex: none;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  margin-top: 2px;
}

.msg.ai .avatar {
  background: var(--brand-soft);
  color: var(--brand);
}

.msg.user .avatar {
  background: var(--brand);
  color: #fff;
}

.bubble {
  max-width: calc(100% - 46px);
  padding: 11px 13px;
  border-radius: 14px;
  font-size: 15px;
  line-height: 1.72;
  overflow-wrap: break-word;
  word-break: break-word;
}

.msg.ai .bubble {
  background: var(--card);
  border: 1px solid var(--border);
  border-top-left-radius: 4px;
  font-size: var(--doc-font-size);
}

.msg.user .bubble {
  background: var(--brand);
  color: #fff;
  border-top-right-radius: 4px;
  white-space: pre-wrap;
}

/* AI 气泡里的 Markdown */
.bubble h1,
.bubble h2,
.bubble h3 {
  font-size: 1.1em;
  margin: 12px 0 6px;
}

.bubble p {
  margin: 6px 0;
}

.bubble ul,
.bubble ol {
  padding-left: 18px;
  margin: 6px 0;
}

.bubble table {
  display: block;
  width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
  font-size: 0.9em;
}

.bubble pre {
  overflow-x: auto;
  border-radius: 8px;
  font-size: 0.85em;
}

.bubble .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 0;
}

.msg-sources {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
  font-size: 11.5px;
  color: var(--text-3);
  line-height: 1.7;
}

.msg-sources b {
  color: var(--brand);
  font-weight: 600;
}

.msg-error {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  background: #fef2f2;
  color: var(--danger);
  font-size: 13px;
  line-height: 1.6;
}

.typing {
  display: inline-flex;
  gap: 3px;
  align-items: center;
  height: 20px;
}

.typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-3);
  animation: bounce 1.2s infinite;
}

.typing i:nth-child(2) {
  animation-delay: 0.15s;
}

.typing i:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes bounce {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.45;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

.chat-empty {
  text-align: center;
  padding: 32px 6px;
}

.chat-empty[hidden] {
  display: none !important;
}

.chat-empty h1 {
  font-size: 19px;
  margin: 6px 0 4px;
}

.muted {
  color: var(--text-3);
  font-size: 13.5px;
  line-height: 1.7;
}

.chips {
  display: flex;
  flex-direction: column;
  gap: 9px;
  margin-top: 20px;
}

.chip {
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 14px;
  color: var(--text-2);
  text-align: left;
  line-height: 1.5;
  min-height: 44px;
}

.chip:active {
  background: var(--brand-soft);
  border-color: #c7d2fe;
  color: var(--brand);
}

/* 问答输入条 */
.composer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom));
  z-index: 26;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px;
  background: var(--card);
  border-top: 1px solid var(--border);
}

.composer[hidden] {
  display: none !important;
}

#chat-input {
  flex: 1;
  min-width: 0;
  max-height: 108px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  font-size: 16px; /* 防止 iOS 聚焦放大 */
  line-height: 1.5;
  color: var(--text);
  resize: none;
  outline: none;
  font-family: inherit;
}

#chat-input:focus {
  border-color: var(--brand);
  background: var(--card);
}

.send-btn {
  flex: none;
  height: 42px;
  padding: 0 18px;
  border: 0;
  border-radius: 12px;
  background: var(--brand);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
}

.send-btn:active {
  background: var(--brand-dark);
}

.send-btn:disabled {
  background: #cbd5e1;
}

.send-btn.stop {
  background: var(--danger);
}

/* ---------- 底部 tab ---------- */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: flex;
  background: var(--card);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 10px rgba(15, 23, 42, 0.05);
}

.tab {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text-3);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 11px;
  padding: 0;
  transition: color 0.15s;
}

.tab.is-active {
  color: var(--brand);
  font-weight: 600;
}

.tab:active {
  background: var(--brand-soft);
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 24px);
  transform: translateX(-50%);
  z-index: 60;
  background: rgba(15, 23, 42, 0.9);
  color: #fff;
  font-size: 13.5px;
  padding: 9px 16px;
  border-radius: 10px;
  max-width: 80vw;
  text-align: center;
  line-height: 1.5;
}

.toast[hidden] {
  display: none !important;
}

.empty-tip {
  text-align: center;
  color: var(--text-3);
  font-size: 14px;
  padding: 50px 16px;
  line-height: 1.8;
}

.empty-tip code {
  background: #f1f5f9;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.92em;
}

/* ---------- 深色模式 ---------- */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --card: #1e293b;
    --text: #e2e8f0;
    --text-2: #cbd5e1;
    --text-3: #94a3b8;
    --border: #334155;
    --brand-soft: #312e81;
  }

  .tabbar,
  .composer {
    background: #1e293b;
  }

  .doc-badge {
    background: #334155;
  }

  .sr-snippet mark {
    background: #a16207;
    color: #fff;
  }

  .msg-error {
    background: #451a1a;
  }

  .empty-tip code {
    background: #334155;
  }
}

/* ---------- 横屏 / 大屏微调 ---------- */
@media (min-width: 640px) {
  .view {
    padding-left: max(14px, calc((100vw - 640px) / 2));
    padding-right: max(14px, calc((100vw - 640px) / 2));
  }

  .doc,
  .bubble {
    font-size: 16.5px;
  }
}

/* ---------- 多教材：顶部教材选择器 ---------- */
.subject-host {
  flex: 0 1 auto;
  min-width: 0;
  margin-left: 10px;
}

.subject-picker {
  position: relative;
  display: inline-flex;
  align-items: center;
  height: 30px;
  padding: 0 22px 0 10px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.2);
  max-width: 132px;
}

.subject-picker-label {
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}

/* 原生 select 覆盖整个胶囊：点哪儿都能唤起系统选择器 */
.subject-picker select {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  border: 0;
  padding: 0;
  font-size: 16px; /* 防 iOS 聚焦放大 */
  appearance: none;
  background: transparent;
  cursor: pointer;
}

.subject-picker-caret {
  position: absolute;
  right: 8px;
  font-size: 9px;
  color: rgba(255, 255, 255, 0.85);
  pointer-events: none;
}

/* 窄屏（小手机）下隐藏标题文字，只留教材名，避免挤成一团 */
@media (max-width: 360px) {
  #topbar-text {
    display: none;
  }
}

/* ---------- 互动实验室视图 ---------- */
/* 内嵌 iframe 撑满整个视图区（顶栏与底部 tab 之间），不留内边距 */
.view-lab {
  padding: 0 !important;
  overflow: hidden;
}

.lab-frame {
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
  background: var(--bg);
}

/* ---------- 问答模式切换（问 AI / 问老师） ---------- */
.ask-mode {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 58px);
  z-index: 27;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px 12px 6px;
  background: var(--card);
  border-top: 1px solid var(--border);
}

.ask-mode[hidden] {
  display: none !important;
}

.mode-btn {
  height: 32px;
  padding: 0 13px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--text-2);
  font-size: 13px;
  cursor: pointer;
}

.mode-btn.is-on {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
  font-weight: 600;
}

.mode-wall {
  margin-left: auto;
  font-size: 12.5px;
  color: var(--brand);
  text-decoration: none;
  white-space: nowrap;
}

/* 有模式栏时，聊天视图底部要多留出它的高度 */
#view-chat.has-mode {
  padding-bottom: 44px;
}

/* ---------- 华丽升级：主题色 + 回答卡片化 ---------- */
.topbar {
  background: linear-gradient(135deg, var(--brand), var(--brand-2, #6366f1)) !important;
}

.bubble h2 {
  margin: 12px 0 6px;
  padding: 5px 9px;
  font-size: 0.95em;
  border-left: 3px solid var(--brand);
  border-radius: 6px;
  background: var(--brand-soft, #eef2ff);
  color: var(--brand);
}

/* ---------- 华丽升级 · 质感层（手机版） ---------- */

/* 背景光晕：跟随教材主题色 */
body {
  background-image:
    radial-gradient(520px 320px at 10% -6%, var(--brand-soft, #eef2ff) 0%, transparent 65%),
    radial-gradient(420px 300px at 95% 2%, color-mix(in srgb, var(--brand, #4f46e5) 14%, transparent) 0%, transparent 60%);
  background-attachment: fixed;
}

/* 顶栏玻璃感 */
.topbar {
  backdrop-filter: saturate(150%) blur(8px);
  -webkit-backdrop-filter: saturate(150%) blur(8px);
}

/* 卡片悬浮 / 按压反馈 */
.chapter,
.sr-item,
.qitem,
.chip,
.doc {
  transition: transform .16s cubic-bezier(.4,0,.2,1), box-shadow .16s, border-color .16s;
}
.chapter:active,
.chip:active,
.sr-item:active,
.tab:active {
  transform: scale(.985);
}

/* 视图切换淡入 */
@keyframes view-in {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}
.view:not([hidden]) {
  animation: view-in .2s cubic-bezier(.4,0,.2,1) both;
}

/* 回答气泡逐条淡入 */
@keyframes bubble-in {
  from { opacity: 0; transform: translateY(6px) scale(.99); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.msg {
  animation: bubble-in .22s cubic-bezier(.4,0,.2,1) both;
}

/* 细滚动条 */
* { scrollbar-width: thin; scrollbar-color: color-mix(in srgb, var(--brand, #4f46e5) 40%, transparent) transparent; }
*::-webkit-scrollbar { width: 6px; height: 6px; }
*::-webkit-scrollbar-thumb {
  background: color-mix(in srgb, var(--brand, #4f46e5) 34%, transparent);
  border-radius: 3px;
}

::selection { background: color-mix(in srgb, var(--brand, #4f46e5) 24%, transparent); }

@media (prefers-reduced-motion: reduce) {
  .view, .msg { animation: none !important; }
}

/* ==========================================================
   手机版闯关启动页（背景用学校照片）
   ========================================================== */
.m-landing {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(28px + env(safe-area-inset-top, 0px)) 18px calc(28px + env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  background:
    radial-gradient(520px 360px at 12% -8%, var(--brand-soft, #eef2ff) 0%, transparent 62%),
    linear-gradient(160deg, #fbfbff 0%, #f3f3fa 100%);
}

.m-landing[hidden] { display: none !important; }

/* 照片层：没有图片时自动不绘制 */
.m-landing-bg {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image: url('/assets/cuc/bg.jpg');
  background-size: cover;
  background-position: center 40%;
  background-repeat: no-repeat;
  opacity: .26;
  -webkit-mask-image: linear-gradient(180deg, rgba(0,0,0,.5), rgba(0,0,0,1) 30%, rgba(0,0,0,1) 70%, rgba(0,0,0,.5));
  mask-image: linear-gradient(180deg, rgba(0,0,0,.5), rgba(0,0,0,1) 30%, rgba(0,0,0,1) 70%, rgba(0,0,0,.5));
}

.m-landing-inner {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 420px;
  text-align: center;
}

.m-landing-school {
  display: inline-block;
  margin: 0 0 12px;
  padding: 5px 13px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  color: var(--brand, #4f46e5);
  background: color-mix(in srgb, var(--brand, #4f46e5) 10%, #fff);
  border: 1px solid color-mix(in srgb, var(--brand, #4f46e5) 22%, transparent);
}

.m-landing-title {
  margin: 0;
  font-size: 26px;
  font-weight: 800;
  color: var(--text, #0f172a);
}

.m-landing-sub {
  margin: 10px 0 22px;
  font-size: 13px;
  line-height: 1.8;
  color: var(--text-2, #475569);
}

.m-landing-cards {
  display: flex;
  flex-direction: column;
  gap: 11px;
  text-align: left;
}

.m-landing-cards .subject-card {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 15px 15px 14px;
  border: 1px solid var(--border, #e2e8f0);
  border-radius: 14px;
  background: rgba(255, 255, 255, .9);
  position: relative;
  overflow: hidden;
  text-align: left;
  min-height: 74px;
}

.m-landing-cards .subject-card::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 4px;
  background: var(--sc, var(--brand));
}

.m-landing-cards .subject-card:active:not(:disabled) {
  transform: scale(.985);
}

.m-landing-cards .subject-card-icon {
  font-size: 20px;
  font-weight: 700;
  color: var(--sc, var(--brand));
  line-height: 1.1;
}

.m-landing-cards .subject-card-name {
  font-size: 16px;
  font-weight: 700;
  color: var(--text, #0f172a);
}

.m-landing-cards .subject-card-meta {
  font-size: 12px;
  color: var(--text-3, #94a3b8);
}

.m-landing-cards .subject-card.is-coming {
  opacity: .55;
  filter: grayscale(.5);
}

.m-landing-foot {
  margin: 18px 0 0;
  font-size: 11.5px;
  line-height: 1.7;
  color: var(--text-3, #94a3b8);
}

/* ==========================================================
   视图过渡（View Transitions）：tab 切换的原生级交叉淡入
   不支持该 API 的浏览器完全忽略这些规则
   ========================================================== */
::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: .22s;
  animation-timing-function: cubic-bezier(.4, 0, .2, 1);
}

/* 顶栏和底栏保持不动，避免整页"呼吸" */
.topbar,
.tabbar,
.composer {
  view-transition-name: none;
}

@media (prefers-reduced-motion: reduce) {
  ::view-transition-old(root),
  ::view-transition-new(root) {
    animation: none !important;
  }
}

/* ---------- ② 流式打字光标（手机版） ---------- */
@keyframes m-caret-blink {
  0%, 49%   { opacity: 1; }
  50%, 100% { opacity: 0; }
}

.msg[data-streaming="1"] .bubble::after {
  content: '▍';
  display: inline;
  margin-left: 1px;
  color: var(--brand, #4f46e5);
  animation: m-caret-blink 1s steps(1, end) infinite;
}

@media (prefers-reduced-motion: reduce) {
  .msg[data-streaming="1"] .bubble::after {
    animation: none;
    opacity: .55;
  }
}

/* 拍照按钮：诊断模式才显示 */
.mode-btn.photo-btn {
  background: #f0fdf4;
  border-color: #bbf7d0;
  color: #15803d;
}

.mode-btn.photo-btn:active {
  background: #dcfce7;
}

/* AI 回答下方的小操作条（复制 / 导出） */
.m-acts {
  display: flex;
  gap: 8px;
  margin: 4px 0 2px 34px;
  opacity: .85;
}

.m-act {
  border: 1px solid var(--border, #e2e8f0);
  background: var(--card, #fff);
  color: var(--text-2, #475569);
  border-radius: 999px;
  padding: 3px 11px;
  font-size: 12px;
  line-height: 1.5;
}

.m-act:active {
  background: var(--brand-soft, #eef2ff);
  color: var(--brand, #4f46e5);
  border-color: var(--brand, #4f46e5);
}

/* ==========================================================
   手机底部标签栏 · 液态玻璃（Liquid Glass 风格）
   · 强模糊 + 高饱和，透出背后内容
   · 顶部一道高光边、底部柔和投影
   · 不支持 backdrop-filter 时退回接近实色的底，保证可读
   ========================================================== */

/* 兜底：所有浏览器先给一个能看清的底 */
.tabbar {
  background: rgba(255, 255, 255, 0.94);
}

@supports (
  (backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))
) {
  .tabbar {
    /* 半透明底：越薄越"玻璃"，但文字要能读，取 0.62 */
    background: linear-gradient(
      180deg,
      rgba(255, 255, 255, 0.72),
      rgba(255, 255, 255, 0.58)
    );
    backdrop-filter: blur(22px) saturate(180%);
    -webkit-backdrop-filter: blur(22px) saturate(180%);

    /* 顶部高光边 + 悬浮投影，制造"一片玻璃浮在内容上"的感觉 */
    border-top: 1px solid rgba(255, 255, 255, 0.75);
    box-shadow:
      0 -1px 0 rgba(15, 23, 42, 0.04),
      0 -10px 30px rgba(15, 23, 42, 0.07),
      inset 0 1px 0 rgba(255, 255, 255, 0.85);
  }

  /* 玻璃上再加一层极淡的主题色晕，避免纯白显得死板 */
  .tabbar::before {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
    background: radial-gradient(
      120% 140% at 50% 0%,
      color-mix(in srgb, var(--brand, #4f46e5) 9%, transparent) 0%,
      transparent 62%
    );
    opacity: 0.9;
  }
}

/* 选中项：胶囊底 + 更实的心跳感，压在玻璃上更清楚 */
.tabbar .tab.is-active {
  background: color-mix(in srgb, var(--brand, #4f46e5) 14%, transparent);
  border-radius: 14px;
}

.tabbar .tab.is-active span {
  color: var(--brand, #4f46e5);
  font-weight: 600;
}

/* 深色模式下的玻璃：白玻璃会发灰，改成深色半透明 */
@media (prefers-color-scheme: dark) {
  .tabbar {
    background: rgba(15, 23, 42, 0.94);
  }

  @supports (
    (backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))
  ) {
    .tabbar {
      background: linear-gradient(
        180deg,
        rgba(30, 41, 59, 0.72),
        rgba(15, 23, 42, 0.62)
      );
      border-top: 1px solid rgba(255, 255, 255, 0.12);
      box-shadow:
        0 -1px 0 rgba(0, 0, 0, 0.3),
        0 -10px 30px rgba(0, 0, 0, 0.35),
        inset 0 1px 0 rgba(255, 255, 255, 0.08);
    }

    .tabbar::before {
      background: radial-gradient(
        120% 140% at 50% 0%,
        color-mix(in srgb, var(--brand, #4f46e5) 22%, transparent) 0%,
        transparent 62%
      );
    }
  }
}

/* 系统开了"降低透明度"就退回实色，这是无障碍要求 */
@media (prefers-reduced-transparency: reduce) {
  .tabbar {
    background: var(--card, #fff) !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }
  .tabbar::before {
    display: none;
  }
}
