/* ===== VARIABLES ===== */
:root {
    --bg:          #f0f4f8;
    --surface:     #ffffff;
    --surface2:    #f8fafc;
    --nav-bg:      #1a3a6b;
    --nav-text:    #e8edf5;
    --accent:      #2563eb;
    --accent-h:    #1d4ed8;
    --text:        #0f172a;
    --text-muted:  #64748b;
    --border:      #e2e8f0;
    --shadow:      0 1px 3px rgba(0,0,0,.10), 0 1px 2px rgba(0,0,0,.06);
    --shadow-md:   0 4px 12px rgba(0,0,0,.10);
    --radius:      12px;
    --radius-sm:   6px;
    --temp:        #ef4444;
    --humi:        #3b82f6;
    --pres:        #8b5cf6;
    --wind:        #06b6d4;
    --rain:        #0ea5e9;
    --lux:         #f59e0b;
    --uv:          #f97316;
}

[data-theme="dark"] {
    --bg:         #0f172a;
    --surface:    #1e293b;
    --surface2:   #1a2640;
    --nav-bg:     #0f1f3d;
    --nav-text:   #e2e8f0;
    --accent:     #3b82f6;
    --accent-h:   #2563eb;
    --text:       #f1f5f9;
    --text-muted: #94a3b8;
    --border:     #334155;
    --shadow:     0 1px 3px rgba(0,0,0,.4), 0 1px 2px rgba(0,0,0,.3);
    --shadow-md:  0 4px 12px rgba(0,0,0,.4);
}

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

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    transition: background .3s, color .3s;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; height: auto; display: block; }

/* ===== NAV ===== */
.nav {
    background: var(--nav-bg);
    color: var(--nav-text);
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: 0 2px 8px rgba(0,0,0,.25);
}
.nav-inner {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 60px;
    gap: 1rem;
}
.nav-brand {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--nav-text);
    text-decoration: none;
    white-space: nowrap;
}
.nav-links {
    display: flex;
    align-items: center;
    gap: .25rem;
    flex-wrap: wrap;
}
.nav-link {
    color: var(--nav-text);
    opacity: .85;
    padding: .4rem .75rem;
    border-radius: var(--radius-sm);
    font-size: .9rem;
    transition: opacity .2s, background .2s;
}
.nav-link:hover, .nav-link.active {
    opacity: 1;
    background: rgba(255,255,255,.12);
    text-decoration: none;
}
.theme-toggle {
    background: rgba(255,255,255,.12);
    border: none;
    border-radius: var(--radius-sm);
    color: var(--nav-text);
    cursor: pointer;
    font-size: 1.1rem;
    padding: .4rem .6rem;
    transition: background .2s;
}
.theme-toggle:hover { background: rgba(255,255,255,.22); }

/* ===== SITE SELECTOR ===== */
.site-selector {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.site-tabs {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 1.5rem;
    display: flex;
    gap: 0;
}
.site-tab {
    display: inline-block;
    padding: .65rem 1.25rem;
    font-size: .9rem;
    color: var(--text-muted);
    border-bottom: 3px solid transparent;
    transition: color .2s, border-color .2s;
    text-decoration: none;
}
.site-tab:hover { color: var(--accent); text-decoration: none; }
.site-tab.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
    font-weight: 600;
}

/* ===== CONTAINER ===== */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1.5rem;
}
.container-sm {
    max-width: 700px;
    margin: 0 auto;
    padding: 1.5rem;
}

/* ===== PAGE HEADER ===== */
.page-header {
    padding: 1.5rem 0 .5rem;
}
.page-header h1 {
    font-size: 1.6rem;
    font-weight: 700;
    margin-bottom: .25rem;
}
.page-header .subtitle {
    color: var(--text-muted);
    font-size: .9rem;
}

/* ===== METRIC CARDS ===== */
.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 1rem;
    margin: 1.25rem 0;
}
.metric-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem 1rem;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    transition: transform .2s, box-shadow .2s;
}
.metric-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }
.metric-icon { font-size: 1.8rem; margin-bottom: .5rem; }
.metric-value {
    font-size: 1.8rem;
    font-weight: 700;
    line-height: 1;
    margin-bottom: .3rem;
}
.metric-unit { font-size: 1rem; font-weight: 400; color: var(--text-muted); }
.metric-label { font-size: .8rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; }
.metric-sub { font-size: .75rem; color: var(--text-muted); margin-top: .2rem; }

.metric-card.temp   { border-top: 3px solid var(--temp); }
.metric-card.humi   { border-top: 3px solid var(--humi); }
.metric-card.pres   { border-top: 3px solid var(--pres); }
.metric-card.wind   { border-top: 3px solid var(--wind); }
.metric-card.winddir{ border-top: 3px solid var(--wind); }
.metric-card.rain   { border-top: 3px solid var(--rain); }
.metric-card.lux    { border-top: 3px solid var(--lux); }
.metric-card.uv     { border-top: 3px solid var(--uv); }

.metric-card.temp   .metric-value { color: var(--temp); }
.metric-card.humi   .metric-value { color: var(--humi); }
.metric-card.pres   .metric-value { color: var(--pres); }
.metric-card.wind   .metric-value { color: var(--wind); }
.metric-card.winddir .metric-value { color: var(--wind); font-size: 1.2rem; }
.metric-card.rain   .metric-value { color: var(--rain); }
.metric-card.lux    .metric-value { color: var(--lux); }
.metric-card.uv     .metric-value { color: var(--uv); }

/* Badge d'avertissement valeur hors plage */
.metric-warning {
    display: none;
    margin-top: .45rem;
    font-size: .72rem;
    font-weight: 600;
    color: #b91c1c;
    background: #fee2e2;
    border-radius: 99px;
    padding: .15rem .55rem;
}
[data-theme="dark"] .metric-warning { background: #7f1d1d; color: #fca5a5; }

/* ESP32 live badge */
.live-badge {
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    font-size: .78rem;
    font-weight: 600;
    color: #16a34a;
    background: #dcfce7;
    border-radius: 99px;
    padding: .2rem .65rem;
}
[data-theme="dark"] .live-badge { background: #14532d; color: #4ade80; }
.live-dot {
    width: 7px; height: 7px;
    border-radius: 50%;
    background: #16a34a;
    animation: pulse 1.8s infinite;
}
@keyframes pulse {
    0%,100% { opacity: 1; }
    50%      { opacity: .3; }
}

/* Source badge */
.source-db {
    font-size: .75rem;
    color: var(--text-muted);
    font-style: italic;
}

/* ===== SECTION CARD ===== */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 1.5rem;
    overflow: hidden;
}
.card-header {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
}
.card-header h2 {
    font-size: 1.05rem;
    font-weight: 600;
    margin: 0;
}
.card-body { padding: 1.25rem 1.5rem; }

/* ===== STATS ROW ===== */
.stats-row {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 1rem;
    margin: 1rem 0;
}
.stat-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: .85rem 1rem;
    box-shadow: var(--shadow);
    text-align: center;
}
.stat-value { font-size: 1.3rem; font-weight: 700; }
.stat-label { font-size: .75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; }

/* ===== RECORDS GRID ===== */
.records-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 1rem;
    margin: 1rem 0;
}
.record-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    box-shadow: var(--shadow);
}
.record-card-title {
    font-size: .78rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: .05em;
    margin-bottom: .5rem;
}
.record-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: .3rem;
    font-size: .85rem;
}
.record-row .label { color: var(--text-muted); }
.record-row .val   { font-weight: 700; font-size: 1rem; }
.record-row .date  { font-size: .72rem; color: var(--text-muted); }

/* Period selector */
.period-selector {
    display: flex;
    align-items: center;
    gap: .5rem;
    font-size: .85rem;
}
.period-selector label { color: var(--text-muted); }
.period-selector select {
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    padding: .3rem .6rem;
    font-size: .85rem;
    cursor: pointer;
}

/* ===== TABLES ===== */
.table-responsive { overflow-x: auto; }

table {
    width: 100%;
    border-collapse: collapse;
    font-size: .88rem;
}
thead th {
    background: var(--nav-bg);
    color: #fff;
    padding: .7rem 1rem;
    text-align: left;
    font-weight: 600;
    font-size: .8rem;
    text-transform: uppercase;
    letter-spacing: .05em;
    white-space: nowrap;
}
tbody tr {
    border-bottom: 1px solid var(--border);
    transition: background .15s;
}
tbody tr:hover { background: var(--surface2); }
tbody td {
    padding: .6rem 1rem;
    color: var(--text);
    white-space: nowrap;
}
tbody td:first-child { font-weight: 500; }

/* ===== FORMS ===== */
.form-row {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    align-items: flex-end;
    margin-bottom: 1rem;
}
.form-group { display: flex; flex-direction: column; gap: .3rem; flex: 1; min-width: 150px; }
.form-group label { font-size: .82rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }
.form-control {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    padding: .55rem .85rem;
    font-size: .9rem;
    width: 100%;
    transition: border-color .2s;
}
.form-control:focus { outline: none; border-color: var(--accent); }
select.form-control { cursor: pointer; }

/* Checkbox table */
.check-table { border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; width: 100%; }
.check-table th { background: var(--surface2); color: var(--text); font-weight: 600; }
.check-table td, .check-table th { padding: .5rem .85rem; border-bottom: 1px solid var(--border); font-size: .85rem; }
.check-table tr:last-child td { border-bottom: none; }

/* ===== BUTTONS ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    padding: .55rem 1.25rem;
    border-radius: var(--radius-sm);
    border: none;
    font-size: .9rem;
    font-weight: 600;
    cursor: pointer;
    transition: background .2s, transform .1s;
    text-decoration: none;
    white-space: nowrap;
}
.btn:hover { text-decoration: none; transform: translateY(-1px); }
.btn:active { transform: none; }
.btn-primary  { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-h); }
.btn-secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-success  { background: #16a34a; color: #fff; }
.btn-success:hover { background: #15803d; }
.btn-sm { padding: .35rem .8rem; font-size: .8rem; }

/* Legacy compatibility */
.button, .button-data {
    display: inline-flex;
    align-items: center;
    padding: .5rem 1.1rem;
    border-radius: var(--radius-sm);
    background: var(--accent);
    color: #fff;
    font-size: .88rem;
    font-weight: 600;
    border: none;
    cursor: pointer;
    text-decoration: none;
    margin: .25rem;
    transition: background .2s;
}
.button:hover, .button-data:hover { background: var(--accent-h); text-decoration: none; }

/* ===== ALERT / ERROR ===== */
.alert {
    padding: .85rem 1.1rem;
    border-radius: var(--radius-sm);
    font-size: .9rem;
    margin-bottom: 1rem;
}
.alert-error   { background: #fee2e2; color: #b91c1c; border: 1px solid #fca5a5; }
.alert-success { background: #dcfce7; color: #15803d; border: 1px solid #86efac; }
[data-theme="dark"] .alert-error   { background: #450a0a; color: #f87171; border-color: #7f1d1d; }
[data-theme="dark"] .alert-success { background: #052e16; color: #4ade80; border-color: #14532d; }

/* ===== ADMIN MODAL ===== */
.modal-overlay {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 200;
    align-items: center;
    justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 2rem;
    width: min(400px, 90vw);
    box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.modal h2 { margin-bottom: 1.25rem; font-size: 1.1rem; }
.modal-footer { display: flex; gap: .75rem; justify-content: flex-end; margin-top: 1.25rem; }

/* ===== AI IMAGE ===== */
.ai-image-wrap { text-align: center; margin: 2rem 0 1rem; }
.ai-image-wrap img { border-radius: var(--radius); max-height: 400px; width: 100%; object-fit: cover; }
.ai-image-wrap p { font-size: .78rem; color: var(--text-muted); margin-top: .5rem; }

/* ===== PAGE LAYOUT HELPERS ===== */
.section-title {
    font-size: 1rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-muted);
    margin: 1.5rem 0 .75rem;
    display: flex;
    align-items: center;
    gap: .5rem;
}
.section-title::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
}
.mt-1 { margin-top: .5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
.mb-1 { margin-bottom: .5rem; }
.mb-2 { margin-bottom: 1rem; }
.text-muted { color: var(--text-muted); }
.text-center { text-align: center; }
.flex { display: flex; }
.items-center { align-items: center; }
.gap-1 { gap: .5rem; }
.gap-2 { gap: 1rem; }
.justify-between { justify-content: space-between; }
.flex-wrap { flex-wrap: wrap; }

/* ===== FOOTER ===== */
footer {
    border-top: 1px solid var(--border);
    padding: 1.5rem;
    text-align: center;
    font-size: .8rem;
    color: var(--text-muted);
    margin-top: 3rem;
}
footer a { color: var(--text-muted); }

/* ===== THEME SCRIPT (inline JS helper) ===== */

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
    .nav-links .nav-link { display: none; }
    .nav-links .theme-toggle { display: flex; }
    .nav-links::before {
        content: '';
    }
    .card-grid { grid-template-columns: repeat(2, 1fr); }
    .records-grid { grid-template-columns: 1fr; }
    thead th, tbody td { padding: .5rem .6rem; font-size: .8rem; }
}
@media (max-width: 480px) {
    .card-grid { grid-template-columns: repeat(2, 1fr); }
    .metric-value { font-size: 1.4rem; }
    .container { padding: 1rem; }
}
