/*
 * Tetro — styles for the block-stacking puzzle game.
 * Companion to main.js. All selectors prefixed with `tg-` to
 * avoid collisions with the host page.
 */

/* Scoped reset so the game lays out the same on any host page,
   regardless of whether the host has its own box-sizing reset.
   Also disables text selection on every descendant — Firefox Mobile
   pops a "Search / Find in Page" toolbar the moment ANY selection
   takes hold (long-press anywhere, double-tap a text node, etc.),
   and the inherited rule on .tg-overlay alone isn't enough because
   some elements explicitly override user-select internally. */
.tg-overlay,
.tg-overlay * {
    box-sizing: border-box;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

.tg-overlay {
    /* Color palette + shared box-shadow patterns, scoped to the game. */
    --tg-fg: #0f380f;
    --tg-bg: #6b8a18;
    --tg-mid: #306230;
    --tg-shadow: 4px 4px 0 var(--tg-fg);
    --tg-shadow-sm: 3px 3px 0 var(--tg-mid);
    /* Shared cell size driving board + sidebar font sizes so they scale
       together. Both terms in the min() are derived from the actual
       layout so the cell saturates whichever dimension binds first.
         Width:  board content (10 cells × 2-char glyphs ≈ 12c) + board
                 padding/border (~28) + frame gap (~16) + sidebar
                 (~80px, stays roughly fixed at narrow widths because
                 the sidebar font is clamped to its 10px floor) +
                 overlay side padding (~8) ≈ 12c + 130.
         Height: 20c for the board + ~6c for the dpad (aspect-ratio 1
                 at half the frame width) = 26c. Chrome (overlay
                 padding + title + 2 stage gaps + board padding) is
                 ~120 on narrow viewports and ~260 on desktop because
                 most of those gaps are vw-clamped; `120px + 10vw`
                 interpolates between the two.
       dvh/dvw track the actual visible viewport, so on iOS the cell
       grows as the URL bar retracts (vh would lock in the larger,
       chrome-collapsed size and overflow when chrome is visible). */
    --tg-cell: clamp(10px, min(calc((100dvh - 120px - 10vw) / 26), calc((100dvw - 130px) / 12)), 24px);
    position: fixed;
    inset: 0;
    background: var(--tg-bg);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    align-items: center;
    /* `safe center` falls back to flex-start when content exceeds the
       viewport, so the top isn't clipped above the scrollable area. */
    justify-content: safe center;
    gap: clamp(12px, 2.5vw, 28px);
    /* Top/bottom padding gives breathing room and acts as a margin when
       content is scrolled to either edge. */
    padding: clamp(12px, 2vw, 24px) clamp(4px, 1.5vw, 16px);
    opacity: 0;
    transform: scale(1.05);
    pointer-events: none;
    transition: opacity 300ms ease, transform 300ms ease;
    font-family: "Lucida Console", "Lucida Sans Typewriter", "Courier New", Courier, monospace;
    overflow-y: auto;
    color: var(--tg-fg);
    /* Touch hardening: disable browser gestures so multi-finger presses
       on the D-pad / A / B can't trigger pinch-zoom, double-tap-zoom,
       pull-to-refresh, or page scroll. */
    touch-action: none;
    overscroll-behavior: contain;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}
.tg-overlay.is-visible {
    opacity: 1;
    transform: scale(1);
    pointer-events: auto;
}

/* Shared button styles. */
.tg-btn-outline {
    font: inherit;
    font-weight: bold;
    letter-spacing: 0.1em;
    color: var(--tg-fg);
    background: var(--tg-bg);
    border: 2px solid var(--tg-fg);
    text-shadow: 1px 1px 0 var(--tg-mid);
    box-shadow: var(--tg-shadow);
    cursor: pointer;
    transition: transform 60ms ease, box-shadow 60ms ease, background 120ms ease, color 120ms ease;
    -webkit-tap-highlight-color: transparent;
}
.tg-btn-outline:hover {
    background: var(--tg-fg);
    color: var(--tg-bg);
}
.tg-btn-outline:active,
.tg-btn-outline.is-pressed {
    transform: translate(4px, 4px);
    box-shadow: 0 0 0 var(--tg-fg);
}

.tg-btn-fill {
    font: inherit;
    font-weight: bold;
    color: var(--tg-bg);
    background: var(--tg-fg);
    border: none;
    box-shadow: var(--tg-shadow-sm);
    cursor: pointer;
    transition: transform 60ms ease, box-shadow 60ms ease, background 80ms ease;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-drag: none;
    /* `none` (not `manipulation`) so the browser never tries to
       interpret a press as scroll/zoom — required for reliable
       multi-touch button presses on iOS Safari. */
    touch-action: none;
    display: grid;
    place-items: center;
    padding: 0;
}
.tg-btn-fill:active,
.tg-btn-fill.is-pressed {
    transform: translate(3px, 3px);
    box-shadow: 0 0 0 var(--tg-mid);
    background: var(--tg-mid);
}

.tg-btn-outline:focus-visible,
.tg-btn-fill:focus-visible {
    outline: 2px dashed var(--tg-bg);
    outline-offset: 3px;
}

.tg-title {
    color: var(--tg-fg);
    text-decoration: none;
    -webkit-tap-highlight-color: transparent;
    font-size: clamp(14px, 2.4vw, 22px);
    font-weight: bold;
    text-shadow: 1px 1px 0 var(--tg-mid);
}

/* Top bar holds the X-close + title on a single row. Lives inside
   .tg-stage so `align-self: stretch` makes it span the stage's
   content width — which is the frame's intrinsic width (board +
   sidebar). That puts the absolutely-positioned X exactly at the
   board's left edge, with the title centered above the play area. */
.tg-top-bar {
    align-self: stretch;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: clamp(28px, 4vw, 40px);
}
.tg-close-x {
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    font-size: clamp(12px, 1.8vw, 18px);
    line-height: 1;
    padding: clamp(3px, 0.5vw, 6px) clamp(8px, 1vw, 12px);
    letter-spacing: 0;
}
.tg-close-x:active,
.tg-close-x.is-pressed {
    /* Override .tg-btn-outline's translate(4px,4px) so the
       translateY(-50%) keep-centered isn't lost when pressed. */
    transform: translate(4px, calc(-50% + 4px));
}

.tg-stage {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    /* Tightened from clamp(20px, 4vw, 40px) so the top-bar↔frame and
       frame↔controls spacing matches the overlay's edge padding — the
       old wider gap pushed the dpad off the bottom of shorter phones
       (iPhone SE etc.) after the top-bar moved inside the stage. */
    gap: clamp(10px, 2vw, 18px);
}

.tg-frame {
    position: relative;
    display: grid;
    grid-template-columns: auto auto;
    gap: clamp(10px, 1.6vw, 20px);
    align-items: start;
}

.tg-board {
    background: var(--tg-bg);
    color: var(--tg-fg);
    border: 2px solid var(--tg-fg);
    padding: clamp(8px, 1.4vw, 14px) clamp(10px, 1.5vw, 16px);
    box-shadow: var(--tg-shadow);
    text-shadow: 1px 1px 0 var(--tg-mid);
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    font-size: var(--tg-cell);
    line-height: 1;
    margin: 0;
    white-space: pre;
    letter-spacing: 0;
    user-select: none;
}
.tg-board.is-game-over,
.tg-board.is-paused {
    cursor: pointer;
}

.tg-board-wrap {
    position: relative;
    display: inline-block;
}
.tg-game-over-text {
    position: absolute;
    left: 50%;
    top: 36%;
    transform: translate(-50%, -50%);
    z-index: 5;
    font-family: inherit;
    /* Font derives from --tg-cell (the board's cell size) so the box
       stays proportional to the play area instead of drifting with
       viewport width. em-based padding scales with the font. */
    font-size: clamp(14px, calc(var(--tg-cell) * 0.9), 22px);
    font-weight: bold;
    line-height: 1.2;
    text-align: center;
    color: var(--tg-fg);
    text-shadow: 1px 1px 0 var(--tg-mid);
    background: var(--tg-bg);
    border: 2px solid var(--tg-fg);
    box-shadow: var(--tg-shadow);
    padding: 1.5em 3em;
    /* Cap horizontally so the box never overflows the play area, even at
       narrow boards where the em-based padding could otherwise push past
       the edges. (90% of .tg-board-wrap = 90% of the play area.) */
    max-width: 90%;
    pointer-events: none;
}
.tg-game-over-text[hidden] {
    display: none;
}
/* GAME OVER sits above center to leave room for the TRY AGAIN button;
   PAUSED has no companion button, so center it vertically. */
#tg-pause-text {
    top: 50%;
}
.tg-restart-btn {
    position: absolute;
    left: 50%;
    /* Below the (now-larger) GAME OVER box with a clear gap so they
       never touch, even at short viewports where the box is taller
       relative to the board. */
    top: 65%;
    transform: translate(-50%, -50%);
    z-index: 5;
    /* Match the QUIT/PAUSE button sizing (--tg-cell-driven). */
    font-size: clamp(10px, calc(var(--tg-cell) * 0.65), 16px);
    padding: clamp(4px, calc(var(--tg-cell) * 0.4), 10px) clamp(8px, calc(var(--tg-cell) * 0.6), 14px);
    white-space: nowrap;
}
.tg-restart-btn:active {
    transform: translate(calc(-50% + 4px), calc(-50% + 4px));
}
.tg-restart-btn[hidden] {
    display: none;
}

.tg-sidebar {
    display: flex;
    flex-direction: column;
    /* Sidebar gap and font scale with --tg-cell so the sidebar shrinks
       with the board when the viewport gets short. */
    gap: clamp(6px, calc(var(--tg-cell) * 0.55), 14px);
    font-size: clamp(10px, calc(var(--tg-cell) * 0.65), 16px);
}
.tg-stat {
    border: 2px solid var(--tg-fg);
    padding: clamp(3px, calc(var(--tg-cell) * 0.35), 8px) clamp(6px, calc(var(--tg-cell) * 0.55), 12px);
    box-shadow: var(--tg-shadow);
    display: flex;
    flex-direction: column;
    gap: clamp(2px, calc(var(--tg-cell) * 0.2), 4px);
}
.tg-stat-label {
    color: var(--tg-fg);
    font-size: 0.8em;
    font-weight: bold;
    letter-spacing: 0.15em;
    text-transform: uppercase;
    text-shadow: 1px 1px 0 var(--tg-mid);
}
.tg-stat-value {
    color: var(--tg-fg);
    font-size: 1.7em;
    line-height: 1;
    text-shadow: 1px 1px 0 var(--tg-mid);
    font-variant-numeric: tabular-nums;
}
.tg-next {
    color: var(--tg-fg);
    text-shadow: 1px 1px 0 var(--tg-mid);
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    font-size: clamp(11px, 1.8vw, 18px);
    line-height: 1;
    margin: 0;
    min-height: 2em;
    white-space: pre;
    user-select: none;
}

.tg-controls {
    align-self: stretch;
    /* width:0 + min-width:100% prevents the controls' intrinsic content
       width (dpad + A/B + gap) from inflating the stage past the frame.
       Stage sizes to the frame; controls then fills it. */
    width: 0;
    min-width: 100%;
    display: grid;
    /* Dpad column + A/B column each get ~50% of the controls' width;
       inside the A/B column, the two buttons flex equally for ~25% each. */
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    align-items: center;
    gap: clamp(16px, 2.5vw, 28px);
}
.tg-dpad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    aspect-ratio: 1;
    width: 100%;
    min-width: 0;
    gap: 0;
}
.tg-dpad-btn {
    width: 100%;
    height: 100%;
    font-size: clamp(16px, 3.5vw, 24px);
}
.tg-dpad-center {
    background: var(--tg-fg);
    grid-column: 2;
    grid-row: 2;
}
.tg-dpad-up { grid-column: 2; grid-row: 1; }
.tg-dpad-left { grid-column: 1; grid-row: 2; }
.tg-dpad-right { grid-column: 3; grid-row: 2; }
.tg-dpad-down { grid-column: 2; grid-row: 3; }

.tg-ab {
    width: 100%;
    display: flex;
    gap: clamp(8px, 1.5vw, 16px);
    transform: rotate(-18deg);
    align-items: center;
    justify-content: flex-end;
    /* Establishes a query container so .tg-ab-btn can size its label
       relative to its own (flex-grown) width via cqw units. */
    container-type: inline-size;
}
.tg-ab-btn {
    flex: 1 1 0;
    min-width: 0;
    aspect-ratio: 1;
    height: auto;
    border-radius: 50%;
    /* 15cqw ≈ 30% of one button's width (each button is ~half of .tg-ab),
       so the label scales with the button. */
    font-size: clamp(14px, 15cqw, 36px);
    text-shadow: 1px 1px 0 var(--tg-mid);
}

