/* ============================================================
   Dinkum Town Planner — sepia surveyor aesthetic.
   Hand-drawn field-journal feel; never generic app-blue.
   ============================================================ */
:root {
  --paper: #f1e3c4;
  --paper-shade: #e6d4ad;
  --paper-deep: #d8c190;
  --ink: #2a1d10;
  --ink-soft: #4a3826;
  --ink-faint: #6b563c;
  --rust: #8b2e1f;
  --sage: #6b7f5c;
  --olive: #97863f;
  --slate: #4a5660;
  --terra: #b86b4a;
  --water: #5b7b8c;
  --gold: #b89233;
  --tile: 16px;
  --noise: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='180' height='180'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' seed='4'/><feColorMatrix values='0 0 0 0 0.16 0 0 0 0 0.11 0 0 0 0 0.06 0 0 0 0.05 0'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font-family: 'EB Garamond', Georgia, serif;
  color: var(--ink);
  background: #1a1006;
  overscroll-behavior: none;
  touch-action: manipulation; /* none ONLY on the canvas; panels must scroll */
  -webkit-user-select: none; user-select: none;
}
kbd {
  font-family: 'JetBrains Mono', monospace; font-size: 0.82em;
  background: var(--paper-deep); border: 1px solid var(--ink-soft);
  border-radius: 3px; padding: 0 4px;
}

#app { display: grid; grid-template-rows: auto 1fr; height: 100vh; height: 100dvh; }

/* ---------- TOP BAR ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px 18px; flex-wrap: wrap;
  padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--paper); background-image: var(--noise);
  border-bottom: 1.5px solid var(--ink); position: relative; z-index: 30;
}
.brand { display: flex; align-items: baseline; gap: 10px; flex: 1 1 auto; min-width: 0; }
.brand h1 {
  font-family: 'DM Serif Display', serif; font-size: clamp(17px, 4.5vw, 26px);
  margin: 0; line-height: 1; white-space: nowrap;
}
.plan-name {
  font-family: 'EB Garamond', serif; font-size: 15px; font-style: italic;
  color: var(--rust); background: transparent; border: none;
  border-bottom: 1px dashed transparent; cursor: pointer; padding: 2px 2px 0;
  max-width: 38vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.plan-name:hover { border-bottom-color: var(--rust); }
.data-ver {
  font-family: 'JetBrains Mono', monospace; font-size: 9.5px; letter-spacing: 0.12em;
  text-transform: uppercase; color: var(--ink-faint); white-space: nowrap;
}
@media (max-width: 620px) { .data-ver { display: none; } }

.tools { display: flex; align-items: center; gap: 6px 10px; flex-wrap: wrap; }
.grp { display: inline-flex; }
.tbtn, .seg-btn {
  font-family: 'JetBrains Mono', monospace; font-size: 11px; letter-spacing: 0.04em;
  font-weight: 500; text-transform: uppercase;
  background: var(--paper-shade); color: var(--ink);
  border: 1.5px solid var(--ink); cursor: pointer;
  min-width: 38px; min-height: 38px; padding: 0 11px;
  transition: background .12s, transform .08s;
}
.grp .tbtn { border-right-width: 0; }
.grp .tbtn:last-child { border-right-width: 1.5px; }
.tbtn:hover, .seg-btn:hover { background: var(--paper-deep); }
.tbtn:active, .seg-btn:active { transform: translateY(1px); }
.tbtn:disabled { opacity: .4; cursor: default; }
.tbtn.on { background: var(--ink); color: var(--paper); }
.seg { display: inline-flex; }
.seg-btn { border-right-width: 0; }
.seg-btn:last-child { border-right-width: 1.5px; }
.seg-btn.on { background: var(--rust); color: var(--paper); border-color: var(--rust); }

/* ---------- MAIN ---------- */
#main { position: relative; overflow: hidden; background: #21160a; }
/* Infinite canvas: parchment fills the whole viewport; the grid lives in the
   SVG (a tiling pattern) so it pans/zooms exactly with the content. */
#viewport {
  position: absolute; inset: 0; overflow: hidden; cursor: grab; touch-action: none;
  background-color: var(--paper);
  background-image:
    radial-gradient(ellipse at top, rgba(184,107,74,.08), transparent 70%),
    radial-gradient(ellipse at bottom, rgba(139,46,31,.05), transparent 70%),
    var(--noise);
}
#viewport.grabbing { cursor: grabbing; }
svg#map {
  position: absolute; inset: 0; width: 100%; height: 100%;
  display: block; touch-action: none;
}

/* ---------- SVG MAP ELEMENTS ---------- */
.item { cursor: pointer; }
#viewport.grabbing .item { cursor: grabbing; }
.b-rect { transition: filter .12s; }
.item-fixed:hover .b-rect { filter: brightness(1.12); }
.lbl {
  font-family: 'JetBrains Mono', monospace; text-anchor: middle;
  dominant-baseline: middle; pointer-events: none; font-weight: 500; letter-spacing: .03em;
}
.lbl.name { fill: #fff; }
.lbl.sub { fill: rgba(255,255,255,.85); }
.lbl.on-region.name { fill: #2a1d10; font-family: 'DM Serif Display', serif; letter-spacing: 0; }
.lbl.on-region.sub { fill: var(--ink-soft); letter-spacing: .1em; }
.layer-grid { pointer-events: none; }
.range-rect { pointer-events: none; }
.sel-rect, .ghost-rect { pointer-events: none; }
.handle { cursor: nwse-resize; }
.handle[data-handle="n"], .handle[data-handle="s"] { cursor: ns-resize; }
.handle[data-handle="e"], .handle[data-handle="w"] { cursor: ew-resize; }
.handle[data-handle="ne"], .handle[data-handle="sw"] { cursor: nesw-resize; }

/* layer visibility */
svg#map.hide-grid .layer-grid,
svg#map.hide-zones .layer-zones,
svg#map.hide-roads .layer-roads,
svg#map.hide-regions .layer-regions,
svg#map.hide-ranges .layer-ranges,
svg#map.hide-labels .lbl { display: none; }

/* ---------- PANELS (palette + inspector) ---------- */
.panel {
  background: var(--paper); background-image: var(--noise);
  display: flex; flex-direction: column; z-index: 20;
  min-height: 0; /* allow the inner scroll area to bound itself (grid/flex item) */
}
#palette-list, .insp-inner { min-height: 0; }
#palette { border-right: 1.5px solid var(--ink); }
#inspector { border-left: 1.5px solid var(--ink); }
/* let the panels (and modals) scroll on touch devices */
#palette-list, .insp-inner, .modal-body {
  touch-action: pan-y; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
}

.palette-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px 6px; font-family: 'DM Serif Display', serif; font-size: 17px;
}
.palette-head .x, .insp-close {
  background: transparent; border: none; font-size: 24px; line-height: 1;
  color: var(--ink-soft); cursor: pointer; padding: 0 4px;
}
#palette-search {
  margin: 0 12px 8px; padding: 9px 11px; font-family: 'EB Garamond', serif; font-size: 15px;
  border: 1.5px solid var(--ink-soft); background: var(--paper-shade); color: var(--ink);
  border-radius: 2px; outline: none;
}
#palette-search:focus { border-color: var(--rust); }
#palette-list { overflow-y: auto; flex: 1; padding: 0 8px 12px; }
.pal-cat { font-family: 'JetBrains Mono', monospace; font-size: 10px; letter-spacing: .16em;
  text-transform: uppercase; color: var(--rust); margin: 12px 6px 6px; }
.pal-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.pal-item {
  display: flex; align-items: center; gap: 8px; text-align: left;
  background: var(--paper-shade); border: 1.5px solid var(--ink-soft);
  border-radius: 3px; padding: 7px 8px; cursor: pointer; transition: background .1s, border-color .1s;
  font-family: 'EB Garamond', serif; color: var(--ink); min-height: 44px;
}
.pal-item:hover { background: var(--paper-deep); }
.pal-item.armed { border-color: var(--rust); background: #f6ead0; box-shadow: 0 0 0 2px rgba(139,46,31,.25); }
.pal-swatch { width: 16px; height: 16px; border: 1px solid var(--ink); border-radius: 2px; flex: none; }
.pal-meta { min-width: 0; line-height: 1.1; }
.pal-name { font-size: 13.5px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pal-size { font-family: 'JetBrains Mono', monospace; font-size: 9.5px; color: var(--ink-faint); }
.palette-hint { font-size: 12px; font-style: italic; color: var(--ink-faint); margin: 4px 12px 10px; line-height: 1.4; }

/* inspector */
.insp-inner { padding: 12px 14px; overflow-y: auto; flex: 1; }
.insp-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.insp-inner h2 { font-family: 'DM Serif Display', serif; font-size: 21px; margin: 0 0 2px; line-height: 1.05; }
.insp-tags { display: flex; flex-wrap: wrap; gap: 5px 8px; align-items: center; margin: 6px 0 10px; }
.pill {
  font-family: 'JetBrains Mono', monospace; font-size: 10px; letter-spacing: .08em;
  text-transform: uppercase; border: 1px solid var(--rust); color: var(--rust);
  border-radius: 99px; padding: 2px 9px;
}
.pill.muted { border-color: var(--ink-faint); color: var(--ink-faint); }
.pill.warn { border-color: var(--rust); color: #fff; background: var(--rust); }
.insp-row { display: flex; flex-wrap: wrap; gap: 6px 18px; font-family: 'JetBrains Mono', monospace;
  font-size: 11px; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-faint); margin: 6px 0; }
.insp-row strong { color: var(--ink); margin-left: 5px; }
.insp-inner p { margin: 7px 0; font-size: 14.5px; line-height: 1.5; }
.insp-inner p.notes { color: var(--ink-soft); font-style: italic; font-size: 13.5px; }
.unconf { font-family: 'JetBrains Mono', monospace; font-size: 11px; color: var(--olive); }
.insp-field { display: flex; gap: 8px; align-items: center; margin: 8px 0; }
.insp-field label { font-family: 'JetBrains Mono', monospace; font-size: 10px; letter-spacing: .1em;
  text-transform: uppercase; color: var(--ink-faint); }
.insp-field input[type=text] {
  flex: 1; padding: 6px 9px; font-family: 'EB Garamond', serif; font-size: 14px;
  border: 1.5px solid var(--ink-soft); background: var(--paper-shade); color: var(--ink); border-radius: 2px;
}
.insp-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.act {
  font-family: 'JetBrains Mono', monospace; font-size: 11px; letter-spacing: .04em; text-transform: uppercase;
  background: var(--paper-shade); border: 1.5px solid var(--ink); color: var(--ink);
  padding: 8px 10px; cursor: pointer; border-radius: 2px; min-height: 38px; flex: 1 1 auto;
}
.act:hover { background: var(--paper-deep); }
.act.danger { color: var(--rust); border-color: var(--rust); }
.act.danger:hover { background: var(--rust); color: var(--paper); }
.act.primary { background: var(--ink); color: var(--paper); }
.swatches { display: flex; flex-wrap: wrap; gap: 5px; margin: 6px 0; }
.swatch-btn { width: 26px; height: 26px; border-radius: 3px; border: 2px solid var(--ink); cursor: pointer; }
.swatch-btn.on { outline: 2px solid var(--gold); outline-offset: 1px; }

.ref-list { margin: 6px 0; padding: 0; list-style: none; }
.ref-list li { font-size: 13.5px; line-height: 1.45; padding: 5px 0; border-bottom: 1px dotted var(--paper-deep); }
.ref-list li b { font-family: 'EB Garamond'; }

/* ---------- MAP OVERLAYS ---------- */
.compass {
  position: absolute; top: 12px; right: 12px; pointer-events: none;
  background: var(--paper); border: 1.5px solid var(--ink); padding: 5px 9px;
  font-family: 'JetBrains Mono', monospace; font-size: 10px; letter-spacing: .14em;
  text-transform: uppercase; color: var(--ink); z-index: 10;
}
.compass .arrow { font-family: 'DM Serif Display', serif; font-size: 15px; margin-right: 4px; }
.stats-badge {
  position: absolute; top: 12px; left: 12px; z-index: 10;
  background: var(--paper); border: 1.5px solid var(--ink); border-radius: 3px;
  padding: 6px 10px; font-family: 'JetBrains Mono', monospace; font-size: 11px;
  color: var(--ink); cursor: pointer; text-align: left; line-height: 1.5; max-width: 60vw;
}
.stats-badge .warn-dot { color: var(--rust); font-weight: 700; }
.hint-bubble {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%);
  background: rgba(241,227,196,.95); border: 1px solid var(--ink-soft); z-index: 10;
  padding: 7px 14px; font-family: 'JetBrains Mono', monospace; font-size: 10px;
  letter-spacing: .06em; text-transform: uppercase; color: var(--ink-soft);
  pointer-events: none; max-width: 90%; text-align: center;
}
.fab {
  position: absolute; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 50%; z-index: 15;
  background: var(--rust); color: var(--paper); border: 2px solid var(--ink);
  font-size: 28px; line-height: 1; cursor: pointer; box-shadow: 0 6px 20px rgba(0,0,0,.4);
  display: none;
}
.fab:active { transform: translateY(1px); }

/* ---------- POPOVERS ---------- */
.pop {
  position: absolute; z-index: 50; background: var(--paper); background-image: var(--noise);
  border: 1.5px solid var(--ink); box-shadow: 0 12px 40px rgba(0,0,0,.4);
  min-width: 200px; padding: 8px;
}
.pop h3 { font-family: 'JetBrains Mono', monospace; font-size: 10px; letter-spacing: .14em;
  text-transform: uppercase; color: var(--rust); margin: 4px 6px 8px; }
.pop-row {
  display: flex; align-items: center; gap: 9px; padding: 8px 8px; cursor: pointer;
  font-size: 14px; border-radius: 2px;
}
.pop-row:hover { background: var(--paper-deep); }
.pop-row input { width: 16px; height: 16px; accent-color: var(--rust); }
.pop-sep { height: 1px; background: var(--ink-soft); opacity: .3; margin: 6px 2px; }

/* ---------- MODALS ---------- */
.modal-bg {
  position: fixed; inset: 0; z-index: 100; background: rgba(20,12,4,.6);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal {
  background: var(--paper); background-image: var(--noise);
  border: 1.5px solid var(--ink); box-shadow: 0 18px 60px rgba(0,0,0,.5);
  width: min(560px, 100%); max-height: 86vh; overflow: hidden; display: flex; flex-direction: column;
}
.modal-head { display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1.5px solid var(--ink); }
.modal-head h2 { font-family: 'DM Serif Display', serif; font-size: 22px; margin: 0; }
.modal-body { padding: 16px 18px; overflow-y: auto; font-size: 15px; line-height: 1.55; }
.modal-body h3 { font-family: 'JetBrains Mono', monospace; font-size: 11px; letter-spacing: .12em;
  text-transform: uppercase; color: var(--rust); margin: 16px 0 6px; }
.tpl-card, .layout-row {
  border: 1.5px solid var(--ink-soft); border-radius: 3px; padding: 12px 14px; margin-bottom: 10px;
  cursor: pointer; background: var(--paper-shade); transition: background .1s, border-color .1s;
}
.tpl-card:hover, .layout-row:hover { background: var(--paper-deep); border-color: var(--ink); }
.tpl-card strong, .layout-row strong { font-family: 'DM Serif Display', serif; font-size: 17px; }
.tpl-card p { margin: 4px 0 0; font-size: 13.5px; color: var(--ink-soft); }
.layout-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.layout-row .meta { font-family: 'JetBrains Mono', monospace; font-size: 10.5px; color: var(--ink-faint); }
.layout-row .del { background: transparent; border: none; color: var(--rust); cursor: pointer; font-size: 18px; padding: 4px 8px; }
.field-input { width: 100%; padding: 10px 12px; font-family: 'EB Garamond', serif; font-size: 16px;
  border: 1.5px solid var(--ink); background: var(--paper-shade); color: var(--ink); border-radius: 2px; }
.modal textarea { width: 100%; min-height: 160px; font-family: 'JetBrains Mono', monospace; font-size: 12px;
  border: 1.5px solid var(--ink); background: var(--paper-shade); color: var(--ink); padding: 10px; border-radius: 2px; }

/* ---------- TOASTS ---------- */
#toast-root { position: fixed; bottom: calc(20px + env(safe-area-inset-bottom)); left: 50%;
  transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast {
  background: var(--ink); color: var(--paper); font-family: 'JetBrains Mono', monospace; font-size: 12px;
  letter-spacing: .03em; padding: 10px 16px; border-radius: 4px; box-shadow: 0 8px 24px rgba(0,0,0,.4);
  animation: toast-in .2s ease; max-width: 86vw;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

/* ============================================================
   RESPONSIVE
   ============================================================ */
/* Desktop: dock palette left, inspector right */
@media (min-width: 900px) {
  #main { display: grid; grid-template-columns: auto 1fr auto; }
  #palette { position: relative; width: 300px; max-height: 100%; }
  #inspector { position: relative; width: 340px; max-height: 100%; }
  body.mode-view #palette { display: none; }
  body.pal-collapsed #palette { display: none; }
  body.insp-collapsed #inspector { display: none; }
  .fab { display: none !important; }
}
/* the collapse buttons live in the toolbar; show their active state */
#btn-palette.off, #btn-inspector.off { opacity: .55; }

/* Mobile / tablet: panels become bottom sheets */
@media (max-width: 899px) {
  #palette, #inspector {
    position: absolute; left: 0; right: 0; bottom: 0; max-height: 62%;
    border-top: 1.5px solid var(--ink); border-left: none; border-right: none;
    border-radius: 14px 14px 0 0; transform: translateY(110%); transition: transform .24s ease;
    box-shadow: 0 -10px 40px rgba(0,0,0,.4); padding-bottom: env(safe-area-inset-bottom);
  }
  #palette.open, #inspector.open { transform: translateY(0); }
  #palette::before, #inspector::before {
    content: ""; position: absolute; top: 7px; left: 50%; transform: translateX(-50%);
    width: 40px; height: 4px; border-radius: 4px; background: var(--ink-soft); opacity: .5;
  }
  .palette-head { padding-top: 16px; }
  body.mode-edit .fab { display: block; }
  .pal-grid { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 480px) {
  .brand h1 { font-size: 17px; }
  .topbar { gap: 6px 8px; }
  .tbtn, .seg-btn { min-width: 36px; min-height: 36px; padding: 0 9px; font-size: 10.5px; }
}
