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

:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --primary-light: #818cf8;
    --accent: #8b5cf6;
    --bg: #0f0f13;
    --bg-card: #18181f;
    --bg-elevated: #1e1e28;
    --bg-input: #23232f;
    --text: #f1f1f4;
    --text-secondary: #9393a8;
    --text-muted: #5e5e73;
    --border: #2a2a38;
    --success: #34d399;
    --danger: #f87171;
    --warning: #fbbf24;
    --vh: 1vh;
}

html { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; overflow-x: hidden; width: 100%; }

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    line-height: 1.5;
    color: var(--text);
    overflow-x: hidden;
}

.hidden { display: none !important; }

.screen { width: 100%; max-width: 480px; }

/* ===== Buttons ===== */
.btn-primary {
    background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
    color: #fff;
    border: none;
    padding: 14px 32px;
    border-radius: 14px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.25s ease;
    box-shadow: 0 4px 20px rgba(99, 102, 241, 0.3);
    width: 100%;
    font-family: inherit;
}

.btn-primary:hover { transform: translateY(-2px); box-shadow: 0 8px 30px rgba(99, 102, 241, 0.4); }
.btn-primary:active { transform: translateY(0); }

.btn-secondary {
    background: var(--bg-elevated);
    color: var(--primary-light);
    border: 1px solid var(--border);
    padding: 12px 24px;
    border-radius: 14px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    width: 100%;
    font-family: inherit;
    margin-bottom: 12px;
}

.btn-secondary:hover { background: var(--bg-input); border-color: var(--primary); }

/* ===== Welcome Screen ===== */
.welcome-screen {
    background: var(--bg-card);
    border-radius: 24px;
    padding: 44px 36px;
    border: 1px solid var(--border);
    animation: fadeUp 0.6s ease-out;
    text-align: center;
}

.welcome-logo { margin-bottom: 20px; display: flex; justify-content: center; }
.welcome-logo svg { filter: drop-shadow(0 8px 24px rgba(99, 102, 241, 0.3)); }

.welcome-title { font-size: 32px; font-weight: 700; margin-bottom: 6px; letter-spacing: -0.5px; }
.welcome-subtitle { font-size: 16px; color: var(--primary-light); font-weight: 500; margin-bottom: 20px; }
.welcome-text { font-size: 14px; color: var(--text-secondary); line-height: 1.7; margin-bottom: 20px; text-align: left; }

.feature-list {
    list-style: none;
    text-align: left;
    margin-bottom: 28px;
}

.feature-list li {
    padding: 8px 0;
    font-size: 14px;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 10px;
}

.feature-list li::before {
    content: '✓';
    color: var(--success);
    font-weight: 700;
    font-size: 16px;
}

/* ===== Level Select Screen ===== */
.level-screen {
    background: var(--bg-card);
    border-radius: 24px;
    padding: 40px 32px;
    border: 1px solid var(--border);
    animation: fadeUp 0.5s ease-out;
    text-align: center;
}

.level-logo { margin-bottom: 20px; display: flex; justify-content: center; }
.level-title { font-size: 20px; font-weight: 600; margin-bottom: 24px; color: var(--text); }

.level-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.level-card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 20px 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    font-family: inherit;
    color: var(--text);
}

.level-card:hover { border-color: var(--primary); background: var(--bg-input); transform: translateY(-2px); }
.level-emoji { font-size: 28px; }
.level-name { font-size: 15px; font-weight: 600; }
.level-desc { font-size: 12px; color: var(--text-muted); }

/* ===== Instructions Screen ===== */
.instructions-screen {
    background: var(--bg-card);
    border-radius: 24px;
    padding: 40px 32px;
    border: 1px solid var(--border);
    animation: fadeUp 0.5s ease-out;
    text-align: center;
}

.instructions-icon { font-size: 48px; margin-bottom: 16px; }
.instructions-title { font-size: 20px; font-weight: 600; margin-bottom: 16px; line-height: 1.4; }
.instructions-text { font-size: 14px; color: var(--text-secondary); line-height: 1.7; margin-bottom: 16px; }

/* ===== How To Use Popup ===== */
.popup-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    padding: 20px;
    animation: fadeIn 0.2s ease;
}

.popup-content {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 32px 28px;
    max-width: 420px;
    width: 100%;
    position: relative;
    animation: fadeUp 0.3s ease-out;
}

.popup-content h3 { font-size: 20px; font-weight: 700; margin-bottom: 24px; text-align: center; }

.popup-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 24px;
    cursor: pointer;
    line-height: 1;
    transition: color 0.2s;
}

.popup-close:hover { color: var(--text); }

.popup-steps { display: flex; flex-direction: column; gap: 18px; margin-bottom: 24px; }

.popup-step {
    display: flex;
    gap: 14px;
    align-items: flex-start;
    text-align: left;
}

.step-num {
    width: 32px;
    height: 32px;
    min-width: 32px;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    color: #fff;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
}

.popup-step strong { display: block; font-size: 14px; margin-bottom: 2px; }
.popup-step p { font-size: 13px; color: var(--text-secondary); line-height: 1.5; }

.popup-btn { margin-top: 0; }

/* ===== Chat Container ===== */
.chat-container {
    max-width: 900px;
    height: 95vh;
    background: var(--bg-card);
    border-radius: 24px;
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: fadeIn 0.4s ease-out;
}

/* ===== Chat Header ===== */
.chat-header {
    background: var(--bg-elevated);
    padding: 14px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.friend-info { display: flex; align-items: center; gap: 12px; }
.header-logo { width: 36px; height: 36px; flex-shrink: 0; }
.header-logo svg { border-radius: 10px; }
.friend-name { font-size: 16px; font-weight: 600; margin-bottom: 1px; }
.status-indicator { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-muted); }
.status-dot { width: 7px; height: 7px; background: var(--success); border-radius: 50%; box-shadow: 0 0 8px rgba(52, 211, 153, 0.5); }

.header-level {
    font-size: 12px;
    color: var(--primary-light);
    background: rgba(99, 102, 241, 0.12);
    padding: 4px 12px;
    border-radius: 20px;
    font-weight: 600;
}

/* ===== Messages Area ===== */
.messages-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    padding-bottom: 28px;
    background: var(--bg);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.messages-area::-webkit-scrollbar { width: 6px; }
.messages-area::-webkit-scrollbar-track { background: transparent; }
.messages-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ===== Messages ===== */
.message {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    animation: fadeUp 0.25s ease-out;
    margin-bottom: 4px;
}

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

.message-content { max-width: 78%; display: flex; flex-direction: column; }
.message.user .message-content { align-items: flex-end; }

.message-logo { width: 30px; height: 30px; flex-shrink: 0; }
.message-logo svg { border-radius: 8px; }

.message-bubble {
    padding: 12px 16px;
    border-radius: 18px;
    word-wrap: break-word;
    overflow-wrap: break-word;
    line-height: 1.6;
    font-size: 14px;
    max-width: 100%;
    min-width: 40px;
}

.message.friend .message-bubble {
    background: var(--bg-elevated);
    color: var(--text);
    border-bottom-left-radius: 6px;
    border: 1px solid var(--border);
}

.message.user .message-bubble {
    background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
    color: #fff;
    border-bottom-right-radius: 6px;
}

.message-time { font-size: 11px; color: var(--text-muted); margin-top: 4px; }
.message.user .message-time { text-align: right; }

/* ===== Voice Message Bubble ===== */
.voice-bubble {
    padding: 10px 14px;
    border-radius: 18px;
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 200px;
    max-width: 300px;
}

.message.friend .voice-bubble {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-bottom-left-radius: 6px;
}

.message.user .voice-bubble {
    background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
    border-bottom-right-radius: 6px;
}

.voice-play-btn {
    width: 36px;
    height: 36px;
    min-width: 36px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.message.friend .voice-play-btn { background: var(--primary); color: #fff; }
.message.user .voice-play-btn { background: rgba(255,255,255,0.25); color: #fff; }
.voice-play-btn:hover { transform: scale(1.08); }

.voice-wave {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.voice-bars {
    display: flex;
    align-items: center;
    gap: 2px;
    height: 24px;
}

.voice-bar {
    width: 3px;
    border-radius: 2px;
    transition: height 0.1s ease;
}

.message.friend .voice-bar { background: var(--text-muted); }
.message.user .voice-bar { background: rgba(255,255,255,0.5); }
.voice-bar.active { background: var(--primary-light) !important; }
.message.user .voice-bar.active { background: #fff !important; }

.voice-duration {
    font-size: 11px;
}

.message.friend .voice-duration { color: var(--text-muted); }
.message.user .voice-duration { color: rgba(255,255,255,0.7); }

/* ===== Correction Message ===== */
.correction-bubble {
    padding: 14px 16px;
    border-radius: 18px;
    border-bottom-left-radius: 6px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    max-width: 100%;
    font-size: 14px;
    line-height: 1.7;
}

.correction-original {
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.correction-fixed {
    color: var(--success);
    margin-bottom: 12px;
    font-weight: 500;
}

.correction-list {
    border-top: 1px solid var(--border);
    padding-top: 10px;
    color: var(--text-secondary);
    font-size: 13px;
}

.correction-item {
    margin-bottom: 6px;
}

.correction-item .wrong { color: var(--danger); font-weight: 500; }
.correction-item .right { color: var(--success); font-weight: 500; }
.correction-item .arrow { color: var(--text-muted); margin: 0 4px; }

/* ===== Typing Indicator ===== */
.typing-indicator { display: flex; align-items: center; gap: 10px; }
.typing-logo { width: 28px; height: 28px; flex-shrink: 0; }
.typing-logo svg { border-radius: 7px; }

.typing-dots {
    background: var(--bg-elevated);
    padding: 12px 16px;
    border-radius: 18px;
    border-bottom-left-radius: 6px;
    display: flex;
    gap: 5px;
    align-items: center;
    border: 1px solid var(--border);
}

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

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

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

/* ===== Stop Button ===== */
.stop-generation-container {
    position: absolute;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
}

.stop-generation-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 18px;
    background: var(--bg-elevated);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 20px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
    font-family: inherit;
}

.stop-generation-btn:hover { background: var(--bg-input); border-color: var(--text-muted); }

/* ===== Input Panel ===== */
.input-panel {
    padding: 12px;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.input-container {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-input);
    border-radius: 16px;
    padding: 6px 6px 6px 16px;
    border: 1px solid var(--border);
    transition: border-color 0.2s ease;
}

.input-container:focus-within { border-color: var(--primary); }

.message-input {
    flex: 1;
    border: none;
    background: transparent;
    outline: none;
    font-size: 14px;
    padding: 10px 4px;
    color: var(--text);
    min-width: 0;
    font-family: inherit;
}

.message-input::placeholder { color: var(--text-muted); }

.voice-btn, .send-btn {
    width: 40px;
    height: 40px;
    min-width: 40px;
    border-radius: 12px;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
    font-family: inherit;
}

.voice-btn {
    background: transparent;
    color: var(--text-muted);
}

.voice-btn:hover { background: rgba(99, 102, 241, 0.1); color: var(--primary-light); }

.send-btn {
    background: linear-gradient(135deg, var(--primary) 0%, var(--accent) 100%);
    color: #fff;
}

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

.voice-btn.recording {
    background: var(--danger);
    color: white;
    animation: pulse 1s infinite;
    box-shadow: 0 0 16px rgba(248, 113, 113, 0.4);
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.08); }
    100% { transform: scale(1); }
}

/* ===== Streaming ===== */
.message.streaming .message-bubble::after {
    content: '▋';
    animation: blink 1s infinite;
    color: var(--primary-light);
    margin-left: 2px;
}

@keyframes blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}

/* ===== Markdown ===== */
.markdown-content { line-height: 1.65; }
.markdown-content p { margin-bottom: 0.6em; }
.markdown-content ul, .markdown-content ol { margin-left: 1.5em; margin-bottom: 0.6em; }
.markdown-content li { margin-bottom: 0.2em; }

.markdown-content code {
    background: rgba(99, 102, 241, 0.12);
    padding: 2px 6px;
    border-radius: 5px;
    font-family: 'SF Mono', monospace;
    font-size: 0.88em;
    color: var(--primary-light);
}

.markdown-content pre {
    background: rgba(0, 0, 0, 0.3);
    padding: 12px;
    border-radius: 10px;
    overflow-x: auto;
    margin-bottom: 0.6em;
    border: 1px solid var(--border);
}

.markdown-content pre code { background: transparent; padding: 0; color: var(--text); }

/* ===== Animations ===== */
@keyframes fadeUp {
    from { opacity: 0; transform: translateY(16px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* ===== Mobile ===== */
@media (max-width: 768px) {
    body { padding: 0; overflow: hidden; position: fixed; width: 100%; height: 100%; }

    .welcome-screen, .level-screen, .instructions-screen {
        border-radius: 0;
        border: none;
        height: 100vh;
        height: calc(var(--vh, 1vh) * 100);
        display: flex;
        align-items: center;
        justify-content: center;
        position: fixed;
        inset: 0;
        max-width: 100vw;
        overflow-y: auto;
    }

    .chat-container {
        height: 100vh;
        height: calc(var(--vh, 1vh) * 100);
        border-radius: 0;
        border: none;
        position: fixed;
        inset: 0;
        max-width: 100%;
    }

    .chat-header { position: fixed; top: 0; left: 0; right: 0; z-index: 100; }
    .messages-area { position: fixed; top: 66px; bottom: 72px; left: 0; right: 0; -webkit-overflow-scrolling: touch; padding: 14px; }
    .input-panel { position: fixed; bottom: 0; left: 0; right: 0; z-index: 100; padding: 8px; padding-bottom: env(safe-area-inset-bottom, 8px); }
    .message-content { max-width: 85%; }
    .message-input { font-size: 16px; }
    .stop-generation-container { bottom: 82px; width: calc(100% - 32px); max-width: 260px; z-index: 101; }
    .stop-generation-btn { width: 100%; justify-content: center; }
}

@supports (-webkit-touch-callout: none) {
    .chat-container { height: -webkit-fill-available; }
    .messages-area { -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
    input { -webkit-appearance: none; appearance: none; }
}

@media (max-width: 375px) {
    .welcome-screen, .level-screen { padding: 24px 20px; }
    .level-grid { gap: 8px; }
    .level-card { padding: 14px 8px; }
}

/* ===== Onboarding Card in Chat ===== */
.onboarding-card {
    margin-bottom: 12px;
    animation: fadeUp 0.4s ease-out;
}

.onboarding-inner {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 20px;
    text-align: center;
}

.onboarding-icon { font-size: 36px; margin-bottom: 10px; }
.onboarding-title { font-size: 16px; font-weight: 600; margin-bottom: 8px; color: var(--text); }
.onboarding-text { font-size: 13px; color: var(--text-secondary); line-height: 1.6; margin-bottom: 14px; }
.onboarding-btn { width: auto; padding: 8px 20px; font-size: 13px; margin-bottom: 0; }

/* ===== Correct (no errors) bubble ===== */
.correct-bubble {
    border-color: rgba(52, 211, 153, 0.3) !important;
}

.correction-correct {
    color: var(--success);
    font-weight: 600;
    font-size: 15px;
    margin-top: 4px;
}
