/* 3D claw machine: full screen canvas with the HUD on top. */
:root {
  --bg: #1a1024;
  --panel: rgba(20, 12, 32, 0.72);
  --line: rgba(255, 255, 255, 0.14);
  --text: #f4eefb;
  --dim: #b7abc8;
  --pink: #ff4fd8;
  --cyan: #45f0ff;
  --gold: #ffd23f;
  --seg: #ff4a4a;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
}

#stage,
#stage canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* ---- HUD layer: only the controls take pointer input */
#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

#hud .btn,
#hud #joystick {
  pointer-events: auto;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.btn {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: rgba(40, 24, 60, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 10px;
  padding: 7px 12px;
  min-height: 36px;
  cursor: pointer;
  touch-action: manipulation;
  white-space: nowrap;
}

.btn:hover {
  border-color: var(--cyan);
}

.btn:active {
  transform: translateY(1px);
  filter: brightness(1.2);
}

.btn.small {
  font-size: 13px;
  padding: 5px 10px;
  min-height: 32px;
}

kbd {
  display: inline-block;
  font-family: ui-monospace, Consolas, monospace;
  font-size: 11px;
  line-height: 1;
  padding: 3px 5px;
  margin-right: 6px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 4px;
  color: var(--dim);
}

/* ---- status, top left */
#status {
  position: absolute;
  top: calc(10px + var(--safe-t));
  left: calc(10px + var(--safe-l));
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  pointer-events: auto;
}

.stat {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 30px;
}

.stat .label {
  font-size: 13px;
  color: var(--dim);
  min-width: 4.5em;
}

.seg {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 22px;
  font-weight: 700;
  color: var(--seg);
  text-shadow: 0 0 8px rgba(255, 74, 74, 0.7);
  min-width: 2.6em;
  letter-spacing: 1px;
}

#wallet {
  color: var(--gold);
  text-shadow: 0 0 8px rgba(255, 210, 63, 0.6);
}

body.low-time #timer {
  animation: blink 0.5s steps(2) infinite;
}

@keyframes blink {
  50% {
    opacity: 0.35;
  }
}

/* ---- menu, top right */
#tools {
  position: absolute;
  top: calc(10px + var(--safe-t));
  right: calc(10px + var(--safe-r));
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
}

#prize-count {
  display: inline-block;
  min-width: 1.6em;
  padding: 0 5px;
  margin-left: 4px;
  border-radius: 9px;
  background: var(--pink);
  color: #fff;
  font-size: 12px;
  text-align: center;
}

/* ---- messages, top centre */
#msgs {
  position: absolute;
  top: calc(12px + var(--safe-t));
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100vw - 440px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

#toast {
  padding: 9px 16px;
  border-radius: 12px;
  background: rgba(255, 79, 216, 0.92);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  line-height: 1.45;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.35);
  opacity: 0;
  transform: translateY(-8px);
  transition: opacity 0.2s, transform 0.2s;
}

#toast.show {
  opacity: 1;
  transform: none;
}

#toast:empty {
  display: none;
}

#hint {
  padding: 6px 12px;
  border-radius: 10px;
  background: rgba(10, 30, 40, 0.8);
  border: 1px solid rgba(69, 240, 255, 0.5);
  color: var(--cyan);
  font-size: 13px;
  text-align: center;
  line-height: 1.45;
}

/* ---- quick views, right middle */
#views {
  position: absolute;
  right: calc(10px + var(--safe-r));
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#views .btn {
  min-width: 78px;
  text-align: left;
}

/* ---- keyboard help, bottom left (desktop) */
#keys-help {
  position: absolute;
  left: calc(10px + var(--safe-l));
  bottom: calc(10px + var(--safe-b));
  margin: 0;
  padding: 8px 12px;
  list-style: none;
  font-size: 12px;
  color: var(--dim);
  line-height: 1.9;
}

/* ---- on-screen joystick, bottom left (touch) */
#joystick {
  position: absolute;
  left: calc(18px + var(--safe-l));
  bottom: calc(30px + var(--safe-b));
  width: 132px;
  height: 132px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.02) 70%);
  border: 2px solid rgba(255, 255, 255, 0.28);
  touch-action: none;
  display: none;
}

#knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 58px;
  height: 58px;
  margin: -29px 0 0 -29px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ff8fe6, #d2189f);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
  pointer-events: none;
}

body.touch #joystick {
  display: block;
}

body.touch #keys-help,
body.touch kbd {
  display: none;
}

/* ---- start and drop, bottom right */
#actions {
  position: absolute;
  right: calc(14px + var(--safe-r));
  bottom: calc(30px + var(--safe-b));
  display: flex;
  align-items: flex-end;
  gap: 12px;
}

.btn.big {
  border-radius: 50%;
  font-size: 16px;
  font-weight: 700;
  width: 72px;
  height: 72px;
  padding: 0;
}

.btn.big.primary {
  width: 92px;
  height: 92px;
  font-size: 20px;
  background: radial-gradient(circle at 35% 30%, #ff6b8a, #c4123e);
  border: 3px solid #ffd6df;
  box-shadow: 0 0 18px rgba(255, 60, 100, 0.55);
}

#credit {
  position: absolute;
  right: calc(8px + var(--safe-r));
  bottom: calc(4px + var(--safe-b));
  font-size: 11px;
  color: #8f8a9a;
}

/* ---- prize popup */
#win-pop {
  position: absolute;
  left: 50%;
  top: 36%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 16px 26px;
  border-radius: 18px;
  background: rgba(30, 12, 44, 0.9);
  border: 2px solid var(--gold);
  box-shadow: 0 0 30px rgba(255, 210, 63, 0.5);
  max-width: calc(100vw - 32px);
}

#win-name {
  font-size: 26px;
  color: var(--gold);
  text-align: center;
}

#win-pop.pop {
  animation: pop 0.45s cubic-bezier(0.2, 1.6, 0.4, 1);
}

@keyframes pop {
  from {
    transform: translate(-50%, -50%) scale(0.4);
    opacity: 0;
  }
  to {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.r-common {
  background: #59627a;
  color: #fff;
}

.r-rare {
  background: #6a4cff;
  color: #fff;
}

.r-limited {
  background: linear-gradient(135deg, #ffe680, #e0a100);
  color: #3a2600;
}

/* ---- debug read-out */
#debug {
  position: absolute;
  left: calc(10px + var(--safe-l));
  top: calc(130px + var(--safe-t));
  margin: 0;
  padding: 6px 10px;
  max-width: min(560px, calc(100vw - 120px));
  white-space: pre-wrap;
  font: 11px/1.5 ui-monospace, Consolas, monospace;
  color: #b9ffcf;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 8px;
}

/* ---- panels */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 4, 14, 0.6);
}

.sheet-box {
  width: min(560px, 100%);
  max-height: calc(100% - 16px);
  overflow-y: auto;
  padding: 14px 18px 18px;
  border-radius: 16px;
  background: #241634;
  border: 1px solid var(--line);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  user-select: text;
  -webkit-user-select: text;
}

.sheet header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sheet h2 {
  margin: 4px 0;
  font-size: 20px;
}

.note {
  margin: 6px 0 10px;
  font-size: 12px;
  color: var(--dim);
}

#prize-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

#prize-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--line);
}

#prize-list .name {
  flex: 1;
}

#prize-list .count {
  color: var(--dim);
  font-family: ui-monospace, Consolas, monospace;
}

#prize-list li.empty {
  justify-content: center;
  color: var(--dim);
  border: 0;
  padding: 24px 0;
}

.tips {
  margin: 8px 0 14px;
  padding-left: 18px;
  line-height: 1.6;
  font-size: 14px;
}

.tips li {
  margin-bottom: 6px;
}

.tips b {
  color: var(--cyan);
  margin-right: 6px;
}

table.keys {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

table.keys th,
table.keys td {
  text-align: left;
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
}

table.keys th {
  color: var(--dim);
  font-weight: 600;
}

/* ---- loading and errors */
#boot,
#noscript {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  font-size: 18px;
  line-height: 1.7;
  background: var(--bg);
  color: var(--text);
}

#boot.error {
  font-size: 16px;
  color: #ffd0d0;
}

/* ---- phones and narrow windows */
@media (max-width: 760px) {
  #msgs {
    top: calc(142px + var(--safe-t));
    max-width: calc(100vw - 24px);
  }

  #toast {
    font-size: 14px;
  }

  #hint {
    font-size: 12px;
  }

  /* a row above the joystick, clear of the cabinet's coin slot */
  #views {
    top: auto;
    right: 50%;
    bottom: calc(176px + var(--safe-b));
    transform: translateX(50%);
    flex-direction: row;
  }

  #views .btn {
    min-width: 0;
    text-align: center;
  }

  #debug {
    top: auto;
    bottom: calc(226px + var(--safe-b));
  }

  .seg {
    font-size: 19px;
  }

  .stat .label {
    min-width: 4.2em;
  }
}

/* ---- short screens (phones held sideways): views in a 2 x 2 block under the menu */
@media (max-height: 500px) and (min-width: 761px) {
  #views {
    display: grid;
    grid-template-columns: 1fr 1fr;
    top: calc(126px + var(--safe-t));
    transform: none;
  }

  #keys-help {
    display: none;
  }
}
