/* Атрибут hidden должен действовать и на блоки с display:flex/grid. */
[hidden] { display: none !important; }

:root {
    --paper: #fbf7ee;
    --paper-deep: #f2e7d4;
    --ink: #24342f;
    --muted: #69746e;
    --line: #dfd2bd;
    --green: #1f785f;
    --green-dark: #145543;
    --coral: #df624f;
    --gold: #e5a83c;
    --blue: #2869a8;
    --shadow: 0 16px 46px rgba(74, 58, 35, .13);
    --radius: 24px;
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--paper); }

body {
    margin: 0;
    color: var(--ink);
    font-family: Inter, ui-rounded, "Segoe UI", Arial, sans-serif;
    background:
        radial-gradient(circle at 9% 7%, rgba(229, 168, 60, .16), transparent 22rem),
        radial-gradient(circle at 92% 28%, rgba(31, 120, 95, .12), transparent 25rem),
        var(--paper);
}

button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.page-shell {
    width: min(1320px, calc(100% - 40px));
    margin: 0 auto;
    padding: 24px 0 36px;
}

.site-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    margin-bottom: 22px;
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 12px;
    color: inherit;
    text-decoration: none;
}

.brand-mark {
    display: grid;
    width: 48px;
    height: 48px;
    place-items: center;
    border: 2px solid var(--ink);
    border-radius: 15px;
    color: #fff;
    font-size: 28px;
    font-weight: 900;
    transform: rotate(-4deg);
    background: var(--coral);
    box-shadow: 4px 4px 0 var(--ink);
}

.brand strong, .brand small { display: block; }
.brand strong { font-family: Georgia, "Times New Roman", serif; font-size: 23px; }
.brand small { margin-top: 2px; color: var(--muted); font-size: 12px; }

.day-badge {
    padding: 9px 14px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: rgba(255, 255, 255, .64);
    text-align: right;
}
.day-badge span, .day-badge strong { display: block; }
.day-badge span { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; }
.day-badge strong { margin-top: 2px; font-size: 14px; }

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

.board-card,
.status-card,
.save-card,
.rules-card,
.results-section {
    border: 1px solid rgba(122, 97, 61, .18);
    border-radius: var(--radius);
    background: rgba(255, 253, 248, .92);
    box-shadow: var(--shadow);
}

.board-card { padding: 22px; }
.board-heading, .results-heading {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 18px;
    margin-bottom: 16px;
}

.eyebrow {
    margin: 0 0 4px;
    color: var(--green);
    font-size: 11px;
    font-weight: 800;
    letter-spacing: .11em;
    text-transform: uppercase;
}

h1, h2, p { margin-top: 0; }
h1, h2 { font-family: Georgia, "Times New Roman", serif; line-height: 1.05; }
h1 { margin-bottom: 0; font-size: clamp(23px, 2.4vw, 34px); }
h2 { margin-bottom: 10px; font-size: 25px; }

.legend { display: flex; flex-wrap: wrap; gap: 12px; color: var(--muted); font-size: 13px; }
.legend span { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.legend-token, .score-dot {
    display: inline-block;
    border: 2px solid #fff;
    border-radius: 50%;
    box-shadow: 0 0 0 1px rgba(36, 52, 47, .25);
}
.legend-token { width: 14px; height: 14px; }
.score-dot { width: 12px; height: 12px; }
.human { background: var(--coral); }
.bot { background: var(--blue); }

.board-status {
    display: flex;
    align-items: center;
    gap: 9px;
    min-height: 43px;
    padding: 10px 13px;
    margin-bottom: 14px;
    border-radius: 12px;
    background: #f2eddf;
    font-size: 14px;
    font-weight: 750;
}
.board-status::before, .roll-label::before {
    content: '';
    flex: 0 0 auto;
    width: 11px;
    height: 11px;
    border: 2px solid #fff;
    border-radius: 50%;
    background: var(--muted);
    box-shadow: 0 0 0 1px rgba(36, 52, 47, .2);
}
.board-status[data-actor="human"] { background: #fae8e1; }
.board-status[data-actor="bot"] { background: #e3edf5; }
.board-status[data-actor="human"]::before,
.roll-overlay[data-actor="human"] .roll-label::before { background: var(--coral); }
.board-status[data-actor="bot"]::before,
.roll-overlay[data-actor="bot"] .roll-label::before { background: var(--blue); }

.board-wrap {
    position: relative;
    overflow: hidden;
    width: 100%;
    aspect-ratio: 1 / 1;
    border: 2px solid #3b4e46;
    border-radius: 18px;
    background: #ece2ce;
    box-shadow: inset 0 0 0 5px rgba(255, 255, 255, .55), 0 8px 0 #d3c2a5;
}

.board { display: block; width: 100%; height: 100%; }
.board-cell { stroke: rgba(49, 63, 56, .17); stroke-width: 2; }
.board-cell.tone-a { fill: #f4dfad; }
.board-cell.tone-b { fill: #e9f0d8; }
.board-cell.tone-c { fill: #f3d2c6; }
.board-cell.tone-d { fill: #dce9ea; }
.board-cell.finish { fill: #efb542; }
.cell-number-back { fill: rgba(255, 253, 247, .78); }
.cell-number {
    fill: #263a32;
    font-family: ui-rounded, "Segoe UI", Arial, sans-serif;
    font-size: 23px;
    font-weight: 800;
    text-anchor: middle;
    dominant-baseline: central;
}
.finish-label { fill: #65440b; font-size: 17px; font-weight: 900; letter-spacing: .06em; text-anchor: middle; }

.ladder-rail { stroke: #8a5b2d; stroke-width: 15; stroke-linecap: round; filter: drop-shadow(0 4px 2px rgba(45, 33, 20, .18)); }
.ladder-rung { stroke: #d28d37; stroke-width: 13; stroke-linecap: round; }
.snake-body { fill: none; stroke-width: 31; stroke-linecap: round; filter: drop-shadow(0 5px 2px rgba(45, 33, 20, .22)); }
.snake-highlight { fill: none; stroke: rgba(255, 255, 255, .38); stroke-width: 7; stroke-linecap: round; }
.snake-head { stroke: rgba(36, 52, 47, .55); stroke-width: 3; }
.snake-eye { fill: #fff; stroke: #26342f; stroke-width: 2; }
.snake-pupil { fill: #26342f; }

.game-token {
    stroke: #fff;
    stroke-width: 6;
    filter: drop-shadow(0 6px 4px rgba(28, 39, 35, .3));
}
.game-token.human-token { fill: var(--coral); }
.game-token.bot-token { fill: var(--blue); }
.token-ring { fill: none; stroke: rgba(36, 52, 47, .28); stroke-width: 3; }
.game-token.is-moving { filter: drop-shadow(0 3px 7px rgba(28, 39, 35, .55)); }
.token-ring.is-active { stroke-width: 5; stroke: var(--gold); }

.start-label { fill: #5d6a64; font-size: 18px; font-weight: 800; text-anchor: middle; }
.jump-flash { animation: jump-flash .7s ease; }

.board-loading {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    color: var(--green-dark);
    font-weight: 750;
    background: rgba(251, 247, 238, .88);
    backdrop-filter: blur(4px);
}
.board-loading[hidden] { display: none; }
.loader {
    width: 24px;
    height: 24px;
    border: 3px solid rgba(31, 120, 95, .2);
    border-top-color: var(--green);
    border-radius: 50%;
    animation: spin .8s linear infinite;
}

.game-panel { display: grid; gap: 16px; }
.status-card, .save-card { padding: 22px; }
.status-text { min-height: 42px; margin-bottom: 18px; color: var(--muted); line-height: 1.45; }

.score-strip {
    display: grid;
    grid-template-columns: 1fr 1fr;
    margin-bottom: 18px;
    border: 1px solid var(--line);
    border-radius: 15px;
    background: var(--paper);
}
.score-strip > div { position: relative; padding: 13px 14px 12px 31px; }
.score-strip > div + div { border-left: 1px solid var(--line); }
.score-strip .score-dot { position: absolute; left: 13px; top: 17px; }
.score-strip small { display: block; color: var(--muted); font-size: 11px; }
.score-strip strong { display: block; margin-top: 2px; font-size: 25px; }

.turn-row { display: flex; align-items: center; gap: 16px; margin-bottom: 16px; }
.turn-row > span { color: var(--muted); font-size: 13px; }
.roll-overlay {
    position: absolute;
    inset: 0;
    z-index: 4;
    display: grid;
    place-items: center;
    background: rgba(251, 247, 238, .26);
}
.roll-popup {
    display: grid;
    justify-items: center;
    gap: 20px;
    min-width: 240px;
    padding: 26px 30px 23px;
    border: 2px solid var(--coral);
    border-radius: 24px;
    background: rgba(255, 253, 248, .98);
    box-shadow: 0 16px 45px rgba(36, 52, 47, .24);
}
.roll-overlay[data-actor="bot"] .roll-popup { border-color: var(--blue); }
.roll-label { display: flex; align-items: center; gap: 9px; margin: 0; font-size: 18px; font-weight: 800; }
.roll-result { min-height: 24px; margin: 0; font-size: 18px; font-weight: 700; }
.dice {
    display: grid;
    flex: 0 0 auto;
    width: 124px;
    height: 124px;
    place-items: center;
    border: 2px solid var(--ink);
    border-radius: 24px;
    background: #fff;
    box-shadow: 4px 5px 0 var(--ink);
}
.die-face { display: block; width: 100%; height: 100%; fill: var(--ink); }
.dice.is-rolling { animation: dice-roll .55s ease-in-out infinite; }

.actions { display: grid; grid-template-columns: 1fr auto; gap: 10px; }
.button {
    min-height: 46px;
    padding: 10px 16px;
    border: 0;
    border-radius: 13px;
    color: var(--ink);
    font-weight: 800;
    cursor: pointer;
    transition: transform .16s ease, box-shadow .16s ease, opacity .16s ease;
}
.button:hover:not(:disabled) { transform: translateY(-2px); }
.button:focus-visible, input:focus-visible, summary:focus-visible { outline: 3px solid rgba(40, 105, 168, .35); outline-offset: 3px; }
.button:disabled { cursor: not-allowed; opacity: .48; }
.button-primary { color: #fff; background: var(--green); box-shadow: 0 5px 0 var(--green-dark); }
.button-secondary { border: 1px solid var(--line); background: #fff; box-shadow: 0 4px 0 #d8c9b1; }
.button-accent { color: #fff; background: var(--coral); }
.button-danger { color: #fff; background: #b54138; }
.button-small { min-height: 37px; padding: 7px 12px; background: #fff; }

.save-card { border-color: rgba(229, 168, 60, .55); background: #fff7dc; box-shadow: 0 14px 35px rgba(143, 101, 24, .14); }
.save-card > p:not(.eyebrow) { color: var(--muted); line-height: 1.4; }
.save-card label { display: block; margin-bottom: 7px; font-size: 12px; font-weight: 800; }
.field-row { display: flex; gap: 8px; }
input {
    min-width: 0;
    width: 100%;
    min-height: 46px;
    padding: 9px 12px;
    border: 1px solid #c7b995;
    border-radius: 12px;
    color: var(--ink);
    background: #fff;
}
input[aria-invalid="true"] { border-color: #b54138; }
.field-hint { min-height: 18px; margin: 6px 0 0; color: #9c332d; font-size: 12px; }

.notice { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 14px 16px; border-radius: 16px; }
.notice strong { display: block; font-size: 14px; }
.notice p { margin: 3px 0 0; font-size: 12px; line-height: 1.35; }
.notice-error { color: #7c2823; border: 1px solid #e1a29e; background: #fff0ee; }

.rules-card { overflow: hidden; box-shadow: none; }
.rules-card summary { padding: 16px 20px; font-weight: 800; cursor: pointer; }
.rules-content { padding: 0 20px 17px; color: var(--muted); font-size: 13px; line-height: 1.5; }
.rules-content p { margin-bottom: 8px; }
.rules-content ul { margin: 0; padding-left: 20px; }
.rules-content li + li { margin-top: 4px; }

.results-section { margin-top: 24px; padding: 22px; }
.results-heading { align-items: center; }
.results-heading h2 { margin: 0; }
.personal-best { margin: 0; padding: 9px 13px; border-radius: 12px; color: var(--green-dark); font-size: 13px; font-weight: 800; background: #e5f1e8; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 15px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 12px 16px; border-bottom: 1px solid #eadfce; text-align: left; }
th { color: var(--muted); font-size: 11px; letter-spacing: .08em; text-transform: uppercase; background: #f7f0e5; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.mine { font-weight: 800; background: #fff3cf; }
td:first-child, th:first-child { width: 90px; text-align: center; }
td:last-child, th:last-child { width: 120px; text-align: right; }
.rank-medal { display: inline-grid; min-width: 30px; height: 30px; padding: 0 7px; place-items: center; border-radius: 999px; background: #efe5d5; }
.empty-row { padding: 24px; color: var(--muted); text-align: center !important; }

footer { padding: 25px 4px 0; color: var(--muted); font-size: 12px; text-align: center; }

.modal-layer {
    position: fixed;
    z-index: 50;
    inset: 0;
    display: grid;
    padding: 20px;
    place-items: center;
    background: rgba(24, 37, 32, .55);
    backdrop-filter: blur(6px);
}
.modal-layer[hidden] { display: none; }
.modal { width: min(430px, 100%); padding: 28px; border-radius: 24px; background: #fffdf8; box-shadow: 0 30px 90px rgba(21, 28, 25, .3); text-align: center; }
.modal-icon { display: grid; width: 54px; height: 54px; margin: 0 auto 13px; place-items: center; border-radius: 50%; color: #fff; font-size: 28px; background: var(--coral); }
.modal p { color: var(--muted); line-height: 1.45; }
.modal-actions { display: flex; justify-content: center; gap: 10px; margin-top: 20px; }
.visually-hidden { position: absolute !important; overflow: hidden; width: 1px; height: 1px; padding: 0; border: 0; margin: -1px; clip: rect(0 0 0 0); white-space: nowrap; }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes dice-roll {
    0% { transform: rotate(0deg) scale(1); }
    30% { transform: rotate(-18deg) scale(1.12); }
    65% { transform: rotate(16deg) scale(.94); }
    100% { transform: rotate(0deg) scale(1); }
}
@keyframes jump-flash {
    0%, 100% { filter: brightness(1); }
    45% { filter: brightness(1.45) drop-shadow(0 0 15px #fff); }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* На широком экране управление остаётся рядом с полем при прокрутке. */
@media (min-width: 921px) {
    .game-panel { position: sticky; top: 16px; }
}

@media (max-width: 920px) {
    .game-layout { grid-template-columns: minmax(0, 1fr); }
    .game-panel { grid-template-columns: 1fr 1fr; align-items: start; }
    .status-card, .notice { grid-column: 1 / -1; }
}

@media (max-width: 600px) {
    .page-shell { width: min(100% - 20px, 520px); padding-top: 12px; }
    .site-header { margin-bottom: 12px; }
    .brand-mark { width: 41px; height: 41px; font-size: 24px; }
    .brand strong { font-size: 18px; }
    .brand small { display: none; }
    .day-badge { padding: 7px 9px; }
    .day-badge span { display: none; }
    .day-badge strong { font-size: 12px; }
    .game-layout { gap: 14px; }
    .board-card { padding: 11px; border-radius: 18px; }
    .board-heading { align-items: flex-start; margin: 3px 2px 11px; }
    .board-heading .eyebrow { display: none; }
    .board-heading h1 { max-width: 220px; font-size: 20px; }
    .legend { justify-content: flex-end; gap: 5px 10px; font-size: 11px; }
    .board-wrap { border-radius: 11px; box-shadow: inset 0 0 0 3px rgba(255,255,255,.5), 0 5px 0 #d3c2a5; }
    .cell-number { font-size: 27px; }
    .game-panel { display: grid; grid-template-columns: 1fr; gap: 12px; }
    .status-card, .save-card { padding: 18px; }
    .status-card h2 { font-size: 23px; }
    .status-text { min-height: auto; margin-bottom: 13px; }
    .turn-row { margin-bottom: 12px; }
    .dice { width: 108px; height: 108px; }
    .roll-popup { min-width: 202px; gap: 14px; padding: 19px 23px; }
    .board-status { font-size: 13px; }
    .actions { grid-template-columns: 1fr; }
    .button { min-height: 48px; }
    .save-card, .rules-card, .notice { grid-column: 1; }
    .field-row { flex-direction: column; }
    .results-section { margin-top: 14px; padding: 16px 12px; border-radius: 18px; }
    .results-heading { align-items: flex-start; }
    .results-heading h2 { font-size: 22px; }
    .personal-best { max-width: 130px; font-size: 11px; text-align: right; }
    th, td { padding: 11px 9px; }
    td:first-child, th:first-child { width: 62px; }
    td:last-child, th:last-child { width: 76px; }
    .modal { padding: 23px 17px; }
    .modal-actions { flex-direction: column; }
}

@media (max-width: 390px) {
    .page-shell { width: calc(100% - 14px); }
    .board-card { padding: 7px; }
    .board-heading { padding: 3px 3px 0; }
    .board-heading h1 { font-size: 18px; }
    .legend span { gap: 4px; }
    .status-card { padding: 16px; }
    .score-strip > div { padding-left: 27px; }
}

/* В одноколоночной вёрстке кнопки доступны во время просмотра поля. */
@media (max-width: 920px) {
    html { scroll-padding-bottom: calc(88px + env(safe-area-inset-bottom, 0px)); }
    body { padding-bottom: calc(88px + env(safe-area-inset-bottom, 0px)); }

    .game-panel .actions {
        position: fixed;
        z-index: 20;
        right: 0;
        bottom: 0;
        left: 0;
        grid-template-columns: minmax(0, 1fr) auto;
        gap: 10px;
        padding: 10px 12px 12px;
        padding-right: max(12px, env(safe-area-inset-right, 0px), calc((100% - 520px) / 2));
        padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
        padding-left: max(12px, env(safe-area-inset-left, 0px), calc((100% - 520px) / 2));
        border-top: 1px solid var(--line);
        background: #fffdf8;
        box-shadow: 0 -6px 24px rgba(74, 58, 35, .12);
    }

    .game-panel .actions .button {
        min-width: 0;
        min-height: 48px;
        padding: 10px 12px;
        font-size: 14px;
    }
}

/* Ссылки на сообщество и отправку игры расположены после таблицы результатов. */
.community-section {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.35fr);
    gap: 24px;
    margin-top: 24px;
}
.community-card {
    min-width: 0;
    padding: 24px;
    border: 1px solid rgba(122, 97, 61, .18);
    border-radius: var(--radius);
    background: rgba(255, 253, 248, .88);
}
.community-card h2 { margin: 0; }
.community-description { margin: 10px 0 18px; color: var(--muted); font-size: 14px; line-height: 1.5; }
.community-button { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; text-align: center; }
.share-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.share-actions .button { min-height: 46px; }
.community-card .button:focus-visible { outline: 3px solid var(--green); outline-offset: 4px; }
.share-status { min-height: 18px; margin: 12px 0 0; color: var(--green-dark); font-size: 12px; line-height: 1.5; }
.share-copy-fallback { margin-top: 10px; }
.share-copy-fallback label { display: block; margin-bottom: 7px; font-size: 12px; font-weight: 800; }

@media (max-width: 920px) {
    .community-section { grid-template-columns: minmax(0, 1fr); gap: 14px; }
}

@media (max-width: 600px) {
    .community-section { margin-top: 14px; }
    .community-card { padding: 18px; border-radius: 18px; }
    .community-card h2 { font-size: 23px; }
    .community-card .button { min-height: 48px; }
    .share-actions .button { flex: 1 1 auto; }
}

/* Итог размещён под полем, чтобы не увеличивать закреплённую панель управления. */
.result-share-card { margin-top: 24px; scroll-margin-top: 16px; }
.result-share-link { display: inline-block; margin-top: 18px; color: var(--green-dark); font-size: 14px; font-weight: 800; text-underline-offset: 3px; }
.result-share-tones { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 18px; }
.result-tone { min-height: 44px; padding: 8px 14px; border: 1px solid var(--line); border-radius: 999px; color: var(--ink); font: inherit; font-size: 14px; background: #fff; cursor: pointer; }
.result-tone[aria-pressed="true"] { border-color: var(--green); color: var(--green-dark); background: #e5f1e8; }
.result-tone:focus-visible, .result-share-link:focus-visible, .result-share-text:focus-visible { outline: 3px solid var(--green); outline-offset: 3px; }
.result-share-preview { margin: 18px 0; padding: 16px; border: 1px solid var(--line); border-radius: 14px; color: var(--ink); font-size: 16px; line-height: 1.6; white-space: pre-line; overflow-wrap: anywhere; background: #f7f0e5; }
.result-share-text { display: block; width: 100%; padding: 12px; border: 1px solid var(--line); border-radius: 12px; color: var(--ink); font: inherit; font-size: 16px; line-height: 1.5; background: #fff; resize: vertical; }
@media (max-width: 600px) {
    .result-share-card { margin-top: 14px; }
    .result-share-preview { padding: 12px; font-size: 15px; }
}

/* Мобильное предложение поделиться появляется после завершения анимации. */
.has-result-share-dialog,
.has-result-share-dialog body { overflow: hidden; overscroll-behavior: none; }
.result-share-dialog {
    display: none;
    position: fixed;
    inset: 0;
    width: min(460px, calc(100% - 24px));
    max-width: none;
    max-height: calc(100vh - 24px);
    max-height: calc(100dvh - 24px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    margin: auto;
    padding: 0;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 22px;
    color: var(--ink);
    background: #fffdf8;
    box-shadow: 0 24px 80px rgba(24, 37, 32, .3);
}
.result-share-dialog[open] { display: flex; flex-direction: column; }
.result-share-dialog::backdrop { background: rgba(24, 37, 32, .55); }
.result-share-dialog-header { display: flex; flex: 0 0 auto; justify-content: flex-end; padding: 4px 6px 0; }
.result-share-close { width: 44px; height: 44px; padding: 0; border: 0; border-radius: 12px; color: var(--muted); font-size: 28px; line-height: 1; background: transparent; cursor: pointer; }
.result-share-close:focus-visible { outline: 3px solid var(--green); outline-offset: -3px; }
.result-share-dialog-content { min-height: 0; padding: 0 20px; overflow-y: auto; overscroll-behavior: contain; }
.result-share-dialog .result-share-card { margin: 0; padding: 0; border: 0; border-radius: 0; background: transparent; }
.result-share-dialog .share-actions { flex-direction: column; }
.result-share-dialog .share-actions .button { flex: 0 0 auto; width: 100%; }
.result-share-dialog #result-share-native { order: -1; }
.result-share-dialog .result-share-text { resize: none; }
.result-share-dialog-footer { flex: 0 0 auto; padding: 12px 20px 16px; }
.result-share-dialog-footer .button { width: 100%; }
