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

:root {
  --bg:          #0e0e10;
  --surface:     #18181b;
  --border:      #2d2d2f;
  --accent:      #9147ff;
  --accent-light:#bf94ff;
  --text:        #efeff1;
  --text-dim:    #adadb8;
  --success:     #00c5a5;
  --cell:        56px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, sans-serif;
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ── Stream ──────────────────────────────────────────── */
#stream-section {
  flex: 0 0 42vh;
  background: #000;
  position: relative;
}

#stream-frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

/* ── Bottom row ──────────────────────────────────────── */
#bottom-section {
  flex: 1;
  display: flex;
  overflow: hidden;
  border-top: 2px solid var(--accent);
  min-height: 0;
}

/* ── Game ────────────────────────────────────────────── */
#game-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px;
  gap: 6px;
  overflow: hidden;
  position: relative;
  min-width: 0;
}

#game-hud {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  padding: 2px 4px;
  font-size: 13px;
  flex-shrink: 0;
}

#channel-name {
  font-weight: 700;
  font-size: 14px;
  color: var(--accent-light);
}

#score-label { color: var(--text-dim); }

#score {
  font-weight: 700;
  color: var(--accent-light);
  transition: transform 0.15s;
}

#score.bump {
  animation: score-bump 0.25s ease;
}

@keyframes score-bump {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.5); color: #ffd700; }
  100% { transform: scale(1); }
}

#status-msg {
  color: var(--success);
  font-size: 12px;
  font-weight: 600;
  opacity: 0;
  transition: opacity 0.4s;
}

.toggle-btn {
  padding: 4px 10px;
  background: var(--surface);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.toggle-btn:hover { background: var(--border); color: var(--text); }

.toggle-btn.active {
  background: rgba(145, 71, 255, 0.18);
  color: var(--accent-light);
  border-color: rgba(145, 71, 255, 0.5);
}

#toggle-stream { margin-left: auto; }

#reset-game {
  padding: 4px 10px;
  background: var(--surface);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}

#reset-game:hover { background: var(--border); color: var(--text); }

#connect-twitch {
  padding: 4px 10px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}

#connect-twitch:hover { background: #7b34e0; }

#game-board {
  display: grid;
  gap: 2px;
  flex-shrink: 0;
}

/* ── Cells ───────────────────────────────────────────── */
.game-cell {
  --tile-glow: #9147ff; /* overridden per-tile by JS */
  width: var(--cell);
  height: var(--cell);
  border-radius: 6px;
  background: var(--surface); /* overridden per-tile by JS */
  border: 1px solid var(--border); /* overridden per-tile by JS */
  cursor: grab;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  transition: transform 0.12s, opacity 0.12s, filter 0.12s;
}

.game-cell:active { cursor: grabbing; }

.game-cell img {
  width: 76%;
  height: 76%;
  object-fit: contain;
  pointer-events: none;
  display: block;
  filter: drop-shadow(0 1px 3px rgba(0,0,0,0.5));
}

.game-cell:hover:not(.dragging):not(.selected) {
  transform: scale(1.06);
  filter: brightness(1.15);
  box-shadow: 0 0 10px var(--tile-glow);
}

/* Selected — continuous glow pulse using the tile's own color */
.game-cell.selected {
  z-index: 2;
  overflow: visible;
  animation: selected-pulse 0.85s ease-in-out infinite;
}

@keyframes selected-pulse {
  0%, 100% { transform: scale(1.07); box-shadow: 0 0 10px var(--tile-glow), 0 0 20px color-mix(in srgb, var(--tile-glow) 40%, transparent); }
  50%       { transform: scale(1.13); box-shadow: 0 0 18px var(--tile-glow), 0 0 36px color-mix(in srgb, var(--tile-glow) 55%, transparent); }
}

/* Dragging state — source cell fades out */
.game-cell.dragging {
  opacity: 0.2;
  transform: scale(0.85) !important;
  cursor: grabbing;
}

/* Target cell highlight during drag — transform is controlled by JS (nudge preview) */
.game-cell.drag-over {
  filter: brightness(1.25);
  box-shadow: 0 0 18px var(--tile-glow);
  overflow: visible;
}

/* ── Floating drag ghost ─────────────────────────────── */
.drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 200;
  border-radius: 8px;
  border: 2px solid var(--tile-glow, var(--accent));
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 12px 32px rgba(0,0,0,0.65), 0 0 24px var(--tile-glow, var(--accent));
  transform: scale(1.25) rotate(4deg);
  cursor: grabbing;
}

.drag-ghost img {
  width: 76%;
  height: 76%;
  object-fit: contain;
  pointer-events: none;
  display: block;
}

/* ── Invalid swap shake ──────────────────────────────── */
.game-cell.shake {
  animation: shake 0.4s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0)   rotate(0deg); }
  15%       { transform: translateX(-8px) rotate(-4deg); }
  30%       { transform: translateX(8px)  rotate(4deg); }
  45%       { transform: translateX(-6px) rotate(-2deg); }
  60%       { transform: translateX(6px)  rotate(2deg); }
  75%       { transform: translateX(-3px); }
  90%       { transform: translateX(3px); }
}

/* ── Match pop ───────────────────────────────────────── */
/*
 * overflow stays 'hidden' (inherited from .game-cell) — this is intentional.
 * Previously we used overflow:visible + a large box-shadow which bled onto
 * adjacent non-matched cells and made them look like they were part of the
 * match. Now we use filter:drop-shadow which is visually contained.
 */
.game-cell.matched {
  animation: match-pop 0.38s ease forwards;
  pointer-events: none;
  z-index: 5;
}

@keyframes match-pop {
  0%   { transform: scale(1);    opacity: 1; filter: drop-shadow(0 0 4px var(--tile-glow)); }
  30%  { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(0);    opacity: 0; filter: none; }
}

/* ── Hint pulse — gold flash ─────────────────────────── */
.game-cell.hint {
  overflow: visible;
  animation: hint-pulse 0.6s ease-in-out 3;
}

@keyframes hint-pulse {
  0%, 100% { transform: scale(1);    box-shadow: none; }
  50%       { transform: scale(1.14); box-shadow: 0 0 18px #ffd700, 0 0 36px rgba(255,215,0,0.4);
              filter: brightness(1.25); }
}

/* ── Existing tiles falling to fill gaps after matches ── */
.game-cell.falling {
  animation: fall-down 0.38s cubic-bezier(0.22, 0.61, 0.36, 1);
}

@keyframes fall-down {
  0%   { transform: translateY(calc(-1 * var(--fall-rows) * (var(--cell) + 2px))); }
  65%  { transform: translateY(calc(var(--fall-rows) * 1.5px)); }
  100% { transform: translateY(0); }
}

/* ── Drop-in with bounce (new pieces after cascade) ──── */
.game-cell.new {
  animation: drop-in 0.42s cubic-bezier(0.22, 0.61, 0.36, 1);
}

@keyframes drop-in {
  0%   { transform: translateY(-52px) scale(0.65); opacity: 0; }
  55%  { transform: translateY(5px)   scale(1.06); opacity: 1; }
  75%  { transform: translateY(-3px)  scale(0.97); }
  100% { transform: translateY(0)     scale(1);    opacity: 1; }
}

/* ── Board entrance — bouncy pop with stagger ────────── */
.game-cell.entrance {
  animation: entrance 0.45s cubic-bezier(0.22, 0.61, 0.36, 1) both;
}

@keyframes entrance {
  0%   { transform: scale(0)    rotate(-15deg); opacity: 0; }
  60%  { transform: scale(1.12) rotate(3deg);   opacity: 1; }
  80%  { transform: scale(0.94) rotate(-1deg); }
  100% { transform: scale(1)    rotate(0deg);  opacity: 1; }
}

/* ── Floating score numbers ──────────────────────────── */
.score-float {
  position: fixed;
  pointer-events: none;
  font-family: 'Inter', system-ui, sans-serif;
  font-weight: 800;
  font-size: 15px;
  color: var(--success);
  text-shadow: 0 0 8px currentColor, 0 1px 6px rgba(0,0,0,0.7);
  z-index: 300;
  transform: translateX(-50%);
  animation: float-up 1.1s cubic-bezier(0.22, 0.61, 0.36, 1) forwards;
  white-space: nowrap;
}

@keyframes float-up {
  0%   { transform: translateX(-50%) translateY(0)     scale(0.6); opacity: 0; }
  18%  { transform: translateX(-50%) translateY(-10px) scale(1.2); opacity: 1; }
  100% { transform: translateX(-50%) translateY(-72px) scale(0.85); opacity: 0; }
}

/* ── Combo screen flash (disabled) ──────────────────── */
#combo-flash { display: none; }

/* ── Loading overlay ─────────────────────────────────── */
#loading-overlay {
  position: absolute;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 10;
  transition: opacity 0.5s;
}

#loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

#loading-text { color: var(--text-dim); font-size: 14px; }

/* ── Chat ────────────────────────────────────────────── */
#chat-section {
  flex: 0 0 320px;
  border-left: 2px solid var(--border);
  display: flex;
  overflow: hidden;
}

#chat-frame {
  width: 100%;
  height: 100%;
  flex: 1;
}

/* ── Responsive ──────────────────────────────────────── */
@media (max-width: 900px) {
  #bottom-section { flex-direction: column-reverse; }

  #chat-section {
    flex: 0 0 180px;
    border-left: none;
    border-bottom: 2px solid var(--border);
  }

  #stream-section { flex: 0 0 35vh; }
}
