* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #0b0e13;
    --bg-accent: #121820;
    --surface: #161c26;
    --surface-hover: #1e2633;
    --surface-elevated: #222b3a;
    --primary: #4f8cff;
    --primary-hover: #3a7af0;
    --primary-glow: rgba(79, 140, 255, 0.25);
    --text: #eef2f8;
    --text-secondary: #a8b4c7;
    --text-muted: #6b7a90;
    --border: rgba(255, 255, 255, 0.07);
    --border-light: rgba(255, 255, 255, 0.12);
    --success: #34d399;
    --error: #f87171;
    --pin: #fbbf24;
    --radius-sm: 8px;
    --radius-md: 14px;
    --radius-lg: 20px;
    --radius-xl: 24px;
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 20px 60px rgba(0, 0, 0, 0.5);
    --header-h: 56px;
    --footer-h: auto;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
}

/* ========== 登录页 ========== */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 24px;
    background:
        radial-gradient(ellipse 80% 60% at 50% -10%, rgba(79, 140, 255, 0.12), transparent),
        var(--bg);
}

.login-box {
    width: 100%;
    max-width: 400px;
    background: var(--surface);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-xl);
    padding: 44px 36px;
    text-align: center;
    box-shadow: var(--shadow-lg);
}

.login-icon {
    width: 64px;
    height: 64px;
    margin: 0 auto 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    background: linear-gradient(135deg, rgba(79, 140, 255, 0.2), rgba(79, 140, 255, 0.05));
    border: 1px solid rgba(79, 140, 255, 0.2);
    border-radius: 18px;
}

.login-box h1 {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.02em;
    margin-bottom: 10px;
}

.login-desc {
    color: var(--text-secondary);
    font-size: 14px;
    line-height: 1.7;
    margin-bottom: 32px;
}

.error-msg {
    background: rgba(248, 113, 113, 0.12);
    border: 1px solid rgba(248, 113, 113, 0.25);
    color: #fca5a5;
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    margin-bottom: 20px;
}

.input-group { margin-bottom: 20px; }

.input-group input {
    width: 100%;
    padding: 15px 18px;
    background: var(--bg);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    color: var(--text);
    font-size: 16px;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.input-group input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-glow);
}

.btn-primary {
    width: 100%;
    padding: 15px;
    background: linear-gradient(135deg, var(--primary), var(--primary-hover));
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s;
    box-shadow: 0 4px 16px var(--primary-glow);
}

.btn-primary:hover { box-shadow: 0 6px 24px var(--primary-glow); }
.btn-primary:active { transform: scale(0.98); }

/* ========== 主布局 ========== */
.chat-page {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    background:
        radial-gradient(ellipse 100% 80% at 50% -20%, rgba(79, 140, 255, 0.08), transparent),
        var(--bg);
}

.app-shell {
    display: flex;
    flex-direction: column;
    height: 100%;
    max-width: 100%;
    margin: 0 auto;
}

/* ========== 顶栏 ========== */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-h);
    padding: 0 16px;
    background: rgba(22, 28, 38, 0.85);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    z-index: 20;
    position: relative;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.brand-icon {
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    background: linear-gradient(135deg, rgba(79, 140, 255, 0.18), rgba(79, 140, 255, 0.06));
    border: 1px solid rgba(79, 140, 255, 0.15);
    border-radius: 10px;
}

.brand-text { min-width: 0; }

.brand-text h1 {
    font-size: 16px;
    font-weight: 700;
    letter-spacing: -0.01em;
    line-height: 1.2;
}

.msg-count {
    display: block;
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 1px;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.sync-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 10px;
    border-radius: 20px;
    background: rgba(255, 255, 255, 0.04);
}

.sync-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-muted);
    flex-shrink: 0;
}

.sync-indicator.online .sync-dot {
    background: var(--success);
    box-shadow: 0 0 8px rgba(52, 211, 153, 0.6);
}

.sync-indicator.offline .sync-dot { background: var(--error); }

.sync-indicator.syncing .sync-dot {
    background: var(--primary);
    animation: pulse 1.2s ease infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.5; transform: scale(0.85); }
}

.sync-status {
    font-size: 11px;
    color: var(--text-muted);
}

.sync-indicator.online .sync-status { color: var(--success); }
.sync-indicator.offline .sync-status { color: var(--error); }

.desktop-toolbar { display: none; }

.toolbar-btn {
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.03);
    font-size: 15px;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, transform 0.1s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.toolbar-btn:hover {
    background: var(--surface-hover);
    border-color: var(--border-light);
}

.toolbar-btn:active { transform: scale(0.94); }
.toolbar-btn.danger:hover { background: rgba(248, 113, 113, 0.12); border-color: rgba(248, 113, 113, 0.3); }

.btn-menu {
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.03);
    color: var(--text);
    font-size: 17px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.btn-logout {
    font-size: 13px;
    color: var(--text-muted);
    text-decoration: none;
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    transition: color 0.15s, background 0.15s;
    white-space: nowrap;
}

.btn-logout:hover { color: var(--text); background: rgba(255, 255, 255, 0.05); }

/* ========== 搜索 ========== */
.search-bar {
    display: none;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    animation: slideDown 0.2s ease;
}

@keyframes slideDown {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}

.search-bar.show { display: flex; }

.search-icon { font-size: 14px; opacity: 0.5; flex-shrink: 0; }

.search-bar input {
    flex: 1;
    padding: 10px 0;
    background: transparent;
    border: none;
    color: var(--text);
    font-size: 15px;
    outline: none;
}

.search-bar input::placeholder { color: var(--text-muted); }

.btn-search-close {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.06);
    color: var(--text-muted);
    font-size: 18px;
    cursor: pointer;
    flex-shrink: 0;
}

/* ========== 工具菜单（手机） ========== */
.tools-menu {
    display: none;
    position: absolute;
    top: calc(var(--header-h) + 4px);
    right: 12px;
    min-width: 180px;
    background: var(--surface-elevated);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    padding: 6px;
    z-index: 40;
    box-shadow: var(--shadow);
    animation: menuIn 0.18s ease;
}

@keyframes menuIn {
    from { opacity: 0; transform: translateY(-6px) scale(0.97); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

.tools-menu.show { display: block; }

.tools-menu button {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 12px 14px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text);
    font-size: 14px;
    text-align: left;
    cursor: pointer;
}

.tools-menu button span:first-child { font-size: 16px; width: 22px; text-align: center; }
.tools-menu button:active { background: rgba(255, 255, 255, 0.06); }
.tools-menu button.danger { color: var(--error); }

.menu-divider {
    height: 1px;
    background: var(--border);
    margin: 4px 8px;
}

/* ========== 消息区 ========== */
.chat-main {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-height: 0;
    position: relative;
}

.messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}

.message-item {
    width: 100%;
    max-width: 100%;
}

.message-item.is-new {
    animation: msgIn 0.28s ease;
}

.message-item.hidden { display: none; }

.message-item.removing {
    opacity: 0;
    transform: translateX(-16px) scale(0.98);
    transition: opacity 0.2s, transform 0.2s;
}

@keyframes msgIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.message-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    overflow: hidden;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.message-item.pinned .message-card {
    border-color: rgba(251, 191, 36, 0.25);
    background: linear-gradient(135deg, rgba(251, 191, 36, 0.06), var(--surface));
}

.message-item:hover .message-card,
.message-item:focus-within .message-card {
    border-color: var(--border-light);
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
}

.message-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 12px 0;
}

.message-label {
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 500;
}

.pin-tag {
    display: inline-block;
    padding: 2px 8px;
    background: rgba(251, 191, 36, 0.15);
    color: var(--pin);
    border-radius: 10px;
    font-size: 10px;
    font-weight: 600;
}

.message-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    opacity: 0.7;
    transition: opacity 0.15s;
}

.message-item:hover .message-actions,
.message-item:focus-within .message-actions { opacity: 1; }

.btn-action {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.15s, color 0.15s;
}

.btn-action svg {
    width: 16px;
    height: 16px;
}

.btn-action:hover { background: rgba(255, 255, 255, 0.06); color: var(--text); }
.btn-action.btn-copy:hover { color: var(--primary); }
.btn-action.btn-pin:hover,
.btn-action.btn-pin.active { color: var(--pin); background: rgba(251, 191, 36, 0.1); }
.btn-action.btn-delete:hover { color: var(--error); background: rgba(248, 113, 113, 0.1); }

.message-bubble {
    padding: 10px 14px 14px;
    font-size: 15px;
    line-height: 1.65;
    word-break: break-word;
    white-space: pre-wrap;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    color: var(--text);
}

.message-bubble.copied {
    background: rgba(52, 211, 153, 0.08);
}

/* ========== 空状态 ========== */
.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 24px;
    text-align: center;
}

.empty-icon-wrap {
    width: 72px;
    height: 72px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(79, 140, 255, 0.08);
    border: 1px solid rgba(79, 140, 255, 0.12);
    border-radius: 20px;
    margin-bottom: 20px;
}

.empty-icon-wrap .icon { font-size: 32px; }

.empty-title {
    font-size: 17px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 6px;
}

.empty-sub {
    font-size: 13px;
    color: var(--text-muted);
    line-height: 1.6;
}

/* ========== 回到底部 ========== */
.btn-scroll-bottom {
    display: none;
    position: absolute;
    right: 16px;
    bottom: 16px;
    width: 46px;
    height: 46px;
    border: 1px solid var(--border-light);
    border-radius: 50%;
    background: rgba(22, 28, 38, 0.92);
    backdrop-filter: blur(12px);
    color: var(--text);
    cursor: pointer;
    box-shadow: var(--shadow);
    z-index: 5;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.15s, box-shadow 0.15s;
}

.btn-scroll-bottom.show {
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-scroll-bottom:active { transform: scale(0.92); }

.new-msg-badge {
    position: absolute;
    top: -5px;
    right: -5px;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    background: var(--error);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--bg);
}

.new-msg-badge.hidden { display: none; }

/* ========== 底部输入 ========== */
.chat-footer {
    flex-shrink: 0;
    padding: 12px 16px;
    padding-bottom: max(12px, var(--safe-bottom));
    background: rgba(22, 28, 38, 0.9);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-top: 1px solid var(--border);
}

.message-form {
    display: flex;
    gap: 10px;
    align-items: flex-end;
}

.input-wrap {
    flex: 1;
    background: var(--bg);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-xl);
    padding: 2px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.input-wrap:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-glow);
}

.message-form textarea {
    display: block;
    width: 100%;
    padding: 11px 16px;
    background: transparent;
    border: none;
    color: var(--text);
    font-size: 16px;
    line-height: 1.45;
    outline: none;
    resize: none;
    max-height: 120px;
    font-family: inherit;
}

.message-form textarea::placeholder { color: var(--text-muted); }

.btn-send {
    flex-shrink: 0;
    height: 46px;
    min-width: 46px;
    padding: 0 16px;
    background: linear-gradient(135deg, var(--primary), var(--primary-hover));
    color: white;
    border: none;
    border-radius: 23px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    box-shadow: 0 4px 14px var(--primary-glow);
    transition: transform 0.15s, box-shadow 0.15s;
    -webkit-tap-highlight-color: transparent;
}

.btn-send:hover { box-shadow: 0 6px 20px var(--primary-glow); }
.btn-send:active { transform: scale(0.96); }
.btn-send:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

.send-icon { flex-shrink: 0; }

/* ========== Toast ========== */
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(80px + var(--safe-bottom));
    transform: translateX(-50%) translateY(12px);
    background: rgba(22, 28, 38, 0.95);
    backdrop-filter: blur(12px);
    color: var(--text);
    padding: 12px 22px;
    border-radius: var(--radius-md);
    font-size: 14px;
    font-weight: 500;
    border: 1px solid var(--border-light);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.25s, transform 0.25s;
    z-index: 1000;
    box-shadow: var(--shadow);
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* 兼容旧 id */
.copy-toast { /* same as toast */ }

/* ========== 手机端优化 ========== */
@media (max-width: 767px) {
    .sync-status { display: none; }

    .sync-indicator {
        padding: 0;
        background: none;
        width: 28px;
        height: 28px;
        justify-content: center;
    }

    .btn-logout {
        padding: 6px 8px;
        font-size: 12px;
    }

    .messages { padding: 12px; gap: 8px; }

    .message-actions { opacity: 1; }

    .btn-action {
        width: 36px;
        height: 36px;
    }

    .btn-action svg {
        width: 17px;
        height: 17px;
    }

    .send-text { display: none; }

    .btn-send {
        width: 46px;
        padding: 0;
        border-radius: 50%;
    }
}

/* ========== 桌面端优化 ========== */
@media (min-width: 768px) {
    .chat-page {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 20px;
    }

    .app-shell {
        width: 100%;
        max-width: 720px;
        height: calc(100vh - 40px);
        height: calc(100dvh - 40px);
        background: var(--surface);
        border: 1px solid var(--border-light);
        border-radius: var(--radius-xl);
        box-shadow: var(--shadow-lg);
        overflow: hidden;
    }

    .chat-header {
        height: 60px;
        padding: 0 20px;
        background: rgba(22, 28, 38, 0.95);
    }

    .brand-text h1 { font-size: 17px; }

    .desktop-toolbar {
        display: flex;
        align-items: center;
        gap: 4px;
    }

    .btn-menu { display: none; }

    .sync-indicator { margin-right: 4px; }

    .messages {
        padding: 20px 24px;
        gap: 12px;
    }

    .message-item {
        max-width: 88%;
    }

    .message-card { border-radius: var(--radius-md); }

    .chat-footer {
        padding: 16px 20px;
    }

    .toast {
        bottom: auto;
        top: 80px;
        transform: translateX(-50%) translateY(-12px);
    }

    .toast.show {
        transform: translateX(-50%) translateY(0);
    }

    .login-box { padding: 48px 40px; }
}

@media (min-width: 1024px) {
    .app-shell { max-width: 780px; }
    .message-item { max-width: 82%; }
}

/* ========== 滚动条 ========== */
.messages::-webkit-scrollbar { width: 5px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 3px;
}
.messages::-webkit-scrollbar-thumb:hover { background: rgba(255, 255, 255, 0.18); }

/* ========== 选中高亮 ========== */
::selection {
    background: rgba(79, 140, 255, 0.35);
    color: var(--text);
}
