/* ==========================================================================
   mobile.css — 全站手机端排版优化层
   在各页面原有样式之后加载（靠后即生效），只影响窄屏，不动桌面版。
   断点：<=900px 平板/大手机，<=560px 常规手机，<=380px 小屏
   ========================================================================== */

/* ---------- 通用兜底：任何页面都不该出现横向滚动条 ---------- */
@media (max-width: 900px) {
    html, body {
        overflow-x: hidden;
        max-width: 100%;
    }
    /* flex/grid 子项默认 min-width:auto 会撑破容器，统一放开 */
    .wrap, .container, .card, .topbar, .topbar-actions, .tabs,
    .chat-box, .preview-content, .hero, .grid, .file-list {
        min-width: 0;
        max-width: 100%;
    }
    /* 关键容器兜底裁剪：宽表格由内部 .table-scroll 自行滚动，
       不会因为 table 的 min-width 反过来把整页撑宽（这是横向溢出的头号成因） */
    .wrap, .container, .hero, .card, .chat-box, .stats, .hero-stats {
        overflow-x: hidden;
    }
    /* 长串不换行导致的溢出（URL、英文、连续字符） */
    body, button, input, select, textarea, td, th, p, span, div {
        word-break: break-word;
        overflow-wrap: anywhere;
    }
    img, video, canvas, svg { max-width: 100%; }
}

/* ==========================================================================
   1. 顶栏（首页 / 聊天室 / 面板 / 图书馆共用一套骨架）
   ========================================================================== */
@media (max-width: 900px) {
    .topbar {
        display: flex !important;
        flex-direction: column;
        align-items: stretch;
        gap: 9px;
        padding: 11px 12px !important;
        border-radius: 16px;
    }
    /* 品牌/标题独占一行 */
    .topbar-brand,
    .topbar-title {
        width: 100%;
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 15px !important;
        line-height: 1.4;
        min-width: 0;
    }
    .topbar-title > span,
    .topbar-brand > span {
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    /* 图标必须限死尺寸 —— 聊天室/面板的站点图标在窄屏会撑成巨幅 */
    .topbar-title img,
    .topbar img,
    .logo {
        width: 32px !important;
        height: 32px !important;
        min-width: 32px !important;
        max-width: 32px !important;
        object-fit: cover;
        border-radius: 9px;
        flex: none !important;
    }
    /* 按钮组：固定两列 + 文字省略，从根上杜绝横向溢出 */
    .topbar-actions {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 6px;
        width: 100%;
        align-items: stretch;
    }
    .topbar-actions > * {
        flex: 1 1 calc(50% - 3px);
        max-width: calc(50% - 3px);
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
    /* 只有一个按钮时占满整行更合理 */
    .topbar-actions > *:only-child { flex-basis: 100%; max-width: 100%; }
}

/* 触控目标不小于 40px，手机上不好点 */
@media (max-width: 560px) {
    .pill, .tb-btn, .header-btn, .btn, .vbtn, button.btn {
        min-height: 40px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 9px 14px;
        font-size: 13px;
        line-height: 1.2;
    }
    /* 文字过长时省略，而不是整段隐藏 ——
       藏掉文字会让只包了 <span class="t"> 的按钮变成一个空白方块 */
    .pill, .tb-btn, .header-btn {
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
}

/* ==========================================================================
   2. 分类标签行（面板）——改成可横向滑动，避免挤成多行
   ========================================================================== */
@media (max-width: 900px) {
    .tabs {
        display: flex !important;
        flex-wrap: nowrap !important;
        gap: 7px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        padding: 3px 1px 7px;
        margin: 0 -2px 12px;
    }
    .tabs::-webkit-scrollbar { display: none; }
    .tabs .pill {
        flex: 0 0 auto;
        white-space: nowrap;
        min-height: 38px;
    }
}

/* ==========================================================================
   3. 表格 —— 统一横向滚动；手机上隐藏次要列
   （.table-scroll 由 mobile.js 自动包裹，见下）
   ========================================================================== */
@media (max-width: 900px) {
    .table-scroll {
        width: 100%;
        max-width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-radius: 12px;
        /* 右侧渐隐，提示还能往右滑 */
        background:
            linear-gradient(to left, rgba(0,0,0,.07), rgba(0,0,0,0) 26px) right center / 26px 100% no-repeat;
    }
    .table-scroll table {
        margin: 0;
        width: 100%;
    }
    /* 表格内元素不再撑破 */
    .table-scroll td,
    .table-scroll th {
        max-width: 44vw;
        overflow-wrap: anywhere;
    }
    /* 手机上隐藏「上传时间 / 下载次数 / 分享码」等次要列，只留名称、类型、大小 */
    @media (max-width: 560px) {
        .file-table th:nth-child(n+4),
        .file-table td:nth-child(n+4) { display: none; }
    }
    /* 概览类文本（收录文件 / 存储占用）在窄屏允许换行，不再被硬裁 */
    .site-stats,
    .storage-info {
        display: block !important;
        white-space: normal !important;
        word-break: break-word;
        overflow-wrap: anywhere;
        line-height: 1.9;
        max-width: 100%;
    }
    .site-stats * { white-space: normal !important; }
}

/* ==========================================================================
   4. 卡片 / 统计块 / 表单
   ========================================================================== */
@media (max-width: 900px) {
    .card { padding: 14px !important; border-radius: 16px; }
    .card-title {
        font-size: 14.5px !important;
        flex-wrap: wrap;
        gap: 6px;
    }
    .card-title .right { margin-left: 0; width: 100%; }
    /* 统计块：手机上固定 2 列，比 auto-fit 更稳，不会挤成 1 列或溢出 */
    .stats,
    .hero-stats {
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 8px !important;
    }
    .stat { padding: 11px 8px !important; min-width: 0; }
    .stat b { font-size: 19px !important; }
    .stat span { font-size: 11.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
    .form-grid { grid-template-columns: 1fr !important; }
    /* 输入类控件在 iOS 上字号 <16px 会触发自动放大 */
    input, select, textarea {
        font-size: 16px !important;
        max-width: 100%;
    }
    .actions-cell { gap: 6px; }
    .actions-cell .btn { flex: 1 1 auto; min-width: 84px; }
}

/* ==========================================================================
   5. 首页 hero —— 压缩高度 + 提升文字对比度
   ========================================================================== */
@media (max-width: 900px) {
    .hero {
        padding: 18px 14px !important;
        gap: 12px !important;
        min-height: 0 !important;
        /* 原为 align-items:center + 可换行 flex，窄屏折成两行时
           首行会被垂直居中拉伸出大片空白，这里改为顶对齐并让两块各占整行 */
        align-items: flex-start !important;
        justify-content: flex-start !important;
        flex-direction: column !important;
    }
    .hero-main {
        flex: 1 1 100% !important;
        width: 100% !important;
        gap: 10px !important;
    }
    .hero-stats {
        flex: 1 1 100% !important;
        width: 100% !important;
        align-self: stretch !important;
    }
    .hero-title { font-size: 24px !important; }
    .hero-sub { font-size: 13.5px !important; }
    .logo, .hero-title .logo {
        width: 64px !important;
        height: 64px !important;
        min-width: 64px !important;
        flex: none !important;
    }
    .logo img, .hero-title .logo img {
        width: 100% !important;
        height: 100% !important;
        object-fit: cover;
    }
    .icon-box {
        width: 64px !important;
        height: 64px !important;
        min-width: 64px !important;
        font-size: 30px !important;
    }
    /* 搜索框与主按钮占满宽度，更好点 */
    .search-bar {
        width: 100% !important;
        max-width: 100% !important;
        flex-wrap: wrap !important;
    }
    .search-bar input { flex: 1 1 100% !important; min-width: 0 !important; }
    .search-bar button { flex: 1 1 100% !important; justify-content: center; }
    .btn-main { width: 100%; justify-content: center; }
}
@media (max-width: 560px) {
    .hero-title { font-size: 22px !important; }
    .grid { grid-template-columns: 1fr !important; }
}

/* ==========================================================================
   6. 聊天室
   ========================================================================== */
@media (max-width: 900px) {
    /* 关键：不要覆盖 .chat-box 的高度，让它保留原有的 flex:1 + min-height:0，
       否则聊天区会被撑高、消息气泡跟着溢出到屏幕外 */
    .chat-box {
        padding: 12px 9px !important;
        border-radius: 15px;
    }
    /* 移动端浏览器工具栏会吃掉一截视口，用 dvh 让输入区真正贴底 */
    .wrap { height: 100dvh !important; }
    .msg { max-width: 100%; gap: 8px; }
    .msg-body { max-width: 78% !important; min-width: 0 !important; }
    .msg.mine .msg-body { max-width: 78% !important; }
    .bubble { max-width: 100%; word-break: break-word; overflow-wrap: anywhere; }
    .bubble img { max-width: 150px !important; height: auto; }
    .welcome-bar { font-size: 12.5px !important; line-height: 1.6; }
    .chat-input,
    .input-bar,
    .composer { width: 100%; }
    .chat-input textarea,
    .chat-input input,
    .composer textarea { min-height: 42px; font-size: 16px !important; }
    .to-bottom { right: 12px !important; }
}

/* ==========================================================================
   7. 图书馆
   ========================================================================== */
@media (max-width: 900px) {
    .container { padding: 10px 11px 30px !important; }
    /* 顶栏是内联 flex，窄屏下强制整块换行 */
    .container > header {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 9px !important;
    }
    .container > header h1 { font-size: 18px !important; }
    .container > header > div {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 7px !important;
    }
    .header-btn {
        flex: 1 1 calc(50% - 4px);
        min-width: 0;
        padding: 10px 8px !important;
        font-size: 12.5px !important;
        justify-content: center;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .welcome-text { width: 100%; font-size: 13px; }
    .site-stats { font-size: 12.5px !important; line-height: 1.8; }
    .breadcrumb { font-size: 12.5px; flex-wrap: wrap; }
    /* 模态框在窄屏贴边铺满，按钮更好点 */
    .changelog-modal, .feedback-modal, .preview-modal, .registerModal {
        width: 96vw !important;
        max-width: 96vw !important;
        max-height: 88vh !important;
    }
    .preview-content { padding: 10px !important; }
}

/* ==========================================================================
   8. 预览页（压缩档位切换条）
   ========================================================================== */
@media (max-width: 900px) {
    body { padding: 8px !important; }
    .preview-container { border-radius: 14px; }
    .preview-header {
        flex-direction: column;
        align-items: stretch;
        gap: 9px;
        padding: 12px 13px;
    }
    .preview-header h2 { max-width: 100% !important; font-size: 1rem !important; }
    .btn-group { display: flex; gap: 8px; }
    .btn-group .btn { flex: 1; text-align: center; min-height: 40px; }
    .variant-bar { gap: 6px; }
    .vbtn { flex: 1 1 calc(50% - 6px); min-height: 42px; font-size: 13px; }
    .variant-bar .quality-hint { flex: 1 1 100%; }
    .metadata-bar { font-size: 12.5px; padding: 8px 11px; }
    .preview-video, .preview-image { max-height: 58vh; }
}
@media (max-width: 560px) {
    .vbtn { flex: 1 1 calc(33.333% - 8px); font-size: 12.5px; padding: 8px 6px; }
    .vbtn em { display: none; }
}

/* ==========================================================================
   9. 可读性：窄屏加深背景蒙版
   各站原本的蒙版渐变写在 min-aspect-ratio:1/1 里（只对横屏生效），
   手机竖屏等于完全没有蒙版，背景图会把文字压得看不清。
   这里用更高的选择器 + !important 直接接管，窄屏统一加厚。
   ========================================================================== */
@media (max-width: 900px) {
    /* ⚠ 这里必须用 background-image，不能用 background 简写：
       简写会把「背景图」那一层一并重置掉。首页把背景图放在 body::before 的
       fixed 伪元素上（为了让 iOS 也能固定背景），用简写会让它整个丢掉背景图。
       页面若定义了 --rin-page-bg（背景图 url）就跟着带上；没定义则为 none，无副作用。 */
    html body::before,
    html body.rin-theme::before {
        background-image:
            radial-gradient(900px 460px at 10% -10%, rgba(255, 236, 168, .50), transparent 62%),
            radial-gradient(700px 420px at 96% 2%, rgba(255, 196, 220, .40), transparent 62%),
            linear-gradient(180deg,
                rgba(255, 253, 247, .80) 0%,
                rgba(255, 251, 238, .85) 45%,
                rgba(255, 247, 224, .92) 100%),
            var(--rin-page-bg, none) !important;
        background-size: auto, auto, auto, cover !important;
        background-position: center, center, center, center !important;
        background-repeat: no-repeat !important;
    }
}
/* 消息气泡本身已有较高不透明底色，这里只做轻量兜底，不降低其可读性 */
@media (max-width: 900px) {
    .hero, .card, .text-preview {
        backdrop-filter: saturate(1.05) blur(1px);
    }
}

/* ==========================================================================
   6. 首页顶栏：低频按钮已降级为纯图标，允许按内容宽度排列（一行放得下）
      默认那条 `.topbar-actions > *` 是「各占 50% 的两列」，会把 5 个按钮挤成 3 行，
      首屏几乎全是导航。这里只对首页（带 -home 后缀）放开。
   ========================================================================== */
@media (max-width: 900px) {
    .topbar-actions-home > * {
        flex: 0 0 auto;
        max-width: none;
    }
    .topbar-actions-home > .tb-user {
        flex: 1 1 100%;
        max-width: 100%;
    }
    /* 首页只有 3 个统计块，3 列正好一行（默认的 2 列会剩一个孤零零换行） */
    .hero-stats {
        grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    }
}
