/* PM: the plant map (views/map.js viewer and kiosk, views/map-editor.js). Tokens only; the five heat ramp tones are the one new set of
   colours, defined for both themes. --map-scale multiplies every font size in the drawing (the kiosk reads a zone from across the room);
   --map-text is the label colour (heat-4/5 and the strong tones switch it to the surface so names stay readable on dark fills). */
:root { --heat-1: #e8eef5; --heat-2: #bcd3e6; --heat-3: #8fb7d6; --heat-4: #5f98c4; --heat-5: #2f6f9f; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --heat-1: #1d2a36; --heat-2: #244157; --heat-3: #2f5d7d; --heat-4: #3f7aa3; --heat-5: #5b9bca; } }
:root[data-theme="dark"] { --heat-1: #1d2a36; --heat-2: #244157; --heat-3: #2f5d7d; --heat-4: #3f7aa3; --heat-5: #5b9bca; }

/* ---------- The stage and the drawing ---------- */
.map-stage { --map-scale: 1; --map-text: var(--text); position: relative; min-height: 320px; height: 480px; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; touch-action: none; user-select: none; }
.map-svg { display: block; width: 100%; height: 100%; font-family: var(--font); outline: none; cursor: grab; }
.map-svg:focus-visible { box-shadow: inset 0 0 0 2px var(--primary); }
.map-svg.dragging { cursor: grabbing; }
.map-paper { fill: var(--surface); }
.map-grid-rect { pointer-events: none; }
.map-shape { cursor: pointer; }
.map-shape:focus { outline: none; }
.map-shape:focus-visible .map-body { stroke: var(--primary); stroke-width: 3; }
.map-shape.missing { opacity: .5; }
.map-shape.locked { cursor: default; }
.map-shape text { fill: var(--map-text); pointer-events: none; }
/* Rinse tanks draw lighter and dashed so a long line reads as the floor does; process tanks solid. */
.kind-tank.fn-rinse .map-body { fill: var(--surface); stroke: var(--tank); stroke-dasharray: 6 4; }
.kind-tank.fn-rinse.tone-ok .map-body, .kind-tank.fn-rinse.tone-warn .map-body, .kind-tank.fn-rinse.tone-bad .map-body { stroke-dasharray: none; }
/* Strong colours (the kiosk always, the Strong colours toggle): saturated tones with surface-coloured text. */
.map-strong .tone-ok .map-body, .map-kiosk .tone-ok .map-body { fill: var(--ok); }
.map-strong .tone-warn .map-body, .map-kiosk .tone-warn .map-body { fill: var(--warn); }
.map-strong .tone-bad .map-body, .map-kiosk .tone-bad .map-body { fill: var(--bad); }
.map-strong .tone-ok text, .map-strong .tone-warn text, .map-strong .tone-bad text, .map-kiosk .tone-ok text, .map-kiosk .tone-warn text, .map-kiosk .tone-bad text { fill: var(--surface); }
.tone-r4 .map-label, .tone-r4 .map-name, .tone-r5 .map-label, .tone-r5 .map-name { fill: var(--surface); }
.map-hot { pointer-events: none; }
/* Editor UI: handles, marquee, selection box; the renderer keeps their strokes crisp at every zoom. */
.map-ui { pointer-events: none; }
.map-ui .map-handle, .map-ui .map-rot, .map-ui .map-pt { pointer-events: all; cursor: pointer; fill: var(--surface); stroke: var(--primary); stroke-width: 1.5; }
.map-ui .map-pt { fill: var(--primary); }
.map-ui .map-pt.mid { fill: var(--surface); opacity: .8; }
.map-ui .map-sel-box { fill: none; stroke: var(--primary); stroke-width: 1; stroke-dasharray: 6 4; animation: map-ants 1s linear infinite; }
.map-ui .map-group-outline { fill: none; stroke: var(--primary); stroke-width: 1; stroke-dasharray: 2 3; opacity: .7; }
.map-ui .map-marquee { fill: var(--primary-soft); fill-opacity: .4; stroke: var(--primary); stroke-width: 1; }
.map-ui .map-rot-stem { stroke: var(--primary); stroke-width: 1; }
.map-ui .map-coords { fill: var(--text-2); font-size: 12px; }
.map-ui .map-draft { fill: none; stroke: var(--primary); stroke-width: 2; stroke-dasharray: 4 3; }
.map-handle[data-handle="n"], .map-handle[data-handle="s"] { cursor: ns-resize; } .map-handle[data-handle="e"], .map-handle[data-handle="w"] { cursor: ew-resize; }
.map-handle[data-handle="ne"], .map-handle[data-handle="sw"] { cursor: nesw-resize; } .map-handle[data-handle="nw"], .map-handle[data-handle="se"] { cursor: nwse-resize; }
.map-rot { cursor: crosshair; }
@keyframes map-ants { to { stroke-dashoffset: -10; } }
@media (prefers-reduced-motion: reduce) { .map-ui .map-sel-box { animation: none; } .map-kiosk .map-svg { transition: none; } }
.map-stage.tool-select .map-svg { cursor: default; } .map-stage.tool-pan .map-svg { cursor: grab; } .map-stage.tool-draw .map-svg { cursor: crosshair; }
.map-stage.tool-select .map-shape { cursor: move; }
/* When the page scrolls after all (a short window), plain wheel scrolls it and the stage says how to zoom, legend or no legend. */
.map-stage.page-scrolls::after { content: attr(data-zoom-hint); position: absolute; left: 8px; bottom: 8px; z-index: 4; padding: 2px 8px; border-radius: var(--radius-sm); background: var(--surface); color: var(--text-2); border: 1px solid var(--border); font-size: 12px; pointer-events: none; }

/* ---------- Viewer: bar, legend, popover ---------- */
.map-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin: 0 0 8px; padding: 8px 10px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); font-size: 13px; }
.map-bar label { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.map-bar label > span { color: var(--text-2); }
.map-bar select, .map-bar input[type=date] { height: 30px; }
.map-bar input[type=range] { width: 200px; }
.map-bar .map-frame-time { font-variant-numeric: tabular-nums; font-weight: 600; min-width: 44px; }
.map-bar details { position: relative; }
.map-bar details > summary { list-style: none; cursor: pointer; display: inline-flex; align-items: center; height: 30px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); background: var(--surface); font-weight: 600; }
.map-bar details > summary::-webkit-details-marker { display: none; }
.map-bar details > .menu { position: absolute; top: 34px; left: 0; z-index: 30; min-width: 200px; padding: 8px; background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius); box-shadow: var(--shadow-lg); display: flex; flex-direction: column; gap: 6px; }
.map-bar .map-asof { color: var(--text-2); }
.map-legend { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 14px; margin: 0 0 8px; padding: 6px 10px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); font-size: 12px; line-height: 1.4; }
.map-legend[hidden], .map-sheet[hidden], .map-pop[hidden], .map-legend-btn[hidden] { display: none; } /* the hidden attribute wins over the display rules above */
.map-legend .lg { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
.map-legend .sw { width: 14px; height: 14px; border-radius: 2px; border: 1px solid var(--border-strong); flex: none; }
.map-legend .sw.hatch { background: repeating-linear-gradient(45deg, var(--bad) 0 2px, transparent 2px 5px); }
.map-legend .sw.ok { background: var(--ok-soft); border-color: var(--ok); } .map-legend .sw.warn { background: var(--warn-soft); border-color: var(--warn); } .map-legend .sw.bad { background: var(--bad-soft); border-color: var(--bad); }
.map-legend.strong .sw.ok { background: var(--ok); } .map-legend.strong .sw.warn { background: var(--warn); } .map-legend.strong .sw.bad { background: var(--bad); }
.map-legend .sw.r1 { background: var(--heat-1); } .map-legend .sw.r2 { background: var(--heat-2); } .map-legend .sw.r3 { background: var(--heat-3); } .map-legend .sw.r4 { background: var(--heat-4); } .map-legend .sw.r5 { background: var(--heat-5); }
.map-legend .sw.none { background: var(--surface); }
.map-legend .note { color: var(--text-2); }
.map-legend .hot-text { font-weight: 600; }
.map-pop { position: fixed; z-index: 40; width: 300px; max-width: calc(100vw - 24px); max-height: 60vh; overflow: auto; padding: 10px 12px; background: var(--surface); color: var(--text); border: 1px solid var(--border-strong); border-radius: var(--radius); box-shadow: var(--shadow-lg); font-size: 13px; line-height: 1.4; }
.map-pop h3 { font-size: 14px; margin: 0 24px 2px 0; }
.map-pop .kind { color: var(--text-2); font-size: 12px; margin-bottom: 6px; }
.map-pop .pop-close { position: absolute; top: 6px; right: 6px; }
.map-pop dl { margin: 0; } .map-pop dt { font-weight: 600; margin-top: 6px; } .map-pop dd { margin: 0; color: var(--text-2); }
.map-pop ul { margin: 2px 0 0; padding-left: 16px; } .map-pop li.bad { color: var(--bad); }
.map-pop .links { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
.map-view-empty { max-width: 560px; }
.map-tabs { margin: 0; }

/* ---------- The kiosk (a wall TV): no chrome, big type, the legend in one row at the bottom ---------- */
body.map-kiosk .sidebar, body.map-kiosk .topbar, body.map-kiosk .banner, body.map-kiosk .demo-bar, body.map-kiosk .presence, body.map-kiosk .live-bar { display: none !important; }
body.map-kiosk .content { padding: 0; max-width: none; }
body.map-kiosk .layout { grid-template-columns: 1fr; }
.map-kiosk-page { display: flex; flex-direction: column; height: 100vh; height: 100dvh; background: var(--bg); }
.map-kiosk-bar { display: flex; align-items: center; gap: 18px; height: 56px; padding: 0 18px; background: var(--surface); border-bottom: 1px solid var(--border); font-size: 18px; white-space: nowrap; overflow: hidden; }
.map-kiosk-bar .name { font-weight: 700; font-size: 22px; }
.map-kiosk-bar .overlays { color: var(--text-2); min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.map-kiosk-bar .frame { font-size: 32px; font-weight: 700; font-variant-numeric: tabular-nums; position: relative; padding-bottom: 4px; }
.map-kiosk-bar .frame i { position: absolute; left: 0; bottom: 0; height: 3px; background: var(--primary); display: block; }
.map-kiosk-bar .clock { font-variant-numeric: tabular-nums; font-weight: 600; }
.map-kiosk-bar .asof { color: var(--text-2); font-size: 15px; }
.map-kiosk-bar .spacer { flex: 1; }
.map-kiosk-bar--stale { background: var(--warn-soft); color: var(--warn); }
.map-kiosk-bar--stale .stale-text { font-size: 28px; font-weight: 700; }
.map-kiosk-page.stale .map-stage { opacity: .6; }
.map-kiosk .map-stage { --map-scale: 1.6; flex: 1; height: auto; min-height: 0; border: 0; border-radius: 0; }
.map-kiosk .map-stage .map-svg { transition: opacity .3s; }
/* The legend across the bottom: up to two rows, so the hot sentence, the notes and the badge line all stay on the wall. */
.map-kiosk .map-legend { position: static; min-height: 44px; max-height: 96px; overflow: hidden; margin: 0; border: 0; border-top: 1px solid var(--border); border-radius: 0; flex-wrap: wrap; font-size: 20px; gap: 2px 22px; padding: 4px 18px; }
.map-kiosk .map-legend .sw { width: 20px; height: 20px; }
.map-kiosk .map-pop .links { display: none; }
.map-kiosk-page:fullscreen { background: var(--bg); }
.map-kiosk-page:fullscreen .fs-btn { display: none; }

/* ---------- The editor: tools, import and layers on the left, the stage, properties on the right ---------- */
.map-editor { display: grid; grid-template-columns: 260px minmax(0, 1fr) 280px; gap: 12px; align-items: start; }
.map-editor .map-stage { height: calc(100vh - 230px); min-height: 420px; }
.map-tools, .map-props { display: flex; flex-direction: column; gap: 10px; max-height: calc(100vh - 230px); overflow-y: auto; scrollbar-width: thin; }
.map-tools .card-body, .map-props .card-body { padding: 10px; }
.map-toolbar { display: flex; flex-wrap: wrap; gap: 4px; }
.map-toolbar .btn { height: 30px; padding: 0 7px; font-size: 12px; }
.map-toolbar .btn[aria-pressed="true"], .map-toolbar .btn.on { background: var(--primary-soft); border-color: var(--primary); color: var(--link); }
.map-toolbar .sep { flex-basis: 100%; height: 0; }
.map-toolbar details { position: relative; }
.map-toolbar details > summary { list-style: none; }
.map-toolbar details > summary::-webkit-details-marker { display: none; }
.map-toolbar details > .menu { position: absolute; top: 32px; left: 0; z-index: 30; min-width: 180px; padding: 6px; background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius); box-shadow: var(--shadow-lg); display: flex; flex-direction: column; gap: 2px; }
.map-toolbar details > .menu .btn { justify-content: flex-start; width: 100%; }
.map-undo-label { font-size: 12px; color: var(--text-2); }
.map-import h3, .map-layers-panel h3 { font-size: 12px; color: var(--text-2); text-transform: uppercase; letter-spacing: .03em; margin: 8px 0 4px; }
.map-import input[type=search] { width: 100%; }
.map-import .helpers { display: flex; flex-direction: column; gap: 6px; margin: 8px 0; }
.map-import .helpers .btn { justify-content: flex-start; white-space: normal; height: auto; min-height: 30px; padding: 4px 10px; text-align: left; }
.map-import .helpers select { width: 100%; }
.map-import .imp { display: flex; align-items: center; gap: 6px; padding: 3px 4px; border-radius: var(--radius-sm); font-size: 12px; cursor: grab; }
.map-import .imp:hover { background: var(--surface-2); }
.map-import .imp .mono { font-weight: 600; } .map-import .imp .who { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-2); }
.map-import .imp .btn { height: 24px; padding: 0 8px; font-size: 11px; }
.map-import details summary { cursor: pointer; font-size: 12px; color: var(--text-2); }
.map-import .empty { padding: 4px; font-size: 12px; }
.map-layers-panel .layer { display: flex; align-items: center; gap: 4px; padding: 3px 4px; border-radius: var(--radius-sm); font-size: 12px; }
.map-layers-panel .layer.current { background: var(--primary-soft); }
.map-layers-panel .layer .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.map-layers-panel .layer input.rename { flex: 1; min-width: 0; height: 24px; font-size: 12px; }
.map-layers-panel .layer .btn { height: 24px; width: 24px; padding: 0; font-size: 11px; }
.map-layers-panel .layer .btn[aria-pressed="false"] { opacity: .45; }
.map-layers-panel .layer [data-lren] { opacity: 0; } /* the rename pencil shows on the row under the pointer, the focused row and the current layer */
.map-layers-panel .layer:hover [data-lren], .map-layers-panel .layer:focus-within [data-lren], .map-layers-panel .layer.current [data-lren] { opacity: 1; }
.map-side-tabs { display: none; }
.map-props .form-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
.map-props .form-grid .wide { grid-column: 1 / -1; }
.map-props label.field { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--text-2); min-width: 0; }
.map-props label.field input, .map-props label.field select, .map-props label.field textarea { width: 100%; height: 30px; font-size: 13px; }
.map-props label.check input { width: auto; height: auto; margin: 0; }
.map-props label.field textarea { height: 56px; }
.map-props label.check { flex-direction: row; align-items: center; gap: 6px; color: var(--text); }
.map-props .palette { display: flex; flex-wrap: wrap; gap: 3px; }
.map-props .palette button { width: 20px; height: 20px; padding: 0; border: 1px solid var(--border-strong); border-radius: 3px; cursor: pointer; }
.map-props .palette button.on { outline: 2px solid var(--primary); outline-offset: 1px; }
.map-props .palette input[type=color] { width: 28px; height: 20px; padding: 0; border: 1px solid var(--border-strong); background: none; }
.map-props .hint { font-size: 12px; color: var(--warn); }
.map-props dl { margin: 0; font-size: 13px; } .map-props dt { color: var(--text-2); font-size: 12px; margin-top: 6px; } .map-props dd { margin: 0; }
.map-shortcuts td { padding: 3px 8px; font-size: 13px; } .map-shortcuts kbd { font: 12px var(--mono); padding: 1px 5px; border: 1px solid var(--border-strong); border-radius: 3px; background: var(--surface-2); }
.map-stage.drop-ok { box-shadow: inset 0 0 0 2px var(--primary); }
.map-too-narrow { max-width: 560px; }
.map-editor-bar { margin: 0 0 8px; }
/* Under 1100 px the panels become tabs above the stage: the tool buttons stay, then one of Import / Layers / Properties, then the stage. */
@media (max-width: 1100px) {
  .map-editor { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); grid-template-areas: 'tabs tabs' 'tools panel' 'stage stage'; gap: 8px; align-items: start; }
  .map-tools { display: contents; } /* its three cards become grid items: the toolbar beside the chosen panel */
  .map-editor .map-side-tabs { display: flex; gap: 4px; grid-area: tabs; }
  .map-side-tabs .btn[aria-selected="true"] { background: var(--primary-soft); border-color: var(--primary); color: var(--link); }
  .tools-card { grid-area: tools; }
  .map-toolbar .sep { flex-basis: 8px; } /* no forced line breaks: the toolbar wraps to its half of the row */
  .import-card, .layers-card, .map-props { grid-area: panel; display: none; max-height: 220px; overflow-y: auto; }
  .map-editor[data-tab="import"] .import-card, .map-editor[data-tab="layers"] .layers-card, .map-editor[data-tab="props"] .map-props { display: block; }
  .map-editor .map-stage { grid-area: stage; height: 60vh; }
}

/* ---------- Phone (under 600 px): the map is the page; one sticky bar row; sheets for the rest ---------- */
@media (max-width: 600px) {
  body.map-view { overflow: hidden; }
  body.map-view .content { padding: 8px 8px 0; }
  body.map-view .page-head { margin-bottom: 6px; } body.map-view .page-head .sub { display: none; } body.map-view .page-head .row { display: none; }
  body.map-view .page-head h1 { font-size: 16px; }
  .map-bar { position: sticky; top: 0; z-index: 10; flex-wrap: nowrap; overflow: hidden; padding: 6px; gap: 6px; }
  .map-bar label > span { display: none; }
  .map-bar select { max-width: 38vw; }
  .map-bar .map-more { margin-left: auto; }
  .map-stage { height: calc(100dvh - 44px - 58px - 70px); min-height: 240px; }
  body.map-view .map-legend:not(.sheet) { display: none; }
  .map-legend-btn { position: absolute; right: 10px; bottom: 10px; z-index: 5; }
  /* Finger-sized controls (44 px) on the bar, the sheets and the popover. */
  .map-bar .btn, .map-bar select, .map-legend-btn, .map-pop .pop-close, .map-sheet .btn, .map-sheet select, .map-sheet input:not([type=checkbox]), .map-legend .sheet-close { min-height: 44px; }
  .map-bar .map-more, .map-pop .pop-close, .map-legend .sheet-close, .map-sheet .sheet-close { min-width: 44px; }
  .map-sheet .stack { display: flex; flex-direction: column; gap: 10px; }
  .map-kiosk-bar { flex-wrap: wrap; height: auto; min-height: 56px; padding: 6px 10px; }
  .map-kiosk .map-legend { max-height: none; font-size: 14px; gap: 2px 12px; padding: 4px 10px; }
  .map-kiosk .map-legend .sw { width: 14px; height: 14px; }
  .map-sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 60; max-height: 70vh; overflow: auto; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--border-strong); border-radius: 12px 12px 0 0; box-shadow: var(--shadow-lg); }
  .map-sheet .row { gap: 8px; } .map-sheet label { display: flex; align-items: center; gap: 6px; }
  .map-sheet .sheet-close { position: absolute; top: 8px; right: 8px; }
  .map-pop { left: 0 !important; right: 0; top: auto !important; bottom: 0; width: auto; max-width: none; border-radius: 12px 12px 0 0; padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .map-legend.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 60; max-height: 60vh; overflow: auto; margin: 0; border-radius: 12px 12px 0 0; flex-direction: column; align-items: flex-start; padding: 12px 16px; }
  .map-kiosk-bar { gap: 10px; padding: 0 10px; font-size: 14px; } .map-kiosk-bar .name { font-size: 16px; } .map-kiosk-bar .frame { font-size: 22px; }
}

/* ---------- Print: the whole map on one landscape page, the legend as a plain list below ---------- */
@page map { size: landscape; }
@media print {
  body.map-print { page: map; }
  .map-bar, .page-head .row, .map-pop, .sidebar, .topbar, .map-legend-btn, .map-tools, .map-props, .map-editor-bar, .map-sheet, .map-side-tabs { display: none !important; }
  .map-stage { position: static; height: auto !important; min-height: 0; border: 0; overflow: visible; }
  .map-stage.page-scrolls::after { content: none; }
  /* The map takes the page less the title and the legend, so both stay on the one landscape sheet. */
  .map-svg { width: 100%; height: auto; max-height: calc(100vh - 170px); }
  .map-page { display: flex; flex-direction: column; } .map-page .map-legend { order: 10; } /* the legend prints below the map as a plain list */
  .map-legend { position: static; display: block; break-inside: avoid; border: 0; padding: 4px 0; font-size: 11px; line-height: 1.3; }
  .map-legend .lg { display: inline-flex; margin-right: 14px; }
  .map-legend .hint-scroll { display: none !important; }
  .map-shape, .map-paper, .map-badge rect, .map-legend .sw { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
  .map-editor { display: block; }
}
