@import url("../theme.css");
@import url("../chrome.css");

:root {
  --topbar: 56px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font-family: "Comic Sans MS", "Trebuchet MS", system-ui, sans-serif;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

/* ---------- Top bar (header chrome is shared in ../chrome.css) ---------- */
/* Keyboard focus visibility (doesn't show on mouse/touch). */
.choice:focus-visible,
.dpad button:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 2px;
}

/* ---------- Play area ---------- */
.stage {
  flex: 1 1 auto;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  min-height: 0;
  touch-action: none; /* quadrant taps shouldn't scroll/zoom the page */
}

/* Size is set by render.js (fitGrid) so cells are always exact squares. */
.grid-wrap {
  position: relative;
}
.grid {
  display: grid;
  /* minmax(0,1fr), not 1fr: a bare 1fr is minmax(auto,1fr), so a cell holding a
     big emoji would force its row/column wider than the empty ones. */
  grid-template-columns: repeat(var(--cols, 9), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows, 7), minmax(0, 1fr));
  gap: 0;
  width: 100%;
  height: 100%;
}
.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* GOAL: pulsing gold ring + bigger emoji so the finish is unmistakable. */
.cell.goal-cell {
  box-shadow: 0 0 0 4px #ffd400 inset;
  animation: goalPulse 1.1s ease-in-out infinite;
}
.cell .goal {
  font-size: calc(var(--cell-size, 32px) * 0.82);
  line-height: 1;
  z-index: 1;
}
@keyframes goalPulse {
  0%, 100% { box-shadow: 0 0 0 3px #ffd400 inset; }
  50%      { box-shadow: 0 0 0 6px #ff9d00 inset; }
}

/* START: dashed white ring marking where the mover begins. */
.cell.start .start-mark {
  position: absolute;
  inset: 14%;
  border: 3px dashed #ffffffcc;
  border-radius: 50%;
}

/* OBSTACLE: hazard emoji on the road. */
.cell .obstacle { font-size: calc(var(--cell-size, 32px) * 0.82); line-height: 1; }

/* Mover overlay floats above the grid and slides between cells. */
.mover {
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  will-change: transform;
  z-index: 2;
}
/* Uploaded photo as a circular token. */
.mover.photo-mover {
  background-size: cover;
  background-position: center;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px #0007;
}
.mover.bump { animation: bump .18s ease; }
@keyframes bump {
  0%, 100% { filter: none; }
  50% { filter: brightness(1.8) drop-shadow(0 0 6px #ff5); }
}
.mover.crash { animation: crash .35s ease; }
@keyframes crash {
  0%, 100% { filter: none; }
  30% { filter: brightness(1.6) drop-shadow(0 0 8px #f33) saturate(2); }
  60% { filter: brightness(1.2) drop-shadow(0 0 5px #f33); }
}

/* ---------- D-pad ---------- */
.dpad {
  position: fixed;
  left: 16px;
  bottom: 16px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 8px;
  z-index: 5;
}
.dpad button {
  width: clamp(56px, 13vw, 88px);
  height: clamp(56px, 13vw, 88px);
  font-size: clamp(28px, 7vw, 44px);
  border: none;
  border-radius: 16px;
  background: var(--card);
  color: var(--primary);
  box-shadow: 0 4px 0 var(--shadow);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.dpad button:active { transform: translateY(3px); box-shadow: 0 1px 0 #0003; }
.dpad .up    { grid-column: 2; grid-row: 1; }
.dpad .left  { grid-column: 1; grid-row: 2; }
.dpad .down  { grid-column: 2; grid-row: 2; }
.dpad .right { grid-column: 3; grid-row: 2; }

/* ---------- Overlay screens (start / win / error) ---------- */
.screen {
  position: fixed;
  inset: 0;
  background: #0008;
  display: none;
  padding: 16px;
  z-index: 10;
  overflow: auto;
}
.screen.show { display: flex; }
/* margin:auto centers the card when it fits, but lets it scroll from the top
   when it is taller than the screen (plain align/justify center would clip it). */
.screen-card {
  position: relative;
  margin: auto;
  background: #fff;
  border-radius: 28px;
  padding: 24px 28px;
  text-align: center;
  box-shadow: 0 10px 0 #0004;
  max-width: 560px;
  width: 100%;
  animation: pop .3s ease;
}
/* Start modal: fixed height, only the theme list scrolls, Play stays visible. */
.start-card {
  position: relative;
  display: flex;
  flex-direction: column;
  max-height: 90vh;
}
.screen-close {
  position: absolute;
  top: 8px;
  right: 12px;
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  font-weight: bold;
  color: var(--ink-soft);
  text-decoration: none;
  border-radius: 50%;
}
.screen-close:active { background: rgba(0,0,0,.08); }
.theme-choices.scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  align-content: start;
  padding: 4px;
}
.start-card .play { flex: 0 0 auto; }

.screen-title { color: var(--primary); margin: 0 0 8px; font-size: clamp(28px, 7vw, 46px); }
.screen-card h2 { color: var(--primary); margin: 8px 0 16px; font-size: clamp(24px, 6vw, 40px); }
.screen-label { color: var(--ink-soft); margin: 14px 0 8px; font-weight: bold; }
.big-emoji { font-size: clamp(60px, 16vw, 120px); }
.actions { display: flex; gap: 16px; justify-content: center; flex-wrap: wrap; margin-top: 8px; }
@keyframes pop {
  from { transform: scale(.6); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}

/* Choice buttons (difficulty + theme) */
.choices {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
}
/* Themes: compact grid so many fit without a tall modal. */
.theme-choices {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 10px;
}
.theme-choices .choice { min-width: 0; padding: 10px 8px; }
.choice {
  font-family: inherit;
  cursor: pointer;
  border: 3px solid transparent;
  border-radius: 18px;
  background: #FBEFE2;
  color: var(--ink);
  padding: 12px 16px;
  min-width: 92px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
}
.choice.selected {
  border-color: var(--accent);
  background: #E6F5F3;
  box-shadow: 0 4px 0 var(--shadow);
}
.choice .choice-main { font-size: 20px; font-weight: bold; }
.choice .choice-icons { font-size: 30px; display: flex; align-items: center; gap: 4px; }
.choice .choice-sub { font-size: 13px; color: var(--ink-soft); }
.play { margin-top: 22px; }

/* "My photo" theme card */
.photo-thumb {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--accent);
  vertical-align: middle;
}
.mini-btn {
  display: inline-block;
  margin-top: 4px;
  font-size: 12px;
  font-weight: bold;
  color: var(--accent-dark);
  background: #fff;
  border: 1px solid var(--accent);
  border-radius: 10px;
  padding: 3px 8px;
}
.mini-btn.remove { color: #c0392b; border-color: #c0392b; }

/* ---------- Touch / mobile ---------- */
/* On touch devices the whole play area is the controller, so drop the D-pad
   and give the maze more room. */
body.touch-mode .dpad { display: none; }
body.touch-mode .stage { padding: 6px; }

/* Touch-zone guide: faint wedges + an arrow per side so it is clear where to
   press. Only shown on touch; pointer-events:none so presses reach the stage. */
.controls {
  display: none;
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}
body.touch-mode .controls { display: block; }
/* Four faint wedges split on the diagonals, matching the tap regions. */
.controls::before {
  content: "";
  position: absolute;
  inset: 0;
  background: conic-gradient(from 45deg,
    rgba(255, 255, 255, 0.10) 0deg 90deg,
    rgba(0, 0, 0, 0.10) 90deg 180deg,
    rgba(255, 255, 255, 0.10) 180deg 270deg,
    rgba(0, 0, 0, 0.10) 270deg 360deg);
}
.controls .ctrl {
  position: absolute;
  transform: translate(-50%, -50%);
  font-size: clamp(30px, 8vw, 60px);
  color: #fff;
  opacity: 0.5;
  text-shadow: 0 2px 5px #0007;
  transition: opacity .08s, transform .08s;
}
.controls .up    { top: 9%;  left: 50%; }
.controls .down  { top: 91%; left: 50%; }
.controls .left  { top: 50%; left: 7%; }
.controls .right { top: 50%; left: 93%; }
.controls .ctrl.active {
  opacity: 1;
  color: #ffd400;
  transform: translate(-50%, -50%) scale(1.5);
}

/* Error screen */
.screen-card.error h2 { color: #c0392b; }
#errorDetails {
  width: 100%;
  height: 160px;
  font-family: monospace;
  font-size: 12px;
  border: 2px solid #ddd;
  border-radius: 12px;
  padding: 10px;
  resize: vertical;
  margin-bottom: 12px;
}
