:root {
    --bg: #f6f7fb;
    --panel: #ffffff;
    --panel-2: #f1f3f9;
    --ink: #0b1220;
    --ink-soft: #4a5568;
    --ink-mute: #6b7280;
    --line: #e5e7eb;
    --brand: #0d6e4f;
    --brand-ink: #ffffff;
    --brand-soft: #e6f4ef;
    --user-bg: #0d6e4f;
    --user-ink: #ffffff;
    --bot-bg: #ffffff;
    --bot-ink: #0b1220;
    --ok: #10b981;
    --warn: #f59e0b;
    --err: #ef4444;
    --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 8px 24px rgba(15, 23, 42, 0.06);
    --radius: 14px;
    --maxw: 920px;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--bg);
    color: var(--ink);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

button,
input,
textarea {
    font-family: inherit;
    font-size: inherit;
    color: inherit;
}

.app {
    display: grid;
    grid-template-rows: auto auto auto 1fr;
    height: 100dvh;
    max-width: var(--maxw);
    margin: 0 auto;
    background: var(--bg);
}

.topbar {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 14px;
    padding: 12px 18px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
}

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

.brand-logo {
    width: 36px;
    height: 36px;
    border-radius: 9px;
    background: var(--brand);
    color: var(--brand-ink);
    display: grid;
    place-items: center;
    font-weight: 700;
    box-shadow: var(--shadow);
    flex-shrink: 0;
}

.brand-text {
    display: flex;
    flex-direction: column;
    line-height: 1.15;
    min-width: 0;
}

.brand-text strong {
    font-size: 15px;
}

.brand-text span {
    font-size: 12px;
    color: var(--ink-mute);
}

.header-search-wrap {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    max-width: 420px;
    margin: 0 auto;
    padding: 0 16px;
    height: 42px;
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 999px;
    transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}

.header-search-wrap:focus-within {
    background: #fff;
    border-color: var(--brand);
    box-shadow: 0 0 0 3px rgba(13, 110, 79, 0.12);
}

.header-search-icon {
    color: var(--ink-mute);
    flex-shrink: 0;
}

.header-search {
    flex: 1;
    min-width: 0;
    border: 0;
    background: transparent;
    outline: none;
    font-size: 14px;
    color: var(--ink);
}

.header-search::placeholder {
    color: var(--ink-mute);
}

.topbar-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    justify-content: flex-end;
}

.search-results {
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    padding: 10px 18px 14px;
    max-height: 220px;
    overflow-y: auto;
}

.search-results[hidden] {
    display: none;
}

.search-result {
    padding: 10px 14px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--panel-2);
    margin-bottom: 8px;
}

.search-result:last-child {
    margin-bottom: 0;
}

.search-result-title {
    font-weight: 600;
    font-size: 13px;
    margin-bottom: 4px;
}

.search-result-meta {
    font-size: 11px;
    color: var(--ink-mute);
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.search-result-text {
    font-size: 13px;
    color: var(--ink-soft);
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.search-empty {
    color: var(--ink-mute);
    font-size: 13px;
    text-align: center;
    padding: 8px 0;
}

.auth-panel {
    display: none;
    gap: 8px;
    align-items: center;
    padding: 10px 18px;
    background: var(--panel-2);
    border-bottom: 1px solid var(--line);
}

.auth-panel label {
    font-size: 12px;
    color: var(--ink-mute);
    white-space: nowrap;
}

.auth-panel input {
    flex: 1;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 8px 14px;
    background: white;
}

body.dev-mode .auth-panel {
    display: flex;
}

body:not(.dev-mode) .dev-only {
    display: none !important;
}

.dev-toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: #111827;
    color: #ffffff;
    padding: 10px 18px;
    border-radius: 999px;
    font-size: 13px;
    z-index: 9999;
    opacity: 0;
    transition: opacity 0.18s ease, transform 0.18s ease;
}

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

.dev-toast.on {
    background: #15803d;
}

.voice-select-wrap {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 4px 10px;
}

.voice-select {
    border: 0;
    background: transparent;
    font-size: 13px;
}

.btn {
    border: 1px solid var(--line);
    background: var(--panel);
    color: var(--ink-soft);
    border-radius: 999px;
    padding: 8px 14px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-weight: 500;
}

.btn:hover {
    background: var(--panel-2);
}

.btn-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--ink-mute);
}

.btn-primary {
    background: var(--brand);
    color: var(--brand-ink);
    border-color: var(--brand);
}

.btn-mic {
    width: 38px;
    height: 38px;
    padding: 0;
    border-radius: 50%;
    flex-shrink: 0;
}

.btn-mic.recording {
    background: var(--err);
    color: #fff;
    border-color: var(--err);
}

.btn-send {
    min-width: 88px;
    justify-content: center;
}

.dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--line);
    display: inline-block;
}

.dot.on {
    background: var(--ok);
}

.stage {
    position: relative;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.chat {
    flex: 1;
    overflow-y: auto;
    padding: 20px 18px 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.app:not(.has-chat) .chat {
    display: none;
}

.app.has-chat .chat {
    display: flex;
}

.center-compose {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    max-width: 640px;
    margin: 0 auto;
    padding: 24px 18px 32px;
    gap: 18px;
}

.app:not(.has-chat) .center-compose {
    flex: 1;
}

.app.has-chat .center-compose {
    flex-shrink: 0;
    justify-content: flex-end;
    padding-top: 12px;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: linear-gradient(to top, var(--panel) 70%, transparent);
}

.app.has-chat .welcome {
    display: none;
}

.welcome {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

.welcome-avatar {
    width: 56px;
    height: 56px;
    border-radius: 16px;
    background: linear-gradient(135deg, #0d6e4f, #3aa67d);
    color: white;
    display: grid;
    place-items: center;
    font-weight: 700;
    font-size: 24px;
}

.welcome h1 {
    margin: 0;
    font-size: 28px;
    font-weight: 700;
}

.welcome p {
    margin: 0;
    color: var(--ink-soft);
    max-width: 480px;
}

.suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
    margin-top: 6px;
}

.suggestion {
    background: var(--brand-soft);
    color: var(--brand);
    border: 1px solid #b8dfd0;
    border-radius: 999px;
    padding: 7px 14px;
    cursor: pointer;
    font-size: 13px;
}

.talk-form {
    width: 100%;
}

.talk-field {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    width: 100%;
    padding: 14px 16px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 24px;
    box-shadow: var(--shadow);
    transition: border-color 0.15s, box-shadow 0.15s;
}

.talk-field:focus-within {
    border-color: var(--brand);
    box-shadow: 0 0 0 4px rgba(13, 110, 79, 0.1), var(--shadow);
}

.app:not(.has-chat) .talk-field {
    min-height: 64px;
}

#input {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    resize: none;
    max-height: 160px;
    padding: 4px 0;
    font-size: 16px;
    line-height: 1.45;
    text-align: left;
}

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

.composer-hint {
    color: var(--ink-mute);
    font-size: 12px;
    text-align: center;
}

.dot-sep {
    color: var(--line);
}

#status.busy { color: var(--warn); }
#status.error { color: var(--err); }

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

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

.msg .avatar {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    flex-shrink: 0;
    display: grid;
    place-items: center;
    font-size: 11px;
    font-weight: 700;
    color: white;
}

.msg.user .avatar {
    background: var(--ink);
}

.msg.bot .avatar {
    background: linear-gradient(135deg, #0d6e4f, #3aa67d);
}

.bubble {
    padding: 10px 14px;
    border-radius: 14px;
    max-width: calc(100% - 50px);
    white-space: pre-wrap;
    word-wrap: break-word;
    box-shadow: var(--shadow);
    border: 1px solid var(--line);
}

.msg.user .bubble {
    background: var(--user-bg);
    color: var(--user-ink);
    border-color: var(--user-bg);
}

.agent-tag {
    margin-top: 6px;
    font-size: 11px;
    color: var(--ink-mute);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.bubble.typing {
    display: inline-flex;
    gap: 4px;
    padding: 12px 14px;
}

.typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--ink-mute);
    opacity: 0.4;
    animation: blink 1.2s infinite;
}

.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes blink {
    0%, 80%, 100% { opacity: 0.3; }
    40% { opacity: 1; }
}

.debug-panel {
    margin-top: 6px;
    background: #0b1220;
    color: #d3d6dd;
    border-radius: 10px;
    padding: 10px 12px;
    font-family: ui-monospace, monospace;
    font-size: 11.5px;
    overflow-x: auto;
}

@media (max-width: 720px) {
    .topbar {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .brand {
        justify-content: center;
    }

    .header-search-wrap {
        max-width: none;
    }

    .topbar-actions {
        justify-content: center;
        flex-wrap: wrap;
    }

    .brand-text span {
        display: none;
    }

    .welcome h1 {
        font-size: 22px;
    }
}

@media (max-width: 520px) {
    .auth-panel {
        flex-wrap: wrap;
    }

    .talk-field {
        padding: 12px;
    }

    .btn-send {
        min-width: 72px;
        padding-inline: 12px;
    }
}
