:root {
    --ink: #172129;
    --muted: #5a6770;
    --paper: #fffdf8;
    --panel: #ffffff;
    --line: #cdd8e0;
    --line-strong: #5f7788;
    --brand: #0f5a7a;
    --brand-2: #d87521;
    --brand-soft: #e9f4fb;
    --warn: #a93a2f;
    --success: #1d8a62;
    --shadow: 0 12px 28px rgba(16, 31, 42, 0.11);
    --cell-size: clamp(36px, 9.3vw, 56px);
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    min-height: 100%;
}

body {
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    color: var(--ink);
    background:
        radial-gradient(circle at 12% 8%, rgba(216, 117, 33, 0.28), transparent 38%),
        radial-gradient(circle at 83% 6%, rgba(15, 90, 122, 0.23), transparent 44%),
        linear-gradient(165deg, #dcecf3 0%, #f3e7d8 48%, #edf3ef 100%);
}

.app-shell {
    width: min(1220px, 96vw);
    margin: 1.1rem auto 1.4rem;
    display: grid;
    gap: 0.95rem;
}

.hero,
.panel,
.panel-card,
.board-wrap,
.tray-shell {
    border: 1px solid var(--line);
    border-radius: 18px;
    background: var(--panel);
    box-shadow: var(--shadow);
}

.hero {
    padding: 1.2rem 1.2rem 1.25rem;
    background:
        linear-gradient(130deg, rgba(15, 90, 122, 0.12), rgba(216, 117, 33, 0.11)),
        var(--paper);
}

.kicker {
    margin: 0;
    font-size: 0.74rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--brand);
    font-weight: 700;
}

h1 {
    margin: 0.3rem 0 0.24rem;
    font-size: clamp(1.95rem, 5.5vw, 3.05rem);
    line-height: 1.04;
    font-family: "Avenir Next Condensed", "IBM Plex Sans Condensed", "Segoe UI", sans-serif;
}

h2,
h3 {
    margin: 0;
}

.subtitle {
    margin: 0;
    max-width: 68ch;
    color: var(--muted);
}

.panel {
    padding: 0.95rem;
}

.panel-head {
    margin-bottom: 0.8rem;
}

.muted {
    color: var(--muted);
    margin: 0.35rem 0 0;
    font-size: 0.95rem;
}

.lobby-grid {
    display: grid;
    gap: 0.75rem;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

.panel-card {
    padding: 0.9rem;
    transition: transform 180ms ease, box-shadow 180ms ease;
}

label {
    display: block;
    margin: 0.62rem 0 0.24rem;
    font-size: 0.88rem;
    color: var(--muted);
    font-weight: 600;
}

input,
button,
select {
    font: inherit;
    border-radius: 12px;
}

input {
    width: 100%;
    padding: 0.56rem 0.66rem;
    border: 1px solid var(--line);
    background: #fff;
    transition: border-color 120ms ease;
}

input:focus-visible {
    outline: none;
    border-color: var(--brand);
}

button {
    border: 0;
    background: linear-gradient(130deg, var(--brand), #3a7899);
    color: #fff;
    cursor: pointer;
    padding: 0.58rem 0.86rem;
    font-weight: 700;
    transition: transform 120ms ease, filter 120ms ease;
}

button:hover {
    filter: brightness(1.03);
    transform: translateY(-1px);
}

button:active {
    transform: translateY(0);
}

button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.status-line {
    min-height: 1.5rem;
    margin: 0.82rem 0 0.08rem;
    color: var(--success);
    font-weight: 700;
}

.status-line.error {
    color: var(--warn);
}

.game-layout {
    display: grid;
    gap: 0.8rem;
}

.game-topbar {
    display: grid;
    gap: 0.8rem;
    grid-template-columns: 1.05fr 1fr auto;
    align-items: center;
    background:
        linear-gradient(120deg, rgba(15, 90, 122, 0.08), rgba(216, 117, 33, 0.08)),
        var(--paper);
}

.topbar-main,
.topbar-meta {
    min-width: 0;
}

.small-label {
    margin: 0;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.09em;
    font-size: 0.76rem;
    font-weight: 700;
}

.code {
    margin: 0.12rem 0 0;
    font-size: clamp(1.22rem, 4vw, 1.5rem);
    letter-spacing: 0.07em;
    font-weight: 800;
}

.turn-status {
    margin: 0.35rem 0 0;
    color: var(--brand-2);
    font-weight: 800;
    font-size: 0.99rem;
}

.you-label {
    margin: 0;
    font-weight: 700;
}

.meta-row {
    margin-top: 0.28rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.38rem;
}

.status-chip {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    padding: 0.18rem 0.58rem;
    font-size: 0.78rem;
    font-weight: 700;
    background: var(--brand-soft);
    color: var(--brand);
}

.status-chip.you {
    background: #fff2e4;
    color: #9a5214;
}

.sync-status,
.sync-time {
    margin: 0.24rem 0 0;
    font-size: 0.85rem;
    color: var(--muted);
}

.sync-status.good {
    color: var(--success);
    font-weight: 700;
}

.sync-status.bad {
    color: var(--warn);
    font-weight: 700;
}

.leave-btn {
    white-space: nowrap;
    background: linear-gradient(120deg, #8f3647, #6a4458);
}

.game-main {
    display: grid;
    gap: 0.8rem;
    grid-template-columns: minmax(0, 1fr);
}

.board-wrap {
    padding: 0.95rem;
    background: linear-gradient(180deg, #fdfefe, #f6f8fa);
}

.board-head {
    margin-bottom: 0.7rem;
}

.board-hint {
    margin: 0.24rem 0 0;
    color: var(--muted);
    font-size: 0.9rem;
}

.board {
    margin: 0 auto;
    display: grid;
    grid-template-columns: repeat(9, var(--cell-size));
    justify-content: center;
    border: 3px solid var(--line-strong);
    border-radius: 7px;
    overflow: hidden;
    background: #fff;
    touch-action: none;
    box-shadow: inset 0 0 0 1px rgba(53, 84, 104, 0.12), 0 18px 26px rgba(16, 31, 42, 0.09);
}

.cell {
    width: var(--cell-size);
    height: var(--cell-size);
    border: 1px solid #a9bbc8;
    background: #fff;
    display: grid;
    place-items: center;
    font-size: clamp(0.96rem, 2.1vw, 1.38rem);
    font-weight: 800;
    color: #19232d;
    padding: 0;
    border-radius: 0;
    transition: background-color 100ms ease, box-shadow 100ms ease, transform 120ms ease;
}

.cell[data-col="2"],
.cell[data-col="5"] {
    border-right: 3px solid var(--line-strong);
}

.cell[data-row="2"],
.cell[data-row="5"] {
    border-bottom: 3px solid var(--line-strong);
}

.cell.fixed {
    background: #edf2f7;
    color: #3a4958;
}

.cell.filled:not(.fixed) {
    background: #fbfbfc;
}

.cell.drop-target {
    background: #e5f4ff;
    box-shadow: inset 0 0 0 2px var(--brand);
}

.cell.drop-invalid {
    background: #ffece8;
    box-shadow: inset 0 0 0 2px #b24131;
}

.cell.highlight-row,
.cell.highlight-col,
.cell.highlight-box {
    background: #f1f9ff;
}

.cell.highlight-hover {
    background: #dff1ff;
    box-shadow: inset 0 0 0 2px #2c6f92;
}

.cell.just-placed {
    animation: placed-pop 560ms ease;
}

@keyframes placed-pop {
    0% {
        transform: scale(0.86);
        background: #fff6cf;
    }
    55% {
        transform: scale(1.03);
        background: #e9fbef;
    }
    100% {
        transform: scale(1);
    }
}

.side-panel {
    display: grid;
    gap: 0.8rem;
    align-content: start;
}

.score-grid {
    display: grid;
    gap: 0.8rem;
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.score-label {
    margin: 0;
    color: var(--muted);
    font-size: 0.77rem;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    font-weight: 700;
}

.player-name {
    margin: 0.14rem 0 0;
    font-weight: 700;
}

.score {
    margin: 0.14rem 0 0;
    font-size: clamp(1.4rem, 4.4vw, 2rem);
    color: var(--brand);
    font-weight: 900;
}

.winner-line {
    margin: 0.45rem 0 0.22rem;
    color: #93410f;
    font-weight: 900;
}

ul {
    margin: 0.45rem 0 0;
    padding-left: 1.1rem;
}

.stats-list,
.history-list,
.log {
    list-style: none;
    padding-left: 0;
}

.stats-list li,
.history-list li,
.log li {
    border-top: 1px dashed #d2dbe2;
    padding: 0.4rem 0;
    font-size: 0.92rem;
}

.stats-list li:first-child,
.history-list li:first-child,
.log li:first-child {
    border-top: 0;
}

.log {
    max-height: 220px;
    overflow: auto;
}

.tray-shell {
    position: sticky;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 0.42rem);
    z-index: 10;
    background: linear-gradient(130deg, #fffef9, #eef5fb);
    border-width: 2px;
    border-color: #93adbf;
}

.tray-head {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.8rem;
    margin-bottom: 0.62rem;
}

.tray-head .muted {
    margin: 0;
    font-size: 0.82rem;
}

.rack {
    display: flex;
    flex-wrap: wrap;
    gap: 0.52rem;
    margin-bottom: 0.72rem;
    touch-action: none;
}

.tile {
    border: 2px solid #31556e;
    background: linear-gradient(130deg, #ffffff, #edf6ff);
    color: #18384a;
    min-width: 2.7rem;
    min-height: 2.7rem;
    display: grid;
    place-items: center;
    border-radius: 13px;
    font-size: 1.1rem;
    font-weight: 900;
    box-shadow: 0 6px 12px rgba(15, 52, 75, 0.11);
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}

.tile.selected {
    background: linear-gradient(120deg, #ffd8ac, #ffd094);
    color: #633100;
    border-color: #b86611;
    transform: translateY(-1px);
}

.tile.dragging {
    opacity: 0.3;
}

.actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.5rem;
}

.actions button {
    min-height: 2.8rem;
}

#pass-btn {
    background: linear-gradient(120deg, #6a556c, #51606f);
}

#rematch-btn {
    background: linear-gradient(120deg, #19705a, #2f8866);
}

.drag-ghost {
    position: fixed;
    width: 52px;
    height: 52px;
    border-radius: 14px;
    background: linear-gradient(120deg, #ffd8ac, #ffc67f);
    border: 2px solid #a05d18;
    color: #4f2800;
    font-weight: 900;
    font-size: 1.2rem;
    display: grid;
    place-items: center;
    box-shadow: 0 12px 22px rgba(54, 32, 10, 0.3);
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 1000;
}

.hidden {
    display: none;
}

@media (min-width: 980px) {
    .game-layout {
        grid-template-columns: minmax(0, 1.45fr) minmax(320px, 0.95fr);
        grid-template-areas:
            "topbar topbar"
            "board side"
            "tray side";
        column-gap: 0.9rem;
        row-gap: 0.8rem;
        align-items: start;
    }

    .game-topbar {
        grid-area: topbar;
    }

    .game-main {
        display: contents;
    }

    .board-wrap {
        grid-area: board;
    }

    .game-main {
        grid-template-columns: 1.45fr 0.95fr;
        align-items: start;
    }

    .tray-shell {
        grid-area: tray;
        position: static;
        border-width: 1px;
        margin-top: 0;
    }

    .side-panel {
        grid-area: side;
        max-height: calc(100vh - 1.4rem);
        position: sticky;
        top: 0.7rem;
        overflow: auto;
        padding-right: 0.15rem;
    }

    .panel-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 14px 28px rgba(14, 30, 43, 0.12);
    }

    .board-wrap {
        padding: 1.1rem;
    }

    .tray-shell {
        margin-top: 0.2rem;
    }
}

@media (min-width: 1200px) {
    :root {
        --cell-size: 60px;
    }
}

@media (max-width: 900px) {
    .game-topbar {
        grid-template-columns: 1fr;
    }

    .leave-btn {
        width: 100%;
    }
}

@media (max-width: 640px) {
    .app-shell {
        width: min(98vw, 740px);
        margin-top: 0.55rem;
        margin-bottom: calc(env(safe-area-inset-bottom, 0px) + 1.05rem);
    }

    .panel,
    .board-wrap,
    .panel-card,
    .hero,
    .tray-shell {
        border-radius: 14px;
    }

    .tray-head {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.22rem;
    }

    .log {
        max-height: 180px;
    }

    .panel-card:hover {
        transform: none;
        box-shadow: var(--shadow);
    }
}
