        /* ==========================================================================
       Design tokens
       ========================================================================== */
        :root {
            --accent: #4f7cff;
            --accent-strong: #3b66e6;
            --accent-contrast: #ffffff;

            --bg: #f4f6fb;
            --surface: #ffffff;
            --surface-2: #eef1f8;
            --border: #e2e6f0;
            --text: #141824;
            --text-muted: #6b7280;

            --bubble-bot: #eef1f8;
            --bubble-bot-text: #141824;

            --danger: #ef4444;

            --radius-sm: 10px;
            --radius: 16px;
            --radius-lg: 22px;

            --shadow-1: 0 1px 2px rgba(16, 24, 40, .06);
            --shadow-card: 0 24px 60px -20px rgba(16, 24, 40, .35), 0 2px 10px rgba(16, 24, 40, .08);

            --header-h: 60px;
            --tap: 44px;
            /* minimum touch target */

            --ease: cubic-bezier(.22, .61, .36, 1);
        }

        @media (prefers-color-scheme: dark) {
            :root {
                --bg: #070a14;
                --surface: #101527;
                --surface-2: #1a2138;
                --border: #232b45;
                --text: #e9ecf5;
                --text-muted: #99a1b8;

                --bubble-bot: #1a2138;
                --bubble-bot-text: #e9ecf5;

                --shadow-card: 0 24px 70px -20px rgba(0, 0, 0, .75), 0 2px 10px rgba(0, 0, 0, .4);
            }
        }

        /* Manual theme override via [data-theme] (toggled by the header button) */
        html[data-theme="light"] {
            --bg: #f4f6fb;
            --surface: #ffffff;
            --surface-2: #eef1f8;
            --border: #e2e6f0;
            --text: #141824;
            --text-muted: #6b7280;
            --bubble-bot: #eef1f8;
            --bubble-bot-text: #141824;
            --shadow-card: 0 24px 60px -20px rgba(16, 24, 40, .35), 0 2px 10px rgba(16, 24, 40, .08);
        }

        html[data-theme="dark"] {
            --bg: #070a14;
            --surface: #101527;
            --surface-2: #1a2138;
            --border: #232b45;
            --text: #e9ecf5;
            --text-muted: #99a1b8;
            --bubble-bot: #1a2138;
            --bubble-bot-text: #e9ecf5;
            --shadow-card: 0 24px 70px -20px rgba(0, 0, 0, .75), 0 2px 10px rgba(0, 0, 0, .4);
        }

        /* ==========================================================================
       Reset & base
       ========================================================================== */
        *,
        *::before,
        *::after {
            box-sizing: border-box;
        }

        html,
        body {
            margin: 0;
            height: 100%;
            overflow: hidden;
            /* no page-level scrolling — only the message list scrolls */
        }

        body {
            background: var(--bg);
            color: var(--text);
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji", sans-serif;
            font-size: 15px;
            line-height: 1.45;
            -webkit-font-smoothing: antialiased;
            text-rendering: optimizeLegibility;
            -webkit-tap-highlight-color: transparent;
        }

        button {
            font: inherit;
            color: inherit;
        }

        /* Focus visibility for keyboard users */
        :focus-visible {
            outline: 2px solid var(--accent);
            outline-offset: 2px;
        }

        /* ==========================================================================
       App shell — mobile-first full screen
       ========================================================================== */
        .app {
            position: relative;
            display: flex;
            flex-direction: column;
            width: 100%;
            height: 100vh;
            /* fallback */
            height: 100dvh;
            /* modern dynamic viewport */
            height: var(--app-height, 100dvh);
            /* JS override for iOS keyboard */
            overflow: hidden;
            background: var(--surface);
        }

        /* --------------------------- Header (sticky top) ------------------------- */
        .app-header {
            position: relative;
            z-index: 10;
            display: flex;
            align-items: center;
            gap: 12px;
            flex: 0 0 auto;
            min-height: var(--header-h);
            padding: 8px 12px;
            padding-top: calc(8px + env(safe-area-inset-top));
            background: var(--surface);
            /* fallback for browsers without color-mix() */
            background: color-mix(in srgb, var(--surface) 86%, transparent);
            backdrop-filter: saturate(180%) blur(14px);
            -webkit-backdrop-filter: saturate(180%) blur(14px);
            border-bottom: 1px solid var(--border);
        }

        .brand {
            display: flex;
            align-items: center;
            gap: 10px;
            min-width: 0;
        }

        /* Hamburger trigger — replaces the old decorative logo beside the title */
        .menu-btn {
            flex: 0 0 auto;
        }

        .brand-text {
            min-width: 0;
        }

        .brand-title {
            margin: 0;
            font-size: 16px;
            font-weight: 700;
            letter-spacing: -.01em;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .icon-btn {
            display: grid;
            place-items: center;
            width: var(--tap);
            height: var(--tap);
            border: 0;
            border-radius: 12px;
            background: transparent;
            color: var(--text-muted);
            cursor: pointer;
            transition: background-color .18s var(--ease), color .18s var(--ease), transform .18s var(--ease);
        }

        .icon-btn:hover {
            background: var(--surface-2);
            color: var(--text);
        }

        .icon-btn:active {
            transform: scale(.94);
        }

        .icon-btn svg {
            width: 20px;
            height: 20px;
        }

        /* ------------------------- Header menu (dropdown) ----------------------- */
        .app-menu {
            position: absolute;
            top: calc(100% - 2px);
            left: calc(12px + env(safe-area-inset-left, 0px));
            z-index: 30;
            display: flex;
            flex-direction: column;
            gap: 2px;
            min-width: 200px;
            max-width: calc(100vw - 24px);
            padding: 6px;
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 14px;
            box-shadow: var(--shadow-card);
        }

        .app-menu[hidden] {
            display: none;
        }

        .menu-item {
            display: flex;
            align-items: center;
            gap: 12px;
            width: 100%;
            min-height: var(--tap);
            padding: 10px 12px;
            border: 0;
            border-radius: 10px;
            background: transparent;
            color: var(--text);
            font-size: 15px;
            text-align: left;
            cursor: pointer;
        }

        .menu-item svg {
            flex: 0 0 auto;
            width: 20px;
            height: 20px;
            color: var(--text-muted);
        }

        /* Hover is a progressive nicety only; touch and keyboard never need it */
        @media (hover: hover) {
            .menu-item:hover {
                background: var(--surface-2);
            }
        }

        .menu-item:active {
            background: var(--surface-2);
        }

        .menu-item--danger,
        .menu-item--danger svg {
            color: var(--danger);
        }

        /* Detected-language trigger — tap to correct the detected language. Kept
           at the 44px touch target from Rule 1 without enlarging the visual. */
        .lang-display {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            flex: 0 1 auto;
            min-width: var(--tap);
            min-height: var(--tap);
            margin-inline-start: 2px;
            padding: 0 8px;
            border: 0;
            border-radius: 12px;
            background: transparent;
            color: var(--text-muted);
            font-size: 13px;
            font-weight: 600;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            cursor: pointer;
        }

        .lang-display:hover {
            background: var(--surface-2);
            color: var(--text);
        }

        .lang-display:active {
            transform: scale(.96);
        }

        .lang-display svg {
            flex: 0 0 auto;
            width: 16px;
            height: 16px;
        }

        .lang-display #lang-display-name {
            min-width: 0;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        /* ------------------------- Language dialog ---------------------------- */
        .lang-dialog {
            width: min(420px, calc(100vw - 32px));
            max-width: calc(100vw - 32px);
            max-height: calc(100dvh - 32px);
            overflow: auto;
            padding: 16px;
            padding-bottom: calc(16px + env(safe-area-inset-bottom));
            border: 1px solid var(--border);
            border-radius: 16px;
            background: var(--surface);
            color: var(--text);
            box-shadow: var(--shadow-card);
        }

        .lang-dialog:not([open]) {
            display: none;
        }

        .lang-dialog::backdrop {
            background: rgb(0 0 0 / 45%);
            backdrop-filter: blur(2px);
        }

        .lang-dialog__head {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 8px;
            margin-bottom: 12px;
        }

        .lang-dialog__title {
            margin: 0;
            font-size: 16px;
            font-weight: 700;
        }

        .lang-dialog__close {
            flex: 0 0 auto;
            margin: -6px -6px -6px 0;
        }

        .lang-dialog__input {
            width: 100%;
            min-height: var(--tap);
            padding: 10px 12px;
            border: 1px solid var(--border);
            border-radius: 12px;
            background: var(--surface-2);
            color: var(--text);
            /* 16px stops iOS from zooming the viewport on focus */
            font-size: 16px;
        }

        .lang-dialog__error,
        .lang-dialog__notice {
            margin: 8px 0 0;
            font-size: 13px;
            line-height: 1.4;
        }

        .lang-dialog__error {
            color: var(--danger);
        }

        .lang-dialog__notice {
            color: var(--text-muted);
        }

        .lang-dialog__actions {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
            margin-top: 14px;
        }

        .lang-dialog__btn {
            flex: 1 1 auto;
            min-height: var(--tap);
            padding: 10px 14px;
            border: 1px solid var(--border);
            border-radius: 12px;
            background: var(--surface-2);
            color: var(--text);
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
        }

        .lang-dialog__btn--primary {
            flex-basis: 100%;
            border-color: transparent;
            background: var(--accent);
            color: var(--accent-contrast);
        }

        /* Read-aloud control inside a bot bubble. Kept at the 44px touch target
           required by Rule 1, with offsets so it does not bulge the bubble. */
        .speak-btn {
            display: inline-grid;
            place-items: center;
            width: var(--tap);
            height: var(--tap);
            margin: 2px -8px -8px 0;
            padding: 0;
            border: 0;
            border-radius: 10px;
            background: transparent;
            color: var(--text-muted);
            cursor: pointer;
        }

        .speak-btn svg {
            width: 18px;
            height: 18px;
        }

        .speak-btn:hover {
            color: var(--text);
            background: color-mix(in srgb, var(--text) 8%, transparent);
        }

        .speak-btn.is-speaking {
            color: var(--accent);
        }

        /* The first cloud chunk is a network round trip, so the control shows
           that it is working instead of appearing to hang. */
        .speak-btn.is-loading {
            opacity: 0.55;
        }

        /* ------------------------ Message list (scrolls) ------------------------ */
        .messages {
            flex: 1 1 auto;
            min-height: 0;
            /* required so flex child can scroll */
            overflow-y: auto;
            overscroll-behavior: contain;
            -webkit-overflow-scrolling: touch;
            padding: 16px 12px 8px;
            display: flex;
            flex-direction: column;
            gap: 12px;
            scroll-behavior: smooth;
        }

        .day-divider {
            align-self: center;
            padding: 4px 12px;
            border-radius: 999px;
            background: var(--surface-2);
            color: var(--text-muted);
            font-size: 11px;
            font-weight: 600;
            letter-spacing: .02em;
            text-transform: uppercase;
        }

        /* Message row (avatar + bubble) */
        .message {
            display: flex;
            align-items: flex-end;
            gap: 8px;
            max-width: 100%;
            animation: message-in .3s var(--ease) both;
        }

        .message.bot {
            justify-content: flex-start;
        }

        .message.user {
            justify-content: flex-end;
        }

        @keyframes message-in {
            from {
                opacity: 0;
                transform: translateY(10px) scale(.98);
            }

            to {
                opacity: 1;
                transform: translateY(0) scale(1);
            }
        }

        .avatar {
            flex: 0 0 auto;
            width: 30px;
            height: 30px;
            border-radius: 50%;
            display: grid;
            place-items: center;
            font-size: 12px;
            font-weight: 700;
            color: #fff;
            background: linear-gradient(135deg, #8b5cf6, var(--accent));
            user-select: none;
        }

        .message.user .avatar {
            display: none;
        }

        /* user bubble is self-explanatory */

        .bubble {
            position: relative;
            max-width: 82%;
            padding: 10px 14px;
            border-radius: var(--radius-lg);
            box-shadow: var(--shadow-1);
            word-wrap: break-word;
            overflow-wrap: anywhere;
            white-space: pre-wrap;
        }

        .message.bot .bubble {
            background: var(--bubble-bot);
            color: var(--bubble-bot-text);
            border-bottom-left-radius: 6px;
        }

        .message.user .bubble {
            background: linear-gradient(135deg, var(--accent), var(--accent-strong));
            color: var(--accent-contrast);
            border-bottom-right-radius: 6px;
        }

        .meta {
            margin-top: 4px;
            font-size: 11px;
            opacity: .7;
            font-variant-numeric: tabular-nums;
        }

        .message.user .meta {
            text-align: right;
        }

        /* Typing indicator */
        .typing .bubble {
            display: inline-flex;
            align-items: center;
            gap: 5px;
            padding: 14px 16px;
        }

        .typing-dot {
            width: 7px;
            height: 7px;
            border-radius: 50%;
            background: var(--text-muted);
            opacity: .5;
            animation: dot-bounce 1.2s var(--ease) infinite;
        }

        .typing-dot:nth-child(2) {
            animation-delay: .16s;
        }

        .typing-dot:nth-child(3) {
            animation-delay: .32s;
        }

        @keyframes dot-bounce {

            0%,
            60%,
            100% {
                transform: translateY(0);
                opacity: .45;
            }

            30% {
                transform: translateY(-5px);
                opacity: 1;
            }
        }

        /* ------------------------- Composer (sticky bottom) --------------------- */
        .composer {
            flex: 0 0 auto;
            z-index: 10;
            padding: 10px 12px;
            padding-bottom: calc(10px + env(safe-area-inset-bottom));
            background: var(--surface);
            /* fallback for browsers without color-mix() */
            background: color-mix(in srgb, var(--surface) 90%, transparent);
            backdrop-filter: saturate(180%) blur(14px);
            -webkit-backdrop-filter: saturate(180%) blur(14px);
            border-top: 1px solid var(--border);
        }

        .composer-inner {
            display: flex;
            align-items: flex-end;
            gap: 8px;
            max-width: 100%;
        }

        .input-wrap {
            flex: 1 1 auto;
            display: flex;
            align-items: center;
            min-height: var(--tap);
            padding: 6px 8px 6px 14px;
            border-radius: var(--radius);
            background: var(--surface-2);
            border: 1px solid transparent;
            transition: border-color .18s var(--ease), background-color .18s var(--ease);
        }

        .input-wrap:focus-within {
            border-color: var(--accent);
            background: var(--surface);
        }

        #message-input {
            flex: 1 1 auto;
            width: 100%;
            min-height: 24px;
            max-height: 140px;
            /* ~5 lines, then scrolls */
            padding: 4px 0;
            border: 0;
            background: transparent;
            color: var(--text);
            font: inherit;
            line-height: 1.4;
            resize: none;
            outline: none;
            overflow-y: auto;
        }

        #message-input::placeholder {
            color: var(--text-muted);
        }

        /* -------------------------- Dictation (microphone) ---------------------- */
        .mic-btn {
            display: grid;
            place-items: center;
            flex: 0 0 auto;
            width: var(--tap);
            height: var(--tap);
            padding: 0;
            border: 0;
            border-radius: 50%;
            background: var(--surface-2);
            color: var(--text-muted);
            cursor: pointer;
            transition: background-color .18s var(--ease), color .18s var(--ease), transform .18s var(--ease);
        }

        /* Revealed by JS only where the SpeechRecognition API exists */
        .mic-btn[hidden] {
            display: none;
        }

        .mic-btn:hover {
            background: color-mix(in srgb, var(--accent) 14%, transparent);
            color: var(--text);
        }

        .mic-btn:active {
            transform: scale(.92);
        }

        .mic-btn:disabled {
            opacity: .45;
            cursor: not-allowed;
        }

        .mic-btn svg {
            width: 20px;
            height: 20px;
        }

        /* Active recording state — pulsing red ring */
        .mic-btn.is-listening {
            color: #fff;
            background: var(--danger);
            animation: mic-pulse 1.6s var(--ease) infinite;
        }

        .mic-btn.is-listening:hover {
            background: var(--danger);
            color: #fff;
        }

        @keyframes mic-pulse {
            0% {
                box-shadow: 0 0 0 0 rgba(239, 68, 68, .5);
            }

            70% {
                box-shadow: 0 0 0 8px rgba(239, 68, 68, 0);
            }

            100% {
                box-shadow: 0 0 0 0 rgba(239, 68, 68, 0);
            }
        }

        .send-btn {
            display: grid;
            place-items: center;
            flex: 0 0 auto;
            width: var(--tap);
            height: var(--tap);
            border: 0;
            border-radius: 50%;
            background: var(--accent);
            color: var(--accent-contrast);
            cursor: pointer;
            box-shadow: 0 6px 16px -6px rgba(79, 124, 255, .45);
            /* fallback */
            box-shadow: 0 6px 16px -6px color-mix(in srgb, var(--accent) 80%, transparent);
            transition: transform .16s var(--ease), background-color .16s var(--ease), opacity .16s var(--ease);
        }

        .send-btn svg {
            width: 20px;
            height: 20px;
        }

        .send-btn:hover:not(:disabled) {
            background: var(--accent-strong);
        }

        .send-btn:active:not(:disabled) {
            transform: scale(.92);
        }

        .send-btn:disabled {
            opacity: .45;
            cursor: not-allowed;
            box-shadow: none;
            background: var(--text-muted);
        }

        /* Screen-reader-only helper */
        .sr-only {
            position: absolute;
            width: 1px;
            height: 1px;
            padding: 0;
            margin: -1px;
            overflow: hidden;
            clip: rect(0 0 0 0);
            white-space: nowrap;
            border: 0;
        }

        /* ==========================================================================
       Tablet / desktop — centered floating card
       ========================================================================== */
        @media (min-width: 768px) {
            body {
                display: grid;
                place-items: center;
                padding: 24px;
            }

            .app {
                width: 100%;
                max-width: 768px;
                height: min(880px, calc(100dvh - 48px));
                border: 1px solid var(--border);
                border-radius: 24px;
                box-shadow: var(--shadow-card);
                overflow: hidden;
            }

            .app-header {
                border-top-left-radius: 24px;
                border-top-right-radius: 24px;
            }

            .composer {
                padding-bottom: 14px;
            }

            .composer-inner {
                max-width: 100%;
            }

            .bubble {
                max-width: 72%;
            }
        }

        @media (min-width: 1000px) {
            .app {
                max-width: 1000px;
            }

            .bubble {
                max-width: 68%;
            }
        }

        /* Wide monitors: keep the message column readable inside the big card */
        @media (min-width: 1200px) {
            .messages {
                padding-inline: 28px;
            }
        }

        /* ==========================================================================
       Motion preferences
       ========================================================================== */
        @media (prefers-reduced-motion: reduce) {

            *,
            *::before,
            *::after {
                animation-duration: .001ms !important;
                animation-iteration-count: 1 !important;
                transition-duration: .001ms !important;
                scroll-behavior: auto !important;
            }
        }
