/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #ffffff;
    --text: #1a1a1a;
    --accent: #e63946;
    --accent-dark: #c62d3a;
    --charcoal: #2b2b2b;
    --border: #e0e0e0;
    --gold: #ffd700;
    --silver: #c0c0c0;
    --bronze: #cd7f32;
    --success: #22c55e;
    --error: #ef4444;
    --radius: 8px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    overflow-x: hidden;
}

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

/* === Nav === */
nav {
    background: var(--charcoal);
    color: #fff;
    padding: 0 1rem;
}
.nav-inner {
    max-width: 1100px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 56px;
}
.nav-brand {
    color: #fff;
    font-weight: 700;
    font-size: 1.1rem;
}
.nav-brand:hover { text-decoration: none; }
.nav-links {
    display: flex;
    align-items: center;
}
.nav-links a {
    color: #ddd;
    margin-left: 1.5rem;
    font-size: 0.95rem;
}
.nav-links a:hover { color: #fff; text-decoration: none; }
.nav-toggle { display: none; }

/* === Main === */
main {
    max-width: 1100px;
    margin: 2rem auto;
    padding: 0 1rem;
}

/* === Cards === */
.card {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
    overflow-x: auto;
}

/* === Flash Messages === */
.flashes { margin-bottom: 1rem; }
.flash {
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 0.5rem;
}
.flash-success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
.flash-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }

/* === Tables === */
table { width: 100%; border-collapse: collapse; }
thead th {
    background: var(--accent);
    color: #fff;
    text-align: left;
    padding: 0.6rem 0.75rem;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    white-space: nowrap;
}
.th-abbr { display: none; }
.th-full { display: inline; }
thead th:first-child { border-radius: var(--radius) 0 0 0; }
thead th:last-child { border-radius: 0 var(--radius) 0 0; }
td {
    padding: 0.55rem 0.75rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.95rem;
}
tbody tr:hover { background: #fafafa; }

.winner { font-weight: 700; }
.elo-up { color: var(--success); }
.elo-down { color: var(--error); }

/* === Rank Colors === */
.rank-gold td:first-child { border-left: 4px solid var(--gold); }
.rank-silver td:first-child { border-left: 4px solid var(--silver); }
.rank-bronze td:first-child { border-left: 4px solid var(--bronze); }

/* === Avatars === */
.avatar-xs, .avatar-sm, .avatar-lg, .avatar-xl {
    border-radius: 50%;
    border: 2px solid var(--charcoal);
    object-fit: cover;
    vertical-align: middle;
}
.avatar-xs { width: 20px; height: 20px; margin-right: 0.2rem; }
.avatar-sm { width: 28px; height: 28px; margin-right: 0.4rem; }
.avatar-lg { width: 64px; height: 64px; }
.avatar-xl { width: 96px; height: 96px; }

.player-link {
    display: inline-flex;
    align-items: center;
    color: var(--text);
}
.player-link:hover { color: var(--accent); }

/* === Buttons === */
.btn {
    display: inline-block;
    padding: 0.5rem 1.2rem;
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    border: none;
    transition: background 0.15s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); text-decoration: none; }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-secondary:hover { background: #ccc; }
.btn-sm { padding: 0.25rem 0.7rem; font-size: 0.8rem; }
.btn-remove {
    background: none;
    border: none;
    color: var(--error);
    cursor: pointer;
    font-size: 1rem;
    padding: 0 0.4rem;
}

/* === Forms === */
label { display: block; margin-bottom: 0.75rem; font-weight: 500; font-size: 0.9rem; }
input, select {
    display: block;
    width: 100%;
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.95rem;
    margin-top: 0.25rem;
}
select { cursor: pointer; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }

/* === Custom Player Select === */
.player-select { position: relative; margin-top: 0.25rem; }
.player-select-trigger {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.95rem;
    background: var(--bg);
    cursor: pointer;
    color: #999;
    font-weight: 400;
}
.player-select-trigger.selected { color: var(--text); }
.player-select-trigger::after {
    content: '▾';
    margin-left: auto;
    font-size: 0.8rem;
    color: #999;
}
.player-select-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 10;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    max-height: 220px;
    overflow-y: auto;
    margin-top: 2px;
}
.player-select-dropdown.open { display: block; }
.player-select-option {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 0.6rem;
    cursor: pointer;
    font-size: 0.95rem;
}
.player-select-option:hover { background: #f5f5f5; }
.player-select-inline { display: inline-block; min-width: 200px; }

.set-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}
.set-row label { margin: 0; min-width: 50px; }
.set-row input { width: 80px; }
.vs { font-weight: 700; }
.form-actions { display: flex; gap: 0.75rem; margin-top: 1rem; }
.filter-bar { margin-bottom: 1rem; }
.filter-bar select { width: auto; min-width: 200px; }

/* === Grids === */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 1rem; }
.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem; }

/* === Player Grid === */
.player-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 1rem;
}
.player-card {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    padding: 1.25rem;
    color: var(--text);
    transition: transform 0.1s, box-shadow 0.1s;
}
.player-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
    text-decoration: none;
}
.rank-badge {
    display: inline-block;
    padding: 0.2rem 0.8rem;
    border-radius: 12px;
    font-size: 0.85rem;
    font-weight: 700;
    background: var(--border);
    color: var(--text);
    margin-right: 0.4rem;
}
.rank-badge-gold { background: var(--gold); color: #1a1a1a; }
.rank-badge-silver { background: var(--silver); color: #1a1a1a; }
.rank-badge-bronze { background: var(--bronze); color: #fff; }
.elo-badge {
    background: var(--charcoal);
    color: #fff;
    padding: 0.2rem 0.8rem;
    border-radius: 12px;
    font-size: 0.85rem;
    font-weight: 600;
}
.stat-line { font-size: 0.85rem; color: #666; }

/* === Profile === */
.profile-header {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    margin-bottom: 2rem;
}
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
    margin-bottom: 1.5rem;
}
.stat-card {
    text-align: center;
    padding: 1rem;
}
.stat-value { display: block; font-size: 1.5rem; font-weight: 700; }
.stat-label { display: block; font-size: 0.8rem; color: #666; text-transform: uppercase; }

.relation-card { text-align: center; }
.relation-card h3 { margin-bottom: 0.5rem; color: var(--accent); }
.empty { color: #999; font-style: italic; }

/* === Match Detail === */
.match-header {
    display: flex;
    align-items: center;
    justify-content: space-around;
    text-align: center;
    margin-bottom: 1.5rem;
}
.match-player { flex: 1; }
.match-score { padding: 0 2rem; }
.big-score { font-size: 2.5rem; font-weight: 700; }
.winner-card { background: #f0fdf4; border-radius: var(--radius); padding: 1rem; }
.elo-change { font-size: 0.85rem; color: #666; }

/* === H2H Matrix === */
.matrix-wrapper { overflow-x: auto; }
.matrix { font-size: 0.8rem; text-align: center; }
.matrix th { padding: 0.4rem; font-size: 0.75rem; white-space: nowrap; background: var(--bg); color: var(--text); text-transform: none; }
.matrix th img { display: inline-block; vertical-align: middle; }
.matrix td { padding: 0.4rem; font-size: 0.8rem; border: 1px solid var(--border); }
.matrix-diag { background: #555; }
.matrix-empty { color: #ccc; }

/* === Chart Legend === */
.chart-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
    margin-bottom: 0.75rem;
}
.legend-item {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.85rem;
    user-select: none;
}
.legend-swatch {
    display: inline-block;
    width: 14px;
    height: 3px;
    border-radius: 2px;
}

/* === Responsive === */
@media (max-width: 768px) {
    .grid-2, .grid-3 { grid-template-columns: 1fr; }
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
    .form-row { grid-template-columns: 1fr; }
    .match-header { flex-direction: column; gap: 1rem; }

    /* Hamburger nav */
    nav { position: relative; }
    .nav-inner { height: 56px; }
    .nav-brand { flex-shrink: 0; white-space: nowrap; }
    .nav-toggle {
        display: flex;
        flex-direction: column;
        justify-content: center;
        gap: 4px;
        background: none;
        border: none;
        cursor: pointer;
        padding: 0.5rem;
        margin-left: auto;
    }
    .nav-toggle span {
        display: block;
        width: 22px;
        height: 2px;
        background: #fff;
        border-radius: 1px;
    }
    .nav-links {
        display: none;
        flex-direction: column;
        position: absolute;
        top: 56px;
        left: 0;
        right: 0;
        background: var(--charcoal);
        padding: 0 1rem 0.5rem;
        z-index: 100;
    }
    .nav-links.open { display: flex; }
    .nav-links a {
        margin-left: 0;
        padding: 0.6rem 0;
        border-top: 1px solid rgba(255,255,255,0.1);
    }

    /* Table mobile fit */
    td, thead th { padding: 0.4rem 0.5rem; font-size: 0.85rem; }
    .card { padding: 1rem; }
    .th-abbr { display: inline; }
    .th-full { display: none; }
}
