:root {
    color-scheme: dark;
    --app-height: 100dvh;
    --app-top: 0px;
    --bg: #070b16;
    --panel: rgba(17, 24, 43, .88);
    --panel-solid: #11182b;
    --panel-soft: #182139;
    --line: rgba(255, 255, 255, .08);
    --text: #f7f9ff;
    --muted: #8f9bb8;
    --primary: #6d7cff;
    --primary-2: #ad6dff;
    --success: #35d39a;
    --danger: #ff647c;
    --warning: #ffb454;
    --shadow: 0 24px 80px rgba(0, 0, 0, .38);
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
    min-height: 100%;
    margin: 0;
}

body {
    overflow: hidden;
    overscroll-behavior: none;
    color: var(--text);
    background:
        radial-gradient(circle at 15% 10%, rgba(109, 124, 255, .18), transparent 30%),
        radial-gradient(circle at 85% 85%, rgba(173, 109, 255, .14), transparent 32%),
        var(--bg);
    font: 14px/1.5 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
textarea {
    font: inherit;
}

button {
    color: inherit;
}

.hidden {
    display: none !important;
}

.app-loading,
.auth-page {
    min-height: var(--app-height, 100dvh);
    display: grid;
    place-items: center;
    padding: 24px;
}

.loading-card {
    display: grid;
    justify-items: center;
    gap: 10px;
    color: var(--muted);
}

.loading-card strong {
    color: var(--text);
    font-size: 24px;
}

.brand-mark {
    width: 48px;
    height: 48px;
    display: grid;
    place-items: center;
    border-radius: 16px;
    color: white;
    font-size: 21px;
    font-weight: 800;
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    box-shadow: 0 12px 36px rgba(109, 124, 255, .35);
}

.auth-wrap {
    width: min(950px, 100%);
    display: grid;
    grid-template-columns: 1.08fr .92fr;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 28px;
    background: rgba(9, 14, 29, .82);
    box-shadow: var(--shadow);
    backdrop-filter: blur(24px);
}

.auth-intro {
    min-height: 610px;
    padding: 58px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    background:
        linear-gradient(145deg, rgba(109, 124, 255, .22), rgba(173, 109, 255, .08)),
        radial-gradient(circle at 25% 20%, rgba(255, 255, 255, .12), transparent 35%);
}

.brand {
    display: flex;
    align-items: center;
    gap: 13px;
    font-weight: 800;
    font-size: 22px;
}

.auth-copy h1 {
    max-width: 430px;
    margin: 0 0 18px;
    font-size: clamp(36px, 5vw, 58px);
    line-height: 1.08;
    letter-spacing: -.04em;
}

.auth-copy p {
    max-width: 430px;
    margin: 0;
    color: #c7cee4;
    font-size: 16px;
}

.feature-pills {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.feature-pills span {
    padding: 8px 12px;
    border: 1px solid rgba(255, 255, 255, .1);
    border-radius: 999px;
    color: #dce2f7;
    background: rgba(255, 255, 255, .05);
}

.auth-panel {
    padding: 50px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.auth-panel h2 {
    margin: 22px 0 6px;
    font-size: 28px;
}

.auth-panel > p {
    margin: 0 0 26px;
    color: var(--muted);
}

.auth-tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    padding: 4px;
    border-radius: 13px;
    background: rgba(255, 255, 255, .05);
}

.auth-tab {
    border: 0;
    padding: 10px;
    border-radius: 10px;
    color: var(--muted);
    background: transparent;
    cursor: pointer;
}

.auth-tab.active {
    color: white;
    background: var(--panel-soft);
    box-shadow: 0 6px 16px rgba(0, 0, 0, .18);
}

.auth-form {
    display: grid;
    gap: 16px;
    margin-top: 22px;
}

.auth-install {
    width: 100%;
    margin-top: 12px;
}

.is-standalone .install-app-btn {
    display: none !important;
}

.field {
    display: grid;
    gap: 7px;
}

.field label {
    color: #bac3da;
    font-size: 13px;
}

.field input,
.field textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 13px;
    outline: none;
    color: var(--text);
    background: rgba(255, 255, 255, .04);
    transition: .2s ease;
}

.field input {
    height: 46px;
    padding: 0 14px;
}

.field textarea {
    padding: 12px 14px;
    resize: none;
}

.field input:focus,
.field textarea:focus {
    border-color: rgba(109, 124, 255, .8);
    box-shadow: 0 0 0 4px rgba(109, 124, 255, .12);
}

.primary-btn,
.soft-btn,
.icon-btn,
.mini-btn,
.danger-btn {
    border: 0;
    cursor: pointer;
    transition: transform .15s ease, opacity .15s ease, background .15s ease;
}

.primary-btn:active,
.soft-btn:active,
.icon-btn:active,
.mini-btn:active,
.danger-btn:active {
    transform: scale(.97);
}

.primary-btn {
    min-height: 46px;
    padding: 0 18px;
    border-radius: 13px;
    color: white;
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    box-shadow: 0 10px 24px rgba(109, 124, 255, .24);
}

.primary-btn:disabled,
.soft-btn:disabled,
.mini-btn:disabled {
    opacity: .48;
    cursor: default;
}

.chat-app {
    width: 100vw;
    height: var(--app-height, 100dvh);
    min-height: 0;
    display: grid;
    grid-template-columns: 350px minmax(0, 1fr);
}

.sidebar {
    min-width: 0;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--line);
    background: rgba(12, 17, 32, .92);
    backdrop-filter: blur(22px);
}

.sidebar-header {
    padding: 20px 18px 14px;
    border-bottom: 1px solid var(--line);
}

.sidebar-brand-row,
.current-user,
.chat-header,
.person-row,
.request-actions,
.chat-tools {
    display: flex;
    align-items: center;
}

.sidebar-brand-row {
    justify-content: space-between;
    margin-bottom: 18px;
}

.sidebar-brand-row .brand {
    font-size: 18px;
}

.sidebar-brand-row .brand-mark {
    width: 38px;
    height: 38px;
    border-radius: 12px;
    font-size: 17px;
}

.header-actions {
    display: flex;
    gap: 7px;
}

.icon-btn {
    width: 36px;
    height: 36px;
    display: grid;
    place-items: center;
    border-radius: 11px;
    color: var(--muted);
    background: rgba(255, 255, 255, .045);
}

.icon-btn:hover {
    color: white;
    background: rgba(255, 255, 255, .09);
}

.current-user {
    gap: 11px;
    margin-bottom: 15px;
}

.user-meta {
    min-width: 0;
    display: grid;
    flex: 1;
}

.user-meta strong,
.person-main strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-meta span,
.person-main span {
    color: var(--muted);
    font-size: 12px;
}

.avatar {
    position: relative;
    flex: 0 0 auto;
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border-radius: 14px;
    color: white;
    font-weight: 800;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .12);
}

.avatar-blue { background: #5b7cfa; }
.avatar-purple { background: #8b5cf6; }
.avatar-pink { background: #ec4899; }
.avatar-orange { background: #f97316; }
.avatar-teal { background: #14b8a6; }
.avatar-green { background: #22c55e; }
.avatar-sky { background: #0ea5e9; }

.avatar.large {
    width: 46px;
    height: 46px;
}

.online-dot {
    position: absolute;
    right: -2px;
    bottom: -2px;
    width: 12px;
    height: 12px;
    border: 2px solid #11182b;
    border-radius: 50%;
    background: var(--success);
}

.search-box {
    position: relative;
}

.search-box input {
    width: 100%;
    height: 42px;
    padding: 0 38px 0 14px;
    border: 1px solid var(--line);
    border-radius: 12px;
    outline: none;
    color: var(--text);
    background: rgba(255, 255, 255, .045);
}

.search-box span {
    position: absolute;
    right: 13px;
    top: 10px;
    color: var(--muted);
}

.sidebar-scroll {
    flex: 1;
    overflow: auto;
    padding: 12px;
}

.section-label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 7px 6px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: .06em;
    text-transform: uppercase;
}

.count-badge,
.unread-badge {
    display: inline-grid;
    place-items: center;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 999px;
    color: white;
    font-size: 11px;
    background: var(--danger);
}

.person-row {
    width: 100%;
    gap: 11px;
    padding: 10px;
    border: 0;
    border-radius: 14px;
    text-align: left;
    color: inherit;
    background: transparent;
}

button.person-row {
    cursor: pointer;
}

.person-row:hover,
.person-row.active {
    background: rgba(255, 255, 255, .06);
}

.person-row.active {
    box-shadow: inset 0 0 0 1px rgba(109, 124, 255, .25);
    background: rgba(109, 124, 255, .11);
}

.person-main {
    min-width: 0;
    display: grid;
    flex: 1;
}

.person-subline {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.person-side {
    display: grid;
    justify-items: end;
    gap: 4px;
    color: var(--muted);
    font-size: 11px;
}

.mini-btn {
    min-height: 30px;
    padding: 0 11px;
    border-radius: 9px;
    color: white;
    background: rgba(109, 124, 255, .2);
}

.mini-btn.accept {
    background: rgba(53, 211, 154, .18);
    color: #8df0cb;
}

.mini-btn.reject {
    color: #ff9aaa;
    background: rgba(255, 100, 124, .14);
}

.request-actions {
    gap: 6px;
}

.empty-list {
    padding: 26px 14px;
    color: var(--muted);
    text-align: center;
    border: 1px dashed var(--line);
    border-radius: 14px;
}

.chat-main {
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background:
        radial-gradient(circle at 70% 15%, rgba(109, 124, 255, .08), transparent 34%),
        rgba(7, 11, 22, .7);
}

.welcome-panel {
    flex: 1;
    display: grid;
    place-items: center;
    padding: 30px;
    text-align: center;
}

.welcome-card {
    max-width: 430px;
}

.welcome-card .brand-mark {
    width: 70px;
    height: 70px;
    margin: 0 auto 20px;
    border-radius: 22px;
    font-size: 28px;
}

.welcome-card h2 {
    margin: 0 0 8px;
    font-size: 28px;
}

.welcome-card p {
    color: var(--muted);
}

.chat-view {
    min-height: 0;
    flex: 1;
    display: flex;
    flex-direction: column;
}

.chat-header {
    min-height: 76px;
    gap: 12px;
    padding: 14px 22px;
    border-bottom: 1px solid var(--line);
    background: rgba(11, 16, 31, .64);
    backdrop-filter: blur(20px);
}

.chat-header .person-main {
    max-width: 500px;
}

.chat-tools {
    margin-left: auto;
    gap: 8px;
}

.privacy-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 9px;
    border-radius: 999px;
    color: #85e8c8;
    font-size: 10px;
    background: rgba(53, 211, 154, .09);
}

.privacy-chip::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--success);
    box-shadow: 0 0 9px var(--success);
}

.messages {
    min-height: 0;
    flex: 1;
    overflow-y: auto;
    padding: 28px clamp(18px, 5vw, 70px);
    scroll-behavior: smooth;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

.message-row {
    display: flex;
    margin: 8px 0;
}

.message-row.mine {
    justify-content: flex-end;
}

.message-bubble {
    max-width: min(68%, 680px);
    padding: 10px 13px 7px;
    border: 1px solid var(--line);
    border-radius: 17px 17px 17px 5px;
    background: var(--panel-soft);
    box-shadow: 0 8px 22px rgba(0, 0, 0, .12);
}

.message-row.mine .message-bubble {
    border-color: rgba(109, 124, 255, .2);
    border-radius: 17px 17px 5px 17px;
    background: linear-gradient(135deg, #5267ef, #785ce3);
}

.message-text {
    white-space: pre-wrap;
    word-break: break-word;
}

.message-time {
    display: block;
    margin-top: 4px;
    color: rgba(255, 255, 255, .58);
    font-size: 10px;
    text-align: right;
}

.message-expiry {
    margin-left: 5px;
    color: rgba(255, 255, 255, .48);
}

.message-day {
    width: max-content;
    margin: 18px auto;
    padding: 4px 10px;
    border-radius: 999px;
    color: var(--muted);
    font-size: 11px;
    background: rgba(255, 255, 255, .05);
}

.composer {
    padding: 14px clamp(16px, 4vw, 48px) calc(14px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: rgba(11, 16, 31, .72);
    backdrop-filter: blur(20px);
}

.composer-inner {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    max-width: 1000px;
    margin: auto;
}

.composer textarea {
    min-height: 46px;
    max-height: 150px;
    flex: 1;
    padding: 12px 14px;
    resize: none;
    border: 1px solid var(--line);
    border-radius: 15px;
    outline: none;
    color: var(--text);
    background: rgba(255, 255, 255, .05);
}

.composer textarea:focus {
    border-color: rgba(109, 124, 255, .55);
}

.send-btn {
    width: 46px;
    height: 46px;
    padding: 0;
    border-radius: 15px;
}

.composer-note {
    max-width: 1000px;
    margin: 7px auto 0;
    color: var(--muted);
    font-size: 11px;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(2, 5, 12, .72);
    backdrop-filter: blur(10px);
}

.modal {
    width: min(480px, 100%);
    padding: 24px;
    border: 1px solid var(--line);
    border-radius: 22px;
    background: var(--panel-solid);
    box-shadow: var(--shadow);
}

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
}

.modal-head h3 {
    margin: 0;
    font-size: 20px;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 20px;
}

.install-steps {
    display: grid;
    gap: 12px;
    margin: 8px 0 0;
    padding: 0;
    list-style: none;
}

.install-steps li {
    display: flex;
    align-items: flex-start;
    gap: 11px;
    padding: 11px 12px;
    border-radius: 12px;
    color: #dbe2f6;
    background: rgba(255, 255, 255, .045);
}

.install-steps b {
    flex: 0 0 auto;
    width: 24px;
    height: 24px;
    display: grid;
    place-items: center;
    border-radius: 8px;
    color: white;
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
}

.soft-btn,
.danger-btn {
    min-height: 40px;
    padding: 0 15px;
    border-radius: 11px;
}

.soft-btn {
    background: rgba(255, 255, 255, .07);
}

.danger-btn {
    color: #ff9aaa;
    background: rgba(255, 100, 124, .13);
}

.setting-hint,
.cleanup-status {
    color: var(--muted);
    font-size: 12px;
}

.cleanup-status {
    min-height: 44px;
    margin-top: 14px;
    padding: 10px 12px;
    border-radius: 11px;
    background: rgba(255, 255, 255, .04);
}

#toast-root {
    position: fixed;
    z-index: 50;
    top: 18px;
    right: 18px;
    display: grid;
    gap: 10px;
}

.connection-status {
    position: fixed;
    z-index: 45;
    top: max(10px, env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
    max-width: calc(100vw - 24px);
    padding: 8px 13px;
    border: 1px solid rgba(255, 180, 84, .32);
    border-radius: 999px;
    color: #ffe0ad;
    font-size: 12px;
    white-space: nowrap;
    background: rgba(70, 43, 15, .94);
    box-shadow: 0 10px 30px rgba(0, 0, 0, .3);
}

.connection-status[hidden] {
    display: none;
}

.toast {
    min-width: 220px;
    max-width: 360px;
    padding: 11px 14px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #182139;
    box-shadow: 0 12px 34px rgba(0, 0, 0, .3);
    animation: toast-in .2s ease;
}

.toast.error {
    border-color: rgba(255, 100, 124, .28);
}

@keyframes toast-in {
    from { transform: translateY(-8px); opacity: 0; }
}

.mobile-back {
    display: none;
}

@media (max-width: 760px) {
    body {
        overflow: hidden;
    }

    .auth-wrap {
        grid-template-columns: 1fr;
    }

    .auth-intro {
        display: none;
    }

    .auth-panel {
        min-height: calc(100vh - 48px);
        padding: 30px 24px;
    }

    .chat-app {
        position: fixed;
        top: var(--app-top, 0px);
        right: 0;
        bottom: auto;
        left: 0;
        display: block;
        height: var(--app-height, 100dvh);
    }

    .sidebar,
    .chat-main {
        width: 100%;
        height: var(--app-height, 100dvh);
        min-height: 0;
    }

    .chat-view {
        height: 100%;
        min-height: 0;
    }

    .chat-app.chat-open .sidebar {
        display: none;
    }

    .chat-app:not(.chat-open) .chat-main {
        display: none;
    }

    .mobile-back {
        display: grid;
    }

    .messages {
        padding: 20px 12px;
    }

    .message-bubble {
        max-width: 84%;
    }

    .chat-header {
        padding: 12px;
    }

    .privacy-chip {
        display: none;
    }

    .composer {
        padding-left: 10px;
        padding-right: 10px;
        padding-bottom: max(12px, env(safe-area-inset-bottom));
    }
}

/* ============================================================
   Telegram 风格聊天皮肤（覆盖式追加，级联优先）
   对齐 Telegram iOS：浅色背景、己方绿气泡/对方白气泡、圆形头像、
   胶囊输入 + 圆形发送、气泡内时间 + 已读双勾。仅改聊天相关界面。
   ============================================================ */
:root {
    --tg-page: #e9edf1;
    --tg-surface: #ffffff;
    --tg-ink: #0f1620;
    --tg-sub: #8a97a6;
    --tg-line: #e9edf2;
    --tg-accent: #3390ec;
    --tg-out: #e4fdd0;
    --tg-out-ink: #12240a;
    --tg-tick: #3ea6ff;
    --tg-wallpaper:
        radial-gradient(circle at 16% 12%, rgba(120, 162, 214, .20), transparent 30%),
        radial-gradient(circle at 84% 90%, rgba(150, 196, 150, .18), transparent 32%),
        linear-gradient(180deg, #cfdae7, #dde6ed 55%, #e7edf1);
}

.chat-app {
    color-scheme: light;
    background: var(--tg-page);
}

/* ---- 会话列表 / 侧栏 ---- */
.sidebar {
    background: var(--tg-surface);
    border-right: 1px solid var(--tg-line);
    backdrop-filter: none;
}
.sidebar-header { border-bottom: 1px solid var(--tg-line); }
.sidebar-brand-row .brand,
.current-user .user-meta strong { color: var(--tg-ink); }
.icon-btn { color: var(--tg-sub); background: #eef1f5; }
.icon-btn:hover { color: var(--tg-ink); background: #e3e8ee; }
.search-box input {
    height: 40px;
    color: var(--tg-ink);
    background: #eef1f5;
    border: 1px solid transparent;
    border-radius: 999px;
}
.search-box span { color: var(--tg-sub); }
.section-label { color: var(--tg-sub); }

.person-row { border-radius: 12px; color: var(--tg-ink); }
.person-row:hover { background: #f2f4f7; }
.person-row.active {
    background: #e9f2fd;
    box-shadow: inset 0 0 0 1px rgba(51, 144, 236, .22);
}
.person-main strong { color: var(--tg-ink); font-size: 16px; font-weight: 600; }
.current-user .user-meta span,
.person-main span,
.person-subline,
.person-side { color: var(--tg-sub); }

/* 圆形头像 */
.avatar {
    width: 52px;
    height: 52px;
    border-radius: 50%;
    font-size: 19px;
    box-shadow: none;
}
.avatar.large { width: 40px; height: 40px; border-radius: 50%; }
.online-dot { border-color: #fff; }

/* 未读徽标：Telegram 蓝圆 */
.unread-badge,
.count-badge { background: var(--tg-accent); }

/* ---- 顶栏 ---- */
.chat-main { background: var(--tg-wallpaper); }
.chat-header {
    min-height: 60px;
    background: rgba(255, 255, 255, .86);
    border-bottom: 1px solid var(--tg-line);
    -webkit-backdrop-filter: blur(18px);
    backdrop-filter: blur(18px);
}
.chat-header .person-main strong { color: var(--tg-ink); }
.chat-header .person-main span { color: var(--tg-sub); }
.chat-header .icon-btn { background: transparent; color: var(--tg-accent); }
.chat-header .icon-btn:hover { background: rgba(51, 144, 236, .1); }
.privacy-chip { color: #2e9e6f; background: rgba(46, 158, 111, .1); }

/* ---- 消息区 ---- */
.welcome-card h2 { color: var(--tg-ink); }
.welcome-card p { color: var(--tg-sub); }
.message-day {
    color: #fff;
    background: rgba(0, 0, 0, .26);
    -webkit-backdrop-filter: blur(6px);
    backdrop-filter: blur(6px);
}
.message-bubble {
    border: 0;
    border-radius: 16px 16px 16px 6px;
    color: var(--tg-ink);
    background: var(--tg-surface);
    box-shadow: 0 1px 1.5px rgba(0, 0, 0, .14);
}
.message-row.mine .message-bubble {
    border: 0;
    border-radius: 16px 16px 6px 16px;
    color: var(--tg-out-ink);
    background: var(--tg-out);
}
.message-time { color: rgba(0, 0, 0, .42); }
.message-row.mine .message-time { color: rgba(24, 92, 12, .6); }
.message-ticks { margin-left: 4px; font-weight: 700; letter-spacing: -3px; }
.message-ticks.read { color: var(--tg-tick); }

/* ---- 输入区：胶囊 + 圆形发送 ---- */
.composer {
    background: rgba(255, 255, 255, .92);
    border-top: 1px solid var(--tg-line);
    -webkit-backdrop-filter: blur(18px);
    backdrop-filter: blur(18px);
}
.composer-inner { align-items: center; }
.composer textarea {
    min-height: 44px;
    padding: 11px 16px;
    color: var(--tg-ink);
    background: #eef1f5;
    border: 1px solid transparent;
    border-radius: 22px;
}
.composer textarea:focus { background: #fff; border-color: rgba(51, 144, 236, .5); }
.send-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--tg-accent);
    box-shadow: 0 4px 12px rgba(51, 144, 236, .34);
}
.composer-note { color: var(--tg-sub); }

@media (max-width: 760px) {
    /* 顶栏：返回(左) · 名称(居中) · 更多 · 头像(最右)，对齐参考图 */
    .chat-header { gap: 10px; padding: 10px 12px; }
    .chat-header .mobile-back { order: 1; }
    .chat-header .person-main { order: 2; flex: 1; text-align: center; }
    .chat-header .chat-tools { order: 3; margin-left: 0; }
    .chat-header .avatar.large { order: 4; }
    .messages { padding: 14px 10px; }
    .message-bubble { max-width: 80%; }
}
