/* TIA ÉLEC LAB PRO — architecture adaptative.
   Une seule table de câblage, un seul moteur. Le desktop maximise l'espace
   horizontal ; le mobile garde l'atelier plein écran et transforme les
   panneaux existants en tiroirs superposés, jamais en colonnes qui le réduisent. */

:root {
  --mobile-top: 46px;
  --mobile-bar: 100px;
  --mobile-safe: env(safe-area-inset-bottom, 0px);
}

/* ---------- Desktop : la table reste le centre ---------- */
[data-device="desktop"] body { overflow: hidden; }
[data-device="desktop"] .app {
  display: grid;
  height: 100dvh;
  min-height: 0;
  grid-template-columns: var(--lib-size) minmax(0, 1fr) var(--insp-size);
  grid-template-rows: minmax(58px, auto) minmax(0, 1fr) var(--console-size);
  grid-template-areas: "top top top" "lib stage insp" "lib console insp";
  --lib-size: 240px;
  --insp-size: 300px;
  --console-size: 156px;
}
[data-device="desktop"] .topbar {
  grid-area: top;
  flex-wrap: nowrap;
  min-height: 58px;
  max-height: 58px;
  overflow: hidden;
}
[data-device="desktop"] .top-actions { flex-wrap: nowrap; overflow-x: auto; }
[data-device="desktop"] .library { grid-area: lib; }
[data-device="desktop"] .stage { grid-area: stage; }
[data-device="desktop"] .inspector { grid-area: insp; }
[data-device="desktop"] .console { grid-area: console; }
[data-device="desktop"] .stage,
[data-device="desktop"] .canvas-wrap { min-width: 0; min-height: 0; overflow: hidden; }
[data-device="desktop"] .toolbar { flex-wrap: nowrap; overflow-x: auto; }
[data-device="desktop"] .canvas-wrap { touch-action: none; overscroll-behavior: contain; }

/* ---------- Mobile : atelier prioritaire ---------- */
[data-device="mobile"] html,
[data-device="mobile"] body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}
[data-device="mobile"] body { font-size: 13px; -webkit-text-size-adjust: 100%; }

[data-device="mobile"] .app {
  position: fixed;
  inset: 0;
  display: block;
  width: 100%;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
  padding: 0;
  background: var(--surface-2);
}

/* Bandeau supérieur : une ligne, informative, sans manger la table. */
[data-device="mobile"] .topbar {
  position: absolute;
  inset: 0 0 auto 0;
  z-index: 40;
  height: var(--mobile-top);
  min-height: var(--mobile-top);
  padding: 4px 8px;
  flex-wrap: nowrap;
  gap: 6px;
  align-items: center;
  background: #fff;
  box-shadow: 0 1px 6px rgba(15, 23, 42, .14);
}
[data-device="mobile"] .brand { min-width: 0; flex: 0 0 auto; }
[data-device="mobile"] .brand .logo { width: 26px; height: 26px; }
[data-device="mobile"] .brand-slogan,
[data-device="mobile"] .project-caption,
[data-device="mobile"] .sim-picker label,
[data-device="mobile"] .status .clock { display: none !important; }
[data-device="mobile"] .brand-name { font-size: 12px; white-space: nowrap; }
[data-device="mobile"] .project-id { display: none; }
[data-device="mobile"] .sim-picker { min-width: 88px; flex: 1 1 auto; }
[data-device="mobile"] .sim-picker select {
  width: 100%;
  min-height: 36px;
  font-size: 12px;
  padding: 4px 20px 4px 6px;
}
[data-device="mobile"] .status {
  min-width: 0;
  flex: 0 1 auto;
  padding: 4px 6px;
  font-size: 11px;
  white-space: nowrap;
}
[data-device="mobile"] .topbar > .top-actions { display: none; }

/* L'atelier occupe tout l'espace entre le bandeau et la barre tactile. */
[data-device="mobile"] .stage {
  position: absolute;
  top: var(--mobile-top);
  left: 0;
  right: 0;
  bottom: calc(var(--mobile-bar) + var(--mobile-safe));
  width: 100%;
  height: auto;
  min-height: 0;
  overflow: hidden;
  padding: 0;
}
[data-device="mobile"] .canvas-wrap {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: contain;
}
[data-device="mobile"] #workshop {
  width: 100%;
  height: 100%;
  touch-action: none;
}
[data-device="mobile"] .toolbar {
  position: absolute;
  top: 4px;
  left: 4px;
  right: 4px;
  z-index: 12;
  flex-wrap: nowrap;
  overflow-x: auto;
  gap: 4px;
  padding: 3px;
  background: rgba(255, 255, 255, .94);
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(15, 23, 42, .12);
  scrollbar-width: none;
}
[data-device="mobile"] .toolbar::-webkit-scrollbar { display: none; }
[data-device="mobile"] .toolbar .btn,
[data-device="mobile"] .toolbar select,
[data-device="mobile"] .toolbar .chk { min-height: 38px; white-space: nowrap; }
[data-device="mobile"] .hud { top: 48px; font-size: 11px; }
[data-device="mobile"] .empty-card { max-width: min(92vw, 420px); padding: 14px; }
[data-device="mobile"] .empty-card h2 { font-size: 18px; }
[data-device="mobile"] .empty-card p.small { display: none; }

/* Zones de prise tactiles : composants et bornes nettement agrandies. */
[data-device="mobile"] .term .thit { r: 17px; }
[data-device="mobile"] .term .dot { stroke-width: 2.2; }
[data-device="mobile"] .term .tlabel { font-size: 11px; }
[data-device="mobile"] #workshop.coarse-pointer .chit { fill: rgba(15, 23, 42, .015); }
[data-device="mobile"] #workshop.touch-connect .term .dot { stroke: #f59e0b; stroke-width: 3; }
[data-device="mobile"] #workshop.pinching { cursor: grabbing; }

/* Barre tactile : actions réelles, cibles >= 44 px, défilement horizontal. */
.mobile-bar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 45;
  height: calc(var(--mobile-bar) + var(--mobile-safe));
  padding: 4px 4px calc(4px + var(--mobile-safe));
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: #fff;
  border-top: 1px solid var(--line);
  box-shadow: 0 -3px 14px rgba(15, 23, 42, .14);
}
[data-device="desktop"] .mobile-bar { display: none !important; }
.mobile-bar-row {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
  min-height: 44px;
}
.mobile-bar-row::-webkit-scrollbar { display: none; }
.mobile-bar button {
  flex: 0 0 auto;
  min-width: 62px;
  min-height: 44px;
  padding: 4px 8px;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--ink);
  font-size: 10.5px;
  font-weight: 600;
  line-height: 1.1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.mobile-bar button[aria-pressed="true"],
.mobile-bar button[aria-expanded="true"] {
  border-color: #ea580c;
  background: #ffedd5;
  color: #7c2d12;
}
.mobile-bar button > span[aria-hidden] { font-size: 17px; line-height: 1; }

/* Panneaux en tiroirs superposés : ils ne réduisent jamais la table. */
[data-device="mobile"] .library,
[data-device="mobile"] .inspector,
[data-device="mobile"] .console,
[data-device="mobile"][data-drawer="actions"] .top-actions {
  position: fixed;
  z-index: 60;
  left: 0;
  right: 0;
  top: var(--mobile-top);
  bottom: calc(var(--mobile-bar) + var(--mobile-safe));
  width: auto;
  height: auto;
  max-width: none;
  margin: 0;
  padding: 10px 10px 16px;
  overflow: auto;
  background: #fff;
  border-radius: 0 0 14px 14px;
  box-shadow: 0 12px 30px rgba(15, 23, 42, .28);
  transform: translateY(-110%);
  transition: transform .18s ease-out;
  visibility: hidden;
}
[data-device="mobile"] .side-rail { display: none !important; }
[data-device="mobile"][data-drawer="library"] .library,
[data-device="mobile"][data-drawer="inspector"] .inspector,
[data-device="mobile"][data-drawer="console"] .console,
[data-device="mobile"][data-drawer="actions"] .top-actions {
  transform: none;
  visibility: visible;
}
[data-device="mobile"][data-drawer="actions"] .top-actions {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 6px;
}
[data-device="mobile"][data-drawer="actions"] .top-actions .btn {
  min-height: 46px;
  flex: 1 1 132px;
}

.mobile-backdrop {
  position: fixed;
  inset: var(--mobile-top) 0 calc(var(--mobile-bar) + var(--mobile-safe)) 0;
  z-index: 55;
  border: 0;
  background: rgba(15, 23, 42, .28);
  -webkit-tap-highlight-color: transparent;
}
[data-device="desktop"] .mobile-backdrop { display: none !important; }

[data-device="mobile"] .console .statusbar { flex-wrap: wrap; gap: 4px; font-size: 11px; }
[data-device="mobile"] .console .statusbar .sb-item { min-height: 22px; }
[data-device="mobile"] .tabs { position: sticky; top: 0; z-index: 2; background: #fff; }
[data-device="mobile"] .tab { min-height: 44px; font-size: 12px; }
[data-device="mobile"] .lib-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 8px; }
[data-device="mobile"] button,
[data-device="mobile"] select { min-height: 44px; }
[data-device="mobile"] .chk { min-height: 44px; display: inline-flex; align-items: center; gap: 4px; }
[data-device="mobile"] .doc-notice { top: calc(var(--mobile-top) + 4px); left: 4px; right: 4px; max-width: none; }

@media (max-height: 620px) {
  [data-device="mobile"] .mobile-bar button { min-width: 56px; }
  [data-device="mobile"] .toolbar { top: 2px; }
}

/* ---------- Bouton de repli de la barre d'outils ---------- */
.stage > .toolbar-toggle {
  position: absolute;
  top: 6px;
  right: 10px;
  z-index: 25;
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 11.5px;
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(15, 23, 42, .10);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.stage > .toolbar-toggle svg { width: 15px; height: 15px; flex: none; }
.stage > .toolbar-toggle:hover { background: #fff7ed; border-color: #fb923c; }
.stage > .toolbar-toggle:active { transform: translateY(1px); }
.stage > .toolbar-toggle[aria-pressed="true"] {
  background: #ffedd5;
  border-color: #ea580c;
  color: #7c2d12;
}
.stage > .toolbar-toggle:focus-visible { outline: 3px solid rgba(251, 146, 60, .45); outline-offset: 1px; }
.stage > .toolbar + .canvas-wrap { padding-top: 0; }
.stage > .toolbar[hidden] { display: none !important; }
[data-device="desktop"] .stage > .toolbar { padding-right: 210px; }
[data-device="desktop"] .stage.toolbar-hidden > .toolbar { display: none !important; }

[data-device="mobile"] .stage > .toolbar-toggle {
  top: 4px;
  right: 4px;
  min-width: 44px;
  min-height: 44px;
  justify-content: center;
  padding: 4px;
  border-radius: 10px;
}
[data-device="mobile"] .stage > .toolbar-toggle span { display: none; }
[data-device="mobile"] .stage > .toolbar { padding-right: 54px; }
[data-device="mobile"] .stage.toolbar-hidden > .toolbar { display: none !important; }

/* ---------- HUD compact : aucune grande zone sombre vide ---------- */
.hud {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-content: flex-end;
  gap: 6px;
  max-width: min(620px, calc(100% - 24px));
  padding: 0;
  background: transparent;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  font-size: 11px;
}
.hud .hud-title,
.hud .chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  padding: 3px 8px;
  border: 1px solid rgba(148, 163, 184, .28);
  border-radius: 999px;
  background: rgba(15, 23, 42, .88);
  color: #e6efff;
  box-shadow: 0 2px 8px rgba(15, 23, 42, .16);
}
.hud .hud-title {
  font-size: 9.5px;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: #94a3b8;
  background: rgba(15, 23, 42, .72);
}
.hud .chip i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #64748b;
  flex: none;
}
.hud .chip.on i { background: #22c55e; box-shadow: 0 0 5px #22c55e; }
.hud .chip.bad i { background: #ef4444; }
.hud .chip.warn i { background: #f59e0b; }
[data-device="mobile"] .hud {
  top: auto;
  right: 8px;
  bottom: 8px;
  left: 8px;
  max-width: none;
  justify-content: flex-start;
  font-size: 10.5px;
}

/* ---------- États visuels des vraies bascules et des commandes ---------- */
.btn[aria-pressed="true"],
.chk.is-on,
.mobile-bar button[aria-pressed="true"],
.mobile-bar button[aria-expanded="true"],
.mobile-bar button[data-simulation="running"],
.mobile-bar button.is-custom {
  border-color: #ea580c;
  background: #ffedd5;
  color: #7c2d12;
}
.btn:not(:disabled):hover,
.mobile-bar button:not(:disabled):hover { border-color: #fb923c; background: #fff7ed; }
.btn:not(:disabled):active,
.mobile-bar button:not(:disabled):active { transform: translateY(1px); }
.btn:disabled,
.mobile-bar button:disabled { opacity: .48; cursor: not-allowed; transform: none; }
.btn[aria-busy="true"],
.mobile-bar button[aria-busy="true"] { opacity: .7; cursor: progress; }
.btn:focus-visible,
.mobile-bar button:focus-visible { outline: 3px solid rgba(251, 146, 60, .45); outline-offset: 1px; }

#btnStart[data-simulation="running"] { border-color: #94a3b8; background: #f1f5f9; color: #64748b; }
#btnStop[data-simulation="running"],
#btnStop[data-simulation="fault"] { border-color: #dc2626; background: #fee2e2; color: #991b1b; }
#btnStop:disabled { border-color: var(--line); background: var(--surface-2); color: var(--ink-2); }

/* ---------- Menu supérieur escamotable ---------- */
.topbar-toggle {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 4px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 11.5px;
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(15, 23, 42, .10);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.topbar-toggle svg { width: 15px; height: 15px; flex: none; }
.topbar-toggle:hover { background: #fff7ed; border-color: #fb923c; }
.topbar-toggle:active { transform: translateY(1px); }
.topbar-toggle:focus-visible { outline: 3px solid rgba(251, 146, 60, .45); outline-offset: 1px; }
.topbar-toggle[hidden] { display: none !important; }

[data-device="desktop"] .stage { position: relative; }
[data-device="desktop"] .app.topbar-hidden { grid-template-rows: 0 minmax(0, 1fr) var(--console-size); }
[data-device="desktop"] .app.topbar-hidden > .topbar { display: none; }
[data-device="desktop"] .app.topbar-hidden .stage > .toolbar { padding-left: 180px; }

.stage > .topbar-show {
  position: absolute;
  top: 6px;
  left: 10px;
  z-index: 26;
}

[data-device="mobile"] .topbar > .topbar-hide {
  min-width: 44px;
  min-height: 44px;
  justify-content: center;
  padding: 4px;
  border-color: rgba(148, 163, 184, .35);
  background: rgba(255, 255, 255, .92);
}
[data-device="mobile"] .topbar > .topbar-hide span { display: none; }
[data-device="mobile"] .app.topbar-hidden { --mobile-top: 0px; }
[data-device="mobile"] .app.topbar-hidden > .topbar { display: none; }
[data-device="mobile"] .app.topbar-hidden .stage > .toolbar { padding-left: 54px; }
[data-device="mobile"] .stage > .topbar-show {
  top: 4px;
  left: 4px;
  min-width: 44px;
  min-height: 44px;
  justify-content: center;
  padding: 4px;
}
[data-device="mobile"] .stage > .topbar-show span { display: none; }
