/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
    --bg-darkest: #0d0f13;
    --bg-dark: #13161c;
    --bg-card: #1a1e27;
    --bg-input: #1e2230;
    --bg-hover: #252a36;
    --border: #2a2f3d;
    --border-light: #353b4d;
    --text: #e2e4e9;
    --text-muted: #8b8fa3;
    --text-dim: #5a5f73;
    --accent: #a78bfa;
    --accent-dim: #7c5cbf;
    --accent-glow: rgba(167, 139, 250, 0.15);
    --success: #4ade80;
    --warning: #fbbf24;
    --danger: #f87171;
    --radius: 8px;
    --radius-lg: 12px;
    --sidebar-w: 280px;
    --topbar-h: 56px;
    --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

html, body {
    height: 100%;
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg-darkest);
    -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.accent { color: var(--accent); }
.muted { color: var(--text-muted); font-size: 13px; }

/* === Buttons === */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 20px;
    border: none;
    border-radius: var(--radius);
    font-family: var(--font);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-dim); }
.btn-secondary { background: var(--bg-hover); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--border); }
.btn-ghost { background: none; color: var(--text-muted); }
.btn-ghost:hover { color: var(--text); }
.btn-small { padding: 6px 12px; font-size: 12px; }
.btn-icon { background: none; border: none; color: var(--text-muted); font-size: 20px; cursor: pointer; padding: 4px 8px; }
.btn-icon:hover { color: var(--text); }

/* === Screens === */
.screen { width: 100%; height: 100%; }

/* === Auth Screen === */
#auth-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(ellipse at 50% 30%, rgba(167,139,250,0.08) 0%, transparent 60%),
                var(--bg-darkest);
}

.auth-container {
    width: 100%;
    max-width: 380px;
    padding: 24px;
}

.auth-logo {
    text-align: center;
    margin-bottom: 32px;
}

.auth-avatar {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid var(--border-light);
    margin-bottom: 16px;
}

.auth-logo h1 {
    font-size: 28px;
    font-weight: 700;
    letter-spacing: -0.5px;
}

.auth-tagline {
    color: var(--text-muted);
    margin-top: 4px;
    font-size: 14px;
}

.auth-form { display: flex; flex-direction: column; gap: 16px; }

.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-group label { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.form-group input {
    padding: 10px 12px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    font-size: 14px;
    font-family: var(--font);
    outline: none;
    transition: border-color 0.15s;
}
.form-group input:focus { border-color: var(--accent); }
.form-group input::placeholder { color: var(--text-dim); }

.auth-error {
    padding: 10px 12px;
    background: rgba(248, 113, 113, 0.1);
    border: 1px solid rgba(248, 113, 113, 0.3);
    border-radius: var(--radius);
    color: var(--danger);
    font-size: 13px;
}

.auth-toggle { text-align: center; color: var(--text-muted); font-size: 13px; }

/* === Top Bar === */
.topbar {
    height: var(--topbar-h);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    background: var(--bg-dark);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 12px; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.topbar-title { font-size: 18px; font-weight: 700; letter-spacing: -0.3px; }

.usage-badge {
    padding: 4px 10px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 99px;
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 500;
}

/* === App Layout === */
#app-screen { display: flex; flex-direction: column; }
.app-body { display: flex; flex: 1; overflow: hidden; }

/* === Sidebar === */
.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: var(--bg-dark);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 0;
    overflow-y: auto;
    padding: 16px;
}

.coach-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 16px 0;
    border-bottom: 1px solid var(--border);
    margin-bottom: 16px;
}

.coach-avatar-wrap {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    overflow: hidden;
    border: 2px solid var(--accent-dim);
    box-shadow: 0 0 20px var(--accent-glow);
    flex-shrink: 0;
}

.coach-avatar {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.coach-info { text-align: center; }
.coach-name { display: block; font-weight: 600; font-size: 15px; }
.coach-title { display: block; color: var(--text-muted); font-size: 12px; font-style: italic; }

.sidebar-section { padding: 12px 0; border-bottom: 1px solid var(--border); }
.sidebar-section:last-child { border-bottom: none; }
.sidebar-section h3 {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-dim);
    margin-bottom: 8px;
}

.collection-status { margin-bottom: 8px; }
.collection-status .stat { display: flex; justify-content: space-between; font-size: 13px; padding: 2px 0; }
.collection-status .stat-value { color: var(--accent); font-weight: 500; }

.upload-btn { width: 100%; cursor: pointer; text-align: center; }

.account-avatar-row { display: flex; align-items: center; gap: 10px; }
.account-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--bg-hover);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    overflow: hidden;
    font-size: 14px;
    font-weight: 600;
    color: var(--text-muted);
}
.account-avatar img { width: 100%; height: 100%; object-fit: cover; }

/* === Chat Area === */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    background: var(--bg-darkest);
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* Message bubbles */
.msg {
    display: flex;
    gap: 12px;
    max-width: 85%;
    animation: fadeIn 0.2s ease;
}
.msg-user { align-self: flex-end; flex-direction: row-reverse; }
.msg-assistant { align-self: flex-start; }

.msg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    flex-shrink: 0;
    object-fit: cover;
}
.msg-user .msg-avatar { display: none; }

.msg-bubble {
    padding: 10px 14px;
    border-radius: var(--radius-lg);
    font-size: 14px;
    line-height: 1.6;
    word-break: break-word;
}

.msg-user .msg-bubble {
    background: var(--accent-dim);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.msg-assistant .msg-bubble {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

/* Deck block inside messages */
.deck-block {
    margin: 8px 0;
    padding: 12px;
    background: var(--bg-dark);
    border: 1px solid var(--border-light);
    border-radius: var(--radius);
    font-family: var(--font-mono);
    font-size: 12px;
    line-height: 1.7;
    position: relative;
}

.deck-block .deck-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
}

.deck-block .deck-name {
    font-weight: 700;
    color: var(--accent);
    font-family: var(--font);
    font-size: 14px;
}

.deck-block .deck-class {
    color: var(--text-muted);
    font-family: var(--font);
    font-size: 12px;
}

.deck-block .deck-cards {
    display: flex;
    flex-direction: column;
    gap: 1px;
}

.deck-card-line {
    font-family: var(--font-mono);
    font-size: 12px;
    line-height: 1.6;
    padding: 1px 0;
    color: var(--text-muted);
}

.deck-card-name {
    color: #fff;
    transition: color 0.2s;
}

.deck-code-area {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid var(--border);
}

.deck-code-row {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.deck-code-text {
    flex: 1;
    font-size: 11px;
    font-family: var(--font-mono);
    color: var(--text-dim);
    word-break: break-all;
    min-width: 0;
}

.btn-copy-code.copied { background: var(--success); border-color: var(--success); }

.deck-code-loading { font-size: 11px; }

.deck-missing {
    display: block;
    font-size: 11px;
    margin-top: 4px;
}

/* Greeting message */
.msg-greeting .msg-bubble {
    background: linear-gradient(135deg, var(--bg-card), rgba(167,139,250,0.08));
    border-color: var(--accent-dim);
}

/* Typing indicator */
.typing-indicator span {
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-muted);
    margin: 0 2px;
    animation: bounce 1.2s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-6px); }
}

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

/* === Chat Input === */
.chat-input-wrap {
    padding: 12px 24px 20px;
    background: var(--bg-darkest);
    border-top: 1px solid var(--border);
}

.chat-form {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    max-width: 800px;
    margin: 0 auto;
}

.chat-input {
    flex: 1;
    padding: 10px 14px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    color: var(--text);
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    resize: none;
    outline: none;
    max-height: 150px;
    transition: border-color 0.15s;
}
.chat-input:focus { border-color: var(--accent); }
.chat-input::placeholder { color: var(--text-dim); }

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

/* === Scrollbar === */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--border-light); }

/* === Rate limit overlay === */
.rate-limit-msg {
    text-align: center;
    padding: 16px;
    background: rgba(251, 191, 36, 0.08);
    border: 1px solid rgba(251, 191, 36, 0.2);
    border-radius: var(--radius);
    color: var(--warning);
    font-size: 13px;
    margin: 8px 24px;
}

/* === Mobile === */
@media (max-width: 768px) {
    :root { --sidebar-w: 100%; }

    .sidebar {
        position: fixed;
        top: var(--topbar-h);
        left: 0;
        bottom: 0;
        z-index: 100;
        width: 280px;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        border-right: 1px solid var(--border);
    }
    .sidebar.open { transform: translateX(0); }

    .sidebar-backdrop {
        position: fixed;
        inset: 0;
        top: var(--topbar-h);
        background: rgba(0,0,0,0.5);
        z-index: 99;
        display: none;
    }
    .sidebar-backdrop.open { display: block; }

    .msg { max-width: 95%; }

    .chat-input-wrap { padding: 8px 12px 12px; }
}

@media (min-width: 769px) {
    #sidebar-toggle { display: none; }
}
