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

:root {
    --primary: #4F46E5;
    --primary-light: #818CF8;
    --primary-dark: #3730A3;
    --bg: #F8FAFC;
    --surface: #FFFFFF;
    --text: #1E293B;
    --text-light: #64748B;
    --border: #E2E8F0;
    --user-bubble: #4F46E5;
    --ai-bubble: #F1F5F9;
    --success: #10B981;
    --warning: #F59E0B;
}

html {
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    height: 100dvh; /* 모바일 주소창 제외한 실제 가시 영역 */
    overflow: hidden;
}

/* ===== Scenario Selection Screen ===== */
.screen {
    display: none;
    height: 100%;
}

.screen.active {
    display: flex;
    flex-direction: column;
}

#scenario-screen {
    align-items: center;
    padding: 40px 20px;
    overflow-y: auto;
}

#scenario-screen h1 {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 8px;
}

#scenario-screen .subtitle {
    color: var(--text-light);
    margin-bottom: 32px;
    font-size: 16px;
}

.scenario-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 16px;
    max-width: 800px;
    width: 100%;
}

.scenario-card {
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: 16px;
    padding: 24px;
    cursor: pointer;
    transition: all 0.2s;
}

.scenario-card:hover {
    border-color: var(--primary);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.15);
}

.scenario-card .icon {
    font-size: 36px;
    margin-bottom: 12px;
}

.scenario-card .title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 4px;
}

.scenario-card .desc {
    font-size: 14px;
    color: var(--text-light);
}

.scenario-card .difficulty {
    display: inline-block;
    margin-top: 8px;
    font-size: 12px;
    padding: 2px 8px;
    border-radius: 8px;
    background: #EEF2FF;
    color: var(--primary);
}

/* ===== Chat Screen ===== */
#chat-screen {
    flex-direction: column;
}

.chat-header {
    display: flex;
    align-items: center;
    padding: 16px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    gap: 12px;
    flex-shrink: 0;
}

.chat-header .back-btn {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 8px;
    color: var(--text);
}

.chat-header .back-btn:hover {
    background: var(--bg);
}

.chat-header .scenario-info {
    flex: 1;
}

.chat-header .scenario-info .name {
    font-weight: 600;
    font-size: 16px;
}

.chat-header .scenario-info .name-en {
    font-size: 13px;
    color: var(--text-light);
}

.chat-messages {
    flex: 1;
    min-height: 0; /* flex 자식이 부모 높이를 넘지 않도록 */
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.message {
    max-width: 80%;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.message.ai {
    align-self: flex-start;
}

.message.user {
    align-self: flex-end;
}

.message .bubble {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 15px;
    line-height: 1.5;
    position: relative;
}

.message.ai .bubble {
    background: var(--ai-bubble);
    color: var(--text);
    border-bottom-left-radius: 4px;
}

.message.user .bubble {
    background: var(--user-bubble);
    color: white;
    border-bottom-right-radius: 4px;
}

.message .actions {
    display: flex;
    gap: 4px;
    align-self: flex-start;
}

.message.user .actions {
    align-self: flex-end;
}

.message .actions button {
    background: none;
    border: none;
    font-size: 12px;
    color: var(--text-light);
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 4px;
}

.message .actions button:hover {
    background: var(--border);
    color: var(--text);
}

.ai-speaker-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 14px;
    margin-left: 4px;
    opacity: 0.6;
}

.ai-speaker-btn:hover {
    opacity: 1;
}

/* Feedback panel (correction / hint) */
.feedback-panel {
    background: #FFFBEB;
    border: 1px solid #FDE68A;
    border-radius: 12px;
    padding: 12px 16px;
    font-size: 14px;
    line-height: 1.6;
    white-space: pre-wrap;
    max-width: 80%;
    align-self: center;
}

.feedback-panel.hint {
    background: #EFF6FF;
    border-color: #BFDBFE;
}

.feedback-panel.suggestion {
    background: #F0FDF4;
    border-color: #BBF7D0;
}

/* ===== Input Area ===== */
.chat-input-area {
    padding: 16px 20px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom)); /* iOS 홈바 영역 확보 */
    background: var(--surface);
    border-top: 1px solid var(--border);
    display: flex;
    gap: 12px;
    align-items: center;
    flex-shrink: 0;
}

.text-input-wrapper {
    flex: 1;
    display: flex;
    align-items: center;
    background: var(--bg);
    border: 2px solid var(--border);
    border-radius: 24px;
    padding: 0 16px;
    transition: border-color 0.2s;
}

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

.text-input-wrapper input {
    flex: 1;
    border: none;
    background: none;
    padding: 12px 0;
    font-size: 15px;
    outline: none;
    color: var(--text);
}

.send-btn {
    background: var(--primary);
    color: white;
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
}

.send-btn:hover {
    background: var(--primary-dark);
}

.send-btn:disabled {
    background: var(--border);
    cursor: not-allowed;
}

.mic-btn {
    background: none;
    border: 2px solid var(--border);
    width: 48px;
    height: 48px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.mic-btn:hover {
    border-color: var(--primary);
}

.mic-btn.recording {
    border-color: #EF4444;
    background: #FEF2F2;
    animation: pulse 1.5s infinite;
}

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

.toolbar {
    display: flex;
    gap: 8px;
    padding: 0 20px 12px;
    background: var(--surface);
    flex-shrink: 0;
}

.toolbar button {
    background: var(--bg);
    border: 1px solid var(--border);
    padding: 6px 14px;
    border-radius: 16px;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
    color: var(--text);
}

.toolbar button:hover {
    background: var(--border);
}

/* Loading dots */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
    background: var(--ai-bubble);
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    align-self: flex-start;
    max-width: 80%;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: var(--text-light);
    border-radius: 50%;
    animation: bounce 1.4s 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); }
}

/* Status text */
.status-text {
    text-align: center;
    font-size: 13px;
    color: var(--text-light);
    padding: 4px;
    flex-shrink: 0;
}

/* Toast notification */
.toast {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%) translateY(-100px);
    background: #1E293B;
    color: white;
    padding: 14px 24px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.5;
    max-width: 440px;
    width: calc(100% - 40px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
    z-index: 1000;
    transition: transform 0.3s ease;
    text-align: center;
}

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

.toast.quota {
    background: #92400E;
}

.toast.error {
    background: #991B1B;
}

/* Responsive */
@media (max-width: 600px) {
    #scenario-screen {
        padding: 24px 16px;
    }

    .scenario-grid {
        grid-template-columns: 1fr;
    }

    .message {
        max-width: 90%;
    }

    #scenario-screen h1 {
        font-size: 22px;
    }

    .chat-header {
        padding: 12px 16px;
    }

    .chat-messages {
        padding: 12px 16px;
    }

    .chat-input-area {
        padding: 10px 12px;
        padding-bottom: calc(10px + env(safe-area-inset-bottom));
        gap: 8px;
    }

    .toolbar {
        padding: 0 16px 8px;
    }

    .text-input-wrapper input {
        font-size: 16px; /* iOS에서 포커스 시 자동 확대 방지 */
    }
}
