:root {
    color-scheme: light;
    --ink: #333333;
    --muted: #777777;
    --page: #e9e9e9;
    --panel: #f3f3f3;
    --cell-a: #eeeeee;
    --cell-b: #d7d7d7;
    --toolbar: #d7d7d7;
    --orange: #d28c37;
    --orange-dark: #bd7825;
    --blue: #245c8e;
    --green: #739b20;
    --red: #9d2817;
    --amber: #d28c37;
    --tile-size: min(calc((100vw - 32px) / 15), 35px);
    --rack-gap: 3px;
}

* {
    box-sizing: border-box;
}

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

body {
    margin: 0;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--ink);
    background: var(--page);
}

body.game-active {
    --tile-size: min(calc((100vw - 20px) / 15), calc((100dvh - 280px) / 15), 35px);
    overflow: hidden;
}

button,
input,
select {
    font: inherit;
}

button {
    min-height: 46px;
    border: 0;
    border-radius: 6px;
    padding: 0 18px;
    color: #ffffff;
    background: var(--orange);
    font-weight: 750;
    cursor: pointer;
}

button:disabled {
    cursor: not-allowed;
    opacity: .42;
}

button.danger {
    color: #ffffff;
    background: var(--red);
}

.hidden {
    display: none !important;
}

.shell {
    width: min(100%, 560px);
    min-height: 100vh;
    margin: 0 auto;
    padding: 12px 16px 96px;
}

body.game-active .shell {
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    padding: 8px 10px 0;
}

.screen {
    display: none;
}

.screen-active {
    display: block;
}

.brand {
    min-height: 30vh;
    display: grid;
    align-content: end;
    padding: 34px 4px 22px;
}

.brand h1,
.topbar h1 {
    margin: 0;
    font-size: clamp(2.4rem, 13vw, 4.8rem);
    line-height: .95;
    letter-spacing: 0;
}

.brand p {
    max-width: 31rem;
    margin: 12px 0 0;
    color: var(--muted);
    font-size: 1.05rem;
}

.eyebrow {
    margin: 0 0 6px;
    color: var(--muted);
    font-size: .78rem;
    font-weight: 850;
    letter-spacing: 0;
    text-transform: uppercase;
}

.panel {
    margin: 14px 0;
    padding: 16px;
    border: 1px solid #d4d4d4;
    border-radius: 6px;
    background: rgba(255, 255, 255, .72);
    box-shadow: 0 10px 26px rgba(0, 0, 0, .08);
}

.panel h2 {
    margin: 0 0 14px;
    font-size: 1.08rem;
}

.active-game-panel {
    border-color: rgba(210, 140, 55, .65);
    background: rgba(255, 255, 255, .82);
}

.button-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 12px;
}

label {
    display: grid;
    gap: 6px;
    margin: 12px 0;
    color: var(--muted);
    font-weight: 700;
}

input,
select {
    width: 100%;
    min-height: 46px;
    border: 1px solid #cdcdcd;
    border-radius: 6px;
    padding: 0 12px;
    color: var(--ink);
    background: #ffffff;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.icon-button,
.ghost-button {
    width: 46px;
    min-height: 46px;
    padding: 0;
    color: var(--muted);
    background: transparent;
    font-size: 1.7rem;
    line-height: 1;
}

.player-list {
    display: grid;
    gap: 8px;
    padding: 0;
    margin: 0 0 16px;
    list-style: none;
}

.player-list li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    border: 1px solid #d4d4d4;
    border-radius: 6px;
    padding: 10px 12px;
    background: #ffffff;
}

#gameScreen.screen-active {
    height: 100%;
    min-height: 0;
    display: grid;
    grid-template-rows: auto auto 1fr auto;
    overflow: hidden;
    padding-bottom: 0;
}

.game-header {
    display: grid;
    grid-template-columns: 48px 1fr 48px;
    align-items: center;
    gap: 8px;
    margin: -4px 0 8px;
}

.game-header p {
    min-width: 0;
    margin: 0;
    color: #343434;
    font-size: clamp(1.45rem, 6.2vw, 2.15rem);
    font-weight: 400;
    line-height: 1.15;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-icon {
    position: relative;
}

.chat-icon::before,
.chat-icon::after {
    content: "";
    position: absolute;
    border-radius: 50%;
    background: #7f7f7f;
}

.chat-icon::before {
    width: 30px;
    height: 23px;
    top: 8px;
    right: 17px;
    box-shadow: 8px -3px 0 0 var(--page);
}

.chat-icon::after {
    width: 29px;
    height: 23px;
    right: 3px;
    top: 18px;
}

.players-bar {
    display: flex;
    gap: 6px;
    min-height: 0;
    overflow-x: auto;
    padding: 0 0 5px;
}

.player-chip {
    min-width: 104px;
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    border-radius: 6px;
    padding: 6px 8px;
    color: #666666;
    background: rgba(255, 255, 255, .45);
    font-size: .82rem;
}

.player-chip.active {
    color: #ffffff;
    background: #8a8a8a;
}

.score-pill,
.tile-count {
    min-width: 28px;
    min-height: 24px;
    display: inline-grid;
    place-items: center;
    border-radius: 999px;
    padding: 0 8px;
    color: #ffffff;
    background: #777777;
    font-weight: 850;
}

.board-wrap {
    display: grid;
    align-items: start;
    min-height: 0;
    margin: 0;
}

.board {
    position: relative;
    width: calc(var(--tile-size) * 15);
    aspect-ratio: 1 / 1;
    justify-self: center;
    overflow: hidden;
    border-radius: 2px;
    background-color: var(--cell-a);
    background-image:
        linear-gradient(45deg, var(--cell-b) 25%, transparent 25%, transparent 75%, var(--cell-b) 75%),
        linear-gradient(45deg, var(--cell-b) 25%, transparent 25%, transparent 75%, var(--cell-b) 75%);
    background-position: 0 0, var(--tile-size) var(--tile-size);
    background-size: calc(var(--tile-size) * 2) calc(var(--tile-size) * 2);
    transition: box-shadow .15s ease;
}

.board::before {
    content: "";
    position: absolute;
    inset: 0;
    background-image:
        linear-gradient(rgba(255, 255, 255, .45) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255, 255, 255, .45) 1px, transparent 1px);
    background-size: var(--tile-size) var(--tile-size);
    pointer-events: none;
}

.board-groups {
    position: absolute;
    inset: 0;
}

.board .tile {
    position: absolute;
    top: calc(var(--tile-size) * var(--tile-row));
    left: calc(var(--tile-size) * var(--tile-col));
}

.group {
    position: absolute;
    top: calc(var(--tile-size) * var(--group-row));
    left: calc(var(--tile-size) * var(--group-col));
    display: flex;
    align-items: center;
    gap: 2px;
    min-width: calc(var(--tile-size) * 3);
    min-height: var(--tile-size);
    padding: 0;
    border: 0;
    background: transparent;
}

.group.invalid .tile {
    box-shadow: 0 0 0 3px rgba(157, 40, 23, .35) inset;
}

.remove-group {
    display: none;
}

.rack-wrap {
    margin: 7px 0 74px;
}

.rack {
    display: grid;
    grid-template-columns: repeat(10, minmax(0, 1fr));
    gap: var(--rack-gap);
    width: 100%;
    min-height: calc((((100vw - 32px) - (9 * var(--rack-gap))) / 10) * 2 + var(--rack-gap));
    max-width: 100%;
    transition: box-shadow .15s ease;
}

.board.drop-ready,
.rack.drop-ready {
    box-shadow: 0 0 0 4px rgba(210, 140, 55, .28);
}

.rack-slot {
    aspect-ratio: 1 / 1;
    border-radius: 4px;
    background: #d7d7d7;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .35);
}

.tile {
    width: var(--tile-size);
    height: var(--tile-size);
    min-height: 0;
    flex: 0 0 var(--tile-size);
    display: grid;
    place-items: center;
    border: 1px solid rgba(255, 255, 255, .24);
    border-radius: 5px;
    padding: 0;
    color: #ffffff;
    background: #777777;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .16);
    font-size: clamp(1.35rem, 6vw, 2.25rem);
    font-weight: 400;
    line-height: 1;
    touch-action: none;
    user-select: none;
}

.rack .tile {
    width: 100%;
    height: auto;
    aspect-ratio: 1 / 1;
    font-size: clamp(1.7rem, 8.5vw, 3.1rem);
}

.tile.selected {
    transform: translateY(-7px);
    box-shadow: 0 9px 16px rgba(0, 0, 0, .22);
    outline: 3px solid rgba(255, 255, 255, .75);
}

.tile.drag-source {
    opacity: .35;
}

.drag-ghost {
    position: fixed;
    z-index: 30;
    width: var(--tile-size);
    height: var(--tile-size);
    pointer-events: none;
    transform: translate(-50%, -50%) scale(1.08);
    opacity: .92;
    box-shadow: 0 12px 26px rgba(0, 0, 0, .24);
}

.tile.red {
    background: var(--red);
}

.tile.blue {
    background: var(--blue);
}

.tile.green {
    background: var(--green);
}

.tile.amber {
    background: var(--amber);
}

.tile.joker {
    color: #333333;
    background: #f7f7f7;
    border-color: rgba(80, 80, 80, .35);
}

.empty-note {
    display: none;
}

.actions {
    position: fixed;
    right: 0;
    bottom: max(8px, env(safe-area-inset-bottom));
    left: 0;
    z-index: 6;
    width: min(calc(100% - 32px), 528px);
    height: 68px;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    align-items: center;
    gap: 0;
    margin: 0 auto;
    border-radius: 8px;
    padding: 0 10px;
    background: var(--toolbar);
}

.toolbar-button {
    min-width: 0;
    min-height: 56px;
    display: grid;
    place-items: center;
    padding: 0;
    color: #7e7e7e;
    background: transparent;
    font-size: clamp(2.8rem, 14vw, 4rem);
    font-weight: 800;
    line-height: 1;
}

.toolbar-spacer {
    min-width: 0;
    min-height: 56px;
}

.primary-action {
    width: 104px;
    height: 104px;
    justify-self: center;
    border-radius: 50%;
    color: #ffffff;
    background: var(--orange);
    font-size: clamp(5rem, 21vw, 7rem);
    font-weight: 300;
    transform: translateY(-18px);
    box-shadow: 0 3px 7px rgba(0, 0, 0, .12);
}

.primary-action:active {
    background: var(--orange-dark);
}

.toast {
    position: fixed;
    left: 50%;
    bottom: 108px;
    z-index: 10;
    max-width: min(92vw, 420px);
    padding: 12px 14px;
    border-radius: 6px;
    color: #ffffff;
    background: rgba(38, 38, 38, .94);
    opacity: 0;
    transform: translate(-50%, 16px);
    transition: .2s ease;
    pointer-events: none;
}

.toast.show {
    opacity: 1;
    transform: translate(-50%, 0);
}

@media (max-width: 380px) {
    :root {
        --tile-size: calc((100vw - 20px) / 15);
    }

    .shell {
        padding-inline: 10px;
    }

    .actions {
        width: calc(100% - 20px);
    }
}

@media (min-width: 680px) {
    .shell {
        padding-inline: 22px;
    }

    #startScreen {
        display: none;
    }

    #startScreen.screen-active {
        display: grid;
        grid-template-columns: 1.1fr .9fr;
        gap: 16px;
        align-items: end;
    }

    .brand {
        grid-row: span 2;
        min-height: 70vh;
    }
}
