:root {
  --bg: #0f1420;
  --panel: #161c2b;
  --card: #1d2536;
  --card2: #232c40;
  --line: #2c374f;
  --text: #e6ebf5;
  --muted: #93a0bd;
  --accent: #4c8dff;
  --accent2: #2f6fe0;
  /* Palette séries (validée sur fond sombre) */
  --s1: #3987e5;
  --s2: #008300;
  --s3: #d55181;
  --s4: #c98500;
  --s5: #199e70;
  /* Palette statuts (réservée aux états, jamais aux séries) */
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  /* Encre graphiques */
  --ink: #ffffff;
  --ink2: #c3c2b7;
  --ink-muted: #898781;
  --grid: #2c2c2a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

button {
  font: inherit;
  color: var(--text);
  background: var(--card2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary {
  background: linear-gradient(90deg, var(--accent2), var(--accent));
  border-color: transparent;
  font-weight: 600;
}
button.primary:hover { filter: brightness(1.12); }
button.subtle { background: transparent; color: var(--muted); }
button:disabled { opacity: .45; cursor: default; }

/* ══ Structure générale ══ */
#app {
  display: grid;
  grid-template-columns: 1fr 390px;
  grid-template-rows: 52px 42px 1fr;
  grid-template-areas:
    "top top"
    "scn cop"
    "main cop";
  height: 100vh;
  height: 100dvh;
  width: 100vw;
}

/* ══ Topbar ══ */
#topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 0 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 8px; }
.brand-ia {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: .5px;
  background: linear-gradient(90deg, #4c8dff, #9a6cff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  white-space: nowrap;
}
.brand-by { font-size: 11px; color: var(--muted); white-space: nowrap; }
.brand-by a { color: var(--accent); text-decoration: none; }
.plant-id { flex: 1; min-width: 0; line-height: 1.25; }
.plant-name { display: block; font-weight: 600; font-size: 13px; }
.plant-sub { display: block; font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-status { display: flex; align-items: center; gap: 14px; }
.clock { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 15px; }
.shift { font-size: 12px; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 3px 8px; }

.state-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  font-weight: 600;
  border-radius: 999px;
  padding: 4px 12px;
  border: 1px solid var(--line);
  background: var(--card);
}
.state-chip.sm { font-size: 11px; padding: 2px 9px; font-weight: 500; }
.state-chip .led { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.state-chip.ok .led { background: var(--good); box-shadow: 0 0 7px var(--good); }
.state-chip.warn .led { background: var(--warning); box-shadow: 0 0 7px var(--warning); }
.state-chip.crit .led { background: var(--critical); box-shadow: 0 0 7px var(--critical); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .25; } }

/* ══ Barre scénarios ══ */
#scenarioBar {
  grid-area: scn;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}
.scn-label { font-size: 12px; color: var(--muted); white-space: nowrap; }
button.scn {
  font-size: 12px;
  padding: 4px 11px;
  border-radius: 999px;
  white-space: nowrap;
  background: var(--card);
}
button.scn:hover { background: var(--card2); }
button.scn.playing { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

/* ══ Main + onglets ══ */
#main {
  grid-area: main;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}
#tabs {
  display: flex;
  gap: 2px;
  padding: 8px 16px 0;
  border-bottom: 1px solid var(--line);
}
.tab {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  color: var(--muted);
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.badge {
  display: inline-block;
  min-width: 16px;
  padding: 1px 5px;
  margin-left: 4px;
  border-radius: 999px;
  background: var(--critical);
  color: #fff;
  font-size: 10px;
  text-align: center;
  animation: blink 1.2s infinite;
}
.hidden { display: none !important; }

.tab-pane {
  display: none;
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px;
  min-height: 0;
}
.tab-pane.active { display: block; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 12px;
}
.card h3 {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: .2px;
}
.h-sub { font-weight: 400; font-size: 11px; color: var(--muted); margin-left: 6px; }
.hint { font-size: 11px; color: var(--muted); margin: 8px 0 0; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; align-items: start; }
.col { min-width: 0; }

/* ══ KPI ══ */
.kpi-band {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}
.kpi {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
}
.kpi .k-label { font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.kpi .k-value { font-size: 22px; font-weight: 700; margin-top: 2px; }
.kpi .k-value small { font-size: 12px; font-weight: 400; color: var(--muted); margin-left: 3px; }
.kpi .k-delta { font-size: 11px; margin-top: 2px; color: var(--muted); }
.kpi .k-delta.up { color: var(--good); }
.kpi .k-delta.down { color: var(--serious); }
.kpi-tight { grid-template-columns: 1fr 1fr; margin-bottom: 0; }

/* ══ Synoptique ══ */
.syn-wrap { padding: 6px; overflow: hidden; }
#synoptic svg { display: block; width: 100%; height: auto; }
.syn-bottom { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.m-label { font-size: 13px; fill: var(--text); font-weight: 600; }
.m-sub { font-size: 11px; fill: var(--muted); }
.m-metric { font-size: 13px; fill: var(--ink2); font-weight: 600; font-variant-numeric: tabular-nums; }
.zone-label { font-size: 11px; fill: var(--muted); letter-spacing: 1.5px; text-transform: uppercase; }
.machine-box { fill: var(--card2); stroke: var(--line); }
.machine-box.state-run { stroke: rgba(12,163,12,.55); }
.machine-box.state-warn { stroke: var(--warning); }
.machine-box.state-crit { stroke: var(--critical); }
.machine-box.hl { stroke: var(--accent); stroke-width: 2.5; filter: drop-shadow(0 0 8px rgba(76,141,255,.8)); }
.flow {
  fill: none;
  stroke: var(--s1);
  stroke-width: 3;
  stroke-dasharray: 10 8;
  animation: flowmove 1.1s linear infinite;
  opacity: .85;
}
.flow.slow { animation-duration: 2.6s; }
.flow.stopped { animation-play-state: paused; opacity: .25; }
@keyframes flowmove { to { stroke-dashoffset: -18; } }
.pulse-ring {
  fill: none;
  stroke: var(--critical);
  stroke-width: 2;
  animation: pulse 1.4s ease-out infinite;
  transform-origin: center;
  transform-box: fill-box;
}
@keyframes pulse {
  0% { opacity: 1; transform: scale(.6); }
  100% { opacity: 0; transform: scale(1.6); }
}

/* ══ Journal / flux ══ */
.feed-card { display: flex; flex-direction: column; min-height: 0; }
.event-feed {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 210px;
  overflow-y: auto;
  font-size: 12px;
}
.event-feed li {
  display: flex;
  gap: 8px;
  padding: 5px 2px;
  border-bottom: 1px solid rgba(44,55,79,.5);
  animation: fadein .4s;
}
@keyframes fadein { from { opacity: 0; transform: translateY(-3px); } }
.event-feed .t { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.event-feed .tag {
  font-size: 10px;
  font-weight: 700;
  border-radius: 4px;
  padding: 1px 6px;
  white-space: nowrap;
  align-self: center;
}
.tag.info { background: rgba(57,135,229,.18); color: var(--s1); }
.tag.warn { background: rgba(250,178,25,.15); color: var(--warning); }
.tag.crit { background: rgba(208,59,59,.18); color: #ff8a8a; }
.tag.ok { background: rgba(12,163,12,.15); color: var(--good); }

/* ══ Graphiques ══ */
.chart { position: relative; width: 100%; height: 240px; }
.chart-sm { height: 180px; }
.chart svg { display: block; }
.viz-tip {
  position: absolute;
  pointer-events: none;
  background: #0b0f18;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 11px;
  line-height: 1.5;
  z-index: 10;
  white-space: nowrap;
  box-shadow: 0 4px 14px rgba(0,0,0,.5);
}
.viz-tip b { font-variant-numeric: tabular-nums; }
.viz-legend {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  font-size: 11px;
  color: var(--ink2);
  margin-top: 4px;
}
.viz-legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

/* ══ Maintenance ══ */
.health-list { list-style: none; margin: 0; padding: 0; }
.health-list li {
  display: grid;
  grid-template-columns: 1fr 110px 44px;
  gap: 10px;
  align-items: center;
  padding: 8px 4px;
  border-bottom: 1px solid rgba(44,55,79,.5);
  font-size: 12.5px;
  cursor: default;
}
.health-list li.clickable { cursor: pointer; }
.health-list li.clickable:hover { background: rgba(76,141,255,.06); }
.health-list .h-name b { display: block; }
.health-list .h-name span { color: var(--muted); font-size: 11px; }
.h-bar { height: 8px; border-radius: 99px; background: var(--card2); overflow: hidden; }
.h-bar i { display: block; height: 100%; border-radius: 99px; }
.h-score { font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.pred-card { border-color: var(--warning); }
.pred-card h3 { color: var(--warning); }
.pred-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0; font-size: 12.5px; }
.pred-grid div { background: var(--card2); border-radius: 8px; padding: 8px 10px; }
.pred-grid .l { color: var(--muted); font-size: 11px; display: block; }
.ot-card {
  margin-top: 10px;
  border: 1px dashed var(--good);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 12px;
  background: rgba(12,163,12,.06);
  animation: fadein .4s;
}
.actions { display: flex; gap: 8px; margin-top: 8px; }

/* ══ Qualité ══ */
.cam-card canvas {
  width: 100%;
  height: auto;
  border-radius: 8px;
  background: #0a0d14;
  border: 1px solid var(--line);
}
.cam-stats { display: flex; gap: 18px; margin-top: 8px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.cam-stats b { color: var(--text); font-variant-numeric: tabular-nums; }

/* ══ Énergie ══ */
.finding-card .f-line { display: flex; gap: 8px; margin: 8px 0; font-size: 12.5px; line-height: 1.5; }
.big-euro { font-size: 26px; font-weight: 800; color: var(--warning); margin: 6px 0; }

/* ══ Planning ══ */
.plan-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.plan-head h3 { margin: 0; }
.plan-actions { display: flex; gap: 8px; }
.plan-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin: 12px 0;
}
.plan-stats .kpi .k-value { font-size: 19px; }
.table-wrap { overflow-x: auto; }
.plan-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.plan-table th {
  text-align: left;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
.plan-table td { padding: 7px 8px; border-bottom: 1px solid rgba(44,55,79,.5); white-space: nowrap; }
.plan-table tr.moved td { animation: rowglow 1.6s; }
@keyframes rowglow { 0% { background: rgba(76,141,255,.25); } 100% { background: transparent; } }
.waste-pill { padding: 2px 8px; border-radius: 99px; font-weight: 600; font-size: 11px; }
.waste-hi { background: rgba(208,59,59,.18); color: #ff8a8a; }
.waste-mid { background: rgba(250,178,25,.14); color: var(--warning); }
.waste-lo { background: rgba(12,163,12,.14); color: var(--good); }
.combo-chip { font-size: 11px; color: var(--muted); }

/* ══ Copilote ══ */
#copilot {
  grid-area: cop;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--line);
  min-height: 0;
}
.cop-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
}
.cop-avatar {
  width: 34px; height: 34px;
  border-radius: 10px;
  display: grid; place-items: center;
  font-size: 17px;
  background: linear-gradient(135deg, #4c8dff, #9a6cff);
  color: #fff;
  flex: none;
}
.cop-name { font-weight: 700; font-size: 13.5px; }
.cop-sub { font-size: 11px; color: var(--muted); }

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}
.msg { max-width: 92%; animation: fadein .3s; }
.msg.user { align-self: flex-end; }
.msg .bubble {
  border-radius: 12px;
  padding: 9px 12px;
  font-size: 12.8px;
  line-height: 1.55;
}
.msg.user .bubble {
  background: linear-gradient(90deg, var(--accent2), var(--accent));
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg.bot .bubble {
  background: var(--card);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}
.msg.bot.alert .bubble { border-color: var(--warning); }
.bubble p { margin: 0 0 7px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul { margin: 4px 0 7px; padding-left: 18px; }
.bubble li { margin: 2px 0; }
.bubble .chart { height: 150px; margin: 8px 0; }
.bubble table { border-collapse: collapse; font-size: 11.5px; margin: 6px 0; width: 100%; }
.bubble th { text-align: left; color: var(--muted); font-weight: 600; padding: 3px 8px 3px 0; border-bottom: 1px solid var(--line); }
.bubble td { padding: 3px 8px 3px 0; border-bottom: 1px solid rgba(44,55,79,.5); font-variant-numeric: tabular-nums; }
.bubble .mini-kpis { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.bubble .mk {
  background: var(--card2);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 11px;
  color: var(--muted);
}
.bubble .mk b { display: block; font-size: 15px; color: var(--text); font-variant-numeric: tabular-nums; }
.bubble .msg-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.bubble .msg-actions button { font-size: 11.5px; padding: 5px 10px; }
.src {
  margin-top: 7px;
  font-size: 10.5px;
  color: var(--muted);
  border-top: 1px dashed var(--line);
  padding-top: 5px;
}
.typing { display: inline-flex; gap: 4px; padding: 4px 2px; }
.typing i {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: tp 1s infinite;
}
.typing i:nth-child(2) { animation-delay: .18s; }
.typing i:nth-child(3) { animation-delay: .36s; }
@keyframes tp { 40% { transform: translateY(-4px); opacity: 1; } 0%,100% { opacity: .4; } }

.chips {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  padding: 8px 12px;
  border-top: 1px solid var(--line);
}
.chips button {
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--card);
  color: var(--ink2);
}
.chips button:hover { color: var(--text); }

.ask {
  display: flex;
  gap: 8px;
  padding: 10px 12px 12px;
}
.ask input {
  flex: 1;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  padding: 9px 12px;
  font-size: 13px;
  outline: none;
}
.ask input:focus { border-color: var(--accent); }
.ask button { padding: 0 14px; border-radius: 10px; }

/* ══ Modale ══ */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(8,11,18,.78);
  backdrop-filter: blur(3px);
  display: grid;
  place-items: center;
  z-index: 100;
}
.modal {
  width: min(560px, 92vw);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 22px 26px;
  box-shadow: 0 20px 60px rgba(0,0,0,.55);
}
.modal h2 { margin: 10px 0 8px; font-size: 20px; }
.modal p, .modal li { font-size: 13.5px; line-height: 1.6; }
.modal ul { padding-left: 20px; }
.modal .small { font-size: 11.5px; color: var(--muted); }
.modal-actions { display: flex; gap: 10px; margin-top: 14px; }
.btn-cta {
  display: inline-block;
  padding: 8px 14px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  color: var(--accent);
  text-decoration: none;
  font-size: 13px;
}
.btn-cta:hover { background: rgba(76,141,255,.1); }

/* ══ Responsive minimal ══ */
@media (max-width: 1180px) {
  #app {
    grid-template-columns: 1fr 330px;
  }
  .two-col, .syn-bottom { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: 52px 42px 1fr 46vh;
    grid-template-areas: "top" "scn" "main" "cop";
  }
  #copilot { border-left: none; border-top: 1px solid var(--line); }
  .plant-id { display: none; }
}
