/* The infinite canvas. The site is one boundless 2-D plane: #viewport is the
   fixed window, #world is the pannable/zoomable layer transformed by canvas.js.
   Colours and the active serif family are shared with the rest of the site. */
:root {
  --ink: #1a1a1a;
  --muted: #6b6b6b;
  --faint: #eaeaea;
  --bg: #fdfdfc;
  --accent: #1a5fb4;
  --danger: #b4291a;
  --beacon: #000; /* beacon star markers */
  --serif: "Noto Serif", Georgia, "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  line-height: 1.25;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
}

/* The window onto the canvas. */
#viewport {
  position: fixed;
  inset: 0;
  cursor: grab;
  touch-action: none;
  background-color: var(--bg);
}
#viewport.panning { cursor: grabbing; }

/* The transformed layer. transform-origin 0 0 keeps the math simple: screen =
   world * scale + translate. */
#world {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  /* will-change is toggled on by canvas.js only during active pan/zoom. Leaving
     it on permanently caches the layer's bitmap at one scale, so zooming in
     stretches that bitmap and text/SVG look blurry; clearing it when idle lets
     the browser re-rasterize crisply at the settled zoom. */
}

/* A curved-diamond "sparkle" marking the world origin (0,0): the canvas centre
   and the point visitors spawn on. It lives in the world, so it scales with the
   zoom like any item; pointer-events:none keeps it from blocking clicks. */
#origin-dot {
  position: absolute;
  left: 0;
  top: 0;
  width: 26px;
  height: 26px;
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 9000000;
}
/* The origin star is two stacked copies of the same path. Both fill black and
   paint their stroke behind the fill, so the stroke only shows as a ring outside
   the star: the back copy's wide black stroke forms the outer outline, and the
   front copy's narrower white stroke punches a white gap between that outline
   and the black star. overflow:visible lets the rings extend past the viewBox. */
#origin-dot svg { display: block; width: 100%; height: 100%; overflow: visible; }
#origin-dot path { fill: #000; paint-order: stroke; stroke-linejoin: round; }
#origin-dot .star-ring { stroke: #000; stroke-width: 6; }
#origin-dot .star-core { stroke: #fff; stroke-width: 3; }

/* Items are absolutely positioned in world coordinates; canvas.js sets left/
   top/width from their data-* attributes. */
.item {
  position: absolute;
  max-width: none;
}

/* A beacon is a named landmark, drawn as a smaller indigo cousin of the origin
   star. Visitors see it as a non-interactive marker (it never blocks panning);
   the owner can drag, rename and delete it. */
.item-beacon { width: 22px; height: 22px; pointer-events: none; }
.item-beacon .item-body { width: 100%; height: 100%; line-height: 0; }
.beacon-star {
  display: block;
  width: 100%;
  height: 100%;
  fill: var(--beacon);
  stroke: #fff;        /* white outline keeps it visible on dark images */
  stroke-width: 1.5;
  paint-order: stroke; /* outline behind the fill */
  stroke-linejoin: round;
}
body[data-authed]:not(.preview) .item-beacon { pointer-events: auto; }
.item-beacon .item-resize { display: none !important; } /* fixed size */

/* A beacon's (and the origin's) "view point": where [text](id) links land. The
   handle is draggable independently of the star; a dashed line connects them.
   Both are owner-only editing affordances, hidden in preview and for visitors. */
.beacon-view, .beacon-link { display: none; }
body[data-authed]:not(.preview) .beacon-view,
body[data-authed]:not(.preview) .beacon-link { display: block; }
.beacon-view {
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--beacon);
  box-shadow: 0 1px 2px #0000002a;
  transform: translate(-50%, -50%);
  cursor: move;
  pointer-events: auto;
  z-index: 4;
}
.beacon-link {
  position: absolute;
  height: 0;
  border-top: 1.5px dashed var(--beacon);
  transform-origin: 0 0;
  pointer-events: none;
  z-index: 3;
}

.item-body { position: relative; }

.item-text .item-body {
  background: #ffffffcc;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0.4rem 1.1rem;
  word-wrap: break-word;
  overflow-wrap: break-word;
  transition: box-shadow 0.12s ease;
}

/* In admin mode, hovering a text box tints it with a faint translucent black so
   it reads as editable. An inset shadow layers over the box's own background,
   so it shows on white and adaptive-transparent boxes alike. */
body[data-authed]:not(.preview) .item-text:hover .item-body {
  box-shadow: inset 0 0 0 1000px rgba(0, 0, 0, 0.06);
}

/* Adaptive text boxes go transparent so the image behind shows through; the
   glyph colour and (over images only) drop shadow are set inline by canvas.js. */
.item-text.adaptive .item-body { background: transparent; border-color: transparent; }
body[data-authed] .item-text.adaptive .item-body { border-color: transparent; }

.item-image .item-body { line-height: 0; }
/* Off-screen images are swapped to a 1x1 placeholder to free decoded memory;
   a faint fill keeps the box visible during fast pans before it reloads. */
.item-image.culled img { background: #f1f1ef; }
.item-image img {
  width: 100%;
  height: auto;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
}

/* Typographic tightening so a text box reads as a self-contained note rather
   than a full document. */
.item-text :first-child { margin-top: 0; }
.item-text :last-child { margin-bottom: 0; }
/* A paragraph break renders as the same gap as a single line break (text boxes
   use hard wraps, so Enter already makes a <br>). */
.item-text p { margin: 0; }
.item-text img { max-width: 100%; height: auto; }
.item-text pre { overflow-x: auto; }

/* The fixed corner label. */
.brand {
  position: fixed;
  left: 1.1rem;
  top: 0.9rem;
  font-family: var(--serif);
  font-size: 1.05rem;
  color: var(--ink);
  text-decoration: none;
  z-index: 50;
  pointer-events: auto;
}
.brand:hover { text-decoration: underline; }

/* Transient hint toast (e.g. "Click to place"). */
.hint {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  font-family: var(--sans);
  font-size: 0.8rem;
  padding: 0.45rem 0.8rem;
  border-radius: 999px;
  z-index: 60;
  opacity: 0;
  transition: opacity 0.15s ease;
  pointer-events: none;
}
.hint.show { opacity: 0.92; }

/* ---- Admin chrome ---- */

#toolbar {
  position: fixed;
  right: 1rem;
  top: 0.9rem;
  display: flex;
  gap: 0.4rem;
  align-items: center;
  z-index: 70;
  font-family: var(--sans);
}
#toolbar form.inline { margin: 0; }
#toolbar button {
  font-family: var(--sans);
  font-size: 0.82rem;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--faint);
  border-radius: 6px;
  padding: 0.38rem 0.7rem;
  cursor: pointer;
  box-shadow: 0 1px 2px #0000000d;
}
#toolbar button:hover { border-color: #d0d0cc; background: #fafaf8; }

/* Editing affordances appear only in admin mode — and not while previewing,
   where the owner sees the canvas exactly as a visitor does. */
.item-del, .item-resize { display: none; }

body[data-authed]:not(.preview) .item { cursor: move; user-select: none; }
body[data-authed]:not(.preview) .item-text .item-body { border-color: var(--faint); }
body[data-authed]:not(.preview) .item:hover .item-del,
body[data-authed]:not(.preview) .item:hover .item-resize { display: block; }

/* In preview, only the toggle stays — everything else in the toolbar hides. */
body.preview #toolbar > :not(#view-toggle) { display: none; }

.item-del {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 22px;
  height: 22px;
  line-height: 1;
  font-size: 15px;
  border: 1px solid var(--faint);
  border-radius: 50%;
  background: #fff;
  color: var(--danger);
  cursor: pointer;
  padding: 0;
  z-index: 2;
}
.item-resize {
  position: absolute;
  right: -6px;
  bottom: -6px;
  width: 14px;
  height: 14px;
  border: 1px solid var(--muted);
  background: #fff;
  border-radius: 3px;
  cursor: nwse-resize;
  z-index: 2;
}
.item.editing { outline: 2px solid var(--accent); outline-offset: 4px; }

/* ---- Dialogs ---- */
dialog {
  border: 1px solid var(--faint);
  border-radius: 10px;
  padding: 1.25rem;
  box-shadow: 0 10px 40px #00000026;
  font-family: var(--sans);
  color: var(--ink);
}
dialog::backdrop { background: #00000040; }

.settings-form { display: flex; flex-direction: column; gap: 0.6rem; min-width: 18rem; }
.settings-form h2 { font-family: var(--serif); margin: 0 0 0.3rem; }
.settings-form label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.85rem; color: var(--muted); }
.settings-form input[type=text], .settings-form select {
  font-family: var(--sans);
  font-size: 0.95rem;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--faint);
  border-radius: 6px;
}
.settings-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 0.3rem; }
.check { display: flex; align-items: center; gap: 0.45rem; font-size: 0.85rem; color: var(--ink); cursor: pointer; }
.check input { margin: 0; }
.opt-group { display: flex; flex-direction: column; gap: 0.4rem; }
.opt-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.z-controls { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.z-controls button { font-size: 0.8rem; }
.favicon-form { border-top: 1px solid var(--faint); margin-top: 0.8rem; padding-top: 0.9rem; }
dialog button {
  font-family: var(--sans);
  font-size: 0.85rem;
  padding: 0.4rem 0.8rem;
  border: 1px solid var(--faint);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
}
dialog button[type=submit] { background: var(--ink); color: #fff; border-color: var(--ink); }

/* ---- Image cropper ---- */
.crop-form { gap: 0.7rem; }
#crop-apply { background: var(--ink); color: #fff; border-color: var(--ink); }
/* The stage is sized in JS to the fitted original; the crop box overlays it. */
.crop-stage { position: relative; align-self: center; line-height: 0; touch-action: none; user-select: none; overflow: hidden; }
#crop-img { display: block; width: 100%; height: 100%; -webkit-user-drag: none; }
.crop-box {
  position: absolute;
  box-sizing: border-box;
  border: 1px solid #fff;
  outline: 1px solid rgba(0, 0, 0, 0.6);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.45); /* dim everything outside the box */
  cursor: move;
}
.crop-h {
  position: absolute;
  width: 14px;
  height: 14px;
  background: #fff;
  border: 1px solid var(--ink);
  border-radius: 50%;
}
.crop-h[data-c=tl] { left: -7px; top: -7px; cursor: nwse-resize; }
.crop-h[data-c=tr] { right: -7px; top: -7px; cursor: nesw-resize; }
.crop-h[data-c=bl] { left: -7px; bottom: -7px; cursor: nesw-resize; }
.crop-h[data-c=br] { right: -7px; bottom: -7px; cursor: nwse-resize; }
.crop-actions { display: flex; align-items: center; gap: 0.5rem; }

/* ---- Inline delete confirm ---- */
/* Clicking an item's × swaps it for a small ✓ / ✗ pair in the same corner. */
.item-del-confirm {
  position: absolute;
  top: -12px;
  right: -12px;
  display: flex;
  gap: 4px;
  z-index: 3;
}
.item-del-confirm button {
  width: 22px;
  height: 22px;
  line-height: 1;
  font-size: 14px;
  padding: 0;
  border: 1px solid var(--faint);
  border-radius: 50%;
  background: #fff;
  cursor: pointer;
  box-shadow: 0 1px 2px #0000001a;
}
.item-del-confirm .yes { color: #1a7f37; }
.item-del-confirm .no { color: var(--muted); }

#editor { width: min(40rem, 92vw); }
#editor-form { display: flex; flex-direction: column; gap: 0.6rem; }
#editor-text {
  width: 100%;
  min-height: 14rem;
  resize: vertical;
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 0.9rem;
  line-height: 1.5;
  padding: 0.7rem;
  border: 1px solid var(--faint);
  border-radius: 6px;
}
.settings-actions .spacer { flex: 1; }
dialog button.link-danger { border-color: transparent; background: none; color: var(--danger); padding-left: 0; }
dialog button.link-danger:hover { text-decoration: underline; }
.editor-actions { display: flex; align-items: center; gap: 0.6rem; }
.editor-tip { flex: 1; font-size: 0.75rem; color: var(--muted); }
