/* Modern CSS Variables for consistent theming */
:root {
    --primary-color: #015495;
    --secondary-color: #841617;
    --accent-color: #029500;
    --text-dark: #1a1a1a;
    --text-light: #666;
    --background-light: #f8f9fa;
    --white: #ffffff;
    --border-radius: 16px;
    --shadow-light: 0 2px 8px rgba(0, 0, 0, 0.1);
    --shadow-medium: 0 4px 20px rgba(0, 0, 0, 0.15);
    --shadow-hover: 0 8px 32px rgba(0, 0, 0, 0.2);
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    --max-width: 1200px;
}

/* Reset and base styles */
* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: #DDE5DD;
    min-height: 100vh; /* fallback for browsers without dvh */
    min-height: 100dvh;
    line-height: 1.6;
    overflow-x: hidden; /* Prevent horizontal scrolling only */
}

/* Main content layout */
.main-content {
    min-height: calc(100vh - 80px); /* fallback */
    min-height: calc(100dvh - 80px); /* dvh avoids iOS address-bar cutoff */
    display: flex;
    flex-direction: column;
    /* justify-content: center; */
    padding: 1rem;
    padding-top: 1.5rem; /* Add space between navbar and content */
}

.container {
    max-width: var(--max-width);
    margin: 0 auto;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

/* Hero section */
.hero-section {
    text-align: center;
    flex-shrink: 0;
    margin-bottom: 1.5rem; /* Reduced from 2rem */
}

.title {
    font-size: clamp(1.75rem, 4vw, 2.5rem);
    font-weight: 900;
    background: #841617;
    color: var(--white);
    padding: clamp(0.75rem, 2vw, 1.5rem);
    margin: 0 0 0.75rem 0;
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-medium);
    line-height: 1.2;
    letter-spacing: -0.02em;
}

.subtitle {
    font-size: clamp(1rem, 2.5vw, 1.25rem);
    color: var(--text-light);
    margin: 0;
    font-weight: 400;
}

/* Character selection section */
.character-selection {
    max-width: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-height: 0; /* Allow flex shrinking */
    margin-top: 1.5rem;
    margin-bottom: 2.5rem;
}

.selection-title {
    text-align: center;
    font-size: clamp(1.5rem, 3vw, 2rem);
    color: var(--primary-color);
    margin-bottom: 2rem; /* Set to consistent 2rem everywhere */
    font-weight: 700;
    position: relative;
    flex-shrink: 0;
}

.selection-title::after {
    content: '';
    position: absolute;
    bottom: -10px;
    left: 50%;
    transform: translateX(-50%);
    width: 60px;
    height: 4px;
    background: var(--secondary-color);
    border-radius: 2px;
}

/* Character grid — Flexbox layout centered horizontally & vertically */
.character-grid {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 1.5rem;
    width: 100%;
    max-width: 1000px;
    margin: 1rem auto 2rem;
}

/* Character cards — strictly 1:1 square */
.character-card {
    background: var(--white);
    border-radius: clamp(8px, 2vw, 16px);
    box-shadow: var(--shadow-light);
    transition: var(--transition);
    cursor: pointer;
    border: 2px solid transparent;
    position: relative;
    overflow: hidden;
    aspect-ratio: 1 / 1;
    width: 240px;
    height: auto;
    max-width: 260px;
    flex: 0 0 auto;
}

.character-image-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    aspect-ratio: 1 / 1;
    background-color: #F8F6F3;
    border-radius: clamp(8px, 2vw, 16px);
    overflow: hidden;
}

.character-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
    transform: scaleX(0);
    transition: var(--transition);
    z-index: 2;
}

.character-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-hover);
    border-color: var(--primary-color);
}

.character-card:hover::before {
    transform: scaleX(1);
}

.character-card:active {
    transform: translateY(-2px);
}

/* Reduce hover effects on touch devices */
@media (hover: none) and (pointer: coarse) {
    .character-card:hover {
        transform: none;
        box-shadow: var(--shadow-light);
    }
    
    .character-card:hover .character-image {
        transform: none;
    }
    
    .character-card:active {
        transform: scale(0.98);
    }
}

.character-image {
    width: 100%;
    height: 100%;
    aspect-ratio: 1 / 1;
    object-fit: cover;
    border-radius: clamp(8px, 2vw, 16px);
    display: block;
}

.character-card:hover .character-image {
    border-color: var(--secondary-color);
    transform: scale(1.05);
}

/* Character info */
.character-info {
    flex-shrink: 0;
}

.character-name {
    font-size: clamp(1rem, 2.5vw, 1.25rem);
    color: var(--primary-color);
    margin: 0 0 0.25rem 0;
    font-weight: 600;
}

.character-description {
    color: var(--text-light);
    margin: 0;
    font-size: clamp(0.75rem, 2vw, 0.9rem);
    font-weight: 400;
}

/* Test mode setup screen */
.test-setup-hero {
    margin-bottom: 1rem;
}

.test-setup-card {
    max-width: 560px;
    margin: 0 auto;
    width: 100%;
    background: var(--white);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-medium);
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
}

.test-setup-label {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--primary-color);
}

.test-setup-hint {
    margin: -0.2rem 0 0.25rem;
    font-size: 0.8rem;
    color: var(--text-light);
}

.test-setup-input,
.test-setup-select {
    width: 100%;
    border: 1px solid #d0d7e2;
    border-radius: 10px;
    padding: 0.7rem 0.8rem;
    font-size: 0.95rem;
    color: var(--text-dark);
    background: #f9fbff;
}

.test-setup-input {
    background: #eef4ff;
    font-weight: 700;
}

.test-setup-btn {
    margin-top: 0.35rem;
    border: none;
    border-radius: 10px;
    background: var(--secondary-color);
    color: var(--white);
    font-weight: 700;
    font-size: 1rem;
    padding: 0.75rem 0.95rem;
    cursor: pointer;
    transition: var(--transition);
}

.test-setup-btn:hover {
    filter: brightness(0.95);
}

.test-setup-error {
    margin-top: 0.35rem;
    border: 1px solid rgba(220, 38, 38, 0.35);
    background: rgba(220, 38, 38, 0.08);
    color: #b91c1c;
    border-radius: 8px;
    padding: 0.55rem 0.7rem;
    font-size: 0.84rem;
    font-weight: 600;
}

.test-setup-error.info {
    border-color: rgba(1, 84, 149, 0.3);
    background: rgba(1, 84, 149, 0.08);
    color: #015495;
}

.test-setup-error.success {
    border-color: rgba(2, 149, 0, 0.35);
    background: rgba(2, 149, 0, 0.08);
    color: #0f7a0f;
}

/* Responsive design */

/* Portrait layout */
@media (orientation: portrait) {
    .main-content {
        padding: 1rem;
        min-height: calc(100vh - 80px); /* fallback */
        min-height: calc(100dvh - 80px);
    }

    .selection-title {
        margin-bottom: 2rem;
    }
}

/* Landscape layout — vertically center the content */
@media (orientation: landscape) {
    .main-content {
        padding: 1rem;
        min-height: calc(100vh - 80px); /* fallback */
        min-height: calc(100dvh - 80px);
        display: flex;
        align-items: center;
    }

    .selection-title {
        margin-bottom: 1.5rem;
    }
}

/* Mobile portrait — both cards stack vertically, 100% square, no scroll */
@media (max-width: 479px) {
    .main-content {
        padding: 0.75rem;
        padding-top: 1rem;
        min-height: calc(100vh - 70px); /* fallback */
        min-height: calc(100dvh - 70px);
    }

    .hero-section {
        margin-bottom: 0.5rem;
    }

    .title {
        font-size: 1.2rem;
        padding: 0.5rem 1rem;
        margin-bottom: 0.25rem;
    }

    .selection-title {
        font-size: 1.1rem;
        margin-bottom: 0.75rem;
    }

    .character-grid {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 0.75rem;
    }

    .character-card {
        width: min(calc((100svh - 220px) / 2), 72vw, 220px);
        max-width: min(calc((100svh - 220px) / 2), 72vw, 220px);
        height: auto;
        aspect-ratio: 1 / 1;
        flex: 0 0 auto;
        margin: 0 auto;
    }

    .subtitle {
        font-size: 0.95rem;
    }
}

/* Extra small devices */
@media (max-width: 320px) {
    .character-grid {
        gap: 0.5rem;
    }
    
    .main-content {
        min-height: calc(100vh - 65px); /* fallback */
        min-height: calc(100dvh - 65px);
        padding-top: 1.2rem;
    }
    
    .title {
        font-size: 1.1rem;
        padding: 0.5rem 1rem;
    }
    
    .selection-title {
        font-size: 1.1rem;
        margin-bottom: 2rem;
    }
}

/* Tablet & Laptop (480px+) */
@media (min-width: 480px) {
    .character-grid {
        gap: 1.5rem;
    }
    .character-card {
        flex: 0 0 240px;
        width: 240px;
        height: auto;
        aspect-ratio: 1 / 1;
    }
}

/* Desktop (768px+) */
@media (min-width: 768px) {
    .character-grid {
        gap: 2rem;
        max-width: 960px;
    }
    .character-card {
        flex: 0 0 260px;
        width: 260px;
        height: auto;
        aspect-ratio: 1 / 1;
    }
}

/* Large desktop (1024px+) */
@media (min-width: 1024px) {
    .character-grid {
        gap: 2.5rem;
        max-width: 1100px;
    }
    .character-card {
        flex: 0 0 280px;
        width: 280px;
        height: auto;
        aspect-ratio: 1 / 1;
    }
}

/* High-resolution displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .character-image {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}

/* Dark mode support (if needed in future) */
@media (prefers-color-scheme: dark) {
    /* Dark mode styles can be added here if needed */
}

/* Reduced motion for accessibility */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    .character-card:hover {
        transform: none;
    }
    
    .character-card:hover .character-image {
        transform: none;
    }
}

/* Focus styles for accessibility */
.character-card:focus {
    outline: 3px solid var(--secondary-color);
    outline-offset: 2px;
}

.character-card:focus-visible {
    outline: 3px solid var(--secondary-color);
    outline-offset: 2px;
}

.character-card.keyboard-focused {
    transform: translateY(-4px);
    box-shadow: var(--shadow-hover);
    border-color: var(--secondary-color);
}

/* Loading state animation */
.character-image[src=""] {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading 1.5s infinite;
}

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

/* Additional mobile optimizations */
html {
    height: 100%;
    overflow-x: hidden; /* Prevent horizontal scroll only */
}

/* Handle different mobile orientations */
@media screen and (orientation: landscape) and (max-height: 500px) {
    .main-content {
        min-height: calc(100vh - 50px); /* fallback */
        min-height: calc(100dvh - 50px);
        padding: 0.5rem;
        padding-top: 0.75rem;
    }
    
    .hero-section {
        margin-bottom: 0.5rem;
    }
    
    .title {
        font-size: 1.1rem;
        padding: 0.3rem 0.8rem;
        margin-bottom: 0.25rem;
    }
    
    .subtitle {
        font-size: 0.8rem;
    }
    
    .selection-title {
        font-size: 1.1rem;
        margin-bottom: 1rem;
    }
    
    .character-grid {
        gap: 0.5rem;
    }
}

/* Handle very wide screens */
@media (min-width: 1400px) {
    .character-grid {
        max-width: 1200px;
    }
}

/* Prevent zoom on input focus (iOS) */
input, select, textarea {
    font-size: 16px !important;
}

