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

html, body { height: 100%; }

body {
  background: #0e0a07 radial-gradient(1200px 700px at 50% 40%, #1d1712, #0e0a07);
  color: #e8dcc4;
  font-family: 'Trebuchet MS', Verdana, sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
}

#wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 10px;
}

/* ---------- HUD ---------- */
#hud {
  display: flex;
  gap: 34px;
  background: linear-gradient(#3a2c1c, #2a1f14);
  border: 1px solid #57432b;
  border-radius: 8px;
  padding: 8px 26px;
  font-size: 15px;
  letter-spacing: 1.5px;
  color: #c9b892;
}
#hud.hidden { visibility: hidden; }
.hud-item.hidden { display: none; } /* toggled MISSION <-> ITEMS by mode */
.hud-item b { color: #ffd98a; margin-left: 4px; }
.life {
  display: inline-block;
  width: 13px; height: 13px;
  background: #4a7de0;
  border-radius: 4px;
  margin-left: 5px;
  vertical-align: -1px;
  box-shadow: 0 0 5px #4a7de088;
}

/* ---------- Stage ---------- */
#row { display: flex; align-items: center; gap: 18px; }
#stage { position: relative; line-height: 0; }

/* ---------- Joystick (mobile only) ---------- */
#dpad {
  /* knob offset, driven by Input.initPad() in game.js */
  --kx: 0px;
  --ky: 0px;
  --pad-size: 132px; /* base diameter; the mobile media query below scales this to the viewport */
  display: none; /* hidden on desktop; shown for coarse pointers below */
  position: relative;
  width: var(--pad-size);
  height: var(--pad-size);
  border-radius: 50%;
  /* dark wood base dish: lit from above, falling into shadow at the rim */
  background:
    radial-gradient(circle at 50% 34%, #3a2c1c 0%, #2a1f14 42%, #241a10 68%, #1a1209 100%);
  border: 2px solid #57432b;
  box-shadow:
    inset 0 10px 18px rgba(0, 0, 0, 0.65),      /* dish depth */
    inset 0 -3px 6px rgba(255, 217, 138, 0.08), /* lower inner bounce light */
    0 4px 0 #6b4a22,                            /* toy-plastic ledge */
    0 10px 26px rgba(0, 0, 0, 0.6);
  touch-action: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
/* subtle etched ring just inside the rim, like the reference template */
#dpad::before {
  content: '';
  position: absolute;
  inset: calc(var(--pad-size) * 0.05);
  border-radius: 50%;
  border: 1px solid rgba(255, 217, 138, 0.14);
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.4);
  pointer-events: none;
}

/* the whole pad is one analog touch surface; arrows + knob only indicate */
#dpad .pad,
#dpad #stick-knob { pointer-events: none; }

/* ---------- Direction arrows (CSS triangles, cream -> golden glow) ---------- */
.pad-arrow {
  position: absolute;
  width: 0;
  height: 0;
  border-style: solid;
  opacity: 0.8;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.7));
  transition: filter 0.12s ease, opacity 0.12s ease;
}
.pad-arrow[data-dir="up"] {
  top: calc(var(--pad-size) * 0.081);
  left: 50%;
  transform: translateX(-50%);
  border-width: 0 calc(var(--pad-size) * 0.05) calc(var(--pad-size) * 0.069) calc(var(--pad-size) * 0.05);
  border-color: transparent transparent #c9b892 transparent;
}
.pad-arrow[data-dir="down"] {
  bottom: calc(var(--pad-size) * 0.081);
  left: 50%;
  transform: translateX(-50%);
  border-width: calc(var(--pad-size) * 0.069) calc(var(--pad-size) * 0.05) 0 calc(var(--pad-size) * 0.05);
  border-color: #c9b892 transparent transparent transparent;
}
.pad-arrow[data-dir="left"] {
  left: calc(var(--pad-size) * 0.081);
  top: 50%;
  transform: translateY(-50%);
  border-width: calc(var(--pad-size) * 0.05) calc(var(--pad-size) * 0.069) calc(var(--pad-size) * 0.05) 0;
  border-color: transparent #c9b892 transparent transparent;
}
.pad-arrow[data-dir="right"] {
  right: calc(var(--pad-size) * 0.081);
  top: 50%;
  transform: translateY(-50%);
  border-width: calc(var(--pad-size) * 0.05) 0 calc(var(--pad-size) * 0.05) calc(var(--pad-size) * 0.069);
  border-color: transparent transparent transparent #c9b892;
}
/* lit state: golden glow while steering that way */
.pad-arrow.held {
  opacity: 1;
  filter: drop-shadow(0 0 4px #ffd98a) drop-shadow(0 0 9px #ffd98a);
}
.pad-arrow.held[data-dir="up"] { border-bottom-color: #ffd98a; }
.pad-arrow.held[data-dir="down"] { border-top-color: #ffd98a; }
.pad-arrow.held[data-dir="left"] { border-right-color: #ffd98a; }
.pad-arrow.held[data-dir="right"] { border-left-color: #ffd98a; }

/* ---------- Thumbstick knob (glossy ivory, physically raised) ---------- */
#stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: calc(var(--pad-size) * 0.42);
  height: calc(var(--pad-size) * 0.42);
  margin: calc(var(--pad-size) * -0.21) 0 0 calc(var(--pad-size) * -0.21); /* center on the base */
  border-radius: 50%;
  /* ivory dome with a specular hot-spot up-left, warm shading below */
  background:
    radial-gradient(circle at 34% 28%, #fffef8 0%, #f2ead6 32%, #e8dcc4 55%, #c9b892 82%, #a9946c 100%);
  border: 1px solid #8a6a3f;
  box-shadow:
    inset 0 -6px 10px rgba(107, 74, 34, 0.45),  /* under-curve shading */
    inset 0 3px 5px rgba(255, 255, 255, 0.85),  /* top rim gleam */
    0 5px 0 #6b4a22,                            /* toy ledge, matches buttons */
    0 9px 16px rgba(0, 0, 0, 0.6);
  transform: translate(var(--kx), var(--ky));
  transition: transform 0.16s cubic-bezier(0.22, 1.4, 0.36, 1); /* springy snap-back */
  will-change: transform;
}
/* extra specular highlight blob, like the template's gloss */
#stick-knob::after {
  content: '';
  position: absolute;
  left: 18%;
  top: 10%;
  width: 42%;
  height: 30%;
  border-radius: 50%;
  background: radial-gradient(ellipse at center, rgba(255, 255, 255, 0.95), rgba(255, 255, 255, 0) 70%);
}
/* while dragging: knob follows the finger with no easing lag */
#dpad.dragging #stick-knob { transition: none; }
#dpad.dragging { opacity: 1; }

/* ---------- Mobile pause button (touch only) ---------- */
#btn-pause-mobile {
  display: none; /* desktop uses Esc/P; shown for coarse pointers below */
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 35;
  width: 52px;
  height: 52px;
  padding: 0;
  margin: 0;
  gap: 5px;
  border-radius: 50%;
  border: 2px solid #8a6a3f;
  background: linear-gradient(#c89a58, #a87b3e);
  box-shadow: 0 3px 0 #6b4a22, 0 5px 12px rgba(0, 0, 0, 0.5);
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  opacity: 0.9;
}
#btn-pause-mobile span {
  display: block;
  width: 6px;
  height: 20px;
  border-radius: 2px;
  background: #2a1c0c;
}
#btn-pause-mobile:active { transform: translateY(2px); box-shadow: 0 1px 0 #6b4a22; }

/* ---------- Mobile-only visibility ---------- */
@media (pointer: coarse) {
  #row { gap: 0; }
  #dpad {
    /* scale with the shorter viewport side so the stick always fits inside
       that gutter instead of spilling past the canvas edge */
    --pad-size: clamp(72px, 24vmin, 124px);
    display: block;
    position: fixed;
    left: 10px;
    bottom: 10px;
    z-index: 30; /* above canvas + overlays' stacking context */
    opacity: 0.9;
  }
  #btn-pause-mobile { display: flex; }        /* shown during play (JS toggles .hidden) */
  #btn-pause-mobile.hidden { display: none; }
}
/* mobile landscape: keep the play area a bit smaller so the on-screen joystick
   has room beside/below it (all other devices fill the screen) */
@media (pointer: coarse) and (orientation: landscape) {
  canvas { height: min(72vh, calc(84vw / 1.5814)); }
}

/* ---------- One-time touch controls toast ---------- */
#mobile-toast {
  position: fixed;
  top: 14px;
  left: 50%;
  transform: translate(-50%, -14px);
  z-index: 40;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 26px;
  border-radius: 999px; /* pill */
  background: linear-gradient(#3a2c1c, #241a10);
  border: 2px solid #6b532f;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.7);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease, transform 0.35s ease;
}
#mobile-toast b {
  color: #ffd98a;
  font-size: 15px;
  letter-spacing: 2px;
}
#mobile-toast span {
  color: #c9b892;
  font-size: 13px;
  letter-spacing: 1px;
}
#mobile-toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

canvas {
  display: block;
  /* scale to fill the viewport (contain-fit, aspect 1088/688 = 1.5814),
     leaving a little room below for the HUD — no longer capped at native res */
  width: auto;
  height: min(84vh, calc(97vw / 1.5814));
  border-radius: 6px;
  box-shadow: 0 12px 44px rgba(0, 0, 0, 0.65);
  touch-action: none;
}
#stage.playing canvas { cursor: none; }

/* ---------- Overlays ---------- */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: rgba(12, 8, 5, 0.74);
  border-radius: 6px;
  text-align: center;
  line-height: 1.5;
}
.overlay.hidden { display: none; }

.overlay h1 {
  font-size: 62px;
  letter-spacing: 6px;
  color: #ffd98a;
  text-shadow: 0 4px 0 #6b4a22, 0 10px 28px #000;
}
.overlay h1 span { color: #7da3ef; text-shadow: 0 4px 0 #2f4577, 0 10px 28px #000; }

.overlay h2 {
  font-size: 40px;
  letter-spacing: 4px;
  color: #ffd98a;
  text-shadow: 0 3px 0 #6b4a22, 0 8px 20px #000;
}

.tag { font-size: 17px; color: #b8a683; letter-spacing: 2px; margin-top: -8px; }
.hint { font-size: 14px; color: #a08d68; max-width: 420px; }

.controls {
  display: grid;
  grid-template-columns: auto auto;
  gap: 10px 36px;
  font-size: 16px;
  color: #d8c9a8;
  text-align: left;
}
kbd {
  background: #2e2418;
  border: 1px solid #5a462c;
  border-radius: 6px;
  padding: 2px 9px;
  color: #ffd98a;
  font-family: inherit;
  font-size: 14px;
}

button {
  font: inherit;
  font-size: 15px;
  font-weight: bold;
  letter-spacing: 2px;
  padding: 9px 28px;
  border-radius: 8px;
  border: 2px solid #8a6a3f;
  background: linear-gradient(#c89a58, #a87b3e);
  color: #2a1c0c;
  cursor: pointer;
  box-shadow: 0 3px 0 #6b4a22, 0 6px 14px rgba(0, 0, 0, 0.5);
  margin-top: 8px;
}
button:hover { filter: brightness(1.1); }
button:active { transform: translateY(2px); box-shadow: 0 1px 0 #6b4a22; }

@media (pointer: coarse) {
  button {
    font-size: 11px;
    padding: 6px 16px;
    letter-spacing: 1px;
    margin-top: 6px;
    border-radius: 7px;
    box-shadow: 0 2px 0 #6b4a22, 0 5px 10px rgba(0, 0, 0, 0.5);
  }
  button:active { transform: translateY(1px); box-shadow: 0 1px 0 #6b4a22; }
  .menu-buttons { gap: 8px; }
  .lvl-btn { font-size: 12px; padding: 6px 0; }
  /* the title card is short on portrait phones (canvas is width-capped, so
     it renders very shallow) — trim the hint text and spacing so the whole
     button cluster fits inside it instead of spilling past the frame */
  .hint { font-size: 11px; line-height: 1.35; }
  kbd { font-size: 11px; padding: 1px 6px; }
  #ov-start.title-screen { padding-bottom: 14px; gap: 8px; }
}

/* ---------- Title screen ---------- */
#ov-start.title-screen {
  background:
    linear-gradient(rgba(12, 8, 5, 0) 55%, rgba(12, 8, 5, 0.78) 88%),
    url("Media/Title%20Card/TableTop%20Tanks.webp") center / cover no-repeat;
  justify-content: flex-end;
  padding-bottom: 26px;
  gap: 12px;
}
.title-spacer { flex: 1; }
.title-hint { text-shadow: 0 2px 6px #000; }

.menu-buttons {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}
.btn-secondary {
  background: linear-gradient(#4a3a24, #352816);
  color: #e8dcc4;
  border-color: #6b532f;
}

/* ---------- Level select ---------- */
/* Scrollable so the grid + buttons are always reachable, even on short (mobile
   landscape) viewports where the whole list can't fit at once. */
#ov-levels {
  background: rgba(12, 8, 5, 0.88);
  justify-content: safe center; /* centers when it fits, else pins to top so nothing clips */
  overflow-y: auto;
  padding: 14px 10px;
  gap: 12px;
}
#level-grid {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 8px;
  max-width: 640px;
  width: 90%;
  flex: none;
}
.lvl-btn {
  font-size: 14px;
  letter-spacing: 0;
  padding: 8px 0;
  border-radius: 8px;
  box-shadow: 0 3px 0 #6b4a22, 0 5px 10px rgba(0, 0, 0, 0.45);
  margin-top: 0;
}
@media (max-width: 720px) {
  #level-grid { grid-template-columns: repeat(5, 1fr); max-width: 340px; }
  .lvl-btn { padding: 6px 0; font-size: 12px; }
}
/* short screens (mostly mobile landscape): compact the heading + grid so the
   whole list fits, and let more columns keep it shallow */
@media (max-height: 480px) {
  #ov-levels { padding: 8px; gap: 8px; }
  #ov-levels h2 { font-size: 22px; }
  #ov-levels .tag { font-size: 12px; margin-top: -4px; }
  #level-grid { grid-template-columns: repeat(10, 1fr); max-width: 560px; gap: 6px; }
  .lvl-btn { padding: 4px 0; font-size: 11px; }
}
/* Portrait: the game board (and this overlay, which is pinned to it) is short, so
   break the menu out to the FULL viewport height — the whole list fits with room
   to spare, no scrolling inside the little board. */
@media (orientation: portrait) {
  #ov-levels {
    position: fixed;
    inset: 0;
    z-index: 45;                 /* above the joystick + toast */
    background: rgba(10, 7, 4, 0.97);
    border-radius: 0;
    padding: 20px 12px;
  }
  #level-grid { max-width: 460px; gap: 10px; }
  .lvl-btn { padding: 10px 0; font-size: 15px; }
}

/* Mission intro card */
#ov-card { background: rgba(12, 8, 5, 0.3); pointer-events: none; }
#ov-card button { pointer-events: auto; } /* BEGIN stays clickable through the card overlay */
.card-box {
  background: linear-gradient(#3a2c1c, #241a10);
  border: 2px solid #6b532f;
  border-radius: 12px;
  padding: 16px 40px 18px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.7);
}
.card-box h2 { font-size: 28px; }
.card-box p { font-size: 14px; color: #c9b892; letter-spacing: 2px; margin-top: 5px; }
#btn-begin { font-size: 15px; padding: 8px 30px; margin-top: 14px; }
#btn-begin.hidden { display: none; } /* survival/sandbox: no BEGIN button on the card */

/* ---------- Level editor (Create Your Own) ---------- */
/* Sits in flow BELOW the canvas so it never covers the bottom row of tiles. */
#ov-editor {
  box-sizing: border-box;
  width: 100%;
  margin-top: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  background: rgba(30, 26, 22, 0.96);
  border: 2px solid #5a462c;
  border-radius: 10px;
  line-height: normal;
}
#ov-editor.hidden { display: none; }
.ed-tools {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow-x: auto;
  flex: 1;
  min-width: 0;
  padding-bottom: 2px;
}
.ed-tools::-webkit-scrollbar { height: 5px; }
.ed-tools::-webkit-scrollbar-thumb { background: #5a462c; border-radius: 3px; }
.ed-group { display: flex; gap: 6px; flex: none; }
.ed-sep { width: 1px; height: 30px; background: #4a443d; flex: none; }
.ed-btn {
  all: unset;
  font: inherit;
  box-sizing: border-box;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: #3d3833;
  border: 2px solid transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: none;
}
.ed-btn:hover { background: #4a443d; }
.ed-btn.sel { border-color: #6f9bff; background: #454b57; }
.ed-btn.ed-enemy { width: 34px; height: 34px; }
.sw { display: block; border-radius: 4px; }
.sw-wall { width: 20px; height: 20px; background: #c9a86a; border: 1px solid #b3945a; }
.sw-crate { width: 20px; height: 20px; background: #d9a961; border: 2px solid #8a6034; }
.sw-laser { width: 5px; height: 24px; background: #ff5a4d; border-radius: 3px; box-shadow: 0 0 5px #ff5a4d; }
.sw-tank { width: 22px; height: 17px; border: 2px solid #2b2b2b; border-radius: 3px; }
.sw-player { background: #4a7de0; border-color: #2f56a8; }
.ed-ico { color: #e7d9bb; font-size: 12px; font-weight: bold; letter-spacing: 1px; }
.ed-actions { display: flex; gap: 6px; flex: none; }
.ed-act {
  all: unset;
  font: inherit;
  box-sizing: border-box;
  height: 40px;
  padding: 0 13px;
  border-radius: 8px;
  background: #4a3a24;
  color: #e8dcc4;
  border: 2px solid #6b532f;
  font-size: 13px;
  font-weight: bold;
  letter-spacing: 1px;
  display: flex;
  align-items: center;
  cursor: pointer;
}
.ed-act:hover { filter: brightness(1.12); }
.ed-act:active { transform: translateY(1px); }
.ed-play { background: linear-gradient(#3aba63, #2a9350); border-color: #257c41; color: #05230f; }
.ed-clear { background: linear-gradient(#a13e3e, #7a2f2f); border-color: #5a2222; color: #f3d9d9; }

@media (pointer: coarse) {
  #ov-editor { padding: 5px 6px; gap: 6px; }
  .ed-btn { width: 34px; height: 34px; }
  .ed-btn.ed-enemy { width: 30px; height: 30px; }
  .sw-wall, .sw-crate { width: 17px; height: 17px; }
  .sw-tank { width: 19px; height: 15px; }
  .ed-sep { height: 26px; }
  .ed-act { height: 34px; padding: 0 9px; font-size: 11px; }
}

/* Editor layout: dock the tool panel into the empty space around the map so it
   reads cleanly and nothing is cropped. Landscape -> panel beside the map (right
   gutter); portrait -> panel below the map. Tools always WRAP (no scroll, no
   cut-off). HUD reserved space + joystick are hidden while building. */
body.editing #hud { display: none; }
@media (pointer: coarse) { body.editing #dpad { display: none; } }

body.editing #stage {
  display: flex;
  gap: 12px;
  min-width: 0;
  max-width: calc(100vw - 20px);
}
body.editing #ov-editor { margin: 0; }
body.editing .ed-tools { overflow: visible; flex-wrap: wrap; } /* everything visible, never scrolled */
body.editing .ed-group { display: contents; } /* let individual tool buttons wrap instead of overflowing as one block */

/* --- Landscape: big map on the left, tool panel fills the right gutter --- */
@media (orientation: landscape) {
  body.editing #stage { flex-direction: row; align-items: stretch; max-height: calc(100vh - 16px); }
  body.editing canvas { height: min(92vh, calc((100vw - 252px) / 1.5814)); width: auto; margin: 0; }
  body.editing #ov-editor {
    flex-direction: column;
    width: 236px;
    align-items: stretch;
    gap: 10px;
    overflow-y: auto;
  }
  body.editing .ed-tools { flex: 1 1 auto; align-content: flex-start; justify-content: center; gap: 8px; }
  body.editing .ed-sep { width: 100%; height: 1px; }              /* group dividers become horizontal rules */
  body.editing .ed-actions { flex-direction: column; gap: 8px; }
  body.editing .ed-act { justify-content: center; height: 42px; }
}

/* --- Portrait: map on top, roomy tool panel spread below --- */
@media (orientation: portrait) {
  body.editing #stage { flex-direction: column; align-items: center; }
  body.editing canvas { height: min(64vh, calc(96vw / 1.5814)); width: auto; margin: 0 auto; }
  body.editing #ov-editor { width: 100%; flex-wrap: wrap; justify-content: center; gap: 10px; padding: 12px; }
  body.editing .ed-tools { flex: 0 1 auto; justify-content: center; gap: 8px; }
}
