/* ==================== 基础变量 ==================== */
:root {
    --bg-1: #0f0f1e;
    --bg-2: #1a1a2e;
    --chat-bg: #0d1117;
    --card: rgba(255, 255, 255, 0.06);
    --card-border: rgba(255, 255, 255, 0.1);
    --text: #e2e8f0;
    --text-dim: #94a3b8;
    --primary: #6366f1;
    --primary-2: #8b5cf6;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --bubble-me: linear-gradient(135deg, #6366f1, #8b5cf6);
    --bubble-other: #2d2d44;
    --radius: 16px;

    /* 响应式尺寸变量（通过 clamp 实现流式缩放）*/
    --content-max: 480px;       /* 内容最大宽度（手机/平板）*/
    --chat-max: 768px;          /* 聊天区最大宽度 */
    --home-max: 560px;          /* 首页内容最大宽度 */
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(135deg, var(--bg-1), var(--bg-2));
    color: var(--text);
    /* 流式字号：手机 15px → 桌面 16px */
    font-size: clamp(15px, 2.5vw, 16px);
    /* 安全区域内边距（刘海屏/底部横条）*/
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.hidden { display: none !important; }

/* ==================== 视图 ==================== */
.view {
    display: none;
    height: 100vh;
    height: 100dvh;
    animation: fadeIn 0.3s ease;
}
.view.active { display: flex; flex-direction: column; }

#view-home.active {
    overflow-y: auto;
    padding: 24px 20px;
    /* 桌面端水平居中 */
    align-items: center;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ==================== 首页 ==================== */
.home-wrapper {
    width: 100%;
    max-width: var(--home-max);
    margin: 0 auto;
}

.hero {
    text-align: center;
    margin-top: 6vh;
    margin-bottom: clamp(24px, 5vh, 48px);
}

.logo {
    /* 流式 logo：手机 52px → 桌面 80px */
    font-size: clamp(52px, 12vw, 80px);
    margin-bottom: 12px;
    filter: drop-shadow(0 0 20px rgba(99, 102, 241, 0.5));
    line-height: 1;
}

.hero h1 {
    /* 流式标题：手机 20px → 桌面 28px */
    font-size: clamp(20px, 4vw, 28px);
    font-weight: 700;
    background: linear-gradient(135deg, #818cf8, #c084fc);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 8px;
    line-height: 1.3;
}

.subtitle { color: var(--text-dim); font-size: clamp(14px, 2.5vw, 16px); }

.actions {
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-width: var(--content-max);
    margin: 0 auto;
}

.features {
    display: flex;
    justify-content: center;
    gap: clamp(16px, 3vw, 28px);
    margin-top: 24px;
    flex-wrap: wrap;
}

.feature {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: clamp(14px, 2.5vw, 16px);
    color: var(--text-dim);
}

/* ==================== 按钮 ==================== */
.btn {
    padding: 14px 24px;
    border: none;
    border-radius: var(--radius);
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    /* 保证触摸目标 ≥ 44px 高 */
    min-height: 48px;
}
.btn:active { transform: scale(0.97); }

.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    color: #fff;
    box-shadow: 0 4px 20px rgba(99, 102, 241, 0.3);
}
.btn-primary:hover { box-shadow: 0 6px 28px rgba(99, 102, 241, 0.45); }

.btn-secondary {
    background: var(--card);
    color: var(--text);
    border: 1px solid var(--card-border);
}
.btn-secondary:hover { background: rgba(255, 255, 255, 0.1); }

.btn-outline {
    background: transparent;
    color: var(--primary);
    border: 1px solid var(--primary);
}
.btn-outline:hover { background: rgba(99, 102, 241, 0.1); }

.btn-sm { padding: 10px 20px; font-size: 14px; min-height: 44px; }
.btn-block { width: 100%; margin-top: 10px; }

.btn-icon {
    background: none;
    border: none;
    color: var(--text);
    font-size: 22px;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: background 0.2s;
    /* 触摸目标 ≥ 44px */
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.btn-icon:hover { background: rgba(255, 255, 255, 0.1); }

/* ==================== 加入面板 ==================== */
.join-panel {
    max-width: var(--content-max);
    margin: 16px auto 0;
    padding: 20px;
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    animation: fadeIn 0.3s ease;
}

.input-group { display: flex; gap: 8px; }

.input-group input {
    flex: 1;
    padding: 12px 16px;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    color: var(--text);
    font-size: clamp(18px, 4vw, 20px);
    letter-spacing: 6px;
    text-align: center;
    outline: none;
    transition: border-color 0.2s;
    min-height: 48px;
}
.input-group input:focus { border-color: var(--primary); }

/* ==================== 聊天界面 ==================== */
.view-chat {
    background: var(--chat-bg);
}

/* 聊天页内容居中容器（桌面端限制宽度）*/
.chat-wrapper {
    width: 100%;
    max-width: var(--chat-max);
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

.chat-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    background: rgba(26, 26, 46, 0.95);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--card-border);
    flex-shrink: 0;
    z-index: 10;
    max-width: var(--chat-max);
    margin: 0 auto;
    width: 100%;
}

.chat-title {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 0;
}

.chat-room-id {
    font-size: clamp(16px, 3vw, 20px);
    font-weight: 700;
    letter-spacing: 3px;
    color: var(--text);
}

.chat-status {
    font-size: 11px;
    color: var(--text-dim);
    margin-top: 2px;
}
.chat-status.connected { color: var(--success); }
.chat-status.disconnected { color: var(--danger); }

/* ==================== 消息列表 ==================== */
.message-list {
    flex: 1;
    overflow-y: auto;
    padding: 16px 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    background: var(--chat-bg);
    -webkit-overflow-scrolling: touch;
    max-width: var(--chat-max);
    margin: 0 auto;
    width: 100%;
}

/* 系统提示 */
.system-notice {
    text-align: center;
    margin: 8px 0;
}
.system-notice span {
    display: inline-block;
    padding: 4px 14px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 8px;
    font-size: 12px;
    color: var(--text-dim);
    max-width: 80%;
}

/* 消息行 */
.msg-row {
    display: flex;
    gap: 8px;
    margin: 6px 0;
    align-items: flex-start;
    animation: msgIn 0.25s ease;
    max-width: 100%;
}
.msg-row.me { flex-direction: row-reverse; }

@keyframes msgIn {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.msg-avatar {
    width: 38px;
    height: 38px;
    border-radius: 8px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    background: var(--bubble-other);
}
.msg-row.me .msg-avatar { background: var(--primary); }

.msg-content {
    max-width: 72%;
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.msg-row.me .msg-content { align-items: flex-end; }

/* 文本气泡 */
.msg-bubble {
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 15px;
    line-height: 1.5;
    word-break: break-word;
    background: var(--bubble-other);
    color: var(--text);
    position: relative;
}
.msg-row.me .msg-bubble {
    background: var(--bubble-me);
    color: #fff;
}

/* 气泡小三角 */
.msg-bubble::before {
    content: '';
    position: absolute;
    top: 12px;
    width: 0;
    height: 0;
    border: 6px solid transparent;
}
.msg-row:not(.me) .msg-bubble::before {
    left: -10px;
    border-right-color: var(--bubble-other);
}
.msg-row.me .msg-bubble::before {
    right: -10px;
    border-left-color: transparent;
}
.msg-row.me .msg-bubble { border-top-right-radius: 4px; }
.msg-row:not(.me) .msg-bubble { border-top-left-radius: 4px; }

.msg-time {
    font-size: 11px;
    color: var(--text-dim);
    margin-top: 4px;
    padding: 0 4px;
}

/* ==================== 文件消息卡片 ==================== */
.msg-file {
    background: var(--bubble-other);
    border-radius: 12px;
    padding: 12px;
    display: flex;
    gap: 10px;
    align-items: center;
    min-width: 220px;
    max-width: 320px;
    cursor: pointer;
    transition: background 0.2s;
}
.msg-row.me .msg-file { background: rgba(99, 102, 241, 0.25); }
.msg-file:active { opacity: 0.85; }

.file-card-icon {
    width: 42px;
    height: 42px;
    border-radius: 8px;
    background: rgba(99, 102, 241, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    flex-shrink: 0;
}

.file-card-info { flex: 1; min-width: 0; }

.file-card-name {
    font-size: 14px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 2px;
}

.file-card-meta {
    font-size: 12px;
    color: var(--text-dim);
    display: flex;
    justify-content: space-between;
}

.file-progress {
    height: 3px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 2px;
    margin-top: 6px;
    overflow: hidden;
}
.file-progress-fill {
    height: 100%;
    background: var(--primary);
    border-radius: 2px;
    width: 0%;
    transition: width 0.2s;
}
.file-progress-fill.done { background: var(--success); }

/* ==================== 输入栏 ==================== */
.chat-input-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 10px;
    background: rgba(26, 26, 46, 0.95);
    backdrop-filter: blur(10px);
    border-top: 1px solid var(--card-border);
    flex-shrink: 0;
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    max-width: var(--chat-max);
    margin: 0 auto;
    width: 100%;
}

.input-btn {
    background: none;
    border: none;
    color: var(--text);
    font-size: 24px;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: background 0.2s;
    flex-shrink: 0;
    /* 触摸目标 ≥ 44px */
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.input-btn:hover { background: rgba(255, 255, 255, 0.1); }

#input-message {
    flex: 1;
    padding: 9px 14px;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--card-border);
    border-radius: 22px;
    color: var(--text);
    font-size: 16px; /* ≥16px 防止 iOS 聚焦放大 */
    outline: none;
    min-width: 0;
    min-height: 40px;
}
#input-message:focus { border-color: var(--primary); }

.btn-send {
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    color: #fff;
    font-size: 14px;
    font-weight: 600;
    padding: 8px 16px;
    border-radius: 20px;
    border: none;
    cursor: pointer;
    flex-shrink: 0;
    min-height: 40px;
}
.btn-send:active { transform: scale(0.95); }
.btn-send:disabled { opacity: 0.4; }

/* ==================== 二维码弹窗 ==================== */
.modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal-mask {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
}

.modal-body {
    position: relative;
    background: var(--bg-2);
    border-radius: var(--radius);
    padding: 20px;
    max-width: 360px;
    width: 100%;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    max-height: 90vh;
    overflow-y: auto;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}
.modal-header h3 { font-size: 18px; }

.qrcode-box {
    display: flex;
    justify-content: center;
    padding: 16px;
    background: #fff;
    border-radius: 12px;
}
#qrcode { display: flex; justify-content: center; }
#qrcode canvas, #qrcode img { max-width: 100%; height: auto; }

.qrcode-tip {
    text-align: center;
    margin-top: 14px;
    font-size: 13px;
    color: var(--text-dim);
    line-height: 1.6;
}
.qrcode-tip strong { color: var(--primary); font-size: 16px; }

/* ==================== 响应式 ==================== */

/* ---- 手机竖屏 (< 480px) ---- */
@media (max-width: 480px) {
    .hero { margin-top: 4vh; }
    .msg-content { max-width: 80%; }
    .msg-file { min-width: 180px; max-width: 260px; }
    .message-list { padding: 12px 8px; }
    .chat-header { padding: 8px 10px; }
    .chat-input-bar { padding: 8px; }
}

/* ---- 手机横屏 / 小平板 (481px - 768px) ---- */
@media (min-width: 481px) and (max-width: 768px) {
    .hero { margin-top: 8vh; }
    .msg-content { max-width: 70%; }
    .msg-file { max-width: 340px; }
    .features { gap: 24px; }
    .message-list { padding: 16px 16px; }
}

/* ---- 平板 (769px - 1024px) ---- */
@media (min-width: 769px) and (max-width: 1024px) {
    :root {
        --home-max: 640px;
        --chat-max: 720px;
    }
    .hero { margin-top: 10vh; }
    .msg-content { max-width: 65%; }
    .msg-file { max-width: 380px; }
    .message-list { padding: 20px 20px; }
    .chat-header { padding: 12px 16px; }
    .chat-input-bar { padding: 10px 16px; }
    .msg-avatar { width: 42px; height: 42px; font-size: 20px; }
}

/* ---- 桌面 (> 1024px) ---- */
@media (min-width: 1025px) {
    :root {
        --home-max: 720px;
        --chat-max: 840px;
    }
    .hero { margin-top: 12vh; }
    .msg-content { max-width: 60%; }
    .msg-file { max-width: 420px; }
    .message-list { padding: 24px 24px; }
    .chat-header { padding: 12px 20px; }
    .chat-input-bar { padding: 12px 20px; }
    .msg-avatar { width: 44px; height: 44px; font-size: 20px; }
    .msg-bubble { font-size: 15px; padding: 12px 16px; }

    /* 桌面端按钮 hover 效果更明显 */
    .btn-primary:hover { transform: translateY(-1px); }
    .device-item:hover { transform: translateX(4px); }
}

/* ---- 大屏桌面 (> 1440px) ---- */
@media (min-width: 1441px) {
    :root {
        --home-max: 800px;
        --chat-max: 960px;
    }
    .hero { margin-top: 14vh; }
}

/* ---- 手机横屏特殊处理 ---- */
@media (max-height: 480px) and (orientation: landscape) {
    .hero { margin-top: 2vh; margin-bottom: 16px; }
    .logo { font-size: 40px; margin-bottom: 4px; }
    .hero h1 { font-size: 18px; margin-bottom: 4px; }
    .features { margin-top: 12px; gap: 12px; }
    .feature { font-size: 13px; }
    .actions { gap: 8px; }
    .btn { min-height: 40px; padding: 10px 20px; }
}

/* ---- 深色/浅色偏好（保持深色为主，微调）---- */
@media (prefers-color-scheme: light) {
    /* 当前设计为深色主题，此处保留扩展位 */
}

/* ---- 减少动画偏好 ---- */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

/* 滚动条 */
.message-list::-webkit-scrollbar { width: 4px; }
.message-list::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 2px; }
.message-list::-webkit-scrollbar-track { background: transparent; }

/* ==================== 文件卡片网速显示 ==================== */
.fc-speed {
    font-size: 11px;
    color: var(--primary);
    font-weight: 600;
    margin-left: 6px;
}
.fc-status-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

/* ==================== 主页附近设备区域 ==================== */
.nearby-section {
    width: 100%;
    max-width: var(--content-max);
    margin: 8px auto 0;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--card-border);
    border-radius: 16px;
    padding: 16px;
    box-sizing: border-box;
}

.nearby-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}

.nearby-title {
    font-size: clamp(14px, 2.5vw, 16px);
    font-weight: 600;
    color: var(--text);
}

.btn-refresh {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: 1px solid var(--card-border);
    background: var(--card);
    color: var(--text);
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}
.btn-refresh:hover {
    background: rgba(99, 102, 241, 0.15);
    border-color: var(--primary);
}
.btn-refresh.spinning {
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

.nearby-info {
    font-size: 12px;
    color: var(--text-dim);
    margin-bottom: 12px;
    padding: 6px 10px;
    background: rgba(255, 255, 255, 0.04);
    border-radius: 8px;
    text-align: center;
}

.device-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 280px;
    overflow-y: auto;
}

.device-item {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 12px 14px;
    background: var(--card);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s;
    animation: slideIn 0.3s ease;
    text-align: center;
    min-height: 48px;
}
.device-item:hover {
    background: rgba(99, 102, 241, 0.15);
    border-color: var(--primary);
    transform: translateX(2px);
}
.device-item:active { transform: scale(0.98); }

@keyframes slideIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.device-item-icon {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--primary), var(--primary-2));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    flex-shrink: 0;
}

.device-item-info {
    flex: 1;
    min-width: 0;
    text-align: center;
}

.device-item-name {
    font-size: 15px;
    font-weight: 600;
    margin-bottom: 2px;
}

.device-item-ip {
    font-size: 12px;
    color: var(--text-dim);
    font-family: monospace;
}

.device-item-action {
    font-size: 20px;
    color: var(--primary);
    flex-shrink: 0;
}

.device-empty {
    text-align: center;
    padding: 24px 16px;
    color: var(--text-dim);
    font-size: 13px;
}
.device-empty-icon {
    font-size: 36px;
    margin-bottom: 8px;
    opacity: 0.5;
}

/* ==================== 来电邀请弹窗 ==================== */
.modal-incoming-body {
    max-width: 320px;
    text-align: center;
}

.incoming-icon {
    font-size: 48px;
    margin-bottom: 12px;
    animation: ringPulse 1s infinite;
}

@keyframes ringPulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.15); }
}

.incoming-desc {
    font-size: 14px;
    color: var(--text-dim);
    margin: 8px 0 20px;
    line-height: 1.5;
}

.incoming-actions {
    display: flex;
    gap: 12px;
}
.incoming-actions .btn { flex: 1; }
