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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #f0f2f5;
    color: #1a1a2e;
    min-height: 100vh;
}

header {
    background: #1a1a2e;
    color: #fff;
    padding: 1rem 2rem;
    display: flex;
    align-items: center;
    gap: 2rem;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

header h1 {
    font-size: 1.4rem;
    font-weight: 700;
    letter-spacing: 0.05em;
}

.controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.controls label {
    font-size: 0.9rem;
    opacity: 0.8;
}

.controls select {
    padding: 0.4rem 0.8rem;
    border-radius: 6px;
    border: none;
    font-size: 0.9rem;
    background: #fff;
    cursor: pointer;
}

.app-layout {
    display: flex;
    max-width: 1400px;
    margin: 0 auto;
    gap: 1rem;
    padding: 1rem;
}

.sidebar {
    width: 280px;
    flex-shrink: 0;
    position: sticky;
    top: 1rem;
    align-self: flex-start;
    max-height: calc(100vh - 80px);
    overflow-y: auto;
}

.sidebar-calc {
    background: #1a1a2e;
    color: #fff;
    border-radius: 10px;
    padding: 1rem;
    margin-bottom: 0.75rem;
    font-size: 0.85rem;
    line-height: 1.6;
}

.sidebar-calc .calc-formula {
    font-family: monospace;
    word-break: break-all;
}

.sidebar-calc .calc-result {
    font-size: 1.3rem;
    font-weight: 700;
    margin-top: 0.5rem;
    color: #4ade80;
}

.sidebar-list {
    background: #fff;
    border-radius: 10px;
    padding: 1rem;
    box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}

.sidebar-list h4 {
    font-size: 0.8rem;
    text-transform: uppercase;
    color: #888;
    margin-bottom: 0.5rem;
    letter-spacing: 0.05em;
}

.sidebar-race {
    margin-bottom: 0.75rem;
}

.sidebar-race .race-label {
    font-weight: 600;
    font-size: 0.85rem;
    color: #1a1a2e;
    margin-bottom: 0.25rem;
}

.sidebar-race .horse-entry {
    font-size: 0.8rem;
    color: #555;
    padding: 2px 0 2px 0.75rem;
    border-left: 3px solid #4ade80;
}

.sidebar-empty {
    font-size: 0.8rem;
    color: #aaa;
    text-align: center;
    padding: 1rem 0;
}

main {
    flex: 1;
    min-width: 0;
    margin: 1rem 0;
}

.empty-state {
    text-align: center;
    padding: 4rem;
    color: #888;
    font-size: 1.1rem;
}

.game-info {
    background: #fff;
    border-radius: 10px;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}

.game-info h2 {
    font-size: 1.2rem;
    margin-bottom: 0.5rem;
}

.game-info .meta {
    display: flex;
    gap: 2rem;
    font-size: 0.9rem;
    color: #555;
}

.tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 0;
    flex-wrap: wrap;
}

.tab {
    padding: 0.6rem 1.2rem;
    background: #ddd;
    border: none;
    border-radius: 8px 8px 0 0;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    transition: background 0.15s;
}

.tab:hover {
    background: #ccc;
}

.tab.active {
    background: #fff;
    color: #1a1a2e;
    font-weight: 600;
}

.tab-content {
    background: #fff;
    border-radius: 0 10px 10px 10px;
    padding: 1.5rem;
    box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}

.race-header {
    margin-bottom: 1rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid #eee;
}

.race-header h3 {
    font-size: 1.1rem;
    margin-bottom: 0.4rem;
}

.race-header .race-meta {
    display: flex;
    gap: 1.5rem;
    font-size: 0.85rem;
    color: #666;
    flex-wrap: wrap;
}

.race-header .race-meta span {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

thead th {
    text-align: left;
    padding: 0.6rem 0.8rem;
    border-bottom: 2px solid #e0e0e0;
    font-weight: 600;
    color: #444;
    white-space: nowrap;
}

tbody tr {
    transition: background 0.15s;
    cursor: pointer;
}

tbody tr:hover {
    background: #f7f8fa;
}

tbody tr.selected {
    background: #d1fae5;
}

tbody tr.selected:hover {
    background: #bbf7d0;
}

tbody tr.scratched {
    cursor: default;
}

tbody td {
    padding: 0.5rem 0.8rem;
    border-bottom: 1px solid #f0f0f0;
}

.pos-cell {
    font-weight: 700;
    text-align: center;
    width: 40px;
}

.horse-name {
    font-weight: 600;
}

.badge {
    display: inline-block;
    font-size: 0.65rem;
    font-weight: 700;
    padding: 1px 5px;
    border-radius: 3px;
    margin-left: 6px;
    vertical-align: middle;
    text-decoration: none;
}

.badge-foreign {
    background: #dbeafe;
    color: #1e40af;
}

.badge-home {
    background: #d1fae5;
    color: #065f46;
}

.badge-change {
    background: #fef3c7;
    color: #92400e;
    margin-left: 0;
    margin-right: 4px;
    cursor: help;
}

.age-cell {
    text-align: center;
    font-variant-numeric: tabular-nums;
    color: #555;
}

.changes-cell {
    white-space: nowrap;
    font-size: 0.7rem;
}

.driver-name {
    color: #555;
}

.trainer-name {
    color: #777;
    font-size: 0.8rem;
}

tr.scratched {
    background: #fef2f2;
    color: #b91c1c;
}

tr.scratched td {
    text-decoration: line-through;
    opacity: 0.7;
}

tr.scratched td.pos-cell {
    text-decoration: none;
}

tr.scratched:hover {
    background: #fee2e2;
}

.loading {
    text-align: center;
    padding: 2rem;
    color: #888;
}

.nav {
    display: flex;
    gap: 0.5rem;
    margin-left: 1rem;
}

.nav-link {
    color: rgba(255,255,255,0.7);
    text-decoration: none;
    padding: 0.4rem 0.8rem;
    border-radius: 6px;
    font-size: 0.9rem;
    transition: background 0.15s;
}

.nav-link:hover {
    background: rgba(255,255,255,0.1);
    color: #fff;
}

.nav-link.active {
    background: rgba(255,255,255,0.15);
    color: #fff;
    font-weight: 600;
}

.refresh-btn {
    padding: 0.4rem 0.9rem;
    border-radius: 6px;
    border: none;
    background: #4ade80;
    color: #1a1a2e;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
}

.refresh-btn:hover {
    background: #22c55e;
}

.refresh-btn:disabled {
    opacity: 0.6;
    cursor: progress;
}

.sync-status {
    font-size: 0.8rem;
    color: #9aa;
}

.spinner {
    display: inline-block;
    width: 0.9rem;
    height: 0.9rem;
    border: 2px solid rgba(74, 222, 128, 0.3);
    border-top-color: #4ade80;
    border-radius: 50%;
    vertical-align: middle;
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.stats-main {
    max-width: 1200px;
    margin: 1rem auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.stats-card {
    background: #fff;
    border-radius: 10px;
    padding: 1.5rem;
    box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}

.stats-card h2 {
    font-size: 1.1rem;
    margin-bottom: 1rem;
    color: #1a1a2e;
}

.stats-card h3.sub {
    font-size: 0.95rem;
    margin: 1rem 0 0.5rem;
    color: #444;
}

.stats-card .lead {
    font-size: 1rem;
    line-height: 1.5;
    margin-bottom: 0.5rem;
}

.stats-card .meta {
    font-size: 0.85rem;
    color: #666;
}

.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 0.75rem;
}

.kpi {
    display: flex;
    flex-direction: column;
    background: #f5f7fa;
    border-radius: 8px;
    padding: 0.75rem;
    text-align: center;
}

.kpi-num {
    font-size: 1.2rem;
    font-weight: 700;
    color: #1a1a2e;
}

.kpi-lbl {
    font-size: 0.75rem;
    color: #666;
    margin-top: 0.2rem;
}

.bar-table .bar-cell {
    position: relative;
    display: flex;
    align-items: center;
    height: 20px;
    min-width: 80px;
}

.bar-table .bar {
    background: linear-gradient(90deg, #4ade80, #22c55e);
    height: 16px;
    border-radius: 3px;
    position: absolute;
    left: 0;
    top: 2px;
    opacity: 0.6;
}

.bar-table .bar-cell span {
    position: relative;
    z-index: 1;
    padding-left: 6px;
    font-weight: 600;
}

.duo {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.duo-block table {
    width: 100%;
}

.stats-card tr.highlight {
    background: #fef9c3;
}

.stats-card .muted {
    color: #888;
    font-size: 0.8rem;
    font-weight: normal;
}

.roi-pos {
    color: #047857;
}

.roi-neg {
    color: #b91c1c;
}

.picks-main {
    max-width: 1200px;
    margin: 1rem auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.picks-summary {
    margin-top: 1rem;
}

.summary-table {
    width: 100%;
    background: #f8fafc;
    border-radius: 8px;
    padding: 0.5rem;
    font-family: -apple-system, "SF Mono", Menlo, monospace;
}

.summary-table tr td {
    padding: 0.4rem 0.6rem;
    border-bottom: 1px solid #e5e7eb;
}

.summary-table tr:last-child td {
    border-bottom: none;
}

.rad-label {
    font-weight: 700;
    color: #1a1a2e;
    width: 80px;
}

.rad-meta {
    color: #888;
    font-size: 0.8rem;
    width: 130px;
}

.rad-nums {
    font-weight: 600;
    color: #047857;
    font-size: 1rem;
    letter-spacing: 0.05em;
}

.rad-count {
    color: #888;
    font-size: 0.8rem;
    text-align: right;
}

.picks-detail .race-block {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid #f0f0f0;
}

.picks-detail h4 {
    font-size: 0.95rem;
    margin-bottom: 0.5rem;
    color: #333;
}

.picks-detail-table {
    width: 100%;
    font-size: 0.85rem;
}

.picks-detail-table tbody tr {
    cursor: default;
}

.picks-detail-table .rank-diff {
    color: #b45309;
    font-weight: 600;
}

.score-table .score-cell {
    text-align: right;
    color: #6b7280;
    font-variant-numeric: tabular-nums;
}

.score-table .score-cell .pts {
    display: block;
    font-weight: 600;
    color: #374151;
}

.score-table .score-cell .raw {
    display: block;
    font-size: 0.78em;
    color: #9aa;
}

.score-table .score-total {
    text-align: right;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.score-table tbody tr:not(.scratched) {
    cursor: pointer;
}

/* User's manual one-per-race pick — own colour, distinct from the green
   recommended-row (.selected) highlight. Higher specificity so it wins. */
.score-table tbody tr.user-pick,
.score-table tbody tr.user-pick.selected {
    background: #fdba74;
}

.score-table tbody tr.user-pick:hover {
    background: #fb923c;
}

.score-col {
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
}

.score-col .muted {
    color: #888;
    font-size: 0.85em;
}

#budget-input {
    color: #1a1a2e;
}

@media (max-width: 768px) {
    .duo {
        grid-template-columns: 1fr;
    }

    .kpi-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .stats-card {
        padding: 1rem;
    }
}

@media (max-width: 768px) {
    .app-layout {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        position: static;
        max-height: none;
    }

    header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }

    .game-info .meta {
        flex-direction: column;
        gap: 0.3rem;
    }

    table {
        font-size: 0.78rem;
    }

    thead th, tbody td {
        padding: 0.4rem;
    }
}
