/* The page is an app that fills the window: a slim bar along the top, the settings down the
   left, and the map on a slate desk filling the rest. Nothing scrolls but the settings (only
   if the window is very short) and the symbol palette. On a phone the page stacks and scrolls. */
:root {
  --ink: #1a1714;
  --paper: #f7f5f0;
  --card: #fffefb;
  --rule: #dcd6ca;
  --faint: #625a50;
  --focus: #2563eb;
  --desk: #2f3a3f; /* the slate the map lies on */
  --desk-ink: #dde3e5; /* text on the desk */
  --desk-faint: #a9b4b8;
  --brass: #b8925a; /* one warm note: the rule under the name, the open menu */
  --bar: 48px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { color: var(--ink); font: 14px/1.4 system-ui, sans-serif; background: var(--paper); }
/* The map page (body.app); other pages that share this sheet, such as the gallery, keep
   their own page layout. */
html:has(body.app), body.app { height: 100%; }
body.app {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--desk);
}
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* Top bar. */
.app .top { flex: none; height: var(--bar); display: flex; align-items: center; gap: 20px; padding: 0 16px; background: var(--paper); border-bottom: 1px solid var(--rule); }
.brand { margin: 0; font-family: "IM Fell English SC", Georgia, serif; font-weight: 400; font-size: 1.45rem; letter-spacing: 0.02em; line-height: 1; padding-bottom: 3px; border-bottom: 2px solid var(--brass); white-space: nowrap; }
.top-actions { display: flex; gap: 4px; margin-left: auto; }
.top-btn { height: 32px; padding: 0 12px; font: inherit; font-weight: 600; color: var(--ink); background: transparent; border: 1px solid transparent; border-radius: 6px; cursor: pointer; }
.top-btn:hover { background: #ece7dc; }
.top-btn[aria-expanded="true"] { border-color: var(--brass); background: var(--card); }
.top-nav { display: flex; gap: 14px; padding-left: 16px; border-left: 1px solid var(--rule); }
.top-nav a, .fine a, #keep-status a { color: var(--ink); font-weight: 600; }

/* Settings on the left, the map beside them. */
.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 272px minmax(0, 1fr); }
.panel { min-height: 0; overflow-y: auto; background: var(--paper); border-right: 1px solid var(--rule); padding: 10px 14px 12px; display: flex; flex-direction: column; gap: 8px; }
.seed-block { display: flex; flex-direction: column; gap: 6px; }
.seed-block small { color: var(--faint); font-size: 0.74rem; line-height: 1.3; }
.group { margin: 0; padding: 6px 0 0; border: 0; border-top: 1px solid var(--rule); display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.group legend { float: left; width: 100%; padding: 0; font-family: "IM Fell English", Georgia, serif; font-size: 1.05rem; }
.group legend + * { clear: both; }
.field { display: flex; flex-direction: column; gap: 0; min-width: 0; }
.field > span:first-child { font-weight: 600; font-size: 0.82rem; display: flex; justify-content: space-between; }
.field output { font-weight: 400; color: var(--faint); font-variant-numeric: tabular-nums; }
.pairs { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 8px; }
.checks { display: flex; flex-wrap: wrap; gap: 2px 12px; }
input[type="number"], input[type="text"], select { width: 100%; padding: 4px 7px; font: inherit; color: var(--ink); background: #fff; border: 1px solid var(--rule); border-radius: 5px; }
input[type="range"] { width: 100%; height: 20px; margin: 0; accent-color: var(--ink); }
.check { display: flex; align-items: center; gap: 6px; font-size: 0.85rem; }
.generate { padding: 8px 14px; font: inherit; font-weight: 600; border: 0; border-radius: 6px; background: var(--ink); color: var(--paper); cursor: pointer; }
.generate:hover { background: #3a332c; }

/* Share, keep and download: small panels dropping from the top bar. */
.pop { inset: auto; top: calc(var(--bar) + 6px); right: 16px; margin: 0; width: 300px; padding: 14px; background: var(--card); color: var(--ink); border: 1px solid var(--rule); border-radius: 8px; box-shadow: 0 10px 30px rgba(20, 28, 32, 0.28); display: none; flex-direction: column; gap: 8px; }
.pop:popover-open { display: flex; }
.group-title { margin: 0; font-family: "IM Fell English", Georgia, serif; font-weight: 400; font-size: 1.15rem; }
.pop output { font-size: 0.8rem; color: var(--faint); min-height: 1.2em; }
@media (prefers-reduced-motion: no-preference) {
  .pop:popover-open { animation: drop-in 0.14s ease-out; }
  @keyframes drop-in { from { opacity: 0; transform: translateY(-4px); } }
}

/* The map area: tools, then the map filling what is left, then the caption. */
.stage { margin: 0; min-width: 0; min-height: 0; display: flex; flex-direction: column; align-items: stretch; gap: 6px; padding: 10px 16px 8px; color: var(--desk-ink); }
.work { flex: 1; min-height: 0; }
/* The map box takes its shape from the map (set from script) and fits the room left. */
.map-frame { display: flex; justify-content: center; width: 100%; height: 100%; }
.map-box { position: relative; overflow: hidden; overflow: clip; height: 100%; max-width: 100%; background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,0.25), 0 12px 32px rgba(0,0,0,0.35); }
.map, .map svg { display: block; width: 100%; height: 100%; }
/* The map box and map clip what sticks out rather than hiding it: a hidden overflow can
   still be scrolled, and Safari scrolled it when an item was pressed, shifting the map.
   The map is drawn a quarter of a view past each edge of the box (MARGIN in zoom.ts), on
   its own graphics layer, so a drag or pinch only moves the picture already drawn. The
   transform origin is the box's top-left corner (25% of 150%). */
.map { position: relative; overflow: hidden; overflow: clip; }
.map svg { position: absolute; left: -25%; top: -25%; width: 150%; height: 150%; overflow: hidden; will-change: transform; transform-origin: 16.6667% 16.6667%; }
.relief { position: absolute; inset: 0; width: 100%; height: 100%; mix-blend-mode: multiply; opacity: 0.55; pointer-events: none; }
.stage figcaption { flex: none; font-size: 0.78rem; color: var(--desk-faint); text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Fade in each newly drawn map. */
.map.fresh { animation: ink-in 0.4s ease-out; }
@keyframes ink-in { from { opacity: 0.25; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .map.fresh { animation: none; } }

/* Editing and export. */
.plain { padding: 6px 10px; font: inherit; font-size: 0.9rem; color: var(--ink); background: var(--card); border: 1.5px solid var(--ink); border-radius: 5px; cursor: pointer; }
.plain:hover:not(:disabled) { background: #efe6d2; }
.plain:disabled { opacity: 0.4; cursor: default; }
.plain[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.toolbar { width: 100%; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.edit-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; flex: 1; }
.edit-tools[hidden], .rename[hidden] { display: none; }
.rename { display: flex; align-items: center; gap: 6px; }
.rename input { padding: 5px 8px; font: inherit; border: 1px solid var(--rule); border-radius: 5px; min-width: 12em; }
.hint { margin: 0; font-size: 0.82rem; color: var(--faint); flex-basis: 100%; }
.map.editing { touch-action: none; cursor: crosshair; }
/* No text selection on the map: dragging across a name would start selecting it (and
   Safari would scroll the page to follow). */
.map { -webkit-user-select: none; user-select: none; }
.map.editing [data-key] { cursor: grab; }
.map [data-key].selected { filter: drop-shadow(0 0 2px var(--focus)) drop-shadow(0 0 2px var(--focus)); }
.fine { color: var(--faint); font-size: 0.76rem; }
#share-link { font-size: 0.8rem; }
#keep-status { font-size: 0.8rem; color: var(--faint); }

/* Zoom controls, over the map's top-right corner. */
.zoom-ctl { flex: none; margin-left: auto; display: flex; align-items: center; gap: 4px; }
.zoom-ctl button { min-width: 32px; height: 32px; padding: 0 8px; font: inherit; font-size: 1.05rem; line-height: 1; color: var(--ink); background: var(--card); border: 1.5px solid var(--ink); border-radius: 4px; cursor: pointer; }
.zoom-ctl button#zoom-fit { font-size: 0.8rem; }
.zoom-ctl button:hover:not(:disabled) { background: #efe6d2; }
.zoom-ctl button:disabled { opacity: 0.35; cursor: default; }
.zoom-ctl output { min-width: 3.4em; text-align: center; font-size: 0.8rem; color: var(--desk-faint); font-variant-numeric: tabular-nums; }
/* Zoomed in, a drag moves around the map instead of scrolling the page. */
.map.zoomed { touch-action: none; cursor: grab; }
.map.zoomed.panning { cursor: grabbing; }

/* Symbol palette for adding drawings to the map. */
.palette { flex-basis: 100%; background: var(--card); border: 1px solid var(--rule); border-radius: 8px; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.palette[hidden] { display: none; }
.pal-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; font-size: 0.9rem; }
.pal-head label { display: flex; align-items: center; gap: 6px; }
.pal-head select { width: auto; }
.pal-head output { min-width: 3em; color: var(--faint); }
.pal-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 6px; max-height: 220px; overflow-y: auto; }
.pal-item { aspect-ratio: 1; padding: 6px; background: #fff; border: 1.5px solid var(--rule); border-radius: 5px; cursor: pointer; }
.pal-item:hover { border-color: var(--ink); }
.pal-item[aria-selected="true"] { border-color: var(--focus); box-shadow: 0 0 0 2px color-mix(in srgb, var(--focus) 35%, transparent); }
.pal-item svg { display: block; width: 100%; height: 100%; }
.map.editing.placing, .map.editing.placing [data-key] { cursor: copy; }

/* Picking box for selecting several items. */
.pick-box { position: absolute; border: 1.5px dashed var(--focus); background: color-mix(in srgb, var(--focus) 10%, transparent); pointer-events: none; }
.map.editing.area { touch-action: none; cursor: crosshair; }

/* Box around the picked items, with a resize handle on each corner. */
.sel-box { position: absolute; border: 1.5px dashed var(--focus); pointer-events: none; box-sizing: border-box; }
.sel-box[hidden] { display: none; }
/* The handles sit just outside the box's corners, so they never cover the picked items
   themselves (a small tree is smaller than a handle). */
.sel-box { --handle: 11px; }
.sel-box [data-corner] { position: absolute; width: var(--handle); height: var(--handle); background: #fff; border: 2px solid var(--focus); border-radius: 2px; pointer-events: auto; touch-action: none; box-sizing: border-box; }
.sel-box [data-corner="nw"] { left: calc(-1 * var(--handle)); top: calc(-1 * var(--handle)); cursor: nwse-resize; }
.sel-box [data-corner="se"] { right: calc(-1 * var(--handle)); bottom: calc(-1 * var(--handle)); cursor: nwse-resize; }
.sel-box [data-corner="ne"] { right: calc(-1 * var(--handle)); top: calc(-1 * var(--handle)); cursor: nesw-resize; }
.sel-box [data-corner="sw"] { left: calc(-1 * var(--handle)); bottom: calc(-1 * var(--handle)); cursor: nesw-resize; }
@media (pointer: coarse) { .sel-box { --handle: 22px; } }

/* Tidier editing layout: fixed toolbar, floating bar for picked items, status over the map,
   palette beside the map. */
.edit-tools { flex-wrap: wrap; row-gap: 6px; }
.tool-group { display: flex; gap: 6px; padding-right: 8px; border-right: 1px solid var(--rule); }
.tool-group:last-of-type { border-right: 0; }
details.keys { flex-basis: 100%; font-size: 0.8rem; color: var(--desk-faint); }
details.keys summary { cursor: pointer; width: max-content; }
details.keys p { margin: 4px 0 0; }
.work { width: 100%; display: flex; gap: 14px; align-items: stretch; justify-content: center; }
.work > .map-frame { flex: 1 1 auto; min-width: 0; }
.palette { flex: 0 0 290px; align-self: flex-start; max-height: 100%; overflow-y: auto; color: var(--ink); }
.palette .pal-grid { max-height: none; }
@media (max-width: 1100px) { .palette { flex-basis: 250px; } }
.sel-bar { position: absolute; z-index: 3; display: flex; flex-wrap: wrap; gap: 4px; padding: 4px; max-width: calc(100% - 16px); background: rgba(255, 253, 248, 0.97); border: 1px solid var(--rule); border-radius: 7px; box-shadow: 0 2px 8px rgba(0,0,0,0.15); }
.sel-bar[hidden] { display: none; }
.sel-bar .plain { padding: 3px 8px; font-size: 0.78rem; }
.plain.danger { border-color: #9b2c1c; color: #9b2c1c; }
.map-status { flex: 1 1 auto; min-width: 0; display: flex; align-items: center; overflow: hidden; }
.status-hint { margin: 0; min-width: 0; font-size: 0.8rem; color: var(--desk-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status-hint:empty { display: none; }
.map-status .rename { min-width: 0; flex-wrap: nowrap; }
.map-status .rename input { min-width: 5em; flex: 1 1 12em; padding: 3px 6px; }
.map-status .rename .plain { padding: 3px 8px; font-size: 0.8rem; white-space: nowrap; }
.map-status .rename:not([hidden]) + .status-hint { display: none; }
.toolbar { align-items: flex-start; }

/* The map and the bar above it share one column exactly as wide as the map, so the hint lines
   up with its left edge and the zoom controls with its right. */
.map-frame { flex-direction: column; align-items: center; }
.map-column { position: relative; height: 100%; max-width: 100%; padding-top: 42px; }
/* The bar sits in room kept above the map, stretched between the column's edges, so it takes
   the map's width but can never widen it (a long hint is cut short instead). */
.map-bar { position: absolute; top: 0; left: 0; right: 0; height: 36px; display: flex; align-items: center; gap: 10px; }
.map-column > .map-box { height: 100%; }
.plain[aria-pressed="true"]:hover:not(:disabled) { background: #3a332c; }


/* The rename box over the desk. */
.map-status .rename label { font-size: 0.8rem; color: var(--desk-ink); }

/* Phones and narrow windows: the page stacks and scrolls, the map first. */
@media (max-width: 760px) {
  body.app { display: block; height: auto; overflow: visible; }
  .top { position: sticky; top: 0; z-index: 5; gap: 6px; padding: 0 8px; }
  .brand { font-size: 1.05rem; }
  .top-actions { gap: 0; }
  .top-btn { padding: 0 6px; font-size: 0.85rem; }
  .top-nav { padding-left: 6px; font-size: 0.85rem; }
  .layout { display: flex; flex-direction: column; }
  .stage { order: -1; padding: 10px; }
  .panel { overflow: visible; border-right: 0; }
  .map-frame { height: auto; }
  .map-box { height: auto; width: 100%; }
  .map-column { width: 100%; height: auto; }
  .map-column > .map-box { height: auto; }
  .work { flex-direction: column; }
  .palette { flex-basis: auto; max-height: 45vh; }
  .pop { left: 10px; right: 10px; width: auto; }
}
/* The narrowest phones: the gallery is still reachable from Keep. */
@media (max-width: 400px) { .top-nav { display: none; } }
