:root {
  --mm: 3px;                 /* screen pixels per paper millimetre (set by JS) */
  --lu: 3px;                 /* legend unit: --mm scaled with the sheet size, A3 = 1 mm (set by JS) */
  --red: #d61f26;
  --route: #1f4fd6;
  --closure: #d61f26;
  --area: #e08a00;
  --ink: #1b1b1b;
  --ui-bg: #f3f4f6;
  --ui-border: #d8dbe0;
  --sheet-w: 420;
  --sheet-h: 297;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: #55595f;
}

#app { display: flex; height: 100%; }

/* ---------------- sidebar ---------------- */
#sidebar {
  width: 320px;
  flex: 0 0 320px;
  background: var(--ui-bg);
  border-right: 1px solid var(--ui-border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.brand { padding: 16px 16px 8px; display: grid; grid-template-columns: auto 1fr; column-gap: 12px; align-items: center; }
.brand .logo { width: 44px; height: 44px; grid-row: 1 / span 2; }
.brand h1 { margin: 0; font-size: 18px; color: var(--red); }
.brand p { margin: 2px 0 0; color: #555; font-size: 12px; }

.panel { padding: 12px 16px; border-top: 1px solid var(--ui-border); }
.panel h2 {
  margin: 0 0 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: #666;
}
.row { display: flex; gap: 8px; }
.row label { flex: 1; display: flex; flex-direction: column; font-size: 12px; color: #444; gap: 2px; }
.row.bottom { align-items: flex-end; margin-top: 10px; }
.row.bottom button { height: 31px; white-space: nowrap; }
.row + .tools, .slider + .tools, .row + .check { margin-top: 10px; }
select, input[type="text"] {
  width: 100%;
  padding: 5px 6px;
  border: 1px solid #bfc4cc;
  border-radius: 4px;
  background: #fff;
  font: inherit;
  font-size: 13px;
}
.check { display: flex; gap: 8px; align-items: flex-start; margin-top: 8px; font-size: 12px; color: #444; }
.check input { margin-top: 2px; }
.slider { display: block; margin-top: 8px; font-size: 12px; color: #444; }
.slider input { width: 100%; }
.hint { margin: 8px 0 0; font-size: 11.5px; color: #666; }

.tools { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
button {
  font: inherit;
  font-size: 13px;
  padding: 6px 10px;
  border: 1px solid #bfc4cc;
  border-radius: 5px;
  background: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
button:hover { background: #eef1f5; }
button.active { background: #1f4fd6; color: #fff; border-color: #1f4fd6; }
button.primary { background: var(--red); color: #fff; border-color: var(--red); }
button.primary:hover { background: #b8161c; }
button.danger { color: #a3161c; }
button:disabled { opacity: .5; cursor: default; }

.sw { display: inline-block; width: 22px; height: 0; border-top: 4px solid var(--route); }
.sw.closure { border-top: 4px dashed var(--closure); }
.sw.route2 { border-top-color: #d61f26; }
.sw.separation { border-top: 4px dotted var(--area); }
.sw.area { height: 12px; border: 1px solid var(--area); background: rgba(224,138,0,.35); }
.sw.big { width: calc(var(--lu) * 12); border-top-width: calc(var(--lu) * 1.4); }
.sw.area.big { height: calc(var(--lu) * 5); border-width: calc(var(--lu) * .3); }

/* marker badges (shared between map, sidebar and legend) */
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 20%;
  font-weight: 800;
  color: #fff;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,.35);
  white-space: nowrap;
  line-height: 1;
}
.badge.startfinish { background: var(--red); }
.badge.ehbo { background: #fff; color: var(--red); border-color: var(--red); }
.badge.wc { background: #1a6fb5; }
.badge.garderobe { background: #7a4bb5; }
.badge.info { background: #0f8a5f; }
.badge.parking { background: #1f4fd6; }
.badge.hek { background: repeating-linear-gradient(135deg, #d61f26 0 6px, #fff 6px 12px); color: #000; }
.badge.vrl { background: #f2b700; color: #000; }
.badge.podium { background: #e07a00; }
.badge.text { background: rgba(255,255,255,.92); color: #000; border-color: #000; font-weight: 700; border-radius: 3px; }

.tool .badge { width: 22px; height: 22px; font-size: 9px; }
.tool .badge.text { font-size: 13px; font-family: Georgia, "Times New Roman", serif; }

/* ---------------- stage & sheet ---------------- */
#stage {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 16px;
}

.sheet {
  position: relative;
  width: calc(var(--mm) * var(--sheet-w));
  height: calc(var(--mm) * var(--sheet-h));
  background: #fff;
  box-shadow: 0 8px 30px rgba(0,0,0,.45);
  overflow: hidden;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: var(--ink);
}
#map { position: absolute; inset: 0; background: #e9ecef; }

/* Leaflet tweaks inside the sheet */
.leaflet-container { font: inherit; }
.leaflet-control-attribution { display: none; }   /* attribution is printed in the meta block */
.leaflet-marker-icon.badge-icon { background: none; border: none; }

.map-badge {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: calc(var(--lu) * 7);
  height: calc(var(--lu) * 7);
  font-size: calc(var(--lu) * 2.6);
  letter-spacing: .02em;
}
.map-badge.text {
  width: auto;
  height: auto;
  padding: calc(var(--lu) * .6) calc(var(--lu) * 1.2);
  font-size: calc(var(--lu) * 2.8);
  border-width: calc(var(--lu) * .25);
  box-shadow: none;
}
.badge-icon { --r: 3.8; }               /* badge half-size in layout units, used to place labels */
.badge-icon.kind-startfinish { --r: 4.8; }
.map-label {
  position: absolute;
  left: 50%;
  top: calc(50% + var(--lu) * var(--r));
  transform: translateX(-50%);
  font-size: calc(var(--lu) * 2.4);
  font-weight: 700;
  line-height: 1.15;
  text-align: center;
  white-space: pre;
  color: #111;
  text-shadow: 0 0 calc(var(--lu) * .6) #fff, 0 0 calc(var(--lu) * .6) #fff, 0 0 calc(var(--lu) * .6) #fff, 0 0 calc(var(--lu) * 1) #fff;
}
.map-badge.startfinish { width: calc(var(--lu) * 9); height: calc(var(--lu) * 9); font-size: calc(var(--lu) * 2.2); }
.map-label.pos-above { top: auto; bottom: calc(50% + var(--lu) * var(--r)); }
.map-label.pos-left, .map-label.pos-right { top: 50%; transform: translateY(-50%); }
.map-label.pos-left { left: auto; right: calc(50% + var(--lu) * (var(--r) + 0.6)); text-align: right; }
.map-label.pos-right { left: calc(50% + var(--lu) * (var(--r) + 0.6)); text-align: left; }

.arrow-icon { background: none; border: none; }
.arrow-icon svg { width: 100%; height: 100%; display: block; }

.leaflet-popup-content { margin: 10px 12px; font-size: 13px; }
.popup-form { display: flex; flex-direction: column; gap: 6px; min-width: 200px; }
.popup-form textarea, .popup-form select { font: inherit; font-size: 13px; padding: 4px 6px; border: 1px solid #bfc4cc; border-radius: 4px; width: 100%; }
.popup-form textarea { resize: vertical; min-height: 44px; }
.popup-form .tools { margin: 2px 0 0; }

/* ---------------- legend ---------------- */
.legend {
  position: absolute;
  z-index: 2000;
  top: calc(var(--lu) * 8);
  right: calc(var(--lu) * 8);
  bottom: calc(var(--lu) * 8);
  width: calc(var(--lu) * 105);
  background: #fff;
  border: calc(var(--lu) * .4) solid var(--ink);
  padding: calc(var(--lu) * 6) calc(var(--lu) * 6) calc(var(--lu) * 5);
  display: flex;
  flex-direction: column;
  gap: calc(var(--lu) * 2.5);
  font-size: calc(var(--lu) * 3.4);
  line-height: 1.3;
  overflow: hidden;
}
.legend [contenteditable] { outline: none; border-radius: 2px; }
.legend [contenteditable]:hover { box-shadow: inset 0 0 0 1px rgba(31,79,214,.35); }
.legend [contenteditable]:focus { box-shadow: inset 0 0 0 1px #1f4fd6; }
.legend [contenteditable]:empty::before { content: attr(data-placeholder); color: #999; }

.legend-title {
  font-size: calc(var(--lu) * 8.5);
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: .01em;
  color: var(--red);
  line-height: 1.05;
}
.legend-sub { font-size: calc(var(--lu) * 3.6); color: #444; margin-bottom: calc(var(--lu) * 3); }
.legend-item { display: flex; align-items: center; gap: calc(var(--lu) * 3); }
#legendMarkers { display: flex; flex-direction: column; gap: calc(var(--lu) * 2.2); }
.legend-item > .sw { flex: 0 0 auto; margin-top: calc(var(--lu) * 1.6); }
.legend-item .badge { flex: 0 0 auto; width: calc(var(--lu) * 7); height: calc(var(--lu) * 7); font-size: calc(var(--lu) * 2.6); border-width: calc(var(--lu) * .4); }
.legend-item .badge.text { width: auto; padding: 0 calc(var(--lu) * 1.2); }
.legend-small { font-size: calc(var(--lu) * 2.8); color: #555; }
.legend-head {
  margin-top: calc(var(--lu) * 3);
  font-weight: 900;
  text-transform: uppercase;
  color: var(--red);
  font-size: calc(var(--lu) * 4.4);
}
.legend-text { white-space: pre-wrap; font-size: calc(var(--lu) * 3.2); }
.legend-footer {
  margin-top: auto;
  font-weight: 900;
  text-transform: uppercase;
  color: var(--red);
  font-size: calc(var(--lu) * 4.2);
}
.hidden { display: none !important; }

/* ---------------- map meta block ---------------- */
.mapmeta {
  position: absolute;
  z-index: 2000;
  left: calc(var(--lu) * 8);
  bottom: calc(var(--lu) * 8);
  background: rgba(255,255,255,.95);
  border: calc(var(--lu) * .3) solid var(--ink);
  padding: calc(var(--lu) * 2.5) calc(var(--lu) * 3.5);
  display: flex;
  gap: calc(var(--lu) * 3.5);
  align-items: center;
  font-size: calc(var(--lu) * 2.6);
  line-height: 1.35;
}
.north svg { width: calc(var(--lu) * 9); height: calc(var(--lu) * 13.5); display: block; }
.meta-scale { font-weight: 900; font-size: calc(var(--lu) * 3.6); }
.scalebar { display: flex; height: calc(var(--lu) * 1.6); border: calc(var(--lu) * .25) solid #111; margin-top: calc(var(--lu) * 1); }
.scalebar span { flex: 1; }
.scalebar span:nth-child(odd) { background: #111; }
.scalebar-labels { display: flex; justify-content: space-between; font-size: calc(var(--lu) * 2.2); margin-bottom: calc(var(--lu) * 1); }
.meta-line[contenteditable] { outline: none; }
.meta-line[contenteditable]:focus { box-shadow: inset 0 0 0 1px #1f4fd6; }
.meta-attr { color: #555; font-size: calc(var(--lu) * 2); }

.not-to-scale {
  position: absolute;
  z-index: 3000;
  top: 10px;
  left: 56px;
  border: none;
  background: var(--red);
  color: #fff;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 12px;
  display: none;
}
.sheet.free-zoom .not-to-scale { display: block; }
.sheet.hide-legend .legend, .sheet.hide-legend .mapmeta { display: none; }
.sheet.exporting .legend { display: flex !important; }
.sheet.exporting .mapmeta { display: flex !important; }
.leaflet-control-zoom { margin: 10px !important; }
.mode-control { margin: 0 10px 10px !important; }
.mode-control a { width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; text-decoration: none; color: #222; background: #fff; }
.mode-control a svg { width: 18px; height: 18px; }
button svg { width: 15px; height: 15px; flex: 0 0 auto; }
.mode-control a.active { background: #1f4fd6; color: #fff; }
.mode-control a:hover { background: #eef1f5; }
.mode-control a.active:hover { background: #1a44b8; }
.mode-control a.disabled { color: #b5bac2; pointer-events: none; }

/* during export: no editing affordances */
.sheet.exporting [contenteditable]:hover,
.sheet.exporting [contenteditable]:focus { box-shadow: none; }
.sheet.exporting .leaflet-pm-draggable,
.sheet.exporting .marker-icon,
.sheet.exporting .leaflet-interactive.leaflet-pm-invalid { display: none !important; }

/* ---------------- busy overlay ---------------- */
#busy {
  position: absolute; inset: 0;
  background: rgba(20,22,26,.6);
  display: flex; flex-direction: column; gap: 14px;
  align-items: center; justify-content: center;
  color: #fff; font-size: 15px; font-weight: 600;
  z-index: 5000;
}
.spinner {
  width: 36px; height: 36px; border-radius: 50%;
  border: 4px solid rgba(255,255,255,.25); border-top-color: #fff;
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.badge svg { width: 62%; height: 62%; display: block; }
.badge.startfinish svg { width: 80%; height: 80%; }
