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

body {
    font-family: Georgia, 'Times New Roman', serif;
    background: linear-gradient(160deg, #2364aa 0%, #3da5d9 45%, #73bfb8 100%);
    min-height: 100vh;
    padding: 20px;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    background: #ffffff;
    border-radius: 12px;
    box-shadow: 0 12px 48px rgba(35, 100, 170, 0.35), 0 4px 16px rgba(0,0,0,0.12);
    overflow: hidden;
    border: 2px solid rgba(61, 165, 217, 0.5);
}

header {
    background: #2364aa;
    color: #ffffff;
    text-align: center;
    padding: 32px 30px;
    border-bottom: 3px solid #fec601;
}

header h1 {
    font-size: 2.5em;
    font-weight: bold;
    font-family: Georgia, 'Palatino Linotype', serif;
    letter-spacing: 4px;
    text-transform: uppercase;
    text-shadow: 2px 2px 8px rgba(0, 0, 0, 0.6);
}

.phase {
    padding: 30px;
}

/* Setup Phase Styles */
.setup-section {
    text-align: center;
    margin-bottom: 30px;
}

.setup-section h2 {
    color: #2364aa;
    margin-bottom: 30px;
    font-size: 2em;
    font-family: Georgia, serif;
    font-weight: bold;
    letter-spacing: 1px;
}

.horse-count-selection {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
    margin-bottom: 30px;
}

.horse-count-selection label {
    font-size: 1.2em;
    font-weight: bold;
    color: #2364aa;
}

.horse-count-selection input {
    padding: 10px;
    font-size: 1.2em;
    border: 2px solid #73bfb8;
    border-radius: 8px;
    text-align: center;
    width: 80px;
    background: #ffffff;
    color: #2364aa;
    font-family: Georgia, serif;
}

button {
    background: #ea7317;
    color: #ffffff;
    border: none;
    padding: 12px 28px;
    font-size: 1.05em;
    border-radius: 6px;
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s, filter 0.15s;
    font-weight: bold;
    font-family: Georgia, serif;
    letter-spacing: 1px;
    text-transform: uppercase;
    box-shadow: 0 3px 14px rgba(234, 115, 23, 0.45);
}

button:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 18px rgba(0,0,0,0.3);
    filter: brightness(1.08);
}

button:active {
    transform: translateY(0);
}

/* Naming Section */
.naming-section {
    text-align: center;
}

.naming-section h3 {
    color: #2364aa;
    margin-bottom: 25px;
    font-size: 1.5em;
    font-family: Georgia, serif;
    font-weight: bold;
    letter-spacing: 1px;
}

#horse-inputs {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    margin-bottom: 30px;
    justify-content: center;
}

.horse-input {
    display: flex;
    align-items: center;
    gap: 8px;
    background: #eef8f7;
    padding: 12px;
    border-radius: 8px;
    border: 1px solid #73bfb8;
    flex: 1 1 auto;
    min-width: 280px;
    max-width: 400px;
}

.horse-input label {
    font-weight: bold;
    color: #2d4a35;
    white-space: nowrap;
    font-size: 0.9em;
    font-family: Georgia, serif;
}

/* Label with inline SVG icon */
.horse-input-label {
    display: flex;
    align-items: center;
    gap: 7px;
    font-weight: bold;
    color: #2364aa;
    font-size: 0.9em;
    font-family: Georgia, serif;
    white-space: nowrap;
}

.horse-input-label em {
    font-style: normal;
    color: #3da5d9;
    font-size: 0.9em;
}

.horse-input input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid #73bfb8;
    border-radius: 6px;
    font-size: 1em;
    background: #ffffff;
    color: #2364aa;
    font-family: Georgia, serif;
}

.horse-input input:focus {
    outline: none;
    border-color: #3da5d9;
    box-shadow: 0 0 0 3px rgba(61, 165, 217, 0.25);
}

/* Racing Phase */
.race-controls {
    text-align: center;
    margin-bottom: 20px;
}

.race-track {
    position: relative;
    background: linear-gradient(to right,
        #27ae60 0%,
        #2ecc71 48%,
        #27ae60 94%,
        #fec601 94%,
        #fec601 97%,
        #ea7317 100%);
    border: 3px solid #1e8449;
    border-radius: 10px;
    padding: 20px;
    min-height: 400px;
    overflow: hidden;
    box-shadow: inset 0 2px 14px rgba(0,0,0,0.15), 0 4px 16px rgba(39, 174, 96, 0.3);
}

/* Dynamic race track height based on number of horses */
.race-track.track-1-horses {
    min-height: 200px;
}

.race-track.track-2-horses {
    min-height: 280px;
}

.race-track.track-3-horses {
    min-height: 320px;
}

.race-track.track-4-horses {
    min-height: 360px;
}

.race-track.track-5-horses {
    min-height: 400px;
}

.race-track.track-6-horses {
    min-height: 420px;
}

.race-track.track-7-horses {
    min-height: 440px;
}

.race-track.track-8-horses {
    min-height: 460px;
}

.finish-line {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 3em;
    z-index: 10;
}

#horses-container {
    position: relative;
    height: 100%;
}

.horse-lane {
    display: flex;
    align-items: center;
    margin: 15px 0;
    position: relative;
    min-height: 50px;
    background: rgba(255, 255, 255, 0.22);
    border-radius: 25px;
    padding: 5px;
    border: 1px solid rgba(255,255,255,0.55);
}

/* Dynamic lane sizing based on number of horses */
.horses-container-1 .horse-lane {
    min-height: 120px;
    margin: 30px 0;
}

.horses-container-2 .horse-lane {
    min-height: 100px;
    margin: 25px 0;
}

.horses-container-3 .horse-lane {
    min-height: 80px;
    margin: 20px 0;
}

.horses-container-4 .horse-lane {
    min-height: 70px;
    margin: 18px 0;
}

.horses-container-5 .horse-lane {
    min-height: 60px;
    margin: 15px 0;
}

.horses-container-6 .horse-lane {
    min-height: 55px;
    margin: 12px 0;
}

.horses-container-7 .horse-lane {
    min-height: 50px;
    margin: 10px 0;
}

.horses-container-8 .horse-lane {
    min-height: 45px;
    margin: 8px 0;
}

.horse {
    position: absolute;
    left: 0;
    width: 54px;
    height: 66px;
    transition: none; /* position is driven by requestAnimationFrame */
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Horse SVG head container */
.horse-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    filter: drop-shadow(2px 3px 5px rgba(0,0,0,0.4));
    user-select: none;
}

/* horse-color-X — colours are baked into the SVG horse head */

/* Gallop animation — head nods and bobs as the horse runs */
.horse-running {
    animation: gallop 0.38s infinite;
    transform-origin: 50% 85%; /* pivot near the neck */
}

@keyframes gallop {
    0%, 100% { transform: translateY(0px)  rotate(0deg);  }
    20%       { transform: translateY(-5px) rotate(-7deg); }
    40%       { transform: translateY(-2px) rotate(1deg);  }
    60%       { transform: translateY(-4px) rotate(6deg);  }
    80%       { transform: translateY(-1px) rotate(-1deg); }
}

/* Accident */
.horse.horse-accident {
    animation: none !important;
}

.horse.horse-accident .horse-icon {
    filter: grayscale(70%) opacity(0.5);
    transform: rotate(25deg);
    transition: filter 0.3s, transform 0.3s;
}

.accident-popup {
    position: absolute;
    right: calc(100% + 10px);
    top: 50%;
    transform: translateY(-50%);
    background: #fff;
    color: #b03030;
    font-size: 0.7em;
    font-weight: bold;
    padding: 4px 10px;
    border-radius: 8px;
    white-space: nowrap;
    z-index: 20;
    pointer-events: none;
    border: 1px solid rgba(200, 60, 60, 0.5);
    box-shadow: 0 2px 10px rgba(200, 0, 0, 0.25);
    animation: accidentPopup 0.4s cubic-bezier(0.22, 1.5, 0.36, 1);
}

.accident-popup::after {
    content: '';
    position: absolute;
    left: 100%;
    top: 50%;
    transform: translateY(-50%);
    border: 5px solid transparent;
    border-left-color: #fff;
}

@keyframes accidentPopup {
    from { opacity: 0; transform: translateY(-50%) scale(0.6); }
    to   { opacity: 1; transform: translateY(-50%) scale(1); }
}

.result-accident-label {
    color: #c0392b;
    font-size: 0.82em;
    font-weight: bold;
    margin-left: 4px;
}

/* Winner pulse on the icon */
.horse.winner .horse-icon {
    animation: winnerPulse 0.7s infinite;
}

@keyframes winnerPulse {
    0%, 100% { transform: scale(1);    filter: drop-shadow(2px 3px 5px rgba(0,0,0,0.3)); }
    50%       { transform: scale(1.18); filter: drop-shadow(0 0 14px rgba(254,198,1,0.9)) drop-shadow(0 0 22px rgba(234,115,23,0.7)) drop-shadow(2px 3px 5px rgba(0,0,0,0.3)); }
}

.horse-name {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    font-weight: bold;
    color: #2364aa;
    background: rgba(254, 198, 1, 0.92);
    padding: 4px 10px;
    border-radius: 12px;
    font-size: 0.85em;
    font-family: Georgia, serif;
    letter-spacing: 0.5px;
    border: 1px solid rgba(234, 115, 23, 0.4);
}

/* winner animation moved to .horse.winner .horse-icon above */

.finish-badge {
    position: absolute;
    top: -22px;
    left: 50%;
    transform: translateX(-50%);
    background: #2364aa;
    color: #ffffff;
    font-size: 0.65em;
    font-weight: bold;
    padding: 2px 7px;
    border-radius: 8px;
    white-space: nowrap;
    font-family: Georgia, serif;
    pointer-events: none;
    border: 1px solid rgba(254, 198, 1, 0.6);
}

#race-status {
    text-align: center;
    margin-top: 20px;
    font-size: 1.1em;
    font-weight: bold;
    color: #2364aa;
    font-family: Georgia, serif;
    letter-spacing: 0.5px;
}

/* Results Phase */
.results-container {
    text-align: center;
}

.results-buttons {
    display: flex;
    gap: 14px;
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 10px;
}

.results-buttons button {
    min-width: 150px;
    flex: 1 1 0;
}

.results-container h2 {
    color: #2364aa;
    margin-bottom: 30px;
    font-size: 2.2em;
    font-family: Georgia, serif;
    font-weight: bold;
    letter-spacing: 2px;
    text-transform: uppercase;
}

#winner-announcement {
    background: #2364aa;
    color: #ffffff;
    padding: 24px 20px;
    border-radius: 10px;
    margin-bottom: 30px;
    font-size: 1.5em;
    font-weight: bold;
    font-family: Georgia, serif;
    letter-spacing: 1px;
    border: 3px solid #fec601;
    box-shadow: 0 6px 24px rgba(35, 100, 170, 0.4);
}

#final-positions {
    background: #eef8f7;
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 30px;
    border: 1px solid #73bfb8;
}

.position-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    margin: 5px 0;
    background: #ffffff;
    border-radius: 6px;
    font-weight: bold;
    font-family: Georgia, serif;
    color: #2364aa;
    border: 1px solid rgba(115, 191, 184, 0.4);
}

.result-horse-name {
    font-size: 1.05em;
}

.result-horse-info {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 0.9em;
    color: inherit;
}

/* 1st — yellow */
.position-item:nth-child(2) {
    background: #fec601;
    color: #2364aa;
    border-color: #ea7317;
}

/* 2nd — sky blue */
.position-item:nth-child(3) {
    background: #3da5d9;
    color: #ffffff;
    border-color: #2364aa;
}

/* 3rd — orange */
.position-item:nth-child(4) {
    background: #ea7317;
    color: #ffffff;
    border-color: #c0550e;
}

/* Dynamic layout adjustments for different numbers of horses */
#horse-inputs.horses-1-2 {
    max-width: 800px;
    margin: 0 auto 30px auto;
}

/* Map selector */
.map-selector-section {
    text-align: center;
    margin-bottom: 26px;
}

.map-selector-section h4 {
    color: #2364aa;
    font-size: 1.05em;
    font-family: Georgia, serif;
    font-weight: bold;
    margin-bottom: 12px;
    letter-spacing: 0.5px;
}

.map-choices {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
}

.map-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    padding: 10px 14px;
    border-radius: 10px;
    border: 2px solid #73bfb8;
    background: #fff;
    cursor: pointer;
    user-select: none;
    width: 105px;
    flex: 0 0 105px;
    box-sizing: border-box;
    transition: border-color 0.15s, transform 0.12s, background 0.15s;
}

.map-card:hover {
    transform: translateY(-2px);
    border-color: #2364aa;
}

.map-card.selected {
    border-color: #2364aa;
    background: #eef8f7;
    box-shadow: 0 0 0 2px rgba(35, 100, 170, 0.18);
}

.map-card-emoji {
    font-size: 1.5em;
    line-height: 1;
}

.map-card-name {
    font-size: 0.8em;
    font-weight: bold;
    font-family: Georgia, serif;
    color: #2364aa;
    white-space: nowrap;
}

.map-card-swatch {
    width: 64px;
    height: 7px;
    border-radius: 4px;
    margin-top: 2px;
}

#horse-inputs.horses-3-4 {
    max-width: 900px;
    margin: 0 auto 30px auto;
}

#horse-inputs.horses-5-6 {
    max-width: 1000px;
    margin: 0 auto 30px auto;
}

#horse-inputs.horses-7-8 {
    max-width: 1100px;
    margin: 0 auto 30px auto;
}

#horse-inputs.horses-9-10 {
    max-width: 1150px;
    margin: 0 auto 30px auto;
}

#horse-inputs.horses-11-12 {
    max-width: 1200px;
    margin: 0 auto 30px auto;
}

/* Responsive Design */
@media (max-width: 1200px) {
    .horse-input {
        min-width: 250px;
        max-width: 350px;
    }
}

@media (max-width: 768px) {
    .container {
        margin: 10px;
        border-radius: 10px;
    }
    
    header h1 {
        font-size: 2em;
    }
    
    .horse-count-selection {
        flex-direction: column;
        gap: 10px;
    }
    
    #horse-inputs {
        flex-direction: column;
        align-items: center;
    }
    
    .horse-input {
        min-width: 260px;
        max-width: 100%;
        width: 100%;
    }
    
    .race-track {
        min-height: 300px;
    }
    
    /* Adjust lane sizes for mobile */
    .horses-container-1 .horse-lane { min-height: 80px; margin: 20px 0; }
    .horses-container-2 .horse-lane { min-height: 70px; margin: 18px 0; }
    .horses-container-3 .horse-lane { min-height: 60px; margin: 15px 0; }
    .horses-container-4 .horse-lane { min-height: 55px; margin: 12px 0; }
    .horses-container-5 .horse-lane { min-height: 50px; margin: 10px 0; }
    .horses-container-6 .horse-lane { min-height: 45px; margin: 8px 0; }
    .horses-container-7 .horse-lane { min-height: 40px; margin: 6px 0; }
    .horses-container-8 .horse-lane { min-height: 35px; margin: 5px 0; }
    
    .horse {
        width: 44px;
        height: 54px;
    }

    .horse-icon svg {
        width: 42px;
        height: 50px;
    }
    
    .finish-line {
        font-size: 2em;
    }
}

@media (max-width: 480px) {
    .horse-input {
        flex-direction: column;
        gap: 5px;
        text-align: center;
        min-width: 200px;
    }
    
    .horse-input label {
        white-space: normal;
        font-size: 0.85em;
    }
    
    .horse-input input {
        width: 100%;
        text-align: center;
    }
}

/* Utility Classes */
.hidden {
    display: none !important;
}

.fade-in {
    animation: fadeIn 0.5s ease-in;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}