/* ================================================
   Elderly English App - Global Styles
   Mobile-First | Dark Theme | High Contrast
   ================================================ */

/* --- Google Font --- */
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;500;700&family=Inter:wght@400;500;600;700&display=swap');

/* --- CSS Variables --- */
:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-card: #1f2b47;
    --bg-input: #253352;
    --text-primary: #eaeaea;
    --text-secondary: #a8b2c1;
    --text-muted: #6b7a90;
    --accent: #4ecca3;
    --accent-hover: #3db88f;
    --accent-glow: rgba(78, 204, 163, 0.25);
    --danger: #e74c5e;
    --warning: #f0a500;
    --success: #4ecca3;
    --bubble-user: #2d5a45;
    --bubble-ai: #2a3a5c;
    --border: rgba(255, 255, 255, 0.08);
    --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 20px;
    --radius-full: 50%;
    --tab-height: 64px;
    --header-height: 56px;
    --font-body: 'Noto Sans TC', 'Inter', -apple-system, sans-serif;
    --font-size-sm: 16px;
    --font-size-base: 18px;
    --font-size-md: 20px;
    --font-size-lg: 22px;
    --font-size-xl: 28px;
    --font-size-xxl: 32px;
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* --- Reset & Base --- */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

html {
    font-size: 100%;
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
}

body {
    font-family: var(--font-body);
    font-size: var(--font-size-base);
    color: var(--text-primary);
    background: var(--bg-primary);
    line-height: 1.6;
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
    /* 禁用雙擊縮放 */
    touch-action: manipulation;
    /* 禁用長按選取 */
    -webkit-user-select: none;
    user-select: none;
}

/* 允許輸入框選取文字 */
input, textarea {
    -webkit-user-select: text;
    user-select: text;
}

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

img { max-width: 100%; display: block; }

/* --- App Container --- */
#app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
    position: relative;
}

/* --- Header --- */
.app-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-height);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 16px;
    z-index: 100;
    backdrop-filter: blur(10px);
}

.app-header h1 {
    font-size: var(--font-size-md);
    font-weight: 600;
    color: var(--text-primary);
}

.header-back {
    position: absolute;
    left: 12px;
    background: none;
    border: none;
    color: var(--accent);
    font-size: var(--font-size-lg);
    cursor: pointer;
    padding: 8px;
    display: flex;
    align-items: center;
    gap: 4px;
}

.header-action {
    position: absolute;
    right: 12px;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    cursor: pointer;
    padding: 8px;
}

/* --- Main Content Area --- */
.page-content {
    flex: 1;
    padding-top: var(--header-height);
    padding-bottom: calc(var(--tab-height) + 16px);
    overflow-y: auto;
}

/* --- Tab Bar (Bottom Navigation) --- */
.tab-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--tab-height);
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-around;
    z-index: 100;
    padding-bottom: env(safe-area-inset-bottom);
    backdrop-filter: blur(10px);
}

.tab-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    height: 100%;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    transition: var(--transition);
    gap: 2px;
    position: relative;
}

.tab-item.active {
    color: var(--accent);
}

.tab-item .tab-icon {
    font-size: 24px;
    line-height: 1;
}

.tab-item .tab-label {
    font-size: 11px;
    font-weight: 500;
}

/* 複習徽章 */
.tab-badge {
    position: absolute;
    top: 4px;
    right: 50%;
    transform: translateX(calc(50% + 12px));
    background: var(--danger);
    color: white;
    font-size: 10px;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
}

/* --- Pages --- */
.page {
    display: none;
    padding: 16px;
    animation: fadeIn 0.3s ease;
}

.page.active {
    display: block;
}

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

/* --- Home Page --- */
.home-greeting {
    padding: 24px 0 16px;
}

.home-greeting h2 {
    font-size: var(--font-size-xl);
    font-weight: 700;
    margin-bottom: 4px;
}

.home-greeting p {
    color: var(--text-secondary);
    font-size: var(--font-size-base);
}

/* Review Alert Card */
.review-alert {
    background: linear-gradient(135deg, #2d5a45, #1f4035);
    border: 1px solid rgba(78, 204, 163, 0.3);
    border-radius: var(--radius-md);
    padding: 16px 20px;
    margin-bottom: 24px;
    display: flex;
    align-items: center;
    gap: 16px;
    cursor: pointer;
    transition: var(--transition);
}

.review-alert:hover, .review-alert:active {
    transform: scale(1.02);
    border-color: var(--accent);
}

.review-alert .review-icon {
    font-size: 36px;
    flex-shrink: 0;
}

.review-alert .review-info h3 {
    font-size: var(--font-size-md);
    color: var(--accent);
    margin-bottom: 2px;
}

.review-alert .review-info p {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
}

.review-alert.hidden {
    display: none;
}

/* Section */
.section-title {
    font-size: var(--font-size-md);
    font-weight: 600;
    margin-bottom: 12px;
    color: var(--text-primary);
}

/* --- Cards Grid --- */
.cards-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 24px;
}

.feature-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 20px 16px;
    text-align: center;
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    min-height: 120px;
    justify-content: center;
}

.feature-card:hover, .feature-card:active {
    border-color: var(--accent);
    background: var(--bg-input);
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.feature-card.locked {
    opacity: 0.4;
    pointer-events: none;
}

.feature-card .card-icon {
    font-size: 36px;
    line-height: 1;
}

.feature-card .card-title {
    font-size: var(--font-size-sm);
    font-weight: 600;
    color: var(--text-primary);
}

.feature-card .card-subtitle {
    font-size: 13px;
    color: var(--text-secondary);
}

.feature-card .card-badge {
    font-size: 11px;
    color: var(--accent);
    background: var(--accent-glow);
    padding: 2px 8px;
    border-radius: 4px;
}

/* --- Lesson List --- */
.lesson-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 24px;
}

.lesson-item {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    cursor: pointer;
    transition: var(--transition);
}

.lesson-item:hover, .lesson-item:active {
    border-color: var(--accent);
    background: var(--bg-input);
}

.lesson-item.locked {
    opacity: 0.4;
    pointer-events: none;
}

.lesson-item.completed {
    border-color: rgba(78, 204, 163, 0.3);
}

.lesson-item .lesson-icon {
    font-size: 32px;
    flex-shrink: 0;
    width: 48px;
    text-align: center;
}

.lesson-item .lesson-info {
    flex: 1;
    min-width: 0;
}

.lesson-item .lesson-info h3 {
    font-size: var(--font-size-base);
    font-weight: 600;
    margin-bottom: 2px;
}

.lesson-item .lesson-info p {
    font-size: 14px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.lesson-item .lesson-status {
    flex-shrink: 0;
    font-size: 20px;
}

/* --- Chat Interface --- */
.chat-page {
    display: none;
    flex-direction: column;
    position: fixed;
    inset: 0;
    z-index: 200;
    background: var(--bg-primary);
    padding: 0;
}

.chat-page.active {
    display: flex;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    padding-top: calc(var(--header-height) + 8px);
    padding-bottom: 80px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.message {
    max-width: 85%;
    padding: 14px 18px;
    border-radius: var(--radius-lg);
    font-size: var(--font-size-base);
    line-height: 1.7;
    word-wrap: break-word;
    animation: msgIn 0.3s ease;
}

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

.message.user {
    align-self: flex-end;
    background: var(--bubble-user);
    color: var(--text-primary);
    border-bottom-right-radius: 4px;
}

.message.assistant {
    align-self: flex-start;
    background: var(--bubble-ai);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

.message.system {
    align-self: center;
    background: var(--bg-input);
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    text-align: center;
    border-radius: var(--radius-md);
    max-width: 90%;
}

.message .replay-btn {
    display: inline-block;
    margin-top: 8px;
    background: none;
    border: 1px solid var(--text-muted);
    color: var(--text-secondary);
    font-size: 13px;
    padding: 4px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
}

.message .replay-btn:active {
    background: var(--bg-input);
    color: var(--accent);
    border-color: var(--accent);
}

/* 載入動畫 */
.message.loading {
    align-self: flex-start;
    background: var(--bubble-ai);
    padding: 18px 24px;
}

.typing-dots {
    display: flex;
    gap: 6px;
}

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

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

@keyframes typingBounce {
    0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
    40% { transform: translateY(-8px); opacity: 1; }
}

/* Chat Input Bar */
.chat-input-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    display: flex;
    align-items: center;
    gap: 8px;
    z-index: 100;
}

.chat-input-bar input {
    flex: 1;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 24px;
    padding: 12px 18px;
    color: var(--text-primary);
    font-size: var(--font-size-base);
    font-family: var(--font-body);
    outline: none;
    transition: var(--transition);
}

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

.chat-input-bar input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

/* Buttons */
.btn-icon {
    width: 48px;
    height: 48px;
    min-width: 48px;
    border-radius: var(--radius-full);
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition);
    font-size: 22px;
    flex-shrink: 0;
}

.btn-mic {
    background: var(--bg-input);
    color: var(--text-secondary);
    border: 1px solid var(--border);
}

.btn-mic.recording {
    background: var(--danger);
    color: white;
    border-color: var(--danger);
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(231, 76, 94, 0.4); }
    50% { box-shadow: 0 0 0 12px rgba(231, 76, 94, 0); }
}

.btn-send {
    background: var(--accent);
    color: var(--bg-primary);
}

.btn-send:active {
    background: var(--accent-hover);
    transform: scale(0.95);
}

/* Help Button (floating) */
.btn-help-float {
    position: fixed;
    bottom: 80px;
    right: 16px;
    width: 56px;
    height: 56px;
    border-radius: var(--radius-full);
    background: linear-gradient(135deg, var(--warning), #e8960a);
    color: white;
    border: none;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(240, 165, 0, 0.4);
    z-index: 90;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--font-body);
}

.btn-help-float:active {
    transform: scale(0.9);
}

/* Help Panel */
.help-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 60vh;
    background: var(--bg-secondary);
    border-top: 2px solid var(--accent);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    padding: 20px;
    z-index: 200;
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    overflow-y: auto;
    transform: translateY(100%);
}

.help-panel.visible {
    transform: translateY(0);
}

.help-panel h3 {
    font-size: var(--font-size-md);
    color: var(--accent);
    margin-bottom: 12px;
}

.help-panel .help-content {
    font-size: var(--font-size-base);
    color: var(--text-primary);
    line-height: 1.8;
    white-space: pre-wrap;
}

.help-panel .close-help {
    position: absolute;
    top: 16px;
    right: 16px;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 24px;
    cursor: pointer;
}

.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 150;
    display: none;
}

.overlay.visible {
    display: block;
}

/* --- Buttons (General) --- */
.btn-primary {
    background: var(--accent);
    color: var(--bg-primary);
    border: none;
    border-radius: var(--radius-md);
    padding: 14px 28px;
    font-size: var(--font-size-base);
    font-weight: 600;
    font-family: var(--font-body);
    cursor: pointer;
    transition: var(--transition);
    min-height: 56px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
}

.btn-primary:active {
    background: var(--accent-hover);
    transform: scale(0.98);
}

.btn-secondary {
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 14px 28px;
    font-size: var(--font-size-base);
    font-weight: 500;
    font-family: var(--font-body);
    cursor: pointer;
    transition: var(--transition);
    min-height: 56px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
}

.btn-secondary:active {
    background: var(--bg-card);
}

/* --- Review Page --- */
.review-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 32px 24px;
    text-align: center;
    margin-bottom: 20px;
}

.review-card .review-phrase {
    font-size: var(--font-size-xl);
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 12px;
}

.review-card .review-translation {
    font-size: var(--font-size-md);
    color: var(--text-secondary);
    display: none;
}

.review-card .review-translation.revealed {
    display: block;
    animation: fadeIn 0.3s ease;
}

.review-card .review-context {
    font-size: 14px;
    color: var(--text-muted);
    margin-top: 8px;
}

.review-actions {
    display: flex;
    gap: 12px;
}

.review-actions button {
    flex: 1;
}

/* Review progress */
.review-progress {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 16px;
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
}

.review-progress .progress-bar {
    flex: 1;
    height: 6px;
    background: var(--bg-input);
    border-radius: 3px;
    overflow: hidden;
}

.review-progress .progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 3px;
    transition: width 0.5s ease;
}

/* --- Profile / Settings Page --- */
.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
    text-align: center;
}

.stat-card .stat-value {
    font-size: var(--font-size-xl);
    font-weight: 700;
    color: var(--accent);
}

.stat-card .stat-label {
    font-size: 14px;
    color: var(--text-secondary);
    margin-top: 4px;
}

/* Speed Toggle */
.speed-toggle {
    display: flex;
    background: var(--bg-input);
    border-radius: var(--radius-md);
    padding: 4px;
    margin-bottom: 24px;
}

.speed-option {
    flex: 1;
    padding: 12px;
    text-align: center;
    border-radius: var(--radius-sm);
    border: none;
    background: none;
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    font-family: var(--font-body);
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
}

.speed-option.active {
    background: var(--accent);
    color: var(--bg-primary);
    font-weight: 600;
}

/* --- Empty State --- */
.empty-state {
    text-align: center;
    padding: 48px 24px;
    color: var(--text-muted);
}

.empty-state .empty-icon {
    font-size: 48px;
    margin-bottom: 16px;
}

.empty-state p {
    font-size: var(--font-size-base);
}

/* --- Login Page --- */
.login-page {
    position: fixed;
    inset: 0;
    z-index: 500;
    background: linear-gradient(160deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}

.login-page.hidden {
    display: none;
}

.login-container {
    width: 100%;
    max-width: 380px;
    text-align: center;
}

.login-logo {
    font-size: 48px;
    font-weight: 700;
    color: var(--accent);
    margin-bottom: 8px;
    letter-spacing: 2px;
}

.login-title {
    font-size: var(--font-size-xl);
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.login-subtitle {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
    margin-bottom: 36px;
}

.login-form {
    text-align: left;
}

.input-group {
    margin-bottom: 16px;
}

.input-group label {
    display: block;
    font-size: var(--font-size-sm);
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.input-group input {
    width: 100%;
    padding: 14px 16px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: var(--font-size-base);
    font-family: var(--font-body);
    outline: none;
    transition: var(--transition);
}

.input-group input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

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

.login-toggle {
    text-align: center;
    margin-top: 16px;
    font-size: 14px;
    color: var(--text-muted);
}

.login-toggle a {
    color: var(--accent);
    font-weight: 500;
}

.login-error {
    text-align: center;
    margin-top: 12px;
    font-size: 14px;
    color: var(--danger);
}

/* --- User Info Card --- */
.user-info-card {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 20px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    margin-bottom: 24px;
}

.user-avatar {
    width: 56px;
    height: 56px;
    border-radius: var(--radius-full);
    background: var(--bg-input);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    flex-shrink: 0;
}

.user-details h2 {
    font-size: var(--font-size-lg);
    font-weight: 600;
    margin-bottom: 2px;
}

.user-details p {
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
}

/* --- Tablet Breakpoint (768px+) --- */
@media (min-width: 768px) {
    .cards-grid {
        grid-template-columns: 1fr 1fr 1fr;
    }

    .message {
        max-width: 70%;
    }

    .feature-card {
        padding: 24px 20px;
        min-height: 140px;
    }
}

/* --- Desktop Breakpoint (1024px+) --- */
@media (min-width: 1024px) {
    #app {
        border-left: 1px solid var(--border);
        border-right: 1px solid var(--border);
    }
}
