:root {
  --bg: #17181c;
  --panel: #22242a;
  --panel-2: #2b2e35;
  --line: #3a3d45;
  --text: #e9e7e3;
  --muted: #9a9ca3;
  --accent: #f39a32;
  --accent-2: #ffb95e;
  --ok: #5cc08a;
  --info: #5aa9e6;
  --warn: #f0b429;
  --danger: #e8645a;
  --fluid: #4fb3d9;
  --radius: 10px;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font-size: 14px; display: flex; flex-direction: column; }
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
h2 { margin: 0 0 6px; font-size: 18px; }
h3 { margin: 18px 0 6px; font-size: 15px; }
h4 { margin: 14px 0 4px; font-size: 13px; color: var(--muted); }
.hint { color: var(--muted); font-size: 12.5px; margin: 4px 0 8px; line-height: 1.45; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.optional { color: var(--muted); font-weight: normal; font-size: 12px; }

/* Barre du haut */
.topbar { display: flex; align-items: center; gap: 24px; padding: 0 18px; height: 52px; background: #111216; border-bottom: 2px solid var(--accent); flex-shrink: 0; }
.brand { font-weight: 700; font-size: 17px; letter-spacing: .3px; white-space: nowrap; }
.logo { color: var(--accent); }
.mainnav { display: flex; gap: 4px; }
.mainnav button { background: none; border: 0; padding: 8px 14px; border-radius: 6px; color: var(--muted); font-weight: 600; }
.mainnav button.active { background: var(--panel-2); color: var(--text); }
.mainnav button:hover { color: var(--text); }
.progress-summary { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; cursor: pointer; }
.progress-summary span { background: var(--panel-2); border: 1px solid var(--line); padding: 3px 8px; border-radius: 20px; font-size: 12px; color: var(--muted); }
.progress-summary span:first-child { color: var(--accent); border-color: var(--accent); }

main { flex: 1; min-height: 0; }
.page { display: none; height: 100%; }
.page.active { display: flex; }
#page-progress.active { flex-direction: column; }

/* Panneaux */
.panel { background: var(--panel); }
.form-panel { width: 380px; flex-shrink: 0; padding: 18px; overflow: auto; border-right: 1px solid var(--line); }
.result-panel { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.result { flex: 1; overflow: auto; padding: 18px; background: var(--bg); position: relative; }

.rows .row { display: grid; grid-template-columns: 1fr 80px 52px 28px; gap: 6px; align-items: center; margin-bottom: 6px; }
.rows .row.invalid .item-input { border-color: var(--danger); }
.row-msg { grid-column: 1 / -1; color: var(--danger); font-size: 12px; }
.unit { color: var(--muted); font-size: 12px; }
.icon-btn { background: none; border: 1px solid transparent; border-radius: 6px; color: var(--muted); height: 30px; }
.icon-btn:hover { border-color: var(--line); color: var(--danger); }
.btn-link { background: none; border: 0; color: var(--accent); padding: 4px 0; font-weight: 600; }
.btn { background: var(--accent); color: #1b1408; border: 0; border-radius: 6px; padding: 7px 14px; font-weight: 700; }
.btn.small { padding: 5px 10px; font-size: 12.5px; }
.btn.ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); }
.check { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.check input[type=number] { width: 70px; }

/* Onglets */
.tabs { display: flex; gap: 2px; padding: 0 12px; background: var(--panel); border-bottom: 1px solid var(--line); flex-shrink: 0; }
.tabs button { background: none; border: 0; border-bottom: 3px solid transparent; padding: 12px 14px 9px; color: var(--muted); font-weight: 600; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }

/* Résumé */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 14px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.kpi-v { font-size: 22px; font-weight: 700; color: var(--accent-2); }
.kpi-l { color: var(--muted); font-size: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.box { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 16px 12px; }
.flows { list-style: none; padding: 0; margin: 0; }
.flows li { padding: 6px 0; border-bottom: 1px solid var(--line); }
.flows li:last-child { border-bottom: 0; }
.flows li.out b { color: var(--ok); }
.flows li.in b { color: var(--info); }
.flows .hint { margin: 2px 0 0; }
.tag { font-size: 11px; background: var(--panel-2); padding: 1px 6px; border-radius: 10px; color: var(--muted); }
.warn { background: rgba(240, 180, 41, .12); border: 1px solid var(--warn); color: var(--warn); padding: 10px 14px; border-radius: var(--radius); margin-bottom: 12px; }
.warn-t { color: var(--warn); }
.empty { color: var(--muted); padding: 40px; text-align: center; font-size: 15px; line-height: 1.6; }
.empty.error { color: var(--danger); }

/* Étapes */
.step { display: flex; gap: 14px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px; }
.step.extract { border-left: 3px solid var(--info); }
.step-n { width: 30px; height: 30px; flex-shrink: 0; border-radius: 50%; background: var(--accent); color: #1b1408; font-weight: 800; display: grid; place-items: center; }
.step.extract .step-n { background: var(--info); }
.step-body { flex: 1; min-width: 0; }
.step-title { font-weight: 700; font-size: 15px; }
.clock { color: var(--warn); font-size: 12.5px; margin-top: 2px; }
.io { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 8px 0; }
.lbl { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); margin-bottom: 2px; }
details summary { cursor: pointer; color: var(--muted); font-size: 12.5px; }
.links { margin: 6px 0; padding-left: 18px; font-size: 12.5px; color: var(--muted); }
.links li { margin: 2px 0; }

/* Schéma */
.graph-wrap { overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.graph .edge { fill: none; stroke: #8a8d96; opacity: .65; }
.graph .edge.fluid { stroke: var(--fluid); }
.graph .edge.warn { stroke: var(--warn); }
.graph .edge:hover { opacity: 1; stroke: var(--accent-2); }
.graph .edge-label { fill: var(--muted); font-size: 10.5px; pointer-events: none; }
.graph .node rect { fill: var(--panel-2); stroke: var(--line); stroke-width: 1.5; }
.graph .node.step rect { stroke: var(--accent); }
.graph .node.extract rect { stroke: var(--info); }
.graph .node.supply rect { stroke: var(--info); stroke-dasharray: 4 3; }
.graph .node.output rect { stroke: var(--ok); fill: rgba(92, 192, 138, .12); }
.graph .node.surplus rect { stroke: var(--muted); stroke-dasharray: 4 3; }
.graph .n-title { fill: var(--text); font-weight: 700; font-size: 12.5px; }
.graph .n-sub { fill: var(--muted); font-size: 11px; }

/* Progression */
.progress-view { flex: 1; overflow: auto; padding: 18px 22px; }
.intro p { margin: 0 0 14px; max-width: 720px; line-height: 1.5; }
.phases { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; margin-bottom: 14px; }
.phase-card { text-align: left; background: var(--panel); border: 2px solid var(--line); border-radius: var(--radius); padding: 14px; }
.phase-card.done { border-color: var(--accent); background: linear-gradient(180deg, rgba(243, 154, 50, .14), var(--panel)); }
.phase-card.static { cursor: default; opacity: .8; }
.phase-n { font-size: 12px; color: var(--accent); font-weight: 700; text-transform: uppercase; letter-spacing: .6px; }
.phase-name { font-weight: 700; font-size: 15px; margin: 4px 0; }
.phase-tiers { color: var(--muted); font-size: 12.5px; }
.phase-state { margin-top: 10px; font-size: 12.5px; font-weight: 600; }
.phase-card.done .phase-state { color: var(--accent-2); }

.split { display: flex; gap: 18px; align-items: flex-start; }
.side { width: 210px; flex-shrink: 0; display: flex; flex-direction: column; gap: 3px; position: sticky; top: 0; }
.side-item { display: flex; justify-content: space-between; background: var(--panel); border: 1px solid var(--line); border-left: 3px solid transparent; border-radius: 6px; padding: 9px 12px; text-align: left; font-weight: 600; }
.side-item.active { border-left-color: var(--accent); background: var(--panel-2); }
.side-item.locked { color: var(--muted); }
.side-item .count { color: var(--muted); font-weight: normal; font-size: 12px; }
.main { flex: 1; min-width: 0; }
.main-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.main-head h2 { margin: 0; }
.actions { display: flex; gap: 6px; }
.lock-msg { color: var(--warn); margin: 0; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 10px; }
.card { text-align: left; background: var(--panel); border: 2px solid var(--line); border-radius: var(--radius); padding: 12px; display: flex; flex-direction: column; gap: 6px; }
.card:hover:not(:disabled) { border-color: var(--muted); }
.card.checked { border-color: var(--ok); background: linear-gradient(180deg, rgba(92, 192, 138, .12), var(--panel)); }
.card.locked { opacity: .45; cursor: not-allowed; }
.card-head { display: flex; align-items: center; gap: 8px; }
.tick { width: 20px; height: 20px; flex-shrink: 0; border-radius: 4px; border: 2px solid var(--line); display: grid; place-items: center; font-size: 12px; color: #10301f; }
.card.checked .tick { background: var(--ok); border-color: var(--ok); }
.card-title { font-weight: 700; flex: 1; }
.badge { font-size: 10.5px; text-transform: uppercase; letter-spacing: .5px; background: rgba(243, 154, 50, .18); color: var(--accent-2); padding: 2px 6px; border-radius: 4px; }
.cost { margin: 0; padding-left: 16px; color: var(--muted); font-size: 12px; }
.unlocks { font-size: 12px; line-height: 1.45; }
.unlocks span { color: var(--muted); }
.mam-level { margin-bottom: 14px; }
.mam-level-label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .6px; margin: 10px 0 6px; }

.toolbar { display: flex; gap: 14px; align-items: center; margin: 8px 0 12px; flex-wrap: wrap; }
.toolbar input[type=search] { width: 340px; }
.hdd-list { display: flex; flex-direction: column; gap: 6px; }
.hdd-row { display: flex; gap: 12px; align-items: flex-start; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; cursor: pointer; }
.hdd-row.checked { border-color: var(--ok); }
.hdd-row input { margin-top: 3px; accent-color: var(--ok); width: 16px; height: 16px; }
.hdd-name { font-weight: 700; }
.hdd-recipe { font-size: 12.5px; margin-top: 2px; }
.hdd-std { font-size: 12px; color: var(--muted); margin-top: 2px; }
.hdd-req { font-size: 12px; color: var(--warn); margin-top: 2px; }

@media (max-width: 1100px) {
  .phases { grid-template-columns: repeat(3, 1fr); }
  .kpis { grid-template-columns: repeat(2, 1fr); }
}

/* Mode « J'ai ces ressources » */
.mode-switch { display: grid; grid-template-columns: 1fr 1fr; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 3px; margin-bottom: 16px; }
.mode-switch button { background: none; border: 0; padding: 8px; border-radius: 6px; color: var(--muted); font-weight: 600; }
.mode-switch button.active { background: var(--accent); color: #1b1408; }
.rows .row.single { grid-template-columns: 1fr; }
input.invalid { border-color: var(--danger); }
#max-target-msg { margin-bottom: 8px; }
select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }
.node-helper { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin: 10px 0 4px; display: flex; flex-direction: column; gap: 6px; }
.node-helper-title { font-weight: 600; font-size: 12.5px; color: var(--muted); }
.node-helper-row { display: flex; gap: 6px; align-items: center; }
.node-helper-row select { flex: 1; min-width: 0; }
.node-helper-row #node-count { width: 60px; }
.node-helper-row #node-preview { flex: 1; }
.max-banner { display: flex; align-items: center; gap: 18px; background: linear-gradient(90deg, rgba(92, 192, 138, .16), var(--panel)); border: 1px solid var(--ok); border-radius: var(--radius); padding: 14px 18px; margin-bottom: 12px; line-height: 1.5; }
.max-v { font-size: 28px; font-weight: 800; color: var(--ok); white-space: nowrap; }
.box.usage { margin-bottom: 12px; }
.usage-line { display: flex; justify-content: space-between; }
.bar { height: 6px; background: var(--bg); border-radius: 3px; margin-top: 4px; overflow: hidden; }
.bar div { height: 100%; background: var(--info); }
[hidden] { display: none !important; }

/* Plan 3D */
.result.full { padding: 0; overflow: hidden; }
.viewer { position: relative; width: 100%; height: 100%; min-height: 420px; }
.viewer-canvas { position: absolute; inset: 0; }
.viewer-canvas canvas { display: block; width: 100%; height: 100%; cursor: grab; }
.viewer-canvas canvas:active { cursor: grabbing; }
.viewer-tools { position: absolute; top: 12px; left: 12px; display: flex; gap: 6px; }
.viewer-tools .btn.ghost { background: rgba(23, 24, 28, .85); }
.viewer-panel { position: absolute; top: 12px; right: 12px; width: 270px; max-height: calc(100% - 24px); overflow: auto; background: rgba(34, 36, 42, .92); border: 1px solid var(--line); border-radius: var(--radius); padding: 2px 14px 10px; }
.viewer-panel h3 { margin: 12px 0 6px; }
.legend { list-style: none; padding: 0; margin: 0; font-size: 12.5px; }
.legend li { display: flex; align-items: center; gap: 6px; padding: 3px 0; flex-wrap: wrap; }
.legend b { margin-left: auto; }
.sw { width: 14px; height: 8px; border-radius: 2px; display: inline-block; }
.viewer-tip { position: absolute; pointer-events: none; background: rgba(17, 18, 22, .95); border: 1px solid var(--accent); border-radius: 8px; padding: 8px 10px; font-size: 12.5px; line-height: 1.5; max-width: 320px; }

/* Mes usines */
#page-factories.active { flex-direction: column; }
.factory-bar { display: flex; gap: 6px; margin-bottom: 12px; }
.factory-bar input { flex: 1; font-weight: 700; font-size: 15px; background: transparent; border-color: transparent; }
.factory-bar input:hover, .factory-bar input:focus { border-color: var(--line); background: var(--bg); }
.factory-list { display: flex; flex-direction: column; gap: 8px; max-width: 960px; margin-top: 12px; }
.factory { display: flex; gap: 14px; align-items: center; background: var(--panel); border: 1px solid var(--line); border-left: 3px solid transparent; border-radius: var(--radius); padding: 12px 14px; }
.factory.current { border-left-color: var(--accent); }
.factory-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.factory-name { font-weight: 700; font-size: 15px; background: transparent; border-color: transparent; padding: 3px 6px; margin-left: -6px; max-width: 420px; }
.factory-name:hover, .factory-name:focus { border-color: var(--line); background: var(--bg); }
.factory-obj { color: var(--text); font-size: 13px; }
.factory-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.btn.danger:hover { color: var(--danger); border-color: var(--danger); }
.file-btn { cursor: pointer; display: inline-flex; align-items: center; }
.note { background: rgba(92, 192, 138, .12); border: 1px solid var(--ok); color: var(--ok); padding: 10px 14px; border-radius: var(--radius); margin-bottom: 12px; max-width: 960px; }
.warn { max-width: 960px; }

/* Plan 3D : plusieurs étages */
.viewer-tools { flex-direction: column; align-items: flex-start; gap: 6px; right: 300px; }
.viewer-tools .seg { display: flex; gap: 2px; flex-wrap: wrap; background: rgba(23, 24, 28, .85); border-radius: 8px; padding: 2px; }
.viewer-tools .seg .btn.ghost { border-color: transparent; }
.mode-hint { font-size: 12px; color: var(--muted); background: rgba(23, 24, 28, .85); border-radius: 6px; padding: 4px 8px; max-width: 420px; line-height: 1.4; }
.floors-list li { display: block; }

/* Aucune usine */
.no-factory-msg { display: none; }
#page-plan.no-factory > aside, #page-plan.no-factory > .result-panel { display: none; }
#page-plan.no-factory > .no-factory-msg { display: flex; flex: 1; flex-direction: column; align-items: center; justify-content: center; gap: 12px; color: var(--muted); font-size: 15px; }

/* V2 : options et actions */
.power-opts { display: flex; gap: 6px; margin: 6px 0 0 22px; }
.power-opts select { flex: 1; min-width: 0; }
.prefs { list-style: none; padding: 0; margin: 0; font-size: 12.5px; }
.prefs li { display: flex; align-items: center; gap: 6px; padding: 3px 0; flex-wrap: wrap; }
.prefs li .icon-btn { height: 22px; margin-left: auto; }
.step-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 6px 0 4px; font-size: 12.5px; color: var(--muted); }
.step-actions select { padding: 3px 6px; font-size: 12.5px; }
.max-line { line-height: 1.2; }
.max-line .max-v { font-size: 24px; }
.box.gen { margin-bottom: 12px; border-color: var(--warn); }
.graph .node.power rect { stroke: var(--warn); stroke-dasharray: 4 3; }

/* V2-10 : apports venant d'une autre usine */
.row-from { grid-column: 1 / -1; font-size: 12px; color: var(--muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin: -2px 0 4px; }
.row-from select { padding: 3px 6px; font-size: 12px; max-width: 100%; }
.rate-input:disabled { opacity: .75; }
.network { max-width: 1100px; margin-top: 28px; }
.network .graph-wrap { margin: 10px 0 12px; }
.network-totals { margin-top: 12px; }

/* V2 lot 5 : pas à pas et sélection de rangée */
.build-step, .row-sel { border-bottom: 1px solid var(--line); padding-bottom: 8px; margin-bottom: 4px; }
.build-step p, .row-sel p { margin: 6px 0; font-size: 12.5px; }
.build-nav { display: flex; gap: 6px; margin: 6px 0; flex-wrap: wrap; }
.row-sel .btn { margin-top: 2px; }

/* V2-16 : import de sauvegarde */
.save-import { display: flex; gap: 16px; align-items: center; justify-content: space-between; background: var(--panel); border: 1px solid var(--line); border-left: 3px solid var(--info); border-radius: var(--radius); padding: 12px 16px; margin-bottom: 14px; max-width: 980px; }
.save-import .hint { margin: 4px 0 0; }
.save-import code, .data-info code { background: var(--bg); padding: 1px 5px; border-radius: 4px; font-size: 11.5px; }
.viewer-canvas canvas.panning { cursor: move; }

/* Emprise de l'usine */
.grid-size { align-items: center; padding: 3px 8px !important; font-size: 12.5px; color: var(--text); }
.grid-size input { width: 58px; padding: 3px 6px; font-size: 12.5px; }
.ok-t { color: var(--ok); }
