/* ============================================================================
   塾饭 SNS · 移动端 v4.0 全局样式
   适用于 ≤768px 设备，沉浸式布局 + 手势支持 + 暗色模式
   ============================================================================ */

@media (max-width: 768px) {

  /* ===== 1. 重置 + 基础 ===== */
  :root {
    --header-h: 56px;
    --tabbar-h: 60px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
    --gap: 14px;
    --pad-x: 14px;
  }

  *, *::before, *::after { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
  html, body { overflow-x: hidden; }
  body {
    font-size: 15px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    padding-top: var(--header-h);
    padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom));
  }
  body.m-no-tabbar { padding-bottom: 0; }
  body.m-no-header { padding-top: 0; }

  /* ===== 2. 沉浸式顶栏 ===== */
  #toptb { display: none !important; }   /* 隐藏 DZ 桌面顶栏 */
  #nv, #mu, #hd .hdc, #qmenu_menu, .p_pop { display: none !important; }
  #hd {
    position: fixed !important;
    top: 0; left: 0; right: 0;
    height: var(--header-h);
    z-index: 1000;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    box-shadow: 0 1px 3px rgba(0,0,0,.04);
    display: flex !important;
    align-items: center;
    padding: 0 var(--pad-x);
    padding-top: var(--safe-top);
    height: calc(var(--header-h) + var(--safe-top));
    transition: transform .3s cubic-bezier(.22,1,.36,1);
  }
  #hd.hidden { transform: translateY(-100%); }

  #hd .wp { width: 100%; margin: 0; padding: 0; }
  #hd .hdc.cl { display: flex !important; align-items: center; justify-content: space-between; width: 100%; height: 100%; }
  #hd .hdc.cl h2 { margin: 0; display: flex; align-items: center; gap: 8px; }
  #hd .boardlogo { height: 28px !important; width: auto; vertical-align: middle; }

  /* 自定义标题文字（DZ 默认用 logo，没有文字） */
  .m-app-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text);
    letter-spacing: -.01em;
  }

  /* ===== 3. 汉堡按钮 ===== */
  .m-menu-btn {
    width: 40px; height: 40px;
    display: inline-flex; align-items: center; justify-content: center;
    background: transparent;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    transition: background .2s;
    color: var(--text);
  }
  .m-menu-btn:active { background: var(--primary-light); }
  .m-menu-btn svg { width: 22px; height: 22px; }

  /* ===== 4. 右侧操作组（搜索/通知/主题切换） ===== */
  .m-actions { display: flex; align-items: center; gap: 4px; }
  .m-actions a, .m-actions button {
    width: 40px; height: 40px;
    display: inline-flex; align-items: center; justify-content: center;
    background: transparent;
    border: none;
    color: var(--text);
    border-radius: 10px;
    cursor: pointer;
    text-decoration: none;
    position: relative;
  }
  .m-actions a:active, .m-actions button:active { background: var(--primary-light); }
  .m-actions svg { width: 20px; height: 20px; }
  .m-actions .badge {
    position: absolute;
    top: 6px; right: 6px;
    min-width: 16px; height: 16px;
    background: var(--accent);
    color: #fff;
    font-size: 10px;
    font-weight: 600;
    border-radius: 8px;
    padding: 0 4px;
    display: flex; align-items: center; justify-content: center;
  }

  /* ===== 5. 抽屉式侧栏 ===== */
  .m-drawer {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 78%;
    max-width: 320px;
    background: var(--bg-card);
    z-index: 1100;
    transform: translateX(-100%);
    transition: transform .3s cubic-bezier(.22,1,.36,1);
    overflow-y: auto;
    box-shadow: 4px 0 24px rgba(0,0,0,.1);
    padding-top: var(--safe-top);
  }
  .m-drawer.open { transform: translateX(0); }
  .m-drawer-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 1099;
    opacity: 0;
    pointer-events: none;
    transition: opacity .3s;
  }
  .m-drawer-overlay.open { opacity: 1; pointer-events: auto; }
  .m-drawer-header {
    padding: 20px 16px 16px;
    border-bottom: 1px solid var(--border-light);
  }
  .m-drawer-header .m-user {
    display: flex; align-items: center; gap: 12px;
  }
  .m-drawer-header .m-user .avatar {
    width: 48px; height: 48px;
    border-radius: 50%;
    object-fit: cover;
    background: var(--border);
  }
  .m-drawer-header .m-user .name { font-weight: 600; color: var(--text); }
  .m-drawer-header .m-user .meta { font-size: 12px; color: var(--text-secondary); }
  .m-drawer-list {
    list-style: none;
    margin: 0;
    padding: 8px 0;
  }
  .m-drawer-list a {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 16px;
    color: var(--text);
    text-decoration: none;
    font-size: 15px;
    transition: background .15s;
  }
  .m-drawer-list a:active { background: var(--primary-light); }
  .m-drawer-list a.active { color: var(--primary); font-weight: 600; background: var(--primary-light); }
  .m-drawer-list .ico {
    width: 22px; height: 22px;
    color: var(--text-secondary);
  }
  .m-drawer-list a.active .ico { color: var(--primary); }
  .m-drawer-divider {
    height: 1px;
    background: var(--border-light);
    margin: 8px 0;
  }

  /* ===== 6. 底部 Tab Bar ===== */
  .m-tabbar {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    height: calc(var(--tabbar-h) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: space-around;
    box-shadow: 0 -2px 8px rgba(0,0,0,.04);
  }
  .m-tabbar a {
    flex: 1;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px;
    padding: 6px 0;
    color: var(--text-tertiary);
    text-decoration: none;
    font-size: 11px;
    transition: color .2s;
    -webkit-tap-highlight-color: transparent;
  }
  .m-tabbar a.active { color: var(--primary); }
  .m-tabbar .tab-ico {
    width: 24px; height: 24px;
    position: relative;
  }
  .m-tabbar .tab-ico svg { width: 24px; height: 24px; }
  .m-tabbar .badge {
    position: absolute;
    top: -2px; right: -6px;
    min-width: 16px; height: 16px;
    background: var(--accent);
    color: #fff;
    font-size: 10px;
    font-weight: 600;
    border-radius: 8px;
    padding: 0 4px;
    display: flex; align-items: center; justify-content: center;
  }

  /* 中央凸起发布按钮 */
  .m-tabbar .tab-fab {
    width: 52px; height: 52px;
    margin-top: -20px;
    background: var(--primary-gradient);
    color: #fff;
    border-radius: 50%;
    box-shadow: 0 6px 16px rgba(67,97,238,.4);
    display: flex; align-items: center; justify-content: center;
    transition: transform .15s;
  }
  .m-tabbar .tab-fab:active { transform: scale(.92); }

  /* ===== 7. 内容区 ===== */
  #wp, .wp { width: 100% !important; padding: 0 var(--pad-x) !important; }
  #ct { padding: 0 !important; }
  #pt { padding: 12px 0 !important; font-size: 12px !important; }
  #pt .z, #pt .y { display: inline-block; }

  /* 隐藏 DZ 默认的辅助元素 */
  .pgs .pg, .pg, .pgsbtn { display: flex !important; }
  .pg a, .pg strong {
    display: inline-flex !important; align-items: center; justify-content: center;
    min-width: 36px; height: 36px;
    padding: 0 10px;
    border-radius: 8px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text);
    text-decoration: none;
    font-size: 14px;
    margin: 0 2px;
  }
  .pg strong { background: var(--primary); color: #fff; border-color: var(--primary); }

  /* ===== 8. 卡片 ===== */
  .bm, .bmw {
    border-radius: 12px !important;
    border: 1px solid var(--border) !important;
    background: var(--bg-card) !important;
    box-shadow: var(--shadow-sm) !important;
    margin-bottom: 12px !important;
  }
  .bm_h {
    padding: 14px 16px !important;
    border-bottom: 1px solid var(--border-light) !important;
    background: transparent !important;
  }
  .bm_h h2, .bm_h .tit { font-size: 15px !important; font-weight: 600 !important; }
  .bm_c { padding: 14px 16px !important; }
  .bm_c .cl { padding: 0 !important; }

  /* ===== 9. 列表行 ===== */
  .list { width: 100% !important; }
  .list th { display: none !important; }
  .list td { padding: 14px 0 !important; border-top: 1px solid var(--border-light) !important; }
  .list tr:first-child td { border-top: none !important; }
  .list .fl_icn { width: 40px !important; padding-right: 12px !important; }
  .list .fl_icn img { width: 32px !important; height: 32px !important; border-radius: 8px !important; }
  .list h2, .list .xst { font-size: 15px !important; font-weight: 500 !important; line-height: 1.4 !important; }
  .list .xg1, .list .xg2 { font-size: 12px !important; color: var(--text-secondary) !important; }

  /* ===== 10. 帖子详情 ===== */
  .pl, .plhin { padding: 14px 16px !important; border-bottom: 1px solid var(--border-light) !important; }
  .pls { width: 60px !important; padding: 14px 8px 14px 16px !important; float: left !important; }
  .pls .avatar { width: 44px !important; height: 44px !important; border-radius: 50% !important; }
  .plc { margin-left: 68px !important; padding: 0 !important; }
  .pi { font-size: 12px !important; margin-bottom: 6px !important; }
  .pi strong, .pi .xs2 { font-size: 13px !important; }
  .postmessage { font-size: 15px !important; line-height: 1.7 !important; }
  .postmessage img, .zoomfile { max-width: 100% !important; height: auto !important; border-radius: 8px !important; }
  .psth, .pth { padding: 12px 16px !important; }

  /* ===== 11. 论坛列表（版块） ===== */
  .fl_tb { margin: 0 !important; padding: 0 !important; }
  .fl_tb td, .fl_tb tr { display: block !important; width: auto !important; padding: 12px 0 !important; border: none !important; }
  .fl_tb tr { padding: 14px 0 !important; border-top: 1px solid var(--border-light) !important; }
  .fl_tb tr:first-child { border-top: none !important; }
  .fl_tb .fl_icn { display: inline-block !important; vertical-align: middle !important; width: 40px !important; }
  .fl_tb h2 { display: inline-block !important; vertical-align: middle !important; margin: 0 8px !important; }

  /* ===== 12. 表单 ===== */
  input[type=text], input[type=password], input[type=email], textarea, select {
    width: 100%;
    padding: 10px 12px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    font-size: 15px;
    -webkit-appearance: none;
    appearance: none;
  }
  input:focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
  }

  button.pn, button.pnc, .pn, .pnc {
    width: 100%;
    padding: 12px;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 500;
    background: var(--bg-card);
    border: 1px solid var(--border);
    color: var(--text);
  }
  button.pnc, .pnc { background: var(--primary) !important; color: #fff !important; border-color: var(--primary) !important; }

  /* ===== 13. SNS 主页 hero（移动版） ===== */
  .sns-hero {
    margin: 0 calc(-1 * var(--pad-x)) 16px;
    padding: 32px 16px;
    background: var(--primary-gradient);
    border-radius: 0 0 24px 24px;
  }
  .sns-hero h1 { font-size: 22px !important; }
  .sns-hero p.sub { font-size: 14px !important; }
  .sns-hero .btns { flex-direction: column !important; gap: 8px !important; }
  .sns-hero .btns a { width: 100% !important; }

  .sns-grid, .sns-row-3, .sns-cols {
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }
  .sns-grid-2 { grid-template-columns: 1fr 1fr !important; }

  /* ===== 14. 圈子广场 ===== */
  .sns-circle-grid {
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 12px !important;
    padding: 12px !important;
  }
  .sns-circle-card { padding: 14px !important; }

  /* ===== 15. 列表瀑布流 ===== */
  .sns-feed-list { gap: 10px !important; }
  .sns-feed-card { padding: 14px !important; border-radius: 12px !important; }
  .sns-feed-header { gap: 8px !important; }
  .sns-feed-content { font-size: 15px !important; }
  .sns-feed-media { grid-template-columns: 1fr 1fr !important; gap: 6px !important; margin-top: 8px !important; }

  /* ===== 16. 头像/缩略图 ===== */
  .avt, .avtm { width: 36px !important; height: 36px !important; border-radius: 50% !important; }
  .avtm { width: 64px !important; height: 64px !important; }

  /* ===== 17. 浮动按钮 ===== */
  #sns-theme-toggle.sns-theme-btn-fixed {
    bottom: calc(16px + var(--safe-bottom) + var(--tabbar-h)) !important;
    right: 16px !important;
    width: 44px !important;
    height: 44px !important;
  }

  /* ===== 18. 滚动吸顶时的内阴影 ===== */
  .m-sticky-shadow {
    box-shadow: 0 4px 16px rgba(0,0,0,.08) !important;
  }

  /* ===== 19. 暗色模式适配 ===== */
  @media (prefers-color-scheme: dark) {
    .m-drawer, .m-tabbar { background: rgba(27,29,42,.85); }
  }
  html[data-theme="dark"] .m-drawer,
  html[data-theme="dark"] .m-tabbar { background: rgba(27,29,42,.85); }
}

/* ===== 20. 极小屏优化（≤380px） ===== */
@media (max-width: 380px) {
  :root { --pad-x: 10px; --gap: 10px; }
  body { font-size: 14px; }
  .pg a, .pg strong { min-width: 32px; height: 32px; font-size: 13px; }
  .sns-feed-content { font-size: 14px; }
}

/* ===== 21. 横屏适配 ===== */
@media (max-width: 1024px) and (orientation: landscape) {
  body { padding-top: calc(var(--header-h) + var(--safe-top)); }
  .m-tabbar { display: none; }
  body { padding-bottom: 0; }
}

/* ===== 22. PWA 增强 ===== */
@media (display-mode: standalone) {
  body { padding-top: calc(var(--header-h) + var(--safe-top)); }
  #hd { padding-top: var(--safe-top); }
}

/* ===== 23. 平板（769-1024）轻调 ===== */
@media (min-width: 769px) and (max-width: 1024px) {
  #wp, .wp { max-width: 100% !important; padding: 0 20px !important; }
  .sns-grid, .sns-cols { grid-template-columns: repeat(2, 1fr) !important; }
  .sns-grid-3 { grid-template-columns: repeat(3, 1fr) !important; }
}