/* Mai's Invitation: a handheld drawn in the browser. On a phone the casing fills the
   whole page: the screen takes everything above the controls. Everything on the casing
   is flat: one dark plastic, one key colour, no shading. */

@font-face {
  font-family: 'PS2P';
  src: url('fonts/press-start-2p.woff2') format('woff2');
  font-display: block;
}

:root {
  /* The screen: four greens, darkest to lightest. js/gfx.js reads these four, so the
     screen is tuned here. */
  --lcd0: #1a2e16;
  --lcd1: #446b2e;
  --lcd2: #8eac3c;
  --lcd3: #cfdd72;

  /* The casing: near-black plastic with a little of the screen's green in it, so the
     two sit together; the keys are the one warm colour. */
  --body: #171a15;
  --ground: #0b0d0a; /* the frame round the screen, and the desk on a wide window */
  --edge: #30362b;
  --print: #7c856f;
  --key: #e5573c;
  --key-down: #a83c26;
  --key-ink: #2a0f08;

  /* The casing's own width and height, and the height of the controls. */
  --dw: 100vw;
  --dh: 100vh;
  --deck: clamp(180px, calc(var(--dh) * 0.3), 260px);
}
@supports (height: 100dvh) {
  :root { --dh: 100dvh; }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--body);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.stage {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
}

/* ------------------------------------------------------------ the casing */

.device {
  display: grid;
  grid-template: 'head head' minmax(0, 1fr) 'pad keys' var(--deck) / 1fr 1fr;
  width: var(--dw);
  height: var(--dh);
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0)
    env(safe-area-inset-left, 0);
  overflow: hidden;
  background: var(--body);
}

.head {
  grid-area: head;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 8px 8px 0;
}

/* the frame round the screen */
.bezel {
  display: flex;
  flex: 1;
  min-height: 0;
  padding: 6px;
  background: var(--ground);
}
/* the room the screen has; js/game.js fits a whole number of dots into it */
.well {
  display: grid;
  flex: 1;
  place-items: center;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
.screen {
  position: relative;
  width: 160px;
  height: 144px;
  background: var(--lcd3);
}
.screen canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: crisp-edges;
  image-rendering: pixelated;
}

/* under the screen: the sound switch, and nothing else */
.strip {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 28px;
  padding: 0 2px;
  background: var(--body); /* (it is seen against the screen for a moment, at the very end) */
}
.snd {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 28px;
  padding: 1px 0 0;
  border: 0;
  background: none;
  color: var(--print);
  font: 8px/8px 'PS2P', monospace;
  cursor: pointer;
}
.snd .slot {
  position: relative;
  width: 24px;
  height: 10px;
  margin-top: -1px;
  background: var(--ground);
}
.snd .slot i {
  position: absolute;
  left: 2px;
  top: 2px;
  width: 9px;
  height: 6px;
  background: var(--print);
}
.snd[aria-checked='true'] .slot i { left: 13px; background: var(--key); }
.snd:focus-visible { outline: 2px solid var(--print); outline-offset: 2px; }

/* ------------------------------------------------------------ the controls */

/* Each half of the deck is one big touch target: a thumb doesn't have to land exactly. */
.zone {
  display: grid;
  place-items: center;
  touch-action: none;
}
.zone.pad { grid-area: pad; }
.zone.keys { grid-area: keys; }

/* the pad: a flat cross of five squares; the pressed arm goes dark */
.dpad {
  --s: min(calc(var(--dw) * 0.4), calc(var(--deck) - 44px), 168px);
  position: relative;
  width: var(--s);
  height: var(--s);
}
.dpad i {
  position: absolute;
  width: 34%;
  height: 34%;
  background: var(--key);
}
.dpad .up { left: 33%; top: 0; }
.dpad .down { left: 33%; bottom: 0; }
.dpad .left { left: 0; top: 33%; }
.dpad .right { right: 0; top: 33%; }
.dpad .mid { left: 33%; top: 33%; }
.dpad i.on { background: var(--key-down); }
/* an arrow on each arm */
.dpad i:not(.mid)::after {
  content: '';
  position: absolute;
  left: 34%;
  top: 38%;
  width: 32%;
  height: 24%;
  background: var(--key-ink);
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
}
.dpad .down::after { transform: rotate(180deg); }
.dpad .left::after { transform: rotate(-90deg); }
.dpad .right::after { transform: rotate(90deg); }

/* A and B: two flat discs, B lower and to the left */
/* the one key: a flat round, as big as the pad's arms are long, in the middle of its side */
.key {
  --k: clamp(60px, calc(var(--dw) * 0.21), 88px);
  display: grid;
  place-items: center;
  width: var(--k);
  height: var(--k);
  padding: 2px 0 0 1px;
  border-radius: 50%;
  background: var(--key);
  color: var(--key-ink);
  font: 13px/13px 'PS2P', monospace;
}
.key.on { background: var(--key-down); }

/* ------------------------------------------------------------ the keys, kicked off */

/* The very end of the game (js/smash.js): Hujiao kicks the pad and the keys off the
   casing, which leaves hollows; then the plastic they sat in breaks, and the screen has
   the whole handheld. */
.dpad.gone i { background: var(--ground); }
.dpad.gone i::after { display: none; }
.key.gone { background: var(--ground); color: transparent; }

.device.broken { grid-template: 'head' minmax(0, 1fr) / minmax(0, 1fr); }
.device.broken .zone { display: none; }
.device.broken .well { align-items: start; }

/* what is drawn over the page while that happens: Hujiao (in the screen's dots), and
   the keys and plastic in the air */
.over {
  position: absolute;
  left: 0;
  top: 0;
  display: none;
  pointer-events: none;
}
.over.bits { z-index: 2; width: 100%; height: 100%; }
.over.cat {
  z-index: 3;
  image-rendering: crisp-edges;
  image-rendering: pixelated;
}

/* ------------------------------------------------------------ on its side */

/* A phone held sideways: the pad on the left, the keys on the right, the screen between. */
@media (orientation: landscape) and (max-height: 540px) {
  .device { grid-template: 'pad head keys' minmax(0, 1fr) / minmax(132px, 23vw) minmax(0, 1fr) minmax(132px, 23vw); }
  .head { padding: 6px 0 0; }
  .strip, .snd { height: 22px; }
  .dpad { --s: min(19vw, 60vh, 160px); }
  .key { --k: clamp(52px, 9.5vw, 80px); }
}

/* ------------------------------------------------------------ on a desk */

/* A wide window: the handheld at phone size, on a darker desk. */
@media (min-width: 620px) and (min-height: 541px) {
  :root {
    --dw: 400px;
    --dh: min(calc(100vh - 48px), 800px);
  }
  html, body { background: var(--ground); }
  .device { outline: 1px solid var(--edge); }
}
