:root {
  --ink: #0c0a10;
  --ink-2: #15121b;
  --ink-3: #201b29;
  --line: #2e2739;
  --line-2: #463c56;
  --text: #f1ecf6;
  --dim: #9b90a9;
  --faint: #6a6078;
  --accent: #d4ff3a;
  --accent-ink: #11140a;
  --danger: #ff6b57;
  --shadow: 4px 4px 0 #000;
  --pixel: "Pixelify Sans", ui-monospace, monospace;
  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --gutter: 20px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }

body {
  margin: 0;
  overflow: hidden;
  background: var(--ink);
  color: var(--text);
  font: 13px/1.4 var(--mono);
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
kbd {
  font: 500 10px/1 var(--mono);
  color: var(--faint);
  border: 1px solid var(--line-2);
  padding: 2px 4px;
}

/* Map viewport */

.viewport {
  position: fixed;
  inset: 0;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  background:
    radial-gradient(ellipse at 50% 45%, transparent 40%, rgba(0, 0, 0, 0.55) 100%),
    radial-gradient(circle, var(--line) 1px, transparent 1.5px) 0 0 / 24px 24px,
    var(--ink);
}
.viewport.dragging { cursor: grabbing; }

.world {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  will-change: transform;
  box-shadow: 0 0 0 1px var(--line-2), 0 30px 80px -20px rgba(0, 0, 0, 0.9);
}
.world.smooth { transition: transform 0.25s cubic-bezier(0.2, 0.8, 0.2, 1); }

#canvas { display: block; image-rendering: pixelated; }
#ascii {
  margin: 0;
  padding: 0;
  background: var(--ink-2);
  font: 16px/16px var(--mono);
  letter-spacing: 6.4px; /* ~one 16px cell per glyph */
  color: var(--dim);
  white-space: pre;
}

/* Floating chrome */

.topbar, .panel, .hud, .credits { position: fixed; z-index: 2; }

.topbar {
  top: var(--gutter);
  left: var(--gutter);
  right: var(--gutter);
  display: flex;
  align-items: center;
  gap: 14px;
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 4px;
  color: var(--text);
  text-decoration: none;
  font: 700 26px/1 var(--pixel);
  letter-spacing: 0.01em;
  text-shadow: 3px 3px 0 #000;
}
.brand .tile { width: 32px; height: 32px; }
.tile { image-rendering: pixelated; display: block; }

.styles, .seedbar, .seg, .panel {
  background: var(--ink-2);
  border: 1px solid var(--line-2);
  box-shadow: var(--shadow);
}

.styles { display: flex; padding: 3px; gap: 3px; }
.styles button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px 7px 9px;
  border: 0;
  background: transparent;
  color: var(--dim);
  font: 500 15px/1 var(--pixel);
}
.styles button .tile { width: 16px; height: 16px; }
.styles button:hover { color: var(--text); background: var(--ink-3); }
.styles button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }

.seedbar {
  display: flex;
  align-items: stretch;
  margin-left: auto;
  min-width: 0;
}
.seedbar label {
  display: grid;
  place-items: center;
  padding: 0 10px 0 14px;
  color: var(--faint);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
}
#seed {
  width: 220px;
  min-width: 0;
  padding: 10px 6px;
  border: 0;
  background: transparent;
  font-size: 15px;
  font-weight: 500;
  outline: none;
}
.seedbar:focus-within { border-color: var(--accent); }
.icon-btn, .daily {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 0;
  border-left: 1px solid var(--line-2);
  background: transparent;
  padding: 0 12px;
  color: var(--dim);
}
.icon-btn svg { width: 16px; height: 16px; }
.daily { font: 500 15px/1 var(--pixel); }
.daily .tile { width: 16px; height: 16px; }
.icon-btn:hover, .daily:hover { color: var(--text); background: var(--ink-3); }
.daily[aria-pressed="true"] { color: var(--accent); }
#random:active svg { transform: rotate(90deg); }

/* Tune panel */

.panel {
  top: 84px;
  right: var(--gutter);
  width: 280px;
  max-height: calc(100vh - 84px - 84px);
  display: flex;
  flex-direction: column;
}
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  font: 700 17px/1 var(--pixel);
  text-align: left;
}
.panel[data-collapsed] .panel-head { border-bottom: 0; }
.panel[data-collapsed] .sliders, .panel[data-collapsed] #edit-recipe { display: none; }

.sliders { overflow-y: auto; padding: 6px 14px 10px; scrollbar-width: thin; scrollbar-color: var(--line-2) transparent; }
.sliders h3 {
  margin: 14px 0 6px;
  color: var(--accent);
  font: 500 11px/1 var(--mono);
  text-transform: uppercase;
  letter-spacing: 0.14em;
}
.sliders h3:first-child { margin-top: 8px; }

.slider { display: block; padding: 5px 0; }
.slider-row { display: flex; justify-content: space-between; margin-bottom: 6px; color: var(--dim); }
.slider output { color: var(--text); font-weight: 500; font-variant-numeric: tabular-nums; }

input[type="range"] {
  --p: 50%;
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  height: 14px;
  margin: 0;
  background: transparent;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  background: linear-gradient(to right, var(--accent) var(--p), var(--line-2) var(--p));
}
input[type="range"]::-moz-range-track { height: 4px; background: var(--line-2); }
input[type="range"]::-moz-range-progress { height: 4px; background: var(--accent); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  margin-top: -5px;
  background: var(--text);
  border: 2px solid #000;
  box-shadow: 2px 2px 0 #000;
}
input[type="range"]::-moz-range-thumb {
  width: 10px;
  height: 10px;
  border-radius: 0;
  background: var(--text);
  border: 2px solid #000;
}
input[type="range"]:hover::-webkit-slider-thumb, input[type="range"]:focus-visible::-webkit-slider-thumb { background: var(--accent); }
input[type="range"]:focus-visible { outline: none; }

.ghost {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 14px 14px;
  padding: 9px 12px;
  border: 1px dashed var(--line-2);
  background: transparent;
  color: var(--dim);
}
.ghost:hover { color: var(--text); border-color: var(--dim); }

/* Bottom HUD */

.hud {
  left: var(--gutter);
  right: var(--gutter);
  bottom: var(--gutter);
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}
.hud > * { pointer-events: auto; }
.stats {
  margin: 0;
  color: var(--dim);
  font-size: 12px;
  text-shadow: 1px 1px 0 #000;
  font-variant-numeric: tabular-nums;
}
.stats b { color: var(--text); font-weight: 500; }
.stats .warn { color: var(--accent); }
.hud-controls { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }

.seg { display: flex; }
.seg button {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 36px;
  justify-content: center;
  padding: 8px 11px;
  border: 0;
  border-left: 1px solid var(--line);
  background: transparent;
  color: var(--dim);
  font-weight: 500;
}
.seg button:first-child { border-left: 0; }
.seg button:hover { color: var(--text); background: var(--ink-3); }
.seg button[aria-pressed="true"] { color: var(--accent-ink); background: var(--accent); }
.seg svg { width: 14px; height: 14px; }

.credits {
  bottom: 70px;
  left: var(--gutter);
  display: flex;
  gap: 12px;
  color: var(--faint);
  font-size: 11px;
}
.credits a { color: inherit; }
.credits a:hover { color: var(--text); }

/* Toasts, errors, dialog */

.toast, .error {
  position: fixed;
  z-index: 5;
  left: 50%;
  transform: translateX(-50%);
  max-width: min(560px, calc(100vw - 32px));
  padding: 10px 14px;
  border: 1px solid #000;
  box-shadow: var(--shadow);
}
.toast {
  bottom: 84px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
  animation: pop 0.18s ease-out;
}
.error {
  top: 84px;
  background: #2a1113;
  border-color: var(--danger);
  color: #ffd3cc;
  white-space: pre-wrap;
  font-size: 12px;
}
.error::before { content: "recipe error  "; color: var(--danger); font-weight: 700; }
@keyframes pop { from { transform: translate(-50%, 8px); opacity: 0; } }

dialog {
  width: min(720px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  padding: 0;
  border: 1px solid var(--line-2);
  background: var(--ink-2);
  color: var(--text);
  box-shadow: 8px 8px 0 #000;
}
dialog::backdrop { background: rgba(6, 5, 9, 0.7); backdrop-filter: blur(2px); }
dialog form { display: flex; flex-direction: column; max-height: inherit; }
dialog header { padding: 16px 18px 10px; }
dialog h2 { margin: 0; font: 700 22px/1 var(--pixel); }
dialog header p { margin: 6px 0 0; color: var(--dim); }
dialog textarea {
  flex: 1;
  min-height: 360px;
  margin: 0 18px;
  padding: 12px;
  border: 1px solid var(--line-2);
  background: var(--ink);
  font: 13px/1.55 var(--mono);
  resize: vertical;
  tab-size: 2;
  outline: none;
}
dialog textarea:focus { border-color: var(--accent); }
dialog footer { display: flex; justify-content: space-between; padding: 14px 18px 18px; }
dialog .ghost { margin: 0; }
.primary {
  padding: 9px 18px;
  border: 1px solid #000;
  background: var(--accent);
  color: var(--accent-ink);
  font: 700 15px/1 var(--pixel);
  box-shadow: 3px 3px 0 #000;
}
.primary:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 #000; }

/* Small screens */

@media (max-width: 900px) {
  :root { --gutter: 12px; }
  .topbar { flex-wrap: wrap; gap: 10px; }
  .brand { font-size: 22px; }
  .brand .tile { width: 24px; height: 24px; }
  .styles { order: 3; flex: 1 1 100%; }
  .styles button { flex: 1; justify-content: center; }
  .seedbar { flex: 1 1 auto; margin-left: 0; }
  #seed { width: 100%; }
  .seedbar label { display: none; }
  .daily span { display: none; }

  .panel {
    top: auto;
    bottom: 64px;
    left: var(--gutter);
    right: var(--gutter);
    width: auto;
    max-height: 46vh;
  }
  .hud { align-items: center; }
  .stats { display: none; }
  .hud-controls { flex-wrap: nowrap; width: 100%; justify-content: space-between; }
  .seg button { padding: 8px 9px; }
  .seg #share span { display: none; }
  .credits { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .world.smooth { transition: none; }
  .toast { animation: none; }
}

/* Link to the game */
.play-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 14px 9px 11px;
  border: 1px solid #000;
  background: var(--accent);
  color: var(--accent-ink);
  box-shadow: var(--shadow);
  text-decoration: none;
  font: 700 15px/1 var(--pixel);
}
.play-link .tile { width: 16px; height: 16px; }
.play-link:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 #000; }
@media (max-width: 900px) {
  .play-link { order: 2; margin-left: auto; padding: 7px 10px; }
  .play-link span { display: none; }
}
