/* TV3 - Carros + YouTube Layout */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: #0b0b0b;
    color: #ffffff;
    overflow: hidden;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ============================================================
   HEADER
   ============================================================ */

header {
    background: linear-gradient(180deg, #0b0b0b 0%, #1a1a1a 100%);
    border-bottom: 3px solid #f0d15a;
    padding: 8px 16px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.header-main {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
    min-width: 0;
}

.event-logo {
    height: 36px;
    width: auto;
    flex-shrink: 0;
}

.header-titles {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.event-name {
    color: #f0d15a;
    font-weight: bold;
    font-size: 1.3em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-subtitle {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.82em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.session-name {
    color: #ffffff;
    font-weight: 600;
}

.categories-info {
    color: #6fdc6f;
}

.header-status {
    display: flex;
    align-items: center;
    gap: 14px;
    flex-shrink: 0;
    font-size: 0.82em;
}

.categories-info {
    color: #6fdc6f;
    font-weight: bold;
}

.car-count {
    color: #6fdc6f;
}

.connection-status {
    padding: 3px 8px;
    border-radius: 3px;
    font-weight: bold;
}

.connection-status.connected {
    background-color: #6fdc6f;
    color: #000;
}

.connection-status.disconnected {
    background-color: #ff5c5c;
    color: #fff;
}

.sync-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: #666;
    transition: background-color 0.3s;
}

.sync-dot.active {
    background-color: #f0d15a;
    box-shadow: 0 0 8px #f0d15a;
}

.time-display {
    font-weight: bold;
    color: #f0d15a;
    font-family: 'Courier New', monospace;
}

/* ============================================================
   MAIN LAYOUT - Responsivo: Horizontal (Landscape) e Vertical (Portrait)
   ============================================================ */

main {
    display: flex;
    gap: 0;
    flex: 1;
    overflow: hidden;
    flex-direction: row;
}

#tv3-left {
    flex: 0 0 40%;
    overflow: hidden;
    padding: 0;
    border-right: 2px solid #f0d15a;
    display: flex;
    flex-direction: column;
}

#cars-section {
    flex: 0 0 auto;
    padding: 10px 10px 6px 10px;
    border-bottom: 1px solid #333;
    overflow: hidden;
}

/* Grid fixo 3 colunas em landscape para caber 6 cards (2 linhas de 3) */
#tv3-left #cars-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    overflow: hidden;
}

#tv3-ranking {
    flex: 1;
    overflow: hidden;
    padding: 6px 10px;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

#tv3-ranking .ranking-table-mini {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.62em;
    background-color: #0b0b0b;
    color: #ffffff;
}

#tv3-ranking .ranking-table-mini thead {
    background-color: #1a1a1a;
    border-bottom: 2px solid #f0d15a;
    position: sticky;
    top: 0;
    z-index: 10;
}

#tv3-ranking .ranking-table-mini th {
    padding: 4px 3px;
    text-align: center;
    font-weight: bold;
    color: #f0d15a;
    white-space: nowrap;
}

#tv3-ranking .ranking-table-mini td {
    padding: 3px 2px;
    text-align: center;
    border-bottom: 1px solid #333;
}

#tv3-ranking .ranking-table-mini .col-driver {
    text-align: left;
    max-width: 90px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#tv3-ranking .ranking-table-mini .col-cat {
    max-width: 50px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    opacity: 0.8;
}

#tv3-ranking .ranking-table-mini .col-best {
    font-family: 'Courier New', monospace;
    color: #6fdc6f;
}

#tv3-ranking .ranking-table-mini tr:nth-child(2n) {
    background-color: rgba(240, 209, 90, 0.02);
}

#tv3-ranking .ranking-table-mini tr.position-1 { background-color: rgba(212, 175, 55, 0.1); }
#tv3-ranking .ranking-table-mini tr.position-2 { background-color: rgba(169, 169, 169, 0.1); }
#tv3-ranking .ranking-table-mini tr.position-3 { background-color: rgba(205, 127, 50, 0.1); }

#tv3-right {
    flex: 0 0 60%;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #000000;
}

/* ============================================================
   PORTRAIT MODE (Vertical - TV em Pé)
   ============================================================ */

@media (orientation: portrait) {
    /* Header mais compacto */
    header {
        padding: 5px 10px;
        gap: 8px;
    }

    .event-logo {
        height: 22px;
    }

    .event-name {
        font-size: 0.95em;
    }

    .header-subtitle {
        font-size: 0.72em;
    }

    .header-status {
        font-size: 0.65em;
        gap: 8px;
        white-space: nowrap;
        flex-wrap: wrap;
    }

    main {
        flex-direction: column;
        gap: 0;
    }

    #tv3-right {
        flex: 0 0 40%;
        width: 100%;
        border-right: none;
        border-bottom: 2px solid #f0d15a;
    }

    /* Em portrait: tv3-left ocupa o resto e exibe carros em cima, ranking embaixo */
    #tv3-left {
        flex: 1;
        min-height: 0;
        width: 100%;
        border-right: none;
        display: flex;
        overflow: hidden;
        flex-direction: column;
        padding: 0;
    }

    #cars-section {
        flex: 1;
        min-height: 0;
        overflow: hidden;
        padding: 5px;
        border-bottom: 2px solid #f0d15a;
        display: flex;
        flex-direction: column;
    }

    /* Portrait: grid fixo 2×3 que preenche exatamente o espaço disponível */
    #tv3-left #cars-grid {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: repeat(3, 1fr);
        gap: 4px;
        height: 100%;
        overflow: hidden;
        align-content: stretch;
    }

    /* Portrait: ranking compacto embaixo dos carros */
    #tv3-ranking {
        flex: 0 0 auto;
        height: 28vh;
        overflow: hidden;
        padding: 5px;
        border-bottom: none;
    }


    /* Card portrait: layout compacto em 3 linhas */
    .car-card-portrait {
        flex-direction: column !important;
        justify-content: space-between;
        padding: 5px 7px !important;
        font-size: clamp(9px, 1.55vh, 13px);
        gap: 2px;
    }

    /* Linha 1: posição + número + piloto */
    .cp-top {
        display: flex;
        align-items: baseline;
        gap: 5px;
        overflow: hidden;
    }

    .cp-pos {
        color: #f0d15a;
        font-weight: 800;
        font-size: 1.1em;
        flex-shrink: 0;
    }

    .cp-num {
        color: #fff;
        font-family: 'Courier New', monospace;
        font-weight: 800;
        font-size: 1.4em;
        flex-shrink: 0;
        line-height: 1;
    }

    .cp-driver {
        color: #fff;
        font-weight: 600;
        font-size: 1em;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        flex: 1;
        min-width: 0;
    }

    /* Linha 2: categoria + posição categoria */
    .cp-mid {
        display: flex;
        align-items: center;
        gap: 5px;
        line-height: 1.1;
    }

    .cp-cat {
        color: #6fdc6f;
        font-size: 0.85em;
        font-weight: bold;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .cp-catpos {
        color: #aaa;
        font-size: 0.8em;
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* Linha 3: 5 stats em linha */
    .cp-stats {
        display: grid;
        grid-template-columns: repeat(5, 1fr);
        gap: 2px;
    }

    .cps {
        background: rgba(0,0,0,0.3);
        border-radius: 3px;
        padding: 2px 3px;
        display: flex;
        flex-direction: column;
        min-width: 0;
        overflow: hidden;
    }

    .cpl {
        color: #999;
        font-size: 0.72em;
        line-height: 1.1;
        white-space: nowrap;
    }

    .cpv {
        color: #6fdc6f;
        font-family: 'Courier New', monospace;
        font-weight: bold;
        font-size: 0.85em;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        line-height: 1.2;
    }

    .cpv.best { color: #f0d15a; }

    /* Tabela Geral Compacta - Portrait */
    .ranking-table-mini {
        width: 100%;
        border-collapse: collapse;
        font-size: 0.65em;
        background-color: #0b0b0b;
        color: #ffffff;
    }

    .ranking-table-mini thead {
        background-color: #1a1a1a;
        border-bottom: 2px solid #f0d15a;
        position: sticky;
        top: 0;
        z-index: 10;
    }

    .ranking-table-mini th {
        padding: 4px 3px;
        text-align: center;
        font-weight: bold;
        color: #f0d15a;
        white-space: nowrap;
    }

    .ranking-table-mini td {
        padding: 3px 2px;
        text-align: center;
        border-bottom: 1px solid #333;
    }

    .ranking-table-mini .col-driver {
        text-align: left;
        max-width: 70px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .ranking-table-mini .col-cat {
        text-align: center;
        max-width: 50px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 0.9em;
        opacity: 0.8;
    }

    .ranking-table-mini .col-best {
        text-align: center;
        font-family: 'Courier New', monospace;
        color: #6fdc6f;
    }

    .ranking-table-mini tr:nth-child(2n) {
        background-color: rgba(240, 209, 90, 0.02);
    }

    .ranking-table-mini tr.position-1 { background-color: rgba(212, 175, 55, 0.1); }
    .ranking-table-mini tr.position-2 { background-color: rgba(169, 169, 169, 0.1); }
    .ranking-table-mini tr.position-3 { background-color: rgba(205, 127, 50, 0.1); }
}

#youtube-container {
    width: 100%;
    height: 100%;
    background-color: #000;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

#youtube-container p {
    color: #999;
    font-size: 0.9em;
}

#youtube-container iframe {
    background-color: #000;
}

/* ============================================================
   CARS GRID
   ============================================================ */

#cars-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 6px;
    padding: 0;
}

.car-card {
    background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
    border: 2px solid #f0d15a;
    border-radius: 6px;
    padding: 8px;
    transition: all 0.3s ease;
    cursor: pointer;
}

.car-card:hover {
    border-color: #ffed4e;
    box-shadow: 0 0 10px rgba(240, 209, 90, 0.5);
    transform: translateY(-1px);
}

.car-position {
    color: #f0d15a;
    font-size: 0.8em;
    font-weight: bold;
    margin-bottom: 4px;
}

.car-number {
    font-size: 2.4em;
    font-weight: bold;
    color: #ffffff;
    text-align: center;
    font-family: 'Courier New', monospace;
    letter-spacing: 1px;
    line-height: 1;
    margin: 4px 0;
}

.car-category {
    text-align: center;
    color: #6fdc6f;
    font-size: 0.75em;
    font-weight: bold;
    margin-bottom: 2px;
}

.car-driver {
    text-align: center;
    color: #ffffff;
    font-size: 0.75em;
    margin-bottom: 6px;
    font-weight: 600;
}

.car-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 3px;
    font-size: 0.65em;
}

.stat {
    background-color: rgba(0, 0, 0, 0.3);
    padding: 6px;
    border-radius: 4px;
    text-align: center;
}

.stat-label {
    color: #999;
    font-size: 0.8em;
    margin-bottom: 2px;
}

.stat-value {
    color: #6fdc6f;
    font-weight: bold;
    font-family: 'Courier New', monospace;
    font-size: 1.1em;
}

/* ============================================================
   LOADING STATE
   ============================================================ */

#cars-grid > p {
    grid-column: 1 / -1;
    text-align: center;
    color: #999;
    padding: 40px 20px;
}

/* ============================================================
   SCROLLBAR CUSTOMIZATION
   ============================================================ */

#tv3-left::-webkit-scrollbar {
    width: 8px;
}

#tv3-left::-webkit-scrollbar-track {
    background: #1a1a1a;
}

#tv3-left::-webkit-scrollbar-thumb {
    background: #f0d15a;
    border-radius: 4px;
}

#tv3-left::-webkit-scrollbar-thumb:hover {
    background: #ffed4e;
}

/* ============================================================
   RESPONSIVE
   ============================================================ */

/* Media queries antigas removidas - usar orientation: portrait em vez de max-width */