/* TETBRIS — Tetris × Bricks */
:root {
  --bg: #0b0f1a;
  --bg-2: #0f1424;
  --panel: #131a2d;
  --panel-2: #1a2240;
  --ink: #e7ecf5;
  --ink-dim: #94a0bd;
  --line: #25304d;
  --accent: #00e5ff;
  --accent-2: #ff3ea5;
  --good: #5cff8a;
  --warn: #ffd166;
  --bad: #ff5577;
  --tc-bg: rgba(11,15,26,0.92);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
html, body {
  background: var(--bg); color: var(--ink);
  font-family: ui-monospace, "JetBrains Mono", "Fira Code", Menlo, Consolas, monospace;
  /* prevent pull-to-refresh & overscroll bounce on iOS */
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}
body {
  min-height: 100vh; min-height: 100dvh; /* dynamic viewport for iOS */
  display: flex; flex-direction: column; overflow-x: hidden;
  /* iOS safe areas */
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

button { font-family: inherit; touch-action: manipulation; }

/* subtle animated background */
.bg-glow {
  position: fixed; inset: 0; z-index: -1;
  background:
    radial-gradient(60vmax 60vmax at 10% 10%, rgba(0,229,255,0.10), transparent 60%),
    radial-gradient(50vmax 50vmax at 90% 90%, rgba(255,62,165,0.10), transparent 60%),
    linear-gradient(180deg, #0b0f1a 0%, #0a0e18 100%);
}
.bg-glow::after {
  content: ""; position: absolute; inset: 0;
  background-image: linear-gradient(rgba(255,255,255,0.03) 1px, transparent 1px),
                    linear-gradient(90deg, rgba(255,255,255,0.03) 1px, transparent 1px);
  background-size: 32px 32px;
  mask-image: radial-gradient(ellipse at center, black 40%, transparent 80%);
  -webkit-mask-image: radial-gradient(ellipse at center, black 40%, transparent 80%);
  animation: drift 30s linear infinite;
}
@keyframes drift { from { transform: translate(0,0); } to { transform: translate(32px, 32px); } }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: rgba(11,15,26,0.85); backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  position: sticky; top: 0; z-index: 5;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
.brand .logo { color: var(--accent); letter-spacing: 2px; font-size: 16px; }
.brand h1 { margin: 0; font-size: 18px; letter-spacing: 4px; color: var(--ink); }
.brand .brand-sub { color: var(--ink-dim); font-size: 10px; letter-spacing: 2px; }

.hud {
  display: flex; gap: 6px; flex-wrap: wrap; flex: 1; justify-content: center;
  min-width: 0;
}
.hud-item {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 4px 8px; min-width: 52px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 6px;
  flex: 0 0 auto;
}
.hud-item .label { font-size: 9px; letter-spacing: 1.5px; color: var(--ink-dim); }
.hud-item span:last-child { font-size: 14px; font-weight: 700; color: var(--ink); }
#lives { color: var(--bad); letter-spacing: 1px; }

.actions { display: flex; gap: 6px; flex: 0 0 auto; }
.btn {
  font: inherit; cursor: pointer;
  background: var(--panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 6px;
  padding: 8px 12px; letter-spacing: 1px;
  transition: transform .08s ease, background .15s ease, border-color .15s ease;
  /* comfortable touch target */
  min-height: 36px; min-width: 36px;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: #001218; border-color: var(--accent); font-weight: 700; }
.btn.primary:hover { background: #5ef6ff; border-color: #5ef6ff; }
.btn.ghost { background: transparent; }
.iconbtn { padding: 6px 10px; font-size: 16px; }

/* ============ Desktop layout (default 3-col) ============ */
.stage {
  display: grid; grid-template-columns: 220px 1fr 220px; gap: 14px;
  padding: 14px; flex: 1; align-items: start;
  min-height: 0;
}

.side { display: flex; flex-direction: column; gap: 12px; }
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px;
}
.panel h3 {
  margin: 0 0 8px 0; font-size: 11px; letter-spacing: 3px; color: var(--ink-dim);
}
.panel canvas { display: block; width: 100%; height: auto; image-rendering: pixelated; background: var(--bg-2); border: 1px solid var(--line); border-radius: 4px; }

.powerups ul, .inventory { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.powerups li {
  display: flex; align-items: center; gap: 8px; padding: 4px 6px;
  border: 1px solid var(--line); border-radius: 4px; font-size: 12px; color: var(--ink-dim);
}
.powerups li .ico { color: var(--accent); width: 14px; text-align: center; }
.inventory li {
  display: flex; align-items: center; gap: 6px; padding: 6px 8px;
  border: 1px solid var(--line); border-radius: 4px; font-size: 13px;
  background: var(--panel-2);
}
.inventory li .ico { color: var(--accent-2); width: 14px; text-align: center; }
.inventory li .key { color: var(--ink-dim); font-size: 11px; margin-left: auto; }
.inventory li.empty { color: var(--ink-dim); justify-content: center; font-style: italic; }
.inventory li.ready { border-color: var(--accent); box-shadow: 0 0 0 1px rgba(0,229,255,0.25) inset; }

.combo-meter { height: 8px; background: var(--bg-2); border: 1px solid var(--line); border-radius: 4px; overflow: hidden; }
.combo-meter > div { height: 100%; width: 0%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width .2s ease; }
.combo-text { text-align: center; margin-top: 6px; color: var(--ink-dim); font-size: 12px; }
.combo-text #combo { color: var(--accent); font-weight: 700; }

.scorelog { margin: 0; padding-left: 18px; font-size: 12px; color: var(--ink-dim); max-height: 140px; overflow: auto; }
.scorelog li { margin: 2px 0; }
.scorelog li.good { color: var(--good); }
.scorelog li.bad { color: var(--bad); }
.scorelog li.warn { color: var(--warn); }

.canvas-wrap {
  position: relative; justify-self: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px; box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  /* On desktop the canvas renders a 320x640 grid + 160px side panel = 480x640.
     We keep the CSS-driven width responsive but constrain aspect ratio. */
  width: 100%;
  max-width: 480px;
}
#game {
  display: block; width: 100%; height: auto; aspect-ratio: 3 / 4;
  background: #06090f; border-radius: 6px; outline: none; image-rendering: pixelated;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none; user-select: none;
}

.overlay {
  position: absolute; inset: 10px; border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(6,9,15,0.85); backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  transition: opacity .2s ease;
  z-index: 10;
}
.overlay.hidden { opacity: 0; pointer-events: none; }
.card {
  text-align: center; padding: 22px 26px; max-width: 360px; width: calc(100% - 24px);
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px;
}
.card h2 { margin: 0 0 8px 0; font-size: 24px; letter-spacing: 6px; color: var(--accent); }
.card p { color: var(--ink-dim); font-size: 13px; line-height: 1.6; }
.overlay-actions { margin: 14px 0 6px; display: flex; gap: 8px; justify-content: center; }
.howto { text-align: left; margin-top: 10px; font-size: 12px; color: var(--ink-dim); }
.howto summary { cursor: pointer; color: var(--accent); }
.howto ul { padding-left: 18px; }
.howto li { margin: 2px 0; }
.howto .tip { color: var(--warn); margin-top: 6px; }

/* ============ Mobile touch controls (hidden on desktop) ============ */
.touch-controls { display: none; }

/* ============ Mobile / tablet layout (default for <= 980px) ============ */
@media (max-width: 980px) {
  /* Switch body to a single scrollable column. The previous non-scrolling
     flex column forced everything to fit in one viewport, which was
     impossible on iPhone 13 (390x844) once the side panel row + canvas
     + touch bar were all stacked. Now we let the page scroll naturally
     and pin the touch bar to the bottom with position:fixed. */
  html, body { height: auto; min-height: 100dvh; }
  body { overflow-y: auto; overflow-x: hidden; }

  .stage { grid-template-columns: 1fr; padding: 6px; gap: 6px; flex: 0 0 auto; }
  .touch-controls { display: flex; }
  .canvas-wrap { padding: 4px; max-width: 100%; }
  .topbar { padding: 6px 8px; gap: 4px; }
  .brand h1 { font-size: 16px; letter-spacing: 2px; }
  .brand-sub { display: none; }
  .hud { gap: 3px; }
  .hud-item { min-width: 44px; padding: 3px 5px; }
  .hud-item .label { font-size: 8px; letter-spacing: 1px; }
  .hud-item span:last-child { font-size: 12px; }
  .btn.iconbtn { padding: 6px 8px; font-size: 14px; min-height: 36px; min-width: 36px; }
  .actions .kbd-only { display: none; }

  /* Side panels: compact horizontal strip. Use the LEFT side only
     (Next/Hold/Active FX) and hide the right side (Combo/Inventory/
     Power-ups list/Log) — they are duplicated in the HUD and the FX
     panel. This keeps the strip narrow. */
  .side {
    display: flex;
    flex-direction: row;
    gap: 6px;
    overflow-x: auto;
    padding: 4px 0;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .side::-webkit-scrollbar { display: none; }
  .side > .panel { flex: 0 0 auto; min-width: 92px; padding: 5px 7px; }
  .side > .panel h3 { font-size: 9px; letter-spacing: 2px; margin-bottom: 3px; }
  .side > .panel canvas { width: 56px; height: 56px; }
  /* Hide right-side (Combo/Inventory/Power-ups list/Log) on mobile.
     IMPORTANT: this rule must come AFTER .side { display:flex } so
     it wins, otherwise the more general selector overrides it. */
  .side.side-right { display: none; }
  .inventory { font-size: 11px; }
  .inventory li { padding: 3px 5px; font-size: 11px; }
  .combo-meter { height: 6px; }
  .combo-text { font-size: 10px; }
  .scorelog { display: none; }
  .footer { display: none; }
}

@media (max-width: 600px) {
  .hud { gap: 2px; }
  .hud-item { min-width: 38px; padding: 2px 3px; }
  .hud-item .label { font-size: 7px; }
  .hud-item span:last-child { font-size: 11px; }
  /* hide lines HUD on tiny screens — keep score/level/lives */
  .hud-lines { display: none; }
  .topbar { padding: 4px 6px; }
  .brand h1 { font-size: 14px; letter-spacing: 1px; }
  .brand .logo { font-size: 12px; }
  .actions { gap: 3px; }
  .btn.iconbtn { padding: 4px 6px; min-height: 32px; min-width: 32px; font-size: 13px; }
}

/* ============ Touch controls styling ============ */
.touch-controls {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  background: var(--tc-bg); backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
  user-select: none; -webkit-user-select: none;
  gap: 10px;
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  justify-content: space-between;
  align-items: center;
  flex-wrap: nowrap;
}
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-template-areas:
    ".  up  ."
    "left .  right"
    ".  down .";
  gap: 4px;
  width: 168px;
  height: 168px;
  flex: 0 0 auto;
}
.dpad .tc-up    { grid-area: up; }
.dpad .tc-left  { grid-area: left; }
.dpad .tc-down  { grid-area: down; }
.dpad .tc-right { grid-area: right; }

.tcd-right {
  display: grid;
  grid-template-rows: 1fr 1fr;
  grid-template-columns: 1fr;
  gap: 6px;
  flex: 1;
  min-width: 0;
  margin-left: 0;
  align-self: stretch;
  height: 168px;
}

.tc-btn {
  background: var(--panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 8px;
  font-size: 18px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  min-height: 44px; min-width: 44px;
  padding: 0;
  cursor: pointer;
  transition: transform 0.05s, background 0.1s, border-color 0.1s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  font-family: inherit;
  width: 100%;
  height: 100%;
}
.tc-btn:active {
  transform: scale(0.95);
  background: var(--panel-2);
  border-color: var(--accent);
}
.tc-hold, .tc-hard {
  font-size: 14px; letter-spacing: 1px;
}
.tc-hold { background: var(--panel-2); }
.tc-hard { background: var(--accent); color: #001218; border-color: var(--accent); }

@media (max-width: 380px) {
  .dpad { width: 140px; height: 140px; }
  .tcd-right { height: 140px; }
  .tc-btn { font-size: 16px; }
  .tc-hold, .tc-hard { font-size: 11px; }
}

/* ===== iPhone-class fit-to-viewport (≤ 480px width) =====
   The goal: make the entire game (topbar + side strip + canvas + touch
   bar) fit in ONE viewport on iPhone 13 / 14 / 15 (390x844–430x932).
   Strategy:
     1. Shrink the topbar to a single tight row.
     2. Replace the side panels row with an inline mini-ribbon merged
        into the topbar (so it doesn't add vertical space).
     3. Cap the canvas height so it leaves room for the touch bar
        below.
     4. Use viewport-based units for the touch bar so the D-pad is
        big but not overflowing. */
@media (max-width: 480px) {
  /* Reserve room for the fixed touch bar so the canvas isn't hidden. */
  body { padding-bottom: 150px; }

  /* Stack topbar in two thin rows: brand+actions on top, HUD+ribbon
     below, keeping everything inside ~100px. */
  .topbar {
    flex-direction: column;
    align-items: stretch;
    padding: 4px 6px;
    gap: 2px;
  }
  .topbar > .brand {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
  }
  .topbar > .actions { display: flex; }
  .topbar > .hud { display: flex; }
  .topbar > .hud .hud-lines { display: none; }
  /* Move the side-left strip into the topbar by making it inline. */
  .stage { padding: 4px; gap: 4px; }
  .side-left {
    display: flex;
    flex-direction: row;
    gap: 4px;
    overflow-x: auto;
    scrollbar-width: none;
    padding: 0;
  }
  .side-left::-webkit-scrollbar { display: none; }
  .side-left > .panel { flex: 0 0 auto; min-width: 76px; padding: 4px 6px; }
  .side-left > .panel h3 {
    font-size: 8px;
    letter-spacing: 1.5px;
    margin: 0 0 2px 0;
  }
  .side-left > .panel canvas { width: 48px; height: 48px; }

  /* Cap canvas height so the whole game fits in one viewport. The
     canvas keeps its 3:4 aspect ratio, but we shrink its width so the
     resulting height leaves room for the fixed touch bar (~150px). */
  .canvas-wrap { max-width: 100%; padding: 4px; }
  #game {
    /* Use viewport-based height so we know it'll fit. The wrapper
       gets a max-height in vh. */
    width: auto;
    height: auto;
    max-width: 100%;
  }
  /* Force the wrap to be tall enough for the canvas at 3:4 ratio
     without exceeding the viewport. We compute:
       viewport - topbar - side - touch bar - safe areas.
     On iPhone 13/14/15, 100dvh ≈ 844-932px, topbar+side ~ 130px,
     touch bar ~ 150px → canvas height ~ 560-650px.
     canvas width = 3/4 * height.
     The CSS clamp keeps it bounded. */
  .canvas-wrap {
    max-height: calc(100dvh - 320px);
  }
  #game { max-height: calc(100dvh - 340px); }
  .footer { display: none; }
}

/* iPhone 13/14/15 (390-430px) — tighten touch bar so it stays inside
   the viewport with the keyboard hint area, and compact D-pad a bit. */
@media (max-width: 430px) {
  .touch-controls {
    padding: 6px 8px;
    gap: 6px;
  }
  .dpad {
    width: 132px;
    height: 132px;
    gap: 3px;
  }
  .tcd-right { height: 132px; gap: 4px; }
  .tc-btn { font-size: 15px; }
  .tc-hold, .tc-hard { font-size: 12px; letter-spacing: 0.5px; }
}

/* Show touch controls also on touch-enabled desktop.
   IMPORTANT: only do this if the viewport is small enough that the
   3-column desktop layout would be cramped (< 1180px), so big touch
   monitors still get the 3-column layout with D-pad below. */
@media (hover: none) and (max-width: 1180px) {
  .touch-controls { display: flex; }
  .stage { grid-template-columns: 1fr; }
}

/* On landscape phones: hide side panels, single-column stage. The
   touch bar is position:fixed and overlays the bottom, so it doesn't
   need its own grid area. */
@media (max-width: 980px) and (orientation: landscape) {
  .stage { grid-template-columns: 1fr; }
  .side { display: none; }
}

.footer {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 12px; padding-bottom: max(6px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  font-size: 11px; color: var(--ink-dim);
  background: rgba(11,15,26,0.7);
}
@media (max-width: 600px) { .kbd-hint { display: none; } .footer { justify-content: center; } }
kbd, code { background: var(--panel-2); padding: 1px 4px; border-radius: 3px; border: 1px solid var(--line); }

/* iOS specific: prevent text selection on long press of game elements */
.topbar, .actions, .hud, .touch-controls, .panel, .canvas-wrap, .overlay, .card {
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}

/* When lives HUD is shown on mobile, give it emphasis (line-height + size) */
@media (max-width: 980px) {
  .hud-lives { display: flex; }
  .hud-lines { display: none; }
  #lives { font-size: 13px !important; }
}
