:root {
  --bg: #0b0d14;
  --bg2: #121624;
  --panel: #151a2b;
  --line: #262d47;
  --text: #eef1fb;
  --dim: #8a93b2;
  --accent: #00e5ff;
  --accent2: #ff3d81;
  --gold: #ffd23f;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --cell: 14px;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background:
    radial-gradient(900px 500px at 15% -10%, rgba(0,229,255,.10), transparent 60%),
    radial-gradient(900px 500px at 90% 110%, rgba(255,61,129,.10), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app {
  min-height: 100%;
  min-height: 100dvh;
  max-width: 980px;
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) 16px max(14px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.logo {
  margin: 0; display: flex; gap: 4px;
  font-size: clamp(1.4rem, 4vw, 2rem); font-weight: 900; letter-spacing: .06em;
}
.logo span:nth-child(1) { color: #ff3d81; }
.logo span:nth-child(2) { color: #ff9f1c; }
.logo span:nth-child(3) { color: #ffd23f; }
.logo span:nth-child(4) { color: #7cff6b; }
.logo span:nth-child(5) { color: #00e5ff; }
.logo span:nth-child(6) { color: #b085ff; }
.top-actions { display: flex; gap: 8px; }
.chip {
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  padding: 8px 14px; font-size: .875rem; font-weight: 600; color: var(--dim);
  display: inline-flex; align-items: center; gap: 6px; transition: .15s;
}
.chip:hover { color: var(--text); border-color: var(--dim); }
.chip[aria-pressed="false"] { color: var(--accent); border-color: rgba(0,229,255,.5); }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 18px;
  align-items: start;
  justify-content: center;
}
.side { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.side-left { align-items: flex-end; }
.side-left > *, .side-right > * { width: 100%; max-width: 190px; }
.panel {
  background: linear-gradient(180deg, var(--panel), var(--bg2));
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  margin: 0;
}
.label {
  margin: 0 0 8px; font-size: .875rem; font-weight: 700; letter-spacing: .14em;
  text-transform: uppercase; color: var(--dim);
}
.mini, .queue { display: block; margin: 0 auto; }

.stats { display: grid; gap: 12px; }
.stat dd { margin: 0; font-family: var(--mono); font-weight: 700; font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.stat .label { margin-bottom: 2px; }
.stat-score dd { font-size: 1.9rem; color: var(--accent); }
.stat-best dd { color: var(--gold); font-size: 1.2rem; }

.board-wrap {
  position: relative;
  width: calc(var(--cell) * 10 + 4px);
  height: calc(var(--cell) * 20 + 4px);
  border-radius: 10px;
  border: 2px solid var(--line);
  background: #080a10;
  box-shadow: 0 0 0 1px #000, 0 20px 60px rgba(0,0,0,.55), 0 0 60px rgba(0,229,255,.07);
  overflow: hidden;
}
#board { display: block; width: 100%; height: 100%; }

.flash { position: absolute; inset: 0; pointer-events: none; opacity: 0; background: #fff; }
.flash.go { animation: flash .35s ease-out; }
@keyframes flash { 0% { opacity: .35; } 100% { opacity: 0; } }
.popup {
  position: absolute; left: 0; right: 0; top: 38%; text-align: center; pointer-events: none;
  font-weight: 900; font-size: 1.7rem; letter-spacing: .08em; color: var(--gold);
  text-shadow: 0 0 20px rgba(255,210,63,.7); opacity: 0;
}
.popup.go { animation: pop .9s ease-out; }
@keyframes pop {
  0% { opacity: 0; transform: translateY(14px) scale(.8); }
  20% { opacity: 1; transform: translateY(0) scale(1.05); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-24px) scale(1); }
}

.overlay {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(8,10,16,.78); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  padding: 16px; text-align: center;
}
.overlay[hidden] { display: none; }
.overlay-card h2 { margin: 0 0 6px; font-size: 2rem; font-weight: 900; letter-spacing: .04em; }
.overlay-card p { margin: 0 0 14px; color: var(--dim); font-size: 1rem; }
.overlay-card .overlay-score { color: var(--gold); font-family: var(--mono); font-weight: 700; }
.overlay-hint { font-size: .85rem !important; margin: 12px 0 0 !important; }

.btn {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 16px; font-weight: 700; font-size: .9rem; letter-spacing: .04em; transition: .15s;
}
.btn:hover { border-color: var(--dim); background: #1b2137; }
.btn:active { transform: translateY(1px); }
.btn-primary {
  background: linear-gradient(135deg, var(--accent), #4d7cff); color: #04101a; border: 0;
  padding: 12px 26px; font-size: 1rem; box-shadow: 0 6px 24px rgba(0,229,255,.3);
}
.btn-primary:hover { background: linear-gradient(135deg, #5cf0ff, #6b92ff); }
.buttons { display: grid; gap: 8px; }
.buttons .btn { min-height: 44px; }

kbd {
  font-family: var(--mono); font-size: .85rem; background: #0b0e19; border: 1px solid var(--line);
  border-bottom-width: 2px; border-radius: 6px; padding: 1px 6px; color: var(--text);
}

.touch { display: none; }
.touch-l, .touch-r { display: contents; }
.tbtn {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  font-weight: 800; font-size: 1.15rem; color: var(--text); min-height: 54px;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.tbtn.on, .tbtn:active { background: #232b4a; border-color: var(--accent); }

.help {
  position: fixed; inset: 0; z-index: 10; display: flex; align-items: center; justify-content: center;
  background: rgba(5,6,10,.75); padding: 16px;
}
.help-card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 20px 24px; width: min(360px, 100%); }
.help-card h2 { margin: 0 0 12px; }
.help-card table { width: 100%; border-collapse: collapse; margin-bottom: 16px; }
.help-card td { padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 1rem; }
.help-card td:last-child { color: var(--dim); text-align: right; }
.help-card .btn { width: 100%; }

/* Phone / small tablet: info strip above board, touch pad below */
@media (max-width: 760px) {
  .app { padding-left: 10px; padding-right: 10px; gap: 8px; }
  .layout {
    flex: none;
    grid-template-columns: auto 1fr;
    grid-template-areas: "hold next" "stats stats" "board board" "btns btns";
    gap: 8px; justify-items: center;
  }
  .side { display: contents; }
  .side-left > *, .side-right > * { max-width: none; }
  .hold-panel { grid-area: hold; padding: 6px 8px; }
  .next-panel { grid-area: next; padding: 6px 8px; justify-self: stretch; }
  .hold-panel .label, .next-panel .label { display: none; }
  .hold-panel, .next-panel { display: flex; align-items: center; justify-content: center; }
  .stats { grid-area: stats; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 8px 10px; justify-self: stretch; }
  .stat dd, .stat-score dd, .stat-best dd { font-size: 1.05rem; }
  .stat .label { font-size: .875rem; letter-spacing: .04em; margin: 0; }
  .board-wrap { grid-area: board; }
  .buttons { grid-area: btns; grid-template-columns: 1fr 1fr; padding: 6px; justify-self: stretch; }
  .btn { padding: 8px 12px; font-size: .9rem; }
  .touch { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
  .touch .tbtn { min-height: 50px; }
  .touch .rep { grid-row: 2; }
  .touch [data-act="left"] { grid-column: 1; }
  .touch [data-act="down"] { grid-column: 2 / span 2; }
  .touch [data-act="right"] { grid-column: 4; }
  #help-btn { display: none; }
}
@media (hover: none) and (pointer: coarse) and (min-width: 761px) {
  .touch { display: grid; grid-template-columns: repeat(7, 1fr); gap: 8px; }
}
/* Short portrait phones: board beside a hold/next column, controls merged into the header row */
@media (orientation: portrait) and (max-width: 760px) and (max-height: 680px) {
  .app {
    display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto auto 1fr auto;
    grid-template-areas: "top btns" "stats stats" "board hold" "board next" "touch touch";
    gap: 6px; padding: max(6px, env(safe-area-inset-top)) 8px max(6px, env(safe-area-inset-bottom));
    min-height: 0; align-content: start;
  }
  .layout, .side { display: contents; }
  .topbar { grid-area: 1 / 1 / 2 / 3; justify-self: start; }
  .logo { display: none; }
  .chip { min-height: 44px; }
  .buttons { grid-area: 1 / 1 / 2 / 3; justify-self: end; width: max-content; grid-template-columns: repeat(2, max-content); background: none; border: 0; padding: 0; gap: 6px; }
  .buttons .btn { min-height: 44px; padding: 4px 6px; font-size: .875rem; }
  .stats { padding: 4px 8px; }
  .stat dd, .stat-score dd, .stat-best dd { font-size: 1rem; }
  .board-wrap { grid-area: board; justify-self: start; align-self: start; }
  .hold-panel { grid-area: hold; padding: 4px; }
  .next-panel { grid-area: next; padding: 4px; align-items: flex-start; }
  .touch { grid-area: touch; gap: 6px; }
  .touch .tbtn { min-height: 44px; }
}

/* Short landscape (phones on their side): board centred, info and controls on the flanks */
@media (orientation: landscape) and (max-height: 520px) {
  .app {
    display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto minmax(0, 1fr) minmax(0, 1fr); grid-template-rows: auto auto 1fr;
    grid-template-areas: "top top board next next" "hold stats board btns btns" "padl padl board padr padr";
    gap: 6px 8px; align-items: start; max-width: none;
    height: 100vh; height: 100dvh; min-height: 0;
    padding: max(6px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  }
  .layout, .side { display: contents; }
  .topbar { grid-area: top; }
  .logo { font-size: 1.2rem; }
  .board-wrap { grid-area: board; align-self: center; }
  .hold-panel { grid-area: hold; justify-self: end; padding: 6px; display: flex; }
  .next-panel { grid-area: next; justify-self: start; padding: 6px 8px; display: flex; }
  .hold-panel .label, .next-panel .label { display: none; }
  .stats { grid-area: stats; grid-template-columns: 1fr 1fr; gap: 2px 12px; padding: 6px 10px; justify-self: start; }
  .stat dd, .stat-score dd, .stat-best dd { font-size: 1rem; }
  .stat .label { margin: 0; letter-spacing: .04em; }
  .buttons { grid-area: btns; grid-template-columns: 1fr 1fr; background: none; border: 0; padding: 0; justify-self: start; width: min(100%, 240px); }
  .buttons .btn { min-height: 44px; padding: 6px 8px; }
  .touch-l { grid-area: padl; justify-self: end; }
  .touch-r { grid-area: padr; justify-self: start; }
}
@media (orientation: landscape) and (max-height: 520px) and (pointer: coarse),
       (orientation: landscape) and (max-height: 520px) and (max-width: 900px) {
  .touch { display: contents; }
  .touch-l, .touch-r { display: grid; gap: 6px; align-self: end; width: 100%; }
  .touch-l { grid-template-columns: repeat(3, minmax(0, 1fr)); max-width: 300px; }
  .touch-r { grid-template-columns: repeat(4, minmax(0, 1fr)); max-width: 360px; }
  .touch .tbtn { min-height: 46px; grid-column: auto; grid-row: auto; }
}
@media (orientation: landscape) and (max-height: 520px) and (max-width: 700px) {
  .touch-r { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stats { gap: 0 8px; padding: 4px 8px; }
  .logo { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .flash.go, .popup.go { animation-duration: .01s; }
}

.noscript { padding: 2rem; color: #fff; }
