:root {
    color-scheme: light;
    --bg: #f6f7f4;
    --panel: #ffffff;
    --ink: #202124;
    --muted: #66706a;
    --line: #d7ded8;
    --cell: #eef2ed;
    --cell-hover: #dfe8e0;
    --tile: #ffd766;
    --tile-edge: #d7a929;
    --accent: #216e63;
    --accent-strong: #124b43;
    --bad: #b3261e;
    --bad-bg: #ffe8e5;
    --good: #1d6f3f;
    --good-bg: #e7f4ea;
    --orphan: #935c00;
    --orphan-bg: #fff0cf;
    --cell-size: 44px;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--ink);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
    font: inherit;
}

button {
    border: 1px solid var(--accent);
    border-radius: 6px;
    background: var(--accent);
    color: #fff;
    min-height: 36px;
    padding: 0 12px;
    cursor: pointer;
}

button:hover {
    background: var(--accent-strong);
}

.secondary-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 36px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #fff;
    color: var(--accent);
    padding: 0 12px;
    text-decoration: none;
    cursor: pointer;
}

.secondary-button:hover {
    border-color: var(--accent);
    background: var(--cell);
    color: var(--accent-strong);
}

input {
    border: 1px solid var(--line);
    border-radius: 6px;
    min-height: 36px;
    padding: 0 10px;
    text-transform: uppercase;
    width: min(200px, 42vw);
}

.lobby-shell {
    display: grid;
    min-height: 100vh;
    place-items: center;
    padding: 24px;
}

.lobby-card {
    width: min(760px, 100%);
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--panel);
    box-shadow: 0 18px 48px rgb(32 33 36 / 8%);
}

.lobby-header {
    display: grid;
    justify-items: center;
    border-bottom: 1px solid var(--line);
    padding: 34px 28px 28px;
    text-align: center;
}

.title-tiles {
    display: flex;
    gap: 5px;
    margin-bottom: 16px;
}

.title-tiles span {
    display: grid;
    width: 36px;
    height: 36px;
    place-items: center;
    border: 1px solid var(--tile-edge);
    border-radius: 5px;
    background: var(--tile);
    color: #3a2b00;
    font-weight: 800;
    box-shadow: 0 2px 0 var(--tile-edge);
}

.lobby-header h1 {
    margin: 0;
    font-size: clamp(2rem, 6vw, 3rem);
    letter-spacing: -0.04em;
}

.lobby-header p,
.lobby-action p {
    color: var(--muted);
}

.lobby-header p {
    margin: 10px 0 0;
}

.nickname-field {
    display: grid;
    grid-template-columns: auto minmax(180px, 280px);
    align-items: center;
    justify-content: center;
    gap: 5px 12px;
    border-bottom: 1px solid var(--line);
    padding: 18px 28px;
}

.nickname-field label {
    font-size: 0.9rem;
    font-weight: 700;
}

.nickname-field input {
    width: 100%;
    text-transform: none;
}

.nickname-field span {
    grid-column: 2;
    color: var(--muted);
    font-size: 0.78rem;
}

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

.lobby-action {
    padding: 26px 28px 30px;
}

.lobby-action + .lobby-action {
    border-left: 1px solid var(--line);
}

.lobby-action h2 {
    margin: 0;
    font-size: 1.15rem;
}

.lobby-action > p {
    min-height: 42px;
    margin: 7px 0 18px;
    font-size: 0.92rem;
    line-height: 1.45;
}

.wide-button {
    width: 100%;
}

.settings-toggle {
    width: 100%;
    margin-top: 10px;
}

.new-game-settings {
    margin-top: 16px;
    border-top: 1px solid var(--line);
    padding-top: 16px;
}

.custom-game-form,
.join-game-form {
    display: grid;
    gap: 10px;
}

.custom-game-form label {
    font-size: 0.9rem;
    font-weight: 700;
}

.custom-game-form input,
.join-game-form input {
    width: 100%;
    text-transform: none;
}

.field-help {
    margin: -4px 0 0;
    font-size: 0.8rem;
    line-height: 1.4;
}

.lobby-status {
    margin: 0;
    border-top: 1px solid #e7aaa4;
    background: var(--bad-bg);
    color: var(--bad);
    padding: 12px 28px;
    text-align: center;
}

.lobby-status.pending {
    border-top-color: var(--line);
    background: var(--cell);
    color: var(--muted);
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
}

.app-shell {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 320px;
    gap: 16px;
    min-height: 100vh;
    padding: 16px;
}

.players-drawer {
    position: fixed;
    top: 50%;
    left: 0;
    z-index: 20;
    width: 250px;
    transform: translate(-100%, -50%);
    transition: transform 180ms ease;
}

.players-drawer.open {
    transform: translate(0, -50%);
}

.players-tab {
    position: absolute;
    top: 0;
    left: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 42px;
    min-height: 118px;
    border-radius: 0 8px 8px 0;
    padding: 10px 8px;
    box-shadow: 3px 3px 12px rgb(32 33 36 / 14%);
    writing-mode: vertical-rl;
}

.players-count {
    display: inline-grid;
    min-width: 22px;
    min-height: 22px;
    place-items: center;
    border-radius: 999px;
    background: rgb(255 255 255 / 18%);
    font-size: 0.75rem;
    line-height: 1;
    writing-mode: horizontal-tb;
}

.players-panel {
    max-height: min(70vh, 520px);
    overflow-y: auto;
    border: 1px solid var(--line);
    border-left: 0;
    border-radius: 0 8px 8px 0;
    background: var(--panel);
    box-shadow: 4px 8px 24px rgb(32 33 36 / 14%);
    padding: 16px;
}

.players-panel h2 {
    margin: 0 0 12px;
    font-size: 1rem;
}

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

.players-list li {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 4px 8px;
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 9px 10px;
}

.players-list li.current-player {
    border-color: #9ac9a7;
    background: var(--good-bg);
}

.players-list li.winner {
    border-color: var(--tile-edge);
    background: #fff8df;
}

.player-name {
    overflow: hidden;
    font-weight: 700;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.player-you {
    color: var(--good);
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
}

.player-progress {
    grid-column: 1 / -1;
    color: var(--muted);
    font-size: 0.76rem;
}

.play-area,
.side-panel section {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
}

.play-area {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    min-height: calc(100vh - 32px);
    overflow: hidden;
}

.top-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    border-bottom: 1px solid var(--line);
    padding: 12px;
}

.game-toolbar {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

.home-button {
    min-width: 72px;
}

.game-status-group {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
    max-width: 100%;
}

.help-button {
    display: inline-grid;
    width: 38px;
    min-width: 38px;
    min-height: 38px;
    place-items: center;
    border-radius: 999px;
    padding: 0;
    font-size: 1.05rem;
    font-weight: 800;
}

.corner-help-button {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 10;
}

.game-corner-help-button {
    top: 22px;
    right: 22px;
}

@media (min-width: 841px) {
    .session-panel .panel-heading {
        padding-right: 42px;
    }
}

.status-pill {
    border-radius: 999px;
    border: 1px solid var(--line);
    color: var(--muted);
    min-width: 0;
    max-width: min(280px, 55vw);
    overflow: hidden;
    padding: 7px 12px;
    text-align: center;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.status-pill.valid {
    background: var(--good-bg);
    border-color: #9ac9a7;
    color: var(--good);
}

.status-pill.invalid {
    background: var(--bad-bg);
    border-color: #e7aaa4;
    color: var(--bad);
}

.status-pill.stale {
    background: var(--orphan-bg);
    border-color: var(--orphan);
    color: var(--orphan);
}

.status-pill.complete {
    background: var(--tile);
    border-color: var(--tile-edge);
    color: #3a2b00;
}

.board-wrap {
    overflow: auto;
    padding: 18px;
}

.grid {
    display: grid;
    gap: 2px;
    width: max-content;
    margin: auto;
}

.cell {
    position: relative;
    display: grid;
    place-items: center;
    width: var(--cell-size);
    height: var(--cell-size);
    min-height: 0;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 4px;
    background: var(--cell);
    color: var(--muted);
    line-height: 1;
}

.cell:hover {
    background: var(--cell-hover);
}

.cell.selected {
    outline: 3px solid var(--accent);
    outline-offset: -3px;
}

.cell.origin::after {
    content: "0,0";
    position: absolute;
    bottom: 3px;
    right: 4px;
    color: #7b8580;
    font-size: 0.52rem;
}

.tile {
    display: grid;
    place-items: center;
    width: calc(var(--cell-size) - 8px);
    height: calc(var(--cell-size) - 8px);
    min-height: 0;
    padding: 0;
    border: 1px solid var(--tile-edge);
    border-radius: 6px;
    background: var(--tile);
    color: #3a2b00;
    font-weight: 800;
    line-height: 1;
    cursor: grab;
    user-select: none;
}

.tile:active {
    cursor: grabbing;
}

.tile.invalid {
    background: var(--bad-bg);
    border-color: var(--bad);
    color: var(--bad);
}

.tile.valid {
    background: var(--good-bg);
    border-color: var(--good);
    color: var(--good);
}

.tile.orphan {
    background: var(--orphan-bg);
    border-color: var(--orphan);
    color: var(--orphan);
}

.side-panel {
    display: grid;
    align-content: start;
    gap: 16px;
}

.side-panel section {
    padding: 12px;
}

.game-id-row {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px;
}

.game-id-row label {
    color: var(--muted);
    font-size: 0.9rem;
}

.game-id-row input {
    width: 100%;
    min-width: 0;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
    font-size: 0.78rem;
    text-transform: none;
}

.game-id-row button {
    padding: 0 10px;
    transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
}

.game-id-row button.copied,
.game-id-row button.copied:hover {
    border-color: var(--good);
    background: var(--good-bg);
    color: var(--good);
}

.game-id-row button:disabled {
    border-color: var(--line);
    background: #e7ebe7;
    color: #87908a;
    cursor: not-allowed;
}

.rack-panel {
    position: sticky;
    top: 16px;
    align-self: start;
    z-index: 2;
    max-height: calc(100vh - 32px);
    overflow-y: auto;
}

.panel-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 10px;
}

.panel-heading h2 {
    font-size: 1rem;
    margin: 0;
}

#rack-count {
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.rack {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
    gap: 8px;
    min-height: 72px;
}

.bag-row {
    display: grid;
    grid-template-columns: 1fr auto auto;
    align-items: center;
    gap: 8px;
    border: 1px solid var(--line);
    border-radius: 6px;
    margin-bottom: 10px;
    padding: 8px;
}

.bag-row span {
    color: var(--muted);
}

.bag-row strong {
    font-variant-numeric: tabular-nums;
}

.bag-row button:disabled,
.dump-button:disabled {
    border-color: var(--line);
    background: #e7ebe7;
    color: #87908a;
    cursor: not-allowed;
}

.rack-item {
    display: grid;
    gap: 4px;
}

.rack-tile {
    position: relative;
    min-height: 44px;
    width: 100%;
}

.rack-tile .count {
    position: absolute;
    top: 3px;
    right: 5px;
    font-size: 0.68rem;
    font-weight: 700;
}

.dump-button {
    min-height: 28px;
    padding: 0 6px;
    border-color: var(--line);
    background: #fff;
    color: var(--accent);
    font-size: 0.75rem;
}

.word-list,
.message-list {
    display: grid;
    gap: 6px;
    list-style: none;
    margin: 0;
    padding: 0;
}

.word-list li,
.message-list li {
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 8px;
}

.word-list li.valid {
    background: var(--good-bg);
    border-color: #9ac9a7;
    color: var(--good);
}

.word-list li.invalid {
    background: var(--bad-bg);
    border-color: #e7aaa4;
    color: var(--bad);
}

.word-button {
    display: block;
    width: 100%;
    min-height: 0;
    border: 0;
    background: transparent;
    color: inherit;
    padding: 0;
    text-align: left;
}

.word-button:hover {
    background: transparent;
    color: var(--accent-strong);
}

.word-definitions {
    display: grid;
    gap: 8px;
    margin-top: 8px;
    border-top: 1px solid #c6dccd;
    padding-top: 8px;
    color: var(--ink);
    font-size: 0.84rem;
    line-height: 1.35;
}

.word-definitions.muted {
    color: var(--muted);
}

.word-definitions.error {
    color: var(--bad);
}

.definition-meaning {
    display: grid;
    gap: 5px;
}

.part-of-speech {
    color: var(--good);
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
}

.definition-row {
    display: grid;
    gap: 3px;
}

.definition-row p {
    margin: 0;
}

.definition-example {
    color: var(--muted);
    font-style: italic;
}

.message-list li {
    color: var(--muted);
}

.app-dialog {
    width: min(600px, calc(100vw - 32px));
    max-height: calc(100vh - 32px);
    overflow-y: auto;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--panel);
    color: var(--ink);
    padding: 22px;
    box-shadow: 0 24px 64px rgb(32 33 36 / 24%);
}

.app-dialog::backdrop {
    background: rgb(18 30 27 / 52%);
    backdrop-filter: blur(2px);
}

.dialog-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 18px;
}

.dialog-heading h2 {
    margin: 0;
    font-size: 1.35rem;
}

.dialog-close-button {
    width: 36px;
    min-width: 36px;
    min-height: 36px;
    border-color: var(--line);
    background: #fff;
    color: var(--muted);
    padding: 0;
    font-size: 1.45rem;
    line-height: 1;
}

.dialog-close-button:hover {
    border-color: var(--accent);
    background: var(--cell);
    color: var(--accent-strong);
}

.dialog-content {
    display: grid;
    gap: 22px;
    margin-bottom: 22px;
}

.dialog-content h3 {
    margin: 0 0 8px;
    font-size: 1rem;
}

.dialog-content p {
    margin: 0 0 10px;
    color: var(--muted);
    font-size: 0.92rem;
    line-height: 1.5;
}

.dialog-content p:last-child {
    margin-bottom: 0;
}

.control-list {
    display: grid;
    gap: 0;
    margin: 0;
}

.control-list div {
    display: grid;
    grid-template-columns: minmax(150px, 0.8fr) minmax(0, 1.2fr);
    gap: 14px;
    border-top: 1px solid var(--line);
    padding: 9px 0;
}

.control-list div:last-child {
    border-bottom: 1px solid var(--line);
}

.control-list dt {
    color: var(--accent-strong);
    font-weight: 700;
}

.control-list dd {
    margin: 0;
    color: var(--muted);
}

.winner-dialog {
    width: min(440px, calc(100vw - 32px));
    text-align: center;
}

.winner-dialog .dialog-heading h2 {
    text-align: left;
}

.winner-mark {
    display: grid;
    width: 64px;
    height: 64px;
    place-items: center;
    margin: 4px auto 18px;
    border: 1px solid var(--tile-edge);
    border-radius: 9px;
    background: var(--tile);
    color: #3a2b00;
    box-shadow: 0 4px 0 var(--tile-edge);
    font-size: 1.75rem;
    font-weight: 900;
}

.winner-message {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 800;
}

.winner-detail {
    margin: 8px 0 22px;
    color: var(--muted);
    line-height: 1.45;
}

@media (max-width: 840px) {
    :root {
        --cell-size: 38px;
    }

    .app-shell {
        grid-template-columns: 1fr;
    }

    .play-area {
        min-height: 66vh;
    }

    .top-bar {
        padding-right: 54px;
    }
}

@media (max-width: 620px) {
    .lobby-shell {
        place-items: start center;
        padding: 12px;
    }

    .lobby-header {
        padding: 28px 20px 24px;
    }

    .lobby-actions {
        grid-template-columns: 1fr;
    }

    .nickname-field {
        grid-template-columns: 1fr;
        padding: 18px 20px;
    }

    .nickname-field span {
        grid-column: 1;
    }

    .lobby-action {
        padding: 22px 20px;
    }

    .lobby-action + .lobby-action {
        border-top: 1px solid var(--line);
        border-left: 0;
    }

    .lobby-action > p {
        min-height: 0;
    }

    .game-id-row {
        grid-template-columns: auto minmax(0, 1fr);
    }

    .game-id-row button {
        grid-column: 1 / -1;
    }

    .top-bar {
        flex-wrap: wrap;
    }

    .game-status-group {
        margin-left: auto;
    }

    .players-drawer {
        width: min(250px, calc(100vw - 52px));
    }

    .control-list div {
        grid-template-columns: 1fr;
        gap: 3px;
    }
}
