* { box-sizing: border-box; }

:root {
    --panel: rgba(15, 23, 39, .94);
    --panel-2: rgba(26, 37, 58, .96);
    --text: #f7f9ff;
    --muted: #aab4c8;
    --accent: #7fe5ff;
    --board-max: 760px;
    --gap: clamp(1px, .32vw, 4px);
}

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    color: var(--text);
    font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background:
        radial-gradient(circle at 15% 8%, #243b61 0, transparent 30%),
        radial-gradient(circle at 88% 18%, #1d4351 0, transparent 26%),
        #09101d;
    overscroll-behavior-y: contain;
}

button, select { font: inherit; }

.app-shell {
    width: min(1180px, calc(100% - 24px));
    margin: 0 auto;
    padding: clamp(12px, 2.2vw, 24px) 0 36px;
}

.topbar {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 18px;
    margin-bottom: 18px;
}

h1 {
    margin: 0 0 4px;
    font-size: clamp(1.8rem, 5vw, 3rem);
    line-height: 1;
}

.topbar p { margin: 0; }
p { color: var(--muted); }

.settings {
    display: flex;
    flex-wrap: wrap;
    align-items: end;
    gap: 9px;
}

.settings label {
    display: grid;
    gap: 5px;
    font-size: .8rem;
    color: var(--muted);
}

select, button {
    min-height: 42px;
    border: 1px solid rgba(255,255,255,.12);
    border-radius: 12px;
    color: var(--text);
    background: #151f33;
    padding: 9px 12px;
}

button {
    cursor: pointer;
    background: linear-gradient(135deg, #35bedb, #5c7cff);
    border: 0;
    font-weight: 800;
}

.game-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 290px;
    gap: 18px;
    align-items: start;
}

.game-card, .help-card {
    border: 1px solid rgba(255,255,255,.09);
    border-radius: 20px;
    background: var(--panel);
    box-shadow: 0 18px 60px rgba(0,0,0,.3);
}

.game-card { position: relative; padding: clamp(10px, 2vw, 16px); overflow: hidden; }
.help-card { padding: 20px; }
.help-card h2, .help-card h3 { margin-top: 0; }
.help-card li { margin-bottom: 9px; color: #c9d2e4; }


.countdown {
    position: absolute;
    top: clamp(8px, 1.5vw, 14px);
    right: clamp(8px, 1.5vw, 14px);
    z-index: 40;
    display: flex;
    align-items: center;
    gap: 8px;
    height: 50px;
    padding: 4px 12px 4px 5px;
    border: 1px solid rgba(255,255,255,.16);
    border-radius: 15px;
    background: rgba(8, 16, 29, .84);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    box-shadow: 0 5px 16px rgba(0,0,0,.22);
}

.timer-icon-wrap {
    position: relative;
    width: 42px;
    height: 42px;
    flex: 0 0 42px;
    display: grid;
    place-items: center;
}

.timer-ring {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    transform: rotate(-90deg);
    overflow: visible;
}

.timer-ring-track,
.timer-ring-progress {
    fill: none;
    stroke-width: 3;
}

.timer-ring-track {
    stroke: rgba(255,255,255,.12);
}

.timer-ring-progress {
    stroke: var(--accent);
    stroke-linecap: round;
    stroke-dasharray: 119.38;
    stroke-dashoffset: 0;
    transition: stroke-dashoffset .25s linear, opacity .2s ease;
}

.hourglass-icon {
    position: relative;
    z-index: 1;
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
    color: #eef7ff;
}

.timer-text {
    min-width: 3.15ch;
    font-size: 1.18rem;
    line-height: 1;
    font-weight: 800;
    letter-spacing: .02em;
    font-variant-numeric: tabular-nums;
}

.countdown.time-low {
    border-color: rgba(255,255,255,.28);
}

.countdown.time-over .timer-ring-progress {
    opacity: 0;
}

.statusbar {
    padding-right: 112px;
}

@media (max-width: 620px) {
    .countdown {
        position: relative;
        top: auto;
        right: auto;
        width: fit-content;
        margin: 0 0 10px auto;
        height: 44px;
        border-radius: 14px;
        padding-right: 10px;
    }

    .timer-icon-wrap {
        width: 36px;
        height: 36px;
        flex-basis: 36px;
    }

    .hourglass-icon { width: 19px; height: 19px; }
    .timer-text { font-size: 1.05rem; }
    .statusbar { padding-right: 0; }
}

.statusbar {
    display: flex;
    gap: 10px 18px;
    flex-wrap: wrap;
    justify-content: center;
    color: #dce5f7;
    margin-bottom: 10px;
    font-size: clamp(.78rem, 2.8vw, .95rem);
}

.material-reservoir {
    position: relative;
    width: min(100%, var(--board-max));
    height: clamp(76px, 14vw, 112px);
    margin: 0 auto -1px;
    border: 2px solid rgba(255,255,255,.17);
    border-bottom: 0;
    border-radius: 18px 18px 0 0;
    overflow: hidden;
    background: #0a1425;
}

#materialCanvas { width: 100%; height: 100%; display: block; }

.reservoir-label {
    position: absolute;
    left: 11px;
    top: 9px;
    color: rgba(255,255,255,.78);
    font-size: .74rem;
    text-shadow: 0 1px 4px rgba(0,0,0,.8);
    pointer-events: none;
}

.game-grid {
    --size: 10;
    display: grid;
    grid-template-columns: repeat(var(--size), 1fr);
    grid-template-rows: repeat(var(--size), 1fr);
    gap: var(--gap);
    width: min(100%, var(--board-max));
    aspect-ratio: 1 / 1;
    margin: 0 auto;
    padding: 0 var(--gap) var(--gap);
    position: relative;
    isolation: isolate;
    background: #07101d;
    border: 2px solid rgba(255,255,255,.16);
    border-top: 0;
    box-shadow: inset 0 0 32px rgba(0,0,0,.5);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.flow-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: 2;
    pointer-events: none;
}

.cell {
    position: relative;
    min-width: 0;
    min-height: 0;
    border-radius: clamp(3px, .8vw, 8px);
    overflow: visible;
    background: linear-gradient(rgba(255,255,255,.022), rgba(255,255,255,.008)), #111d30;
    border: 1px solid rgba(255,255,255,.05);
    z-index: 1;
}

.cell.open {
    background: linear-gradient(rgba(31,58,85,.18), rgba(8,19,33,.12));
    border-color: rgba(255,255,255,.025);
}

.cell.drop-target::after {
    content: "";
    position: absolute;
    inset: 2px;
    border: 2px solid rgba(127,229,255,.78);
    border-radius: inherit;
    box-shadow: 0 0 12px rgba(127,229,255,.25);
    pointer-events: none;
    z-index: 7;
}

.piece {
    --drag-x: 0px;
    --drag-y: 0px;
    position: absolute;
    inset: clamp(2px, .45vw, 4px);
    border-radius: clamp(6px, 1.4vw, 12px);
    cursor: grab;
    touch-action: none;
    user-select: none;
    z-index: 5;
    transform: translate3d(var(--drag-x), var(--drag-y), 0);
    transition: transform 150ms cubic-bezier(.2,.82,.25,1), opacity .23s ease, filter .18s ease;
    will-change: transform;
    box-shadow: inset 0 1px 0 rgba(255,255,255,.3), 0 4px 10px rgba(0,0,0,.32);
}

.piece.dragging {
    z-index: 20;
    cursor: grabbing;
    filter: brightness(1.12) saturate(1.06);
    transition: opacity .2s ease, filter .12s ease;
    box-shadow: inset 0 1px 0 rgba(255,255,255,.4), 0 9px 20px rgba(0,0,0,.43);
}

.piece.preview-target { z-index: 8; transition: transform 70ms linear; }

.piece.type-0 {
    background: radial-gradient(circle at 35% 28%, rgba(255,255,255,.65), transparent 20%),
                repeating-linear-gradient(45deg, transparent 0 7px, rgba(255,255,255,.12) 7px 10px), #eb5757;
}

.piece.type-1 {
    border-radius: 50%;
    background: radial-gradient(circle at 38% 30%, rgba(255,255,255,.7), transparent 18%), #2d9cdb;
}

.piece.type-2 {
    clip-path: polygon(50% 0%, 98% 38%, 80% 100%, 20% 100%, 2% 38%);
    background: repeating-linear-gradient(0deg, rgba(255,255,255,.13) 0 5px, transparent 5px 10px), #f2c94c;
}

.piece.type-3 {
    border-radius: 8px;
    background: radial-gradient(circle, rgba(255,255,255,.28) 1px, transparent 2px) 0 0 / 9px 9px, #6fcf97;
}

.piece.type-4 {
    transform: translate3d(var(--drag-x), var(--drag-y), 0) rotate(45deg) scale(.78);
    border-radius: 6px;
    background: linear-gradient(90deg, transparent 46%, rgba(255,255,255,.22) 46% 54%, transparent 54%),
                linear-gradient(transparent 46%, rgba(255,255,255,.22) 46% 54%, transparent 54%), #bb6bd9;
}

.piece.type-4.dragging { transform: translate3d(var(--drag-x), var(--drag-y), 0) rotate(45deg) scale(.84); }
.piece.type-4.preview-target { transform: translate3d(var(--drag-x), var(--drag-y), 0) rotate(45deg) scale(.78); }

.piece.exploding {
    opacity: 0;
    transform: scale(.2) rotate(18deg);
    filter: blur(6px) brightness(1.8);
}

.smoke { position: absolute; left: 50%; top: 50%; width: 10px; height: 10px; pointer-events: none; z-index: 30; }
.smoke::before, .smoke::after, .smoke span {
    content: "";
    position: absolute;
    border-radius: 50%;
    background: rgba(225,233,245,.82);
    filter: blur(2px);
    animation: puff .55s ease-out forwards;
}
.smoke::before { width: 18px; height: 18px; left: -18px; top: -4px; }
.smoke::after { width: 24px; height: 24px; left: 4px; top: -12px; animation-delay: .03s; }
.smoke span { width: 20px; height: 20px; left: -3px; top: 5px; animation-delay: .07s; }

@keyframes puff {
    from { opacity: .9; transform: scale(.45) translateY(0); }
    to { opacity: 0; transform: scale(2.2) translateY(-17px); }
}

.goal {
    width: min(100%, var(--board-max));
    margin: 7px auto 0;
    text-align: center;
    color: var(--muted);
    font-size: clamp(.72rem, 2.6vw, .85rem);
}
.goal-line {
    height: 5px;
    background: linear-gradient(90deg, transparent, #50e3c2 15%, #50e3c2 85%, transparent);
    border-radius: 999px;
    box-shadow: 0 0 15px rgba(80,227,194,.6);
    margin-bottom: 6px;
}

.message { min-height: 28px; margin-top: 8px; text-align: center; font-weight: 800; color: #82ffe0; }

@media (max-width: 900px) {
    .game-layout { grid-template-columns: 1fr; }
    .topbar { align-items: stretch; flex-direction: column; }
    .help-card { order: 2; }
}

@media (max-width: 600px) {
    .app-shell { width: 100%; padding-top: 8px; }
    .topbar { padding: 0 10px; margin-bottom: 10px; }
    .topbar p { font-size: .82rem; }
    .game-card { padding: 9px 5px 12px; border-radius: 0; border-left: 0; border-right: 0; }
    .help-card { margin: 0 10px; border-radius: 16px; }
    .settings { display: grid; grid-template-columns: 1fr 1.25fr; }
    .settings button { grid-column: 1 / -1; }
    .settings select, .settings button { width: 100%; }
    .statusbar { margin-inline: 6px; }
    .material-reservoir, .game-grid, .goal { width: 100%; }
    .material-reservoir { border-radius: 12px 12px 0 0; }
    .game-grid { border-left: 0; border-right: 0; padding-left: 2px; padding-right: 2px; }
}

@media (hover: none) and (pointer: coarse) {
    .piece { inset: 2px; }
    .cell.drop-target::after { border-width: 3px; }
}
