:root {
    --bg-app: #f0f4f9;
    --bg-surface: #ffffff;
    --bg-user: #d3e3fd;
    --bg-bot: #ffffff;
    --bg-error: #fce8e6;
    --text-primary: #1f1f1f;
    --text-secondary: #5f6368;
    --text-error: #b3261e;
    --accent: #1a73e8;
    --accent-hover: #1765cc;
    --border: #e0e3e7;
    --shadow-soft: 0 1px 3px rgba(60, 64, 67, 0.12), 0 4px 12px rgba(60, 64, 67, 0.08);
    --shadow-composer: 0 2px 8px rgba(60, 64, 67, 0.15), 0 8px 24px rgba(60, 64, 67, 0.1);
    --radius-bubble: 20px;
    --radius-pill: 999px;
    --header-h: 64px;
    --composer-h: 72px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
}

* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
}

body {
    margin: 0;
    font-family: "Google Sans", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg-app);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
}

.app {
    max-width: 820px;
    margin: 0 auto;
    height: 100dvh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    background: var(--bg-app);
    overflow: hidden;
    position: relative;
}

/* —— Header —— */
.app-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: calc(10px + var(--safe-top)) 16px 10px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    z-index: 10;
}

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

.brand-avatar,
.avatar {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-pill);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.brand-avatar,
.avatar-bot {
    background: linear-gradient(135deg, #1a73e8 0%, #4285f4 100%);
    color: #fff;
    box-shadow: 0 2px 8px rgba(26, 115, 232, 0.35);
}

.avatar-user {
    background: #e8f0fe;
    color: var(--accent);
}

.header-titles {
    min-width: 0;
}

.header-titles h1 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    line-height: 1.2;
    letter-spacing: -0.02em;
}

.header-subtitle {
    margin: 2px 0 0;
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

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

.mode-toggle {
    display: inline-flex;
    padding: 3px;
    border-radius: var(--radius-pill);
    background: #f1f3f4;
    border: 1px solid var(--border);
}

.mode-toggle-btn {
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 600;
    padding: 6px 10px;
    border-radius: var(--radius-pill);
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.mode-toggle-btn.is-active {
    background: var(--bg-surface);
    color: var(--accent);
    box-shadow: 0 1px 3px rgba(60, 64, 67, 0.16);
}

.mode-toggle-btn:not(.is-active):hover {
    color: var(--text-primary);
}

.bubble-footer {
    display: flex;
    justify-content: flex-end;
    margin-top: 8px;
}

.bubble-audio-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 1px solid var(--border);
    background: #f8fafc;
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 600;
    padding: 6px 10px;
    border-radius: var(--radius-pill);
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.bubble-audio-btn:hover {
    background: #e8f0fe;
    border-color: #c2d7fc;
    color: var(--accent);
}

.bubble-audio-btn.is-playing {
    background: #e8f0fe;
    border-color: var(--accent);
    color: var(--accent);
}

.bubble-audio-btn:disabled {
    opacity: 0.55;
    cursor: wait;
}

/* —— Config panel —— */
.config-panel {
    display: none;
    flex-shrink: 0;
    padding: 12px 16px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.config-panel label {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
}

#voiceSelect {
    flex: 1;
    min-width: 160px;
    font-size: 14px;
    padding: 10px 12px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--bg-surface);
    color: var(--text-primary);
}

/* —— Chat area —— */
.chat-main {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.quick-suggestions {
    flex-shrink: 0;
    display: flex;
    gap: 8px;
    padding: 12px 16px 4px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.quick-suggestions::-webkit-scrollbar {
    display: none;
}

.suggestion-chip {
    flex: 0 0 auto;
    padding: 8px 14px;
    border-radius: var(--radius-pill);
    border: 1px solid var(--border);
    background: var(--bg-surface);
    color: var(--text-primary);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
    white-space: nowrap;
}

.suggestion-chip:hover {
    background: #e8f0fe;
    border-color: #c2d7fc;
}

.suggestion-chip:active {
    transform: scale(0.98);
}

.chat-thread {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 8px 16px 16px;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

/* —— Messages —— */
.message-row {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    margin-bottom: 16px;
    animation: message-in 0.28s ease-out both;
}

.message-row.user {
    flex-direction: row-reverse;
}

.message-body {
    max-width: min(85%, 560px);
    min-width: 0;
}

.bubble {
    padding: 12px 16px;
    border-radius: var(--radius-bubble);
    white-space: pre-wrap;
    line-height: 1.5;
    font-size: 15px;
    word-break: break-word;
}

.bot-bubble {
    background: var(--bg-bot);
    color: var(--text-primary);
    border: 1px solid var(--border);
    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.06);
    border-bottom-left-radius: 6px;
}

.user .bubble {
    background: var(--bg-user);
    color: var(--text-primary);
    border-bottom-right-radius: 6px;
}

.message-row.error .bubble {
    background: var(--bg-error);
    color: var(--text-error);
    border: 1px solid #f9ab00;
    border-bottom-left-radius: 6px;
}

/* —— Typing indicator —— */
.message-row.typing-row .bubble {
    padding: 14px 18px;
}

.typing-bubble {
    display: flex;
    align-items: center;
    gap: 5px;
    min-height: 24px;
}

.typing-bubble.has-status {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
}

.typing-status {
    color: var(--text-secondary);
    font-size: 0.92rem;
    line-height: 1.35;
}

.typing-dots {
    display: flex;
    align-items: center;
    gap: 5px;
}

.typing-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-secondary);
    opacity: 0.45;
    animation: typing-bounce 1.2s infinite ease-in-out;
}

.typing-dots .typing-dot:nth-child(2) {
    animation-delay: 0.15s;
}

.typing-dots .typing-dot:nth-child(3) {
    animation-delay: 0.3s;
}

.hidden {
    display: none !important;
}

/* —— Composer —— */
.composer-shell {
    flex-shrink: 0;
    padding: 10px 12px calc(10px + var(--safe-bottom));
    background: linear-gradient(to top, var(--bg-app) 70%, transparent);
}

.composer {
    display: flex;
    align-items: center;
    gap: 6px;
    max-width: 100%;
    margin: 0 auto;
    padding: 6px 6px 6px 10px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 28px;
    box-shadow: var(--shadow-composer);
}

.composer-field {
    flex: 1;
    min-width: 0;
}

.composer input {
    width: 100%;
    border: none;
    outline: none;
    background: transparent;
    font-size: 16px;
    padding: 10px 4px;
    color: var(--text-primary);
}

.composer input::placeholder {
    color: #9aa0a6;
}

.icon-btn {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    padding: 0;
    border: none;
    border-radius: var(--radius-pill);
    background: transparent;
    color: var(--text-secondary);
    font-size: 18px;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
}

.icon-btn:hover {
    background: #f1f3f4;
    color: var(--text-primary);
}

.header-icon-btn {
    background: #f1f3f4;
    font-size: 16px;
}

.composer-icon {
    width: 38px;
    height: 38px;
    font-size: 17px;
}

.send-btn {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    padding: 0;
    border: none;
    border-radius: var(--radius-pill);
    background: var(--accent);
    color: #fff;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
}

.send-btn:hover:not(:disabled) {
    background: var(--accent-hover);
}

.send-btn:active:not(:disabled) {
    transform: scale(0.96);
}

.send-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.send-icon {
    font-size: 20px;
    font-weight: 700;
    line-height: 1;
    margin-top: -1px;
}

.btn-secondary,
.btn-ghost {
    font-size: 13px;
    font-weight: 600;
    padding: 9px 14px;
    border-radius: 12px;
    border: none;
    cursor: pointer;
}

.btn-secondary {
    background: var(--accent);
    color: #fff;
}

.btn-ghost {
    background: #f1f3f4;
    color: var(--text-primary);
}

/* —— Scanner —— */
.scanner-panel {
    display: none;
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(var(--composer-h) + var(--safe-bottom));
    max-width: 820px;
    margin: 0 auto;
    padding: 16px;
    background: var(--bg-surface);
    border-top: 1px solid var(--border);
    z-index: 100;
    box-shadow: var(--shadow-soft);
}

#reader {
    width: 100%;
    max-width: 360px;
    min-height: 260px;
    margin: 0 auto;
}

.scanner-close {
    margin-top: 12px;
    width: 100%;
}

/* —— Animations —— */
@keyframes message-in {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes typing-bounce {
    0%,
    60%,
    100% {
        transform: translateY(0);
        opacity: 0.35;
    }
    30% {
        transform: translateY(-5px);
        opacity: 0.9;
    }
}

/* —— Mobile —— */
@media (max-width: 480px) {
    .app-header {
        padding-left: 12px;
        padding-right: 12px;
    }

    .header-titles h1 {
        font-size: 17px;
    }

    .quick-suggestions {
        padding-left: 12px;
        padding-right: 12px;
    }

    .chat-thread {
        padding-left: 12px;
        padding-right: 12px;
    }

    .bubble {
        font-size: 14px;
        padding: 11px 14px;
    }

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

    .composer-shell {
        padding-left: 8px;
        padding-right: 8px;
    }

    .composer-icon {
        width: 36px;
        height: 36px;
    }

    .mode-toggle-btn {
        font-size: 11px;
        padding: 5px 8px;
    }

    .send-btn {
        width: 38px;
        height: 38px;
    }

    .scanner-panel {
        bottom: calc(64px + var(--safe-bottom));
    }
}

@media (prefers-reduced-motion: reduce) {
    .message-row {
        animation: none;
    }

    .typing-dot {
        animation: none;
        opacity: 0.6;
    }

    .chat-thread {
        scroll-behavior: auto;
    }
}
