/**
 * Science Playground - Shared Styles
 * Used across all subject playgrounds
 * Copyright © 2025 Holistic Quality LLC
 */

@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700;800;900&display=swap');

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

:root {
    --bg-dark: #0f0a1e;
    --bg-card: #1a1230;
    --bg-hover: #251845;
    --text: #f8fafc;
    --text-muted: #a5b4fc;
    --accent: #818cf8;
    --gold: #fbbf24;
    --green: #4ade80;
    --pink: #f472b6;
    --cyan: #22d3ee;
    --orange: #fb923c;
    --red: #f87171;
    --subject-color: var(--accent);
}

html, body {
    font-family: 'Nunito', sans-serif;
    background: var(--bg-dark);
    color: var(--text);
    min-height: 100vh;
    overflow-x: hidden;
}

/* Animated background */
.bg-animation {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    pointer-events: none;
    z-index: 0;
    background: 
        radial-gradient(circle at 10% 20%, color-mix(in srgb, var(--subject-color) 15%, transparent) 0%, transparent 40%),
        radial-gradient(circle at 90% 80%, rgba(244, 114, 182, 0.1) 0%, transparent 40%),
        radial-gradient(circle at 50% 50%, color-mix(in srgb, var(--subject-color) 8%, transparent) 0%, transparent 50%);
}

.bg-animation::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%239C92AC' fill-opacity='0.03'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

/* Header */
.header {
    position: relative;
    z-index: 10;
    text-align: center;
    padding: 1.5rem 1rem 1rem;
}

.header-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    max-width: 1200px;
    margin: 0 auto 1rem;
    padding: 0 1rem;
}

.header-back {
    color: var(--text-muted);
    text-decoration: none;
    padding: 0.5rem 1rem;
    border-radius: 12px;
    background: rgba(255,255,255,0.05);
    font-weight: 600;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.header-back:hover {
    background: rgba(255,255,255,0.1);
    transform: translateX(-3px);
}

.header-hub {
    color: var(--gold);
    text-decoration: none;
    padding: 0.5rem 1rem;
    border-radius: 12px;
    background: rgba(251, 191, 36, 0.1);
    border: 1px solid rgba(251, 191, 36, 0.3);
    font-weight: 700;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.header-hub:hover {
    background: rgba(251, 191, 36, 0.2);
    transform: scale(1.05);
}

.logo {
    font-size: 2.5rem;
    font-weight: 900;
    margin-bottom: 0.5rem;
    text-shadow: 0 0 40px color-mix(in srgb, var(--subject-color) 50%, transparent);
}

.logo-icon {
    display: inline-block;
    animation: bounce 2s infinite;
}

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

.tagline {
    color: var(--text-muted);
    font-size: 1.1rem;
    font-weight: 600;
    max-width: 600px;
    margin: 0 auto;
}

/* Stats bar */
.stats-bar {
    display: flex;
    justify-content: center;
    gap: 2rem;
    padding: 1rem 1.5rem;
    margin: 1rem auto;
    max-width: 700px;
    background: rgba(255,255,255,0.03);
    border-radius: 16px;
    border: 1px solid rgba(255,255,255,0.05);
}

.stat {
    text-align: center;
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 800;
    color: var(--gold);
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1px;
}

/* Level indicator */
.level-bar {
    max-width: 400px;
    margin: 1rem auto;
    padding: 0 1rem;
}

.level-info {
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.5rem;
    font-size: 0.85rem;
}

.level-name {
    color: var(--subject-color);
    font-weight: 700;
}

.level-xp {
    color: var(--text-muted);
}

.level-progress {
    height: 8px;
    background: rgba(255,255,255,0.1);
    border-radius: 4px;
    overflow: hidden;
}

.level-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--subject-color), var(--gold));
    border-radius: 4px;
    transition: width 0.5s ease;
}

/* Toys grid */
.toys-container {
    position: relative;
    z-index: 10;
    max-width: 1400px;
    margin: 0 auto;
    padding: 1rem;
}

.section-title {
    font-size: 1.5rem;
    font-weight: 800;
    margin: 2rem 0 1rem;
    padding-left: 0.5rem;
    border-left: 4px solid var(--subject-color);
    color: var(--subject-color);
}

.toys-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1.25rem;
}

/* Toy card */
.toy-card {
    background: var(--bg-card);
    border-radius: 20px;
    overflow: hidden;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border: 2px solid rgba(255,255,255,0.05);
    position: relative;
}

.toy-card::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 4px;
    background: var(--toy-color, var(--subject-color));
    opacity: 0;
    transition: opacity 0.3s;
}

.toy-card:hover {
    transform: translateY(-8px) scale(1.02);
    border-color: var(--toy-color, var(--subject-color));
    box-shadow: 
        0 20px 40px rgba(0,0,0,0.4),
        0 0 30px color-mix(in srgb, var(--toy-color, var(--subject-color)) 30%, transparent);
}

.toy-card:hover::before {
    opacity: 1;
}

.toy-card:active {
    transform: translateY(-4px) scale(1.01);
}

.toy-card.locked {
    opacity: 0.5;
    cursor: not-allowed;
}

.toy-card.locked:hover {
    transform: none;
    box-shadow: none;
}

.toy-card.completed {
    border-color: var(--green);
}

.toy-card.completed::after {
    content: '✓';
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background: var(--green);
    color: #000;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 14px;
    z-index: 10;
}

.toy-preview {
    height: 180px;
    background: #0a0615;
    position: relative;
    overflow: hidden;
}

.toy-preview canvas {
    width: 100%;
    height: 100%;
}

.toy-badge {
    position: absolute;
    top: 0.75rem;
    left: 0.75rem;
    background: rgba(0,0,0,0.7);
    padding: 0.3rem 0.6rem;
    border-radius: 8px;
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--text-muted);
    backdrop-filter: blur(4px);
}

.toy-difficulty {
    position: absolute;
    bottom: 0.75rem;
    left: 0.75rem;
    display: flex;
    gap: 3px;
}

.toy-diff-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: rgba(255,255,255,0.2);
}

.toy-diff-dot.filled {
    background: var(--gold);
}

.toy-xp {
    position: absolute;
    bottom: 0.75rem;
    right: 0.75rem;
    background: rgba(251, 191, 36, 0.2);
    color: var(--gold);
    padding: 0.2rem 0.5rem;
    border-radius: 6px;
    font-size: 0.7rem;
    font-weight: 700;
}

.toy-info {
    padding: 1rem 1.25rem 1.25rem;
}

.toy-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.5rem;
}

.toy-emoji {
    font-size: 2rem;
    line-height: 1;
}

.toy-title {
    flex: 1;
}

.toy-name {
    font-size: 1.15rem;
    font-weight: 800;
    color: var(--toy-color, var(--subject-color));
    line-height: 1.2;
}

.toy-subtitle {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.toy-desc {
    color: var(--text-muted);
    font-size: 0.9rem;
    line-height: 1.4;
    margin-bottom: 0.75rem;
}

.toy-learn {
    font-size: 0.8rem;
    color: var(--cyan);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* Fullscreen mode */
.fullscreen {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: var(--bg-dark);
    z-index: 1000;
    flex-direction: column;
}

.fullscreen.active {
    display: flex;
}

.fs-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    background: rgba(0,0,0,0.5);
    border-bottom: 1px solid rgba(255,255,255,0.1);
}

.fs-title {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.fs-title-emoji {
    font-size: 1.75rem;
}

.fs-title-text h2 {
    font-size: 1.25rem;
    font-weight: 800;
    margin-bottom: 0.1rem;
}

.fs-title-text p {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.fs-actions {
    display: flex;
    gap: 0.75rem;
}

.fs-btn {
    background: rgba(255,255,255,0.1);
    border: none;
    color: white;
    padding: 0.75rem 1.25rem;
    border-radius: 12px;
    font-size: 0.9rem;
    font-weight: 700;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.fs-btn:hover {
    background: rgba(255,255,255,0.2);
    transform: scale(1.05);
}

.fs-btn-close {
    background: rgba(248, 113, 113, 0.2);
}

.fs-btn-close:hover {
    background: rgba(248, 113, 113, 0.4);
}

.fs-viz {
    flex: 1;
    position: relative;
    overflow: hidden;
}

.fs-footer {
    padding: 1rem 1.5rem;
    background: rgba(0,0,0,0.5);
    border-top: 1px solid rgba(255,255,255,0.1);
    display: flex;
    gap: 1rem;
}

.fs-learn {
    flex: 1;
    background: rgba(129, 140, 248, 0.1);
    border: 1px solid rgba(129, 140, 248, 0.2);
    border-radius: 12px;
    padding: 0.75rem 1rem;
}

.fs-learn h4 {
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--subject-color);
    margin-bottom: 0.25rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.fs-learn p {
    font-size: 0.85rem;
    color: var(--text-muted);
    line-height: 1.4;
}

.fs-challenge {
    background: rgba(251, 191, 36, 0.1);
    border: 1px solid rgba(251, 191, 36, 0.2);
    border-radius: 12px;
    padding: 0.75rem 1rem;
    min-width: 200px;
}

.fs-challenge h4 {
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--gold);
    margin-bottom: 0.25rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.fs-challenge p {
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* Popups */
.popup {
    position: fixed;
    z-index: 1001;
    border-radius: 16px;
    padding: 1rem 1.5rem;
    box-shadow: 0 10px 40px rgba(0,0,0,0.4);
    transform: translateX(150%);
    transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.popup.show {
    transform: translateX(0);
}

.popup-achievement {
    top: 2rem;
    right: 2rem;
    background: linear-gradient(135deg, #fbbf24, #f59e0b);
    color: #1a1a1a;
    display: flex;
    align-items: center;
    gap: 1rem;
}

.popup-achievement .popup-icon {
    font-size: 2.5rem;
}

.popup-achievement h4 {
    font-weight: 800;
}

.popup-fact {
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%) translateY(150%);
    background: linear-gradient(135deg, var(--subject-color), color-mix(in srgb, var(--subject-color) 70%, #8b5cf6));
    color: white;
    max-width: 450px;
    text-align: center;
}

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

.popup-fact h4 {
    font-size: 1rem;
    margin-bottom: 0.25rem;
}

.popup-fact p {
    font-size: 0.9rem;
    opacity: 0.9;
}

.popup-levelup {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0);
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    color: white;
    text-align: center;
    padding: 2rem 3rem;
}

.popup-levelup.show {
    transform: translate(-50%, -50%) scale(1);
}

.popup-levelup .popup-icon {
    font-size: 4rem;
    margin-bottom: 0.5rem;
}

.popup-levelup h3 {
    font-size: 1.5rem;
    margin-bottom: 0.25rem;
}

/* Loading */
.loading {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
}

.loading-dots {
    display: flex;
    gap: 0.5rem;
}

.loading-dot {
    width: 12px;
    height: 12px;
    background: var(--subject-color);
    border-radius: 50%;
    animation: loadPulse 1.4s infinite ease-in-out both;
}

.loading-dot:nth-child(1) { animation-delay: -0.32s; }
.loading-dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes loadPulse {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* Subject cards (for hub) */
.subjects-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
    padding: 1rem;
    max-width: 1200px;
    margin: 0 auto;
}

.subject-card {
    background: var(--bg-card);
    border-radius: 24px;
    padding: 1.5rem;
    text-decoration: none;
    color: var(--text);
    border: 2px solid rgba(255,255,255,0.05);
    transition: all 0.3s;
    position: relative;
    overflow: hidden;
}

.subject-card::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 4px;
    background: var(--card-color);
}

.subject-card:hover {
    transform: translateY(-8px);
    border-color: var(--card-color);
    box-shadow: 0 20px 40px rgba(0,0,0,0.3), 0 0 30px color-mix(in srgb, var(--card-color) 20%, transparent);
}

.subject-icon {
    font-size: 3.5rem;
    margin-bottom: 1rem;
}

.subject-name {
    font-size: 1.4rem;
    font-weight: 800;
    color: var(--card-color);
    margin-bottom: 0.5rem;
}

.subject-desc {
    color: var(--text-muted);
    font-size: 0.95rem;
    line-height: 1.4;
    margin-bottom: 1rem;
}

.subject-stats {
    display: flex;
    gap: 1rem;
    font-size: 0.8rem;
}

.subject-stat {
    color: var(--text-muted);
}

.subject-stat strong {
    color: var(--card-color);
}

/* Progress ring */
.progress-ring {
    position: absolute;
    top: 1rem;
    right: 1rem;
    width: 50px;
    height: 50px;
}

.progress-ring circle {
    fill: none;
    stroke-width: 4;
}

.progress-ring .bg {
    stroke: rgba(255,255,255,0.1);
}

.progress-ring .progress {
    stroke: var(--card-color);
    stroke-linecap: round;
    transform: rotate(-90deg);
    transform-origin: 50% 50%;
    transition: stroke-dashoffset 0.5s;
}

.progress-ring .text {
    fill: var(--card-color);
    font-size: 12px;
    font-weight: 800;
    text-anchor: middle;
    dominant-baseline: central;
}

/* Responsive */
@media (max-width: 768px) {
    .logo { font-size: 1.8rem; }
    .stats-bar { gap: 1rem; padding: 0.75rem; flex-wrap: wrap; }
    .stat-value { font-size: 1.2rem; }
    .toys-grid { grid-template-columns: 1fr; gap: 1rem; }
    .toy-preview { height: 160px; }
    .fs-header { padding: 0.75rem 1rem; flex-wrap: wrap; gap: 0.5rem; }
    .fs-title-text h2 { font-size: 1rem; }
    .fs-btn { padding: 0.5rem 0.75rem; font-size: 0.8rem; }
    .fs-footer { padding: 0.75rem; flex-direction: column; }
    .header-nav { flex-direction: column; gap: 0.5rem; }
    .subjects-grid { grid-template-columns: 1fr; }
}

/* Scrollbar */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.2); }

/* ===== ACCESSIBILITY ===== */

/* Focus styles for keyboard navigation */
*:focus {
    outline: none;
}

*:focus-visible {
    outline: 3px solid var(--subject-color, #6366f1);
    outline-offset: 3px;
    border-radius: 4px;
}

/* Skip link for accessibility */
.skip-link {
    position: absolute;
    top: -100%;
    left: 50%;
    transform: translateX(-50%);
    background: var(--subject-color, #6366f1);
    color: white;
    padding: 1rem 2rem;
    border-radius: 0 0 10px 10px;
    z-index: 10000;
    font-weight: 700;
    text-decoration: none;
    transition: top 0.3s;
}

.skip-link:focus {
    top: 0;
}

/* ========================================
   TABLET BREAKPOINTS (iPad, Surface, etc)
   ======================================== */

/* Large tablets / small laptops (1024px) */
@media (max-width: 1024px) {
    .toys-grid, .subjects-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 1rem;
    }
    
    .stats-bar {
        gap: 1.5rem;
        padding: 1rem;
    }
    
    .hero h1 {
        font-size: 2.8rem;
    }
}

/* Standard tablets - iPad (768px-1024px) */
@media (max-width: 900px) {
    .toys-grid, .subjects-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 1rem;
    }
    
    .header-nav {
        padding: 0 0.5rem;
    }
    
    .fs-header {
        flex-direction: column;
        gap: 1rem;
        text-align: center;
    }
    
    .fs-footer {
        flex-direction: column;
        gap: 1rem;
    }
}

/* Small tablets / large phones (600px) */
@media (max-width: 600px) {
    .toys-grid, .subjects-grid {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }
    
    .toy-card, .subject-card {
        min-height: 120px;
    }
    
    .stats-bar {
        flex-wrap: wrap;
        gap: 1rem;
    }
    
    .stat {
        flex: 1 1 80px;
    }
}

/* ========================================
   iPHONE SAFE AREA (notch, home indicator)
   ======================================== */

@supports (padding: env(safe-area-inset-top)) {
    body {
        padding-top: env(safe-area-inset-top);
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    .header {
        padding-top: calc(1.5rem + env(safe-area-inset-top));
    }
    
    .fs-header {
        padding-top: calc(1rem + env(safe-area-inset-top));
    }
    
    .fs-footer {
        padding-bottom: calc(1rem + env(safe-area-inset-bottom));
    }
    
    footer {
        padding-bottom: calc(2rem + env(safe-area-inset-bottom));
    }
}

/* ========================================
   TOUCH INTERACTIONS
   ======================================== */

/* Prevent double-tap zoom on interactive elements */
.toy-card, .subject-card, button, a, .fs-btn {
    touch-action: manipulation;
}

/* Canvas touch handling */
canvas {
    touch-action: none; /* Allow custom touch handling */
}

/* Minimum 44x44px touch targets (Apple HIG) */
.toy-card, .subject-card {
    min-height: 44px;
    min-width: 44px;
}

button, .fs-btn, .header-back, .header-hub {
    min-height: 44px;
    min-width: 44px;
}

/* Active states for touch feedback */
.toy-card:active, .subject-card:active {
    transform: scale(0.98);
    opacity: 0.9;
}

button:active, .fs-btn:active {
    transform: scale(0.95);
}

/* ========================================
   ENHANCED FOCUS STATES (keyboard nav)
   ======================================== */

*:focus {
    outline: none;
}

*:focus-visible {
    outline: 3px solid var(--gold);
    outline-offset: 2px;
    border-radius: 4px;
}

.toy-card:focus-visible, .subject-card:focus-visible {
    outline-offset: 4px;
    box-shadow: 0 0 0 4px rgba(251, 191, 36, 0.3);
}

button:focus-visible, .fs-btn:focus-visible {
    outline-offset: 3px;
}

/* ========================================
   LANDSCAPE MODE FIXES
   ======================================== */

@media (max-height: 500px) and (orientation: landscape) {
    .header {
        padding: 0.5rem 1rem;
    }
    
    .logo {
        font-size: 1.8rem;
    }
    
    .tagline {
        display: none;
    }
    
    .stats-bar {
        padding: 0.5rem 1rem;
        margin: 0.5rem auto;
    }
    
    .fs-footer {
        padding: 0.5rem 1rem;
    }
    
    .fs-learn, .fs-challenge {
        padding: 0.5rem;
    }
}

/* ========================================
   FULLSCREEN MODE MOBILE FIXES
   ======================================== */

.fullscreen {
    overscroll-behavior: contain; /* Prevent pull-to-refresh */
}

@media (max-width: 768px) {
    .fullscreen {
        padding: 0;
    }
    
    .fs-header {
        padding: 0.75rem;
        gap: 0.5rem;
    }
    
    .fs-title-text h2 {
        font-size: 1.2rem;
    }
    
    .fs-btn {
        padding: 0.5rem 0.75rem;
        font-size: 0.85rem;
    }
    
    .fs-viz {
        height: calc(100vh - 200px);
        height: calc(100dvh - 200px); /* Dynamic viewport height */
    }
    
    .fs-footer {
        max-height: 30vh;
        overflow-y: auto;
    }
}

/* Dynamic viewport height for mobile browsers */
@supports (height: 100dvh) {
    .fullscreen {
        height: 100dvh;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    :root {
        --bg-main: #000;
        --bg-card: #111;
        --text-main: #fff;
        --text-muted: #ccc;
    }
    
    .toy-card, .subject-card {
        border: 2px solid white;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    .bg-animation {
        animation: none;
        background: var(--bg-main);
    }
}

/* Screen reader only text */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}
