/* ==========================================================================
   竖屏兼容 · 手机 / 平板竖屏 / 折叠屏
   解决：微信内置浏览器、HarmonyOS、iOS Safari 的安全区与地址栏高度
   ========================================================================== */

/* -------- 1. 动态视口高度（解决移动端地址栏遮挡） -------- */
:root {
  --vh: 1vh;
  --app-h: 100vh;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --tabbar-h: 0px;
}
@supports (height: 100dvh) {
  :root { --app-h: 100dvh; }
}

/* -------- 2. 超小屏（< 400px）：iPhone SE / 小屏安卓 -------- */
@media (max-width: 400px) {
  .main { padding: 12px 11px 80px; }
  .card { padding: 12px 12px; border-radius: 11px; }
  .topbar h1 { font-size: 17.5px; }
  .topbar__desc { font-size: 11px; }
  .btn { height: 38px; font-size: 13px; padding: 0 14px; }
  .btn--sm { height: 30px; font-size: 12px; padding: 0 10px; }
  .stat { padding: 11px 12px; }
  .stat__num { font-size: 19px; }
  .stat__label { font-size: 11px; }
  .grid { gap: 9px; }
  .grid--4 { gap: 8px; }
  .field__label { font-size: 11.5px; }
  .input { height: 38px; font-size: 13px; padding: 0 10px; }
  textarea.input { min-height: 72px; }
  .tag { font-size: 10.5px; padding: 2px 7px; }
  .hero { padding: 15px; }
  .hero__title { font-size: 17px; }
  .analysis__body { font-size: 12.5px; }
  .flash__q { font-size: 14.5px; }
  .toast-wrap { left: 8px; right: 8px; }
  /* 底部导航更紧凑 */
  .tabbar__item { font-size: 9.5px; padding: 4px 4px; }
  .tabbar__item svg { width: 18px; height: 18px; }
}

/* -------- 3. 超小屏（< 360px）-------- */
@media (max-width: 360px) {
  .main { padding: 10px 9px 78px; }
  .card { padding: 11px 10px; }
  .topbar h1 { font-size: 16px; }
  .grid--4 { grid-template-columns: 1fr; }
  .grade-grid { grid-template-columns: 1fr; }
  .btn { padding: 0 11px; font-size: 12.5px; }
  .stat__num { font-size: 17px; }
  .tabbar__item { font-size: 9px; }
  .tabbar__item svg { width: 17px; height: 17px; }
  /* 会员卡片单列 */
  .member-grid, .plan-grid, .pack-grid { grid-template-columns: 1fr !important; }
}

/* -------- 4. 竖屏（高度 > 宽度）：主要场景 -------- */
@media (orientation: portrait) {
  /* 底部导航固定，主内容留出空间 */
  .main { padding-bottom: calc(78px + var(--safe-bottom)) !important; }
  .tabbar { padding-bottom: calc(6px + var(--safe-bottom)) !important; }

  /* AI 对话区在窄屏拉高 */
  [data-chat-list] { max-height: 46vh !important; }

  /* 详情页大图缩放 */
  .flash__img img, .mcard__img img { max-height: 54vh; object-fit: contain; }

  /* 表格横向滚动 */
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table { min-width: 480px; }

  /* 长公式横向滚动不撑破 */
  .katex-display { overflow-x: auto; overflow-y: hidden; max-width: 100%; }

  /* 输入框防止 iOS 自动放大（字号 >= 16px） */
  @supports (-webkit-touch-callout: none) {
    .input, textarea.input, select.input {
      font-size: 16px !important;
    }
  }
}

/* -------- 5. 横屏（矮屏）：隐藏侧栏，导航横向 -------- */
@media (orientation: landscape) and (max-height: 500px) {
  .sidebar { display: none; }
  .main { padding: 10px 14px 10px !important; }
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 90;
    background: rgba(255,255,255,.96); backdrop-filter: blur(16px);
    border-top: 1px solid var(--line);
    padding: 3px 4px calc(3px + var(--safe-bottom));
    justify-content: space-around;
  }
  .tabbar__item { font-size: 9px; padding: 3px 8px; }
  .tabbar__item svg { width: 17px; height: 17px; }
  .main { padding-bottom: calc(56px + var(--safe-bottom)) !important; }
  .auth__side { display: none; }
  .auth__panel { padding: 16px 20px; }
  #authRoot { height: auto; min-height: var(--app-h); overflow: auto; align-items: flex-start; padding: 12px; }
  [data-chat-list] { max-height: 60vh !important; }
}

/* -------- 6. 微信内置浏览器 / 鸿蒙 -------- */
@media (max-width: 900px) {
  /* 修复 iOS/鸿蒙 100vh 抖动 */
  #authRoot { height: var(--app-h); }
  html, body { overscroll-behavior-y: none; }

  /* 顶部安全区（刘海屏） */
  .topbar { padding-top: calc(var(--safe-top) * 0.5); }

  /* 按钮最小点击尺寸 44px（触屏标准） */
  .btn--sm { min-height: 32px; }
  .icon-btn { min-width: 36px; min-height: 36px; }

  /* 弹窗在手机上全屏 */
  .modal__panel, .modal .card {
    max-height: calc(var(--app-h) - 40px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* 模态背景点击区收起时不受 safe-area 影响 */
  .modal { padding: calc(10px + var(--safe-top)) 10px calc(10px + var(--safe-bottom)); }

  /* 长按不选中文字（触摸体验） */
  .tabbar__item, .quote-item, .mcard { -webkit-user-select: none; user-select: none; }

  /* 横向滚动区域加惯性 */
  .tag-list, .chips, .seg { -webkit-overflow-scrolling: touch; scroll-behavior: smooth; }
}

/* -------- 7. 平板 / 中屏（768-1024）-------- */
@media (min-width: 768px) and (max-width: 1024px) {
  .grid--4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid--3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* 仪表盘 hero 溢出修复（768 宽度下实测 811>731） */
.dash-hero { min-width: 0; max-width: 100%; overflow: hidden; }
.dash-hero__in { min-width: 0; }
.dash-hero__title { min-width: 0; overflow-wrap: break-word; }
.dash-hero__gauge { flex: 0 0 auto; }
.dash-hero__ring, .ring { max-width: 100%; }

@media (max-width: 900px) {
  .dash-hero { flex-wrap: wrap; }
  .dash-hero__gauge { display: none; }
}

@media (max-width: 820px) {
  .dash-hero { padding: 16px; }
  .dash-hero__title { font-size: 19px; }
}

/* -------- 8. 折叠屏 / 超宽竖屏 -------- */
@media (min-width: 540px) and (max-width: 820px) and (orientation: portrait) {
  .grid--4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tabbar { display: none; }
  .main { padding-bottom: 24px !important; }
}

/* -------- 9. 竖屏下 AI 答疑卡片优化 -------- */
@media (max-width: 560px) {
  [data-chat-card] [data-chat-list] {
    max-height: 44vh !important;
    padding: 2px 0 !important;
  }
  [data-chat-card] [data-chat-input] {
    height: 40px !important;
    font-size: 13.5px !important;
  }
  [data-chat-card] [data-chat-send] {
    height: 40px !important;
    padding: 0 16px !important;
    font-size: 13px !important;
  }
  /* 快捷提问按钮换行更紧凑 */
  [data-chat-card] [data-chat-quick] {
    font-size: 11.5px !important;
    padding: 5px 10px !important;
  }
}

/* -------- 10. 充值页网格（响应式类） -------- */
.ch-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.plan-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px;
}
.pack-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}

@media (max-width: 640px) {
  .ch-grid { gap: 10px; }
  .plan-grid { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 11px; }
  .pack-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
}

@media (max-width: 480px) {
  /* 双渠道在窄竖屏上下排列，避免文字挤压 */
  .ch-grid { grid-template-columns: 1fr; gap: 10px; }
  .plan-grid { grid-template-columns: 1fr; gap: 11px; }
  .pack-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; }
}

@media (max-width: 340px) {
  .pack-grid { grid-template-columns: 1fr; }
}

/* -------- 11. 键盘弹出时（body.kb-open） -------- */
body.kb-open .tabbar {
  transform: translateY(100%);
  transition: transform .2s ease;
}
body.kb-open .main {
  padding-bottom: 12px !important;
}

/* -------- 12. 触屏优化 -------- */
@media (hover: none) and (pointer: coarse) {
  /* 触屏上取消 hover 位移，防止误触 */
  .btn:hover, .card:hover, .quiz-card:hover { transform: none !important; }
  .btn:active { transform: scale(.97); }

  /* 加大可点击区域 */
  .tag { padding: 4px 9px; }
  .nav__item { min-height: 40px; }

  /* 输入框在触屏不缩放 */
  input, textarea, select { font-size: max(16px, 1em); }
}

/* -------- 13. 长内容横向溢出保护 -------- */
.main { overflow-x: hidden; }
.flash__q, .analysis__body, .mcard__q, .answer-box__text {
  overflow-wrap: break-word;
  word-break: break-word;
  max-width: 100%;
}
pre, code { white-space: pre-wrap; word-break: break-all; }
img { max-width: 100%; height: auto; }

/* ==========================================================================
   类型训练
   ========================================================================== */
.drill-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 12px;
}

.drill-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 20px 12px;
  border: 1.5px solid var(--line);
  border-radius: 14px;
  background: #fff;
  cursor: pointer;
  transition: all .22s cubic-bezier(.22,.8,.3,1);
  text-align: center;
  font-family: inherit;
}
.drill-card:hover {
  border-color: rgba(22,93,255,.4);
  background: #f9fcff;
  transform: translateY(-2px);
  box-shadow: 0 8px 22px -10px rgba(22,93,255,.3);
}
.drill-card:active { transform: translateY(0) scale(.98); }
.drill-card:disabled { opacity: .6; cursor: default; transform: none; }

.drill-card__icon {
  width: 42px; height: 42px;
  border-radius: 12px;
  background: linear-gradient(135deg, #e8f3ff, #d6e9ff);
  display: grid; place-items: center;
  color: var(--main-blue);
}
.drill-card__icon svg { width: 21px; height: 21px; }
.drill-card__label {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-dark);
}
.drill-card__count {
  font-size: 11.5px;
  color: var(--soft-grey);
}
.drill-card__count b { color: var(--main-blue); font-size: 13px; }

@media (max-width: 560px) {
  .drill-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .drill-card { padding: 16px 10px; gap: 6px; }
  .drill-card__icon { width: 36px; height: 36px; border-radius: 10px; }
  .drill-card__icon svg { width: 18px; height: 18px; }
  .drill-card__label { font-size: 12.5px; }
}

@media (max-width: 360px) {
  .drill-grid { grid-template-columns: 1fr; }
}

/* ==========================================================================
   手机端底部导航：账号入口
   ========================================================================== */
.tabbar__item.is-account .tabbar__avatar {
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--light-blue);
  display: grid; place-items: center;
  font-size: 12px;
  overflow: hidden;
}

/* 手机端底部导航安全区 */
@media (max-width: 900px) {
  .tabbar { padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px)) !important; }
  .main { padding-bottom: calc(86px + env(safe-area-inset-bottom, 0px)) !important; }
}

/* ==========================================================================
   登录页体验优化
   ========================================================================== */
.auth__panel { position: relative; }

.auth__close {
  position: absolute;
  top: 14px; right: 14px;
  width: 32px; height: 32px;
  border: 0;
  border-radius: 9px;
  background: rgba(0,0,0,.04);
  color: var(--soft-grey);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: all .18s ease;
  z-index: 2;
}
.auth__close:hover {
  background: rgba(245,63,63,.1);
  color: var(--danger);
}

/* 加载旋转图标 */
.auth__spinner {
  display: inline-block;
  width: 13px; height: 13px;
  border: 2px solid rgba(255,255,255,.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: authSpin .7s linear infinite;
  vertical-align: -2px;
  margin-right: 3px;
}
@keyframes authSpin { to { transform: rotate(360deg); } }

/* 表单输入框错误态 */
.auth__form .input:focus {
  border-color: var(--main-blue);
  box-shadow: 0 0 0 3px rgba(22,93,255,.1);
}

/* 移动端登录页 */
@media (max-width: 560px) {
  #authRoot {
    padding: calc(16px + env(safe-area-inset-top, 0px)) 14px
             calc(16px + env(safe-area-inset-bottom, 0px));
  }
  .auth { border-radius: 16px; }
  .auth__panel { padding: 26px 20px; }
  .auth__title { font-size: 17.5px; }
  .auth__sub { font-size: 12px; }
  .auth__brand { margin-bottom: 20px; }
  .auth__tabs { margin-bottom: 18px; }
  .auth__tab { height: 36px; font-size: 13px; }
  .auth__close { top: 10px; right: 10px; width: 30px; height: 30px; }
  /* iOS 自动缩放规避 */
  .auth__form .input { font-size: 16px; }
  .auth .btn--lg { height: 46px; font-size: 15px; }
}

/* 矮屏（横屏手机）登录页 */
@media (max-height: 620px) {
  .auth__panel { padding: 18px 22px; }
  .auth__brand { margin-bottom: 14px; }
  .auth__tabs { margin-bottom: 14px; }
  .auth__form .input { height: 36px; }
  .auth .btn--lg { height: 40px; margin-top: 12px !important; }
}


/* ==========================================================================
   左侧导航：折叠「更多」
   ========================================================================== */
.nav__more {
  margin-top: 6px;
}

.nav__more-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 13px;
  border: 0;
  border-radius: 11px;
  background: transparent;
  color: var(--text-grey, #86909c);
  font-size: 13.5px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: all .18s ease;
  text-align: left;
}
.nav__more-btn:hover {
  background: rgba(22, 93, 255, .06);
  color: var(--main-blue, #165dff);
}
.nav__more-btn svg {
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
  opacity: .8;
}
.nav__more-btn span:first-of-type {
  flex: 1;
}

.nav__more-arrow {
  display: grid;
  place-items: center;
  transition: transform .24s cubic-bezier(.22, .9, .3, 1);
}
.nav__more-arrow svg {
  width: 14px;
  height: 14px;
  opacity: .55;
}
.nav__more-btn.is-open .nav__more-arrow {
  transform: rotate(90deg);
}

.nav__more-panel {
  margin-top: 4px;
  padding-left: 4px;
  border-left: 2px solid rgba(22, 93, 255, .1);
  margin-left: 9px;
  animation: navExpand .28s cubic-bezier(.22, .9, .3, 1);
}
.nav__more-panel[hidden] {
  display: none;
}

@keyframes navExpand {
  from {
    opacity: 0;
    transform: translateY(-6px);
    max-height: 0;
  }
  to {
    opacity: 1;
    transform: translateY(0);
    max-height: 800px;
  }
}

.nav__more-panel .nav__label {
  font-size: 10.5px;
  margin: 10px 0 4px 9px;
  opacity: .7;
}
.nav__more-panel .nav__item {
  padding-left: 15px;
  font-size: 13px;
}

/* 学生模式的「更多」按钮配色 */
[data-mode="student"] .nav__more-btn:hover {
  background: linear-gradient(120deg, rgba(99, 102, 241, .1), rgba(139, 92, 246, .06));
  color: #6366f1;
}
[data-mode="student"] .nav__more-panel {
  border-left-color: rgba(139, 92, 246, .14);
}
