/* ─── CSS custom properties ─────────────────────────────────────────────────── */
:root {
    /* Colours */
    --bg-page:        #f4f3ef;
    --bg-card:        #ffffff;
    --bg-card-alt:    #f9f8f5;
    --text-primary:   #1e1e1e;
    --text-secondary: #5a5a5a;
    --text-muted:     #888;
    --border-color:   #dbd9d3;
    --link-color:     #1e3a5f;

    /* Brand */
    --nav-bg:         #1e3a5f;
    --nav-text:       #e8edf2;
    --nav-active:     #c9a84c;
    --accent-gold:    #c9a84c;
    --accent-blue:    #1e3a5f;

    /* Eval colours */
    --eval-decisive-white: #226ace;
    --eval-slight-white:   #226acec4;
    --eval-balanced:       #1b9d53e8;
    --eval-slight-black:   #db3333c4;
    --eval-decisive-black: #db3333;

    /* Board squares */
    --sq-light: #f0d9b5;
    --sq-dark:  #b58863;

    /* Misc */
    --radius:   0.5rem;
    --shadow:   0 2px 12px rgba(0,0,0,.08);
    --shadow-lg:0 4px 24px rgba(0,0,0,.12);

    /* Fonts */
    --font-body:   'Source Sans 3', system-ui, sans-serif;
    --font-head:   'Playfair Display', Georgia, serif;
    --font-mono:   'Roboto Mono', 'Courier New', monospace;
}

/* ─── Base ───────────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

html {
    scroll-behavior: smooth;
    font-size: 16px;
}

body {
    font-family: var(--font-body);
    background-color: var(--bg-page);
    color: var(--text-primary);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

h1, h2, h3, h4, h5, h6 {
    font-family: var(--font-head);
    color: var(--text-primary);
}

a { color: var(--link-color); }
a:hover { text-decoration: underline; }

p { text-align: justify; }

code, .mono { font-family: var(--font-mono); }

main.main-content {
    flex: 1;
    padding-bottom: 3rem;
}

main.main-content.no-pb {
    padding-bottom: 0;
}

/* ─── Navbar ─────────────────────────────────────────────────────────────────── */
.site-navbar {
    background-color: var(--nav-bg);
    padding: .75rem 0;
    box-shadow: 0 2px 8px rgba(0,0,0,.2);
}

.site-navbar .navbar-brand {
    font-family: var(--font-head);
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--nav-text) !important;
    letter-spacing: .02em;
    line-height: 1;
    display: flex;
    align-items: center;
}

.site-navbar .nav-link {
    color: rgba(232,237,242,.75) !important;
    font-weight: 400;
    font-size: .95rem;
    padding: .4rem .9rem;
    border-radius: var(--radius);
    transition: color .15s, background-color .15s;
}

.site-navbar .nav-link:hover,
.site-navbar .nav-link.active {
    color: var(--nav-active) !important;
    background-color: rgba(255,255,255,.06);
}

.site-navbar .navbar-toggler {
    border-color: rgba(232,237,242,.3);
}
.site-navbar .navbar-toggler-icon {
    filter: invert(1);
}

/* ─── Page hero ──────────────────────────────────────────────────────────────── */
.page-hero {
    background: linear-gradient(135deg, #2a5298 0%, #3a6bc4 100%);
    color: #fff;
    padding: 2rem 0;
    margin-bottom: 2.5rem;
}

.page-hero h1 {
    color: #fff;
    font-size: clamp(1.8rem, 4vw, 2.8rem);
}

.page-hero p {
    color: rgba(255,255,255,.82);
    font-size: 1.1rem;
    max-width: 680px;
}

/* ─── Cards ──────────────────────────────────────────────────────────────────── */
.card {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.card-header {
    background-color: var(--bg-card-alt);
    border-bottom: 1px solid var(--border-color);
    font-family: var(--font-head);
    font-weight: 600;
    font-size: 1rem;
    color: var(--text-primary);
}

/* ─── Stat cards (homepage) ──────────────────────────────────────────────────── */
.stat-card {
    text-align: center;
    padding: 1.5rem 1rem;
}

.stat-number {
    font-family: var(--font-mono);
    font-size: clamp(1.8rem, 4vw, 2.6rem);
    font-weight: 600;
    color: var(--accent-blue);
    line-height: 1.1;
}

.stat-label {
    font-size: .85rem;
    color: var(--text-secondary);
    margin-top: .3rem;
    text-transform: uppercase;
    letter-spacing: .06em;
}

/* ─── Section titles ─────────────────────────────────────────────────────────── */
.section-title {
    font-family: var(--font-head);
    font-size: 1.4rem;
    color: var(--accent-blue);
    border-left: 4px solid var(--accent-gold);
    padding-left: .75rem;
    margin-bottom: 1.25rem;
}

/* ─── Eval gauge ─────────────────────────────────────────────────────────────── */
.eval-gauge-wrap {
    margin: 1.25rem 0;
}

.eval-gauge-bar {
    height: 22px;
    border-radius: var(--radius);
    background: linear-gradient(to right, #1a1a1a 0%, #f0f0f0 100%);
    position: relative;
    overflow: hidden;
    box-shadow: inset 0 1px 3px rgba(0,0,0,.3);
}

.eval-gauge-marker {
    position: absolute;
    top: 0;
    height: 100%;
    width: 3px;
    background: var(--accent-gold);
    transform: translateX(-50%);
    transition: left .5s ease;
}

.eval-gauge-labels {
    display: flex;
    justify-content: space-between;
    font-size: .75rem;
    color: var(--text-muted);
    margin-top: .25rem;
    font-family: var(--font-mono);
}

.eval-result-value {
    font-family: var(--font-mono);
    font-size: 2rem;
    font-weight: 600;
    line-height: 1;
}

.eval-result-label {
    font-size: .9rem;
    color: var(--text-secondary);
    margin-top: .25rem;
}

/* Eval colour classes */
.eval-white-strong  { color: #226ace !important; }
.eval-white-slight  { color: var(--eval-slight-white) !important; }
.eval-balanced      { color: var(--eval-balanced) !important; }
.eval-black-slight  { color: var(--eval-slight-black) !important; }
.eval-black-strong  { color: #db3333 !important; }

/* ─── Chart legend ───────────────────────────────────────────────────────────── */
.chart-legend-item {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
}
.chart-legend-swatch {
    display: inline-block;
    width: .75rem;
    height: .75rem;
    border-radius: 2px;
    flex-shrink: 0;
    background: currentColor;
}

/* ─── Mini chessboard ────────────────────────────────────────────────────────── */
.mini-board-wrap {
    text-align: center;
}

.mini-board-label {
    font-size: .8rem;
    color: var(--text-secondary);
    margin-bottom: .3rem;
    text-transform: uppercase;
    letter-spacing: .05em;
}

.mini-board {
    display: inline-flex;
    border: 2px solid var(--border-color);
    border-radius: 4px;
    overflow: hidden;
}

.sq {
    width: 46px;
    height: 54px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    padding: 5px 2px 4px;
    position: relative;
}

.sq-light { background: var(--sq-light); }
.sq-dark  { background: var(--sq-dark); }

.piece {
    font-size: 1.8rem;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* SVG piece image */
.piece-img {
    width: 1.6em;
    height: 1.6em;
    display: block;
    object-fit: contain;
}

/* Unicode fallback — colour based on rank, not square, to avoid alternating effect */
.rank-white .piece-fallback {
    color: #f5f0de;
    text-shadow: 0 0 3px #000, 0 0 2px rgba(0,0,0,.9),
                 1px 1px 0 rgba(0,0,0,.6), -1px -1px 0 rgba(0,0,0,.6);
}
.rank-black .piece-fallback {
    color: #1a1a1a;
    text-shadow: 0 0 3px rgba(255,255,255,.95), 0 0 2px rgba(255,255,255,.8),
                 1px 1px 0 rgba(255,255,255,.5), -1px -1px 0 rgba(255,255,255,.5);
}

.file-label {
    font-size: .6rem;
    font-family: var(--font-mono);
    color: rgba(0,0,0,.45);
    line-height: 1;
}
.sq-dark .file-label { color: rgba(255,255,255,.55); }

/* Responsive board */
@media (max-width: 480px) {
    .sq { width: 36px; height: 44px; }
    .piece { font-size: 1.4rem; }
}

/* ─── Position code badge ────────────────────────────────────────────────────── */
.position-code {
    font-family: var(--font-mono);
    font-size: .95rem;
    background: var(--bg-card-alt);
    border: 1px solid var(--border-color);
    border-radius: .3rem;
    padding: .15rem .5rem;
    display: inline-block;
}

.position-code.classical {
    border-color: var(--accent-gold);
    color: var(--accent-blue);
}

/* ─── Tables ─────────────────────────────────────────────────────────────────── */
.table {
    color: var(--text-primary);
    border-color: var(--border-color);
}

.table th {
    font-family: var(--font-body);
    font-weight: 600;
    font-size: .8rem;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-secondary);
    border-bottom: 2px solid var(--border-color);
    background-color: var(--bg-card-alt);
}

.table td {
    vertical-align: middle;
    border-color: var(--border-color);
}

.table-striped > tbody > tr:nth-of-type(odd) > * {
    background-color: rgba(0,0,0,.025);
    color: var(--text-primary);
}

.table tbody tr:hover {
    background-color: rgba(30,58,95,.06);
    cursor: default;
}

/* Inline eval pill */
.eval-pill {
    display: inline-block;
    font-family: var(--font-mono);
    font-size: .82rem;
    font-weight: 600;
    padding: .15rem .5rem;
    border-radius: 1rem;
    min-width: 3.5rem;
    text-align: center;
}

.eval-pill.eval-white-strong { background: rgba(34,106,206,.15); color: #226ace !important; }
.eval-pill.eval-white-slight { background: rgba(34,106,206,.08); color: #226acec4 !important; }
.eval-pill.eval-balanced     { background: rgba(27,157,83,.10);  color: #1b9d53e8 !important; }
.eval-pill.eval-black-slight { background: rgba(219,51,51,.08);  color: #db3333c4 !important; }
.eval-pill.eval-black-strong { background: rgba(219,51,51,.15);  color: #db3333 !important; }

/* ─── Distribution chart container ─────────────────────────────────────────── */
.chart-container {
    position: relative;
    width: 100%;
    height: 360px;
}

/* ─── Tom Select overrides ───────────────────────────────────────────────────── */
.ts-wrapper .ts-control {
    background-color: var(--bg-card) !important;
    border-color: var(--border-color) !important;
    color: var(--text-primary) !important;
    font-family: var(--font-mono);
    font-size: .9rem;
}
.ts-wrapper.focus .ts-control { border-color: var(--accent-blue) !important; }
.ts-dropdown {
    background-color: var(--bg-card) !important;
    border-color: var(--border-color) !important;
    color: var(--text-primary) !important;
    font-family: var(--font-mono);
    font-size: .9rem;
}
.ts-dropdown .option:hover,
.ts-dropdown .option.active { background-color: var(--accent-blue) !important; color: #fff !important; }

/* ─── Full-width board preview (matchup page) ────────────────────────────────── */
.board-preview-wrap .mini-board {
    display: flex;
    width: 100%;
    border-radius: var(--radius);
    overflow: hidden;
    border: 2px solid var(--border-color);
}
.board-preview-wrap .sq {
    flex: 1;
    min-width: 0;
    height: 56px;
    padding: 5px 2px 4px;
}
.board-preview-wrap .piece-img  { width: 1.5em; height: 1.5em; }
.board-preview-wrap .piece      { font-size: 1.7rem; }
.board-preview-wrap .file-label { font-size: .58rem; }

/* ─── Position builder (matchup page interactive board) ─────────────────────── */
.builder-board {
    display: inline-flex;
    border: 2px solid var(--border-color);
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;
    user-select: none;
}

.builder-sq {
    width: 54px;
    height: 62px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    padding: 6px 2px 4px;
    position: relative;
    transition: outline .1s;
}

.builder-sq .piece { font-size: 2.1rem; }

.builder-sq.selected {
    outline: 3px solid var(--accent-gold);
    outline-offset: -3px;
    z-index: 1;
}

.builder-status {
    font-size: .8rem;
    min-height: 1.1rem;
}
.builder-status.valid   { color: var(--eval-balanced); }
.builder-status.invalid { color: var(--eval-slight-black); }

.builder-instruction {
    font-size: .85rem;
    color: var(--text-muted);
    font-style: italic;
}

@media (max-width: 480px) {
    .builder-sq { width: 40px; height: 48px; padding: 4px 1px 3px; }
    .builder-sq .piece { font-size: 1.5rem; }
}

/* ─── Matchup result panel ───────────────────────────────────────────────────── */
#matchupResult {
    display: none;
    animation: fadeIn .3s ease;
}

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

/* ─── Explorer rank indicator ───────────────────────────────────────────────── */
.rank-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.8rem;
    height: 1.8rem;
    border-radius: 50%;
    font-size: .75rem;
    font-weight: 600;
    font-family: var(--font-mono);
    background: var(--bg-card-alt);
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
}

/* ─── Highlight row for classical position ──────────────────────────────────── */
.row-classical {
    outline: 2px solid var(--accent-gold);
    outline-offset: -2px;
}

/* ─── About page sections ────────────────────────────────────────────────────── */
.about-section {
    padding: 1.75rem 0;
    border-bottom: 1px solid var(--border-color);
}
.about-section:last-child { border-bottom: none; }

.about-section h2 {
    display: flex;
    align-items: center;
    gap: .5rem;
}

.about-icon {
    font-size: 1.5rem;
    line-height: 1;
    flex-shrink: 0;
}

/* ─── Legal page ─────────────────────────────────────────────────────────────── */
.legal-section {
    padding: 2.5rem 0;
    border-bottom: 3px solid var(--border-color);
}
.legal-section:last-child { border-bottom: none; }

.legal-content h2 {
    font-size: 1.7rem;
    font-weight: 700;
    color: var(--accent-blue);
    margin-bottom: 1.5rem;
    padding-bottom: .6rem;
    border-bottom: 2px solid var(--accent-gold);
    display: inline-block;
}

.legal-content h3 {
    font-size: 1rem;
    font-weight: 600;
    margin-top: 1.5rem;
    margin-bottom: .4rem;
    color: var(--text-primary);
}

.legal-content p, .legal-content li {
    color: var(--text-secondary);
    font-size: .95rem;
    line-height: 1.7;
}

/* ─── Alerts / notices ───────────────────────────────────────────────────────── */
.notice-banner {
    background: rgba(201,168,76,.12);
    border: 1px solid rgba(201,168,76,.4);
    border-radius: var(--radius);
    padding: .75rem 1rem;
    font-size: .875rem;
    color: var(--text-secondary);
}

/* ─── Footer ─────────────────────────────────────────────────────────────────── */
.site-footer {
    background-color: var(--nav-bg);
    color: rgba(232,237,242,.7);
    padding: .85rem 0;
    margin-top: auto;
    font-size: .875rem;
}

.footer-legal-link {
    color: rgba(232,237,242,.65);
    text-decoration: none;
    font-size: .85rem;
    transition: color .15s;
}
.footer-legal-link:hover { color: var(--nav-active); text-decoration: none; }

.footer-sep {
    color: rgba(232,237,242,.35);
    font-size: .85rem;
}

.footer-credit {
    color: rgba(232,237,242,.55);
    font-size: .85rem;
}
.footer-credit a {
    color: rgba(232,237,242,.65);
    text-decoration: none;
    transition: color .15s;
}
.footer-credit a:hover { color: var(--nav-active); }

/* ─── Loading spinner ────────────────────────────────────────────────────────── */
.spinner-border-sm { width: 1rem; height: 1rem; }

/* ─── Utility ────────────────────────────────────────────────────────────────── */
.text-mono { font-family: var(--font-mono); }
.text-gold  { color: var(--accent-gold) !important; }

/* ─── Position board tooltip ─────────────────────────────────────────────────── */
.pos-tooltip-wrap {
    display: inline-block;
    cursor: default;
}

/* Tooltip is rendered as a fixed singleton (#posTooltip) positioned by JS */
#posTooltip {
    display: none;
    position: fixed;
    z-index: 9999;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    padding: .6rem;
    pointer-events: none;
}

/* Board squares inside tooltip — slightly larger than before */
#posTooltip .mini-board .sq {
    width: 44px;
    height: 52px;
    padding: 4px 2px 4px;
}
#posTooltip .mini-board .piece { font-size: 1.65rem; }
#posTooltip .mini-board .piece-img { width: 1.45em; height: 1.45em; }
#posTooltip .mini-board .file-label { font-size: .55rem; }

/* ─── Explorer sortable columns ──────────────────────────────────────────────── */
.sortable-th {
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}
.sortable-th:hover { color: var(--accent-blue); }
.sort-icon { font-size: .65rem; opacity: .5; margin-left: .2rem; }
.sortable-th.sort-asc .sort-icon::after  { content: '▲'; opacity: 1; }
.sortable-th.sort-desc .sort-icon::after { content: '▼'; opacity: 1; }
.sortable-th:not(.sort-asc):not(.sort-desc) .sort-icon::after { content: '⇅'; }

/* ─── Responsive adjustments ─────────────────────────────────────────────────── */
@media (max-width: 768px) {
    .page-hero { padding: 1.5rem 0; }
    .stat-number { font-size: 1.8rem; }
    main.main-content { padding-bottom: 1rem; }

    /* Extreme matchup value: break to new centred line on mobile */
    .extreme-val-wrap {
        flex-basis: 100%;
        text-align: center !important;
        margin-left: 0 !important;
    }

    /* Extreme matchup cards: center all items on mobile */
    .extreme-matchup-flex {
        justify-content: center;
        text-align: center;
    }
    .extreme-matchup-flex > div { text-align: center; }
    .extreme-matchup-btn { text-align: center; }
}

@media (max-width: 576px) {
    .section-title { font-size: 1.2rem; }
    .ts-wrapper .ts-control { font-size: .8rem; }

    /* Matchup code input: narrower on mobile */
    #codeWhite, #codeBlack {
        max-width: 10rem !important;
    }
    /* Hide Reset button label on mobile, show only icon */
    .reset-label { display: none; }
}
