/* ==================== 基础 ==================== */
:root {
    --bg: #f5f6f8;
    --card: #ffffff;
    --text: #16181d;
    --muted: #8a909c;
    --line: #e7e9ee;
    --accent: #2f6bff;
    --danger: #e5484d;
    --bar: rgba(255, 255, 255, .92);
    --shadow: 0 12px 32px rgba(16, 20, 30, .14);
    --radius: 14px;
    --photo-h: 120px; /* 每行图片的统一高度 */
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1115;
        --card: #181b21;
        --text: #e9ecf1;
        --muted: #8b93a1;
        --line: #272b34;
        --accent: #4a7dff;
        --bar: rgba(24, 27, 33, .92);
        --shadow: 0 12px 32px rgba(0, 0, 0, .5);
    }
}

* { box-sizing: border-box; }

html { height: 100%; }

body {
    margin: 0;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
                 "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

/* ==================== 通用控件 ==================== */
.btn {
    height: 46px;
    padding: 0 18px;
    border: 1px solid transparent;
    border-radius: 12px;
    font-family: inherit;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: transform .08s ease, opacity .15s ease, background .15s ease;
    -webkit-user-select: none;
    user-select: none;
}

.btn:active { transform: scale(.985); }
.btn[disabled] { opacity: .5; pointer-events: none; }

.btn-primary { background: var(--accent); color: #fff; }
.btn-ghost { background: var(--card); color: var(--text); border-color: var(--line); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-block { display: block; width: 100%; }

/* 卡片里的轻量文字按钮：时间 / 复制 / 删除共用 */
.mini-btn {
    border: 0;
    background: none;
    padding: 3px 7px;
    border-radius: 7px;
    font-family: inherit;
    font-size: 12px;
    line-height: 1.6;
    color: var(--muted);
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
    white-space: nowrap;
}

.mini-btn:active { background: var(--line); }
.mini-btn.primary { color: var(--accent); font-weight: 600; }
.mini-btn.danger { color: #fff; background: var(--danger); }
.mini-btn.danger:active { background: var(--danger); }

.mini-time {
    padding: 3px 2px;
    color: var(--muted);
    font-size: 11px;
    white-space: nowrap;
}

.text-btn {
    border: 0;
    background: none;
    padding: 8px 6px;
    font-family: inherit;
    font-size: 15px;
    color: var(--muted);
    cursor: pointer;
    min-height: 40px;
}

.icon-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: 10px;
    background: none;
    color: var(--muted);
    cursor: pointer;
}

.icon-btn:active { background: var(--line); }
.icon-btn.spin svg { animation: spin .6s linear; }

@keyframes spin { to { transform: rotate(360deg); } }

/* ==================== 连接码页 ==================== */
.gate {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow-y: auto;
    padding: 28px 20px calc(28px + env(safe-area-inset-bottom));
}

.gate-card {
    width: 100%;
    max-width: 360px;
    text-align: center;
}

.gate-title {
    margin: 0 0 10px;
    font-size: 30px;
    font-weight: 700;
    letter-spacing: .06em;
}

.gate-desc {
    margin: 0 0 26px;
    color: var(--muted);
    font-size: 14px;
    line-height: 1.7;
}

.gate-input {
    width: 100%;
    height: 54px;
    padding: 0 16px;
    margin-bottom: 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--card);
    color: var(--text);
    font-family: inherit;
    font-size: 19px;
    font-weight: 600;
    letter-spacing: .08em;
    text-align: center;
    outline: none;
    transition: border-color .15s ease;
}

.gate-input::placeholder { font-size: 15px; font-weight: 400; letter-spacing: 0; }
.gate-input:focus { border-color: var(--accent); }

.gate-error {
    margin: 0 0 12px;
    color: var(--danger);
    font-size: 13px;
    text-align: center;
}

.gate-recent {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    margin-top: 20px;
}

.chip {
    border: 1px solid var(--line);
    background: var(--card);
    border-radius: 99px;
    padding: 7px 14px;
    font-family: inherit;
    font-size: 13px;
    color: var(--text);
    cursor: pointer;
}

.gate-tip {
    margin: 22px 0 0;
    color: var(--muted);
    font-size: 12px;
}

/* ==================== 主界面 ==================== */
.app {
    flex: 1;
    min-height: 0;
    width: 100%;
    max-width: 720px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 8px 16px;
    padding-top: calc(8px + env(safe-area-inset-top));
    border-bottom: 1px solid var(--line);
    background: var(--bar);
    backdrop-filter: blur(12px);
}

.topbar-room { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.topbar-label { color: var(--muted); font-size: 12px; }
.topbar-code {
    font-size: 17px;
    font-weight: 700;
    letter-spacing: .04em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.topbar-actions { display: flex; align-items: center; gap: 2px; flex-shrink: 0; }

.list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 10px 14px 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.load-more {
    flex-shrink: 0;
    padding: 6px 14px 10px;
    text-align: center;
    color: var(--muted);
    font-size: 12px;
}

/* 收到新内容时的提示条，点一下回到最新 */
.new-tip {
    flex-shrink: 0;
    align-self: center;
    max-width: calc(100% - 28px);
    margin: 8px 14px 0;
    padding: 7px 16px;
    border: 0;
    border-radius: 99px;
    background: var(--accent);
    color: #fff;
    font-family: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 6px 18px rgba(47, 107, 255, .32);
    animation: fade .15s ease;
}

.new-tip:active { transform: scale(.97); }

/* ==================== 文字消息卡片 ==================== */
.item {
    position: relative;
    flex-shrink: 0; /* 内容变多时不能被压缩，交给滚动区滚动 */
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
}

.item-text-msg { padding: 10px 12px; }

/* 时间 / 复制 / 删除浮在右上角，不额外占一行 */
.text-tools {
    float: right;
    display: flex;
    align-items: center;
    gap: 0;
    margin: 0 0 2px 10px;
}

.item-text {
    margin: 0;
    font-size: 15px;
    line-height: 1.65;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.item-text.clamped {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 8;
    overflow: hidden;
}

.item-more {
    display: inline-block;
    margin-top: 4px;
    border: 0;
    background: none;
    padding: 2px 0;
    color: var(--accent);
    font-family: inherit;
    font-size: 12px;
    cursor: pointer;
}

/* ==================== 图片流（相邻图片自动并排） ==================== */
.photo-row {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 6px;
    flex-shrink: 0;
}

/*
 * 每个格子高度固定、宽度由图片自身比例决定，
 * 因此同一行里的图片高度一定一致，一行放不下会自动换行。
 */
.photo-cell {
    position: relative;
    flex: 0 0 auto;
    height: var(--photo-h);
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--bg);
    overflow: hidden;
}

.photo-img {
    display: block;
    height: 100%;
    width: auto;
    object-fit: cover;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.photo-img:active { opacity: .88; }

/* 时间和删除浮在图片底部，不额外占用高度 */
.photo-bar {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 2px;
    padding: 8px 3px 2px 6px;
    background: linear-gradient(to top, rgba(0, 0, 0, .62), rgba(0, 0, 0, 0));
    pointer-events: none;
}

.photo-bar .mini-time {
    padding: 0;
    color: #fff;
    font-size: 10px;
    text-shadow: 0 1px 2px rgba(0, 0, 0, .5);
}

.photo-bar .mini-btn {
    pointer-events: auto;
    padding: 1px 6px;
    border-radius: 6px;
    color: #fff;
    background: rgba(255, 255, 255, .22);
    font-size: 10px;
}

.photo-bar .mini-btn:active { background: rgba(255, 255, 255, .4); }
.photo-bar .mini-btn.danger { background: var(--danger); }

/* ==================== 文件网格（一条消息含多个文件） ==================== */
.media-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px;
}

.media-cell {
    display: flex;
    flex-direction: column;
    gap: 4px;
    width: calc((100% - 12px) / 3);
    min-width: 0;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.media-grid.one .media-cell { width: min(52%, 200px); }

.media-cell:active { opacity: .8; }

.media-thumb {
    display: block;
    width: 100%;
    aspect-ratio: 1 / 1;
    object-fit: cover;
    border-radius: 10px;
    background: var(--bg);
}

.media-box {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    aspect-ratio: 1 / 1;
    border-radius: 10px;
    font-size: 13px;
    font-weight: 700;
    letter-spacing: .02em;
}

.media-name {
    margin: 0;
    font-size: 11px;
    line-height: 1.35;
    color: var(--muted);
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.media-ext {
    margin: 0;
    font-size: 10px;
    color: var(--muted);
    text-align: center;
}

.item-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 0 6px 0 10px;
    min-height: 30px;
    border-top: 1px solid var(--line);
}

.item-actions { display: flex; align-items: center; gap: 2px; }

/* ==================== 空状态 ==================== */
.empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    color: var(--muted);
    padding-bottom: 60px;
}

.empty-icon {
    width: 66px;
    height: 66px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: var(--card);
    border: 1px solid var(--line);
    margin-bottom: 10px;
}

.empty-title { margin: 0; font-size: 15px; font-weight: 600; color: var(--text); }
.empty-sub { margin: 0; font-size: 13px; }

/* ==================== 底部操作栏 ==================== */
.composer {
    display: flex;
    gap: 10px;
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: var(--bar);
    backdrop-filter: blur(12px);
}

.composer .btn { flex: 1; }

/* ==================== 文字输入弹层 ==================== */
.sheet {
    position: fixed;
    inset: 0;
    z-index: 70;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    background: rgba(15, 17, 21, .4);
    animation: fade .15s ease;
}

.sheet-panel {
    display: flex;
    flex-direction: column;
    max-height: 90dvh;
    padding: 6px 14px calc(12px + env(safe-area-inset-bottom));
    background: var(--card);
    border-radius: 18px 18px 0 0;
    animation: sheet-up .2s ease;
}

.sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 2px 10px;
}

.sheet-title { font-size: 15px; font-weight: 600; }
.sheet-count { color: var(--muted); font-size: 12px; }

.sheet-textarea {
    flex: 1;
    min-height: 34vh;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--bg);
    color: var(--text);
    font-family: inherit;
    font-size: 16px;
    line-height: 1.6;
    resize: none;
    outline: none;
}

.sheet-textarea:focus { border-color: var(--accent); }

/* 操作按钮放在弹层底部，加大方便手指点击 */
.sheet-foot {
    display: flex;
    gap: 12px;
    padding-top: 12px;
}

.sheet-foot .btn {
    flex: 1;
    height: 54px;
    font-size: 17px;
    border-radius: 14px;
}

@keyframes sheet-up { from { transform: translateY(16px); opacity: .6; } }
@keyframes fade { from { opacity: 0; } }

/* ==================== 确认弹窗 ==================== */
.dialog {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: rgba(15, 17, 21, .45);
    animation: fade .15s ease;
}

.dialog-card {
    width: min(92vw, 340px);
    padding: 20px;
    border-radius: 16px;
    background: var(--card);
    box-shadow: var(--shadow);
}

.dialog-message {
    margin: 0 0 18px;
    font-size: 15px;
    line-height: 1.55;
    text-align: center;
    overflow-wrap: anywhere;
}

.dialog-actions { display: flex; gap: 10px; }
.dialog-actions .btn { flex: 1; }

/* ==================== 提示与进度 ==================== */
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(90px + env(safe-area-inset-bottom));
    z-index: 120;
    transform: translateX(-50%);
    max-width: 86vw;
    padding: 10px 18px;
    border-radius: 99px;
    background: rgba(22, 24, 29, .93);
    color: #fff;
    font-size: 14px;
    text-align: center;
    animation: fade .15s ease;
}

.progress {
    position: fixed;
    top: calc(12px + env(safe-area-inset-top));
    left: 50%;
    z-index: 120;
    width: min(90vw, 340px);
    transform: translateX(-50%);
    padding: 10px 14px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--card);
    box-shadow: var(--shadow);
}

.progress-text {
    font-size: 13px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.progress-bar {
    height: 5px;
    margin-top: 8px;
    border-radius: 99px;
    background: var(--line);
    overflow: hidden;
}

.progress-bar i {
    display: block;
    width: 0;
    height: 100%;
    border-radius: 99px;
    background: var(--accent);
    transition: width .15s ease;
}

/* ==================== 桌面端微调 ==================== */
@media (min-width: 720px) {
    .sheet { align-items: center; justify-content: center; }
    .sheet-panel { width: min(560px, 92vw); border-radius: 18px; max-height: 76dvh; }
    .sheet-textarea { min-height: 220px; }
    .item:hover { border-color: #d8dce4; }
    .toast { bottom: 40px; }

    .photo-cell { height: 150px; }
    .media-cell { width: calc((100% - 24px) / 4); }
}
