/* ============================================================
   Secure IoT Monitor — feuille de style partagée
   Thème clair par défaut, thème sombre via [data-theme="dark"].
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=IBM+Plex+Mono:wght@400;500;600&display=swap');

:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --border: #e6e8ec;
  --border-soft: #eef0f3;
  --text-primary: #14171c;
  --text-muted: #6b7280;
  --text-dim: #9aa1ab;

  --accent-blue: #2f6fed;
  --accent-blue-soft: #eaf1ff;

  --status-ok: #16b378;
  --status-ok-soft: #e6f8f1;
  --status-warn: #f2a900;
  --status-warn-soft: #fef3e0;
  --status-critical: #e5484d;
  --status-critical-soft: #fdecec;
  --track: #eceef1;

  --font-display: 'Inter', sans-serif;
  --font-body: 'Inter', sans-serif;
  --font-mono: 'IBM Plex Mono', monospace;

  --radius: 12px;
  --shadow: 0 1px 2px rgba(20,23,28,0.04), 0 8px 24px rgba(20,23,28,0.05);
  --shadow-lg: 0 4px 10px rgba(20,23,28,0.06), 0 16px 40px rgba(20,23,28,0.12);
}

[data-theme="dark"] {
  --bg: #12151a;
  --card: #1a1e24;
  --border: #2a3038;
  --border-soft: #232830;
  --text-primary: #e8eaed;
  --text-muted: #949ca7;
  --text-dim: #6b7480;

  --accent-blue: #5b8dff;
  --accent-blue-soft: #1e2a45;

  --status-ok: #3ecf8e;
  --status-ok-soft: #14322a;
  --status-warn: #f2a900;
  --status-warn-soft: #3a2c10;
  --status-critical: #f0676b;
  --status-critical-soft: #3a1d1f;
  --track: #262c34;

  --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 8px 24px rgba(0,0,0,0.28);
  --shadow-lg: 0 4px 10px rgba(0,0,0,0.35), 0 16px 40px rgba(0,0,0,0.45);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font-body);
  min-height: 100vh;
  transition: background-color .18s ease, color .18s ease;
}

a { color: var(--accent-blue); }

.app {
  max-width: min(1320px, 97vw);
  margin: 0 auto;
  padding: clamp(12px, 2vw, 20px) clamp(12px, 2.4vw, 24px) 48px;
}

/* ── Barre supérieure ─────────────────────────────────────────── */
.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 20px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.topbar-left { display: flex; align-items: center; gap: 14px; }
.topbar-icon {
  width: 36px; height: 36px; border-radius: 8px;
  background: var(--accent-blue-soft); color: var(--accent-blue);
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.topbar-title { font-family: var(--font-display); font-weight: 700; font-size: 18px; letter-spacing: -0.01em; }
.topbar-sub { font-size: 12.5px; color: var(--text-muted); margin-top: 1px; }
.topbar-right { display: flex; align-items: center; gap: 10px; justify-self: end; }

/* ── Navigation entre pages ───────────────────────────────────── */
.nav {
  display: flex; gap: 4px; background: var(--track); border-radius: 9px; padding: 3px;
  justify-self: center;
}
.nav a {
  font-size: 12.5px; font-weight: 500; color: var(--text-muted); text-decoration: none;
  padding: 7px 14px; border-radius: 6px; white-space: nowrap;
}
.nav a:hover { color: var(--text-primary); }
.nav a.active { background: var(--card); color: var(--accent-blue); box-shadow: var(--shadow); }

/* ── Boutons ──────────────────────────────────────────────────── */
.icon-btn {
  width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--card); color: var(--text-muted);
  display: flex; align-items: center; justify-content: center; cursor: pointer;
  position: relative; flex-shrink: 0;
}
.icon-btn:hover { color: var(--text-primary); }
.icon-btn .badge {
  position: absolute; top: -4px; right: -4px;
  background: var(--status-critical); color: #fff;
  font-family: var(--font-mono); font-size: 10px; font-weight: 600;
  min-width: 16px; height: 16px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center; padding: 0 3px;
}
.btn {
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  border: 1px solid var(--border); background: var(--card); color: var(--text-primary);
  border-radius: 8px; padding: 9px 14px; font-size: 13px; font-weight: 500;
  font-family: var(--font-body); cursor: pointer;
}
.btn:hover { border-color: var(--text-dim); }
.btn svg { width: 15px; height: 15px; }
.btn-primary { background: var(--accent-blue); border-color: var(--accent-blue); color: #fff; }
.btn-primary:hover { opacity: .9; border-color: var(--accent-blue); }
.btn-danger { color: var(--status-critical); border-color: var(--border); }
.btn-danger:hover { border-color: var(--status-critical); }
.btn-sm { padding: 6px 10px; font-size: 12px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

/* ── Cartes ───────────────────────────────────────────────────── */
.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px;
}
.card-title {
  font-family: var(--font-display); font-weight: 600; font-size: 13.5px;
  color: var(--text-primary); display: flex; align-items: center; gap: 8px;
}
.card-title .delta { font-size: 11.5px; font-weight: 600; font-family: var(--font-mono); }

.section-heading { display: flex; align-items: center; gap: 12px; margin: 4px 0 12px; }
.section-heading h2 { font-family: var(--font-display); font-weight: 700; font-size: 15px; margin: 0; }

/* ── Dropdowns ────────────────────────────────────────────────── */
.dropdown-panel {
  position: absolute; top: calc(100% + 8px); z-index: 100;
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow-lg); display: none; overflow: hidden;
}
.dropdown-panel.open { display: block; }
.dropdown-heading {
  font-family: var(--font-display); font-weight: 600; font-size: 12px;
  color: var(--text-muted); padding: 12px 16px 8px; border-bottom: 1px solid var(--border-soft);
}

/* Sélecteur de machine */
.machine-switch { position: relative; }
.machine-switch-btn {
  display: flex; align-items: center; gap: 8px;
  border: 1px solid var(--border); background: var(--card); border-radius: 8px;
  padding: 8px 14px; font-size: 13px; font-weight: 500; color: var(--text-primary);
  cursor: pointer; font-family: var(--font-mono);
}
.machine-switch-badge, .unread-badge {
  background: var(--status-critical); color: #fff; font-family: var(--font-mono);
  font-size: 10px; font-weight: 700; min-width: 16px; height: 16px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center; padding: 0 4px;
}
.machine-dropdown { left: 50%; transform: translateX(-50%); width: 260px; }
.machine-row {
  width: 100%; display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; border: none; background: transparent; cursor: pointer;
  font-family: var(--font-body); font-size: 13px; color: var(--text-primary); text-align: left;
}
.machine-row:hover { background: var(--bg); }
.machine-row .check { width: 14px; color: var(--accent-blue); flex-shrink: 0; }
.machine-row .name { font-family: var(--font-mono); font-size: 12.5px; flex: 1; }

/* Notifications */
.notif-wrap { position: relative; }
.notif-dropdown { right: 0; width: 320px; max-height: 340px; }
.notif-list { max-height: 290px; overflow-y: auto; }
.notif-row { display: flex; align-items: flex-start; gap: 10px; padding: 11px 16px; border-bottom: 1px solid var(--border-soft); }
.notif-row:last-child { border-bottom: none; }
.notif-dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 5px; flex-shrink: 0; }
.notif-dot.critical { background: var(--status-critical); }
.notif-dot.warning { background: var(--status-warn); }
.notif-text { font-size: 12.5px; color: var(--text-primary); line-height: 1.4; }
.notif-time { font-family: var(--font-mono); font-size: 10.5px; color: var(--text-dim); margin-top: 3px; display: block; }
.notif-empty { padding: 24px 16px; text-align: center; font-size: 12.5px; color: var(--text-dim); }

/* Menu utilisateur */
.user-menu { position: relative; }
.user-avatar {
  width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--accent-blue-soft); color: var(--accent-blue);
  font-family: var(--font-display); font-weight: 700; font-size: 12.5px;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.user-dropdown { right: 0; width: 220px; }
.user-dropdown .who { padding: 12px 16px; border-bottom: 1px solid var(--border-soft); }
.user-dropdown .who .name { font-size: 13px; font-weight: 600; }
.user-dropdown .who .role { font-family: var(--font-mono); font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.user-dropdown button {
  width: 100%; text-align: left; border: none; background: transparent; cursor: pointer;
  padding: 10px 16px; font-size: 13px; font-family: var(--font-body); color: var(--text-primary);
}
.user-dropdown button:hover { background: var(--bg); }

/* ── Page de connexion ────────────────────────────────────────── */
.login-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%; max-width: 380px;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-lg); padding: 32px 30px;
}
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 22px; }
.login-brand .title { font-family: var(--font-display); font-weight: 700; font-size: 18px; }
.login-brand .sub { font-size: 12px; color: var(--text-muted); margin-top: 1px; }

/* ── Formulaires ──────────────────────────────────────────────── */
.field { margin-bottom: 14px; }
.field label {
  display: block; font-size: 12px; font-weight: 500; color: var(--text-muted);
  margin-bottom: 5px;
}
.field input, .field select, .field textarea {
  width: 100%; border: 1px solid var(--border); background: var(--bg);
  color: var(--text-primary); border-radius: 8px; padding: 10px 12px;
  font-size: 13.5px; font-family: var(--font-body);
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--accent-blue);
}
.field input[type="checkbox"] { width: auto; accent-color: var(--accent-blue); }
.field .hint { font-size: 11.5px; color: var(--text-dim); margin-top: 4px; }

.form-error {
  background: var(--status-critical-soft); border: 1px solid var(--status-critical);
  color: var(--status-critical); border-radius: 8px; padding: 9px 12px;
  font-size: 12.5px; margin-bottom: 14px; display: none;
}
.form-error.visible { display: block; }
.form-ok {
  background: var(--status-ok-soft); border: 1px solid var(--status-ok);
  color: var(--status-ok); border-radius: 8px; padding: 9px 12px;
  font-size: 12.5px; margin-bottom: 14px; display: none;
}
.form-ok.visible { display: block; }

/* ── Tableaux (machines / utilisateurs) ───────────────────────── */
.table-wrap { overflow-x: auto; }
table.data {
  width: 100%; border-collapse: collapse; font-size: 13px;
}
table.data th {
  text-align: left; font-family: var(--font-display); font-weight: 600; font-size: 11.5px;
  text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted);
  padding: 10px 12px; border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.data td {
  padding: 12px; border-bottom: 1px solid var(--border-soft); vertical-align: middle;
}
table.data tr:last-child td { border-bottom: none; }
table.data .mono { font-family: var(--font-mono); font-size: 12.5px; }
table.data .actions { display: flex; gap: 6px; justify-content: flex-end; }

.pill {
  display: inline-flex; align-items: center; font-family: var(--font-mono);
  font-size: 10.5px; letter-spacing: 0.04em; text-transform: uppercase;
  padding: 3px 9px; border-radius: 10px; white-space: nowrap;
}
.pill.ok { color: var(--status-ok); background: var(--status-ok-soft); }
.pill.warn { color: var(--status-warn); background: var(--status-warn-soft); }
.pill.critical { color: var(--status-critical); background: var(--status-critical-soft); }
.pill.muted { color: var(--text-muted); background: var(--track); }
.pill.info { color: var(--accent-blue); background: var(--accent-blue-soft); }

.empty-row { padding: 28px 12px; text-align: center; color: var(--text-dim); font-size: 13px; }

/* ── Modale ───────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(10,12,16,0.55); z-index: 300;
  display: none; align-items: center; justify-content: center; padding: 20px;
}
.modal-backdrop.open { display: flex; }
.modal {
  width: 100%; max-width: 440px; background: var(--card);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-lg); max-height: 90vh; overflow-y: auto;
}
.modal-head {
  padding: 16px 20px; border-bottom: 1px solid var(--border-soft);
  display: flex; align-items: center; gap: 10px;
}
.modal-head h3 { margin: 0; font-family: var(--font-display); font-size: 15px; font-weight: 700; }
.modal-head .close { margin-left: auto; background: none; border: none; cursor: pointer; color: var(--text-muted); font-size: 20px; line-height: 1; }
.modal-body { padding: 18px 20px; }
.modal-foot {
  padding: 14px 20px; border-top: 1px solid var(--border-soft);
  display: flex; gap: 8px; justify-content: flex-end;
}

/* ── Dashboard : rangée du haut ───────────────────────────────── */
.top-row {
  display: grid;
  grid-template-columns: minmax(200px, 22%) minmax(320px, 1fr) minmax(240px, 26%);
  gap: 16px; margin-bottom: 16px;
}
@media (max-width: 1150px) {
  .top-row { grid-template-columns: 1fr 1fr; }
  .top-row .summary-card { grid-column: 1 / -1; }
}
@media (max-width: 720px) { .top-row { grid-template-columns: 1fr; } }

.health-card { display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.health-donut-wrap { position: relative; width: 168px; height: 168px; margin: 14px 0 10px; }
.health-donut-value { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.health-donut-value .num { font-family: var(--font-display); font-weight: 800; font-size: 32px; letter-spacing: -0.02em; }
.health-legend { display: flex; gap: 16px; font-size: 11.5px; color: var(--text-muted); margin-top: 4px; }
.health-legend span { display: flex; align-items: center; gap: 5px; }
.legend-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }

.trend-card { display: flex; flex-direction: column; }
.trend-header { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.trend-header .card-title { margin-right: auto; }
.range-group { display: flex; gap: 2px; background: var(--track); border-radius: 7px; padding: 2px; }
.range-btn {
  font-family: var(--font-body); font-size: 12px; font-weight: 500; color: var(--text-muted);
  background: transparent; border: none; border-radius: 5px; padding: 6px 12px; cursor: pointer;
}
.range-btn.active { background: var(--accent-blue); color: #fff; }
.chart-wrap { position: relative; height: 190px; margin-top: 4px; }

.summary-card { display: flex; flex-direction: column; }
.summary-list { margin-top: 12px; display: flex; flex-direction: column; }
.summary-row { display: flex; align-items: flex-start; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border-soft); }
.summary-row:last-child { border-bottom: none; }
.summary-chevron {
  width: 20px; height: 20px; border-radius: 6px; background: var(--track);
  display: flex; align-items: center; justify-content: center; flex-shrink: 0; margin-top: 1px;
}
.summary-text { font-size: 12.5px; line-height: 1.4; color: var(--text-primary); }
.summary-delta { font-family: var(--font-mono); font-size: 11px; font-weight: 600; margin-top: 2px; display: block; }
.summary-delta.up { color: var(--status-critical); }
.summary-delta.down { color: var(--status-ok); }

/* ── Dashboard : cartes capteurs ──────────────────────────────── */
.sensor-filter { position: relative; margin-left: auto; }
.sensor-filter-dropdown { right: 0; width: 230px; max-height: 320px; }
.sensor-filter-row {
  display: flex; align-items: center; gap: 9px; padding: 9px 16px;
  font-size: 12.5px; color: var(--text-primary); cursor: pointer;
}
.sensor-filter-row:hover { background: var(--bg); }
.sensor-filter-row input[type="checkbox"] { width: 14px; height: 14px; accent-color: var(--accent-blue); cursor: pointer; }
.sensor-filter-all { border-bottom: 1px solid var(--border-soft); font-weight: 600; }
#sensor-filter-list { max-height: 220px; overflow-y: auto; }

.sensor-row {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
  gap: clamp(8px, 1.2vw, 14px);
}
.sensor-card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px 10px 14px;
}
.sensor-card-head { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 2px; }
.sensor-name { font-family: var(--font-display); font-weight: 600; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sensor-tag { font-family: var(--font-mono); font-size: 9px; color: var(--text-dim); letter-spacing: 0.03em; }
.sensor-gauge-wrap { position: relative; width: 100px; height: 100px; margin: 4px auto 0; }
.sensor-gauge-wrap svg { position: absolute; top: 0; left: 0; }
.sensor-gauge-center {
  position: absolute; inset: 0; top: 14px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 0 4px; text-align: center;
}
.sensor-gauge-value-num { font-family: var(--font-display); font-weight: 800; line-height: 1; max-width: 84px; white-space: nowrap; }
.sensor-gauge-unit { font-family: var(--font-mono); font-size: 9.5px; color: var(--text-muted); font-weight: 500; margin-top: 2px; }

.mock-banner {
  font-family: var(--font-mono); font-size: 11px; color: var(--status-warn);
  background: var(--status-warn-soft); border: 1px solid var(--status-warn);
  border-radius: 8px; padding: 9px 14px; margin-bottom: 16px; display: none;
}
.mock-banner.visible { display: block; }

footer { text-align: center; font-family: var(--font-mono); font-size: 10.5px; color: var(--text-dim); margin-top: 24px; }

/* ── Responsive topbar ────────────────────────────────────────── */
@media (max-width: 860px) {
  .topbar { grid-template-columns: 1fr auto; row-gap: 12px; }
  .nav { grid-column: 1 / -1; justify-self: stretch; overflow-x: auto; }
  .nav a { flex: 1; text-align: center; }
}
@media (max-width: 560px) {
  .topbar-sub { display: none; }
  .btn .btn-label { display: none; }
  .btn { padding: 9px; }
  .notif-dropdown, .user-dropdown { width: min(300px, 88vw); }
}
