/* ===== CSS Variables ===== */
:root {
    --bg: #F2F2F7;
    --card-bg: #FFFFFF;
    --text: #1C1C1E;
    --text-secondary: #8E8E93;
    --text-tertiary: #C7C7CC;
    --separator: #E5E5EA;
    --accent: #FF6138;
    --accent-light: rgba(255, 97, 56, 0.08);
    --accent-border: rgba(255, 97, 56, 0.3);
    --green-bg: rgba(52, 199, 89, 0.08);
    --green: #34C759;
    --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
    --card-radius: 14px;
    --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #000000;
        --card-bg: #1C1C1E;
        --text: #F2F2F7;
        --text-secondary: #98989D;
        --text-tertiary: #48484A;
        --separator: #38383A;
        --accent: #FF7047;
        --accent-light: rgba(255, 112, 71, 0.12);
        --accent-border: rgba(255, 112, 71, 0.35);
        --green-bg: rgba(52, 199, 89, 0.12);
        --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    }
}

/* ===== Reset & Base ===== */
[hidden] {
    display: none !important;
}

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

html {
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100dvh;
}

/* ===== Landing Page ===== */
.landing {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100dvh;
    padding: 24px;
}

.landing-content {
    text-align: center;
    max-width: 400px;
    width: 100%;
}

.landing-logo {
    margin-bottom: 20px;
}

.landing-title {
    font-size: 32px;
    font-weight: 700;
    letter-spacing: -0.5px;
    margin-bottom: 8px;
}

.landing-subtitle {
    color: var(--text-secondary);
    font-size: 16px;
    margin-bottom: 32px;
}

.code-form {
    display: flex;
    gap: 10px;
}

.code-input {
    flex: 1;
    padding: 14px 18px;
    font-size: 17px;
    font-family: var(--font);
    border: 1px solid var(--separator);
    border-radius: 12px;
    background: var(--card-bg);
    color: var(--text);
    outline: none;
    transition: border-color 0.2s;
}

.code-input:focus {
    border-color: var(--accent);
}

.code-input::placeholder {
    color: var(--text-tertiary);
}

.code-button {
    padding: 14px 24px;
    font-size: 17px;
    font-weight: 600;
    font-family: var(--font);
    background: var(--accent);
    color: #FFFFFF;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    transition: opacity 0.2s;
    white-space: nowrap;
}

.code-button:hover {
    opacity: 0.85;
}

.code-button:active {
    opacity: 0.7;
}

.landing-error {
    color: #FF3B30;
    font-size: 15px;
    margin-top: 16px;
}

/* ===== Loading ===== */
.loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 60vh;
    gap: 16px;
    color: var(--text-secondary);
    font-size: 16px;
}

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid var(--separator);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* ===== Error State ===== */
.error-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 60vh;
    gap: 12px;
}

.error-emoji {
    font-size: 48px;
    color: var(--text-tertiary);
    font-weight: 700;
}

.error-text {
    font-size: 18px;
    color: var(--text-secondary);
}

.back-link {
    color: var(--accent);
    text-decoration: none;
    font-size: 16px;
    font-weight: 500;
    margin-top: 8px;
}

.back-link:hover {
    text-decoration: underline;
}

/* ===== Tournament Content ===== */
.tournament-content {
    max-width: 960px;
    margin: 0 auto;
    padding: 0 16px 40px;
}

/* ===== Header ===== */
.tournament-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 0;
    position: sticky;
    top: 0;
    background: var(--bg);
    z-index: 100;
}

.header-back {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    color: var(--accent);
    text-decoration: none;
    border-radius: 10px;
    transition: background 0.2s;
    flex-shrink: 0;
}

.header-back:hover {
    background: var(--accent-light);
}

.header-title {
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.3px;
}

.header-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 2px;
}

.header-date {
    font-size: 14px;
    color: var(--text-secondary);
}

.header-badge {
    display: inline-block;
    font-size: 12px;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 6px;
    background: var(--accent-light);
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

/* ===== Tabs (Segmented Control) ===== */
.tabs {
    display: flex;
    background: var(--card-bg);
    border-radius: 10px;
    padding: 3px;
    margin-bottom: 20px;
    border: 1px solid var(--separator);
}

.tab {
    flex: 1;
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 600;
    font-family: var(--font);
    border: none;
    background: transparent;
    color: var(--text-secondary);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.tab.active {
    background: var(--accent);
    color: #FFFFFF;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* ===== Section ===== */
.section {
    margin-top: 24px;
}

.section-title {
    font-size: 20px;
    font-weight: 700;
    margin-bottom: 14px;
    letter-spacing: -0.2px;
}

/* ===== Teams Grid ===== */
.teams-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 10px;
}

.team-card {
    background: var(--card-bg);
    border-radius: var(--card-radius);
    padding: 14px 16px;
    box-shadow: var(--card-shadow);
    display: flex;
    align-items: center;
    gap: 12px;
}

.team-circle {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: 700;
    color: #FFFFFF;
    flex-shrink: 0;
}

.team-info {
    flex: 1;
    min-width: 0;
}

.team-name {
    font-size: 16px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.team-members {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 4px;
}

.member-badge {
    display: inline-block;
    font-size: 12px;
    padding: 2px 8px;
    background: var(--bg);
    color: var(--text-secondary);
    border-radius: 20px;
    white-space: nowrap;
}

/* ===== Group Tabs ===== */
.group-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
}

.group-tab {
    padding: 8px 18px;
    font-size: 14px;
    font-weight: 600;
    font-family: var(--font);
    border: none;
    background: var(--card-bg);
    color: var(--text-secondary);
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
    box-shadow: var(--card-shadow);
}

.group-tab.active {
    background: var(--accent);
    color: #FFFFFF;
}

.group-tab:hover:not(.active) {
    background: var(--separator);
}

/* ===== Groups ===== */
.group-card {
    background: var(--card-bg);
    border-radius: var(--card-radius);
    box-shadow: var(--card-shadow);
    overflow: hidden;
    margin-bottom: 20px;
}

.group-header {
    padding: 14px 16px 10px;
    font-size: 17px;
    font-weight: 700;
}

/* Standings Table */
.standings-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

.standings-table th {
    padding: 8px 10px;
    text-align: left;
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 1px solid var(--separator);
}

.standings-table th:not(:first-child) {
    text-align: center;
}

.standings-table td {
    padding: 10px 10px;
    border-bottom: 1px solid var(--separator);
}

.standings-table td:not(:first-child) {
    text-align: center;
    font-variant-numeric: tabular-nums;
}

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

.standings-table .team-cell {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 500;
}

.standings-table .team-cell .team-circle-sm {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 700;
    color: #FFFFFF;
    flex-shrink: 0;
}

.standings-table .advancing {
    background: var(--green-bg);
}

.standings-table .points-cell {
    font-weight: 700;
}

/* Match List */
.match-list {
    padding: 4px 0;
}

.match-list-header {
    padding: 12px 16px 6px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-top: 1px solid var(--separator);
}

.match-item {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    gap: 8px;
}

.match-team {
    flex: 1;
    font-size: 15px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 8px;
}

.match-team.right {
    justify-content: flex-end;
    text-align: right;
}

.match-team.winner {
    font-weight: 700;
}

.match-score {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-secondary);
    min-width: 56px;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

.match-score.played {
    color: var(--accent);
    font-weight: 700;
}

.match-score.unplayed {
    color: var(--text-tertiary);
}

.match-separator {
    height: 1px;
    background: var(--separator);
    margin: 0 16px;
}

/* ===== Bracket ===== */
.bracket-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 16px;
}

.bracket-wrapper {
    display: flex;
    position: relative;
    padding: 16px 0;
}

.bracket-round {
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.round-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    text-align: center;
    margin-bottom: 12px;
    width: 150px;
}

.bracket-matches {
    position: relative;
}

.bracket-match {
    width: 150px;
    background: var(--card-bg);
    border-radius: 10px;
    border: 1px solid var(--separator);
    overflow: hidden;
    position: absolute;
    left: 0;
}

.bracket-match.played {
    border-color: var(--accent-border);
    box-shadow: 0 1px 4px rgba(255, 97, 56, 0.1);
}

.bracket-match-row {
    display: flex;
    align-items: center;
    padding: 6px 10px;
    gap: 6px;
    height: 31px;
}

.bracket-match-row + .bracket-match-row {
    border-top: 1px solid var(--separator);
}

.bracket-match-row.winner {
    background: var(--accent-light);
}

.bracket-team-circle {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 9px;
    font-weight: 700;
    color: #FFFFFF;
    flex-shrink: 0;
}

.bracket-team-name {
    flex: 1;
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.bracket-team-name.tbd {
    color: var(--text-tertiary);
    font-style: italic;
}

.bracket-team-score {
    font-size: 13px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: var(--text-secondary);
    min-width: 16px;
    text-align: right;
}

.bracket-match-row.winner .bracket-team-name {
    font-weight: 700;
}

.bracket-match-row.winner .bracket-team-score {
    color: var(--accent);
    font-weight: 700;
}

.bracket-connectors {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
}

.bracket-connectors line {
    stroke: var(--separator);
    stroke-width: 1.5;
}

/* ===== Participants ===== */
.participants-card {
    background: var(--card-bg);
    border-radius: var(--card-radius);
    box-shadow: var(--card-shadow);
    overflow: hidden;
}

.participants-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    font-size: 17px;
    font-weight: 700;
    border-bottom: 1px solid var(--separator);
}

.participants-icon {
    font-size: 22px;
}

.participants-count {
    color: var(--text-secondary);
    font-weight: 500;
    font-size: 15px;
}

.participants-list-inner {
    padding: 4px 0;
}

.participant-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
}

.participant-item + .participant-item {
    border-top: 1px solid var(--separator);
}

.participant-number {
    font-size: 14px;
    font-variant-numeric: tabular-nums;
    color: var(--text-secondary);
    min-width: 28px;
    text-align: right;
}

.participant-name {
    font-size: 15px;
    font-weight: 500;
}

/* ===== Empty State ===== */
.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
    font-size: 16px;
}

/* ===== Responsive ===== */
@media (max-width: 600px) {
    .teams-grid {
        grid-template-columns: 1fr;
    }

    .tournament-content {
        padding: 0 12px 32px;
    }

    .landing-title {
        font-size: 28px;
    }

    .code-form {
        flex-direction: column;
    }

    .code-button {
        width: 100%;
    }

    .standings-table {
        font-size: 13px;
    }

    .standings-table th,
    .standings-table td {
        padding: 8px 6px;
    }
}

@media (min-width: 601px) and (max-width: 960px) {
    .teams-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ===== Scrollbar (WebKit) ===== */
.bracket-scroll::-webkit-scrollbar {
    height: 6px;
}

.bracket-scroll::-webkit-scrollbar-track {
    background: transparent;
}

.bracket-scroll::-webkit-scrollbar-thumb {
    background: var(--text-tertiary);
    border-radius: 3px;
}
