@import url("https://fonts.googleapis.com/css2?family=DM+Mono:wght@400;500&family=Fredoka:wght@500;600;700&display=swap");

:root {
  color-scheme: light;
  --ink: #30261e;
  --muted: #786b5c;
  --paper: #fffaf0;
  --cream: #f8eccd;
  --yellow: #f6d365;
  --green: #8ab66b;
  --pale-green: #b9d6a5;
  --dark-green: #42624a;
  --brown: #70472f;
  --orange: #ee8959;
  --red: #b84d3d;
  --shadow: 0 6px 0 rgba(80, 55, 31, 0.18);
  --gap: 18px;
  --radius: 18px;

  /* One type scale, used everywhere. Nothing below --fs-2xs. */
  --fs-2xs: 0.7rem;
  --fs-xs: 0.78rem;
  --fs-sm: 0.86rem;
  --fs-md: 1rem;
  --fs-lg: 1.2rem;

  font-family: "DM Mono", monospace;
  font-synthesis: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 12px 12px, rgba(112, 71, 47, 0.06) 2px, transparent 2px) 0 0 / 24px 24px,
    var(--cream);
}

button { font: inherit; }

h1, h2 { font-family: "Fredoka", sans-serif; margin: 0; }

/* ---------- Reload entry screen ---------- */

.start-screen {
  position: fixed;
  z-index: 20;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background:
    radial-gradient(circle at 12px 12px, rgba(112, 71, 47, 0.1) 2px, transparent 2px) 0 0 / 24px 24px,
    rgba(248, 236, 205, 0.97);
}

.title-card {
  width: min(560px, 100%);
  padding: clamp(28px, 6vw, 52px);
  border: 4px solid var(--ink);
  border-radius: 24px;
  background: var(--paper);
  box-shadow: 12px 12px 0 var(--ink);
  text-align: center;
}

.title-card .eyebrow { margin-bottom: 12px; }
.title-card h1 { display: grid; font-size: clamp(2rem, 8vw, 4rem); line-height: 0.88; text-transform: uppercase; }
.title-card h1 span { color: var(--brown); font-size: 0.55em; }
.title-tagline { margin: 22px auto 10px; font-family: "Fredoka", sans-serif; font-size: var(--fs-lg); }
.save-summary { min-height: 1.5em; margin: 0 0 24px; color: var(--muted); font-size: var(--fs-xs); }
.title-actions { display: flex; justify-content: center; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.title-links { display: flex; justify-content: center; gap: 16px; }

.app {
  width: min(1460px, calc(100% - 28px));
  margin: 0 auto;
  padding: 18px 0 20px;
  display: grid;
  gap: var(--gap);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--dark-green);
  font-size: var(--fs-2xs);
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

/* ---------- Shared surfaces ---------- */

.panel {
  min-width: 0;
  overflow: hidden;
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: var(--shadow);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 16px;
  border-bottom: 3px solid var(--ink);
}

.panel-head h2 { font-size: var(--fs-lg); line-height: 1.1; }
.panel-head--shop { align-items: flex-end; background: var(--green); }
.panel-head--flow { background: var(--pale-green); }

.panel-badge {
  color: var(--ink);
  font-size: var(--fs-2xs);
  opacity: 0.75;
  text-align: right;
}

.meter {
  height: 10px;
  overflow: hidden;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--cream);
}

.meter--slim { height: 8px; }

.meter > span {
  display: block;
  width: 0;
  height: 100%;
  background: var(--orange);
  transition: width 300ms ease-out, background-color 300ms linear;
}

.icon-button, .text-button {
  border: 0;
  color: var(--ink);
  background: transparent;
  cursor: pointer;
}

.icon-button {
  align-self: center;
  padding: 9px 13px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  font-size: var(--fs-sm);
  font-weight: 500;
  white-space: nowrap;
  box-shadow: 3px 3px 0 var(--ink);
}

.icon-button:hover { background: var(--yellow); }

/* ---------- Top bar: wordmark + live numbers ---------- */

.topbar {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: var(--gap);
  align-items: center;
}

.wordmark {
  display: grid;
  font-size: clamp(0.95rem, 1.5vw, 1.3rem);
  line-height: 0.95;
  letter-spacing: -0.03em;
  text-transform: uppercase;
  white-space: nowrap;
}

.wordmark span { color: var(--brown); font-size: 0.72em; }

.hud {
  display: grid;
  grid-template-columns: 1.15fr 1.4fr 1.15fr 0.8fr;
  overflow: hidden;
  border: 3px solid var(--ink);
  border-radius: 14px;
  background: var(--paper);
  box-shadow: var(--shadow);
}

.hud-tile {
  display: grid;
  align-content: center;
  gap: 3px;
  min-width: 0;
  padding: 9px 14px;
}

.hud-tile + .hud-tile { border-left: 2px solid var(--ink); }
.hud-tile--cash { background: var(--yellow); }
.hud-tile--clock { background: var(--cream); }

.hud-label {
  color: var(--muted);
  font-size: var(--fs-2xs);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-tile--cash .hud-label { color: var(--brown); }

.hud-value {
  font-family: "Fredoka", sans-serif;
  font-size: 1.6rem;
  font-weight: 700;
  line-height: 1.05;
}

.hud-value--sm { font-size: 1.15rem; }
.hud-value small { color: var(--muted); font-family: "DM Mono", monospace; font-size: var(--fs-xs); font-weight: 400; }
.hud-sub { color: var(--muted); font-size: var(--fs-2xs); white-space: nowrap; }
.hud-sub span { color: var(--dark-green); }

.hud-tile--food .meter { margin-top: 3px; }
.hud-tile--food .meter > span { background: var(--green); }
.hud-tile--food.is-low .meter > span { background: var(--red); }
.hud-tile--food.is-low .hud-label { color: var(--red); }

/* ---------- Mission strip ---------- */

.mission {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) minmax(180px, 260px);
  gap: 6px 18px;
  align-items: center;
  padding: 12px 18px;
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: var(--shadow);
}

.mission > .eyebrow {
  margin: 0;
  padding-right: 18px;
  border-right: 2px solid rgba(48, 38, 30, 0.15);
  white-space: nowrap;
}

.mission-copy { display: grid; gap: 2px; min-width: 0; }
.mission-copy strong { font-family: "Fredoka", sans-serif; font-size: var(--fs-md); }
.mission-copy p { margin: 0; color: var(--muted); font-size: var(--fs-xs); line-height: 1.35; }
.mission-progress { display: grid; gap: 5px; }

.mission-figures {
  font-family: "Fredoka", sans-serif;
  font-size: var(--fs-sm);
  text-align: right;
  white-space: nowrap;
}

/* ---------- Board: shop | stage | phone ---------- */

.board {
  display: grid;
  grid-template-columns: minmax(285px, 0.85fr) minmax(440px, 1.9fr) minmax(305px, 0.88fr);
  gap: var(--gap);
  height: clamp(580px, 76vh, 800px);
}

.board > * { min-height: 0; }
.stage-column { display: flex; flex-direction: column; gap: var(--gap); min-width: 0; }

.panel--stage { position: relative; flex: 1 1 auto; min-height: 240px; }
.panel--flow { flex: 0 0 auto; }

#scene {
  display: block;
  width: 100%;
  height: 100%;
  cursor: pointer;
  touch-action: manipulation;
}

.toast-region {
  position: absolute;
  z-index: 3;
  top: 14px;
  right: 14px;
  display: grid;
  gap: 7px;
  pointer-events: none;
}

.toast {
  padding: 8px 11px;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: var(--yellow);
  box-shadow: 3px 3px 0 var(--ink);
  font-size: var(--fs-xs);
  animation: toast-in 2.8s both;
}

@keyframes toast-in {
  0% { opacity: 0; transform: translateX(20px); }
  8%, 80% { opacity: 1; transform: none; }
  100% { opacity: 0; transform: translateY(-8px); }
}

/* ---------- Pipeline: left to right, with utilisation ---------- */

.flow {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr) auto minmax(0, 1fr);
  gap: 14px;
  align-items: start;
  padding: 14px 18px 15px;
}

.flow-step { display: grid; gap: 4px; min-width: 0; }

.flow-label {
  color: var(--muted);
  font-size: var(--fs-2xs);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.flow-value { font-family: "Fredoka", sans-serif; font-size: var(--fs-lg); line-height: 1; }
.flow-step .meter { margin-top: 2px; }
.flow-step .meter > span { background: var(--green); }
.flow-step--money .meter > span { background: var(--yellow); }
.flow-util { color: var(--dark-green); font-size: var(--fs-2xs); }
.flow-step.is-starved .meter > span { background: var(--red); }
.flow-step.is-starved .flow-util { color: var(--red); }

.flow-join {
  align-self: center;
  padding-top: 14px;
  color: var(--muted);
  font-size: var(--fs-md);
}

.flow-foot {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 4px 18px;
  padding: 9px 18px;
  border-top: 2px solid var(--ink);
  background: var(--cream);
}

.flow-foot-item { color: var(--muted); font-size: var(--fs-2xs); text-transform: uppercase; }
.flow-foot-item strong { color: var(--brown); font-size: var(--fs-xs); }
.flow-foot-item.is-crowded { color: var(--red); font-weight: 500; }

/* ---------- Shop ---------- */

.panel--shop { display: flex; flex-direction: column; }
.panel-head--shop h2 { font-size: 1.35rem; }
.panel-head--shop .eyebrow { margin-bottom: 2px; color: var(--ink); opacity: 0.7; }

.panel-head--shop .panel-badge {
  padding: 3px 8px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--paper);
  opacity: 1;
  white-space: nowrap;
}

.upgrade-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-color: var(--green) transparent;
  scrollbar-width: thin;
}

.upgrade-group {
  position: sticky;
  z-index: 1;
  top: 0;
  padding: 6px 16px 5px;
  border-bottom: 2px solid var(--ink);
  color: var(--dark-green);
  background: var(--cream);
  font-size: var(--fs-2xs);
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.upgrade {
  position: relative;
  width: 100%;
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto;
  grid-template-areas: "icon body price";
  align-items: center;
  gap: 5px 12px;
  padding: 11px 16px 12px;
  border: 0;
  border-bottom: 2px solid rgba(48, 38, 30, 0.18);
  color: var(--ink);
  text-align: left;
  background: var(--paper);
  cursor: pointer;
  transition: background 120ms, transform 120ms;
}

.upgrade:hover:not(:disabled) { background: #fff2b5; }
.upgrade:focus-visible { outline: 3px solid var(--dark-green); outline-offset: -3px; }
.upgrade:disabled { cursor: not-allowed; }
.upgrade:disabled .upgrade-icon, .upgrade:disabled .upgrade-name { opacity: 0.55; }
.upgrade:disabled .upgrade-description { opacity: 0.7; }

.upgrade-icon {
  grid-area: icon;
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border: 2px solid var(--ink);
  border-radius: 10px;
  background: var(--cream);
  font-size: 1.35rem;
}

.upgrade-body { grid-area: body; min-width: 0; }

.upgrade-name {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-family: "Fredoka", sans-serif;
  font-size: var(--fs-sm);
  font-weight: 600;
}

.upgrade-level {
  flex: 0 0 auto;
  padding: 1px 5px;
  border-radius: 4px;
  color: var(--dark-green);
  background: rgba(66, 98, 74, 0.12);
  font-family: "DM Mono", monospace;
  font-size: var(--fs-2xs);
  font-weight: 400;
}

.upgrade-description {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: var(--fs-2xs);
  line-height: 1.4;
}

/* Empty for most rows, so it collapses out of the way until it has something
   to say. */
.upgrade-note { display: none; }

.upgrade-note:not(:empty) {
  display: block;
  margin-top: 3px;
  color: var(--red);
  font-size: var(--fs-2xs);
  line-height: 1.35;
}

.upgrade-price {
  grid-area: price;
  align-self: center;
  padding: 5px 8px;
  border-radius: 6px;
  color: var(--paper);
  background: var(--brown);
  font-size: var(--fs-xs);
  white-space: nowrap;
}

.upgrade:disabled .upgrade-price { background: var(--muted); }
.upgrade[data-block="full"] .upgrade-price { background: var(--dark-green); }

/* Rides the bottom edge of the row, so gaining cash never reflows the list. */
.upgrade-meter {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  display: none;
  height: 4px;
  background: rgba(48, 38, 30, 0.1);
}

.upgrade[data-block="cash"] .upgrade-meter { display: block; }

.upgrade-meter > span {
  display: block;
  width: 0;
  height: 100%;
  background: var(--yellow);
  transition: width 300ms ease-out;
}

.upgrade[data-upgrade="emergency"] { background: #fdf0d8; }
.upgrade[data-upgrade="emergency"] .upgrade-price { background: var(--red); }

/* ---------- Phone ---------- */

.panel--phone {
  display: flex;
  flex-direction: column;
  border-width: 5px;
  border-radius: 30px;
  background: #d9ded2;
  box-shadow: 0 8px 0 rgba(48, 38, 30, 0.22);
}

.phone-speaker, .phone-home {
  flex: 0 0 auto;
  border-radius: 99px;
  background: var(--ink);
}

.phone-speaker { width: 48px; height: 5px; margin: 8px auto 5px; opacity: 0.72; }
.phone-home { width: 64px; height: 5px; margin: 7px auto 9px; opacity: 0.75; }

.phone-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px 10px;
  border-top: 3px solid var(--ink);
  border-bottom: 3px solid var(--ink);
  background: var(--dark-green);
  color: var(--paper);
}

.phone-head .eyebrow { margin: 0; color: #d8f0c4; font-size: var(--fs-2xs); }
.phone-head h2 { font-size: var(--fs-md); }

.app-avatar {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--yellow);
  font-size: var(--fs-md);
}

.status-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 3px;
  border-radius: 50%;
  background: #b9ef78;
  box-shadow: 0 0 0 2px rgba(185, 239, 120, 0.16);
}

.signal { margin-left: auto; color: #d8f0c4; font-size: var(--fs-2xs); letter-spacing: 2px; }

.comms-feed {
  flex: 1 1 auto;
  display: grid;
  align-content: start;
  gap: 10px;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 13px 11px 15px;
  scroll-behavior: smooth;
  scrollbar-color: var(--dark-green) transparent;
  scrollbar-width: thin;
}

.message-row { display: flex; align-items: flex-end; gap: 7px; }

.message-avatar {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--cream);
  font-size: var(--fs-sm);
  box-shadow: 1px 2px 0 rgba(48, 38, 30, 0.18);
}

.message-bubble {
  position: relative;
  max-width: calc(100% - 42px);
  padding: 7px 10px 8px;
  border: 2px solid var(--ink);
  border-radius: 13px 13px 13px 3px;
  background: var(--paper);
  box-shadow: 2px 2px 0 rgba(48, 38, 30, 0.16);
}

.message-bubble::before {
  position: absolute;
  bottom: -2px;
  left: -6px;
  width: 8px;
  height: 8px;
  border-bottom: 2px solid var(--ink);
  background: var(--paper);
  content: "";
  transform: skewX(-34deg);
}

.message-meta { display: flex; justify-content: space-between; gap: 10px; color: var(--dark-green); font-size: var(--fs-2xs); }
.message-meta span { color: var(--muted); white-space: nowrap; }
.message-bubble p { margin: 4px 0 0; font-size: var(--fs-xs); line-height: 1.45; }
.system-message .message-avatar { background: var(--pale-green); }
.system-message .message-bubble { background: #edf3e8; }

/* ---------- Footer + dialog ---------- */

footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 4px;
  color: var(--muted);
  font-size: var(--fs-2xs);
}

footer p { margin: 0; }
.text-button { color: var(--muted); font-size: var(--fs-2xs); text-decoration: underline; }
.footer-actions { display: flex; gap: 14px; }
.footer-actions > span { opacity: 0.7; }

dialog {
  max-width: 420px;
  padding: 24px;
  border: 3px solid var(--ink);
  border-radius: 16px;
  color: var(--ink);
  background: var(--paper);
  box-shadow: 8px 8px 0 var(--ink);
}

dialog::backdrop { background: rgba(48, 38, 30, 0.65); }
dialog h2 { font-size: 1.4rem; }
dialog p:not(.eyebrow) { color: var(--muted); font-size: var(--fs-sm); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.primary-button, .secondary-button, .danger-button { padding: 9px 12px; border: 2px solid var(--ink); border-radius: 7px; font-size: var(--fs-sm); font-weight: 500; cursor: pointer; }
.primary-button { background: var(--yellow); box-shadow: 3px 3px 0 var(--ink); }
.secondary-button { background: var(--cream); }
.danger-button { color: white; background: var(--red); }

.roadmap-dialog { width: min(680px, calc(100% - 28px)); max-width: 680px; }
.roadmap-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-top: 18px; }
.roadmap-list section { padding: 13px; border: 2px solid var(--ink); border-radius: 10px; background: var(--cream); }
.roadmap-list h3 { margin: 7px 0 3px; font-family: "Fredoka", sans-serif; font-size: var(--fs-md); }
.roadmap-list p { margin: 0; line-height: 1.45; }
.roadmap-status { display: inline-block; padding: 2px 6px; border-radius: 4px; color: var(--paper); background: var(--muted); font-size: var(--fs-2xs); text-transform: uppercase; }
.roadmap-status.is-live { color: var(--ink); background: var(--green); }
.roadmap-status.is-next { color: var(--ink); background: var(--yellow); }
.roadmap-actions { align-items: center; justify-content: space-between; }
.roadmap-actions a { color: var(--dark-green); font-size: var(--fs-xs); }

.changelog-dialog { width: min(600px, calc(100% - 28px)); max-width: 600px; }
.change-list { display: grid; gap: 10px; max-height: min(52vh, 480px); margin-top: 18px; overflow-y: auto; }
.change-list section { padding: 13px 15px; border: 2px solid var(--ink); border-radius: 10px; background: var(--cream); }
.change-list time { color: var(--dark-green); font-size: var(--fs-2xs); text-transform: uppercase; }
.change-list h3 { margin: 4px 0 6px; font-family: "Fredoka", sans-serif; font-size: var(--fs-md); }
.change-list ul { margin: 0; padding-left: 20px; color: var(--muted); font-size: var(--fs-xs); line-height: 1.55; }

/* ---------- Responsive ---------- */

/* Two columns: stage keeps the width, shop and phone share a rail. */
@media (max-width: 1180px) {
  .board {
    grid-template-columns: minmax(0, 1.55fr) minmax(290px, 0.95fr);
    grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
    height: clamp(680px, 88vh, 940px);
  }

  .stage-column { grid-column: 1; grid-row: 1 / span 2; }
  .panel--shop { grid-column: 2; grid-row: 1; }
  .panel--phone { grid-column: 2; grid-row: 2; }
}

/* Single column: the page scrolls, panels take natural heights. */
@media (max-width: 900px) {
  .topbar { grid-template-columns: minmax(0, 1fr) auto; }
  .wordmark { font-size: 1.1rem; }
  .hud { grid-column: 1 / -1; grid-row: 2; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .hud-tile:nth-child(3) { border-left: 0; }
  .hud-tile:nth-child(n + 3) { border-top: 2px solid var(--ink); }

  .mission { grid-template-columns: minmax(0, 1fr); gap: 8px; }
  .mission > .eyebrow { padding-right: 0; border-right: 0; }
  .mission-progress { grid-template-columns: minmax(0, 1fr); }
  .mission-figures { text-align: left; }

  .board { grid-template-columns: minmax(0, 1fr); grid-template-rows: none; height: auto; }
  .stage-column, .panel--shop, .panel--phone { grid-column: 1; grid-row: auto; }
  .panel--stage { height: 440px; }
  .upgrade-list { max-height: 460px; }
  .comms-feed { height: 320px; }
}

@media (max-width: 620px) {
  :root { --gap: 12px; }
  .app { width: calc(100% - 18px); padding-top: 14px; }
  .hud { grid-template-columns: minmax(0, 1fr); }
  .hud-tile + .hud-tile { border-top: 2px solid var(--ink); border-left: 0; }
  .hud-value { font-size: 1.35rem; }

  .flow { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .flow-join { justify-self: center; padding: 0; transform: rotate(90deg); }
  .panel--stage { height: 380px; }
  .roadmap-list { grid-template-columns: minmax(0, 1fr); }
  .roadmap-actions { align-items: stretch; flex-direction: column; }
  footer { align-items: flex-start; flex-direction: column; }
  .footer-actions { flex-wrap: wrap; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
