.hm-page {
    margin: 0;
    background: #10261f;
    color: #f4efe4;
    font-family: "Segoe UI", system-ui, sans-serif;
}

.hm-shell {
    max-width: 1280px;
    margin: 0 auto;
    padding: 1rem 1rem 2.5rem;
}

/* Desktop play: fill the visible viewport under the fixed game header */
body.hm-page.with-game-header .hm-shell {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    width: min(1600px, calc(100vw - 1.5rem));
    max-width: none;
    height: calc(100dvh - 80px);
    max-height: calc(100dvh - 80px);
    padding: 0.65rem 1rem 0.55rem;
}

.hm-hud,
.hm-toolbar {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-bottom: 0.55rem;
    flex: 0 0 auto;
}

.hm-lcd,
.hm-chip {
    background: #0c1c17;
    border: 1px solid #c6a15b;
    border-radius: 10px;
    padding: 0.45rem 0.7rem;
    color: #f4c430;
    font-weight: 700;
}

.hm-lcd span {
    display: block;
    color: #c6a15b;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.hm-board {
    display: flex;
    gap: 0.85rem;
    align-items: stretch;
    flex: 1 1 auto;
    min-height: 0;
}

.hm-gallows {
    background: #0a0a0a;
    border: 2px solid #c6a15b;
    border-radius: 16px;
    flex: 0 0 auto;
    align-self: stretch;
    height: 100%;
    width: auto;
    aspect-ratio: 1;
    /* Prefer a square as tall as the board; leave room for the word + keys */
    max-width: calc(100% - 300px);
    min-width: 220px;
    min-height: 0;
    display: grid;
    place-items: center;
    padding: 0.2rem;
    overflow: hidden;
    position: relative;
    box-sizing: border-box;
}

.hm-side {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    height: 100%;
    overflow: hidden;
}

.hm-gallows.is-escape,
.hm-gallows.is-collapse {
    overflow: visible;
    z-index: 3;
}

.hm-robot {
    position: relative;
    width: 100%;
    height: 100%;
    aspect-ratio: 1;
    max-width: 100%;
}

.hm-swing-rig,
.hm-escape-rig,
.hm-run-bob {
    position: absolute;
    inset: 0;
}

.hm-swing-rig {
    transform-origin: 53.5% 26%;
}

.hm-swing-rig.is-swinging {
    animation: hm-swing 0.85s ease-in-out;
}

.hm-escape-rig {
    transform-origin: 53.5% 40%;
    animation: hm-escape 1.85s ease-in forwards;
}

.hm-escape-rig .hm-run-bob {
    animation: hm-run-bob 0.28s ease-in-out 0.55s infinite;
}

.hm-escape-rig .hm-arm-l {
    transform-origin: 48% 42%;
    animation: hm-arm-raise-l 0.35s ease-out 0.28s forwards;
}

.hm-escape-rig .hm-arm-r {
    transform-origin: 58% 42%;
    animation: hm-arm-raise-r 0.35s ease-out 0.28s forwards;
}

.hm-robot .hm-part {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
    user-select: none;
}

.hm-robot .hm-part.is-fresh {
    animation: hm-pop 0.45s ease;
}

.hm-robot .hm-eyes {
    z-index: 5;
    mix-blend-mode: screen;
    animation: hm-eye-flicker 3.4s steps(1, end) infinite;
    will-change: opacity, filter;
}

.hm-robot .hm-eyes.is-fresh {
    animation:
        hm-pop 0.45s ease,
        hm-eye-flicker 3.4s steps(1, end) 0.45s infinite;
}

.hm-robot .hm-cog {
    z-index: 4;
    transform-origin: 54.39% 45.41%;
    animation: hm-cog-spin var(--hm-cog-dur, 5s) linear infinite;
    will-change: transform;
}

.hm-robot .hm-cog.is-fresh {
    animation:
        hm-pop 0.45s ease,
        hm-cog-spin var(--hm-cog-dur, 5s) linear 0.45s infinite;
}

/* Lose: eye lights go out, cog stops */
.hm-robot.is-dead .hm-eyes,
.hm-robot .hm-eyes.hm-eyes-out {
    animation: hm-eyes-die 0.4s ease forwards;
    mix-blend-mode: normal;
}

.hm-robot.is-dead .hm-cog,
.hm-robot .hm-cog.hm-cog-stopped {
    animation: none;
}

.hm-robot.is-collapsing .hm-cog.hm-fall-body {
    animation-name: hm-fall-body;
    animation-duration: 1.05s;
    animation-timing-function: cubic-bezier(0.45, 0.05, 0.7, 1.1);
    animation-delay: 0.05s;
    animation-fill-mode: forwards;
}

@keyframes hm-eye-flicker {
    0%, 100% { opacity: 0.92; filter: brightness(1.05); }
    6% { opacity: 0.28; filter: brightness(0.45); }
    8% { opacity: 1; filter: brightness(1.45); }
    11% { opacity: 0.7; filter: brightness(0.85); }
    18% { opacity: 0.95; filter: brightness(1.15); }
    37% { opacity: 0.2; filter: brightness(0.35); }
    39% { opacity: 1; filter: brightness(1.55); }
    42% { opacity: 0.55; filter: brightness(0.7); }
    44% { opacity: 0.98; filter: brightness(1.2); }
    63% { opacity: 0.9; filter: brightness(1.05); }
    71% { opacity: 0.35; filter: brightness(0.5); }
    73% { opacity: 1; filter: brightness(1.4); }
    88% { opacity: 0.85; filter: brightness(1); }
}

@keyframes hm-eyes-die {
    0% { opacity: 0.9; filter: brightness(1.1); }
    35% { opacity: 0.55; filter: brightness(0.6); }
    55% { opacity: 0.15; filter: brightness(0.25); }
    100% { opacity: 0; filter: brightness(0); }
}

@keyframes hm-cog-spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(calc(360deg * var(--hm-cog-dir, 1))); }
}

@keyframes hm-pop {
    0% { filter: brightness(2.2); }
    100% { filter: brightness(1); }
}

@keyframes hm-swing {
    0% { transform: rotate(0deg); }
    18% { transform: rotate(7deg); }
    42% { transform: rotate(-5.5deg); }
    64% { transform: rotate(3.2deg); }
    82% { transform: rotate(-1.4deg); }
    100% { transform: rotate(0deg); }
}

@keyframes hm-arm-raise-l {
    from { transform: rotate(0deg); }
    to { transform: rotate(-125deg); }
}

@keyframes hm-arm-raise-r {
    from { transform: rotate(0deg); }
    to { transform: rotate(125deg); }
}

@keyframes hm-run-bob {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-6%); }
}

@keyframes hm-escape {
    0% {
        transform: translate(0, 0) rotate(0deg);
        opacity: 1;
    }
    12% {
        transform: translate(1%, 10%) rotate(-10deg);
    }
    22% {
        transform: translate(4%, 22%) rotate(6deg);
    }
    32% {
        transform: translate(8%, 16%) rotate(0deg);
    }
    48% {
        transform: translate(28%, 14%);
    }
    70% {
        transform: translate(70%, 12%);
        opacity: 1;
    }
    100% {
        transform: translate(145%, 10%);
        opacity: 0;
    }
}

.hm-robot.is-collapsing .hm-fall {
    animation-duration: 1.05s;
    animation-timing-function: cubic-bezier(0.45, 0.05, 0.7, 1.1);
    animation-fill-mode: forwards;
}

.hm-robot.is-collapsing .hm-fall-body {
    animation-name: hm-fall-body;
    animation-delay: 0.05s;
    z-index: 2;
}

.hm-robot.is-collapsing .hm-fall-larm {
    animation-name: hm-fall-larm;
    animation-delay: 0.12s;
    z-index: 3;
}

.hm-robot.is-collapsing .hm-fall-rarm {
    animation-name: hm-fall-rarm;
    animation-delay: 0.18s;
    z-index: 3;
}

.hm-robot.is-collapsing .hm-fall-lleg {
    animation-name: hm-fall-lleg;
    animation-delay: 0.22s;
    z-index: 2;
}

.hm-robot.is-collapsing .hm-fall-rleg {
    animation-name: hm-fall-rleg;
    animation-delay: 0.28s;
    z-index: 2;
}

.hm-robot.is-collapsing .hm-fall-head {
    animation-name: hm-fall-head;
    animation-delay: 0.38s;
    z-index: 4;
}

@keyframes hm-fall-body {
    0% { transform: translate(0, 0) rotate(0deg); }
    55% { transform: translate(-1%, 28%) rotate(-18deg); }
    75% { transform: translate(-1%, 34%) rotate(-12deg); }
    100% { transform: translate(-1%, 33%) rotate(-16deg); }
}

@keyframes hm-fall-larm {
    0% { transform: translate(0, 0) rotate(0deg); }
    60% { transform: translate(-10%, 30%) rotate(-95deg); }
    100% { transform: translate(-9%, 35%) rotate(-78deg); }
}

@keyframes hm-fall-rarm {
    0% { transform: translate(0, 0) rotate(0deg); }
    60% { transform: translate(11%, 31%) rotate(100deg); }
    100% { transform: translate(10%, 36%) rotate(84deg); }
}

@keyframes hm-fall-lleg {
    0% { transform: translate(0, 0) rotate(0deg); }
    65% { transform: translate(-6%, 34%) rotate(48deg); }
    100% { transform: translate(-5%, 37%) rotate(36deg); }
}

@keyframes hm-fall-rleg {
    0% { transform: translate(0, 0) rotate(0deg); }
    65% { transform: translate(7%, 35%) rotate(-52deg); }
    100% { transform: translate(6%, 38%) rotate(-40deg); }
}

@keyframes hm-fall-head {
    0% { transform: translate(0, 0) rotate(0deg); }
    45% { transform: translate(3%, 18%) rotate(40deg); }
    70% { transform: translate(5%, 34%) rotate(78deg); }
    85% { transform: translate(4%, 37%) rotate(62deg); }
    100% { transform: translate(5%, 36%) rotate(70deg); }
}

@media (prefers-reduced-motion: reduce) {
    .hm-swing-rig.is-swinging,
    .hm-robot .hm-part.is-fresh,
    .hm-robot .hm-eyes,
    .hm-robot .hm-cog,
    .hm-escape-rig,
    .hm-escape-rig .hm-run-bob,
    .hm-escape-rig .hm-arm-l,
    .hm-escape-rig .hm-arm-r,
    .hm-robot.is-collapsing .hm-fall {
        animation: none;
    }

    .hm-robot .hm-eyes {
        opacity: 0.9;
        filter: brightness(1.1);
    }

    .hm-robot.is-dead .hm-eyes,
    .hm-robot .hm-eyes.hm-eyes-out {
        opacity: 0;
        filter: brightness(0);
    }

    .hm-robot.is-collapsing .hm-fall-body { transform: translate(-1%, 33%) rotate(-16deg); }
    .hm-robot.is-collapsing .hm-fall-larm { transform: translate(-9%, 35%) rotate(-78deg); }
    .hm-robot.is-collapsing .hm-fall-rarm { transform: translate(10%, 36%) rotate(84deg); }
    .hm-robot.is-collapsing .hm-fall-lleg { transform: translate(-5%, 37%) rotate(36deg); }
    .hm-robot.is-collapsing .hm-fall-rleg { transform: translate(6%, 38%) rotate(-40deg); }
    .hm-robot.is-collapsing .hm-fall-head { transform: translate(5%, 36%) rotate(70deg); }
}

.hm-card {
    background: #f7f3e8;
    color: #1a1208;
    border-radius: 16px;
    padding: 1rem 1.1rem 1.15rem;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.hm-strand {
    margin: 0;
    color: #8a5a12;
    font-size: 0.82rem;
    font-weight: 800;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.hm-clue {
    margin: 0.35rem 0 0.9rem;
    font-size: 1.15rem;
    line-height: 1.4;
}

.hm-tiles {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    min-height: 3.2rem;
    flex: 1 1 auto;
    align-content: center;
}

.hm-tile {
    width: 2.15rem;
    height: 2.7rem;
    border-bottom: 3px solid #1a1208;
    display: grid;
    place-items: center;
    font-size: 1.45rem;
    font-weight: 800;
    text-transform: uppercase;
    color: #143028;
}

.hm-tile.is-missed {
    color: #c90c0f;
}

.hm-status {
    min-height: 1.3rem;
    margin: 0.75rem 0 0;
    font-weight: 700;
    color: #5c3b12;
}

.hm-keys {
    display: grid;
    grid-template-columns: repeat(9, minmax(0, 1fr));
    gap: 0.4rem;
    margin-top: 0;
    flex: 0 0 auto;
    background: #0a0a0a;
    border: 2px solid #c6a15b;
    border-radius: 16px;
    padding: 0.7rem;
}

.hm-key {
    min-height: clamp(34px, 5.2vh, 44px);
    border-radius: 8px;
    border: 2px solid #1a1208;
    background: #fff;
    color: #1a1208;
    font-weight: 800;
    cursor: pointer;
}

.hm-key:hover {
    background: #f4c430;
    color: #1a1208;
}

.hm-key.is-hit {
    background: #143028;
    color: #f4efe4;
    border-color: #143028;
}

.hm-key.is-miss {
    background: #e7e5e4;
    color: #57534e;
    border-color: #a8a29e;
}

.hm-key:disabled {
    cursor: default;
}

.hm-tool-btn {
    background: #1e3344;
    color: #f4efe4;
    border: 2px solid #c6a15b;
    border-radius: 10px;
    min-height: 44px;
    padding: 0.4rem 0.75rem;
    font-weight: 700;
    cursor: pointer;
}

.hm-tool-btn:hover {
    background: #c6a15b;
    color: #1a1208;
}

.hm-note {
    margin: 0.55rem 0 0;
    color: #d7c7a1;
    flex: 0 0 auto;
    font-size: 0.9rem;
    line-height: 1.35;
}

body.hm-page.with-game-header .hm-note {
    margin-top: 0.45rem;
}

.hm-settings-note {
    margin: 0.75rem 0 0;
    color: #5c3b12;
}

.difficulty-pill {
    padding: 0.45rem 0.75rem;
    min-height: 44px;
    border-radius: 999px;
    border: 2px solid #c90c0f;
    background: #fff !important;
    color: #c90c0f !important;
    font-weight: 700;
    cursor: pointer;
}

.difficulty-pill.is-selected,
.difficulty-pill:hover {
    background: #c90c0f !important;
    color: #fff !important;
}

.modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.72);
    z-index: 12000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: #fff;
    color: #1a1a1a;
    border-radius: 1rem;
    max-width: 640px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-large {
    max-width: 760px;
}

.modal-header,
.modal-footer {
    padding: 1.1rem 1.4rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.modal-header {
    border-bottom: 2px solid #eee;
}

.modal-header h2 {
    margin: 0;
    color: #c90c0f;
    font-size: 1.35rem;
}

.modal-close {
    background: #fff !important;
    border: 2px solid #c90c0f;
    color: #c90c0f !important;
    border-radius: 8px;
    cursor: pointer;
    padding: 0.35rem 0.55rem;
}

.modal-close:hover {
    background: #c90c0f !important;
    color: #fff !important;
}

.modal-body {
    padding: 1.2rem 1.4rem;
}

.modal-footer {
    border-top: 2px solid #eee;
    justify-content: flex-end;
    flex-wrap: wrap;
}

.help-section {
    margin-bottom: 1.1rem;
}

.help-section h3 {
    color: #c90c0f;
    margin: 0 0 0.4rem;
}

.help-section ul {
    margin: 0.3rem 0 0;
    padding-left: 1.2rem;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.stat-card {
    background: #f8fafc;
    border-radius: 12px;
    padding: 0.85rem;
    text-align: center;
}

.stat-value {
    font-size: 1.6rem;
    color: #c90c0f;
    font-weight: 700;
}

.stat-label,
.win-stat span {
    color: #64748b;
    font-size: 0.85rem;
}

.difficulty-stats {
    display: grid;
    gap: 0.45rem;
}

.difficulty-stats div {
    display: flex;
    justify-content: space-between;
    padding: 0.45rem 0.55rem;
    background: #f8fafc;
    border-radius: 8px;
}

.result-message {
    font-size: 1.05rem;
    margin: 0 0 1rem;
}

.win-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.6rem;
    text-align: center;
}

.win-stat strong {
    display: block;
    font-size: 1.35rem;
    color: #c90c0f;
}

.hm-page .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.65rem 1.1rem;
    min-height: 44px;
    border-radius: 0.55rem;
    border: 2px solid transparent;
    font-weight: 700;
    cursor: pointer;
}

.hm-page .btn-primary {
    background: #c90c0f !important;
    color: #fff !important;
    border-color: #c90c0f;
}

.hm-page .btn-secondary {
    background: #fff !important;
    color: #c90c0f !important;
    border-color: #c90c0f;
}

.hm-page .btn-secondary:hover,
.hm-page .btn-primary:hover {
    background: #9d090c !important;
    color: #fff !important;
    border-color: #9d090c;
}

@media (max-width: 700px) {
    body.hm-page.with-game-header .hm-shell {
        height: auto;
        max-height: none;
        padding: 4.6rem 0.85rem 1.5rem;
    }

    .hm-board {
        display: flex;
        flex-direction: column;
        flex: 0 0 auto;
        min-height: 0;
    }

    .hm-gallows {
        width: min(100%, 340px);
        height: auto;
        max-width: 340px;
        aspect-ratio: 1;
        margin: 0 auto;
        align-self: center;
    }

    .hm-side {
        height: auto;
        min-height: 0;
        overflow: visible;
    }

    .hm-card {
        flex: 0 0 auto;
    }

    .hm-tiles {
        flex: 0 0 auto;
    }

    .hm-keys {
        grid-template-columns: repeat(7, minmax(0, 1fr));
    }
}

