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

:root {
    --bg: #f5f5f7;
    --surface: #ffffff;
    --text: #1d1d1f;
    --text-2: #86868b;
    --accent: #007AFF;
    --accent-dark: #0056CC;
    --red: #FF3B30;
    --green: #34C759;
    --border: rgba(0, 0, 0, 0.06);
    --font: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'SF Pro Text',
             'Helvetica Neue', 'Segoe UI', system-ui, sans-serif;
}

html[data-theme="dark"] {
    --bg: #1a1a1e;
    --surface: #2c2c2e;
    --text: #f5f5f7;
    --text-2: #98989d;
    --accent: #0A84FF;
    --accent-dark: #409CFF;
    --red: #FF453A;
    --green: #30D158;
    --border: rgba(255, 255, 255, 0.08);
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    transition: background 0.3s, color 0.3s;
}

/* ── Layout ── */
.app {
    display: flex;
    flex-direction: column;
    /* 100dvh = 动态视口高度，自动适配移动端地址栏/键盘 */
    height: 100dvh;
    /* iOS fallback */
    height: 100vh;
    height: 100%;
    /* iOS safe-area: 确保内容不被刘海/home bar遮挡 */
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    max-width: 960px;
    margin: 0 auto;
    padding: 0 24px;
    /* 允许内部滚动 */
    overflow: hidden;
}

/* ── Header ── */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 0 12px;
}

.title-group {
    display: flex;
    align-items: center;
    gap: 10px;
}

.title-group h1 {
    font-size: 20px;
    font-weight: 600;
    letter-spacing: -0.4px;
}

.globe { color: var(--accent); }

/* ── Domain Toggle ── */
.domain-toggle {
    display: flex;
    background: var(--border);
    border-radius: 6px;
    padding: 2px;
    gap: 2px;
}

.domain-option {
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.3px;
    padding: 3px 8px;
    border-radius: 4px;
    color: var(--text-2);
    cursor: pointer;
    transition: all 0.2s;
    user-select: none;
}

.domain-option:hover {
    color: var(--text);
}

.domain-option.active {
    background: var(--accent);
    color: white;
}

html[data-theme="dark"] .domain-toggle {
    background: rgba(255, 255, 255, 0.06);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.status-group {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: var(--text-2);
}

#waveform {
    width: 80px;
    height: 28px;
    flex-shrink: 0;
    border-radius: 4px;
}

/* ── Theme Toggle ── */
.icon-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: none;
    background: none;
    color: var(--text-2);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
    /* 移动端：消除 300ms 点击延迟 */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.icon-btn:hover {
    background: var(--border);
    color: var(--text);
}

.icon-btn .icon-sun { display: block; }
.icon-btn .icon-moon { display: none; }
.icon-btn.dark .icon-sun { display: none; }
.icon-btn.dark .icon-moon { display: block; }

.clear-btn, .action-btn {
    font-family: var(--font);
    font-size: 12px;
    font-weight: 500;
    color: var(--text-2);
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 4px 10px;
    cursor: pointer;
    transition: all 0.15s;
}
.clear-btn:hover {
    color: var(--red);
    border-color: var(--red);
}
.action-btn:hover {
    color: var(--accent);
    border-color: var(--accent);
}
.action-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* ── Column Labels ── */
.column-labels {
    display: flex;
    align-items: center;
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
}

.col-label {
    flex: 1;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 1.2px;
    color: var(--text-2);
    text-transform: uppercase;
}

.col-label:last-child {
    text-align: right;
}

.col-separator {
    width: 1px;
    height: 12px;
    background: var(--border);
    margin: 0 16px;
}

/* ── Content ── */
.content {
    flex: 1;
    overflow-y: auto;
    padding: 12px 0;
    scroll-behavior: smooth;
}

.content::-webkit-scrollbar { width: 4px; }
.content::-webkit-scrollbar-thumb {
    background: rgba(0,0,0,0.12);
    border-radius: 2px;
}

html[data-theme="dark"] .content::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.12);
}

/* ── Empty State ── */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: 12px;
    color: var(--text-2);
    opacity: 0.8;
    user-select: none;
}
.empty-title { font-size: 17px; font-weight: 500; }
.empty-sub   { font-size: 14px; opacity: 0.6; }

/* ── Entry Row ── */
.entry-row {
    display: flex;
    align-items: stretch;
    min-height: 48px;
    border-bottom: 1px solid var(--border);
    animation: slideIn 0.25s ease;
}

.entry-row.active {
    background: rgba(0, 122, 255, 0.02);
}

html[data-theme="dark"] .entry-row.active {
    background: rgba(10, 132, 255, 0.04);
}

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

.entry-cell {
    flex: 1;
    min-width: 0;
    padding: 12px 0;
}

.entry-cell:first-child {
    padding-right: 20px;
}

.entry-cell:last-child {
    padding-left: 20px;
}

.entry-center {
    width: 1px;
    background: var(--border);
    align-self: stretch;
    flex-shrink: 0;
}

/* ── Text Styles ── */
.entry-text {
    font-size: 16px;
    line-height: 1.55;
    word-wrap: break-word;
    cursor: pointer;
    border-radius: 4px;
    padding: 2px 4px;
    margin: -2px -4px;
    transition: background 0.15s;
}

.entry-text:hover {
    background: rgba(0, 0, 0, 0.03);
}

html[data-theme="dark"] .entry-text:hover {
    background: rgba(255, 255, 255, 0.05);
}

.entry-text:active {
    background: rgba(0, 0, 0, 0.06);
}

/* Original text: bold + blue left accent */
.entry-text.original {
    font-weight: 500;
    border-left: 3px solid var(--accent);
    padding-left: 12px;
    margin-left: 0;
}

/* Auto-corrected text: green left accent */
.entry-text.corrected {
    border-left-color: var(--green);
}

/* Translation: lighter, regular */
.entry-text.translated {
    font-weight: 400;
    color: var(--text);
    opacity: 0.8;
}

/* Streaming cursor */
.entry-text.streaming {
    opacity: 0.8;
}

.entry-text.streaming::after {
    content: '\25CD';
    animation: blink 0.5s step-end infinite;
    color: var(--accent);
}

@keyframes blink {
    50% { opacity: 0; }
}

/* Interim (while speaking) */
.entry-text.interim {
    color: var(--text-2);
    font-weight: 400;
    font-style: italic;
}

.entry-text.failed {
    color: var(--red);
    opacity: 0.5;
    font-style: italic;
}

/* Copied feedback */
.entry-text.copied {
    background: rgba(0, 122, 255, 0.08) !important;
}

/* Shimmer loading */
.shimmer {
    display: inline-block;
    width: 70%;
    height: 16px;
    background: linear-gradient(90deg, #e8e8ed 25%, #f2f2f7 50%, #e8e8ed 75%);
    background-size: 200% 100%;
    animation: shimmer 1.5s ease-in-out infinite;
    border-radius: 4px;
    margin-top: 4px;
}

html[data-theme="dark"] .shimmer {
    background: linear-gradient(90deg, #3a3a3c 25%, #48484a 50%, #3a3a3c 75%);
    background-size: 200% 100%;
}

@keyframes shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ── Footer / Toolbar ── */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 16px 0 28px;
    border-top: 1px solid var(--border);
    /* 确保 toolbar 不会被压缩 */
    flex-shrink: 0;
    /* 允许横向滚动（备选，过渡方案） */
    overflow-x: auto;
    overflow-y: visible;
    /* iOS safe-area: 底部留出 home bar 空间 */
    padding-bottom: max(28px, env(safe-area-inset-bottom, 28px));
}

.input-area {
    display: flex;
    align-items: center;
    flex: 1;
    min-width: 0; /* 允许收缩，防止按钮被挤出 */
    max-width: 420px;
    background: var(--surface);
    border-radius: 20px;
    border: 1px solid var(--border);
    padding: 0 4px 0 16px;
    transition: border-color 0.2s;
    /* 移动端：点击输入框立即响应，不等待 300ms */
    touch-action: manipulation;
}

.input-area:focus-within {
    border-color: var(--accent);
}

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

.text-input::placeholder {
    color: var(--text-2);
    opacity: 0.6;
}

.send-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: none;
    background: var(--accent);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
    flex-shrink: 0;
    /* 移动端：消除 300ms 点击延迟 */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.send-btn:hover {
    background: var(--accent-dark);
    transform: scale(1.05);
}

/* ── Voice Input Button (mobile keyboard speech fallback) ── */
.voice-input-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: none;
    background: none;
    color: var(--text-2);
    cursor: pointer;
    display: none; /* 默认隐藏，只有在需要时显示 */
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
    flex-shrink: 0;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.voice-input-btn:hover {
    color: var(--accent);
}

/* 移动端且不支持 SpeechRecognition 时，显示备用语音按钮 */
@media (max-width: 768px) {
    .voice-input-btn {
        display: flex;
    }
}

/* ── Tool Button (TTS) ── */
.tool-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-2);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
    /* 移动端：消除 300ms 点击延迟 */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.tool-btn:hover {
    color: var(--accent);
    border-color: var(--accent);
}

.tool-btn.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

.mic-btn {
    width: 52px;
    height: 52px;
    border-radius: 50%;
    border: none;
    background: var(--accent);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    box-shadow: 0 2px 12px rgba(0,122,255,0.3);
    outline: none;
    flex-shrink: 0;
    /* 移动端：消除 300ms 点击延迟 */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

html[data-theme="dark"] .mic-btn {
    box-shadow: 0 2px 12px rgba(10,132,255,0.25);
}

.mic-btn:hover {
    background: var(--accent-dark);
    transform: scale(1.04);
}
.mic-btn:active { transform: scale(0.96); }

.mic-btn.recording {
    background: var(--red);
    box-shadow: 0 2px 12px rgba(255,59,48,0.3);
    animation: micPulse 2s ease-in-out infinite;
}
.mic-btn.recording:hover { background: #e02e25; }

html[data-theme="dark"] .mic-btn.recording:hover { background: #cc2a22; }

.mic-btn:disabled {
    background: var(--text-2);
    cursor: not-allowed;
    box-shadow: none;
}

@keyframes micPulse {
    0%, 100% { box-shadow: 0 2px 12px rgba(255,59,48,0.3); }
    50%      { box-shadow: 0 2px 24px rgba(255,59,48,0.5), 0 0 0 8px rgba(255,59,48,0.08); }
}

/* ── Responsive ── */

/* Tablet */
@media (max-width: 768px) {
    .app { padding: 0 16px; }
    .header-actions { gap: 8px; }
    #waveform { width: 60px; }
    .status-text { display: none; }
    .toolbar { gap: 12px; }
    .input-area { max-width: 320px; }
    .modal { width: 95%; max-height: 85vh; }
    .offline-banner { margin: 0 -16px; }
}

/* Phone */
@media (max-width: 480px) {
    .app {
        padding: 0 10px;
        /* 关键：确保 app 高度不会超出可视区 */
        height: 100dvh;
        height: -webkit-fill-available;
    }

    /* Header: keep horizontal, compact */
    .header {
        flex-shrink: 0;
        padding: 10px 0 8px;
        gap: 6px;
    }
    .title-group h1 { font-size: 17px; }
    #waveform { display: none; }
    .status-text { display: none; }

    /* Header actions: wrap only on very small screens, keep compact */
    .header-actions {
        gap: 4px;
    }
    .icon-btn {
        width: 26px;
        height: 26px;
    }
    .clear-btn, .action-btn {
        font-size: 10px;
        padding: 3px 7px;
    }

    /* Column labels */
    .col-label { font-size: 10px; letter-spacing: 0.8px; }
    .col-separator { margin: 0 8px; }

    /* Entry: tighter spacing */
    .entry-row { min-height: 40px; }
    .entry-cell { padding: 8px 0; }
    .entry-cell:first-child { padding-right: 10px; }
    .entry-cell:last-child { padding-left: 10px; }
    .entry-text { font-size: 14px; line-height: 1.45; }
    .entry-text.original { padding-left: 8px; border-left-width: 2px; }

    /* Toolbar: tighter */
    .toolbar {
        gap: 8px;
        padding: 12px 0 20px;
    }
    .input-area { max-width: none; }
    .mic-btn { width: 46px; height: 46px; }
    .tool-btn { width: 36px; height: 36px; }

    /* Buttons: smaller text */
    .clear-btn, .action-btn {
        font-size: 11px;
        padding: 3px 8px;
    }

    /* Modal: nearly full screen */
    .modal {
        width: 100%;
        max-width: none;
        max-height: 90vh;
        border-radius: 12px;
    }
    .modal-header { padding: 16px 18px; }
    .modal-body { padding: 18px; }
    .summary-content h1 { font-size: 18px; }
    .summary-content h2 { font-size: 15px; }
    .summary-content p, .summary-content li { font-size: 13px; }

    /* History items */
    .history-item { padding: 10px 12px; }
    .history-date { font-size: 13px; }
    .history-preview { font-size: 12px; }

    .offline-banner { margin: 0 -10px; }
}

/* Very small phones */
@media (max-width: 360px) {
    .app { padding: 0 8px; }
    .entry-text { font-size: 13px; }
    .input-area { padding: 0 4px 0 12px; }
    .text-input { font-size: 13px; }
}

/* Safe area for modern phones (notch, home bar) */
@supports (padding: env(safe-area-inset-bottom)) {
    .toolbar {
        padding-bottom: calc(16px + env(safe-area-inset-bottom));
    }
}

@supports not (padding: env(safe-area-inset-bottom)) {
    .toolbar {
        padding-bottom: 32px;
    }
}

/* ── Modal ── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    opacity: 0;
    visibility: hidden;
    transition: all 0.25s ease;
}

html[data-theme="dark"] .modal-overlay {
    background: rgba(0, 0, 0, 0.6);
}

.modal-overlay.open {
    opacity: 1;
    visibility: visible;
}

.modal {
    background: var(--surface);
    border-radius: 16px;
    width: 90%;
    max-width: 680px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
    transform: translateY(20px);
    transition: transform 0.25s ease;
}

html[data-theme="dark"] .modal {
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.modal-overlay.open .modal {
    transform: translateY(0);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    font-size: 17px;
    font-weight: 600;
}

.modal-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.modal-btn {
    font-family: var(--font);
    font-size: 13px;
    font-weight: 500;
    padding: 6px 14px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    transition: all 0.15s;
}
.modal-btn:hover { background: var(--bg); }
.modal-btn.primary {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}
.modal-btn.primary:hover { background: var(--accent-dark); }

.modal-close {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: none;
    background: none;
    color: var(--text-2);
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}
.modal-close:hover {
    background: var(--bg);
    color: var(--text);
}

.modal-body {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
}

.modal-body::-webkit-scrollbar { width: 4px; }
.modal-body::-webkit-scrollbar-thumb {
    background: rgba(0,0,0,0.12);
    border-radius: 2px;
}

html[data-theme="dark"] .modal-body::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.12);
}

.summary-loading {
    text-align: center;
    padding: 40px 0;
    color: var(--text-2);
    font-size: 15px;
}

/* Summary content (rendered markdown) */
.summary-content h1 { font-size: 20px; font-weight: 600; margin-bottom: 16px; }
.summary-content h2 { font-size: 16px; font-weight: 600; margin: 16px 0 8px; color: var(--accent); }
.summary-content h3 { font-size: 15px; font-weight: 600; margin: 12px 0 6px; }
.summary-content p { font-size: 14px; line-height: 1.6; margin-bottom: 8px; }
.summary-content ul, .summary-content ol { padding-left: 20px; margin-bottom: 8px; }
.summary-content li { font-size: 14px; line-height: 1.6; margin-bottom: 4px; }
.summary-content hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.summary-content blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 12px;
    margin: 8px 0;
    color: var(--text-2);
    font-size: 13px;
}
.summary-content code {
    background: var(--bg);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
}

/* ── History ── */
.history-item {
    padding: 14px 16px;
    border-radius: 10px;
    cursor: pointer;
    transition: background 0.15s;
    margin-bottom: 4px;
}

.history-item:hover {
    background: var(--bg);
}

.history-item.current {
    background: rgba(0, 122, 255, 0.06);
    cursor: default;
}

html[data-theme="dark"] .history-item.current {
    background: rgba(10, 132, 255, 0.1);
}

.history-info {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 4px;
}

.history-date {
    font-size: 14px;
    font-weight: 500;
}

.history-count {
    font-size: 12px;
    color: var(--text-2);
    background: var(--bg);
    padding: 2px 8px;
    border-radius: 4px;
}

.history-preview {
    font-size: 13px;
    color: var(--text-2);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-empty {
    text-align: center;
    padding: 40px 0;
    color: var(--text-2);
    font-size: 15px;
}

/* ── Offline Banner ── */
.offline-banner {
    background: var(--red);
    color: white;
    text-align: center;
    font-size: 13px;
    font-weight: 500;
    padding: 8px 0;
    margin: 0 -24px;
    transform: translateY(-100%);
    transition: transform 0.3s ease;
}

html[data-theme="dark"] .offline-banner {
    background: #cc2a22;
}

.offline-banner.visible {
    transform: translateY(0);
}

/* ── Retry Button ── */
.retry-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-2);
    font-size: 14px;
    cursor: pointer;
    margin-left: 8px;
    vertical-align: middle;
    transition: all 0.15s;
}

.retry-btn:hover {
    color: var(--accent);
    border-color: var(--accent);
}

/* ── Floating Window (Picture-in-Picture) ── */
.icon-btn .icon-expand { display: block; }
.icon-btn .icon-return { display: none; }
.icon-btn.active .icon-expand { display: none; }
.icon-btn.active .icon-return { display: block; }

#floatBtn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}
#floatBtn:disabled:hover {
    background: none;
}

/* ── TTS Group ── */
.tts-group {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}

.tts-speed {
    font-size: 9px;
    font-weight: 600;
    color: var(--text-2);
    cursor: pointer;
    padding: 1px 4px;
    border-radius: 3px;
    transition: all 0.15s;
    user-select: none;
    /* 移动端：消除 300ms 点击延迟 */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.tts-speed:hover {
    color: var(--accent);
    background: rgba(0, 122, 255, 0.06);
}

/* ── Help / Shortcuts ── */
.modal-sm {
    max-width: 360px;
}

.shortcut-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.shortcut-row {
    display: flex;
    align-items: center;
    gap: 16px;
}

.shortcut-row kbd {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 48px;
    padding: 4px 10px;
    font-family: var(--font);
    font-size: 13px;
    font-weight: 500;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
}

.shortcut-row span {
    font-size: 14px;
    color: var(--text-2);
}
