:root {
    /* Colors (Dark mode by default for that "cool" look) */
    --bg-color: #0f0f11;
    --surface-color: #1a1a1c;
    --surface-light: #2a2a2d;
    --text-primary: #f4f4f5;
    --text-secondary: #a1a1aa;
    --accent-color: #e2e8f0;
    --success-color: #10b981;
    --error-color: #ef4444;
    --border-color: #3f3f46;
    
    /* Typography */
    --font-serif: 'Cormorant Garamond', serif;
    --font-sans: 'Inter', sans-serif;
    
    /* Animation */
    --transition-snappy: 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    --transition-smooth: 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

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

body {
    background-color: var(--bg-color);
    color: var(--text-primary);
    font-family: var(--font-sans);
    height: 100dvh; /* use fixed height so child flex containers can scroll internally */
    display: flex;
    flex-direction: column;
    overflow: hidden; /* Prevent pull-to-refresh */
    user-select: none; /* Prevent accidental text highlighting */
    -webkit-user-select: none;
}

input {
    user-select: auto;
    -webkit-user-select: auto;
}

/* Typography Utilities */
.serif-title {
    font-family: var(--font-serif);
    font-size: 2.5rem;
    font-weight: 600;
    line-height: 1.1;
    letter-spacing: -0.02em;
}

/* Header */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.5rem 2rem;
}

.logo {
    display: flex;
    overflow: hidden;
    font-family: var(--font-serif);
    font-size: 2rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.logo .letter {
    display: inline-block;
    transform: translateX(20px);
    opacity: 0;
}

.icon-btn {
    background: none;
    border: none;
    color: var(--text-primary);
    cursor: pointer;
    width: 2rem;
    height: 2rem;
    padding: 0.25rem;
    transition: transform 0.2s;
}

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

/* Main Layout */
.game-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 0 1rem 2rem 1rem;
    max-width: 600px;
    margin: 0 auto;
    width: 100%;
    min-height: 0;
}

.category-display {
    text-align: center;
    margin-bottom: 2rem;
    opacity: 0;
    animation: fadeIn 1s ease forwards 1s; /* fade in after logo */
}

.category-label {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    color: var(--text-secondary);
    font-weight: 600;
}

#category-name {
    font-family: var(--font-serif);
    font-size: 2.25rem;
    font-weight: 400;
    font-style: italic;
    color: var(--accent-color);
    margin-top: 0.25rem;
}

/* Timeline */
.timeline-area {
    flex: 1;
    position: relative;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 1rem 0 6rem; /* Extra bottom padding so users can scroll the bottom card up */
    scrollbar-width: none;
    min-height: 0;
}

.timeline-area::-webkit-scrollbar {
    display: none;
}

.timeline {
    position: relative;
    min-height: 100%;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.timeline-line {
    position: absolute;
    left: 2rem;
    top: 0;
    bottom: 0;
    width: 2px;
    background: var(--border-color);
    z-index: 0;
}

/* Drop Targets for Timeline */
.drop-target {
    height: 2rem;
    margin: -1rem 0;
    z-index: 10;
    position: relative;
    transition: all 0.3s ease;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
}
.drop-target.has-card {
    height: auto;
    padding: 1rem 0;
    margin: 0;
}
.drop-target.active-hover {
    height: 120px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px dashed var(--text-secondary);
}

/* Cards (3D Flips) */
.timeline-card {
    position: relative;
    z-index: 1;
    width: calc(100% - 4rem);
    margin-left: 3rem;
    margin-bottom: 1.5rem;
    perspective: 1000px;
    cursor: grab;
}

.timeline-card::before {
    content: '';
    position: absolute;
    left: -1.25rem;
    top: 50%;
    transform: translateY(-50%);
    width: 12px;
    height: 12px;
    background: var(--bg-color);
    border: 2px solid var(--text-primary);
    border-radius: 50%;
    z-index: 2;
}

.card-inner {
    position: relative;
    width: 100%;
    height: 100px;
    text-align: center;
    transition: transform 0.6s var(--transition-snappy);
    transform-style: preserve-3d;
}

.timeline-card.flipped .card-inner {
    transform: rotateY(180deg);
}

.card-front, .card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    border-radius: 12px;
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 20px rgba(0,0,0,0.2);
}

.card-front {
    /* Event text side */
    font-family: var(--font-serif);
    font-size: 1.4rem;
    font-weight: 600;
    line-height: 1.2;
    text-align: center;
    justify-content: center;
}
.card-front.correct {
    border-color: var(--success-color);
}
.card-back.wrong, .card-front.wrong {
    border-color: var(--error-color);
}

.card-back.wrong-permanent {
    border-color: var(--error-color) !important;
}

.card-back {
    /* Year side */
    transform: rotateY(180deg);
    background: var(--surface-light);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}
.card-back .year {
    font-family: var(--font-serif);
    font-size: 2.25rem;
    font-weight: 700;
}
.card-back .event-text {
    font-family: var(--font-sans);
    font-size: 0.85rem;
    line-height: 1.3;
    text-align: center;
    font-weight: 400;
}
.card-back.correct-year .year {
    color: var(--success-color);
}
.card-back.incorrect-year .year {
    color: var(--error-color);
}

/* Active Card Area */
.active-card-area {
    margin-bottom: 1rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    z-index: 20;
}

.deck-counter {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
}

.active-card-container {
    width: 100%;
    height: 120px;
    position: relative;
    perspective: 1000px;
}

.active-card-container.game-over {
    height: auto;
    padding-bottom: 0.5rem;
}

.active-card {
    width: 100%;
    height: 100px;
    background: var(--surface-color);
    border: 1px solid var(--text-primary);
    border-radius: 12px;
    padding: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    font-family: var(--font-serif);
    font-size: 1.4rem;
    font-weight: 600;
    line-height: 1.2;
    cursor: grab;
    box-shadow: 0 8px 30px rgba(0,0,0,0.4);
    touch-action: none;
    z-index: 100;
    transition: box-shadow 0.2s, transform 0.2s;
}

.active-card:active {
    cursor: grabbing;
    transform: scale(1.02);
}

.active-card.dragging {
    opacity: 0.9;
    box-shadow: 0 15px 40px rgba(0,0,0,0.6);
    position: fixed;
    pointer-events: none;
}

/* Controls */
#confirm-container {
    position: fixed;
    bottom: 3rem;
    left: 0;
    right: 0;
    width: 100%;
    display: flex;
    justify-content: center;
    z-index: 200;
    animation: slideUp 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

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

#confirm-container.hidden {
    display: none;
}

.primary-btn {
    background: var(--text-primary);
    color: var(--bg-color);
    font-family: var(--font-sans);
    font-weight: 600;
    font-size: 1rem;
    padding: 1rem 2rem;
    border-radius: 30px;
    border: none;
    cursor: pointer;
    transition: transform 0.2s, opacity 0.2s;
    width: 100%;
    max-width: 300px;
}

.primary-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.primary-btn:not(:disabled):active {
    transform: scale(0.97);
}

.secondary-btn {
    background: var(--surface-light);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    padding: 0.5rem 1rem;
    border-radius: 8px;
    cursor: pointer;
    font-family: var(--font-sans);
    transition: background 0.2s;
}

.secondary-btn:active {
    background: var(--border-color);
}

/* Modals */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.8);
    backdrop-filter: blur(5px);
    z-index: 999;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 1;
    transition: opacity 0.3s;
}

.modal-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.modal {
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 3rem 2.5rem;
    width: 90%;
    max-width: 440px;
    position: fixed;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%) scale(1);
    z-index: 1000;
    box-shadow: 0 20px 50px rgba(0,0,0,0.5);
    transition: transform 0.3s var(--transition-snappy), opacity 0.3s;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.modal.hidden {
    transform: translate(-50%, -50%) scale(0.9);
    opacity: 0;
    pointer-events: none;
}

.modal input {
    width: 100%;
    padding: 1rem;
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    border-radius: 8px;
    font-family: var(--font-sans);
    font-size: 1rem;
    outline: none;
}
.modal input:focus {
    border-color: var(--text-primary);
}

.setting-group {
    margin-bottom: 2rem;
}

.setting-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    color: var(--text-secondary);
}

.results {
    display: flex;
    justify-content: center;
    gap: 2rem;
    font-size: 1.25rem;
}

.leaderboard-list {
    max-height: 200px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    font-size: 0.9rem;
}

.leaderboard-item {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem;
    background: var(--surface-color);
    border-radius: 4px;
}

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

@keyframes slideLetters {
    to { transform: translateX(0); opacity: 1; }
}

/* Animating card sliding into position correctly */
.animating-slide {
    transition: transform 0.8s var(--transition-snappy) !important;
}

/* Toast Notification */
.toast {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%) translateY(0);
    background: var(--text-primary);
    color: var(--bg-color);
    padding: 0.75rem 1.5rem;
    border-radius: 30px;
    font-weight: 600;
    font-size: 0.95rem;
    z-index: 10000;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
    transition: transform 0.3s var(--transition-snappy), opacity 0.3s;
}

.toast.hidden {
    opacity: 0;
    transform: translateX(-50%) translateY(20px);
    pointer-events: none;
}

.input-row {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}
.input-row input {
    flex: 1;
}
.icon-action-btn {
    background: var(--surface-light);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.75rem;
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}
.icon-action-btn:hover {
    background: var(--border-color);
}
.modal.keyboard-up {
    top: 10px !important;
    transform: translateX(-50%) scale(1) !important;
}
