:root {
    --bg-color: #f8fafc;
    --card-bg: #ffffff;
    --text-color: #1e293b;
    --accent-color: #3b82f6;
    --border-color: #e2e8f0;
    --header-bg: rgba(255, 255, 255, 0.8);
    --green: #22c55e;
    --red: #ef4444;
    --gray: #94a3b8;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="dark"] {
    --bg-color: #0f172a;
    --card-bg: #1e293b;
    --text-color: #f1f5f9;
    --border-color: #334155;
    --header-bg: rgba(15, 23, 42, 0.8);
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.3);
}

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

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    transition: var(--transition);
    line-height: 1.5;
    /* Le tableau est volontairement large (beaucoup de colonnes).
       On évite donc de "brider" la largeur avec une page trop étroite. */
    padding: 1.25rem;
    /* IMPORTANT: On verrouille le scroll global.
       Le scroll doit exister UNIQUEMENT dans .table-body (Firefox inclus). */
    height: 100vh;
    overflow: hidden;
}

html {
    height: 100%;
    overflow: hidden;
}

html, body {
    overscroll-behavior: none;
}

.container {
    /* "Canvas" pleine largeur pour ne pas couper les colonnes.
       Le scroll horizontal est géré par .table-container. */
    width: 100%;
    max-width: none;
    margin: 0 auto;
    /* Layout "dashboard" : pas de scroll page */
    height: calc(100vh - 2.5rem); /* 2 * body padding */
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding-bottom: 4rem; /* laisse de la place pour le footer fixed */
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    padding: 1.5rem;
    background: var(--header-bg);
    backdrop-filter: blur(10px);
    border-radius: 1rem;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow);
    position: sticky;
    top: 1rem;
    z-index: 100;
}

h1 {
    font-size: 1.5rem;
    font-weight: 800;
    background: linear-gradient(135deg, #3b82f6, #2563eb);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.stats-bar {
    display: flex;
    gap: 1.5rem;
    align-items: center;
}

.stat-item {
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    font-size: 0.9rem;
    font-weight: 600;
}

.stat-item span {
    color: var(--accent-color);
}

.theme-toggle {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    color: var(--text-color);
    padding: 0.5rem;
    border-radius: 0.5rem;
    cursor: pointer;
    font-size: 1.2rem;
    transition: var(--transition);
}

.theme-toggle:hover {
    transform: scale(1.1);
    border-color: var(--accent-color);
}

.search-container {
    flex: 1;
    margin: 0 2rem;
    max-width: 500px;
}

#globalSearch {
    width: 100%;
    padding: 0.75rem 1rem;
    border-radius: 0.75rem;
    border: 1px solid var(--border-color);
    background: var(--card-bg);
    color: var(--text-color);
    font-size: 0.9rem;
    transition: var(--transition);
}

#globalSearch:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.table-shell {
    background: var(--card-bg);
    border-radius: 1rem;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow);
    margin-top: 1rem;
    width: 100%;
    /* Permet au body scrollable de prendre l'espace restant sans faire scroller la page */
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.table-header {
    /* Pas de scroll visible ici, mais on laisse le contenu vertical (filtres) s'afficher */
    overflow-x: hidden;
    overflow-y: visible;
    border-bottom: 1px solid var(--border-color);
    /* Laisse l'entête respirer (titres + filtres) */
    padding-bottom: 0.25rem;
    /* IMPORTANT (Firefox): empêche le header de se faire "compresser" dans le layout flex */
    flex: 0 0 auto;
}

.table-body {
    overflow: auto; /* scroll vertical + horizontal */
    /* prend tout l'espace restant (évite le scroll global) */
    flex: 1 1 auto;
    max-height: none;
    min-height: 0;
    overscroll-behavior: contain;
}

.tracking-table {
    width: 100%;
    table-layout: fixed;
    /*
      IMPORTANT (sticky header):
      Avec border-collapse: collapse, certains navigateurs peuvent laisser apparaître
      le contenu du tbody sous l'entête sticky lors du scroll.
      En mode "separate", l'entête reste parfaitement opaque.
    */
    border-collapse: separate;
    border-spacing: 0;
    text-align: left;
    /* La table devient plus large que le conteneur, et le scroll horizontal gère le reste */
    min-width: 1750px;
}

th {
    background: var(--bg-color);
    padding: 0.75rem 1rem;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--gray);
    border-bottom: 1px solid var(--border-color);
    vertical-align: top;
    /* Le titre doit pouvoir être tronqué sans cacher le filtre */
    white-space: normal;
    overflow: visible; /* important pour les inputs de filtre */
}

/*
  IMPORTANT:
  L'entête est dans une table séparée (hors zone scrollable).
  Donc PAS de sticky ici, sinon Firefox peut "compresser" la hauteur
  et rendre les filtres invisibles.
*/
.table-header thead th {
    position: static;
    z-index: 1;
    background: var(--card-bg);
    background-clip: padding-box;
    box-shadow: 0 2px 0 rgba(255, 255, 255, 0.06);
}

/* Titre dans l'entête : une ligne + ellipsis, sans impacter l'input */
.th-title {
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.column-filter {
    display: block;
    width: 100%;
    margin-top: 0.4rem;
    padding: 0.35rem 0.45rem;
    height: 26px;
    line-height: 26px;
    border-radius: 0.4rem;
    border: 1px solid var(--border-color);
    background: var(--card-bg);
    color: var(--text-color);
    font-size: 0.75rem;
    font-weight: 400;
    text-transform: none;
    letter-spacing: normal;
}

/* Filtres toujours visibles : évite tout clipping vertical */
.table-header {
    overflow-y: visible;
}
.table-header table {
    overflow: visible;
}

/* --------------------------------------------------------------------------
   Largeurs de colonnes (compact / lisible)
   IMPORTANT: on force les largeurs sur TH + TD pour que le header séparé
   reste parfaitement aligné avec le body.
   -------------------------------------------------------------------------- */

/* SERIAL : un peu plus large */
.tracking-table th:nth-child(1),
.tracking-table td:nth-child(1) {
    width: 130px;
}

/* TOWN : plus large */
.tracking-table th:nth-child(2),
.tracking-table td:nth-child(2) {
    /* -20% vs v15 */
    width: 152px;
}

/* ARENA : plus large */
.tracking-table th:nth-child(3),
.tracking-table td:nth-child(3) {
    /* -20% vs v15 */
    width: 184px;
}

/* STATUS : moins large */
.tracking-table th:nth-child(4),
.tracking-table td:nth-child(4) {
    width: 90px;
}

/* PUBLIC IP : moins large */
.tracking-table th:nth-child(5),
.tracking-table td:nth-child(5) {
    width: 110px;
}

/* ANYDESK : moins large */
.tracking-table th:nth-child(6),
.tracking-table td:nth-child(6) {
    width: 120px;
}

/* SCREEN : compact */
.tracking-table th:nth-child(7),
.tracking-table td:nth-child(7) {
    width: 75px;
}

/* STATUS 4G : plus compact */
.tracking-table th:nth-child(8),
.tracking-table td:nth-child(8) {
    /* -20% vs v15 */
    width: 130px;
}

/* TEMP : compact */
.tracking-table th:nth-child(9),
.tracking-table td:nth-child(9) {
    width: 58px;
}

/* START DATE / LAST SHUTDOWN / TIMESTAMP : plus compacts (et autorise 2 lignes) */
.tracking-table th:nth-child(10),
.tracking-table td:nth-child(10) {
    width: 140px;
}
.tracking-table th:nth-child(11),
.tracking-table td:nth-child(11) {
    width: 140px;
}
.tracking-table th:nth-child(12),
.tracking-table td:nth-child(12) {
    width: 165px;
}

/* Autorise le retour à la ligne uniquement dans les colonnes de dates */
.tracking-table td:nth-child(10),
.tracking-table td:nth-child(11),
.tracking-table td:nth-child(12) {
    white-space: normal;
    line-height: 1.15;
}

/* Les titres restent sur une ligne */
.tracking-table th:nth-child(10) .th-title,
.tracking-table th:nth-child(11) .th-title,
.tracking-table th:nth-child(12) .th-title {
    white-space: nowrap;
}

/* Filtres compacts pour les colonnes étroites */
th:nth-child(1) .column-filter,
th:nth-child(7) .column-filter,
th:nth-child(8) .column-filter,
th:nth-child(9) .column-filter {
    padding: 0.25rem;
    font-size: 0.7rem;
    margin-top: 0.35rem;
}

.column-filter:focus {
    outline: none;
    border-color: var(--accent-color);
}

td {
    padding: 1rem;
    font-size: 0.9rem;
    border-bottom: 1px solid var(--border-color);
    transition: var(--transition);
    white-space: nowrap;
}

/* Empêche les retours à la ligne dans les <code> */
td code {
    white-space: nowrap;
}


/* Dates sur 2 lignes (autorise les retours à la ligne sur le corps du tableau) */
td:nth-child(10), td:nth-child(11), td:nth-child(12) {
    white-space: normal;
    line-height: 1.15;
}
.date-2lines { display: inline-block; }
.date-2lines .line2 { display:block; }

/* Alignement Anydesk à droite */
td:nth-child(6) {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

td:nth-child(6) code {
    display: block;
    text-align: right;
}

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

tr {
    transition: var(--transition);
}

tr:hover td {
    background: rgba(59, 130, 246, 0.05);
}

.status-badge {
    padding: 0.25rem 0.75rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
}

.status-online {
    background: rgba(34, 197, 94, 0.1);
    color: var(--green);
    box-shadow: 0 0 10px rgba(34, 197, 94, 0.3), 0 0 20px rgba(34, 197, 94, 0.1);
    border: 1px solid rgba(34, 197, 94, 0.4);
    animation: neon-green 2s ease-in-out infinite alternate;
}

.status-offline {
    background: rgba(239, 68, 68, 0.1);
    color: var(--red);
    box-shadow: 0 0 10px rgba(239, 68, 68, 0.3), 0 0 20px rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.4);
    animation: neon-red 2s ease-in-out infinite alternate;
}

.status-pending {
    background: rgba(148, 163, 184, 0.1);
    color: var(--gray);
}

footer {
    text-align: center;
    padding: 0.75rem 1rem;
    color: var(--gray);
    font-size: 0.85rem;
    border-top: 1px solid var(--border-color);
    background: var(--header-bg);
    backdrop-filter: blur(10px);
    position: fixed;
    left: 1.25rem;
    right: 1.25rem;
    bottom: 1.25rem;
    border-radius: 0.75rem;
}

/* Base Badge Style override */
.status-badge {
    padding: 0.4rem 0.8rem;
    transition: var(--transition);
}

/* Neon Animations */
@keyframes neon-green {
    from {
        box-shadow: 0 0 5px rgba(34, 197, 94, 0.2), 0 0 10px rgba(34, 197, 94, 0.1);
    }

    to {
        box-shadow: 0 0 15px rgba(34, 197, 94, 0.5), 0 0 25px rgba(34, 197, 94, 0.3);
    }
}

@keyframes neon-red {
    from {
        box-shadow: 0 0 5px rgba(239, 68, 68, 0.2), 0 0 10px rgba(239, 68, 68, 0.1);
    }

    to {
        box-shadow: 0 0 15px rgba(239, 68, 68, 0.5), 0 0 25px rgba(239, 68, 68, 0.3);
    }
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

tr {
    animation: fadeIn 0.4s ease-out forwards;
}

/* Responsive */
@media (max-width: 1024px) {
    body {
        padding: 1rem;
    }

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

    .table-body {
        overflow-x: auto;
    }
}
