* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #1c2430;
  --panel: #26303f;
  --panel-2: #2f3b4d;
  --text: #e8eef6;
  --muted: #9db0c6;
  --accent: #ffb347;
}

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none; /* no pull-to-refresh mid-game */
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body { touch-action: manipulation; } /* kills double-tap zoom app-wide, keeps palette scroll */

/* game surfaces: no long-press callouts / text selection on touch */
#topbar, #palette, #stage {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

body { display: flex; flex-direction: column; }

#topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid #0008;
  flex: 0 0 auto;
  /* NEVER clip the controls, and never let them be SCROLLED away either. The
     bar used to be nowrap with visible overflow, so on tablets the right-hand
     end fell off the edge with no way to reach it. Wrapping fixed that — but
     the belt-and-braces `overflow-x: auto` left behind meant a stray sideways
     swipe on a touchscreen could still push the whole bar out of view and
     leave it there. Wrapping alone cannot overflow, so the scroll has to go. */
  flex-wrap: wrap;
  row-gap: 6px;
  overflow: visible;
}

#topbar h1 { font-size: 17px; font-weight: 700; white-space: nowrap; }

.spacer { flex: 1 1 0; min-width: 0; }

.stat { color: var(--muted); font-size: 13px; white-space: nowrap; }

.chip-row { display: flex; gap: 6px; flex-wrap: nowrap; }

.chip {
  border: 1px solid #0006;
  background: var(--panel-2);
  color: var(--text);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: transform .08s, background .15s;
}
.chip:hover { transform: translateY(-1px); }
.chip.active { background: var(--accent); color: #26200f; font-weight: 700; }

.ctrl {
  background: var(--panel-2);
  border: 1px solid #0006;
  color: var(--text);
  font-size: 15px;
  border-radius: 8px;
  padding: 5px 10px;
  cursor: pointer;
}
.ctrl:hover { background: #3a4860; }
.ctrl.active { background: var(--accent); color: #26200f; }

#btn-palette { display: none; } /* drawer toggle: phones only (media query below) */

/* narrow panes: shrink the topbar so it holds one row down to ~740px */
@media (max-width: 1080px) {
  #topbar { gap: 8px; }
  .chip { padding: 4px 9px; font-size: 12px; }
}
@media (max-width: 920px) {
  #topbar { gap: 6px; padding: 8px 10px; }
  #topbar h1 { font-size: 15px; }
  .chip-row { gap: 4px; }
  .chip { padding: 4px 7px; font-size: 11px; }
  .chip:not(.active) .chip-label { display: none; } /* emoji-only, active keeps its name */
  .ctrl { padding: 4px 8px; font-size: 14px; }
  .stat { font-size: 12px; }
}
/* Terrain picker drops to its own full row well before things get tight. It is
   the widest group, so moving it early keeps title + stats + controls together
   on one reachable row. Coarse pointers get 44px targets, which makes the bar
   much wider — so they take the second row sooner. */
@media (max-width: 1024px) {
  #terrain-picker { order: 10; width: 100%; justify-content: center; }
}
@media (pointer: coarse) and (max-width: 1180px) {
  #terrain-picker { order: 10; width: 100%; justify-content: center; }
}
@media (max-width: 740px) {
  #topbar { row-gap: 6px; }
}

#main { display: flex; flex: 1 1 auto; min-height: 0; }

#palette {
  width: 180px;
  flex: 0 0 auto;
  background: var(--panel);
  border-right: 1px solid #0008;
  overflow-y: auto;
  padding: 10px;
}

#palette .hint {
  font-size: 11px;
  color: var(--muted);
  text-align: center;
  margin-bottom: 10px;
  line-height: 1.5;
}

.group-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
  margin: 12px 0 6px;
}

.pal-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }

.pal-item {
  background: var(--panel-2);
  border: 2px solid transparent;
  border-radius: 10px;
  padding: 7px 2px 5px;
  text-align: center;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  transition: transform .08s, border-color .15s;
}
.pal-item:hover { transform: scale(1.06); }
.pal-item:active { cursor: grabbing; }
.pal-item.rigged { border-color: #55b545; background: rgba(95, 191, 78, 0.14); }
.pal-item.proc { border-color: #d95848; background: rgba(217, 88, 72, 0.14); }
.pal-item.selected { border-color: var(--accent); background: #415070; }
.pal-item .em { font-size: 26px; line-height: 1.15; display: block; }
.pal-item .nm { font-size: 10px; color: var(--muted); display: block; margin-top: 2px; }

#stage { position: relative; flex: 1 1 auto; min-width: 0; }

#world { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

#toast {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  background: #000c;
  color: #fff;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s;
}
#toast.show { opacity: 1; }

/* 🦸⏱️ Ada's time freeze: a cyan flash that settles into a cool, slightly
   desaturating wash over the stage. Pure compositing — no per-frame JS. */
#freeze-tint {
  position: absolute;
  inset: 0;
  pointer-events: none; /* no z-index: DOM order keeps it under the toast */
  background: rgba(140, 210, 255, 0.10);
  box-shadow: inset 0 0 18vmax rgba(70, 180, 255, 0.45); /* cold vignette */
  backdrop-filter: saturate(0.6);
  -webkit-backdrop-filter: saturate(0.6);
  opacity: 0;
  transition: opacity .3s ease;
}
#freeze-tint.on { opacity: 1; animation: freeze-flash .45s ease-out; }
@keyframes freeze-flash { /* the moment time stops: a bright cyan blink */
  0% { background: rgba(190, 235, 255, 0.55); }
  100% { background: rgba(140, 210, 255, 0.10); }
}

/* ---------- 🎯 scenario mode ---------- */
#scenario-hud {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: #000c;
  color: #fff;
  padding: 7px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  max-width: calc(100% - 24px);
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
  z-index: 5;
}

#scenario-overlay,
#save-overlay {
  position: fixed;
  inset: 0;
  z-index: 30; /* above the phone palette drawer (z-index 20) */
  background: #000a;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
#scenario-overlay[hidden],
#save-overlay[hidden] { display: none; }

#scenario-panel,
#save-panel {
  background: var(--panel);
  border: 1px solid #0008;
  border-radius: 16px;
  padding: 18px;
  width: min(560px, 100%);
  max-height: min(80vh, 640px);
  overflow-y: auto;
  box-shadow: 0 12px 40px #000a;
}
#scenario-panel h2,
#save-panel h2 { font-size: 18px; margin-bottom: 4px; }
.sc-sub { color: var(--muted); font-size: 12px; margin-bottom: 14px; }

#scenario-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.sc-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: var(--panel-2);
  border: 2px solid #0006;
  border-radius: 14px;
  padding: 14px 10px;
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  text-align: center;
  transition: transform .08s, border-color .15s;
}
.sc-card:hover { transform: scale(1.03); border-color: var(--accent); }
.sc-em { font-size: 34px; line-height: 1.1; }
.sc-name { font-size: 14px; font-weight: 700; }
.sc-goal { font-size: 11px; color: var(--muted); line-height: 1.35; }

/* phones: mission cards stack in one scrollable column */
@media (max-width: 540px) {
  #scenario-cards { grid-template-columns: 1fr; }
}

/* ---------- 💾 save & share ---------- */
.slot-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-2);
  border: 1px solid #0006;
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.slot-info {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.slot-info.slot-empty { color: var(--muted); }

.big-btn {
  background: var(--accent);
  color: #26200f;
  border: 1px solid #0006;
  border-radius: 10px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  padding: 8px 14px;
  min-height: 38px;
  cursor: pointer;
  transition: transform .08s;
}
.big-btn:hover { transform: scale(1.04); }
.big-btn.alt { background: var(--panel); color: var(--text); }
.big-btn:disabled { opacity: .35; cursor: default; transform: none; }
.big-btn.wide { width: 100%; margin: 4px 0 6px; }

#save-panel .group-title { margin-top: 14px; }
#save-panel textarea {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid #0006;
  border-radius: 10px;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 11px;
  padding: 8px;
  resize: none;
  margin: 2px 0 6px;
}

/* ---------- touch: chunky targets for little fingers ---------- */
@media (pointer: coarse) {
  .ctrl { min-width: 44px; min-height: 44px; padding: 8px 12px; font-size: 17px; }
  .big-btn { min-height: 44px; font-size: 14px; }
  .chip { min-height: 44px; padding: 8px 14px; }
  /* this block sits after the width queries, so without this the 44px targets
     silently undo their label-hiding and the bar swells past the screen */
  @media (max-width: 1024px) { .chip:not(.active) .chip-label { display: none; } }
  #palette { width: 220px; }
  .pal-item { padding: 12px 4px 10px; min-height: 44px; }
  .pal-item .em { font-size: 30px; }
  .pal-item .nm { font-size: 11px; }
}

/* Phones AND tablets: the palette becomes a slide-in drawer with a 🐾 toggle,
   so the world gets the full width and the sidebar can be put away. A tablet
   used to be stuck with the desktop layout — a fixed sidebar eating a third of
   a 1024px screen, and no way to hide it. The coarse-pointer arm carries it up
   to 1280 so a big tablet gets the drawer as well, while a 1280 laptop with a
   mouse keeps the sidebar it has room for. */
@media (max-width: 1024px), (pointer: coarse) and (max-width: 1280px) {
  #btn-palette { display: block; }
  #main { position: relative; }
  #palette {
    position: absolute;
    left: 0; top: 0; bottom: 0;
    z-index: 20;
    transform: translateX(-105%);
    transition: transform .22s ease;
    box-shadow: 4px 0 18px #0007;
  }
  #palette.open { transform: translateX(0); }
}

/* 💾 save library: a named list that scrolls once it gets long */
.save-new { display: flex; gap: 8px; margin-bottom: 10px; }
.save-new input {
  flex: 1; min-width: 0; padding: 10px 12px; font: inherit; font-size: 15px;
  border-radius: 10px; border: 2px solid #3a4358; background: #1b2030; color: #e8eef7;
}
.save-new input:focus { outline: none; border-color: #6cd35e; }
#save-slots { max-height: 46vh; overflow-y: auto; }
.slot-name { font-weight: 700; }
.slot-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.icon-btn {
  flex: 0 0 auto; padding: 8px 10px; font-size: 16px; line-height: 1;
  border-radius: 10px; border: 2px solid #3a4358; background: #232a3b; color: #e8eef7; cursor: pointer;
}
.icon-btn:hover { border-color: #6cd35e; }
.icon-btn.danger { border-color: #ef645a; color: #ef645a; font-size: 13px; font-weight: 700; }

/* 🕹️ First-person thumbstick. Bottom-left, thumb-sized, and only on screen
   while you are riding a creature — the keyboard drives the same two axes, so
   this is the touch half of the same control, not a second control. */
#fpv-stick {
  position: absolute;
  left: 22px;
  bottom: 22px;
  width: 132px;
  height: 132px;
  border-radius: 50%;
  background: #0006;
  border: 2px solid #fff6;
  touch-action: none;          /* the browser must not steal the drag to scroll */
  z-index: 6;
}
#fpv-stick[hidden] { display: none; }
#fpv-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 56px;
  height: 56px;
  margin: -28px 0 0 -28px;
  border-radius: 50%;
  background: #ffffffcc;
  box-shadow: 0 2px 8px #0007;
  pointer-events: none;        /* the pad handles every pointer, knob included */
}
@media (max-width: 720px) {    /* phone: a little bigger, and clear of the toast */
  #fpv-stick { width: 148px; height: 148px; left: 16px; bottom: 64px; }
}

/* ⚔️⚡ First-person action buttons: bottom-right, opposite the thumbstick, so
   a two-thumbed grip reaches both. Only the buttons a creature actually has
   are shown — a sheep has no attack, a wolf has no special. */
#fpv-actions {
  position: absolute;
  right: 18px;
  bottom: 24px;
  display: flex;
  gap: 12px;
  align-items: flex-end;
  z-index: 6;
}
#fpv-actions[hidden] { display: none; }
.fpv-btn {
  width: 66px;
  height: 66px;
  border-radius: 50%;
  border: 2px solid #fff6;
  background: #0007;
  color: #fff;
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  touch-action: none;
  transition: transform .08s, opacity .15s;
}
.fpv-btn:active { transform: scale(0.92); }
.fpv-btn[hidden] { display: none; }
.fpv-btn.cool { opacity: 0.35; }   /* cooling down — pressing it does nothing yet */
@media (max-width: 720px) {
  #fpv-actions { right: 14px; bottom: 64px; gap: 14px; }
  .fpv-btn { width: 74px; height: 74px; font-size: 30px; }
}
