* {
  box-sizing: border-box;
}

:root {
  --ui-font: 'Pixelify Sans', ui-monospace, monospace;
  --panel-bg: rgba(24, 15, 8, 0.86);
  --panel-text: #f6ead2;
  --gold: #f2be28;
  --gold-dark: #b8860b;
  --ink-dark: #2b2118;
}

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  background: #2b1d12;
  font-family: var(--ui-font);
  overflow: hidden;
}

#world {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}

#world:active {
  cursor: grabbing;
}

#hud {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  pointer-events: none;
  color: var(--panel-text);
}

.hud-left {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
}

.hud-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* Live viewer count with a blinking pixel "on air" dot. */
#viewers {
  display: flex;
  align-items: center;
  gap: 7px;
}

#viewers[hidden] {
  display: none;
}

#viewers::before {
  content: '';
  width: 8px;
  height: 8px;
  background: #e5402f;
  box-shadow: 0 0 0 2px #5a1510;
  animation: live-blink 1.2s steps(1) infinite;
}

@keyframes live-blink {
  50% { background: #7a2018; }
}

@media (prefers-reduced-motion: reduce) {
  #viewers::before {
    animation: none;
  }
}

/* How many days the colony has been going (day 1 = the day it was founded). */
#day {
  pointer-events: auto;
  cursor: help;
}

#day[hidden] {
  display: none;
}

#viewers,
#day,
#food,
#ticker {
  background: var(--panel-bg);
  padding: 7px 12px;
  border: 2px solid rgba(0, 0, 0, 0.5);
  font-size: 14px;
}

/* Food meter: pixel segments lit by level, colored hungry → full. */
#food {
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: auto;
  cursor: help;
}

#food:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.food-label {
  font-weight: 600;
}

.food-bar {
  display: flex;
  gap: 2px;
  padding: 2px;
  background: #120c07;
  box-shadow: 0 0 0 2px #3b2a1c;
}

.food-bar i {
  width: 8px;
  height: 12px;
  background: #3b2a1c;
}

.food-bar i.on.starving { background: #d8402c; }
.food-bar i.on.hungry { background: #e8922c; }
.food-bar i.on.fed { background: #d6c43a; }
.food-bar i.on.full { background: #6cc24a; }

.food-status {
  min-width: 5.5em;
}

.food-trend {
  width: 1em;
  text-align: center;
  color: #d9cbb0;
}

#ticker {
  max-width: 480px;
  text-align: right;
  pointer-events: auto;
}

#ticker[hidden] {
  display: none;
}

/* Title doubles as the fold button, with a pixel triangle like the leaderboard's. */
.ticker-toggle {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  width: 100%;
  padding: 0;
  background: none;
  border: 0;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--gold);
  text-transform: uppercase; /* matches the leaderboard's headings */
  letter-spacing: 0.04em;
  cursor: pointer;
}

.ticker-toggle::before {
  content: '';
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 6px solid currentColor;
}

.ticker-toggle[aria-expanded="false"]::before {
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  border-left: 6px solid currentColor;
  border-right: 0;
}

.ticker-toggle:hover {
  color: #ffe27a;
}

.ticker-toggle:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.ticker-body {
  list-style: none;
  margin: 6px 0 0;
  padding: 0;
}

.ticker-body li + li {
  margin-top: 4px;
}

/* Chunky 8-bit button: hard 2px border and a drop "shadow" that presses in. */
.pixel-btn {
  pointer-events: auto;
  font: inherit;
  font-family: var(--ui-font);
  font-weight: 600;
  font-size: 16px;
  color: var(--ink-dark);
  background: var(--gold);
  border: 2px solid var(--ink-dark);
  box-shadow: 0 4px 0 var(--ink-dark);
  padding: 8px 14px;
  cursor: pointer;
}

.pixel-btn:hover {
  background: #ffd24a;
}

.pixel-btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 var(--ink-dark);
}

.pixel-btn:disabled {
  background: #a89c86;
  cursor: not-allowed;
  transform: none;
  box-shadow: 0 4px 0 var(--ink-dark);
}

.pixel-btn:focus-visible,
.lb-row:focus-visible,
.toast button:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.pixel-btn.small {
  font-size: 13px;
  padding: 4px 10px;
  box-shadow: 0 3px 0 var(--ink-dark);
}

#buyButton {
  position: absolute;
  right: 16px;
  bottom: 18px;
  font-size: 18px;
  padding: 10px 18px;
}

/* --- Leaderboard --- */
#leaderboard {
  position: absolute;
  top: 96px;
  left: 12px;
  width: min(300px, calc(100vw - 24px));
  max-height: calc(100vh - 180px);
  overflow-y: auto;
  background: var(--panel-bg);
  border: 2px solid rgba(0, 0, 0, 0.6);
  color: var(--panel-text);
  padding: 4px 10px 10px;
}

#leaderboard.hidden {
  display: none;
}

#leaderboard h3 {
  margin: 10px 0 4px;
  font-size: 14px;
  font-weight: 600;
  color: var(--gold);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Each board folds down to its title; a pixel triangle shows which way. */
.lb-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 2px 0;
  background: none;
  border: 0;
  font: inherit;
  color: inherit;
  text-transform: inherit;
  letter-spacing: inherit;
  text-align: left;
  cursor: pointer;
}

.lb-toggle::before {
  content: '';
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 6px solid currentColor;
}

.lb-toggle[aria-expanded="false"]::before {
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  border-left: 6px solid currentColor;
  border-right: 0;
}

.lb-toggle:hover {
  color: #ffe27a;
}

.lb-close {
  position: sticky;
  top: 0;
  float: right;
  margin: 2px -4px 0 0;
  padding: 0 6px;
  background: none;
  border: 0;
  font: inherit;
  font-size: 18px;
  line-height: 1;
  color: #b9a888;
  cursor: pointer;
}

.lb-close:hover {
  color: #fff;
}

.lb-toggle:focus-visible,
.lb-close:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

#leaderboard ol {
  list-style: none;
  margin: 0;
  padding: 0;
}

.lb-row {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 3px 4px;
  background: none;
  border: 0;
  color: inherit;
  font: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}

.lb-row:hover {
  background: rgba(255, 255, 255, 0.08);
}

.lb-rank {
  width: 1em;
  color: #b9a888;
}

.lb-row img {
  width: 30px;
  height: 20px;
  image-rendering: pixelated;
}

.lb-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lb-flag {
  color: var(--gold);
  margin-left: 4px;
}

.lb-owner {
  display: block;
  font-size: 12px;
  color: #b9a888;
}

.lb-value small {
  color: #b9a888;
}

/* --- Toasts --- */
#toasts {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  width: min(460px, calc(100vw - 24px));
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 8px 10px 8px 14px;
  background: var(--panel-bg);
  color: var(--panel-text);
  border: 2px solid var(--gold);
  font-size: 15px;
  animation: toast-in 0.25s steps(4) both;
}

.toast-hatching,
.toast-hatched {
  border-color: #f0a8c0;
}

.toast span {
  flex: 1;
}

.toast button {
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  background: var(--gold);
  color: var(--ink-dark);
  border: 2px solid var(--ink-dark);
  padding: 3px 8px;
  cursor: pointer;
}

@keyframes toast-in {
  from { transform: translateY(12px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

/* --- Buy modal --- */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(10, 6, 2, 0.6);
}

.modal.hidden {
  display: none;
}

.modal-card {
  position: relative;
  width: min(560px, 100%);
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  padding: 18px 20px 16px;
  background: #f3e6c8;
  color: var(--ink-dark);
  border: 3px solid var(--ink-dark);
  box-shadow: 0 6px 0 var(--ink-dark);
}

.modal-card h2 {
  margin: 0 0 4px;
  font-size: 24px;
  font-weight: 600;
}

.modal-lede {
  margin: 0 0 12px;
  font-size: 15px;
  color: #5a4a36;
}

.modal-close {
  position: absolute;
  top: 6px;
  right: 10px;
  background: none;
  border: 0;
  font: inherit;
  font-size: 26px;
  line-height: 1;
  color: var(--ink-dark);
  cursor: pointer;
}

#buyPreview {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 3 / 1;
  image-rendering: pixelated;
  border: 2px solid var(--ink-dark);
  margin-bottom: 12px;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

@media (max-width: 480px) {
  .field-row {
    grid-template-columns: 1fr;
  }
}

#buyForm label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 15px;
  font-weight: 600;
}

#buyForm .opt {
  font-weight: 400;
  font-size: 12px;
  color: #6b5a44;
}

#buyForm input[type="text"],
#buyForm input:not([type]) {
  font: inherit;
  font-size: 16px;
  font-weight: 400;
  padding: 6px 8px;
  border: 2px solid var(--ink-dark);
  background: #fffaf0;
  color: var(--ink-dark);
}

#buyForm fieldset {
  border: 0;
  margin: 12px 0 0;
  padding: 0;
}

#buyForm legend {
  font-weight: 600;
  font-size: 15px;
  margin-bottom: 6px;
}

/* Honeycomb color picker. Cells are hexagon-clipped buttons, so the selection mark
   is drawn inside (inset rings) rather than as an outline that would be clipped. */
.hex-picker {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 18px;
}

.hex-grid {
  position: relative;
  flex: none;
}

.hex-cell {
  position: absolute;
  padding: 0;
  border: 0;
  cursor: pointer;
}

.hex-cell:hover {
  filter: brightness(1.15);
}

.hex-cell.is-selected {
  z-index: 1;
  box-shadow: inset 0 0 0 2px #fff, inset 0 0 0 4px var(--ink-dark);
}

.hex-cell:focus-visible {
  outline: none;
  box-shadow: inset 0 0 0 3px var(--gold), inset 0 0 0 5px var(--ink-dark);
}

.hex-side {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.hex-shades {
  display: flex;
  gap: 2px;
}

.hex-shades .hex-cell {
  position: static;
}

.hex-readout {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}

.hex-chip {
  width: 26px;
  height: 26px;
  border: 2px solid var(--ink-dark);
}

.persona input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.persona:has(input:focus-visible) {
  outline: 2px solid var(--ink-dark);
  outline-offset: 2px;
}

.personas {
  display: grid;
  gap: 6px;
}

.persona {
  position: relative;
  display: block !important;
  padding: 7px 10px;
  border: 2px solid #c9b48c;
  background: #fffaf0;
  font-weight: 400 !important;
  font-size: 14px !important;
  cursor: pointer;
}

.persona strong {
  text-transform: capitalize;
  margin-right: 4px;
}

.persona:has(input:checked) {
  border-color: var(--ink-dark);
  background: #ffe9a8;
}

.form-error {
  min-height: 1.2em;
  margin: 10px 0 6px;
  color: #a1281c;
  font-size: 14px;
}

#buySubmit {
  width: 100%;
  font-size: 18px;
}

.fineprint {
  margin: 8px 0 0;
  font-size: 12px;
  color: #6b5a44;
  text-align: center;
}

/* The ant's journal: an open, well-used spiral journal — red cover, two cream
   pages ruled in dark brown, metal rings down the spine, a ribbon bookmark —
   in a pixel font. Everything uses hard color stops (no blur) so it stays 8-bit,
   and the wear (yellowed edges, coffee ring, smudges, dog-ear, faded older ink)
   is layered into the page backgrounds. Text sits on a 24px rhythm to match the rules. */
#panel {
  --line: 17px;
  --cover: #b8332b;
  --cover-dark: #7d1f1a;
  --outline: #2a1410;
  --paper: #eedfba;
  --paper-dark: #d9c494;
  --rule: #6b4a2c;
  --ink: #26285a;
  --ink-soft: #5b5576;
  --stain: rgba(128, 80, 30, 0.2);
  position: absolute;
  top: 12px;
  right: 12px;
  width: min(420px, calc(100vw - 24px));
  padding: 6px;
  color: var(--ink);
  font-family: 'VT323', ui-monospace, monospace;
  font-size: 15px;
  line-height: var(--line);
  background: var(--cover);
  border: 3px solid var(--outline);
  /* pixel cover: lighter top edge, darker bottom edge, hard drop shadow */
  box-shadow:
    inset 0 3px 0 #d4524a,
    inset 0 -4px 0 var(--cover-dark),
    6px 6px 0 rgba(0, 0, 0, 0.45);
}

#panel.hidden {
  display: none;
}

/* The journal sits where the ticker is; hide the ticker while it's open. */
body.journal-open #ticker {
  visibility: hidden;
}

#panelClose {
  position: absolute;
  top: -3px;
  right: -3px;
  width: 22px;
  height: 22px;
  z-index: 3;
  padding: 0;
  background: var(--outline);
  color: #f3e3c0;
  border: 0;
  font-family: var(--ui-font);
  font-size: 16px;
  line-height: 22px;
  cursor: pointer;
}

#panelClose:hover {
  background: #4a2018;
}

.jr-spread {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 14px 1fr;
  height: min(330px, calc(100vh - 60px));
}

.jr-page {
  position: relative;
  overflow-y: auto;
  padding: var(--line) 9px var(--line) 9px;
  background-color: var(--paper);
  background-attachment: local, local, local, local, local;
  scrollbar-width: thin;
  scrollbar-color: var(--rule) transparent;
}

.jr-left {
  /* worn: notched top-left corner, smudge low on the page, shading toward the spine */
  clip-path: polygon(0 6px, 6px 6px, 6px 0, 100% 0, 100% 100%, 0 100%);
  background-image:
    linear-gradient(to left, rgba(110, 70, 30, 0.22) 0 4px, rgba(110, 70, 30, 0.1) 4px 8px, transparent 8px),
    radial-gradient(circle at 24% 88%, rgba(150, 110, 50, 0.16) 0 15px, transparent 16px),
    linear-gradient(to bottom, rgba(170, 130, 60, 0.18) 0 4px, transparent 4px),
    repeating-linear-gradient(to bottom, transparent 0 calc(var(--line) - 2px), var(--rule) calc(var(--line) - 2px) var(--line));
  box-shadow: inset 4px 0 0 rgba(160, 120, 55, 0.18), inset 0 -4px 0 rgba(160, 120, 55, 0.2);
}

.jr-right {
  background-image:
    linear-gradient(to right, rgba(110, 70, 30, 0.22) 0 4px, rgba(110, 70, 30, 0.1) 4px 8px, transparent 8px),
    radial-gradient(circle at 76% 78%, transparent 0 14px, var(--stain) 14px 17px, transparent 17px),
    radial-gradient(circle at 70% 82%, rgba(128, 80, 30, 0.08) 0 13px, transparent 14px),
    linear-gradient(to bottom, rgba(170, 130, 60, 0.18) 0 4px, transparent 4px),
    repeating-linear-gradient(to bottom, transparent 0 calc(var(--line) - 2px), var(--rule) calc(var(--line) - 2px) var(--line));
  box-shadow: inset -4px 0 0 rgba(160, 120, 55, 0.18), inset 0 -4px 0 rgba(160, 120, 55, 0.2);
}

/* Dog-eared bottom corner of the right page: the cover shows through, the folded flap on top. */
.jr-dogear {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 14px;
  height: 14px;
  background: linear-gradient(to top left, var(--cover) 0 50%, var(--paper-dark) 50% 100%);
  box-shadow: -2px -2px 0 rgba(90, 60, 25, 0.25);
  pointer-events: none;
}

/* Metal spiral rings across the gap between the pages. */
.jr-spiral {
  background:
    repeating-linear-gradient(to bottom,
      transparent 0 6px,
      #1e1e24 6px 7px,
      #d9d9e2 7px 9px,
      #9a9aa8 9px 11px,
      #1e1e24 11px 12px,
      transparent 12px 17px),
    linear-gradient(to right, var(--cover-dark) 0 3px, var(--outline) 3px 11px, var(--cover-dark) 11px);
}

/* Ribbon bookmark hanging over the top of the right page. */
.jr-ribbon {
  position: absolute;
  top: -9px;
  right: 28px;
  width: 10px;
  height: 32px;
  z-index: 2;
  background: linear-gradient(to right, #2e6f94 0 2px, #3e93c2 2px 8px, #2e6f94 8px);
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 80%, 0 100%);
  pointer-events: none;
}

.jr-page h2 {
  margin: 0;
  font-family: var(--ui-font);
  font-size: 13px;
  font-weight: 600;
  line-height: var(--line);
  color: var(--ink);
  letter-spacing: 0.02em;
}

.jr-page h3 {
  margin: var(--line) 0 0;
  font-family: var(--ui-font);
  font-size: 10px;
  font-weight: 600;
  line-height: var(--line);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
}

.jr-page h3:first-child {
  margin-top: 0;
}

.jr-page p,
.jr-page ul {
  margin: 0;
}

.jr-sub {
  color: var(--ink-soft);
}

.jr-swatch {
  display: inline-block;
  width: 9px;
  height: 9px;
  margin-right: 4px;
  vertical-align: -1px;
  box-shadow: 0 0 0 1px var(--outline);
}

/* The ant's portrait, taped in. */
.jr-photo {
  position: relative;
  display: inline-block;
  margin: 4px 0 2px 2px;
  padding: 3px 5px;
  background: #f7f0dc;
  box-shadow: 0 0 0 2px #c9b68a, 3px 3px 0 rgba(90, 60, 25, 0.25);
}

.jr-photo img {
  display: block;
  width: 46px;
  height: 30px;
  image-rendering: pixelated;
  background: #7a5a38;
}

.jr-photo::before {
  content: '';
  position: absolute;
  top: -6px;
  left: 50%;
  width: 30px;
  height: 9px;
  margin-left: -15px;
  background: rgba(244, 236, 196, 0.75);
  box-shadow: inset 0 0 0 1px rgba(150, 130, 80, 0.35);
  clip-path: polygon(0 0, 100% 0, 96% 50%, 100% 100%, 0 100%, 4% 50%);
}

.jr-facts {
  list-style: none;
  padding: 0;
}

.jr-facts li::before {
  content: '- ';
  color: var(--ink-soft);
}

.jr-thought {
  color: #3d2a62;
}

.jr-tag {
  color: #a23a2c;
  font-size: 13px;
}

/* The ant's photo with the Follow button right under it, so it's there without scrolling. */
.jr-photo-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 4px;
}

.jr-follow {
  margin-top: 0;
  padding: 0 5px;
  font-family: var(--ui-font);
  font-size: 10px;
  line-height: 16px;
  color: var(--outline);
  background: #f2be28;
  border: 2px solid var(--outline);
  box-shadow: 2px 2px 0 var(--outline);
  cursor: pointer;
}

.jr-follow[aria-pressed="true"] {
  background: #9ccf6a;
}

.jr-follow:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

.jr-date {
  color: var(--ink-soft);
  font-size: 13px;
}

/* Older entries' ink has faded. */
.jr-entry:nth-of-type(3) { opacity: 0.85; }
.jr-entry:nth-of-type(4) { opacity: 0.74; }
.jr-entry:nth-of-type(5) { opacity: 0.64; }

.jr-entry-thought p {
  color: #3d2a62;
}

.jr-empty {
  color: var(--ink-soft);
}

/* Phones: one page under the other, no spiral. */
@media (max-width: 640px) {
  .jr-spread {
    grid-template-columns: 1fr;
    height: auto;
    max-height: min(420px, calc(100vh - 60px));
    overflow-y: auto;
  }

  .jr-spiral {
    display: none;
  }

  .jr-page {
    max-height: none;
    overflow: visible;
    min-height: 0;
  }
}

@media (max-width: 640px) {
  #toasts {
    bottom: 76px;
  }

  #ticker {
    display: none;
  }
}

