:root {
  --ink: #161925;
  --slate: #20253a;
  --raised: #2b3149;
  --line: #3b4260;
  --parch: #ebe2cd;
  --dim: #9a9484;
  --brass: #d2a94f;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", "URW Palladio L", P052, Georgia, serif;
  --sans: system-ui, "Segoe UI", "Noto Sans", sans-serif;
}

* { box-sizing: border-box; }
* { scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--ink);
  color: var(--parch);
  font: 14px/1.45 var(--sans);
  overflow: hidden;
  overscroll-behavior: none;
}

button, input, summary { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; }

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 328px;
  height: 100dvh;
}

.stage {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
}

.world {
  display: grid;
  place-items: center;
  min-height: 0;
  min-width: 0;
  padding: 12px 12px 0;
  overflow: hidden;
}

.frame.grab { cursor: grab; }
.frame {
  position: relative;
  overflow: hidden;
  border-radius: 3px;
  box-shadow: 0 0 0 1px var(--line), 0 18px 40px #0a0c14;
  touch-action: none;
  cursor: crosshair;
  user-select: none;
  -webkit-user-select: none;
}

#stage {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  transform-origin: 0 0;
  will-change: transform;
}

#stage > canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#sprites { pointer-events: none; }

#glow {
  pointer-events: none;
  mix-blend-mode: screen;
  opacity: 0.9;
}

#dark {
  position: absolute;
  inset: 0;
  background: #070a26;
  mix-blend-mode: multiply;
  opacity: 0;
  pointer-events: none;
}

#fog {
  position: absolute;
  inset: 0;
  background: #d4dae2;
  opacity: 0;
  pointer-events: none;
}

#census {
  position: absolute;
  left: 8px;
  top: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  max-width: 45%;
  pointer-events: none;
}
#census .wx { padding: 2px 8px; }
#census span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 7px 2px 3px;
  background: #161925c8;
  border-radius: 2px;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
}
#census canvas {
  width: 18px;
  height: 18px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#mini {
  position: absolute;
  left: 50%;
  bottom: 6px;
  transform: translateX(-50%);
  width: min(70%, 520px);
  height: 34px;
  background: #0d0f18d8;
  box-shadow: 0 0 0 1px var(--line);
  border-radius: 2px;
  cursor: ew-resize;
  z-index: 3;
  opacity: 0.85;
}
#mini:hover { opacity: 1; }
#miniMap { display: block; width: 100%; height: 100%; image-rendering: pixelated; image-rendering: crisp-edges; }
#miniBox { position: absolute; top: 0; bottom: 0; border: 1px solid var(--brass); background: #d2a94f22; pointer-events: none; }

#watch {
  position: absolute;
  right: 8px;
  top: 8px;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 8px;
  align-items: center;
  max-width: min(46%, 300px);
  padding: 6px 8px;
  background: #161925e6;
  border: 1px solid var(--line);
  border-radius: 2px;
  font-size: 12px;
  line-height: 1.35;
  z-index: 3;
}
#watchPic { width: 56px; height: 36px; image-rendering: pixelated; image-rendering: crisp-edges; }
#watchText b { display: block; font: 600 15px/1.2 var(--serif); }
#watch button { grid-column: 1 / -1; padding: 2px 8px; background: var(--raised); border: 1px solid var(--line); border-radius: 3px; cursor: pointer; }
#watch button:hover { border-color: var(--dim); }

.sizes { display: flex; gap: 4px; align-items: center; padding-bottom: 4px; border-bottom: 1px solid var(--line); margin-bottom: 2px; }
.sizes span { color: var(--dim); padding-right: 4px; }
.sizes button { min-width: 2.6em; text-align: center; }
#placeList { display: grid; gap: 4px; }
#linkOut { width: 220px; padding: 5px 9px; background: var(--ink); border: 1px solid var(--line); border-radius: 3px; }

#watch[hidden], #mini[hidden], #linkOut[hidden] { display: none; }

#tip {
  position: absolute;
  margin: 0;
  padding: 2px 8px;
  max-width: 260px;
  background: #161925e6;
  border: 1px solid var(--line);
  border-radius: 2px;
  font-size: 12px;
  line-height: 1.35;
  pointer-events: none;
  z-index: 2;
}

#ring {
  position: absolute;
  border: 1px solid #ebe2cdb0;
  border-radius: 50%;
  pointer-events: none;
  box-shadow: 0 0 0 1px #16192580;
}

#news {
  position: absolute;
  left: 50%;
  top: 10px;
  transform: translateX(-50%);
  margin: 0;
  padding: 4px 12px;
  max-width: 90%;
  font: italic 15px/1.3 var(--serif);
  text-align: center;
  background: #161925d0;
  border-radius: 2px;
  pointer-events: none;
}
#news:empty { display: none; }

.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 8px;
  padding: 10px 12px;
}

.bar button, .menu summary {
  padding: 5px 11px;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 3px;
  cursor: pointer;
  list-style: none;
  white-space: nowrap;
}
.menu summary::-webkit-details-marker { display: none; }
.bar button:hover, .menu summary:hover { border-color: var(--dim); }
.bar button[aria-pressed="true"] { border-color: var(--brass); color: var(--brass); }

.zoom { display: inline-flex; gap: 2px; }
.zoom button { min-width: 2.1em; padding-left: 7px; padding-right: 7px; text-align: center; }
#zoomReset { min-width: 3.2em; font-variant-numeric: tabular-nums; }
.frame.pan { cursor: grabbing; }

.brush, .check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
.brush input { width: 96px; accent-color: var(--brass); }
.brush output { min-width: 2ch; font-variant-numeric: tabular-nums; }
.check input { accent-color: var(--brass); margin: 0; }

.menu { position: relative; }
.sheet {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 0;
  z-index: 5;
  display: grid;
  gap: 4px;
  width: max-content;
  max-width: calc(100vw - 24px);
  padding: 8px;
  background: var(--slate);
  border: 1px solid var(--line);
  border-radius: 3px;
  box-shadow: 0 12px 30px #0a0c14;
  max-height: 70dvh;
  overflow-y: auto;
}
.sheet button { text-align: left; }
.saverow { display: flex; gap: 4px; }
.saverow input {
  flex: 1;
  min-width: 0;
  width: 150px;
  padding: 5px 9px;
  background: var(--ink);
  border: 1px solid var(--line);
  border-radius: 3px;
}
.saverow input::placeholder { color: var(--dim); }
#saveList { display: grid; gap: 4px; }
#saveList:empty::before { content: "No saved worlds yet."; color: var(--dim); padding: 4px; }
#saveList div { display: flex; gap: 4px; align-items: center; }
#saveList .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 4px; }
#saveList .when { color: var(--dim); font-size: 12px; white-space: nowrap; }
.sheet .check { padding: 5px 4px 2px; }

#readout {
  margin: 0 0 0 auto;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

#find {
  display: block;
  flex: none;
  width: calc(100% - 36px);
  margin: 0 18px 8px;
  padding: 5px 9px;
  background: var(--ink);
  color: var(--parch);
  border: 1px solid var(--line);
  border-radius: 3px;
  -webkit-appearance: none;
  appearance: none;
}
#find::placeholder { color: var(--dim); opacity: 1; }
#find::-webkit-search-decoration { -webkit-appearance: none; }

.box {
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100dvh;
  overflow: hidden;
  background: var(--slate);
  border-left: 1px solid var(--line);
}
.box > header, .cats { flex: none; }

.box header { padding: 16px 18px 10px; }
.box h1 {
  margin: 0;
  font: 600 30px/1 var(--serif);
  letter-spacing: 0.01em;
}
.box header p { margin: 6px 0 0; color: var(--dim); }

.cats {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 14px;
  padding: 4px 18px 10px;
}
.cats button {
  padding: 2px 0;
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  font: 16px/1.3 var(--serif);
  color: var(--dim);
  cursor: pointer;
}
.cats button:hover { color: var(--parch); }
.cats button[aria-selected="true"] { color: var(--parch); border-bottom-color: var(--brass); }
.cats.off button[aria-selected="true"] { border-bottom-color: transparent; color: var(--dim); }

.swatches {
  flex: 1 1 0;
  min-height: 120px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(62px, 1fr));
  gap: 10px 8px;
  align-content: start;
  padding: 6px 18px 14px;
}
.swatches button {
  display: grid;
  gap: 4px;
  padding: 0;
  background: none;
  border: 0;
  cursor: pointer;
  color: var(--dim);
  font-size: 12px;
  line-height: 1.2;
  text-align: left;
}
.swatches canvas {
  display: block;
  width: 100%;
  height: 40px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  border-radius: 2px;
  box-shadow: inset 0 0 0 1px #0a0c1480, 0 0 0 1px var(--line);
}
.swatches button:hover { color: var(--parch); }
.swatches button[aria-pressed="true"] { color: var(--parch); }
.swatches button[aria-pressed="true"] canvas { box-shadow: 0 0 0 2px var(--slate), 0 0 0 4px var(--brass); }

.codex {
  flex: none;
  display: block;
  border-top: 1px solid var(--line);
  background: var(--slate);
  font: 15px/1.5 var(--serif);
}
.codex summary {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 10px 18px;
  cursor: pointer;
  list-style: none;
  font: 600 19px/1.2 var(--serif);
}
.codex summary::-webkit-details-marker { display: none; }
.codex summary::after { content: "show notes"; font: 12px/1 var(--sans); color: var(--dim); white-space: nowrap; }
.codex[open] summary::after { content: "hide notes"; }
.codex summary:hover::after { color: var(--parch); }
#codex {
  max-height: 34dvh;
  overflow-y: auto;
  padding: 0 18px 16px;
}
.codex p { margin: 0 0 8px; max-width: 34em; }
.codex ul { margin: 0 0 10px; padding: 0; list-style: none; }
.codex .facts li { color: var(--dim); }
.codex .mix li { padding: 3px 0; border-top: 1px solid #3b426080; }
.codex .mix button {
  padding: 0;
  background: none;
  border: 0;
  font: inherit;
  color: var(--brass);
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: #d2a94f60;
  text-underline-offset: 3px;
}
.codex .gone { color: var(--dim); font-style: italic; }

@media (max-width: 760px) {
  html, body { font-size: 13px; }
  .app { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); }
  .stage { grid-template-rows: 44dvh auto; }
  .world { padding: 6px 6px 0; }
  .bar { padding: 8px 6px; gap: 6px; }
  .bar button, .menu summary { padding: 6px 9px; }
  .brush input { width: 72px; }
  #readout { flex-basis: 100%; margin: 0; text-align: left; min-height: 1.45em; }
  .box { border-left: 0; border-top: 1px solid var(--line); display: block; height: auto; overflow-y: auto; }
  .swatches { overflow: visible; min-height: 0; }
  .codex summary { padding: 10px 12px; }
  #codex { max-height: none; padding: 0 12px 14px; }
  .box header { padding: 10px 12px 6px; }
  .box h1 { font-size: 22px; }
  .box header p { display: none; }
  .cats { padding: 2px 12px 8px; }
  #find { margin: 0 12px 6px; width: calc(100% - 24px); }
  #census { left: 4px; top: 4px; max-width: 96%; }
  #watch { top: auto; bottom: 46px; right: 4px; }
  #mini { height: 26px; width: 80%; }
  #news { top: auto; bottom: 8px; font-size: 13px; }
  .swatches { padding: 4px 12px 12px; grid-template-columns: repeat(auto-fill, minmax(56px, 1fr)); }

}
