:root {
  --bg: #1d2b1c;
  --grass: #5d8f3e;
  --panel: rgba(28, 33, 24, 0.86);
  --panel-border: rgba(255, 240, 200, 0.14);
  --text: #f4ecd6;
  --text-dim: #b9b19a;
  --accent: #f2c14e;
  --danger: #e0644a;
  --ok: #8fd16a;
  --radius: 10px;
  --gutter: 12px;
  --font-pixel: "Pixelify Sans", ui-monospace, monospace;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 var(--font-ui);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

#app {
  position: fixed;
  inset: 0;
}

#world {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  touch-action: none;
  cursor: grab;
}

/* Shared look for every floating UI layer */
.hud {
  position: absolute;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  backdrop-filter: blur(6px);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
}

.hud[hidden] { display: none; }

/* Centered with auto margins: left:50% + translate would cap the width at half the screen. */
#topbar {
  top: max(var(--gutter), env(safe-area-inset-top));
  left: var(--gutter);
  right: var(--gutter);
  margin-inline: auto;
  width: fit-content;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 5px 6px 5px 12px;
  white-space: nowrap;
}

.clock { display: flex; align-items: center; gap: 8px; }
.clock .day { font: 700 15px/1 var(--font-ui); color: var(--text); font-variant-numeric: tabular-nums; }
.clock .time { font: 500 14px/1 var(--font-ui); color: var(--text-dim); font-variant-numeric: tabular-nums; min-width: 3.1em; }
.clock .weather { font-size: 12px; color: #a9c8e6; }

.season-chip {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 2px 7px;
  border-radius: 999px;
  color: #1d2b1c;
  background: var(--season, #8fd16a);
}
.season-chip[data-season="0"] { --season: #9edb78; }
.season-chip[data-season="1"] { --season: #f2c14e; }
.season-chip[data-season="2"] { --season: #e8955a; }
.season-chip[data-season="3"] { --season: #b9d8ec; }

/* Sun by day, crescent moon by night */
.tod { width: 14px; height: 14px; border-radius: 50%; flex: none; transition: background 0.4s, box-shadow 0.4s; }
.tod.sun { background: #f6c94f; box-shadow: 0 0 6px rgba(246, 201, 79, 0.7); }
.tod.moon { background: transparent; box-shadow: inset -4px -2px 0 0 #e6e2cf; }

.stock { display: flex; align-items: center; gap: 10px; padding-left: 10px; border-left: 1px solid var(--panel-border); }
.res { display: inline-flex; align-items: center; gap: 4px; font: 700 14px/1 var(--font-ui); font-variant-numeric: tabular-nums; }
.res svg { width: 16px; height: 16px; shape-rendering: crispEdges; }
.res b { min-width: 2ch; font-weight: 700; }
.res.full b { color: var(--accent); }
.res.low b { color: var(--danger); }
.res[hidden] { display: none; }
.mineral { color: #c9a8f5; }
.buried { list-style: none; margin: 6px 0 0; padding: 0; max-height: 200px; overflow-y: auto; font-size: 12px; line-height: 1.5; }
.buried li { padding: 2px 0; border-top: 1px solid var(--panel-border); }
.buried li:first-child { border-top: 0; }
#panel button:disabled { opacity: 0.45; cursor: not-allowed; }

.speed {
  display: flex;
  gap: 2px;
  padding: 2px;
  background: rgba(0, 0, 0, 0.25);
  border-radius: 8px;
}
.speed button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font: 700 13px/1 var(--font-ui);
  min-width: 34px;
  height: 28px;
  border-radius: 6px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.speed button svg { width: 11px; height: 11px; fill: currentColor; }
.speed button:hover { color: var(--text); background: rgba(255, 255, 255, 0.07); }
.speed button[aria-pressed="true"] { background: var(--accent); color: #2a2110; }
.speed button[data-speed="0"][aria-pressed="true"] { background: var(--danger); color: #fff; }
.speed button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

#app.paused #world { filter: saturate(0.7) brightness(0.92); }

#toolbar {
  bottom: max(var(--gutter), env(safe-area-inset-bottom));
  left: var(--gutter);
  right: var(--gutter);
  margin-inline: auto;
  width: fit-content;
}

#panel {
  top: 72px;
  right: var(--gutter);
  width: 280px;
  max-height: calc(100% - 180px);
  overflow-y: auto;
  padding: 10px 12px 12px;
}

#log {
  bottom: max(var(--gutter), env(safe-area-inset-bottom));
  left: var(--gutter);
  width: 300px;
  padding: 6px 4px;
  pointer-events: none;
}
/* The whole kept history scrolls; about six lines show at once. */
#log ol {
  list-style: none; margin: 0; padding: 0 2px 0 0; display: flex; flex-direction: column; gap: 2px;
  max-height: 10.5em; overflow-y: auto; overscroll-behavior: contain; pointer-events: auto;
  scrollbar-width: thin; scrollbar-color: rgba(255, 255, 255, 0.25) transparent;
}
#log ol::-webkit-scrollbar { width: 6px; }
#log ol::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.25); border-radius: 3px; }
#log li {
  display: flex;
  gap: 8px;
  align-items: baseline;
  padding: 3px 8px;
  border-left: 3px solid var(--kind, #7f8a74);
  border-radius: 3px;
  font-size: 12.5px;
  line-height: 1.35;
}
#log li.new { animation: log-in 0.35s ease-out; }
#log li:nth-last-child(n + 7) { opacity: 0.6; }
#log li .when { color: var(--text-dim); font-size: 11px; font-variant-numeric: tabular-nums; flex: none; min-width: 5.6em; }
#log li.bad { --kind: var(--danger); }
#log li.good { --kind: var(--ok); }
#log li.info { --kind: #8fb4d9; }
#log li .times { margin-left: auto; padding: 0 5px; border-radius: 8px; background: rgba(255, 255, 255, 0.12); font-size: 11px; font-variant-numeric: tabular-nums; flex: none; }
#log-filters { display: flex; gap: 3px; margin: 0 4px 4px; pointer-events: auto; }
#log-filters button { font: 600 11px/1 var(--font-ui); padding: 3px 5px; border-radius: 6px; border: 1px solid var(--panel-border); background: rgba(255, 255, 255, 0.06); color: var(--text); cursor: pointer; }
#log-filters button[aria-pressed="false"] { opacity: 0.35; filter: grayscale(1); }
#log-filters [data-act="level"] { margin-left: auto; color: var(--text-dim); }
#log-filters button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.toggle.select { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.toggle.select select { font: inherit; padding: 2px 4px; border-radius: 6px; background: rgba(0, 0, 0, 0.3); color: inherit; border: 1px solid var(--panel-border); }
@keyframes log-in { from { opacity: 0; transform: translateX(-8px); } }

/* Follows the mouse; positioned from JS. */
#tooltip {
  position: fixed;
  left: 0;
  top: 0;
  padding: 6px 10px;
  font-size: 12px;
  color: var(--text-dim);
  pointer-events: none;
  white-space: nowrap;
  z-index: 5;
}
#tooltip b { color: var(--text); font-weight: 600; }
#tooltip .bad { color: var(--danger); font-weight: 600; }

@media (max-width: 640px) {
  #topbar { gap: 6px; padding-left: 8px; }
  .clock { gap: 6px; }
  .clock .weather { display: none; }
  .season-chip { font-size: 10px; padding: 2px 5px; letter-spacing: 0.03em; }
  .speed button { min-width: 28px; height: 26px; }
  #panel {
    top: auto;
    bottom: 84px;
    left: var(--gutter);
    right: var(--gutter);
    width: auto;
  }
  /* Phones: full width along the bottom, two lines tall (scroll for more). */
  #log { right: var(--gutter); width: auto; }
  #log ol { max-height: 4.6em; }
}

@media (max-width: 380px) {
  .tod { display: none; }
  #topbar { gap: 4px; }
  .speed button { min-width: 26px; }
}

/* Narrow screens: stockpile moves to its own row under the clock and speed controls. */
@media (max-width: 640px) {
  #topbar { flex-wrap: wrap; justify-content: center; row-gap: 4px; padding: 5px 6px; }
  .stock { order: 3; width: 100%; justify-content: center; border-left: 0; border-top: 1px solid var(--panel-border); padding: 5px 0 1px; gap: 16px; }
}

/* ---------- Build menu ---------- */
#toolbar { display: flex; flex-direction: column; gap: 4px; padding: 5px; max-width: calc(100% - 2 * var(--gutter)); }
#toolbar .tools, #toolbar .palette { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; justify-content: center; }
#toolbar .tools::-webkit-scrollbar, #toolbar .palette::-webkit-scrollbar { display: none; }
#toolbar .palette { padding-bottom: 4px; border-bottom: 1px solid var(--panel-border); }
#toolbar .palette[hidden] { display: none; }
.build-btn {
  appearance: none;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  border-radius: 8px;
  padding: 4px 8px 5px;
  min-width: 74px;
  display: grid;
  justify-items: center;
  gap: 1px;
  cursor: pointer;
  font: inherit;
  flex: none;
}
.build-btn img { width: 36px; height: 36px; image-rendering: pixelated; }
.build-btn .name { font-size: 12px; font-weight: 600; white-space: nowrap; }
.build-btn .cost { display: flex; gap: 6px; font-size: 11px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.build-btn .cost span { display: inline-flex; align-items: center; gap: 2px; }
.build-btn .cost svg { width: 10px; height: 10px; shape-rendering: crispEdges; }
.build-btn .cost .short { color: var(--danger); }
.build-btn:hover { background: rgba(255, 255, 255, 0.09); }
.build-btn.locked { opacity: 0.45; filter: grayscale(0.8); }
.build-btn.locked:hover { background: none; }
.build-btn.locked .name::before { content: ''; display: inline-block; width: 7px; height: 8px; margin-right: 3px; vertical-align: -1px;
  background: linear-gradient(currentColor, currentColor) 0 3px / 7px 5px no-repeat, radial-gradient(circle at 50% 100%, transparent 1.5px, currentColor 1.6px 2.6px, transparent 2.7px) 0 0 / 7px 5px no-repeat; }
.build-btn[aria-pressed="true"] { background: rgba(242, 193, 78, 0.18); border-color: var(--accent); }
.build-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

#hint {
  bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 84px);
  left: var(--gutter);
  right: var(--gutter);
  margin-inline: auto;
  width: fit-content;
  max-width: calc(100% - 2 * var(--gutter));
  padding: 6px 12px;
  font-size: 12.5px;
  text-align: center;
}
#hint.bad { color: #ffb3a6; border-color: rgba(224, 100, 74, 0.5); }

#app.placing #world { cursor: crosshair; }

/* Keep the log clear of the build menu on mid-size screens. */
@media (max-width: 1180px) {
  #log { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 84px); width: 260px; }
}
@media (max-width: 640px) {
  .build-btn { min-width: 64px; padding: 3px 6px 4px; }
  .build-btn img { width: 30px; height: 30px; }
  #hint { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 136px); }
  #log { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 76px); }
}
@media (max-width: 640px) {
  /* While placing, the hint takes the log's place. */
  #app.placing #log { display: none; }
  #hint { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 76px); }
}

/* ---------- Population ---------- */
.res.pop small { color: var(--text-dim); font-size: 12px; font-weight: 600; margin-left: -4px; }
.res.pop.crowded small { color: var(--danger); }

/* Milestone banner: drops in under the top bar, then fades away. */
#banner {
  position: absolute;
  top: 74px;
  left: 0;
  right: 0;
  margin-inline: auto;
  width: fit-content;
  max-width: calc(100% - 2 * var(--gutter));
  padding: 10px 22px 12px;
  text-align: center;
  background: linear-gradient(180deg, rgba(60, 44, 18, 0.92), rgba(36, 28, 14, 0.92));
  border: 1px solid rgba(242, 193, 78, 0.6);
  border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4), 0 0 24px rgba(242, 193, 78, 0.2);
  pointer-events: none;
  animation: banner 6s ease-in-out forwards;
}
#banner strong { display: block; font: 400 26px/1.1 var(--font-pixel); color: var(--accent); letter-spacing: 0.03em; }
#banner span { font-size: 13px; color: var(--text); }
@keyframes banner {
  0% { opacity: 0; transform: translateY(-12px) scale(0.96); }
  8%, 85% { opacity: 1; transform: none; }
  100% { opacity: 0; transform: translateY(-6px); }
}
@media (max-width: 640px) { #banner { top: 100px; } #banner strong { font-size: 22px; } }

/* ---------- Tool buttons ---------- */
.tool-btn {
  position: relative;
  appearance: none;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  border-radius: 8px;
  padding: 4px 6px 5px;
  min-width: 58px;
  display: grid;
  justify-items: center;
  gap: 1px;
  cursor: pointer;
  font: inherit;
  flex: none;
}
.tool-btn img { width: 28px; height: 28px; image-rendering: pixelated; }
.tool-btn .name { font-size: 11.5px; font-weight: 600; }
.tool-btn:hover { background: rgba(255, 255, 255, 0.09); }
.tool-btn[aria-pressed="true"] { background: rgba(242, 193, 78, 0.18); border-color: var(--accent); }
.tool-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tool-btn .badge {
  position: absolute; top: 2px; right: 4px;
  min-width: 16px; height: 16px; padding: 0 4px;
  border-radius: 8px; background: var(--ok); color: #1d2b1c;
  font-size: 10.5px; font-weight: 700; line-height: 16px; text-align: center;
}
.tool-btn .badge.empty { background: #6b6658; color: var(--text); }
.build-btn.brush { min-width: 62px; }

/* Raise hint and log above the taller toolbar when a palette is open. */
#app.palette-open #hint { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 160px); }
@media (max-width: 1180px) {
  #app.palette-open #log { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 160px); }
}

/* ---------- Info panel ---------- */
#panel header { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
#panel h2 { margin: 0; font: 400 20px/1.1 var(--font-pixel); color: var(--accent); }
#panel .close { appearance: none; border: 0; background: transparent; color: var(--text-dim); font-size: 22px; line-height: 1; cursor: pointer; padding: 0 4px; border-radius: 6px; }
#panel .close:hover { color: var(--text); background: rgba(255, 255, 255, 0.08); }
#panel .sub { margin: 0 0 8px; font-size: 12.5px; color: var(--text-dim); }
#panel .sub b { color: var(--text); font-weight: 600; }
#panel .facts { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; margin: 8px 0 0; font-size: 12.5px; }
#panel dt { color: var(--text-dim); }
#panel dd { margin: 0; }
#panel .dim { color: var(--text-dim); }
#panel .bad { color: var(--danger); }
#panel .good { color: #9fd38a; }
#panel .god { margin-top: 6px; }
#panel .god p { margin: 1px 0 0; font-size: 11px; }
.bar.god { --fill: var(--god); }
#panel .dedicate { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; margin-top: 8px; font-size: 12px; }
#panel .dedicate button { font: inherit; padding: 3px 7px; border-radius: 6px; border: 1px solid var(--panel-border); background: rgba(255, 255, 255, 0.05); color: inherit; cursor: pointer; }
#panel .dedicate button[aria-pressed="true"] { border-color: var(--accent); background: rgba(242, 193, 78, 0.18); }
.bar { display: grid; grid-template-columns: 4.6em 1fr minmax(3.4em, auto); align-items: center; gap: 8px; font-size: 12px; margin: 3px 0; }
.bar-label { color: var(--text-dim); }
.bar-track { height: 7px; border-radius: 4px; background: rgba(0, 0, 0, 0.35); overflow: hidden; }
.bar-fill { display: block; height: 100%; border-radius: 4px; background: var(--fill, var(--accent)); transition: width 0.25s; }
.bar-value { text-align: right; font-variant-numeric: tabular-nums; }
.bar.warm { --fill: #e8a24a; }
.bar.cool { --fill: #7fb2e0; }
.bar.good { --fill: var(--ok); }
.bar.bad { --fill: var(--danger); }
#panel .actions { display: flex; gap: 6px; margin-top: 10px; }
#panel .actions:empty { display: none; }
#panel .actions button {
  flex: 1; appearance: none; border: 1px solid var(--panel-border); background: rgba(255, 255, 255, 0.06);
  color: var(--text); font: 600 12.5px/1 var(--font-ui); padding: 8px 10px; border-radius: 7px; cursor: pointer;
}
#panel .actions button:hover { background: rgba(255, 255, 255, 0.12); }
#panel .actions button[aria-pressed="true"] { background: rgba(242, 193, 78, 0.2); border-color: var(--accent); }
#panel .actions button.danger { color: #ffb3a6; border-color: rgba(224, 100, 74, 0.5); }
#panel .actions button.danger[data-armed="1"] { background: var(--danger); color: #fff; }

@media (max-width: 640px) {
  .tool-btn { min-width: 50px; padding: 3px 4px 4px; }
  .tool-btn img { width: 24px; height: 24px; }
  .tool-btn .name { font-size: 10.5px; }
  /* Panel becomes a bottom sheet above the toolbar. */
  #panel { top: auto; left: var(--gutter); right: var(--gutter); width: auto; bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 76px); max-height: 46%; }
  #app.palette-open #hint { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 140px); }
}
@media (max-width: 640px) {
  #panel:not([hidden]) ~ #log { display: none; }
}

/* ---------- Vitals and stats ---------- */
.vitals { display: flex; align-items: center; gap: 10px; padding-left: 10px; border-left: 1px solid var(--panel-border); }
.icon-btn {
  appearance: none; border: 0; background: rgba(0, 0, 0, 0.25); color: var(--text-dim);
  width: 32px; height: 32px; border-radius: 8px; cursor: pointer; display: grid; place-items: center; flex: none;
}
.icon-btn svg { width: 14px; height: 14px; fill: currentColor; }
.icon-btn:hover { color: var(--text); background: rgba(255, 255, 255, 0.08); }
.icon-btn[aria-pressed="true"] { color: #2a2110; background: var(--accent); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

#stats {
  top: 72px;
  left: var(--gutter);
  width: 300px;
  padding: 10px 12px 10px;
  --pop: #c98500;   /* validated categorical pair (dark surface) */
  --beds: #3987e5;
}
#stats header { display: flex; align-items: center; justify-content: space-between; }
#stats h2 { margin: 0; font: 400 18px/1.1 var(--font-pixel); color: var(--text); }
#stats .close { appearance: none; border: 0; background: transparent; color: var(--text-dim); font-size: 22px; line-height: 1; cursor: pointer; padding: 0 4px; border-radius: 6px; }
#stats .close:hover { color: var(--text); background: rgba(255, 255, 255, 0.08); }
#stats .legend { display: flex; gap: 14px; margin: 6px 0 4px; font-size: 12px; color: var(--text-dim); }
#stats .legend span { display: inline-flex; align-items: center; gap: 6px; }
#stats .swatch { display: inline-block; width: 14px; height: 0; border-top: 2px solid var(--pop); }
#stats .swatch.beds { border-top: 2px dashed var(--beds); }
.chart-wrap { position: relative; }
#pop-chart { display: block; width: 100%; height: 130px; cursor: crosshair; }
.chart-tip {
  position: absolute; top: 4px; pointer-events: none;
  background: rgba(15, 18, 12, 0.92); border: 1px solid var(--panel-border); border-radius: 6px;
  padding: 4px 8px; font-size: 11.5px; white-space: nowrap; color: var(--text);
}
.chart-tip .dim { color: var(--text-dim); }
#stats .facts { display: grid; grid-template-columns: 1fr auto 1fr auto; gap: 3px 10px; margin: 8px 0 0; font-size: 12.5px; }
#stats dt { color: var(--text-dim); }
#stats dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }
.table-view { margin-top: 8px; font-size: 12px; color: var(--text-dim); }
.table-view summary { cursor: pointer; }
.table-view table { width: 100%; border-collapse: collapse; margin-top: 6px; font-variant-numeric: tabular-nums; }
.table-view th, .table-view td { text-align: right; padding: 2px 4px; }
.table-view th:first-child, .table-view td:first-child { text-align: left; }
.table-view th { color: var(--text-dim); font-weight: 600; border-bottom: 1px solid var(--panel-border); }
.table-view td { color: var(--text); }
#stats-table { max-height: 140px; overflow-y: auto; }

@media (max-width: 1180px) {
  .vitals { display: none; } /* shown in the stats panel instead */
}
@media (max-width: 640px) {
  #stats { top: 118px; right: var(--gutter); width: auto; max-height: calc(100% - 220px); overflow-y: auto; }
  #stats:not([hidden]) ~ #log { display: none; }
  .icon-btn { width: 28px; height: 26px; }
}
/* Short screens: with the stats panel open, the log keeps only its newest entries. */
@media (max-height: 820px) {
  #stats:not([hidden]) ~ #log ol { max-height: 4.6em; }
}

/* ---------- Game menu ---------- */
#menu {
  top: 70px;
  left: 0; right: 0;
  margin-inline: auto;
  width: min(320px, calc(100% - 2 * var(--gutter)));
  padding: 14px 16px 12px;
  z-index: 6;
}
#menu h2 { margin: 0; font: 400 24px/1.1 var(--font-pixel); color: var(--accent); }
#menu .island { margin: 2px 0 12px; font-size: 12.5px; color: var(--text-dim); }
#menu .menu-actions { display: grid; gap: 6px; }
#menu .menu-actions button {
  appearance: none; border: 1px solid var(--panel-border); background: rgba(255, 255, 255, 0.06); color: var(--text);
  font: 600 13.5px/1 var(--font-ui); padding: 10px 12px; border-radius: 8px; cursor: pointer; text-align: left;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
}
#menu .menu-actions button small { font-weight: 400; color: var(--text-dim); font-size: 11.5px; }
#menu .menu-actions button:hover:not(:disabled) { background: rgba(255, 255, 255, 0.12); }
#menu .menu-actions button:disabled { opacity: 0.45; cursor: default; }
#menu .menu-actions button.primary { background: var(--accent); color: #2a2110; border-color: var(--accent); }
#menu .menu-actions button.primary small { color: #5a4718; }
#menu .menu-actions button[data-armed="1"] { background: var(--danger); border-color: var(--danger); color: #fff; }
#menu .menu-actions button[data-armed="1"] small { color: #ffe0d8; }
#menu label.toggle { display: flex; align-items: center; gap: 8px; margin: 12px 0 0; font-size: 12.5px; cursor: pointer; }
#menu label.toggle input { accent-color: var(--accent); width: 16px; height: 16px; }
#menu .keys { margin: 12px 0 0; padding-top: 10px; border-top: 1px solid var(--panel-border); font-size: 11.5px; color: var(--text-dim); line-height: 1.6; }
#menu kbd { font: 600 10.5px/1 var(--font-ui); background: rgba(255, 255, 255, 0.1); border-radius: 4px; padding: 2px 5px; color: var(--text); }
@media (max-width: 640px) { #menu { top: 110px; } #menu .keys { display: none; } }
@media (max-width: 640px) {
  /* Row 1: clock + speed. Row 2: stockpile + stats/menu buttons. */
  .stock { flex: 1 1 auto; width: auto; border-top: 0; padding-top: 2px; justify-content: flex-start; gap: 12px; }
  #stats-toggle, #menu-toggle { order: 4; }
  /* Hints sit above the log unless the log is hidden for placing. */
  #hint { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 136px); }
  #app.placing #hint { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 76px); }
  #app.placing.palette-open #hint { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 140px); }
}
@media (max-width: 640px) {
  /* A visible hint temporarily takes the log's place. */
  #hint:not([hidden]) ~ #log { display: none; }
  #hint { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 76px); }
  #app.palette-open #hint { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + 140px); }
}

/* Sound toggle: speaker with waves, or with a cross when muted. */
#sound-toggle .cross { display: none; }
#sound-toggle.muted .cross { display: inline; }
#sound-toggle.muted .waves { display: none; }
#sound-toggle[aria-pressed="true"] { color: var(--text-dim); background: rgba(0, 0, 0, 0.25); }
#sound-toggle.muted { color: var(--danger); }
#trial { top: 70px; left: 0; right: 0; margin-inline: auto; width: min(380px, calc(100% - 2 * var(--gutter))); padding: 14px 16px 12px; z-index: 7; }
#trial h2 { margin: 0 0 2px; font: 400 22px/1.1 var(--font-pixel); color: var(--accent); }
#trial .charge { margin: 0 0 10px; font-size: 13px; }
#trial dl { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; margin: 0 0 12px; font-size: 12.5px; }
#trial dt { color: var(--text-dim); }
#trial dd { margin: 0; }
#trial .verdicts { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; }
#trial .verdicts button { appearance: none; border: 1px solid var(--panel-border); background: rgba(255, 255, 255, 0.06); color: var(--text); font: 600 12.5px/1.2 var(--font-ui); padding: 8px 6px; border-radius: 8px; cursor: pointer; }
#trial .verdicts button:hover { background: rgba(255, 255, 255, 0.12); }
#trial .verdicts .innocent { border-color: var(--ok); }
#trial .verdicts .village { grid-column: 1 / -1; background: var(--accent); color: #2a2110; border-color: var(--accent); }
#trial .hint { margin: 8px 0 0; font-size: 11.5px; color: var(--text-dim); }
#menu label.toggle input[type="range"] { width: auto; flex: 1; height: auto; margin-left: auto; max-width: 120px; }
@media (max-width: 640px) { #sound-toggle { display: none; } /* in the menu instead */ }

/* Many buildings: the build/terrain palette wraps onto extra rows when it
   doesn't fit (scrolling past 40% of the screen), and the hint and log sit
   above the toolbar whatever its height (--toolbar-h is measured in ui.js). */
#toolbar .palette { flex-wrap: wrap; overflow-x: hidden; overflow-y: auto; max-height: 40vh; max-width: 800px; align-self: center; }
#app.palette-open #hint, #app.palette-open #log { bottom: calc(max(var(--gutter), env(safe-area-inset-bottom)) + var(--toolbar-h, 140px) + 8px); }
@media (max-width: 640px) {
  /* Seven stockpile counters plus the stats and menu buttons on one row. */
  .stock { gap: 7px; }
  .stock .res { font-size: 12px; gap: 2px; }
  .stock .res svg { width: 13px; height: 13px; }
  #toolbar .build-btn { min-width: 58px; padding: 3px 4px 4px; }
  #toolbar .build-btn img { width: 26px; height: 26px; }
  #toolbar .build-btn .name { font-size: 11px; }
}
