:root {
    --bg-primary: #08080f;
    --bg-surface: rgba(255, 255, 255, 0.03);
    --border-subtle: rgba(255, 255, 255, 0.07);
    --border-active: rgba(99, 102, 241, 0.5);
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --accent-1: #6366f1;
    --accent-2: #8b5cf6;
    --accent-3: #a855f7;
    --gradient: linear-gradient(135deg, #6366f1, #8b5cf6, #a855f7);
    --gradient-subtle: linear-gradient(135deg, rgba(99, 102, 241, 0.2), rgba(168, 85, 247, 0.1));
    --danger: #ef4444;
    --success: #22c55e;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;
    --shadow-glow: 0 0 40px rgba(99, 102, 241, 0.08);
}

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

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 14px;
    color: var(--text-primary);
    background: var(--bg-primary);
    position: relative;
}

body::before {
    content: '';
    position: fixed;
    inset: 0;
    background:
        radial-gradient(ellipse 600px 400px at 15% 20%, rgba(99, 102, 241, 0.08) 0%, transparent 70%),
        radial-gradient(ellipse 500px 500px at 85% 80%, rgba(168, 85, 247, 0.06) 0%, transparent 70%),
        radial-gradient(ellipse 300px 300px at 50% 50%, rgba(139, 92, 246, 0.03) 0%, transparent 70%);
    pointer-events: none;
}

/* ===== App Shell ===== */
#app {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100vw;
    background: rgba(255, 255, 255, 0.02);
    backdrop-filter: blur(60px) saturate(150%);
    -webkit-backdrop-filter: blur(60px) saturate(150%);
}

/* ===== Header ===== */
.header {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 10px 20px;
    border-bottom: 1px solid var(--border-subtle);
    background: rgba(0, 0, 0, 0.3);
    flex-shrink: 0;
    min-height: 56px;
}

.logo {
    font-size: 16px;
    font-weight: 800;
    letter-spacing: -0.03em;
    background: var(--gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    white-space: nowrap;
    margin-right: 8px;
}

.controls {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    flex-wrap: wrap;
}

.ctrl-group {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.ctrl-label {
    font-size: 9px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--text-muted);
    padding-left: 10px;
}

select {
    appearance: none;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    padding: 6px 28px 6px 10px;
    color: var(--text-primary);
    font-size: 12px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.15s ease;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 12 12'%3E%3Cpath fill='%2394a3b8' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
    min-width: 150px;
}

select:hover { background-color: rgba(255, 255, 255, 0.07); border-color: rgba(99, 102, 241, 0.3); }
select:focus { outline: none; border-color: var(--accent-1); box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.15); }
select option { background: #111118; color: var(--text-primary); }

.provider-col {
    display: flex;
    align-items: center;
    gap: 6px;
}

.provider-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-muted);
    flex-shrink: 0;
    transition: background 0.2s;
}

.provider-dot.active { background: var(--success); box-shadow: 0 0 6px rgba(34, 197, 94, 0.4); }

.model-count {
    font-size: 11px;
    color: var(--text-muted);
    white-space: nowrap;
    padding-left: 4px;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 11px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.15s ease;
    padding: 6px 12px;
    white-space: nowrap;
}

.btn:active { transform: scale(0.97); }

.btn-icon {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border-subtle);
    color: var(--text-secondary);
    padding: 6px 8px;
    font-size: 13px;
}

.btn-icon:hover { background: rgba(255, 255, 255, 0.08); color: var(--text-primary); border-color: rgba(99, 102, 241, 0.3); }

.btn-primary {
    background: var(--gradient);
    color: white;
    box-shadow: 0 2px 10px rgba(99, 102, 241, 0.25);
}

.btn-primary:hover { box-shadow: 0 4px 16px rgba(99, 102, 241, 0.35); transform: translateY(-0.5px); }
.btn-primary:disabled { opacity: 0.35; cursor: not-allowed; transform: none; box-shadow: none; }

.btn-ghost {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border-subtle);
    color: var(--text-secondary);
}

.btn-ghost:hover { background: rgba(255, 255, 255, 0.08); color: var(--text-primary); }

.header-right {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

/* ===== Chat ===== */
.chat {
    flex: 1;
    overflow-y: auto;
    padding: 24px 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
}

.chat::-webkit-scrollbar { width: 4px; }
.chat::-webkit-scrollbar-track { background: transparent; }
.chat::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.06); border-radius: 10px; }
.chat::-webkit-scrollbar-thumb:hover { background: rgba(255, 255, 255, 0.12); }

.msg {
    max-width: 78%;
    padding: 12px 18px;
    border-radius: var(--radius-lg);
    font-size: 13.5px;
    line-height: 1.65;
    animation: msgIn 0.2s ease;
    white-space: pre-wrap;
    word-wrap: break-word;
}

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

.msg.user {
    align-self: flex-end;
    background: var(--gradient);
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 12px rgba(99, 102, 241, 0.2);
    color: white;
}

.msg.ai {
    align-self: flex-start;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border-subtle);
    border-bottom-left-radius: 4px;
}

.msg.error {
    align-self: flex-start;
    background: rgba(239, 68, 68, 0.08);
    border: 1px solid rgba(239, 68, 68, 0.2);
    border-bottom-left-radius: 4px;
    color: #fca5a5;
}

.typing {
    display: inline-flex;
    gap: 4px;
    align-items: center;
    padding: 4px 0;
}

.typing span {
    width: 5px; height: 5px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: typing 1.4s infinite;
}

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

@keyframes typing {
    0%, 60%, 100% { opacity: 0.2; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-3px); }
}

/* ===== Empty State ===== */
.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: var(--text-muted);
    text-align: center;
    padding: 40px 20px;
    user-select: none;
}

.empty-state .icon {
    font-size: 36px;
    opacity: 0.15;
    margin-bottom: 4px;
}

.empty-state h3 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.empty-state p {
    font-size: 13px;
    max-width: 360px;
    line-height: 1.6;
    color: var(--text-muted);
}

.empty-state .shortcuts {
    display: flex;
    gap: 20px;
    margin-top: 8px;
    font-size: 11px;
    color: var(--text-muted);
}

.empty-state .shortcuts span {
    display: flex;
    align-items: center;
    gap: 4px;
}

.empty-state .shortcuts kbd {
    background: rgba(255,255,255,0.06);
    border: 1px solid var(--border-subtle);
    border-radius: 4px;
    padding: 1px 5px;
    font-size: 10px;
    font-family: inherit;
}

/* ===== Input ===== */
.input-area {
    padding: 12px 20px 16px;
    border-top: 1px solid var(--border-subtle);
    background: rgba(0, 0, 0, 0.2);
    display: flex;
    gap: 8px;
    align-items: flex-end;
    flex-shrink: 0;
}

.input-wrap {
    flex: 1;
    display: flex;
    align-items: center;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 8px 14px;
    transition: all 0.15s ease;
}

.input-wrap:focus-within {
    border-color: var(--accent-1);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1);
    background: rgba(255, 255, 255, 0.05);
}

textarea {
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-primary);
    width: 100%;
    resize: none;
    font-size: 13.5px;
    font-family: inherit;
    line-height: 1.5;
    max-height: 120px;
}

textarea::placeholder { color: var(--text-muted); }

.btn-send {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-md);
    border: none;
    background: var(--gradient);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    transition: all 0.15s ease;
    box-shadow: 0 2px 10px rgba(99, 102, 241, 0.2);
    flex-shrink: 0;
}

.btn-send:hover { transform: scale(1.05); box-shadow: 0 4px 16px rgba(99, 102, 241, 0.3); }
.btn-send:active { transform: scale(0.93); }

/* ===== Modal ===== */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    z-index: 100;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal.open { display: flex; }

.modal-box {
    background: rgba(18, 18, 28, 0.98);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-xl);
    width: min(100%, 480px);
    max-height: min(90vh, 640px);
    display: flex;
    flex-direction: column;
    box-shadow:
        0 20px 60px rgba(0, 0, 0, 0.6),
        inset 0 1px 0 rgba(255, 255, 255, 0.05);
    animation: modalIn 0.2s ease;
}

@keyframes modalIn {
    from { opacity: 0; transform: scale(0.96) translateY(8px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px 12px;
    border-bottom: 1px solid var(--border-subtle);
    flex-shrink: 0;
}

.modal-title {
    font-size: 15px;
    font-weight: 700;
    background: var(--gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.modal-close {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 16px;
    transition: all 0.15s;
}

.modal-close:hover { background: rgba(255, 255, 255, 0.08); color: var(--text-primary); }

.modal-body {
    overflow-y: auto;
    padding: 16px 20px 20px;
}

.modal-body::-webkit-scrollbar { width: 4px; }
.modal-body::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.06); border-radius: 10px; }

.modal-hint {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 16px;
    line-height: 1.5;
}

.api-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
}

.api-row label {
    flex-shrink: 0;
    width: 110px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-secondary);
}

.api-row label .check { display: none; color: var(--success); margin-left: 2px; }
.api-row.saved label .check { display: inline; }

.api-row input {
    flex: 1;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    padding: 7px 10px;
    color: var(--text-primary);
    font-size: 12px;
    font-family: inherit;
    transition: all 0.15s;
}

.api-row input:focus { outline: none; border-color: var(--accent-1); box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1); }
.api-row input::placeholder { color: var(--text-muted); opacity: 0.5; }

.api-row input:disabled {
    opacity: 0.5;
    cursor: pointer;
}

.btn-save {
    width: 100%;
    background: var(--gradient);
    color: white;
    border: none;
    padding: 10px;
    border-radius: var(--radius-md);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    cursor: pointer;
    transition: all 0.15s;
    margin-top: 8px;
    box-shadow: 0 2px 10px rgba(99, 102, 241, 0.2);
}

.btn-save:hover { box-shadow: 0 4px 16px rgba(99, 102, 241, 0.3); }

/* ===== Toast ===== */
.toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(18, 18, 28, 0.95);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 10px 18px;
    z-index: 200;
    animation: toastIn 0.25s ease;
    font-size: 12.5px;
    color: var(--text-primary);
    pointer-events: none;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    white-space: nowrap;
}

.toast.err { border-color: rgba(239, 68, 68, 0.3); color: #fca5a5; }
.toast.ok { border-color: rgba(34, 197, 94, 0.3); color: #86efac; }

@keyframes toastIn {
    from { opacity: 0; transform: translateX(-50%) translateY(8px); }
    to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .header { flex-wrap: wrap; padding: 8px 12px; gap: 8px; }
    .control-label { display: none; }
    .controls { order: 3; width: 100%; }
    select { min-width: 0; flex: 1; font-size: 11px; }
    .header-right { margin-left: auto; }
    .chat { padding: 16px 12px; }
    .msg { max-width: 92%; font-size: 13px; }
    .input-area { padding: 8px 12px 12px; }
    .modal-box { width: 100%; border-radius: var(--radius-lg); max-height: 95vh; }
    .api-row { flex-wrap: wrap; }
    .api-row label { width: 100%; margin-bottom: -2px; }
}

@media (max-width: 480px) {
    .btn span.label { display: none; }
}
