:root {
  --ink: #f4ead8;
  --ink-soft: rgba(244, 234, 216, 0.68);
  --panel: rgba(28, 22, 16, 0.86);
  --panel-line: rgba(244, 234, 216, 0.14);
  --chip: rgba(244, 234, 216, 0.1);
  --chip-hover: rgba(244, 234, 216, 0.18);
  --yellow: #ffd23f;
  --yellow-ink: #2a2118;
  --red: #e0553f;
  --good: #7ed37a;
  --bad: #ff7a64;
  --gap: clamp(0.5rem, 1.4vw, 0.9rem);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #1b1712;
  color: var(--ink);
  font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

#stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
}
#stage.panning { cursor: grabbing; }

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--yellow); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- top bar */
.bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 3;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem 0.8rem;
  padding: 0.45rem var(--gap);
  background: linear-gradient(rgba(20, 15, 10, 0.9), rgba(20, 15, 10, 0.72));
  border-bottom: 1px solid var(--panel-line);
  backdrop-filter: blur(4px);
}
.home {
  color: var(--ink-soft);
  text-decoration: none;
  font-size: 0.9rem;
  padding: 0.2rem 0.35rem;
  border-radius: 6px;
}
.home:hover { color: var(--ink); background: var(--chip); }
.title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  white-space: nowrap;
}
.views, .speed, .tools { display: flex; gap: 0.3rem; flex-wrap: wrap; }
.views { margin-right: auto; }
.tab, .chip {
  border: 1px solid var(--panel-line);
  background: var(--chip);
  border-radius: 999px;
  padding: 0.28rem 0.75rem;
  font-size: 0.85rem;
  line-height: 1.2;
  white-space: nowrap;
}
.chip { padding: 0.28rem 0.6rem; min-width: 2.1rem; }
.tab:hover, .chip:hover { background: var(--chip-hover); }
.tab[aria-pressed="true"], .chip[aria-pressed="true"] {
  background: var(--yellow);
  border-color: var(--yellow);
  color: var(--yellow-ink);
  font-weight: 700;
}
.tab .alert-dot {
  display: inline-block;
  width: 0.5rem; height: 0.5rem;
  margin-left: 0.35rem;
  border-radius: 50%;
  background: var(--bad);
  vertical-align: middle;
}

/* ---------------------------------------------------------------- cards */
.card {
  position: fixed;
  z-index: 2;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(4px);
}
.you {
  left: var(--gap);
  bottom: calc(var(--gap) + var(--safe-b));
  width: min(23rem, calc(100vw - 2 * var(--gap)));
  padding: 0.65rem 0.75rem 0.75rem;
}
.you-head { display: flex; align-items: center; gap: 0.45rem; font-size: 0.92rem; }
.you-state { margin-left: auto; color: var(--ink-soft); font-size: 0.82rem; text-align: right; }
.swatch {
  display: inline-block;
  width: 0.8rem; height: 0.8rem;
  border-radius: 50%;
  flex: none;
  border: 1.5px solid rgba(0, 0, 0, 0.55);
}
.swatch-yellow { background: var(--yellow); }
.swatch-black { background: #1d1a17; border-color: rgba(244, 234, 216, 0.5); margin-right: 0.45rem; }
.swatch-red { background: #c0392b; margin-right: 0.45rem; }

.meter {
  height: 6px;
  margin: 0.45rem 0 0.55rem;
  border-radius: 3px;
  background: rgba(244, 234, 216, 0.12);
  overflow: hidden;
}
.meter-fill {
  height: 100%;
  width: 100%;
  background: var(--good);
  border-radius: 3px;
  transition: width 0.25s, background-color 0.25s;
}
.meter-fill.low { background: var(--bad); }
.meter-red .meter-fill { background: var(--red); }

.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.35rem; }
.btn {
  border: 1px solid var(--panel-line);
  background: var(--chip);
  border-radius: 9px;
  padding: 0.42rem 0.5rem;
  font-size: 0.85rem;
  line-height: 1.2;
}
.btn:hover:not(:disabled) { background: var(--chip-hover); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn-go {
  background: var(--yellow);
  border-color: var(--yellow);
  color: var(--yellow-ink);
  font-weight: 700;
  padding: 0.55rem 1.2rem;
}
.btn-go:hover:not(:disabled) { background: #ffe07a; }
.btn-colony { display: none; }

/* ---------------------------------------------------------------- colony panel */
.colony {
  right: var(--gap);
  top: 3.6rem;
  bottom: calc(var(--gap) + var(--safe-b));
  width: 16.5rem;
  padding: 0.75rem 0.85rem;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.colony h2 {
  margin: 0 0 0.5rem;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
}
.colony h2.red-head { margin-top: 1rem; }
.colony h3 {
  margin: 0.9rem 0 0.35rem;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
}
.sheet-close { display: none; }
.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.25rem 0.75rem;
  margin: 0;
}
.stats div { display: flex; justify-content: space-between; gap: 0.4rem; font-size: 0.85rem; }
.stats dt { color: var(--ink-soft); }
.stats dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }
.queen-row { display: flex; align-items: center; gap: 0.5rem; margin-top: 0.45rem; font-size: 0.85rem; color: var(--ink-soft); }
.queen-row .meter { flex: 1; margin: 0; }
.tri-wrap { display: flex; justify-content: center; }
#tri {
  width: 200px;
  height: 172px;
  touch-action: none;
  cursor: pointer;
  border-radius: 8px;
}
.tri-text { margin: 0.2rem 0 0; text-align: center; font-size: 0.82rem; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.caste { display: flex; align-items: center; gap: 0.45rem; font-size: 0.8rem; color: var(--ink-soft); }
.caste input { flex: 1; min-width: 0; accent-color: var(--yellow); }
.log {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 0.8rem;
  line-height: 1.35;
}
.log li { padding: 0.25rem 0; border-top: 1px solid var(--panel-line); color: var(--ink-soft); }
.log li:first-child { border-top: 0; }
.log .t { color: rgba(244, 234, 216, 0.4); margin-right: 0.35rem; font-variant-numeric: tabular-nums; }
.log .good { color: var(--good); }
.log .bad { color: var(--bad); }

/* ---------------------------------------------------------------- toasts */
.toasts {
  position: fixed;
  z-index: 4;
  top: 3.8rem;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  pointer-events: none;
  width: min(30rem, calc(100vw - 2rem));
}
.toast {
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-left: 4px solid var(--ink-soft);
  border-radius: 9px;
  padding: 0.45rem 0.8rem;
  font-size: 0.88rem;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
  animation: toast-in 0.25s ease-out;
}
.toast.good { border-left-color: var(--good); }
.toast.bad { border-left-color: var(--bad); }
.toast.out { opacity: 0; transition: opacity 0.4s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-6px); } }

/* ---------------------------------------------------------------- overlays */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(14, 10, 7, 0.62);
  backdrop-filter: blur(3px);
  overflow-y: auto;
}
.panel {
  width: min(34rem, 100%);
  margin: auto;
  background: #2a2118;
  border: 1px solid var(--panel-line);
  border-radius: 18px;
  padding: clamp(1.1rem, 4vw, 2rem);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.panel-wide { width: min(58rem, 100%); }
.panel h2 { margin: 0 0 0.8rem; font-size: 1.3rem; }
.big {
  font-size: clamp(2rem, 7vw, 3rem) !important;
  font-weight: 900;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  line-height: 1;
}
.big span { color: var(--yellow); }
.lede { line-height: 1.55; color: var(--ink-soft); margin: 0 0 1rem; }
.lede .y { color: var(--yellow); }
.diff {
  border: 1px solid var(--panel-line);
  border-radius: 12px;
  padding: 0.5rem 0.8rem 0.7rem;
  margin: 0 0 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 1.2rem;
}
.diff legend { padding: 0 0.35rem; font-size: 0.8rem; color: var(--ink-soft); }
.diff label { display: flex; align-items: center; gap: 0.35rem; cursor: pointer; }
.diff input { accent-color: var(--yellow); }
.row { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.best { min-height: 1em; margin: 0.8rem 0 0; font-size: 0.85rem; color: var(--ink-soft); }
.foot {
  margin-top: 1.2rem;
  padding-top: 0.9rem;
  border-top: 1px solid var(--panel-line);
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
}
.foot a { color: var(--yellow); text-decoration: none; }
.foot a:hover { text-decoration: underline; }
.foot small { color: var(--ink-soft); line-height: 1.45; }
.help-cols { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.2rem; }
.help-cols h3 { margin: 0 0 0.4rem; font-size: 0.95rem; color: var(--yellow); }
.help-cols ul { margin: 0; padding-left: 1.1rem; line-height: 1.5; font-size: 0.9rem; color: var(--ink-soft); }
.help-cols li { margin-bottom: 0.35rem; }
.help-cols b { color: var(--ink); }
.keys { font-size: 0.82rem; color: var(--ink-soft); line-height: 1.6; margin: 1rem 0; }
.end-stats { margin: 0 0 1.2rem; gap: 0.35rem 1.5rem; }
.end-stats div { font-size: 0.95rem; }
.noscript { position: fixed; inset: 40% 1rem auto; text-align: center; }

/* ---------------------------------------------------------------- narrow screens */
@media (max-width: 860px) {
  .tools .chip#trailsBtn, .tools .chip#soundBtn { display: none; }
  .colony {
    left: 0; right: 0; bottom: 0; top: auto;
    width: auto;
    max-height: 72vh;
    border-radius: 16px 16px 0 0;
    padding-bottom: calc(0.9rem + var(--safe-b));
    transform: translateY(105%);
    transition: transform 0.25s ease;
    z-index: 5;
  }
  .colony.open { transform: none; }
  .sheet-close {
    display: block;
    position: absolute;
    top: 0.4rem; right: 0.5rem;
    border: 0;
    background: none;
    font-size: 1.5rem;
    line-height: 1;
    color: var(--ink-soft);
  }
  .btn-colony { display: block; background: rgba(255, 210, 63, 0.16); border-color: rgba(255, 210, 63, 0.4); }
  .you { right: var(--gap); width: auto; }
  .actions { grid-template-columns: repeat(4, 1fr); }
  .help-cols { grid-template-columns: 1fr; }
}
@media (max-width: 520px) {
  html, body { font-size: 14px; }
  .title { display: none; }
  .views { margin-right: 0; }
  .tab, .chip { padding: 0.25rem 0.55rem; font-size: 0.8rem; }
  .speed .chip[data-speed="4"] { display: none; }
  .you { padding: 0.5rem 0.55rem 0.6rem; }
  .btn { padding: 0.4rem 0.3rem; font-size: 0.78rem; }
  .toasts { top: 5.6rem; }
}
@media (prefers-reduced-motion: reduce) {
  .toast { animation: none; }
  .colony { transition: none; }
}
