.table-wrapper {
    width: 100%;
    overflow-x: auto;
    /* This makes the table scroll horizontally BY DEFAULT */
    background-color: var(--color-white);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.race-results-table {
    width: 100%;
    min-width: 1600px;
    /* Force scrolling on smaller screens */
    border-collapse: collapse;
}

/* UPDATED HEADER STYLES */
.race-results-table th {
    background-color: var(--color-primary);
    color: var(--color-white);
    padding: 1rem 0.5rem;
    /* Increased padding */
    text-align: left;
    font-size: 0.85rem;
    text-transform: uppercase;
    font-weight: 800;
    /* Extra Bold */
    letter-spacing: 0.5px;
    white-space: nowrap;
}

.race-results-table tbody tr {
    transition: background-color 0.2s ease;
}

/* Darker zebra striping */
.race-results-table tbody tr:nth-child(even) {
    background-color: #f2f4f6;
}

.race-results-table tbody tr:hover {
    background-color: #e8f5e9;
    /* Light green hover */
}

/* UPDATED CELL STYLES */
.race-results-table td {
    padding: 0.85rem 0.5rem;
    font-size: 0.92rem;
    /* Larger font */
    color: #000000;
    /* Pure black text */
    font-weight: 500;
    /* Thicker weight */
    border-bottom: 1px solid #e0e0e0;
    white-space: nowrap;
    vertical-align: middle;
    line-height: 1.4;
    font-feature-settings: "tnum";
    font-variant-numeric: tabular-nums;
}

.race-results-table td strong {
    color: #000;
    font-weight: 700;
}

/* Highlight Horse Name (Column 3) */
.race-results-table td:nth-child(3) {
    font-weight: 700;
    /* Bold */
    color: var(--color-primary);
    font-size: 1rem;
}

.race-results-table td a {
    color: var(--color-primary);
    text-decoration: none;
    font-weight: 600;
}


/* Apply strike-through ONLY to scratched */
tr.is-scratched {
    opacity: 0.6;
    background-color: #f8dbdc !important;
}

/* Apply strikethrough ONLY to the horse name (Column 3) */
tr.is-scratched td:nth-child(3),
tr.is-scratched td:nth-child(3) * {
    text-decoration: line-through;
}

/* SCRATCHED Watermark overlay removed */

/* Other non-finishers are just faded/highlighted, NOT strikethrough */
tr.is-dnf,
tr.is-refused,
tr.is-rtl,
tr.is-dq {
    opacity: 0.6;
    background-color: #f8dbdc !important;
}

tr.is-dnf td,
tr.is-refused td,
tr.is-rtl td,
tr.is-dq td {
    text-decoration: none !important;
}



/* --- Desktop Table Logic (1200px and up) --- */
@media (min-width: 1200px) {
    .table-wrapper {
        overflow-x: hidden;
        /* Disable horizontal scroll on desktop */
    }

    .race-results-table {
        table-layout: fixed;
        /* Force table to fit 100% width */
        min-width: 100%;
        /* Override the min-width */
    }

    .race-results-table th,
    .race-results-table td {
        white-space: normal;
        /* Allow text to wrap */
        word-break: break-word;
        /* Break long words */
    }

    /* --- Set specific widths for 14 columns --- */

    /* 1. Finish */
    .race-results-table th:nth-child(1),
    .race-results-table td:nth-child(1) {
        width: 5%;
    }

    /* 2. No */
    .race-results-table th:nth-child(2),
    .race-results-table td:nth-child(2) {
        width: 4%;
    }

    /* 3. Horse Name */
    .race-results-table th:nth-child(3),
    .race-results-table td:nth-child(3) {
        width: 15%;
    }

    /* 4. Rating */
    .race-results-table th:nth-child(4),
    .race-results-table td:nth-child(4) {
        width: 5%;
    }

    /* 5. Horse Wt */
    .race-results-table th:nth-child(5),
    .race-results-table td:nth-child(5) {
        width: 6%;
    }

    /* 6. Rider Wt */
    .race-results-table th:nth-child(6),
    .race-results-table td:nth-child(6) {
        width: 6%;
    }

    /* 7. Jockey */
    .race-results-table th:nth-child(7),
    .race-results-table td:nth-child(7) {
        width: 12%;
    }

    /* 8. Act Wt */
    .race-results-table th:nth-child(8),
    .race-results-table td:nth-child(8) {
        width: 6%;
    }

    /* 9. Trainer */
    .race-results-table th:nth-child(9),
    .race-results-table td:nth-child(9) {
        width: 12%;
    }

    /* 10. Bar */
    .race-results-table th:nth-child(10),
    .race-results-table td:nth-child(10) {
        width: 4%;
    }

    /* 11. 800m */
    .race-results-table th:nth-child(11),
    .race-results-table td:nth-child(11) {
        width: 5%;
    }

    /* 12. 400m */
    .race-results-table th:nth-child(12),
    .race-results-table td:nth-child(12) {
        width: 5%;
    }

    /* 13. Finish Time */
    .race-results-table th:nth-child(13),
    .race-results-table td:nth-child(13) {
        width: 8%;
    }

    /* 14. Margin */
    .race-results-table th:nth-child(14),
    .race-results-table td:nth-child(14) {
        width: 7%;
    }
}

/* Column highlighting */
.race-results-table td:nth-child(2) {
    background-color: #e8f5e9 !important; /* Subtle Green */
}

.race-results-table td:nth-child(11),
.race-results-table td:nth-child(12) {
    background-color: #fffde7 !important; /* Subtle Yellow */
}

.race-results-table td:nth-child(13) {
    background-color: #e8f5e9 !important; /* Subtle Green */
}

.race-results-table td:nth-child(14) {
    background-color: #fffde7 !important; /* Subtle Yellow */
}




/*
 * NEW RESPONSIVE STYLES
 */
@media (max-width: 768px) {
    .race-title-bar {
        flex-direction: column;
        align-items: stretch;
    }

    .prev-results-nav {
        order: 2;
        /* Move it below the title */
        margin: 0.75rem 0 0.5rem 0;
        /* Adjust margin for mobile */
        width: 100%;
        min-width: 0;
    }

    .race-date {
        order: 1;
        /* Move date above the nav */
        text-align: left;
    }
}


/*
 * NEW STYLES FOR PREVIOUS RESULTS NAV
 */
.race-title-bar {
    display: flex;
    /* justify-content: space-between; <-- REMOVED */
    align-items: center;
    flex-wrap: wrap;
    /* Allow wrapping on small screens */
}

.prev-results-nav {
    display: flex;
    align-items: center;
    margin: 0 1rem 0 auto;
    /* <-- UPDATED: top right bottom left */
    /* Add some space */
    /* flex-grow: 1; <-- REMOVED */
    /* Allow it to take up space */
    /* min-width: 250px; <-- REMOVED */
    /* Prevent it from getting too small */
}

.prev-results-nav .form-control-sm {
    padding: 0.375rem 0.75rem;
    font-size: 0.9rem;
    border-radius: 0.25rem;
    border: 1px solid #ced4da;
    background-color: #fff;
    color: #495057;
    height: 38px;
    flex-grow: 1;
    /* Allow select to grow */
}

.prev-results-nav .btn-go {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    width: 38px;
    height: 38px;
    border: 1px solid var(--color-primary);
    background-color: var(--color-primary);
    color: var(--color-white);
    border-radius: 0.25rem;
    margin-left: 0.5rem;
    cursor: pointer;
    font-size: 1rem;
    transition: background-color 0.2s ease, border-color 0.2s ease;
}

.prev-results-nav .btn-go:hover {
    background-color: var(--color-primary-dark);
    border-color: var(--color-primary-dark);
}

/* ----------------------------------------------------
   NEW RACE INFO BOX STYLES (Grid Layout)
   ---------------------------------------------------- */

.race-info-box {
    display: flex;
    flex-direction: column;
    background-color: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.race-info-box.active {
    display: flex;
}

/* --- Top Row: Details & Track Image --- */
.race-info-top-row {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    justify-content: space-between;
    align-items: flex-start;
}

.race-info-text {
    flex: 1;
    min-width: 280px;
}

.race-info-track {
    flex: 0 0 auto;
    display: flex;
    justify-content: center;
}

/* --- Bottom Row: Media Section (CSS Grid) --- */
.race-media-section {
    display: grid;
    /* Creates equal width columns, auto-fitting available space */
    grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
    gap: 20px;
    margin-top: 20px;
    padding-top: 20px;
    border-top: 1px solid #eee;
    width: 100%;
}

.race-media-item {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
}

.media-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.85rem;
    font-weight: 700;
    color: #0A4D0A;
    /* Dark green */
    text-transform: uppercase;
    min-height: 24.5px;
}

/* --- Shared Media Container (Aspect Ratio Enforcement) --- */
.media-content-wrapper {
    position: relative;
    width: 100%;
    /* 16:9 Aspect Ratio (9 / 16 * 100 = 56.25%) */
    /* padding-bottom: 56.25%; */
    height: 400px;
    background-color: #000;
    border-radius: 6px;
    border: 1px solid #ddd;
    overflow: hidden;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    max-width: 600px;
    max-height: 400px;
}

/* Ensure contents fill the wrapper perfectly */
.media-content-wrapper iframe,
.media-content-wrapper video,
.media-content-wrapper img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: right;
    /* Keeps image looking good */
    border: 0;
    display: block;
}

/* Specific styling for Photo Finish overlay */
.photo-container {
    cursor: zoom-in;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.photo-container:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    border-color: var(--color-primary);
}

.pf-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    padding: 8px;
    text-align: center;
    font-size: 0.8rem;
    pointer-events: none;
    /* Let clicks pass through to container */
    backdrop-filter: blur(2px);
}

/* --- Photo Finish Zoom Modal --- */
.pf-modal {
    display: none;
    position: fixed;
    z-index: 2000;
    padding-top: 50px;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0, 0, 0, 0.9);
}

.pf-modal-content {
    margin: auto;
    display: block;
    max-width: 90%;
    max-height: 90vh;
    border-radius: 4px;
    box-shadow: 0 0 20px rgba(255, 255, 255, 0.2);
}

#pf-caption {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
    text-align: center;
    color: #ccc;
    padding: 10px 0;
    height: 150px;
}

.pf-close {
    position: absolute;
    top: 15px;
    right: 35px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
    cursor: pointer;
}

.pf-close:hover,
.pf-close:focus {
    color: #bbb;
    text-decoration: none;
    cursor: pointer;
}

/* Mobile Adjustments */
@media (max-width: 768px) {
    .race-media-section {
        grid-template-columns: 1fr;
        /* Stack vertically on mobile */
    }

    /* Force track image to center on its own line when wrapped */
    .race-info-top-row {
        justify-content: center;
    }
}

/* --- Video Tabs Styling --- */
.video-header-row {
    display: flex;
    justify-content: flex-start;
    /* Aligns to left */
    align-items: center;
    gap: 20px;
}

.video-tabs {
    display: flex;
    gap: 5px;
}

.video-tab-btn {
    background-color: #f8f9fa;
    border: 1px solid #ddd;
    border-radius: 4px;
    padding: 4px 12px;
    font-size: 0.8rem;
    cursor: pointer;
    font-weight: 600;
    color: #666;
    transition: all 0.2s ease;
}

.video-tab-btn:hover {
    background-color: #e2e2e2;
    color: #333;
}

.video-tab-btn.active {
    background-color: var(--color-primary);
    /* Your primary color */
    color: #fff;
    border-color: var(--color-primary);
}

/* --- FIXED IFRAME VISIBILITY --- */
/* We use !important or higher specificity to ensure we override the default 'display: block' */

.media-content-wrapper iframe.video-frame {
    display: none !important;
    width: 100%;
    height: 100%;
    z-index: 1;
    /* Default layer */
}

.media-content-wrapper iframe.video-frame.active {
    display: block !important;
    z-index: 10;
    /* Bring active video to top */
}

/* --- Calendar Widget Styles --- */

.calendar-widget-wrapper {
    position: relative;
    margin-left: auto;
    /* Push to right */
}

.btn-calendar-toggle {
    background-color: var(--color-primary);
    color: var(--color-white);
    border: 1px solid var(--color-primary);
    padding: 0.5rem 1rem;
    border-radius: 4px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    transition: background 0.2s;
}

.btn-calendar-toggle:hover {
    background-color: var(--color-green);
}

/* Floating Popover */
.calendar-popover {
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 10px;
    width: 300px;
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    z-index: 100;
    padding: 1rem;
}

.calendar-popover.hide {
    display: none;
}

/* Header */
.calendar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

#cal-month-year {
    font-weight: 700;
    color: var(--color-primary);
    text-transform: uppercase;
}

.cal-nav-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: #666;
    font-size: 1rem;
    padding: 5px;
}

.cal-nav-btn:hover {
    color: var(--color-primary);
}

/* Grid System */
.calendar-days-header {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    text-align: center;
    font-size: 0.75rem;
    font-weight: 700;
    color: #999;
    margin-bottom: 0.5rem;
}

.calendar-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 4px;
}

.cal-day {
    aspect-ratio: 1/1;
    /* Keeps them square */
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 0.9rem;
    color: #ccc;
    /* Default (inactive) color */
    border-radius: 4px;
    cursor: default;
    position: relative;
}

/* Days that have race events */
.cal-day.has-event {
    color: var(--color-text-dark);
    background-color: #f0f8f0;
    cursor: pointer;
    font-weight: 600;
}

.cal-day.has-event:hover {
    background-color: var(--color-secondary);
    color: #000;
}

/* The currently selected event (if matches page) */
.cal-day.is-selected {
    background-color: var(--color-primary);
    color: #fff;
}

/* Indicator dot for events */
.cal-day.has-event::after {
    content: '';
    position: absolute;
    bottom: 4px;
    width: 4px;
    height: 4px;
    background-color: var(--color-green);
    border-radius: 50%;
}

.cal-day.is-selected::after {
    background-color: #fff;
}

/* --- Race Controls Group (Calendar + Quick Link) --- */

/* Wrapper to group buttons on the right */
.race-controls-wrapper {
    display: flex;
    align-items: center;
    gap: 10px;
    /* Space between buttons */
    margin-left: auto;
    /* Push group to the far right */
}

/* Quick Link Button Style */
.btn-quick-link {
    background-color: var(--color-white);
    color: var(--color-primary);
    border: 1px solid var(--color-primary);
    padding: 0 1rem;
    /* Match height of calendar button */
    border-radius: 4px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
    /* It's an <a> tag, remove underline */
    height: 38px;
    /* Fixed height to match calendar btn */
    transition: all 0.2s ease;
}

.btn-quick-link:hover {
    background-color: var(--color-primary);
    color: var(--color-white);
}

/* Update Calendar Wrapper to remove auto-margin since the parent handles it */
.calendar-widget-wrapper {
    margin-left: 0;
    position: relative;
}

/* Mobile Responsiveness */
@media (max-width: 768px) {
    .race-controls-wrapper {
        width: 100%;
        flex-direction: column;
        /* Stack buttons on mobile */
        gap: 0.5rem;
        margin-top: 0.5rem;
    }

    .btn-quick-link,
    .calendar-widget-wrapper,
    .btn-calendar-toggle {
        width: 100%;
        justify-content: center;
    }

    /* Ensure popover is still positioned correctly */
    .calendar-popover {
        width: 100%;
    }
}

/* --- Dividend Section Styles --- */
.dividend-section {
    background-color: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.dividend-section.hide {
    display: none;
}

.dividend-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--color-primary);
}

.dividend-header h3 {
    margin: 0;
    font-size: 1.1rem;
    color: var(--color-primary);
    font-weight: 700;
    text-transform: uppercase;
    display: flex;
    align-items: center;
    gap: 8px;
}

.dividend-subtitle {
    font-size: 0.8rem;
    color: #666;
    font-style: italic;
}

/* Transition for smooth toggle */
.dividend-grid {
    display: grid;
    /* Responsive Grid: 
       - Mobile: 1 column
       - Tablet: 2 columns 
       - Desktop: 3 columns or more depending on space */
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 20px;
    max-height: 2000px;
    /* Arbitrary large height for transition */
    overflow: hidden;
    transition: max-height 0.5s ease-in-out, opacity 0.5s ease-in-out, margin-top 0.3s ease;
    opacity: 1;
}

.dividend-grid.collapsed {
    max-height: 0;
    opacity: 0;
    margin-top: 0;
    gap: 0;
    /* Remove gap when collapsed */
}

/* Icon rotation */
#dividend-toggle-icon {
    transition: transform 0.3s ease;
}

#dividend-toggle-icon.rotate {
    transform: rotate(180deg);
}

/* Individual Dividend Item Table */
.dividend-item-table {
    width: 100%;
    border-collapse: collapse;
    background-color: #fafafa;
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid #eee;
}

.dividend-item-table th,
.dividend-item-table td {
    padding: 8px 12px;
    text-align: left;
    font-size: 0.9rem;
}

.dividend-item-table th {
    background-color: var(--color-primary-light);
    /* Lighter green */
    color: #fff;
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.8rem;
    width: 30%;
    /* Label column width */
}

.dividend-item-table td {
    color: #333;
    font-weight: 500;
}

.dividend-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid #e0e0e0;
}

.dividend-row:last-child {
    border-bottom: none;
}

/* New Modern Card Layout for Dividends */
.div-card {
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.div-card-header {
    background-color: #f4f4f4;
    padding: 8px 12px;
    font-weight: 700;
    color: var(--color-text-dark);
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #ddd;
}

.div-type-badge {
    background-color: var(--color-primary);
    color: #fff;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
}

.div-card-body {
    padding: 10px 12px;
}

.div-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 4px;
    font-size: 0.9rem;
}

.div-row:last-child {
    margin-bottom: 0;
}

.div-label {
    color: #777;
    font-size: 0.8rem;
}

.div-value {
    font-weight: 600;
    color: #000;
    text-align: right;
}

.div-price {
    color: var(--color-primary);
    font-weight: 700;
}

/* Loading State for Dividends */
.dividend-loading {
    grid-column: 1 / -1;
    text-align: center;
    padding: 20px;
    color: #999;
    font-style: italic;
}