/* Next Level Snake cabinet — shared by Rails and the browser preview. */

:root {
  --frame-pad: 22px;
  --frame-radius: 30px;
  --field-radius: 8px;
  --green: #2ce05e;
  --red: #ff3b30;
  --field: #050505;
  --bezel-hi: #9aa1a9;
  --bezel-mid: #79808a;
  --bezel-lo: #666d76;
  --bezel-texture: 0.3;
  --bezel-radius: var(--frame-radius);
  /* Cabinet chrome lives OUTSIDE the padding box, so adding a border or a
     marquee never changes the size of the playfield. */
  --bezel-border: 0px;
  --cabinet-top: 0px;
  --cabinet-bottom: 0px;
}

.snake-root { width: 100%; }

/* Native image/selection dragging must never steal a joystick gesture. */
.snake-frame,
.snake-frame * {
  -webkit-user-drag: none;
  -webkit-user-select: none;
  user-select: none;
}

.snake-frame {
  position: relative;
  width: 100%;
  /* border + reduced padding = the same content box, so the field never moves.
     Cabinet spacing is applied by .screen as padding (see styles.css): a margin
     here would collapse out of .screen and misalign the overlay panels. */
  padding: calc(var(--frame-pad) - var(--bezel-border));
  border: var(--bezel-border) solid transparent;
  border-radius: var(--bezel-radius);
  background:
    linear-gradient(158deg, rgba(255, 255, 255, 0.24), rgba(255, 255, 255, 0) 42%),
    linear-gradient(180deg, var(--bezel-hi) 0%, var(--bezel-mid) 55%, var(--bezel-lo) 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.55),
    inset 0 -2px 0 rgba(0, 0, 0, 0.3),
    0 34px 70px -30px rgba(0, 0, 0, 0.9),
    0 2px 0 rgba(255, 255, 255, 0.05);
}

.snake-frame::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background-image: url('img/metal.jpg');
  background-size: cover;
  background-position: center;
  opacity: var(--bezel-texture);
  mix-blend-mode: overlay;
  pointer-events: none;
}

.snake-field {
  position: relative;
  width: 100%;
  aspect-ratio: 24 / 18;
  background: var(--field);
  border-radius: var(--field-radius);
  overflow: hidden;
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.06),
    inset 0 3px 34px rgba(0, 0, 0, 0.95);
}

.snake-field::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    repeating-linear-gradient(180deg, rgba(255, 255, 255, 0.035) 0 1px, transparent 1px 3px),
    radial-gradient(125% 105% at 50% 50%, transparent 52%, rgba(0, 0, 0, 0.6) 100%);
}

/* No scanlines, grain, or internal outlines on the Classic snake. */
html[data-theme='classic'] .snake-field::after { background: none; }

.snake-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: pan-y;
  outline: none;
}

.terminal-cursor {
  display: none;
  position: absolute;
  top: 14px;
  left: 18px;
  z-index: 6;
  color: #ffffff;
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-size: clamp(22px, 3vw, 30px);
  font-weight: 500;
  line-height: 1;
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.85);
  pointer-events: none;
  user-select: none;
}

.terminal-hack-code {
  display: none;
  position: absolute;
  inset: 12px 16px;
  z-index: 5;
  margin: 0;
  padding: 0;
  color: #31f084;
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-size: clamp(11px, 1.3vw, 14px);
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-all;
  overflow-y: auto;
  pointer-events: none;
  text-shadow: 0 0 8px rgba(49, 240, 132, 0.45);
}

.snake-root.is-hacking .terminal-hack-code {
  display: block;
}

.snake-root.is-hacking > .snake-frame > .snake-field > .terminal-cursor {
  display: none !important;
}

.terminal-hack-cursor {
  display: inline-block;
  color: #ffffff;
  font-weight: 700;
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.9);
  animation: terminal-cursor-blink 1.2s step-end infinite;
  margin-left: 2px;
}

.terminal-access-granted {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: rgba(0, 12, 5, 0.32);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 110ms ease, visibility 110ms ease;
}
.terminal-access-granted.is-visible {
  opacity: 1;
  visibility: visible;
  animation: terminal-access-flash 1.2s ease-out both;
}
.terminal-access-granted__text {
  color: #a6ffc2;
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-size: clamp(32px, 6vw, 64px);
  font-weight: 700;
  letter-spacing: 0.18em;
  text-align: center;
  text-shadow: 0 0 6px #2ce05e, 0 0 18px #2ce05e, 0 0 38px rgba(44, 224, 94, 0.85);
  animation: terminal-access-text-flash 160ms steps(2, end) 4;
}
.terminal-access-glasses {
  position: absolute;
  z-index: 9;
  display: none;
  align-items: center;
  justify-content: center;
  filter: drop-shadow(0 0 5px rgba(0, 0, 0, 0.85));
  pointer-events: none;
}
.terminal-access-glasses.is-visible { display: flex; }
.terminal-access-glasses svg { width: 100%; height: 100%; overflow: visible; }
@keyframes terminal-access-flash {
  0%, 100% { background: rgba(0, 12, 5, 0.12); }
  12%, 30% { background: rgba(44, 224, 94, 0.32); }
  42%, 58% { background: rgba(0, 12, 5, 0.08); }
  70% { background: rgba(44, 224, 94, 0.24); }
}
@keyframes terminal-access-text-flash {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.38; }
}

/* Visible only in Terminal, before/after a run or while it is paused. */
html[data-theme='terminal'] .snake-root:not(.is-active) .terminal-cursor {
  display: block;
  animation: terminal-cursor-blink 1.2s step-end infinite;
}

@keyframes terminal-cursor-blink {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0; }
}

/* Gesture capture applies to the whole site, but only during active play.
   A fixed body also prevents iOS overscroll. app.js preserves/restores the
   scroll offset when these classes change on start/pause/resume/end. */
html.is-playing {
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
}

body.is-playing {
  position: fixed;
  top: var(--play-scroll-top, 0px);
  left: 0;
  right: 0;
  padding-right: var(--play-scrollbar-gap, 0px);
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
}

body.is-playing .snake-root,
body.is-playing .snake-canvas { touch-action: none; }

/* ---------- Arcade theme: a cartoony cabinet around the same playfield ----------
   The marquee sits in --cabinet-top and the control deck in --cabinet-bottom,
   both outside the frame's padding box. The field keeps its exact size, so
   gameplay is identical to every other theme. */

.cabinet { display: none; pointer-events: none; }

html[data-theme='arcade'] {
  --bezel-border: 5px;
  --cabinet-top: 0px;
  --cabinet-bottom: 96px;
  --bezel-texture: 0;
  --bezel-radius: 26px;
  --bezel-hi: #ff5f72;
  --bezel-mid: #d21f3d;
  --bezel-lo: #8b1029;
}

html[data-theme='arcade'] .snake-frame {
  border-color: #22080f;
  box-shadow:
    inset 0 3px 0 rgba(255, 255, 255, 0.34),
    inset 0 -7px 0 rgba(0, 0, 0, 0.26),
    0 30px 60px -26px rgba(0, 0, 0, 0.88);
}

/* candy-striped side art */
html[data-theme='arcade'] .snake-frame::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  background: repeating-linear-gradient(
    115deg,
    rgba(255, 214, 64, 0.17) 0 14px,
    rgba(255, 214, 64, 0) 14px 36px
  );
}

html[data-theme='arcade'] .cabinet { display: block; }

html[data-theme='arcade'] .cabinet__marquee {
  position: absolute;
  left: 4%;
  right: 4%;
  bottom: calc(100% + 8px);
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  border: 5px solid #0a1f5c;
  border-radius: 20px 20px 12px 12px;
  background: linear-gradient(180deg, #5b9bff, #2563e8 58%, #1747b8);
  box-shadow:
    inset 0 3px 0 rgba(255, 255, 255, 0.55),
    0 0 34px rgba(70, 140, 255, 0.42);
  animation: marquee-glow 3.2s ease-in-out infinite;
}

.cabinet__marquee-text {
  font-family: 'Space Grotesk', 'Helvetica Neue', sans-serif;
  font-weight: 700;
  font-size: clamp(12px, 1.9vw, 20px);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: #ffe98a;
  text-shadow: 0 2px 0 #0a1f5c;
  white-space: nowrap;
}

.cabinet__bulb {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 2px solid #22080f;
  background: #fff7d4;
  box-shadow: 0 0 12px rgba(255, 240, 180, 0.9);
  animation: bulb-blink 1.1s steps(1, end) infinite;
}

.cabinet__bulb:last-of-type { animation-delay: 0.55s; }

html[data-theme='arcade'] .cabinet__deck {
  position: absolute;
  left: -2%;
  right: -2%;
  top: calc(100% + 10px);
  height: 78px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 0 24px;
  border: 5px solid #0a1f5c;
  border-radius: 14px 14px 28px 28px;
  background: linear-gradient(180deg, #3f86ff, #1d4fc4 55%, #163c96);
  box-shadow:
    inset 0 3px 0 rgba(255, 255, 255, 0.35),
    0 20px 36px -22px rgba(0, 0, 0, 0.92);
}

.cabinet__stick {
  position: relative;
  width: 28px;
  height: 54px;
  flex: none;
  cursor: grab;
  touch-action: none;
  user-select: none;
  pointer-events: auto;
}

/* The visual knob is small on phones; make near misses hit the stick too. */
.cabinet__stick::after {
  content: '';
  position: absolute;
  inset: -14px -18px -8px;
  cursor: grab;
  z-index: 2;
}

.cabinet__stick:active,
.cabinet__stick.is-grabbed {
  cursor: grabbing;
}

.cabinet__stick.is-grabbed::after { cursor: grabbing; }

.cabinet__stick::before {
  content: '';
  position: absolute;
  left: 50%;
  bottom: 4px;
  width: 9px;
  height: 34px;
  margin-left: -4.5px;
  border-radius: 5px;
  background: linear-gradient(180deg, #ccd2da, #7c848e);
  transform-origin: 50% 100%;
  transform: rotate(var(--stick-rot, 0deg));
}

.cabinet__stick i {
  position: absolute;
  left: 50%;
  top: 0;
  width: 28px;
  height: 28px;
  margin-left: -14px;
  border-radius: 50%;
  border: 3px solid #22080f;
  background: radial-gradient(circle at 32% 28%, #ff929e, #d81f3c 58%, #8b1029);
  transform-origin: 50% 100%;
  transform: translate(var(--stick-x, 0px), 0px) rotate(var(--stick-rot, 0deg));
  animation: stick-wobble 2.8s ease-in-out infinite;
  pointer-events: auto;
}

.cabinet__stick.is-grabbed i,
.cabinet__stick[style*="--stick-rot"] i {
  animation: none !important;
}

.cabinet__deck { pointer-events: auto; }
.cabinet__buttons { display: flex; gap: 12px; pointer-events: auto; }

.cabinet__buttons i {
  position: relative;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 4px solid #22080f;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.3), 0 3px 0 rgba(0, 0, 0, 0.38);
  pointer-events: auto;
  cursor: pointer;
  user-select: none;
  transition: transform 80ms ease, box-shadow 80ms ease;
}

/* Invisible hit pad: the painted button stays 32px, but the finger target is
   a full 42px so a slightly-off press still lands on the button. */
.cabinet__buttons i::after {
  content: '';
  position: absolute;
  inset: -5px;
  border-radius: 50%;
}

.cabinet__buttons i:active,
.cabinet__buttons i.is-pressed {
  transform: translateY(3px);
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.4), 0 1px 0 rgba(0, 0, 0, 0.5);
}

.cabinet__buttons i:nth-child(1) { background: #ff3b4e; }
.cabinet__buttons i:nth-child(2) { background: #ffd23f; }
.cabinet__buttons i:nth-child(3) { background: #8fc1ff; }
.cabinet__buttons i:nth-child(4) { background: #ff3b4e; }

/* Arcade Deck Cartridge Slot (Drag to pull out Snake Cartridge) */
.cabinet__slot {
  position: relative;
  width: 44px;
  height: 16px;
  flex: none;
  cursor: grab;
  touch-action: none;
  user-select: none;
  z-index: 100;
}

/* The slot is a 16px slit; pad the grab area so a thumb can actually catch it. */
.cabinet__slot::after {
  content: '';
  position: absolute;
  inset: -10px -6px;
  cursor: grab;
}

.arcade-cartridge {
  position: absolute;
  top: 0;
  left: 0;
  width: 44px;
  height: 16px;
  border-radius: 5px;
  border: 2px solid #22080f;
  background: linear-gradient(180deg, #9ba4ae 0%, #686f7b 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.45);
  cursor: grab;
  transform: translateY(var(--cart-y, 0px));
  transition: transform 220ms cubic-bezier(0.16, 1, 0.3, 1), box-shadow 150ms ease;
  transform-origin: top center;
  box-sizing: border-box;
}

.arcade-cartridge.is-dragging {
  cursor: grabbing;
  transition: none;
}

.arcade-cartridge.is-ejected {
  transform: translateY(48px);
  cursor: pointer;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.9);
}

/* The visible back edge of the cartridge when inserted in the slot */
.arcade-cartridge__back {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

.arcade-cart-grip {
  display: flex;
  flex-direction: column;
  gap: 1.5px;
}

.arcade-cart-grip i {
  display: block;
  width: 16px;
  height: 1.5px;
  background: #22080f;
  border-bottom: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 1px;
}

/* The mini cartridge body revealed above when dragged down */
.arcade-cartridge__body {
  position: absolute;
  bottom: calc(100% + 1px);
  left: 0;
  width: 40px;
  height: 48px;
  background: linear-gradient(180deg, #90949d 0%, #767a83 40%, #63666f 100%);
  border: 2px solid #22080f;
  border-radius: 5px 5px 0 0;
  box-shadow: 0 -6px 16px rgba(0, 0, 0, 0.85), inset 0 1px 0 rgba(255, 255, 255, 0.4);
  padding: 3px 4px;
  box-sizing: border-box;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.arcade-cartridge.is-ejected .arcade-cartridge__body,
.arcade-cartridge.is-dragging .arcade-cartridge__body,
.arcade-cartridge[style*="--cart-y"] .arcade-cartridge__body {
  opacity: 1;
  pointer-events: auto;
}

.arcade-cart-notch {
  position: absolute;
  top: -2px;
  right: 5px;
  width: 8px;
  height: 4px;
  background: #22080f;
  border-radius: 0 0 2px 2px;
}

/* Sideways SNAKE label */
.arcade-cart-label {
  background: linear-gradient(135deg, #0f1c11 0%, #152c1a 50%, #0c180e 100%);
  border: 1px solid #234d2b;
  border-radius: 3px;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.8);
  box-sizing: border-box;
}

.arcade-cart-text {
  font-family: 'Space Grotesk', 'Helvetica Neue', sans-serif;
  font-size: 8px;
  font-weight: 800;
  letter-spacing: 0.16em;
  color: #2ce05e;
  text-shadow: 0 0 5px rgba(44, 224, 94, 0.7), 0 1px 0 #000;
  transform: rotate(-90deg);
  white-space: nowrap;
  line-height: 1;
  display: block;
  text-align: center;
}

@keyframes marquee-glow {
  0%, 100% {
    box-shadow: inset 0 3px 0 rgba(255, 255, 255, 0.55), 0 0 24px rgba(70, 140, 255, 0.3);
  }
  50% {
    box-shadow: inset 0 3px 0 rgba(255, 255, 255, 0.55), 0 0 52px rgba(90, 160, 255, 0.55);
  }
}

@keyframes bulb-blink {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0.22; }
}

@keyframes stick-wobble {
  0%, 100% { transform: rotate(-8deg); }
  50% { transform: rotate(8deg); }
}

@media (prefers-reduced-motion: reduce) {
  html[data-theme='terminal'] .snake-root:not(.is-active) .terminal-cursor { animation: none; }
  html[data-theme='arcade'] .cabinet__marquee,
  html[data-theme='arcade'] .cabinet__bulb,
  html[data-theme='arcade'] .cabinet__stick i { animation: none; }
}

/* When the rail is below the screen, reserve the deck's space in EVERY theme.
   Otherwise toggling Arcade adds/removes 70-96px before the settings buttons
   and mobile scroll anchoring visibly jumps the page between frames. */
@media (max-width: 1100px) {
  :root { --cabinet-bottom: 96px; }
}

@media (max-width: 720px) {
  :root {
    --frame-pad: 12px;
    --frame-radius: 20px;
    --field-radius: 6px;
    --cabinet-bottom: 82px;
  }
  html[data-theme='arcade'] {
    --bezel-border: 4px;
    --cabinet-top: 0px;
    --cabinet-bottom: 82px;
    /* Nested rounding. A phone bezel leaves only ~4px between the cabinet edge
       and the playfield, so the field's corners have to follow the cabinet's:
       at 6px the box read as square and its corners poked into the bezel. */
    --bezel-radius: 24px;
    --field-radius: 20px;
  }
  /* Keep the control deck inside the cabinet's own width. At left/right -2% it
     stuck out past the bezel on both sides and the viewport clipped its
     rounded corners, which is what made the whole box look square-edged. */
  html[data-theme='arcade'] .cabinet__deck {
    left: 0;
    right: 0;
    height: 62px;
    padding: 0 12px;
    gap: 12px;
    border-radius: 16px 16px 24px 24px;
  }
  /* The painted buttons stay their original 24px mobile size, while the
     invisible ::after hit pad is enlarged to a full 44px touch target so
     they are easy to press without changing their visual scale. */
  .cabinet__stick { width: 22px; height: 42px; }
  .cabinet__stick::after { inset: -14px -14px -10px; }
  .cabinet__stick i { width: 22px; height: 22px; margin-left: -11px; }
  .cabinet__stick::before { height: 26px; }
  .cabinet__buttons { gap: 10px; }
  .cabinet__buttons i { width: 24px; height: 24px; border-width: 3px; }
  .cabinet__buttons i::after { inset: -10px; }
  .cabinet__slot { width: 36px; height: 16px; }
  .cabinet__slot::after { inset: -14px -8px; }
  .arcade-cartridge { width: 36px; height: 16px; }
  .arcade-cart-text { font-size: 6px; letter-spacing: 0.08em; }
}
