:root {
    font-family: Roboto, "Arial", sans-serif;
    color: #242747;
    background: #eff1fa;
    font-synthesis: none;
    -webkit-tap-highlight-color: transparent;
    color-scheme: light;
    --ink: #242747;
    --subtle: #566285;
    --primary: #3c43bd;
    --primary-dark: #30349a;
    --border: #dce1f3;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
    margin: 0;
    min-width: 260px;
    background: radial-gradient(ellipse at 68% 0%, #fff 0%, #eff1fa 48%, #e8ecfb 100%);
    overscroll-behavior: none;
}

button {
    font: inherit;
    color: inherit;
    border: 0;
    cursor: pointer;
    touch-action: manipulation;
}

button:focus-visible { outline: 3px solid #9399ee; outline-offset: 3px; }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { cursor: default; }

.app {
    width: 100%;
    max-width: 500px;
    height: 100vh;
    height: 100dvh;
    max-height: 945px;
    margin: 0 auto;
    padding: 14px 14px 15px;
    padding: max(14px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right))
        max(15px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
    display: flex;
    flex-direction: column;

}

.app-header, .brand, .levels-button, .level-header, .goal, .controls, .board-state, .sheet-header {
    display: flex;
    align-items: center;
}

.app-header { justify-content: space-between; }
.brand h1 { margin-left: 10px !important; }
.brand h1 { margin: 0; font-size: 22px; font-weight: 900; letter-spacing: -0.8px; }

.brand-icon {
    width: 41px;
    height: 41px;
    flex: none;
    display: grid;
    place-items: center;
    border-radius: 13px;
    background: linear-gradient(145deg, #373982, #212451);
    box-shadow: 0 3px 6px #24274726;
}

.brand-icon::after {
    content: "";
    width: 16px;
    height: 16px;
    transform: translateY(1px) rotate(-45deg);
    background: linear-gradient(155deg, #d0f6ff, #71c7fc);
    border-radius: 75% 3px 75% 75%;
    box-shadow: inset 2px 1px #e8faffb0;
}

.levels-button {
    height: 44px;
    padding: 0 12px;

    border: 1px solid #d8ddf2;
    border-radius: 13px;
    font-size: 14px;
    font-weight: 700;
    background: #ffffffd9;
    color: #353e7f;
}

.grid-icon {
    position: relative;
    width: 14px;
    height: 14px;
}
.grid-icon::after {
    content: "";
    display: block;
    width: 5px;
    height: 5px;
    border-radius: 1px;
    background: currentColor;
    box-shadow: 8px 0 currentColor, 0 8px currentColor, 8px 8px currentColor;
}

.workbench {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 22px;
    padding: 12px 9px 0;
    box-shadow: 0 8px 22px #383e7513;
}

.level-header {
    padding: 0 5px 11px;
    justify-content: space-between;

}

.level-number {
    color: #4c50ac;
    font-weight: 800;
    font-size: 11px;
    letter-spacing: 1.7px;
    line-height: 1;
    margin-bottom: 5px;
}

.level-copy h2, .sheet h2 {
    font-size: 23px;
    letter-spacing: -0.72px;
    line-height: 1.08;
    margin: 0;
    font-weight: 850;
}

.goal {
    flex: none;

    padding: 7px 9px;
    border-radius: 10px;
    background: #eceffd;
    color: #41478e;
    font-size: 13px;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
}
.goal[data-full="true"] { background: #def6ed; color: #076650; }

.cup-icon {
    display: inline-block;
    width: 14px;
    height: 14px;
    clip-path: polygon(0 0, 15% 0, 29% 83%, 71% 83%, 85% 0, 100% 0, 83% 100%, 17% 100%);
    background: currentColor;
}

.board {
    flex: 1;
    min-height: 0;
    position: relative;
    isolation: isolate;
    overflow: hidden;
    border: 1px solid #d9e1f3;
    border-radius: 14px;
    background: #f4f7ff;
}

canvas {
    display: block;
    position: absolute;
    width: 100%;
    height: 100%;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    cursor: crosshair;
}

body[data-state="running"] canvas, body[data-state="won"] canvas { cursor: default; }

.board-state {
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%);

    padding: 5px 8px;
    border: 1px solid #d6dff4;
    border-radius: 20px;
    background: #f7f9fff2;
    pointer-events: none;
    color: #505b80;
    font-size: 9px;
    font-weight: 850;
    letter-spacing: 1.05px;
    white-space: nowrap;
}

.board-state span:first-child {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #6568c7;
}
body[data-state="running"] .board-state span:first-child { background: #0079d0; }
body[data-state="won"] .board-state span:first-child { background: #008663; }

.timer {
    position: absolute;
    height: 3px;
    bottom: 5px;
    left: 11px;
    right: 11px;
    border-radius: 2px;
    overflow: hidden;
    background: #dbe5f7;
    pointer-events: none;
}
.timer div {
    height: 100%;
    border-radius: inherit;
    background: #5561cf;
    transform-origin: left center;
}

.hint {
    display: flex;
    align-items: center;
    min-height: 42px;
    margin: 0;
    padding: 5px 5px 6px;
    font-size: 13px;
    line-height: 1.24;
    color: var(--subtle);
}

.controls { }

.tool-button, .primary, .secondary {
    display: flex;
    align-items: center;
    justify-content: center;

    min-height: 52px;
    border-radius: 13px;
    font-size: 14px;
    font-weight: 800;
}

.tool-button {
    flex: 0 1 23%;
    padding: 0 8px;
    min-width: 58px;
    background: #fff;
    color: #41478e;
    border: 1px solid #d8def2;
}

.tool-button:disabled {
    background: #f4f5fd;
    color: #727a9b;
    border-color: #dfe3f4;
}

.undo-icon { font-size: 20px; line-height: 15px; font-weight: 600; }
.clear-icon {
    display: inline-block;
    position: relative;
    width: 10px;
    height: 12px;
    margin-top: 2px;
    border: 1.4px solid currentColor;
    border-top-width: 0;
    border-radius: 0 0 2px 2px;
}
.clear-icon::before, .clear-icon::after {
    position: absolute;
    content: "";
    background: currentColor;
    height: 1.4px;
    border-radius: 1px;
}
.clear-icon::before { width: 13px; top: -1px; left: -3px; }
.clear-icon::after { width: 5px; top: -4px; left: 1px; }

.primary {
    flex: 1;
    background: linear-gradient(180deg, #5057d4 0%, var(--primary) 68%, #373db2 100%);
    color: #fff;
    box-shadow: inset 0 -2px #292d963b, 0 5px 11px #3e46c236;
    padding: 0 10px;
}

.primary:active { background: var(--primary-dark); }
.action-icon {
    width: 13px;
    height: 15px;
    flex: none;
    background: #fff;
    clip-path: polygon(6% 0, 99% 50%, 6% 100%);
}
body[data-state="running"] .action-icon {
    width: 11px;
    height: 11px;
    clip-path: none;
    border-radius: 2px;
}

.sheet {
    width: calc(100vw - 32px);
    max-width: 436px;
    max-height: calc(100vh - 30px);
    max-height: calc(100dvh - 30px);
    padding: 20px;
    border: 1px solid #d9def3;
    border-radius: 22px;
    box-shadow: 0 25px 60px #22254a55;
    color: var(--ink);
    background: #fdfdff;
}
.sheet::backdrop { background: #1c23579e; }
.sheet-header { justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.sheet p { margin: 6px 0 0; color: var(--subtle); font-size: 14px; }
.close {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    color: #404887;
    background: #edf0ff;
    font-size: 26px;
    flex: none;
}
.level-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.level-pages {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-gap: 6px;
    margin-bottom: 12px;
}
.level-page {
    min-height: 44px;
    padding: 4px;
    border: 1px solid #dde2f5;
    border-radius: 10px;
    background: #f5f7ff;
    color: #41478e;
    font-size: 12px;
    font-weight: 800;
    white-space: nowrap;
}
.level-page[aria-pressed="true"] {
    background: #4349bc;
    border-color: #4349bc;
    color: white;
}
.level-option {
    display: flex;
    align-items: center;
    text-align: left;

    min-height: 48px;
    padding: 6px;
    border: 1px solid #dde2f5;
    border-radius: 11px;
    background: #f5f7ff;
    color: #363b67;
    font-size: 12px;
    font-weight: 800;
}
.level-option[aria-current="true"] { border-color: #868ceb; background: #eeefff; }
.level-option .level-index {
    display: grid;
    place-items: center;
    flex: none;
    width: 23px;
    height: 23px;
    border-radius: 7px;
    background: #e6eaff;
    color: #42499c;
    font-size: 11px;
}
.level-option[data-solved="true"] .level-index { background: #d9f6e9; color: #0c694f; }

.win-sheet {
    position: fixed;
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    margin: 0 auto 14px;
    margin-bottom: max(14px, env(safe-area-inset-bottom));
    text-align: center;
}
.win-sheet[open] { animation: win-slide 200ms ease-out; }
.win-sheet::backdrop { background: #1c23574d; }
@keyframes win-slide {
    from { transform: translateY(24px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
    .win-sheet[open] { animation: none; }
}
.win-sheet .primary, .win-sheet .secondary { width: 100%; }
.win-sheet .primary { margin-top: 19px; }
.secondary { background: #eff1fd; color: #3e438d; margin-top: 8px; }
.win-medallion {
    position: relative;
    display: grid;
    place-items: center;
    width: 64px;
    height: 64px;
    color: #08775c;
    background: #dcf8ee;
    border-radius: 20px;
    margin: 1px auto 14px;
}
.win-medallion .cup-icon { width: 30px; height: 27px; }
.win-check {
    position: absolute;
    right: 6px;
    top: 4px;
    font-size: 15px;
    font-weight: 800;
}

@media (max-height: 620px) {
    .app { padding-top: 8px; padding-bottom: 8px; }
    .workbench { padding-top: 8px; }
    .level-header { padding-bottom: 7px; }
    .hint { min-height: 34px; font-size: 12px; }
    .tool-button, .primary, .secondary { min-height: 46px; }
}

@media (max-width: 340px) {
    .app { padding-left: 8px; padding-right: 8px; }
    .controls { gap: 5px; }
    .tool-button { padding: 0 5px; gap: 4px; font-size: 13px; }
}

.app-header { margin-bottom: 12px; }
.workbench { margin-bottom: 12px; }
.levels-button .grid-icon { margin-right: 8px; }
.goal .cup-icon { margin-right: 7px; }
.goal { margin-left: 7px; }
.controls > button + button { margin-left: 8px; }
.tool-button > span, .primary .action-icon { margin-right: 7px; }
.board-state > span:first-child { margin-right: 5px; }
.level-option > .level-index { margin-right: 7px; }
.level-list { grid-gap: 7px; }
@media (max-height: 620px) {
    .app-header, .workbench { margin-bottom: 8px; }
}
@media (max-width: 340px) {
    .controls > button + button { margin-left: 5px; }
    .tool-button > span { margin-right: 4px; }
}
