:root {
    --bg: #f6f7fb;
    --panel: #ffffff;
    --panel-2: #f1f3f9;
    --ink: #0b1220;
    --ink-soft: #4a5568;
    --ink-mute: #6b7280;
    --line: #e5e7eb;
    --brand: #1e63ff;
    --brand-ink: #ffffff;
    --brand-soft: #e8efff;
    --user-bg: #1e63ff;
    --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: 820px;
}

* {
    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;
    -moz-osx-font-smoothing: grayscale;
}

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

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

/* Topbar */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
}

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

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

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

.brand-text strong {
    font-size: 15px;
    color: var(--ink);
}

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

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

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

body.dev-mode .dev-only {
    animation: dev-reveal 0.32s ease;
}

@keyframes dev-reveal {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: translateY(0); }
}

.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;
    font-weight: 500;
    box-shadow: 0 10px 30px rgba(0,0,0,0.25);
    z-index: 9999;
    opacity: 0;
    transition: opacity 0.18s ease, transform 0.18s ease;
    pointer-events: none;
    letter-spacing: 0.02em;
}

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

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

.dev-toast.off {
    background: #6b7280;
}

.voice-select-wrap {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 1px solid var(--line);
    background: var(--panel);
    border-radius: 10px;
    padding: 4px 10px 4px 12px;
    color: var(--ink-mute);
    font-weight: 500;
    transition: background 0.12s, border-color 0.12s;
}

.voice-select-wrap:hover {
    background: var(--panel-2);
    border-color: var(--ink-mute);
}

.voice-select-label {
    font-size: 12px;
    color: var(--ink-mute);
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

.voice-select {
    appearance: none;
    -webkit-appearance: none;
    border: 0;
    background: transparent;
    color: var(--ink);
    font: inherit;
    font-size: 13px;
    padding: 4px 18px 4px 0;
    cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6' fill='none'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%23808594' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0 center;
}

.voice-select:focus {
    outline: 2px solid var(--brand);
    outline-offset: 2px;
    border-radius: 4px;
}

.voice-select option {
    background: var(--panel);
    color: var(--ink);
}

.btn {
    border: 1px solid var(--line);
    background: var(--panel);
    color: var(--ink-soft);
    border-radius: 10px;
    padding: 8px 12px;
    cursor: pointer;
    transition: background 0.12s, color 0.12s, border-color 0.12s, transform 0.06s;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-weight: 500;
}

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

.btn:active {
    transform: translateY(1px);
}

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

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

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

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

.btn-primary:hover {
    background: #1751cf;
    color: var(--brand-ink);
}

.btn-mic {
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--panel-2);
    color: var(--ink-soft);
    border-color: var(--line);
    flex-shrink: 0;
    transition: background 0.15s, color 0.15s, transform 0.15s;
}

.btn-mic:hover {
    background: var(--brand-soft);
    color: var(--brand);
}

.btn-mic.recording {
    background: var(--err);
    color: #fff;
    border-color: var(--err);
    animation: pulse-mic 1.2s infinite;
}

.btn-mic[disabled] {
    opacity: 0.4;
    cursor: not-allowed;
}

@keyframes pulse-mic {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.55); }
    50%      { box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); }
}

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

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

/* Chat area */
.chat {
    overflow-y: auto;
    padding: 24px 18px 8px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}

.welcome {
    display: grid;
    place-items: center;
    height: 100%;
}

.welcome-card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 28px;
    max-width: 560px;
    width: 100%;
    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, #1e63ff, #6b8dff);
    color: white;
    display: grid;
    place-items: center;
    font-weight: 700;
    font-size: 24px;
    box-shadow: var(--shadow);
}

.welcome-card h1 {
    margin: 6px 0 0;
    font-size: 22px;
}

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

.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 #d6e3ff;
    border-radius: 999px;
    padding: 7px 14px;
    cursor: pointer;
    font-size: 13px;
    transition: background 0.12s, border-color 0.12s;
}

.suggestion:hover {
    background: #dde7ff;
    border-color: #b9cdff;
}

/* Messages */
.msg {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    max-width: 100%;
    animation: pop 0.18s ease-out;
}

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

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

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

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

.msg.bot .avatar {
    background: linear-gradient(135deg, #1e63ff, #6b8dff);
}

.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);
    border-bottom-right-radius: 4px;
}

.msg.bot .bubble {
    background: var(--bot-bg);
    color: var(--bot-ink);
    border-bottom-left-radius: 4px;
}

.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;
        transform: translateY(0);
    }
    40% {
        opacity: 1;
        transform: translateY(-2px);
    }
}

/* Recommendation cards */
.recos {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.reco {
    border: 1px solid var(--line);
    background: #fafbff;
    border-radius: 12px;
    padding: 12px 14px;
    font-size: 13.5px;
}

.reco-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
    margin-bottom: 6px;
}

.reco-fam {
    font-weight: 600;
    color: var(--ink);
}

.reco-codes {
    color: var(--ink-mute);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 12px;
}

.reco-kits {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 8px;
    margin-top: 6px;
}

.kit {
    background: white;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 8px 10px;
    display: flex;
    flex-direction: column;
}

.kit-tag {
    font-size: 11px;
    color: var(--ink-mute);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.kit-ref {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 13px;
    color: var(--ink);
    margin-top: 2px;
}

.kit-pvp {
    color: var(--brand);
    font-weight: 600;
    margin-top: 4px;
}

/* Debug panel */
.debug-panel {
    margin-top: 6px;
    background: #0b1220;
    color: #d3d6dd;
    border-radius: 10px;
    padding: 10px 12px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 11.5px;
    overflow-x: auto;
    white-space: pre;
}

.debug-panel .k {
    color: #94a3b8;
}

.debug-panel .v {
    color: #f4f6fb;
}

.debug-panel .ok {
    color: #34d399;
}

.debug-panel .warn {
    color: #fbbf24;
}

/* Composer */
.composer {
    background: var(--panel);
    border-top: 1px solid var(--line);
    padding: 12px 14px max(12px, env(safe-area-inset-bottom));
}

#form {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 8px 10px 8px 14px;
}

#form:focus-within {
    border-color: var(--brand);
    background: white;
    box-shadow: 0 0 0 3px rgba(30, 99, 255, 0.12);
}

#input {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    resize: none;
    max-height: 180px;
    padding: 6px 0;
    line-height: 1.45;
}

#btn-send {
    flex-shrink: 0;
    padding: 8px 14px;
    border-radius: 10px;
}

.composer-hint {
    color: var(--ink-mute);
    font-size: 11.5px;
    text-align: center;
    margin-top: 6px;
    display: flex;
    gap: 8px;
    align-items: center;
    justify-content: center;
}

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

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

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

/* Mobile tweaks */
@media (max-width: 520px) {
    .topbar {
        padding: 10px 12px;
    }
    .chat {
        padding: 14px 10px 6px;
    }
    .composer {
        padding: 8px 10px max(8px, env(safe-area-inset-bottom));
    }
    .welcome-card {
        padding: 20px;
    }
    .brand-text span {
        display: none;
    }
}
