* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --sky-top: #ffe3c8;
  --sky-bottom: #f3a6a0;
  --board-bg: #fff6ec;
  --text: #5b4358;
  --text-dim: #a08698;
  --panel: #ffffffcc;
  --accent: #e2725b;
  --grid: #f3ddc8;
  /* social-kit chrome rides the live palette: applyPaletteToCss() rewrites
     these source vars for the light AND dark variant of every level theme,
     so the kit's chips follow the monument-valley scheme automatically */
  --soc-bg: var(--board-bg);
  --soc-fg: var(--text);
  --soc-dim: var(--text-dim);
  --soc-line: var(--grid);
  --soc-accent: var(--accent);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

html,
body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial,
    sans-serif;
  background: linear-gradient(180deg, var(--sky-top), var(--sky-bottom));
  transition: background 0.8s ease;
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  position: fixed;
  inset: 0;
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-top) + 8px) calc(var(--safe-right) + 12px)
    calc(var(--safe-bottom) + 8px) calc(var(--safe-left) + 12px);
  touch-action: none;
}

.game-shell {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  min-height: 0;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}

/* ---------- HUD ---------- */

.hud {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  width: 100%;
  gap: 8px;
}

.hud-side {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: var(--panel);
  border-radius: 14px;
  padding: 8px 10px;
  min-width: 72px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.hud-label {
  font-size: 0.62rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-dim);
}

.hud-side canvas {
  width: 52px;
  height: 34px;
}

.next-stack {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.next-stack canvas.next-small {
  width: 40px;
  height: 26px;
  opacity: 0.6;
}

.hud-mid {
  flex: 1;
  text-align: center;
  padding-top: 2px;
}

.score {
  font-size: 1.9rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.hud-sub {
  font-size: 0.8rem;
  color: var(--text-dim);
  font-weight: 600;
}

.theme-name {
  font-size: 0.68rem;
  color: var(--text-dim);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  margin-top: 2px;
}

/* ---------- Board ---------- */

.board-wrap {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

#board {
  aspect-ratio: 1 / 2;
  max-height: 100%;
  max-width: 100%;
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(40, 20, 60, 0.18);
  touch-action: none;
}

/* ---------- Button bar ---------- */

.btn-bar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  width: 100%;
}

.btn-bar.hidden {
  display: none;
}

.ctrl-btn {
  appearance: none;
  border: none;
  background: var(--panel);
  color: var(--text);
  border-radius: 14px;
  font-size: 1.25rem;
  padding: 12px 0;
  font-weight: 700;
  cursor: pointer;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  touch-action: none;
  transition: transform 0.06s ease;
}

.ctrl-btn:active {
  transform: scale(0.92);
}

/* ---------- Overlay screens ---------- */

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(var(--safe-top) + 20px) calc(var(--safe-right) + 20px)
    calc(var(--safe-bottom) + 20px) calc(var(--safe-left) + 20px);
  background: linear-gradient(180deg, var(--sky-top), var(--sky-bottom));
  transition: opacity 0.3s ease;
  z-index: 10;
  overflow: hidden;
  /* Menus rely on click events, so no preventDefault here; manipulation
     keeps taps and scrolling but disables double-tap zoom. */
  touch-action: manipulation;
}

.overlay.translucent {
  background: color-mix(in srgb, var(--sky-bottom) 55%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.overlay.gone {
  opacity: 0;
  pointer-events: none;
}

.sheet {
  width: 100%;
  max-width: 380px;
  max-height: 100%;
  overflow-y: auto;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: stretch;
}

.title {
  font-size: 2.3rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  line-height: 1.1;
}

.subtitle {
  color: var(--text-dim);
  font-weight: 600;
  font-size: 0.9rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.big-number {
  font-size: 3rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.badge {
  align-self: center;
  background: var(--accent);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  border-radius: 999px;
  padding: 6px 14px;
}

.menu-btn {
  appearance: none;
  border: none;
  border-radius: 16px;
  padding: 15px 20px;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  background: var(--panel);
  color: var(--text);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transition: transform 0.06s ease;
}

.menu-btn:active {
  transform: scale(0.97);
}

.menu-btn.primary {
  background: var(--accent);
  color: #fff;
}

.menu-btn.update-btn {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  color: var(--accent);
  border: 1.5px solid var(--accent);
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  border-radius: 16px;
  padding: 12px 16px;
  font-weight: 600;
  font-size: 0.95rem;
}

.toggle-value {
  color: var(--accent);
  font-weight: 700;
}

/* ---------- History ---------- */

.stats-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.stat {
  background: var(--panel);
  border-radius: 14px;
  padding: 10px 4px;
}

.stat b {
  display: block;
  font-size: 1.1rem;
  font-variant-numeric: tabular-nums;
}

.stat span {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  font-weight: 700;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 44dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}

.history-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  background: var(--panel);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 0.85rem;
  font-weight: 600;
}

.history-row .when {
  color: var(--text-dim);
  font-size: 0.72rem;
  font-weight: 500;
}

.history-row .pts {
  font-variant-numeric: tabular-nums;
  font-weight: 800;
}

.history-empty {
  color: var(--text-dim);
  padding: 20px 0;
  font-weight: 600;
}

/* ---------- Pause button (in HUD middle) ---------- */

.pause-btn {
  appearance: none;
  border: none;
  background: var(--panel);
  color: var(--text-dim);
  border-radius: 999px;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 5px 14px;
  margin-top: 6px;
  cursor: pointer;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: none;
}

.pause-btn.visible {
  display: inline-block;
}

@media (min-height: 700px) {
  .hud-side canvas {
    width: 60px;
    height: 40px;
  }
}

/* ---------- Duel ---------- */

.score-input {
  width: 7em;
  border: none;
  border-radius: 10px;
  padding: 8px 10px;
  font: inherit;
  font-weight: 700;
  text-align: right;
  color: var(--text);
  background: color-mix(in srgb, var(--text) 8%, transparent);
}

.preset-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.preset-btn {
  appearance: none;
  border: none;
  border-radius: 12px;
  padding: 10px 0;
  font-weight: 700;
  cursor: pointer;
  background: var(--panel);
  color: var(--text-dim);
}

.paste-box {
  width: 100%;
  min-height: 64px;
  border: none;
  border-radius: 14px;
  padding: 12px;
  font: inherit;
  font-size: 0.8rem;
  color: var(--text);
  background: var(--panel);
  resize: none;
  -webkit-user-select: text;
  user-select: text;
}

.duel-status {
  font-style: italic;
}

.name-input {
  width: 9em;
  border: none;
  border-radius: 10px;
  padding: 8px 10px;
  font: inherit;
  font-weight: 700;
  text-align: right;
  color: var(--text);
  background: color-mix(in srgb, var(--text) 8%, transparent);
}

.mode-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.mode-btn.selected {
  background: var(--accent);
  color: #fff;
}

.lobby-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.lobby-row {
  background: var(--panel);
  border-radius: 12px;
  padding: 10px 14px;
  font-weight: 700;
  text-align: left;
}

.menu-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.spectate-banner {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  top: calc(env(safe-area-inset-top, 0px) + 6px);
  z-index: 9;
  background: var(--panel);
  color: var(--text-dim);
  font-size: 0.72rem;
  font-weight: 700;
  padding: 6px 14px;
  border-radius: 999px;
  display: none;
}

.countdown {
  font-size: 6rem;
  font-weight: 800;
}

.opponent-view {
  position: fixed;
  z-index: 8;
  width: 92px;
  border-radius: 12px;
  overflow: hidden;
  /* No backdrop blur here: it floats over a canvas and blurring a live
     backdrop forces the GPU to recomposite constantly. */
  background: var(--board-bg);
  box-shadow: 0 8px 28px rgba(30, 15, 45, 0.35);
  touch-action: none;
  cursor: grab;
  -webkit-user-select: none;
  user-select: none;
  transition: opacity 0.3s ease;
}

.opponent-view.stale {
  opacity: 0.45;
}

.opponent-view.out {
  opacity: 0.4;
  filter: grayscale(0.8);
}

.opponent-view.viewing {
  outline: 2.5px solid var(--accent);
  outline-offset: 1px;
}

.spectate-banner {
  cursor: pointer;
}

/* ---------- Settings / how-it-works ---------- */

.link-btn {
  appearance: none;
  border: none;
  background: none;
  color: var(--text-dim);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  padding: 6px;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.how-sheet {
  gap: 10px;
}

.how-card {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  background: var(--panel);
  border-radius: 16px;
  padding: 14px;
  text-align: left;
  font-size: 0.85rem;
  line-height: 1.45;
  color: var(--text);
}

.how-card .how-icon {
  font-size: 1.3rem;
  line-height: 1.2;
}

.how-card b {
  display: block;
  margin-bottom: 3px;
}

.how-card i {
  font-style: normal;
  font-weight: 700;
  color: var(--accent);
}

.opponent-view.compact {
  width: 68px;
}

.opponent-view.compact .opponent-bar {
  font-size: 0.52rem;
  padding: 3px 5px;
}

.opponent-view.compact .opponent-target {
  display: none;
}

.opponent-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 7px;
  font-size: 0.6rem;
  font-weight: 800;
  color: var(--text);
}

.opponent-bar .opponent-score {
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

.opponent-view canvas {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 2;
}

.opponent-target {
  padding: 3px 7px 5px;
  font-size: 0.55rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
  text-align: center;
}

/* ---------- room lobby & toasts (p2p rebuild) ---------- */

.lobby-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%) translateY(12px);
  bottom: calc(env(safe-area-inset-bottom, 0px) + 24px);
  z-index: 30;
  background: var(--panel);
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 700;
  padding: 10px 18px;
  border-radius: 999px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.toast.shown {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.mode-btn:disabled {
  opacity: 0.55;
  cursor: default;
}
