:root {
  --bg: #070914;
  --panel: rgba(20, 24, 48, 0.62);
  --border: rgba(120, 140, 255, 0.18);
  --text: #e9ecff;
  --muted: #8a93c4;
  --accent: #45d6ff;
  --accent2: #ff5ad8;
  --touch-h: 0px;
  /* safe-area insets (iPhone notch / Dynamic Island / home indicator); viewport-fit=cover */
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
}
* { box-sizing: border-box; }
html {
  /* solid dark colour on the root so the whole canvas (incl. iOS home-indicator / status-bar areas) is never white */
  background-color: #070914;
  height: 100%;
}
html, body {
  margin: 0; overflow: hidden; width: 100%;
  background-color: #070914;
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  overscroll-behavior: none;
}
body {
  position: fixed; inset: 0; margin: 0; overflow: hidden;
  background-image: radial-gradient(ellipse at 50% 20%, #1a1f4a 0%, #0b0e22 45%, #070914 100%);
}
/* Full-bleed backdrop that over-extends past every edge, so no uncovered strip can appear */
body::before {
  content: ""; position: fixed; left: 0; right: 0; top: -25vh; bottom: -25vh; z-index: -1; pointer-events: none;
  background: radial-gradient(ellipse at 50% 40%, #1a1f4a 0%, #0b0e22 35%, #070914 70%) #070914;
}
button { font: inherit; color: inherit; }
kbd {
  display: inline-block; min-width: 1.6em; padding: 1px 5px; margin-right: 3px;
  border-radius: 5px; border: 1px solid var(--border); border-bottom-width: 2px;
  background: rgba(255,255,255,0.06); font-size: 0.78em; text-align: center; font-family: inherit;
}

#app {
  /* Pinned to the actual visible viewport (iOS standalone can expose less than the screen).
     --app-h (set from visualViewport/innerHeight, never larger) only refines it. */
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  height: var(--app-h, auto); max-height: 100%;
  max-width: 1200px; margin: 0 auto; box-sizing: border-box; overflow: hidden;
  display: grid;
  grid-template-columns: minmax(150px, 230px) minmax(0, 1fr) minmax(150px, 230px);
  grid-template-rows: minmax(0, 1fr) auto;
  grid-template-areas: "left stage right" "touch touch touch";
  gap: 12px;
  padding: max(12px, var(--sat)) max(12px, var(--sar)) max(12px, var(--sab)) max(12px, var(--sal));
}
.panel { display: flex; flex-direction: column; gap: 12px; min-width: 0; min-height: 0; padding-top: 4px; }
.panel.left { grid-area: left; }
.panel.right { grid-area: right; }
#stage { grid-area: stage; position: relative; min-width: 0; min-height: 0; touch-action: none; overscroll-behavior: none; }
#app, #touch, .tbtn, .tool, .card { touch-action: none; }
[hidden] { display: none !important; }
#gl { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

.logo {
  margin: 0 0 2px; font-size: 30px; letter-spacing: 0.22em; font-weight: 900;
  background: linear-gradient(90deg, #22e3f2, #b25cff 50%, #ff4a6a);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 0 12px rgba(120, 100, 255, 0.35));
}
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 10px 12px; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 8px 30px rgba(0,0,0,0.35), inset 0 1px 0 rgba(255,255,255,0.05);
  display: flex; flex-direction: column; gap: 6px; text-align: left;
}
.hold-card { cursor: pointer; }
.hold-card:active { transform: scale(0.98); }
.label { font-size: 11px; letter-spacing: 0.18em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
.preview { width: 100%; aspect-ratio: 2 / 1.25; display: block; }
.preview.tall { aspect-ratio: 1 / 2.3; max-height: 52vh; max-height: 52dvh; }
.stats { gap: 8px; }
.stat { display: flex; flex-direction: column; }
.value { font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: 0.02em; transition: color .2s; }
.value.bump { animation: bump .35s ease-out; }
@keyframes bump { 0% { transform: scale(1.25); color: #fff; } 100% { transform: scale(1); } }
.keys ul { list-style: none; margin: 0; padding: 0; font-size: 12px; color: #c4c9ee; display: grid; gap: 3px; }
.panel.left { overflow-y: auto; scrollbar-width: none; }
@media (max-height: 860px) and (pointer: fine) { .keys ul { font-size: 11px; gap: 1px; } .stats { gap: 4px; } .value { font-size: 20px; } }
.tools { display: flex; gap: 8px; }
.tool {
  flex: 1; height: 42px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--panel); cursor: pointer; font-size: 16px; transition: background .15s, transform .1s;
}
@media (hover: hover) { .tool:hover { background: rgba(60, 70, 140, 0.6); } }
.tool:active { transform: scale(0.95); }
.tool.off { opacity: 0.45; }

.overlay {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(5, 7, 20, 0.55); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  opacity: 0; pointer-events: none; transition: opacity .25s; border-radius: 16px; z-index: 5;
}
.overlay.show { opacity: 1; pointer-events: auto; }
.overlay {
  --bw: var(--board-w, 300px);
  --bh: var(--board-h, 560px);
  /* scale the overlay text with the on-screen board, limited by its width or its height */
  --ov-fs: clamp(9px, min(calc(var(--bw) * 0.047), calc(var(--bh) * 0.024)), 15px);
  overflow: hidden;
}
.overlay-inner {
  text-align: center; box-sizing: border-box;
  width: min(calc(var(--bw) - 12px), 340px); max-width: calc(100% - 12px); max-height: min(100%, var(--bh));
  transform: translateY(var(--board-dy, 0px)); /* centre on the well, not the canvas */
  padding: clamp(8px, calc(var(--bw) * 0.05), 20px);
  font-size: var(--ov-fs); overflow: hidden;
}
.overlay h2 {
  white-space: nowrap;
  margin: 0 0 0.2em; font-size: clamp(20px, calc(var(--bw) * 0.13), 48px); letter-spacing: 0.16em; font-weight: 900;
  background: linear-gradient(90deg, #22e3f2, #b25cff 50%, #ff4a6a);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.overlay p { margin: 0.4em 0; color: #c4c9ee; font-size: calc(var(--ov-fs) * 1.1); text-wrap: balance; }
.ov-score { font-size: calc(var(--ov-fs) * 1.25) !important; font-weight: 700; color: #fff !important; }
.ov-score:empty { display: none; }
.hint { font-size: var(--ov-fs) !important; color: var(--muted) !important; margin-top: 1.1em !important; }
.overlay h2.long { font-size: clamp(18px, calc(var(--bw) * 0.095), 40px); letter-spacing: 0.1em; }
.ov-score span { display: block; white-space: nowrap; }
.ov-score span + span { font-size: 0.8em; font-weight: 600; color: #c4c9ee; margin-top: 0.15em; }
.gestures {
  display: none; grid-template-columns: max-content max-content; justify-content: center;
  column-gap: 0.8em; row-gap: 0.3em; margin: 1.1em auto 0; padding: 0; text-align: left;
  font-size: var(--ov-fs); line-height: 1.25; white-space: nowrap;
}
.gestures dt { color: #e9ecff; font-weight: 700; text-align: right; margin: 0; }
.gestures dd { color: var(--muted); margin: 0; }
.big-btn {
  margin-top: 0.9em; padding: 0.8em 2.4em; font-size: calc(var(--ov-fs) * 1.1) !important; border-radius: 999px; border: 0; cursor: pointer;
  font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase; color: #0a0c1c;
  background: linear-gradient(90deg, #22e3f2, #b25cff);
  box-shadow: 0 6px 24px rgba(120, 100, 255, 0.45);
  transition: transform .1s, box-shadow .2s;
}
.big-btn:hover { box-shadow: 0 8px 32px rgba(120, 100, 255, 0.7); }
.big-btn:active { transform: scale(0.96); }

#popups { position: absolute; inset: 0; pointer-events: none; overflow: hidden; z-index: 4; }
.popup {
  position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%);
  font-weight: 900; font-size: clamp(22px, 6vw, 40px); letter-spacing: 0.1em; white-space: nowrap;
  color: #fff; text-shadow: 0 0 18px var(--glow, #45d6ff), 0 0 4px rgba(0,0,0,0.6);
  animation: pop 1.1s cubic-bezier(.2,.9,.3,1) forwards; text-align: center;
}
.popup small { display: block; font-size: 0.5em; letter-spacing: 0.15em; opacity: 0.9; }
@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, -30%) scale(0.6); }
  12% { opacity: 1; transform: translate(-50%, -50%) scale(1.12); }
  25% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  65% { opacity: 1; transform: translate(-50%, -65%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -110%) scale(1); }
}

#touch { grid-area: touch; display: none; justify-content: space-between; gap: 10px; }
.pad { display: flex; gap: 8px; min-width: 0; }
.tbtn {
  width: 58px; height: 58px; border-radius: 16px; border: 1px solid var(--border);
  background: linear-gradient(180deg, rgba(40,46,92,0.85), rgba(22,26,54,0.85));
  font-size: 24px; touch-action: none; box-shadow: 0 4px 14px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.08);
  transition: transform .06s, background .1s;
}
.tbtn.pressed { transform: scale(0.92); background: linear-gradient(180deg, rgba(70,80,160,0.95), rgba(40,46,100,0.95)); }
.tbtn.accent { background: linear-gradient(180deg, rgba(178,92,255,0.75), rgba(90,50,170,0.85)); }

.touch-only { display: none; }

/* Touch devices */
@media (pointer: coarse) {
  .desktop-only { display: none !important; }
  .touch-only { display: block; }
  #touch.touch-only { display: flex; }
  .gestures.touch-only { display: grid; }
}

/* Narrow portrait screens (phones): HUD becomes a compact top bar, board gets full width */
/* ---------- Portrait phones & tall tablets: compact HUD bar on top, board fills the middle, small controls ---------- */
@media (max-width: 760px) and (orientation: portrait), (orientation: portrait) and (max-aspect-ratio: 4/5) {
  #app {
    max-width: none;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas: "left right" "stage stage" "touch touch";
    gap: 6px;
    padding: max(6px, var(--sat)) max(8px, var(--sar)) max(10px, var(--sab)) max(8px, var(--sal));
  }
  .panel { flex-direction: row; gap: 4px; padding-top: 0; align-items: stretch; overflow: visible !important; }
  .logo { display: none; }
  .card { padding: 3px 6px 4px; border-radius: 9px; gap: 1px; box-shadow: 0 4px 14px rgba(0,0,0,0.3), inset 0 1px 0 rgba(255,255,255,0.05); }
  .label { font-size: 8px; line-height: 10px; letter-spacing: 0.08em; }
  .hold-card { width: 46px; flex: none; }
  .preview, .preview.tall { aspect-ratio: auto; height: 27px; max-height: none; }
  .next-card { width: 100px; flex: none; }
  /* stats: two rows of "LABEL value" pairs (Score/Level, Best/Lines) */
  .stats { flex: 1; min-width: 0; display: grid; grid-template-columns: max-content minmax(0, 1fr) max-content max-content; align-content: center; gap: 2px 4px; padding-top: 3px; }
  .stat { display: contents; }
  .stat .label { align-self: center; }
  .stat:nth-child(1) > * { grid-row: 1; } .stat:nth-child(2) > * { grid-row: 2; }
  .stat:nth-child(3) > * { grid-row: 1; } .stat:nth-child(4) > * { grid-row: 2; }
  .stat:nth-child(1) .label, .stat:nth-child(2) .label { grid-column: 1; }
  .stat:nth-child(1) .value, .stat:nth-child(2) .value { grid-column: 2; }
  .stat:nth-child(3) .label, .stat:nth-child(4) .label { grid-column: 3; padding-left: 2px; }
  .stat:nth-child(3) .value, .stat:nth-child(4) .value { grid-column: 4; min-width: 1.6em; }
  .value { font-size: 13px; line-height: 15px; white-space: nowrap; overflow: hidden; text-overflow: clip; }
  .tools { display: grid; grid-template-columns: repeat(2, 28px); grid-auto-rows: minmax(0, 1fr); gap: 3px; }
  .tool { width: 28px; height: auto; min-height: 0; font-size: 11px; border-radius: 7px; padding: 0; }
  .desktop-only { display: none !important; }

  #touch { gap: 10px; }
  #touch .pad { flex: 1; gap: 6px; }
  #touch .tbtn { flex: 1; width: auto; min-width: 0; max-width: 64px; height: 42px; border-radius: 11px; font-size: 18px; box-shadow: 0 2px 8px rgba(0,0,0,0.35), inset 0 1px 0 rgba(255,255,255,0.08); }
  #touch .pad:last-child { justify-content: flex-end; }
  #a2hs { bottom: calc(max(10px, var(--sab)) + 54px); }
}
/* Very narrow phones (360px Android, iPhone SE 1st gen 320px) */
@media (max-width: 370px) and (orientation: portrait) {
  .panel { gap: 3px; }
  .card { padding-left: 4px; padding-right: 4px; }
  .hold-card { width: 40px; }
  .next-card { width: 86px; }
  .stats { column-gap: 3px; }
  .value { font-size: 12px; }
  .label { font-size: 7.5px; letter-spacing: 0.04em; }
  .tools { grid-template-columns: repeat(2, 25px); }
  .tool { width: 25px; font-size: 10px; }
  #touch .tbtn { height: 40px; font-size: 17px; }
  #touch { gap: 8px; }
  #touch .pad { gap: 5px; }
}
/* Portrait tablets: same arrangement, scaled up */
@media (min-width: 600px) and (orientation: portrait) and (max-aspect-ratio: 4/5) {
  #app { gap: 10px; padding: max(12px, var(--sat)) max(16px, var(--sar)) max(14px, var(--sab)) max(16px, var(--sal)); }
  .panel { gap: 8px; }
  .card { padding: 6px 10px 8px; border-radius: 12px; gap: 3px; }
  .label { font-size: 11px; line-height: 13px; letter-spacing: 0.14em; }
  .hold-card { width: 92px; }
  .preview, .preview.tall { height: 50px; }
  .next-card { width: 220px; }
  .stats { gap: 4px 10px; }
  .stat:nth-child(3) .label, .stat:nth-child(4) .label { padding-left: 8px; }
  .value { font-size: 22px; line-height: 26px; }
  .tools { grid-template-columns: repeat(2, 44px); gap: 6px; }
  .tool { width: 44px; font-size: 15px; border-radius: 10px; }
  #touch .pad { gap: 10px; }
  #touch .tbtn { max-width: 84px; height: 54px; font-size: 22px; border-radius: 14px; }
  #a2hs { bottom: calc(max(14px, var(--sab)) + 68px); }
}
/* ---------- Short landscape phones: HUD + control pads in side columns, board in the middle ---------- */
@media (pointer: coarse) and (max-height: 500px) and (orientation: landscape) {
  #app {
    max-width: none;
    grid-template-columns: minmax(120px, 172px) minmax(0, 1fr) minmax(120px, 172px);
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas: "left stage right" "padl stage padr";
    gap: 6px 10px;
    padding: max(6px, var(--sat)) max(10px, var(--sar)) max(8px, var(--sab)) max(10px, var(--sal));
  }
  #touch.touch-only { display: contents; }
  #touch .pad:first-child { grid-area: padl; }
  #touch .pad:last-child { grid-area: padr; }
  #touch .pad { gap: 6px; }
  #touch .tbtn { flex: 1; width: auto; min-width: 0; height: 44px; border-radius: 11px; font-size: 18px; }
  .panel { gap: 6px; padding-top: 0; overflow: hidden !important; }
  .card { padding: 5px 8px 6px; border-radius: 10px; gap: 2px; }
  .label { font-size: 9px; letter-spacing: 0.12em; }
  .preview { aspect-ratio: auto; height: 40px; }
  .stats { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 3px 8px; }
  .value { font-size: 15px; line-height: 18px; white-space: nowrap; overflow: hidden; }
  .next-card { flex: 1 1 auto; min-height: 0; }
  .preview.tall { aspect-ratio: auto; flex: 1 1 auto; height: auto; min-height: 0; max-height: none; }
  .tools { gap: 5px; }
  .tool { height: 30px; font-size: 13px; border-radius: 9px; }
  .keys { display: none; }
  .logo { display: none; }
  #a2hs { bottom: max(10px, var(--sab)); }
}

/* Fullscreen / standalone: use every pixel */
:fullscreen body, :-webkit-full-screen body { background: var(--bg); }
#app:fullscreen { max-width: none; width: 100%; height: 100%; background: radial-gradient(ellipse at 50% 20%, #1a1f4a 0%, #0b0e22 45%, var(--bg) 100%); }
#app:-webkit-full-screen { max-width: none; width: 100%; height: 100%; background: radial-gradient(ellipse at 50% 20%, #1a1f4a 0%, #0b0e22 45%, var(--bg) 100%); }

/* iOS "Add to Home Screen" hint */
.a2hs {
  position: fixed; left: 0; right: 0; margin: 0 auto; width: max-content;
  bottom: calc(max(10px, var(--sab)) + 76px);
  z-index: 50; display: flex; align-items: center; gap: 10px;
  padding: 9px 10px 9px 14px; border-radius: 12px; max-width: calc(100vw - 24px);
  background: rgba(30, 36, 76, 0.95); border: 1px solid rgba(120,140,255,0.35);
  box-shadow: 0 10px 30px rgba(0,0,0,0.5); font-size: 13px; color: var(--text);
  animation: hintIn .35s ease-out;
}
.a2hs b { color: #fff; }
.a2hs .share-ico { color: var(--accent); }
.a2hs button { border: 0; background: rgba(255,255,255,0.1); border-radius: 8px; width: 28px; height: 28px; cursor: pointer; flex: none; }
@keyframes hintIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

/* Camera panel */
.cam-panel {
  position: absolute; top: 8px; left: 0; right: 0; margin: 0 auto; width: max-content; max-width: calc(100% - 8px);
  z-index: 6; display: flex; flex-direction: column; gap: 6px; padding: 8px;
  background: rgba(16, 20, 44, 0.88); border: 1px solid rgba(120,140,255,0.3); border-radius: 14px;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); box-shadow: 0 10px 30px rgba(0,0,0,0.45);
  animation: hintIn .2s ease-out; touch-action: none;
}
.cam-row { display: flex; gap: 5px; justify-content: center; align-items: center; }
.cam-sep { width: 4px; }
.cbtn {
  min-width: 34px; height: 34px; padding: 0 8px; border-radius: 9px; border: 1px solid var(--border);
  background: rgba(40,46,92,0.85); cursor: pointer; font-size: 14px; touch-action: none; flex: none;
  transition: transform .06s, background .12s;
}
@media (hover: hover) { .cbtn:hover { background: rgba(60,70,140,0.9); } }
.cbtn.pressed, .cbtn:active { transform: scale(0.92); }
.cbtn.wide { font-size: 12px; font-weight: 700; letter-spacing: 0.02em; flex: 1; min-width: 0; padding: 0 6px; }
.cam-panel { min-width: min(300px, calc(100% - 8px)); }
.cbtn.active { background: linear-gradient(180deg, rgba(69,214,255,0.55), rgba(178,92,255,0.55)); border-color: rgba(160,180,255,0.6); }
.cbtn.close { background: transparent; }
.tool.on { background: linear-gradient(180deg, rgba(69,214,255,0.45), rgba(178,92,255,0.45)); }
.keys li.small { font-size: 11px; color: var(--muted); }
@media (max-width: 400px) {
  .cbtn { min-width: 30px; height: 32px; padding: 0 6px; font-size: 13px; }
  .cbtn.wide { font-size: 11px; }
  .cam-row { gap: 4px; }
}
