* {
    box-sizing: border-box;
}

:root {
    --board-cell-size: 64px;
    --cell-gap: 4px;
    --board-padding: 8px;
}

[hidden] {
    display: none !important;
}

body {
    margin: 0;
    min-height: 100vh;
    font-family: Arial, sans-serif;
    color: #1f2933;
    background: #f4f7fb;
}

.app {
    width: min(980px, 100%);
    margin: 0 auto;
    padding: 32px 20px 120px;
}

.app-header {
    margin-bottom: 24px;
}

.stage-label {
    margin: 0 0 6px;
    color: #6b7785;
    font-size: 14px;
    font-weight: 700;
}

.app-header h1 {
    margin: 0 0 8px;
    font-size: 36px;
}

.app-header p:last-child {
    margin: 0;
    color: #5f6f82;
}

.game-area {
    display: grid;
    grid-template-columns: max-content 340px;
    gap: 24px;
    align-items: start;
}

.board-scene {
    position: relative;
    width: max-content;
    padding: var(--scene-top-pad, 0px) var(--scene-right-pad, 0px) var(--scene-bottom-pad, 0px) var(--scene-left-pad, 0px);
}

.board {
    display: grid;
    grid-template-columns: repeat(var(--board-width, var(--board-size)), var(--board-cell-size));
    grid-template-rows: repeat(var(--board-height, var(--board-size)), var(--board-cell-size));
    gap: var(--cell-gap);
    padding: var(--board-padding);
    background: #d8e0ea;
    border: 2px solid #9aa9b8;
    touch-action: none;
}

.cell {
    position: relative;
    width: var(--board-cell-size);
    height: var(--board-cell-size);
    display: grid;
    place-items: center;
    overflow: visible;
    border: 1px solid #9aa9b8;
    background: #ffffff;
    user-select: none;
}

.cell.filled {
    background: var(--piece-color);
    border-color: rgba(31, 41, 51, 0.2);
    cursor: grab;
}

.cell.ghost {
    background: color-mix(in srgb, var(--piece-color) 30%, white);
    outline: 3px solid color-mix(in srgb, var(--piece-color) 55%, transparent);
    outline-offset: -5px;
}

.cell.ghost.invalid {
    background: rgba(120, 130, 145, 0.22);
    outline-color: rgba(120, 130, 145, 0.55);
}

.peg {
    position: absolute;
    z-index: 5;
    width: 34%;
    height: 34%;
    border: 3px solid #334155;
    border-radius: 50%;
    background: #f8fafc;
    box-shadow: 0 2px 4px rgba(31, 41, 51, 0.18);
}

.peg.colored {
    border-color: color-mix(in srgb, var(--peg-color) 70%, #1f2933);
    background: color-mix(in srgb, var(--peg-color) 58%, #ffffff);
}

.hole {
    position: absolute;
    z-index: 4;
    width: 46%;
    height: 46%;
    border: 3px solid rgba(31, 41, 51, 0.42);
    border-radius: 50%;
    background: #f4f7fb;
    box-shadow: inset 0 1px 3px rgba(31, 41, 51, 0.22);
}

.turn-marker {
    position: absolute;
    z-index: 6;
    display: grid;
    place-items: center;
    width: 44%;
    height: 44%;
    border-radius: 50%;
    color: #1f2933;
    font-size: clamp(12px, 35%, 18px);
    font-weight: 900;
    line-height: 1;
    pointer-events: none;
}

.turn-marker.turn-l {
    color: #0f766e;
}

.turn-marker.turn-r {
    color: #b42318;
}

.magnet-marker {
    position: absolute;
    left: 50%;
    top: 50%;
    z-index: 7;
    display: grid;
    place-items: center;
    width: auto;
    height: auto;
    color: #1f2933;
    font-size: clamp(18px, 52%, 28px);
    font-weight: 900;
    line-height: 1;
    pointer-events: none;
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.78), 0 0 5px rgba(255, 255, 255, 0.65);
    transform: translate(-50%, -50%);
}

.magnet-marker.magnet-n {
    color: #c62828;
}

.magnet-marker.magnet-s {
    color: #1d4ed8;
}

.field-line {
    position: absolute;
    z-index: 5;
    pointer-events: none;
    background: #f59e0b;
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.48);
}

.field-line::after {
    content: "";
    position: absolute;
    width: 0;
    height: 0;
}

.field-line.e,
.field-line.w {
    top: 50%;
    width: calc(100% + var(--cell-gap));
    height: 5px;
    transform: translateY(-50%);
}

.field-line.n,
.field-line.s {
    left: 50%;
    width: 5px;
    height: calc(100% + var(--cell-gap));
    transform: translateX(-50%);
}

.field-line.e {
    left: 50%;
}

.field-line.e::after {
    right: -2px;
    top: -5px;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
    border-left: 10px solid #f59e0b;
}

.field-line.w {
    right: 50%;
}

.field-line.w::after {
    left: -2px;
    top: -5px;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
    border-right: 10px solid #f59e0b;
}

.field-line.n {
    bottom: 50%;
}

.field-line.n::after {
    left: -5px;
    top: -2px;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-bottom: 10px solid #f59e0b;
}

.field-line.s {
    top: 50%;
}

.field-line.s::after {
    left: -5px;
    bottom: -2px;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-top: 10px solid #f59e0b;
}

.path-segment {
    position: absolute;
    z-index: 3;
    background: #25364a;
    pointer-events: none;
}

.path-segment.n,
.path-segment.s {
    width: 12%;
    height: calc(50% + 4px);
    left: 50%;
    transform: translateX(-50%);
}

.path-segment.e,
.path-segment.w {
    width: calc(50% + 4px);
    height: 12%;
    top: 50%;
    transform: translateY(-50%);
}

.path-segment.n {
    top: 0;
}

.path-segment.s {
    bottom: 0;
}

.path-segment.e {
    right: 0;
}

.path-segment.w {
    left: 0;
}

.endpoint {
    position: absolute;
    z-index: 2;
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    border: 2px solid #25364a;
    border-radius: 50%;
    background: #ffffff;
    color: #25364a;
    font-weight: 800;
}

.endpoint::after {
    content: "";
    position: absolute;
    background: #25364a;
}

.endpoint.dir-w,
.endpoint.dir-e {
    top: calc(var(--scene-top-pad, 0px) + var(--board-padding) + var(--endpoint-row) * (var(--board-cell-size) + var(--cell-gap)) + (var(--board-cell-size) - 34px) / 2);
}

.endpoint.dir-w {
    left: 0;
}

.endpoint.dir-w::after {
    left: 32px;
    width: 28px;
    height: 8px;
}

.endpoint.dir-e {
    right: 0;
}

.endpoint.dir-e::after {
    right: 32px;
    width: 28px;
    height: 8px;
}

.endpoint.dir-n,
.endpoint.dir-s {
    left: calc(var(--scene-left-pad, 0px) + var(--board-padding) + var(--endpoint-col) * (var(--board-cell-size) + var(--cell-gap)) + (var(--board-cell-size) - 34px) / 2);
}

.endpoint.dir-n {
    top: 0;
}

.endpoint.dir-n::after {
    top: 32px;
    left: 50%;
    width: 8px;
    height: 28px;
    transform: translateX(-50%);
}

.endpoint.dir-s {
    bottom: 0;
}

.endpoint.dir-s::after {
    bottom: 32px;
    left: 50%;
    width: 8px;
    height: 28px;
    transform: translateX(-50%);
}

.side-panel {
    padding: 16px;
    border: 1px solid #c6d1dd;
    background: #ffffff;
}

.level-nav {
    display: grid;
    grid-template-columns: 44px 1fr 44px;
    gap: 8px;
    align-items: center;
    margin-bottom: 16px;
}

.level-picker {
    position: relative;
}

.arrow-button,
.solver-button {
    border: 1px solid #8fa1b2;
    background: #ffffff;
    color: #1f2933;
    cursor: pointer;
}

.arrow-button {
    height: 40px;
    font-size: 22px;
}

.arrow-button:disabled {
    color: #9aa9b8;
    cursor: not-allowed;
    background: #edf1f5;
}

.current-stage {
    display: grid;
    place-items: center;
    width: 100%;
    height: 40px;
    border: 1px solid #25364a;
    background: #25364a;
    color: #ffffff;
    cursor: pointer;
    font-weight: 800;
}

.current-stage:hover,
.current-stage[aria-expanded="true"] {
    background: #344759;
}

.current-stage.solved {
    border-color: #117a45;
    background: #16834f;
    color: #ffffff;
}

.current-stage.solved:hover,
.current-stage.solved[aria-expanded="true"] {
    background: #0f6b3d;
}

.stage-menu {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    z-index: 12;
    width: 100%;
    padding: 6px;
    border: 1px solid #8fa1b2;
    background: #ffffff;
    box-shadow: 0 12px 26px rgba(31, 41, 51, 0.16);
}

.stage-menu-item {
    width: 100%;
    min-height: 34px;
    border: 0;
    background: #ffffff;
    color: #25364a;
    cursor: pointer;
    font-weight: 800;
    text-align: center;
}

.stage-menu-item:hover,
.stage-menu-item[aria-checked="true"] {
    background: #eef4fb;
}

.stage-menu-item.solved {
    color: #117a45;
}

.stage-menu-item.solved[aria-checked="true"] {
    background: #dcfce7;
}

.size-controls {
    display: grid;
    gap: 10px;
    margin: 0 0 14px;
    padding: 12px;
    border: 1px solid #d7e0ea;
    background: #f8fafc;
}

.size-controls label {
    display: grid;
    grid-template-columns: 52px 1fr 32px;
    gap: 10px;
    align-items: center;
    color: #344759;
    font-size: 13px;
    font-weight: 700;
}

.size-controls input[type="range"] {
    width: 100%;
    accent-color: #25364a;
    cursor: pointer;
}

.size-controls output {
    text-align: right;
    color: #5f6f82;
    font-variant-numeric: tabular-nums;
}

.side-panel h2 {
    margin: 0 0 12px;
    font-size: 20px;
}

.piece-tray {
    min-height: 260px;
    display: flex;
    flex-wrap: wrap;
    gap: 18px 16px;
    align-items: flex-start;
    margin-bottom: 18px;
    padding: 14px;
    border: 1px dashed #9aa9b8;
    background: #f8fafc;
}

.piece {
    position: relative;
    flex: 0 0 auto;
    cursor: grab;
    touch-action: none;
    outline: none;
}

.piece:focus,
.piece:focus-visible,
.piece:active {
    outline: none;
}

.piece.source-dragging {
    opacity: 0.18;
}

.piece.dragging {
    position: fixed;
    z-index: 20;
    pointer-events: none;
    cursor: grabbing;
    opacity: 0.92;
}

.piece.placed {
    visibility: hidden;
}

.piece-cell {
    position: absolute;
    display: grid;
    place-items: center;
    border: 1px solid rgba(31, 41, 51, 0.2);
    background: var(--piece-color);
    box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.14);
}

.solver-button {
    width: 100%;
    padding: 10px 12px;
    font-size: 16px;
}

.solver-button:hover,
.arrow-button:not(:disabled):hover {
    background: #eef4fb;
}

.solver-report {
    min-height: 24px;
    margin: 10px 0 0;
    color: #344759;
    font-weight: 700;
    line-height: 1.4;
}

.message {
    margin: 16px 0 0;
    color: #5f6f82;
    line-height: 1.5;
}

.message.success {
    padding: 14px 16px;
    border: 2px solid #117a45;
    background: #dcfce7;
    color: #14532d;
    font-size: 18px;
    font-weight: 800;
    box-shadow: 0 8px 20px rgba(17, 122, 69, 0.18);
}

.help-card {
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 30;
    width: 96px;
}

.help-card:has(.help-content:not([hidden])) {
    width: min(320px, calc(100vw - 40px));
}

.help-toggle {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid #25364a;
    background: #25364a;
    color: #ffffff;
    cursor: pointer;
    font-size: 16px;
    font-weight: 800;
    box-shadow: 0 10px 30px rgba(31, 41, 51, 0.16);
}

.help-toggle:hover {
    background: #344759;
}

.help-content {
    margin-top: 8px;
    padding: 14px;
    border: 1px solid #c6d1dd;
    background: rgba(255, 255, 255, 0.97);
    box-shadow: 0 10px 30px rgba(31, 41, 51, 0.12);
    max-height: min(560px, calc(100vh - 96px));
    overflow-y: auto;
}

.help-section + .help-section {
    margin-top: 14px;
    padding-top: 14px;
    border-top: 1px solid #e1e8f0;
}

.secondary-toggle {
    width: 100%;
    padding: 9px 10px;
    border: 1px solid #8fa1b2;
    background: #ffffff;
    color: #25364a;
    cursor: pointer;
    font-weight: 800;
}

.secondary-toggle:hover {
    background: #eef4fb;
}

.rules-content {
    display: grid;
    gap: 12px;
    margin-top: 12px;
    padding: 12px;
    border: 1px solid #d7e0ea;
    background: #f8fafc;
}

.rules-content[hidden] {
    display: none;
}

.help-card h2 {
    margin: 0 0 8px;
    color: #25364a;
    font-size: 14px;
}

.rule-list {
    display: grid;
    gap: 6px;
    margin: 0;
    padding-left: 18px;
    color: #344759;
    font-size: 13px;
    line-height: 1.45;
}

.help-card dl {
    display: grid;
    gap: 6px;
    margin: 0;
}

.help-card dl > div {
    display: grid;
    grid-template-columns: 68px 1fr;
    gap: 8px;
    align-items: center;
}

.help-card dt {
    display: grid;
    place-items: center;
    min-height: 28px;
    border: 1px solid #9aa9b8;
    background: #eef4fb;
    font-size: 13px;
    font-weight: 800;
}

.key-bind-button {
    width: 100%;
    min-height: 28px;
    border: 0;
    background: transparent;
    color: #25364a;
    cursor: pointer;
    font: inherit;
}

.key-bind-button:hover,
.key-bind-button.listening {
    background: #dbeafe;
}

.help-card dd {
    margin: 0;
    color: #344759;
    font-size: 13px;
}

.rule-overlay {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: grid;
    place-items: center;
    padding: 20px;
    background: rgba(31, 41, 51, 0.42);
}

.rule-dialog {
    position: relative;
    width: min(420px, 100%);
    padding: 22px;
    border: 1px solid #8fa1b2;
    background: #ffffff;
    box-shadow: 0 22px 50px rgba(31, 41, 51, 0.28);
}

.rule-close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 32px;
    height: 32px;
    border: 1px solid #9aa9b8;
    background: #ffffff;
    color: #25364a;
    cursor: pointer;
    font-size: 18px;
    font-weight: 800;
    line-height: 1;
}

.rule-close:hover {
    background: #eef4fb;
}

.rule-eyebrow {
    margin: 0 0 6px;
    color: #6b7785;
    font-size: 13px;
    font-weight: 800;
}

.rule-dialog h2 {
    margin: 0 36px 14px 0;
    color: #1f2933;
    font-size: 22px;
}

.rule-dialog .rules-content {
    margin-top: 0;
}

@supports not (background: color-mix(in srgb, red 50%, white)) {
    .cell.ghost {
        background: rgba(225, 95, 65, 0.28);
        outline-color: rgba(225, 95, 65, 0.55);
    }
}

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

    .help-card,
    .help-card:has(.help-content:not([hidden])) {
        position: static;
        width: auto;
        margin: 0 20px 20px;
    }
}
