:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --ink: #111111;
  --ink-dim: #8a8a8a;
  --line: #e8e8e8;
  --accent: #0a49ff;
  --col: 300px;            /* masonry column width */
  --gap: 14px;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; min-height: 100%;
  background: var(--bg); color: var(--ink);
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
button, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }
body.mapmode { overflow: hidden; height: 100%; }

/* ========== paper card (shared) ========== */
.tag {
  display: inline-block; font-size: 10.5px; letter-spacing: .02em; padding: 1px 7px;
  border-radius: 999px; margin-bottom: 7px; color: var(--c, var(--ink-dim));
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--c, var(--line)) 45%, transparent);
}
.ttl { margin: 0 0 5px; font-size: 13.5px; font-weight: 600; line-height: 1.3; }
.meta { color: var(--ink-dim); font-size: 12px; }
.snip { margin: 9px 0 0; color: #444; font-size: 12.5px; line-height: 1.5; }
.pg { width: 100%; height: auto; display: block; background: #f1f1f3; opacity: 0; transition: opacity .35s ease; }
.pg.loaded { opacity: 1; }
.pg.noimg { display: none; }

/* ========== GRID (masonry river) ========== */
.stage { padding: var(--gap); padding-bottom: 64px; }
.river { column-width: var(--col); column-gap: var(--gap); max-width: 1500px; margin: 0 auto; }
.river .cell {
  break-inside: avoid; margin: 0 0 var(--gap); width: 100%;
  position: relative; cursor: pointer; border-radius: 6px; overflow: hidden;
  background: var(--panel); box-shadow: inset 0 0 0 1px var(--line);
  border-top: 3px solid var(--edge, var(--line));
  transition: box-shadow .15s ease;
}
.river .cell .body { padding: 11px 13px; }
.river .cell .pg { border-bottom: 1px solid var(--line); }
.river .cell:hover { box-shadow: inset 0 0 0 1px var(--edge, var(--accent)), 0 4px 16px rgba(0,0,0,.08); }
.river .cell.current {
  box-shadow: inset 0 0 0 2px var(--accent); cursor: default;
  column-span: all; display: grid; grid-template-columns: 220px 1fr; gap: 4px; background: #fbfcff;
}
.river .cell.current .pg { border-bottom: 0; border-right: 1px solid var(--line); }
.river .cell.current .ttl { font-size: 17px; }
.river .cell.current .body { padding: 16px 18px; }
@media (max-width: 560px) { .river .cell.current { grid-template-columns: 1fr; } }

/* ========== MAP (pan/zoom) ========== */
.viewport { position: fixed; inset: 0; overflow: hidden; touch-action: none; cursor: grab; background: var(--bg); }
.viewport.grabbing { cursor: grabbing; }
.space { position: absolute; top: 0; left: 0; transform-origin: 0 0; will-change: transform; }
/* image tiles: the first page, ringed in its field colour */
.space .tile { position: absolute; transform: translate(-50%, -50%); cursor: pointer; }
.space .tile > img {
  display: block; width: 58px; height: auto; background: #f1f1f3; opacity: 0;
  transition: opacity .35s ease; box-shadow: 0 0 0 1.5px var(--edge, rgba(0,0,0,.2));
}
.space .tile > img.loaded { opacity: 1; }
.space .tile.noimg > img { display: none; }
.space .tile.saved > img { box-shadow: 0 0 0 2.5px var(--ink); }
.space .tile:hover { z-index: 6; }
.space .tile:hover > img { box-shadow: 0 2px 12px rgba(0,0,0,.3), 0 0 0 2px var(--edge, var(--accent)); }
/* dot fallback when there are no images */
.space .tile.dotonly { width: 9px; height: 9px; margin: -4.5px 0 0 -4.5px; border-radius: 50%; background: var(--edge, #888); box-shadow: 0 0 0 1px rgba(255,255,255,.7); }
.space .tile.dotonly > img { display: none; }
.space .tile .lbl {
  position: absolute; left: 64px; top: 0; white-space: nowrap; display: none;
  background: rgba(255,255,255,.97); color: var(--ink); font-size: 11px; padding: 3px 8px;
  border-radius: 4px; box-shadow: 0 1px 6px rgba(0,0,0,.22); max-width: 320px; overflow: hidden; text-overflow: ellipsis;
}
.space .tile.dotonly .lbl { left: 12px; top: -4px; }
.space .tile:hover .lbl { display: block; }

/* focus card (map mode) */
.focus {
  position: fixed; left: 12px; top: 12px; z-index: 16; width: 340px; max-width: calc(100vw - 24px);
  background: var(--panel); border-radius: 8px; overflow: hidden;
  box-shadow: 0 6px 26px rgba(0,0,0,.16), inset 0 0 0 1px var(--line);
  border-top: 3px solid var(--edge, var(--line));
  display: grid; grid-template-columns: 130px 1fr;
}
.focus .pg { border-right: 1px solid var(--line); align-self: start; }
.focus .body { padding: 14px 16px; }
.focus .acts.on { grid-column: 1 / -1; display: flex; gap: 6px; padding: 0 16px 14px; }

/* hover actions (river cards) */
.acts { position: absolute; top: 9px; right: 9px; display: none; gap: 4px; }
.cell:hover .acts { display: flex; }
.acts button {
  width: 23px; height: 23px; padding: 0; line-height: 1; border: 0; border-radius: 5px; cursor: pointer;
  background: #f2f2f4; box-shadow: inset 0 0 0 1px var(--line); font-size: 12px; color: var(--ink-dim);
}
.acts button:hover { background: #fff; color: var(--ink); }
.acts .heart.on { background: var(--accent); color: #fff; box-shadow: none; }

/* ========== bottom bar ========== */
.bar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 9px 14px; background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(10px); border-top: 1px solid var(--line);
}
.bar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.mix { display: inline-flex; align-items: center; gap: 7px; font-size: 10.5px; color: var(--ink-dim); border: 1px solid var(--line); border-radius: 999px; padding: 3px 11px; white-space: nowrap; }
.mix input[type="range"] { -webkit-appearance: none; appearance: none; width: 84px; height: 3px; border-radius: 3px; background: linear-gradient(90deg, var(--accent), #b45309); outline: none; cursor: pointer; }
.mix input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 13px; height: 13px; border-radius: 50%; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.35), inset 0 0 0 1px var(--line); cursor: pointer; }
.mix input[type="range"]::-moz-range-thumb { width: 13px; height: 13px; border: 0; border-radius: 50%; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.35); cursor: pointer; }
.q { border: 1px solid var(--line); border-radius: 999px; padding: 6px 13px; width: 230px; background: var(--bg); outline: none; }
.q:focus { border-color: var(--accent); }
.bar button, .toggle {
  background: none; border: 1px solid var(--line); color: var(--ink-dim);
  padding: 6px 13px; border-radius: 999px; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.bar button:hover, .toggle:hover { color: var(--ink); border-color: var(--accent); }
.bar button[aria-pressed="true"] { color: var(--bg); background: var(--accent); border-color: var(--accent); }

/* ========== trail / minimap / legend / shelf ========== */
.trail { flex: 1 1 auto; min-width: 0; display: flex; gap: 5px; overflow-x: auto; scrollbar-width: none; }
.trail::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; display: inline-flex; align-items: center; gap: 5px; max-width: 150px;
  padding: 3px 9px 3px 6px; border-radius: 999px; border: 1px solid var(--line); background: var(--bg);
  cursor: pointer; font-size: 11px; color: var(--ink-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chip:hover { color: var(--ink); border-color: var(--accent); }
.chip i { width: 7px; height: 7px; border-radius: 50%; flex: 0 0 auto; }

.minimap { position: fixed; right: 10px; bottom: 56px; z-index: 15; width: 180px; height: 180px; border: 1px solid var(--line); border-radius: 6px; background: rgba(255,255,255,.9); box-shadow: 0 2px 10px rgba(0,0,0,.12); cursor: crosshair; }

.legend { position: fixed; right: 10px; bottom: 244px; z-index: 15; display: flex; flex-direction: column; gap: 2px; align-items: flex-end; }
.leg { display: inline-flex; align-items: center; gap: 6px; border: 0; background: none; cursor: pointer; font-size: 11px; color: var(--ink); padding: 2px 4px; border-radius: 4px; }
.leg:hover { background: rgba(0,0,0,.05); }
.leg.off { opacity: .32; }
.leg i { width: 9px; height: 9px; border-radius: 50%; }

.shelf { position: fixed; left: 10px; bottom: 56px; z-index: 25; width: 320px; max-height: 62vh; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 30px rgba(0,0,0,.18); display: flex; flex-direction: column; overflow: hidden; }
.shelf-head { display: flex; justify-content: space-between; align-items: center; padding: 9px 13px; border-bottom: 1px solid var(--line); font-weight: 600; }
.shelf-head button { border: 0; background: none; font-size: 17px; cursor: pointer; color: var(--ink-dim); }
.shelf-items { display: flex; flex-direction: column; gap: 6px; padding: 10px; overflow-y: auto; }
.shelf-row { border-left: 3px solid var(--line); padding: 5px 10px; cursor: pointer; border-radius: 3px; }
.shelf-row:hover { background: #f7f7f7; }
.shelf-row .ttl { display: block; font-size: 12.5px; font-weight: 600; line-height: 1.3; }
.shelf-row .meta { font-size: 11px; }
.shelf-empty { color: var(--ink-dim); padding: 16px; }

/* ========== loading / about ========== */
.loading { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--ink-dim); background: var(--bg); z-index: 40; }
dialog#about { max-width: 470px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); color: var(--ink); padding: 26px; line-height: 1.55; }
dialog#about::backdrop { background: rgba(0,0,0,.45); }
dialog#about h2 { margin-top: 0; font-weight: 700; }
dialog#about a { color: var(--accent); }
dialog#about button { margin-top: 8px; background: none; border: 1px solid var(--line); color: var(--ink); padding: 6px 16px; border-radius: 999px; cursor: pointer; }

@media (max-width: 640px) {
  :root { --col: 100%; --gap: 10px; }
  .q { width: 140px; }
  .minimap { width: 120px; height: 120px; }
  .legend { bottom: 186px; }
  .focus { width: 260px; }
}
