:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --primary-light: #dbeafe;
    --success: #16a34a;
    --success-light: #dcfce7;
    --danger: #dc2626;
    --danger-light: #fee2e2;
    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-400: #9ca3af;
    --gray-500: #6b7280;
    --gray-600: #4b5563;
    --gray-700: #374151;
    --gray-800: #1f2937;
    --gray-900: #111827;
    --bg: #f3f4f6;
    --surface: #ffffff;
    --surface-2: #f9fafb;
    --text: #1f2937;
    --text-secondary: #6b7280;
    --border: #e5e7eb;
    --border-light: #f3f4f6;
    --screen-font: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Mono', monospace;
    --sans-font: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

[data-theme="dark"] {
    --bg: #111827;
    --surface: #1f2937;
    --surface-2: #374151;
    --text: #f9fafb;
    --text-secondary: #9ca3af;
    --border: #374151;
    --border-light: #1f2937;
    --primary-light: #1e3a5f;
    --success-light: #14532d;
    --danger-light: #7f1d1d;
}

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

html, body {
    min-height: 100%;
}

body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans-font);
    transition: background 0.3s ease, color 0.3s ease;
}

.portal-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.portal-title {
    font-weight: 700;
    font-size: 16px;
    color: var(--text);
}

.portal-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.sound-toggle, .theme-toggle {
    background: var(--surface-2);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px;
    cursor: pointer;
    font-family: var(--sans-font);
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    line-height: 0;
}

.sound-toggle svg, .theme-toggle svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

.sound-toggle:hover, .theme-toggle:hover {
    background: var(--border);
}

.sound-toggle[aria-pressed="true"] {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
}

.phone-stage {
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 32px 16px 40px;
}

.phone {
    width: 400px;
    max-width: 100%;
    background: var(--surface);
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.12);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transition: background 0.3s ease;
}

.phone-status {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    background: var(--surface-2);
    border-bottom: 1px solid var(--border-light);
    color: var(--text-secondary);
    font-size: 13px;
    font-weight: 500;
}

.status-right {
    display: flex;
    align-items: center;
    gap: 8px;
}

.signal-dots {
    display: flex;
    align-items: flex-end;
    gap: 2px;
    height: 12px;
}

.signal-dots i {
    width: 3px;
    background: var(--text-secondary);
    display: block;
}

.signal-dots i:nth-child(1) { height: 4px; }
.signal-dots i:nth-child(2) { height: 6px; }
.signal-dots i:nth-child(3) { height: 8px; }
.signal-dots i:nth-child(4) { height: 10px; }

.phone-screen {
    height: 500px;
    background: var(--surface-2);
    padding: 20px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    position: relative;
    font-family: var(--screen-font);
    border-bottom: 1px solid var(--border-light);
}

.screen-text {
    font-size: 14px;
    line-height: 1.6;
    color: var(--text);
    white-space: pre-wrap;
    word-break: break-word;
}

.input-echo {
    color: var(--text);
    font-family: var(--screen-font);
    font-size: 14px;
}

.cursor-block {
    display: inline-block;
    background: var(--primary);
    color: white;
    width: 8px;
    height: 16px;
    vertical-align: text-bottom;
    animation: blink 1s steps(1) infinite;
}

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

.loading-ellipsis {
    font-family: var(--screen-font);
    color: var(--text-secondary);
}

.phone-compose {
    display: none;
    position: absolute;
    left: -9999px;
    opacity: 0;
    pointer-events: none;
    height: 0;
    width: 0;
    overflow: hidden;
}

.phone-compose.visible {
    display: block;
    position: absolute;
    left: -9999px;
    opacity: 0;
    pointer-events: none;
    height: 0;
    width: 0;
    overflow: hidden;
}

.compose-label {
    display: none;
}

.phone-compose input {
    flex: 1;
    min-width: 0;
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px 14px;
    font-family: var(--screen-font);
    font-size: 14px;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.phone-compose input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.phone-compose input::placeholder {
    color: var(--text-secondary);
    opacity: 0.6;
}

.phone-keypad {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 10px;
    padding: 16px 20px;
    background: var(--surface);
}

.key {
    min-height: 52px;
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 12px;
    font-size: 18px;
    font-weight: 600;
    cursor: pointer;
    font-family: var(--sans-font);
    transition: all 0.15s ease;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.key:focus-visible {
    outline: 3px solid var(--primary);
    outline-offset: 2px;
}

.key.is-pressed {
    background: var(--border);
    transform: scale(0.95);
}

.key-action {
    background: var(--surface-2);
    color: var(--text-secondary);
    font-size: 14px;
}

.key-delete {
    color: var(--danger);
}

.key-delete:hover {
    background: var(--danger-light);
}

.phone-actions {
    display: flex;
    gap: 12px;
    padding: 0 20px 20px;
    background: var(--surface);
}

.action {
    flex: 1;
    min-height: 52px;
    border: none;
    border-radius: 12px;
    color: white;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: 0.02em;
    cursor: pointer;
    font-family: var(--sans-font);
    transition: all 0.2s ease;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.action:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.action:active {
    transform: translateY(0);
}

.action:focus-visible {
    outline: 3px solid var(--gray-300);
    outline-offset: 2px;
}

.action-send {
    background: var(--primary);
}

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

.action-end {
    background: var(--danger);
}

.action-end:hover {
    background: #b91c1c;
}

@media (max-width: 480px) {
    html, body {
        height: 100%;
        overscroll-behavior: none;
    }

    body {
        overflow: hidden;
        height: 100dvh;
        display: flex;
        flex-direction: column;
    }

    .portal-bar {
        flex-shrink: 0;
        padding: 12px 16px;
        padding-top: max(12px, env(safe-area-inset-top));
    }

    .portal-title {
        font-size: 15px;
    }

    .phone-stage {
        flex: 1 1 auto;
        min-height: 0;
        padding: 0;
        padding-bottom: env(safe-area-inset-bottom);
        align-items: stretch;
        display: flex;
    }

    .phone {
        width: 100%;
        border-radius: 0;
        box-shadow: none;
        height: 100%;
    }

    .phone-status {
        padding: 12px 16px;
        font-size: 12px;
    }

    .phone-screen {
        flex: 1 1 auto;
        min-height: 0;
        height: auto;
        padding: 16px;
    }

    .phone-compose.visible {
        padding: 0;
        display: block;
        position: absolute;
        left: -9999px;
        opacity: 0;
        pointer-events: none;
        height: 0;
        width: 0;
    }

    .phone-keypad {
        gap: 8px;
        padding: 12px 16px;
    }

    .key {
        min-height: 48px;
        font-size: 16px;
    }

    .phone-actions {
        gap: 10px;
        padding: 0 16px 16px;
    }

    .action {
        min-height: 48px;
        font-size: 15px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .cursor-block {
        animation: none;
    }

    .key, .action {
        transition: none;
    }
}

.quiz-timer {
    position: absolute;
    top: 8px;
    right: 20px;
    background: var(--primary);
    color: white;
    font-size: 13px;
    font-weight: 700;
    font-family: var(--screen-font);
    padding: 4px 10px;
    border-radius: 8px;
    z-index: 10;
    transition: background 0.3s;
}

.quiz-timer.urgent {
    background: var(--danger);
    animation: pulse 0.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

/* ─── Splash Screen ────────────────────────────────── */

.splash {
    position: fixed;
    inset: 0;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 40%, #0f172a 100%);
    overflow: hidden;
}

.splash-bg {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.splash-ring {
    position: absolute;
    border-radius: 50%;
    border: 1.5px solid rgba(99, 102, 241, 0.15);
    animation: ring-pulse 4s ease-in-out infinite;
}

.splash-ring-1 {
    width: 260px;
    height: 260px;
    animation-delay: 0s;
}

.splash-ring-2 {
    width: 400px;
    height: 400px;
    animation-delay: 0.8s;
}

.splash-ring-3 {
    width: 540px;
    height: 540px;
    animation-delay: 1.6s;
}

@keyframes ring-pulse {
    0%, 100% { transform: scale(1); opacity: 0.3; }
    50% { transform: scale(1.08); opacity: 0.7; }
}

.splash-content {
    position: relative;
    text-align: center;
    color: white;
}

.splash-logo {
    position: relative;
    width: 88px;
    height: 88px;
    margin: 0 auto 24px;
}

.splash-logo-inner {
    width: 88px;
    height: 88px;
    border-radius: 22px;
    background: linear-gradient(135deg, #6366f1, #4f46e5, #4338ca);
    color: white;
    font-size: 42px;
    font-weight: 800;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--sans-font);
    box-shadow: 0 0 40px rgba(99, 102, 241, 0.4), 0 8px 32px rgba(0, 0, 0, 0.3);
    animation: logo-breathe 2.5s ease-in-out infinite;
    position: relative;
    z-index: 2;
}

.splash-logo-pulse {
    position: absolute;
    inset: -8px;
    border-radius: 28px;
    border: 2px solid rgba(99, 102, 241, 0.3);
    animation: logo-ring 2s ease-out infinite;
}

@keyframes logo-breathe {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

@keyframes logo-ring {
    0% { transform: scale(1); opacity: 0.6; }
    100% { transform: scale(1.5); opacity: 0; }
}

.splash-title {
    font-size: 32px;
    font-weight: 800;
    letter-spacing: 0.18em;
    margin: 0 0 6px;
    background: linear-gradient(90deg, #c7d2fe, #e0e7ff, #c7d2fe);
    background-size: 200% auto;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    animation: shimmer 3s linear infinite;
}

@keyframes shimmer {
    0% { background-position: -200% center; }
    100% { background-position: 200% center; }
}

.splash-sub {
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.35em;
    color: rgba(196, 181, 253, 0.7);
    margin: 0 0 32px;
}

.splash-tagline {
    font-size: 16px;
    font-weight: 500;
    color: rgba(196, 181, 253, 0.9);
    min-height: 24px;
    margin: 0 0 36px;
}

.splash-tagline::after {
    content: '|';
    animation: blink-cursor 0.8s steps(1) infinite;
    margin-left: 1px;
    color: #818cf8;
}

@keyframes blink-cursor {
    0%, 49% { opacity: 1; }
    50%, 100% { opacity: 0; }
}

.splash-dots {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin: 0 0 40px;
}

.splash-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #6366f1;
    animation: dot-bounce 1.2s ease-in-out infinite;
}

.splash-dots span:nth-child(2) { animation-delay: 0.15s; }
.splash-dots span:nth-child(3) { animation-delay: 0.3s; }

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

.splash-footer {
    font-size: 11px;
    color: rgba(148, 163, 184, 0.5);
    letter-spacing: 0.08em;
}

.splash-exit {
    animation: splash-out 0.7s ease-in forwards;
}

@keyframes splash-out {
    0% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.04); opacity: 0.6; }
    100% { transform: scale(1.1); opacity: 0; }
}
