/* ===== ROOT VARIABLES ===== */
:root {
  --bg-deep: #0a0e27;
  --bg-mid: #111638;
  --bg-panel: rgba(16, 20, 52, 0.85);
  --glow-cyan: #4cc9f0;
  --glow-cyan-dim: rgba(76, 201, 240, 0.25);
  --glow-cyan-bright: rgba(76, 201, 240, 0.7);
  --glow-green: #2ee89a;
  --glow-red: #ff4d6d;
  --text-primary: #e8ecf4;
  --text-secondary: rgba(200, 210, 230, 0.6);
  --piece-1: #ff4d6d;
  --piece-2: #ff8c42;
  --piece-3: #ffd166;
  --piece-4: #06d6a0;
  --piece-5: #4cc9f0;
  --piece-6: #7b61ff;
  --piece-7: #f72585;
  --piece-8: #4361ee;
  --node-size: 92px;
  --piece-size: 76px;
  --font-display: 'Fredoka', 'Nunito', sans-serif;
  --font-body: 'Nunito', 'Segoe UI', sans-serif;
  --bubble-accent: #f0c050;
  --bubble-accent-glow: rgba(240, 192, 80, 0.5);
}

/* ===== RESET & BASE ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  overflow: hidden;
  touch-action: manipulation;
}

body {
  font-family: var(--font-body);
  background: var(--bg-deep);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
}

/* ===== CANVASES ===== */
#bg-canvas,
#confetti-canvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

#bg-canvas {
  z-index: 0;
}

#confetti-canvas {
  z-index: 500;
}

/* ===== GAME WRAPPER ===== */
#game-wrapper {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 1000px;
  height: 100vh;
  height: 100dvh;
  padding: 20px;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr auto auto;
  gap: 10px;
  overflow: visible;
  min-height: 0;
  transition: padding 0.35s cubic-bezier(0.34, 1.3, 0.64, 1),
              transform 0.35s cubic-bezier(0.34, 1.3, 0.64, 1);
}

/* When the hint panel is up, slide the whole board area to the right
   so the panel has room without overlapping the game. The transform is
   tied to .panel-open (panel visibility) rather than .hint-locked (input
   lock) so the board stays shifted while a lingering finale panel is
   still on screen. */
#game-wrapper.panel-open {
  transform: translateX(clamp(60px, 9vw, 130px));
}

/* ===== HINT BUTTON (upper left) ===== */
#hint-btn {
  position: fixed;
  top: clamp(14px, 2.4vh, 26px);
  left: clamp(14px, 2.4vw, 26px);
  width: clamp(58px, 7vw, 76px);
  height: clamp(58px, 7vw, 76px);
  border: none;
  border-radius: 50%;
  background: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
  -webkit-tap-highlight-color: transparent;
  padding: 0;
}

#hint-btn img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  display: block;
}

#hint-btn:hover { transform: scale(1.06); }
#hint-btn:active { transform: scale(0.94); }

#hint-btn.active img {
  filter: drop-shadow(0 0 14px rgba(255, 209, 102, 0.8));
}

/* ===== HOME BUTTON (upper right) ===== */
#home-btn {
  position: fixed;
  top: clamp(14px, 2.4vh, 26px);
  right: clamp(14px, 2.4vw, 26px);
  width: clamp(44px, 5.5vw, 56px);
  height: clamp(44px, 5.5vw, 56px);
  border-radius: 50%;
  background: rgba(76, 201, 240, 0.08);
  border: 1px solid rgba(76, 201, 240, 0.25);
  color: rgba(76, 201, 240, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  text-decoration: none;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), background 0.2s ease, color 0.2s ease;
  box-shadow:
    0 2px 10px rgba(0, 0, 0, 0.25),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
  -webkit-tap-highlight-color: transparent;
}

#home-btn svg {
  width: 56%;
  height: 56%;
}

#home-btn:hover {
  transform: scale(1.08);
  background: rgba(76, 201, 240, 0.18);
  color: var(--glow-cyan);
  box-shadow: 0 0 16px var(--glow-cyan-dim);
}

#home-btn:active { transform: scale(0.94); }

/* ===== HINT PANEL (left pane) ===== */
#hint-panel {
  position: fixed;
  top: clamp(100px, 13vh, 140px);
  left: clamp(14px, 2.4vw, 26px);
  width: min(300px, calc(50vw - 40px));
  max-width: 320px;
  opacity: 0;
  transform: translateY(-6px) scale(0.96);
  transform-origin: top left;
  pointer-events: none;
  z-index: 25;
  transition: opacity 0.3s ease, transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
  background:
    radial-gradient(ellipse 80% 60% at 30% 10%, rgba(255, 209, 102, 0.06) 0%, transparent 60%),
    rgba(14, 14, 30, 0.96);
  border-radius: 14px;
  border: 1px solid rgba(255, 209, 102, 0.28);
  box-shadow:
    0 14px 40px rgba(0, 0, 0, 0.5),
    0 4px 12px rgba(0, 0, 0, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.04);
  backdrop-filter: blur(16px) saturate(1.3);
  -webkit-backdrop-filter: blur(16px) saturate(1.3);
  padding: clamp(18px, 2.6vw, 24px) clamp(20px, 2.8vw, 26px);
  color: var(--text-primary);
}

#hint-panel.show {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

#hint-close {
  position: absolute;
  top: 6px;
  right: 10px;
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.35);
  font-size: clamp(1.15rem, 1.8vw, 1.35rem);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  line-height: 1;
  transition: all 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

#hint-close:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.08);
}

#hint-text {
  font-family: var(--font-body);
  font-size: clamp(0.95rem, 1.4vw, 1.08rem);
  font-weight: 600;
  line-height: 1.45;
  color: var(--text-primary);
  margin: 0 0 14px 0;
}

#hint-text strong { color: #ffd166; }

#hint-text .hint-avoid {
  font-style: italic;
  font-weight: 800;
  color: var(--text-primary);
  padding-right: 0.18em;
}

#hint-feedback {
  font-family: var(--font-body);
  font-size: clamp(0.95rem, 1.35vw, 1.06rem);
  font-weight: 700;
  color: var(--glow-green);
  margin: 10px 0 0 0;
  line-height: 1.4;
}

#hint-feedback.wrong {
  color: var(--glow-red);
}

#hint-feedback .impossible-tag {
  font-weight: 800;
  color: #ff7a8a;
  letter-spacing: 0.02em;
}

#hint-feedback .impossible-tag em {
  font-style: italic;
  font-weight: 800;
  background: linear-gradient(135deg, #ff8c66 0%, #ff5e7e 50%, #c64dff 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 0 16px rgba(255, 94, 126, 0.35);
}

/* Celebratory finale message — replaces the question + feedback when the
   student answers the final puzzle correctly. */
#hint-feedback.hint-feedback-finale {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 8px;
  margin: 4px 0 6px;
  padding: 14px 10px 12px;
  background: radial-gradient(ellipse at top, rgba(255, 209, 102, 0.12), transparent 70%);
  border-top: 1px solid rgba(255, 209, 102, 0.22);
  border-bottom: 1px solid rgba(255, 209, 102, 0.22);
  animation: finaleEnter 0.55s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}

#hint-feedback.hint-feedback-finale .finale-bingo {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(2rem, 4.2vw, 2.7rem);
  background: linear-gradient(135deg, #ffe082 0%, #ffd166 50%, #ffb84d 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: #ffd166;
  letter-spacing: 0.01em;
  text-shadow: 0 0 22px rgba(255, 209, 102, 0.45);
  animation: finalePopBingo 0.7s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}

#hint-feedback.hint-feedback-finale .finale-line {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(1rem, 1.6vw, 1.18rem);
  color: #ffe9a8;
  line-height: 1.35;
  animation: finaleEnter 0.55s ease-out 0.18s backwards;
}

#hint-feedback.hint-feedback-finale .finale-line.finale-cta {
  font-weight: 700;
  color: #fff8de;
  font-size: clamp(1.05rem, 1.75vw, 1.28rem);
  margin-top: 2px;
  animation-delay: 0.32s;
}

@keyframes finaleEnter {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes finalePopBingo {
  0%   { opacity: 0; transform: scale(0.5) rotate(-6deg); }
  60%  { opacity: 1; transform: scale(1.12) rotate(2deg); }
  100% { opacity: 1; transform: scale(1) rotate(0); }
}

#hint-choice-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}

.hint-choice {
  flex: 1 1 auto;
  min-width: 54px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1.5px solid rgba(255, 209, 102, 0.45);
  background: rgba(255, 209, 102, 0.08);
  color: #ffe9a8;
  font-family: var(--font-body);
  font-size: clamp(0.95rem, 1.35vw, 1.05rem);
  font-weight: 700;
  cursor: pointer;
  transition: all 0.18s ease;
  -webkit-tap-highlight-color: transparent;
}

.hint-choice:hover {
  background: rgba(255, 209, 102, 0.18);
  border-color: rgba(255, 209, 102, 0.8);
  transform: translateY(-1px);
}

.hint-choice:active { transform: translateY(0) scale(0.97); }

.hint-choice.correct {
  background: rgba(46, 232, 154, 0.22);
  border-color: var(--glow-green);
  color: #c3ffe1;
}

.hint-choice.wrong {
  background: rgba(255, 77, 109, 0.22);
  border-color: var(--glow-red);
  color: #ffd6de;
}

.hint-choice.disabled {
  pointer-events: none;
  opacity: 0.55;
}

#hint-integer-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
}

#hint-integer-input {
  flex: 0 1 70px;
  width: 70px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1.5px solid rgba(255, 209, 102, 0.45);
  background: rgba(255, 209, 102, 0.08);
  color: #fff8de;
  font-family: var(--font-body);
  font-size: clamp(1rem, 1.5vw, 1.15rem);
  font-weight: 700;
  text-align: center;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  -moz-appearance: textfield;
}

#hint-integer-input::-webkit-outer-spin-button,
#hint-integer-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

#hint-integer-input:focus {
  border-color: rgba(255, 209, 102, 0.9);
  box-shadow: 0 0 12px rgba(255, 209, 102, 0.25);
}

#hint-integer-submit {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 10px;
  border: 1.5px solid rgba(76, 201, 240, 0.4);
  background: rgba(76, 201, 240, 0.12);
  color: var(--glow-cyan);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

#hint-integer-submit svg {
  width: 22px;
  height: 22px;
}

#hint-integer-submit:hover {
  background: rgba(76, 201, 240, 0.22);
  border-color: var(--glow-cyan-bright);
}

#hint-integer-submit:active { transform: scale(0.94); }

#hint-integer-pair-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}

#hint-integer-pair-row input {
  flex: 0 1 60px;
  width: 60px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1.5px solid rgba(255, 209, 102, 0.45);
  background: rgba(255, 209, 102, 0.08);
  color: #fff8de;
  font-family: var(--font-body);
  font-size: clamp(1rem, 1.5vw, 1.15rem);
  font-weight: 700;
  text-align: center;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  -moz-appearance: textfield;
}

#hint-integer-pair-row input::-webkit-outer-spin-button,
#hint-integer-pair-row input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

#hint-integer-pair-row input:focus {
  border-color: rgba(255, 209, 102, 0.9);
  box-shadow: 0 0 12px rgba(255, 209, 102, 0.25);
}

#hint-integer-pair-row .hint-and {
  font-family: var(--font-body);
  font-size: 0.95rem;
  font-weight: 700;
  color: rgba(255, 209, 102, 0.75);
}

#hint-integer-pair-submit {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 10px;
  border: 1.5px solid rgba(76, 201, 240, 0.4);
  background: rgba(76, 201, 240, 0.12);
  color: var(--glow-cyan);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

#hint-integer-pair-submit svg {
  width: 22px;
  height: 22px;
}

#hint-integer-pair-submit:hover {
  background: rgba(76, 201, 240, 0.22);
  border-color: var(--glow-cyan-bright);
}

#hint-integer-pair-submit:active { transform: scale(0.94); }

#hint-next {
  display: flex;
  align-items: center;
  justify-content: center;
  width: fit-content;
  min-width: 64px;
  margin: clamp(10px, 1.6vw, 14px) auto 0;
  padding: clamp(6px, 0.9vw, 9px) clamp(24px, 2.8vw, 34px);
  border-radius: 8px;
  border: 1px solid rgba(255, 209, 102, 0.4);
  background: rgba(255, 209, 102, 0.08);
  color: #ffd166;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.15s ease, box-shadow 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

#hint-next:hover {
  background: rgba(255, 209, 102, 0.18);
  border-color: rgba(255, 209, 102, 0.7);
  box-shadow: 0 0 14px rgba(255, 209, 102, 0.22);
}

#hint-next:active { transform: scale(0.96); }

#hint-next svg {
  width: clamp(22px, 2.6vw, 28px);
  height: clamp(22px, 2.6vw, 28px);
}

.hidden { display: none !important; }

/* ===== BOTTOM CONTROLS ===== */
#bottom-controls {
  grid-column: 1;
  grid-row: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding-bottom: 4px;
}

#reset-btn {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1.5px solid rgba(76, 201, 240, 0.25);
  background: rgba(76, 201, 240, 0.08);
  color: var(--glow-cyan);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.25s ease;
  padding: 0;
}

#reset-btn:hover {
  background: rgba(76, 201, 240, 0.18);
  border-color: var(--glow-cyan-bright);
  box-shadow: 0 0 14px var(--glow-cyan-dim);
}

#reset-btn:active { transform: scale(0.92); }

#reset-btn .reset-icon {
  width: 20px;
  height: 20px;
}

.submit-btn {
  padding: 8px 22px;
  border-radius: 10px;
  border: 1.5px solid rgba(46, 232, 154, 0.4);
  background: rgba(46, 232, 154, 0.1);
  color: var(--glow-green);
  font-family: var(--font-body);
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.25s ease;
  letter-spacing: 0.03em;
}

.submit-btn:hover {
  background: rgba(46, 232, 154, 0.2);
  border-color: rgba(46, 232, 154, 0.7);
  box-shadow: 0 0 14px rgba(46, 232, 154, 0.22);
}

.submit-btn:active { transform: scale(0.94); }

/* Lock pieces/nodes during hint mode */
#game-wrapper.hint-locked .piece,
#game-wrapper.hint-locked .node {
  pointer-events: none;
}

#game-wrapper.hint-locked .piece {
  opacity: 0.55;
  filter: saturate(0.6);
}

#game-wrapper.hint-locked .node .piece {
  opacity: 1;
  filter: none;
}

#game-wrapper.hint-locked #submit-btn,
#game-wrapper.hint-locked #reset-btn {
  opacity: 0.45;
  pointer-events: none;
}

/* Hint-focus / highlight styles */
.node.hint-focus {
  border: 3px solid #ffd166 !important;
  box-shadow:
    0 0 28px rgba(255, 209, 102, 0.55),
    inset 0 0 18px rgba(255, 209, 102, 0.15) !important;
  animation: hintFocusPulse 1.4s ease-in-out infinite !important;
  z-index: 3;
}

.node.hint-flash-big {
  animation: hintBigFlash 0.45s ease 4 !important;
  border-color: #ffd166 !important;
  box-shadow: 0 0 30px rgba(255, 209, 102, 0.65) !important;
  z-index: 3;
}

/* Two numbers each trying to enter the same node — "but not both" moment.
   Overlap VERTICALLY inside the highlighted node, with a little separation
   so both digits remain readable. */
.piece.hint-half-piece {
  position: absolute;
  left: 50%;
  top: 50%;
  pointer-events: none;
  cursor: default;
  z-index: 15;
  animation: hintHalfEnter 0.38s cubic-bezier(.34, 1.56, .64, 1) backwards;
  transition: none;
}

.piece.hint-half-piece.hint-half-top {
  transform: translate(-50%, -88%);
}

.piece.hint-half-piece.hint-half-bottom {
  transform: translate(-50%, -12%);
}

.piece.hint-half-piece:hover { filter: none; }
.piece.hint-half-piece.hint-half-top:hover    { transform: translate(-50%, -88%); }
.piece.hint-half-piece.hint-half-bottom:hover { transform: translate(-50%, -12%); }

/* While a number is "halfway into" a board node during the hint, hide its
   twin from the tray so it doesn't look like the same number is in two places.
   Uses !important because the `hint-locked .piece` rule has higher specificity. */
.piece.hint-tray-hidden {
  opacity: 0 !important;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

@keyframes hintHalfEnter {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* Pulsing question mark placeholder inside a node (after removing the rejected piece). */
.hint-question-mark {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 2.4rem;
  color: #ffd166;
  text-shadow:
    0 0 14px rgba(255, 209, 102, 0.75),
    0 0 28px rgba(255, 209, 102, 0.4);
  pointer-events: none;
  z-index: 12;
  animation: hintQuestionPulse 1.1s ease-in-out infinite;
}

@keyframes hintQuestionPulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1);    opacity: 0.9; }
  50%      { transform: translate(-50%, -50%) scale(1.15); opacity: 1;   }
}

/* Persistent highlight on the NOT-CONNECTED node — stays glowing the whole hint. */
.node.hint-highlight-persist {
  border: 3px solid #ff8c42 !important;
  box-shadow:
    0 0 28px rgba(255, 140, 66, 0.65),
    inset 0 0 18px rgba(255, 140, 66, 0.18) !important;
  animation: hintHighlightPersist 1.6s ease-in-out infinite !important;
  z-index: 3;
}

@keyframes hintHighlightPersist {
  0%, 100% {
    box-shadow:
      0 0 22px rgba(255, 140, 66, 0.5),
      inset 0 0 14px rgba(255, 140, 66, 0.14);
  }
  50% {
    box-shadow:
      0 0 40px rgba(255, 140, 66, 0.9),
      inset 0 0 22px rgba(255, 140, 66, 0.22);
  }
}

/* Sequential "tour" of connections: one edge + its remote circle lights up
   at a time, then fades. Used on step 0 to illustrate that the center spot
   has lots of connections. */
.edge-line.hint-edge-sequence {
  stroke: #ffd166 !important;
  stroke-width: 4.5 !important;
  opacity: 1 !important;
  animation: hintEdgeSequenceLine 0.7s ease-out both;
}

.edge-glow.hint-edge-sequence-glow {
  stroke: #ffd166 !important;
  opacity: 0.7 !important;
  animation: hintEdgeSequenceGlow 0.7s ease-out both;
}

@keyframes hintEdgeSequenceLine {
  0%   { opacity: 0.2; stroke-width: 2.5; }
  35%  { opacity: 1;   stroke-width: 5.5; }
  100% { opacity: 0;   stroke-width: 3; }
}

@keyframes hintEdgeSequenceGlow {
  0%   { opacity: 0.1; }
  40%  { opacity: 0.8; }
  100% { opacity: 0; }
}

.node.hint-connection-remote {
  border: 3px solid #ffd166 !important;
  box-shadow:
    0 0 32px rgba(255, 209, 102, 0.75),
    inset 0 0 18px rgba(255, 209, 102, 0.2) !important;
  animation: hintConnectionRemote 0.7s ease-out both;
  z-index: 3;
}

@keyframes hintConnectionRemote {
  0%   {
    box-shadow:
      0 0 10px rgba(255, 209, 102, 0.2),
      inset 0 0 6px rgba(255, 209, 102, 0.05);
  }
  40%  {
    box-shadow:
      0 0 38px rgba(255, 209, 102, 0.95),
      inset 0 0 22px rgba(255, 209, 102, 0.25);
  }
  100% {
    box-shadow:
      0 0 10px rgba(255, 209, 102, 0.15),
      inset 0 0 6px rgba(255, 209, 102, 0.05);
  }
}

/* Gentle pink pulse on the edges that CONNECT to the focused node */
.edge-line.hint-pulse-connect {
  stroke: #ff5e9c !important;
  opacity: 0.85 !important;
  stroke-width: 3.5 !important;
  animation: hintPulseEdgeLine 1.8s ease-in-out infinite !important;
}

.edge-glow.hint-pulse-connect-glow {
  stroke: #ff5e9c !important;
  opacity: 0.35 !important;
  animation: hintPulseEdgeGlow 1.8s ease-in-out infinite !important;
}

@keyframes hintPulseEdgeLine {
  0%, 100% { opacity: 0.55; stroke-width: 3; }
  50%      { opacity: 0.95; stroke-width: 4.5; }
}

@keyframes hintPulseEdgeGlow {
  0%, 100% { opacity: 0.18; }
  50%      { opacity: 0.5; }
}

@keyframes hintFocusPulse {
  0%, 100% {
    box-shadow:
      0 0 22px rgba(255, 209, 102, 0.45),
      inset 0 0 14px rgba(255, 209, 102, 0.12);
  }
  50% {
    box-shadow:
      0 0 38px rgba(255, 209, 102, 0.8),
      inset 0 0 22px rgba(255, 209, 102, 0.2);
  }
}

@keyframes hintBigFlash {
  0%, 100% {
    box-shadow: 0 0 18px rgba(255, 209, 102, 0.3);
    transform: translate(-50%, -50%) scale(1);
  }
  50% {
    box-shadow: 0 0 45px rgba(255, 209, 102, 0.85);
    transform: translate(-50%, -50%) scale(1.12);
  }
}

/* ===== BOARD AREA ===== */
#board-area {
  position: relative;
  grid-column: 1;
  grid-row: 1;
  min-height: 0;
  background: transparent;
  border-radius: 20px;
  border: none;
  box-shadow: none;
  overflow: visible;
}

#bottom-controls {
  grid-row: 3;
}

/* ===== SVG CONNECTIONS ===== */
#connections {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* Glow layer */
.edge-glow {
  stroke: var(--glow-cyan);
  stroke-width: 14;
  stroke-linecap: round;
  opacity: 0.1;
  filter: url(#glow);
  vector-effect: non-scaling-stroke;
  transition: stroke 0.4s ease, opacity 0.4s ease;
}

/* Main line */
.edge-line {
  stroke: var(--glow-cyan);
  stroke-width: 2.5;
  stroke-linecap: round;
  opacity: 0.5;
  vector-effect: non-scaling-stroke;
  transition: stroke 0.4s ease, opacity 0.4s ease, stroke-width 0.3s ease;
}

/* Edge states */
.edge-line.valid {
  stroke: var(--glow-green);
  opacity: 0.65;
}
.edge-glow.valid-glow {
  stroke: var(--glow-green);
  opacity: 0.12;
}

.edge-line.conflict {
  stroke: var(--glow-red);
  opacity: 0.9;
  stroke-width: 3.5;
  animation: conflictPulse 0.8s ease-in-out infinite;
}
.edge-glow.conflict-glow {
  stroke: var(--glow-red);
  opacity: 0.25;
  animation: conflictPulse 0.8s ease-in-out infinite;
}

.edge-line.victory {
  stroke: var(--glow-green);
  opacity: 1;
  stroke-width: 4;
}
.edge-glow.victory-glow {
  stroke: var(--glow-green);
  opacity: 0.35;
}

/* Subtle flow animation on idle lines */
.edge-line {
  stroke-dasharray: 1000;
  stroke-dashoffset: 0;
}

/* ===== NODES ===== */
#nodes-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.node {
  position: absolute;
  width: var(--node-size);
  height: var(--node-size);
  transform: translate(-50%, -50%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: box-shadow 0.3s ease, border-color 0.3s ease, background 0.3s ease;
  background: radial-gradient(circle at 35% 35%, rgba(76, 201, 240, 0.08), rgba(10, 14, 39, 0.9));
  border: 2.5px solid rgba(76, 201, 240, 0.25);
  box-shadow:
    0 0 18px rgba(76, 201, 240, 0.12),
    inset 0 0 12px rgba(76, 201, 240, 0.06);
  animation: nodePulse 3s ease-in-out infinite, nodeEnter 0.5s ease-out backwards;
  z-index: 2;
}

.node.tutorial-pulse {
  border: 4px solid #00d4ff;
  box-shadow: 0 0 16px rgba(0, 212, 255, 0.6), inset 0 0 8px rgba(0, 212, 255, 0.15);
  animation: none;
}

.node:nth-child(1) { animation-delay: 0.05s, 0.1s; }
.node:nth-child(2) { animation-delay: 0.1s, 0.15s; }
.node:nth-child(3) { animation-delay: 0.15s, 0.2s; }
.node:nth-child(4) { animation-delay: 0.2s, 0.25s; }
.node:nth-child(5) { animation-delay: 0.25s, 0.3s; }
.node:nth-child(6) { animation-delay: 0.3s, 0.35s; }
.node:nth-child(7) { animation-delay: 0.35s, 0.4s; }
.node:nth-child(8) { animation-delay: 0.4s, 0.45s; }

.node::after {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  border: 1px solid rgba(76, 201, 240, 0.08);
  animation: nodeRipple 3s ease-in-out infinite;
  pointer-events: none;
}

.node:hover {
  border-color: rgba(76, 201, 240, 0.5);
  box-shadow:
    0 0 25px rgba(76, 201, 240, 0.25),
    inset 0 0 15px rgba(76, 201, 240, 0.1);
}

.node.drop-target {
  border-color: rgba(76, 201, 240, 0.4);
  box-shadow: 0 0 22px rgba(76, 201, 240, 0.2);
}

.node.hover-target {
  border-color: rgba(76, 201, 240, 0.8);
  box-shadow:
    0 0 30px rgba(76, 201, 240, 0.4),
    inset 0 0 20px rgba(76, 201, 240, 0.15);
  transform: translate(-50%, -50%) scale(1.08);
}

.node.filled {
  animation: none;
  background: radial-gradient(circle at 35% 35%,
    color-mix(in srgb, var(--piece-color) 15%, transparent),
    rgba(10, 14, 39, 0.85));
  border-color: color-mix(in srgb, var(--piece-color) 50%, transparent);
  box-shadow:
    0 0 22px color-mix(in srgb, var(--piece-color) 30%, transparent),
    inset 0 0 15px color-mix(in srgb, var(--piece-color) 10%, transparent);
}

.node.filled::after {
  animation: none;
  opacity: 0;
}

.node.conflict {
  /* Only the connection lines go red — circles stay their normal color.
     Keep a tiny shake for feedback but no red border/glow. */
  animation: conflictShake 0.4s ease;
}

.node.victory-node {
  animation: victoryNodeGlow 1s ease forwards;
}

/* ===== PIECES ===== */
.piece {
  width: var(--piece-size);
  height: var(--piece-size);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.7rem;
  color: #fff;
  cursor: grab;
  position: relative;
  touch-action: none;
  transition: transform 0.2s ease, box-shadow 0.2s ease, filter 0.2s ease;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  z-index: 10;
}

.piece.tutorial-pulse {
  animation: selectedPulse 1.2s ease-in-out infinite;
}

/* Piece color theming */
.piece[data-value="1"] {
  background: linear-gradient(145deg, #ff6b85, #e8405f);
  box-shadow: 0 0 16px rgba(255, 77, 109, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);
}
.piece[data-value="2"] {
  background: linear-gradient(145deg, #ffa25e, #e87830);
  box-shadow: 0 0 16px rgba(255, 140, 66, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);
}
.piece[data-value="3"] {
  background: linear-gradient(145deg, #ffe082, #f0be30);
  box-shadow: 0 0 16px rgba(255, 209, 102, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);
  color: #5a4800;
  text-shadow: 0 1px 2px rgba(255, 255, 255, 0.3);
}
.piece[data-value="4"] {
  background: linear-gradient(145deg, #34edb5, #05b880);
  box-shadow: 0 0 16px rgba(6, 214, 160, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);
}
.piece[data-value="5"] {
  background: linear-gradient(145deg, #6dd5f5, #2ea8d8);
  box-shadow: 0 0 16px rgba(76, 201, 240, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);
}
.piece[data-value="6"] {
  background: linear-gradient(145deg, #9b82ff, #6344e8);
  box-shadow: 0 0 16px rgba(123, 97, 255, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);
}
.piece[data-value="7"] {
  background: linear-gradient(145deg, #ff4da0, #d01a6b);
  box-shadow: 0 0 16px rgba(247, 37, 133, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);
}
.piece[data-value="8"] {
  background: linear-gradient(145deg, #6882ff, #3550d0);
  box-shadow: 0 0 16px rgba(67, 97, 238, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);
}

/* Piece states */
.piece:hover {
  transform: scale(1.1);
  filter: brightness(1.12);
}

.piece.selected {
  transform: scale(1.15);
  filter: brightness(1.15);
  animation: selectedPulse 0.9s ease-in-out infinite;
  z-index: 20;
  outline: 3px solid rgba(255, 255, 255, 0.9);
  outline-offset: 3px;
}

.piece.lifting {
  transform: scale(1.08);
}

.piece.dragging {
  cursor: grabbing;
  transform: scale(1.18);
  filter: brightness(1.15);
  z-index: 1000;
  opacity: 0.92;
}

.piece.placing {
  animation: placeBounce 0.35s ease;
}

.piece.on-board {
  cursor: pointer;
}

/* Staggered entrance */
.piece.entering {
  animation: pieceEnter 0.45s ease-out backwards;
}

/* ===== TRAY AREA (horizontal, below board) ===== */
#tray-area {
  grid-column: 1;
  grid-row: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  overflow-x: auto;
  overflow-y: hidden;
}

/* ===== PIECES TRAY ===== */
#pieces-tray {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  gap: 6px;
  padding: 10px;
  background: var(--bg-panel);
  border-radius: 16px;
  border: 1px solid rgba(76, 201, 240, 0.1);
  box-shadow: inset 0 0 30px rgba(0, 0, 0, 0.2);
  flex-wrap: nowrap;
  flex-shrink: 0;
}

.tray-slot {
  width: var(--piece-size);
  height: var(--piece-size);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.3s ease;
}

.tray-slot:not(:has(.piece)) {
  border: 2px solid transparent;
  background: transparent;
}

.tray-slot:has(.piece) {
  border: 2px solid transparent;
}

/* ===== (controls removed – buttons now in toolbar) ===== */

/* ===== MODALS ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 7, 20, 0.85);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
}

.modal-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.modal-content,
.victory-content {
  background: linear-gradient(160deg, #141838, #0e1230);
  border: 1px solid rgba(76, 201, 240, 0.2);
  border-radius: 20px;
  padding: 32px 36px;
  max-width: 380px;
  width: 90%;
  text-align: center;
  box-shadow: 0 0 60px rgba(76, 201, 240, 0.1);
  transform: scale(0.9) translateY(20px);
  transition: transform 0.35s ease;
}


/* ===== INSTRUCTION BOARD OVERRIDES ===== */
#instr-stage {
  background: color-mix(in srgb, var(--bg-mid) 95%, black);
}

#instr-board {
  margin-bottom: clamp(20px, 3.5vw, 28px);
}

/* warning text-transform handled by shared instructions.css */

/* (bubble class removed — using shared label system) */

/* ── Red X on illegal edge ── */
.instr-x-mark {
  position: absolute;
  font-size: clamp(1.4rem, 4vw, 2.2rem);
  font-weight: 900;
  color: #ff4d6d;
  text-shadow: 0 0 12px rgba(255, 77, 109, 0.6);
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 8;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.instr-x-mark.show {
  opacity: 1;
}

#instr-edges {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.instr-edge-line {
  stroke: rgba(76, 201, 240, 0.35);
  stroke-width: 1.5;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
  transition: stroke 0.4s ease, opacity 0.4s ease;
}

.instr-edge-line.conflict {
  stroke: var(--glow-red);
  stroke-width: 2.5;
  vector-effect: non-scaling-stroke;
  animation: conflictPulse 0.8s ease-in-out infinite;
}

.instr-edge-line.valid {
  stroke: var(--glow-green);
  opacity: 0.7;
}

/* ── Mini Nodes ── */
.instr-node {
  position: absolute;
  width: clamp(26px, 5.5vw, 38px);
  height: clamp(26px, 5.5vw, 38px);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle at 35% 35%, rgba(76, 201, 240, 0.08), rgba(10, 14, 39, 0.9));
  border: 2px solid rgba(76, 201, 240, 0.25);
  box-shadow: 0 0 10px rgba(76, 201, 240, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.4s ease;
  z-index: 2;
  pointer-events: none;
}

.instr-node.glow {
  border-color: rgba(76, 201, 240, 0.7);
  box-shadow: 0 0 20px rgba(76, 201, 240, 0.35), inset 0 0 10px rgba(76, 201, 240, 0.1);
}

.instr-node.filled {
  background: radial-gradient(circle at 35% 35%,
    color-mix(in srgb, var(--piece-color) 15%, transparent),
    rgba(10, 14, 39, 0.85));
  border-color: color-mix(in srgb, var(--piece-color) 50%, transparent);
  box-shadow: 0 0 14px color-mix(in srgb, var(--piece-color) 30%, transparent);
}

.instr-node.conflict {
  border-color: rgba(255, 77, 109, 0.7);
  box-shadow: 0 0 16px rgba(255, 77, 109, 0.3);
}

/* ── Mini Pieces ── */
.instr-piece {
  position: absolute;
  width: clamp(28px, 6vw, 42px);
  height: clamp(28px, 6vw, 42px);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Fredoka', 'Nunito', sans-serif;
  font-weight: 600;
  font-size: clamp(0.85rem, 2.2vw, 1.2rem);
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  pointer-events: none;
  z-index: 5;
  opacity: 0;
  transform: translate(-50%, -50%);
  transition: left 0.5s cubic-bezier(0.4, 0, 0.2, 1),
              top 0.5s cubic-bezier(0.4, 0, 0.2, 1),
              opacity 0.3s ease;
}

.instr-piece.selected {
  outline: 2.5px solid rgba(255, 255, 255, 0.85);
  outline-offset: 2px;
  filter: brightness(1.15);
}

@keyframes instrShake {
  0%   { transform: translate(-50%, -50%) rotate(0deg); }
  12%  { transform: translate(calc(-50% + 6px), -50%) rotate(3deg); }
  24%  { transform: translate(calc(-50% - 5px), -50%) rotate(-2.5deg); }
  36%  { transform: translate(calc(-50% + 4px), -50%) rotate(2deg); }
  48%  { transform: translate(calc(-50% - 3px), -50%) rotate(-1.2deg); }
  60%  { transform: translate(calc(-50% + 2px), -50%) rotate(0.5deg); }
  75%  { transform: translate(calc(-50% - 1px), -50%) rotate(-0.2deg); }
  100% { transform: translate(-50%, -50%) rotate(0deg); }
}

.instr-piece.spring-rattle {
  animation: instrShake 0.45s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}

/* ── Mini Tray ── */
.instr-tray {
  position: absolute;
  bottom: -22%;
  left: 5%;
  right: 5%;
  /* Match the piece size with a comfortable margin so the numbers
     sit inside the tray instead of poking out the top/bottom. */
  height: clamp(46px, 8vw, 60px);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: var(--bg-panel, rgba(16, 20, 52, 0.85));
  border-radius: 10px;
  border: 1px solid rgba(76, 201, 240, 0.1);
  box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.2);
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
  z-index: 1;
}

.instr-tray.show {
  opacity: 1;
}

.modal-overlay.show .modal-content,
.modal-overlay.show .victory-content {
  transform: scale(1) translateY(0);
}

.modal-content h2,
.victory-title {
  font-family: var(--font-display);
  font-size: 2rem;
  color: var(--glow-cyan);
  text-shadow: 0 0 15px rgba(76, 201, 240, 0.3);
  margin-bottom: 16px;
}


.modal-btn {
  margin-top: 18px;
  padding: 10px 32px;
  border: 2px solid var(--glow-cyan);
  border-radius: 12px;
  background: rgba(76, 201, 240, 0.1);
  color: var(--glow-cyan);
  font-family: var(--font-body);
  font-size: 1.15rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.25s ease;
}

.modal-btn:hover {
  background: rgba(76, 201, 240, 0.22);
  box-shadow: 0 0 20px var(--glow-cyan-dim);
}

/* ===== VICTORY ACTION ROW ===== */
.victory-actions {
  margin-top: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.victory-actions .modal-btn { margin-top: 0; }

#victory-home-btn {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: rgba(76, 201, 240, 0.08);
  border: 2px solid rgba(76, 201, 240, 0.4);
  color: var(--glow-cyan);
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1),
              background 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

#victory-home-btn svg {
  width: 58%;
  height: 58%;
}

#victory-home-btn:hover {
  transform: scale(1.06);
  background: rgba(76, 201, 240, 0.22);
  box-shadow: 0 0 18px var(--glow-cyan-dim);
}

#victory-home-btn:active { transform: scale(0.94); }

/* ===== VICTORY ===== */
.victory-icon {
  font-size: 4rem;
  margin-bottom: 8px;
  animation: victoryStarSpin 1s ease;
}

.victory-title {
  font-size: 2.35rem;
  color: #fff;
  text-shadow: 0 0 20px rgba(46, 232, 154, 0.5);
}

.victory-sub {
  color: var(--text-secondary);
  font-size: 1.1rem;
  margin-bottom: 4px;
}

/* ===== KEYFRAME ANIMATIONS ===== */
@keyframes nodePulse {
  0%, 100% {
    box-shadow:
      0 0 18px rgba(76, 201, 240, 0.12),
      inset 0 0 12px rgba(76, 201, 240, 0.06);
    border-color: rgba(76, 201, 240, 0.22);
  }
  50% {
    box-shadow:
      0 0 25px rgba(76, 201, 240, 0.2),
      inset 0 0 18px rgba(76, 201, 240, 0.1);
    border-color: rgba(76, 201, 240, 0.35);
  }
}

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

@keyframes nodeRipple {
  0%, 100% { transform: scale(1); opacity: 0.15; }
  50% { transform: scale(1.12); opacity: 0; }
}

@keyframes conflictPulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

@keyframes conflictShake {
  0%, 100% { transform: translate(-50%, -50%) translateX(0); }
  20% { transform: translate(-50%, -50%) translateX(-4px); }
  40% { transform: translate(-50%, -50%) translateX(4px); }
  60% { transform: translate(-50%, -50%) translateX(-3px); }
  80% { transform: translate(-50%, -50%) translateX(2px); }
}

@keyframes selectedPulse {
  0%, 100% {
    transform: scale(1.15);
    filter: brightness(1.15);
  }
  50% {
    transform: scale(1.08);
    filter: brightness(1.05);
  }
}

@keyframes placeBounce {
  0% { transform: scale(0.7); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

@keyframes pieceEnter {
  from {
    opacity: 0;
    transform: translateY(18px) scale(0.7);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes victoryStarSpin {
  0% { transform: scale(0) rotate(-180deg); }
  60% { transform: scale(1.2) rotate(15deg); }
  100% { transform: scale(1) rotate(0); }
}

@keyframes victoryNodeGlow {
  0% { box-shadow: 0 0 15px rgba(46, 232, 154, 0.2); }
  50% { box-shadow: 0 0 35px rgba(46, 232, 154, 0.6); }
  100% { box-shadow: 0 0 22px rgba(46, 232, 154, 0.3); }
}

/* Hint flash */
.node.hint-flash {
  animation: hintFlash 0.4s ease 3;
}

.edge-line.hint-edge {
  stroke: #ffd166;
  opacity: 1;
  stroke-width: 4;
  animation: hintEdgeFlash 0.4s ease 3;
}

.edge-glow.hint-edge-glow {
  stroke: #ffd166;
  opacity: 0.35;
  animation: hintEdgeFlash 0.4s ease 3;
}

@keyframes hintFlash {
  0%, 100% { box-shadow: 0 0 18px rgba(255, 209, 102, 0.1); border-color: rgba(255, 209, 102, 0.3); }
  50% { box-shadow: 0 0 30px rgba(255, 209, 102, 0.6); border-color: rgba(255, 209, 102, 0.9); }
}

@keyframes hintEdgeFlash {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 1; }
}

@keyframes victoryEdgeWave {
  0% { stroke-width: 2.5; opacity: 0.5; }
  50% { stroke-width: 5; opacity: 1; }
  100% { stroke-width: 3.5; opacity: 0.85; }
}

/* ===== RESPONSIVE ===== */
@media (max-width: 600px) {
  :root {
    --node-size: 70px;
    --piece-size: 58px;
  }

  #game-wrapper {
    padding: 10px 8px 10px;
    gap: 10px;
  }

  #pieces-tray {
    gap: 4px;
    padding: 8px;
  }

  .piece {
    font-size: 1.2rem;
  }

  #board-area {
    border-radius: 14px;
  }

  .tool-btn {
    width: 34px;
    height: 34px;
  }

  .tool-btn svg {
    width: 17px;
    height: 17px;
  }

  .modal-content,
  .victory-content {
    padding: 24px 20px;
  }
}

@media (max-width: 380px) {
  :root {
    --node-size: 58px;
    --piece-size: 48px;
  }

  .piece {
    font-size: 1.05rem;
  }

  #pieces-tray {
    gap: 3px;
    padding: 8px;
  }
}

@media (max-height: 700px) {
  :root {
    --piece-size: 52px;
    --node-size: 64px;
  }

  #pieces-tray {
    gap: 3px;
    padding: 6px;
  }

  .piece { font-size: 1.1rem; }

}

@media (max-height: 560px) {
  :root {
    --piece-size: 40px;
    --node-size: 52px;
  }

  .piece { font-size: 0.95rem; }

}

/* ===== LAYOUT VARIANTS ===== */



/* ===== MINIMUM SCREEN SIZE GATE ===== */
#screen-too-small {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: rgba(5, 7, 20, 0.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
}

#screen-too-small p {
  font-family: var(--font-display);
  font-size: 1.3rem;
  color: var(--text-primary);
}

@media (max-width: 359px), (max-height: 499px) {
  #screen-too-small {
    display: flex;
  }
}

/* ── Mobile (≤640px): rotate the bowtie board 90° so it sits
   landscape-style across the top half of the phone, then lay the
   number tray out as a compact 4×2 grid below it. The hint panel
   slides down from the top; the hint lightbulb tucks into the
   top-left so it doesn't collide with the feedback button. ───── */
@media (max-width: 640px) {
  :root {
    --node-size: 56px;
    --piece-size: 46px;
  }

  /* Hint lightbulb: top-left, smaller. Keeps it away from the
     bottom-right feedback launcher. */
  #hint-btn {
    top: 14px;
    bottom: auto;
    left: 14px;
    right: auto;
    width: 50px;
    height: 50px;
  }

  /* Hint panel slides down from the very top, full width */
  #hint-panel {
    top: 8px;
    left: 8px;
    right: 8px;
    width: auto;
    max-width: none;
    max-height: 40vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    transform-origin: top center;
  }

  /* Don't slide the wrapper sideways when the hint opens — there isn't
     room on mobile. */
  #game-wrapper.panel-open { transform: none; }

  /* Wrapper: leave room at top for the hint area */
  #game-wrapper {
    padding: clamp(60px, 10vh, 80px) 8px 8px;
    gap: 8px;
    grid-template-rows: auto auto auto;
  }

  /* Board area: reserve a landscape strip across the top half of
     the screen for the rotated bowtie. The actual rotation happens
     on the children below — board-area itself is a landscape box
     of width 96vw × height 64vw (i.e. 3:2 landscape). */
  #board-area {
    width: 96vw;
    height: 64vw;
    aspect-ratio: auto;
    min-height: 0;
    margin: 0 auto;
    position: relative;
    overflow: visible;
  }

  /* Rotate the SVG connections and the absolutely-positioned nodes
     90° counter-clockwise. We give each child explicit *portrait*
     dimensions (height × width swapped) so that after the 90°
     rotation their visual bounding box matches the landscape
     parent. preserveAspectRatio="none" on the SVG handles the
     stretch, and percentage-positioned nodes rotate with the box.
     Drag-and-drop still works because getBoundingClientRect()
     returns the post-transform visual rect. */
  #connections,
  #nodes-container {
    position: absolute;
    width: 64vw;   /* parent's height — becomes width after rotation */
    height: 96vw;  /* parent's width  — becomes height after rotation */
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(-90deg);
    transform-origin: center center;
  }

  /* Tray: compact 4×2 grid below the board so it never spills off
     the screen. */
  #pieces-tray {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    justify-items: center;
    max-width: 92vw;
    width: 100%;
    margin: 0 auto;
    padding: 8px;
  }
  #tray-area { overflow-x: visible; overflow-y: visible; }

  /* Tighter bottom controls */
  #bottom-controls {
    padding-bottom: 2px;
    gap: 8px;
  }
}

/* Even tighter on very small phones */
@media (max-width: 380px) {
  :root {
    --node-size: 48px;
    --piece-size: 38px;
  }
}

/* ── Orientation-aware gate messages (mobile rotation) ── */
.gate-rotate,
.gate-small {
  display: none;
}
@media (max-width: 479px) {
  .gate-small { display: block; }
}
@media (max-height: 499px) and (min-width: 480px) {
  .gate-rotate { display: block; }
}
