:root {
    --bg-dark: #1a0b2e;
    --bg-mid: #4a154b;
    --bg-light: #2d0a31;
    --primary: #ff9933;
    --primary-glow: rgba(255, 153, 51, 0.6);
    --secondary: #00e5ff;
    --accent: #ff007f;
    --text-main: #ffffff;
    --text-muted: #d0cce5;
    --glass-bg: rgba(255, 255, 255, 0.08);
    --glass-border: rgba(255, 255, 255, 0.2);
    --correct: #00e676;
    --wrong: #ff1744;
}

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

body {
    font-family: 'Outfit', sans-serif;
    /* Animated Gradient Base */
    background: linear-gradient(45deg, var(--bg-dark), var(--bg-mid), var(--bg-light));
    background-size: 400% 400%;
    animation: gradientBG 20s ease infinite;
    color: var(--text-main);
    min-height: 100vh;
    overflow-x: hidden;
    transition: background 1s ease;
}

@keyframes gradientBG {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

#tsparticles {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    z-index: -1;
    pointer-events: none;
}

#app {
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: 40px 20px;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.screen {
    display: none;
    height: 100%;
    flex-direction: column;
    animation: fadeIn 0.4s ease-out;
}

.screen.active {
    display: flex;
}

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

/* Glassmorphism */
.glass-panel, .panel, .card {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 16px;
    padding: 24px;
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.3);
}

h1, h2, h3 {
    font-weight: 800;
    margin-bottom: 0.5em;
}

h1 {
    font-size: 3rem;
    background: -webkit-linear-gradient(var(--primary), var(--secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.btn {
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.3s ease;
}

.btn.primary {
    background: var(--primary);
    color: #000;
}

.btn.primary.glow:hover {
    box-shadow: 0 0 20px var(--primary-glow);
    transform: translateY(-2px);
}

.btn.secondary {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-main);
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.btn.secondary:hover {
    background: rgba(255, 255, 255, 0.2);
}

.hidden {
    display: none !important;
}

/* Setup Screen */
#setup-screen {
    align-items: center;
    justify-content: center;
    text-align: center;
}

.team-inputs {
    margin: 20px 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
    max-width: 300px;
}

/* Game Mode Selector */
.mode-selector {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 15px;
    margin-bottom: 20px;
}
.mode-card {
    background: rgba(0,0,0,0.3);
    border: 2px solid var(--glass-border);
    border-radius: 12px;
    padding: 15px;
    text-align: center;
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;
}
.mode-card h4 {
    margin: 0 0 5px 0;
    font-size: 1.2rem;
    color: white;
}
.mode-card p {
    margin: 0;
    font-size: 0.9rem;
    color: var(--text-muted);
}
.mode-card.active {
    border-color: var(--primary);
    background: rgba(0, 210, 255, 0.15);
    box-shadow: 0 0 15px rgba(0, 210, 255, 0.3);
}
.mode-card.locked {
    opacity: 0.6;
    cursor: not-allowed;
    background: rgba(0,0,0,0.5);
}
.mode-card .lock-icon {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 2.5rem;
    opacity: 0.2;
}

.team-input-row {
    display: flex;
    gap: 10px;
}

input[type="text"] {
    flex: 1;
    padding: 10px;
    border-radius: 6px;
    border: 1px solid var(--glass-border);
    background: rgba(0,0,0,0.2);
    color: white;
    font-family: inherit;
}

.avatar-btn {
    border: 1px solid var(--glass-border);
    background: rgba(0,0,0,0.2);
    font-size: 1.4rem;
    cursor: pointer;
    border-radius: 6px;
    padding: 0 12px;
    transition: transform 0.2s, background 0.2s;
}
.avatar-btn:hover {
    transform: scale(1.1);
    background: rgba(255,255,255,0.1);
}

.spacer { margin: 20px 0; }

/* Game Screen */
#game-screen header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.turn-indicator {
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--primary);
    text-shadow: 0 0 10px var(--primary-glow);
}

#game-screen main {
    display: flex;
    flex-direction: column;
    gap: 20px;
    flex: 1;
    min-height: 0;
}

/* Custom Player */
.custom-player {
    background: linear-gradient(145deg, rgba(0,0,0,0.4), rgba(0,0,0,0.2));
    border-radius: 20px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    border: 1px solid var(--glass-border);
}

.visualizer {
    display: flex;
    gap: 6px;
    height: 40px;
    align-items: flex-end;
}
.visualizer .bar {
    width: 10px;
    background: var(--text-muted);
    border-radius: 4px;
    transition: height 0.1s;
    height: 6px;
}
.visualizer.playing .bar {
    background: var(--secondary);
    animation: bounce 1s infinite alternate ease-in-out;
}
.visualizer.playing .bar:nth-child(1) { animation-duration: 0.6s; }
.visualizer.playing .bar:nth-child(2) { animation-duration: 0.8s; }
.visualizer.playing .bar:nth-child(3) { animation-duration: 0.5s; }
.visualizer.playing .bar:nth-child(4) { animation-duration: 0.9s; }
.visualizer.playing .bar:nth-child(5) { animation-duration: 0.7s; }

@keyframes bounce {
    0% { height: 6px; }
    100% { height: 40px; box-shadow: 0 0 10px var(--secondary); }
}

.play-btn {
    width: 70px;
    height: 70px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    border: none;
    color: white;
    font-size: 2rem;
    cursor: pointer;
    box-shadow: 0 0 20px var(--primary-glow);
    display: flex;
    justify-content: center;
    align-items: center;
    transition: transform 0.2s, box-shadow 0.2s;
    padding-left: 6px; /* Offset to center the play triangle optically */
}
.play-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 0 30px rgba(255,153,51,0.8);
}
.play-btn.playing {
    padding-left: 0;
    background: linear-gradient(135deg, var(--accent), var(--secondary));
    box-shadow: 0 0 20px rgba(0, 229, 255, 0.6);
}

.volume-control {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 60%;
}
.volume-control input[type=range] {
    flex: 1;
    accent-color: var(--secondary);
    cursor: pointer;
}

/* Hide volume control on touch devices since iOS/Android disable JS volume control */
@media (hover: none) and (pointer: coarse) {
    .volume-control {
        display: none;
    }
}

/* Timeline */
.timeline-container {
    padding: 20px;
    border-radius: 16px;
    background: rgba(0,0,0,0.15);
    border: 1px solid rgba(255,255,255,0.05);
    width: 100%;
    min-width: 0; /* Prevents overflow pushing */
}

.timeline {
    display: flex;
    gap: 15px;
    overflow-x: auto;
    padding: 20px 0;
    scroll-behavior: smooth;
    min-height: 250px;
    align-items: center;
}

.timeline-dropzone {
    min-width: 30px;
    height: 200px;
    border: 2px dashed rgba(255,255,255,0.1);
    border-radius: 12px;
    transition: all 0.2s;
    cursor: flex;
}
.timeline-dropzone.drag-over {
    background: rgba(0, 210, 255, 0.2);
    border-color: var(--primary);
    transform: scale(1.05);
}
.timeline-dropzone.provisional-guess {
    background: rgba(255, 215, 0, 0.2);
    border: 2px solid gold;
    color: gold;
    font-size: 3.5rem;
    font-weight: bold;
    min-width: 140px;
    transform: scale(1.02);
}

.timeline::-webkit-scrollbar {
    height: 8px;
}
.timeline::-webkit-scrollbar-thumb {
    background: var(--primary);
    border-radius: 4px;
}

.card {
    min-width: 160px;
    height: 230px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    background: linear-gradient(145deg, rgba(255,255,255,0.15), rgba(0,0,0,0.4));
    position: relative;
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275), box-shadow 0.4s;
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255,255,255,0.2);
}

.card:hover {
    transform: translateY(-8px);
    box-shadow: 0 15px 30px rgba(0,0,0,0.6), 0 0 20px var(--primary-glow);
}

.card .year {
    font-size: 2.5rem;
    font-weight: 800;
    color: var(--primary);
}
.card .title {
    font-size: 0.9rem;
    margin-top: 10px;
    color: var(--text-muted);
}

/* Active Area */
.active-song-area {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

audio {
    width: 100%;
    outline: none;
    border-radius: 30px;
}

.placement-controls {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 20px;
    max-height: 250px;
    overflow-y: auto;
    padding-right: 10px;
}

.placement-controls::-webkit-scrollbar {
    width: 6px;
}
.placement-controls::-webkit-scrollbar-thumb {
    background: var(--primary);
    border-radius: 4px;
}

.draggable-card {
    cursor: grab;
    margin: 10px auto;
    font-size: 3rem !important;
    color: var(--secondary) !important;
    border: 2px dashed var(--secondary) !important;
    height: 100px !important;
    min-width: 100px !important;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0,0,0,0.5);
    transition: opacity 0.3s;
}
.draggable-card:active {
    cursor: grabbing;
    opacity: 0.8;
}
.draggable-card.ghosted {
    opacity: 0.2;
    cursor: not-allowed;
}

.place-btn {
    background: rgba(255,255,255,0.05);
    border: 1px dashed var(--primary);
    color: white;
    padding: 12px 16px;
    min-height: 50px; /* Touch friendly */
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    font-family: inherit;
    font-size: 1rem;
}
.place-btn:hover, .place-btn.drag-over {
    background: var(--primary);
    color: #000;
}

.place-btn.selected {
    background: var(--secondary);
    border: 2px solid #fff;
    color: white;
    font-weight: bold;
    transform: scale(1.02);
}

/* Scoreboard */
.sidebar {
    grid-column: 1 / -1;
    display: flex;
    gap: 20px;
    overflow-x: auto;
}
.sidebar ul {
    list-style: none;
    display: flex;
    gap: 20px;
    width: 100%;
}
.sidebar li {
    background: rgba(0,0,0,0.2);
    padding: 15px;
    border-radius: 10px;
    flex: 1;
    text-align: center;
    position: relative;
}
.sidebar li.active-team {
    border: 1px solid var(--primary);
    box-shadow: 0 0 15px rgba(0,210,255,0.2);
}

/* Modals */
.modal {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
}
.modal-content {
    text-align: center;
    max-width: 400px;
    width: 90%;
    animation: zoomIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
@keyframes zoomIn {
    from { transform: scale(0.8); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.song-reveal {
    margin: 20px 0;
    padding: 20px;
}
.reveal-year {
    font-size: 3rem;
    font-weight: 800;
    color: var(--secondary);
    margin-top: 10px;
}

/* 3D Flip Card Animation for Modal */
.flip-card-container {
    background-color: transparent;
    width: 280px;
    height: 380px;
    perspective: 1000px;
    margin: 0 auto 20px;
}
.flip-card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    text-align: center;
    transition: transform 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    transform-style: preserve-3d;
}
.flip-card-container.flipped .flip-card-inner {
    transform: rotateY(180deg);
}
.flip-card-front, .flip-card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border-radius: 16px;
    box-shadow: 0 15px 35px rgba(0,0,0,0.5);
}
.flip-card-front {
    background: linear-gradient(145deg, var(--bg-mid), var(--bg-dark));
    border: 2px solid var(--primary);
}
.flip-card-back {
    transform: rotateY(180deg);
    background: linear-gradient(135deg, rgba(255,255,255,0.1), rgba(0,0,0,0.4));
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(12px);
    transition: box-shadow 0.3s, border-color 0.3s;
}

/* Win / Lose Animation States */
.correct-glow {
    box-shadow: 0 0 50px var(--correct), inset 0 0 20px var(--correct);
    border-color: var(--correct);
}

.wrong-shake {
    animation: shake 0.5s cubic-bezier(.36,.07,.19,.97) both;
    box-shadow: 0 0 50px var(--wrong), inset 0 0 20px var(--wrong);
    border-color: var(--wrong);
}

@keyframes shake {
  10%, 90% { transform: rotateY(180deg) translate3d(-3px, 0, 0); }
  20%, 80% { transform: rotateY(180deg) translate3d(5px, 0, 0); }
  30%, 50%, 70% { transform: rotateY(180deg) translate3d(-8px, 0, 0); }
  40%, 60% { transform: rotateY(180deg) translate3d(8px, 0, 0); }
}

@media (max-width: 768px) {
    #app {
        padding: 0; /* Full bleed on mobile */
    }
    
    #game-screen header {
        position: sticky;
        top: 0;
        z-index: 50;
        background: rgba(0,0,0,0.85);
        backdrop-filter: blur(10px);
        margin: 0;
        padding: 15px 20px;
        border-bottom: 1px solid var(--glass-border);
    }
    
    #game-screen header h2 { 
        margin: 0; 
        font-size: 1.5rem; 
    }
    
    .turn-indicator {
        font-size: 1rem;
    }
    
    /* Timeline horizontal scroll-snapping */
    .timeline {
        scroll-snap-type: x mandatory;
        padding: 10px 50%; /* Allows scrolling to the exact center */
        min-height: auto;
    }
    
    .card {
        scroll-snap-align: center;
        min-width: 65vw;
        height: auto;
        aspect-ratio: 2/3;
    }
    .card .year { font-size: 2rem; }
    
    .timeline-dropzone {
        min-width: 60px;
        scroll-snap-align: center;
        height: 50vw; /* Match roughly card height */
        max-height: 230px;
    }
    
    /* Compact panels */
    .glass-panel, .panel {
        padding: 15px;
        border-radius: 12px;
        margin: 0 10px;
    }
    
    /* Compact the active player area */
    .custom-player { 
        padding: 10px; 
        gap: 15px; 
        flex-direction: row; 
        justify-content: space-around; 
    }
    .play-btn { width: 50px; height: 50px; font-size: 1.5rem; }
    .visualizer { height: 30px; }
    
    #mystery-dragger { 
        min-width: 80px !important; 
        height: 100px !important; 
        font-size: 2.5rem !important; 
        margin: 5px auto;
    }
    
    .placement-controls {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
        max-height: 200px;
    }
    .place-btn { padding: 10px; min-height: 44px; font-size: 0.9rem; }
    
    /* Sticky Scoreboard Footer */
    .sidebar {
        position: sticky;
        bottom: 0;
        z-index: 50;
        background: rgba(0,0,0,0.9);
        backdrop-filter: blur(10px);
        padding: 10px;
        margin: 0;
        border-radius: 0;
        border-top: 1px solid var(--glass-border);
    }
    .sidebar h3 { display: none; }
    .sidebar ul { gap: 8px; }
    .sidebar li { padding: 8px; font-size: 0.8rem; }
    
    /* Adjust dynamically injected inline HTML in Scoreboard via CSS overrides */
    .sidebar li strong { font-size: 0.9rem !important; }
    .sidebar .progress-track { gap: 2px !important; margin: 4px 0 !important; }
    .sidebar .progress-track div { width: 8px !important; height: 8px !important; }

    /* Modal Mobile Fixes */
    .modal-content {
        padding: 20px 10px;
        max-height: 95vh;
        overflow-y: auto;
    }
    
    .flip-card-container {
        height: 240px; /* Shorter card to prevent overlapping buttons */
        width: 170px;
        margin: 0 auto 15px auto; /* explicitly re-center */
    }
    
    .flip-card-front span {
        font-size: 4rem !important;
    }
    
    #result-title {
        font-size: 2rem !important;
        margin-bottom: 10px !important;
    }
    
    .reveal-year {
        font-size: 2.2rem;
    }
}

/* Puthu Metu Mode Styles */
.puthu-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 20px;
}
.puthu-scoreboard {
    display: flex;
    gap: 15px;
    margin-top: 10px;
    flex-wrap: wrap;
    justify-content: center;
}
.puthu-score-box {
    background: rgba(0,0,0,0.5);
    border: 1px solid var(--glass-border);
    padding: 8px 15px;
    border-radius: 8px;
    font-size: 1.1rem;
    font-weight: 600;
}
.puthu-score-box.active-team {
    border-color: var(--primary);
    box-shadow: 0 0 10px var(--primary-glow);
}
@keyframes pulse {
    0% { transform: scale(1); box-shadow: 0 0 15px var(--primary-glow); }
    50% { transform: scale(1.05); box-shadow: 0 0 30px var(--primary-glow); }
    100% { transform: scale(1); box-shadow: 0 0 15px var(--primary-glow); }
}
