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

:root {
    --glass: rgba(15, 12, 8, 0.7);
    --glass-border: rgba(255, 235, 200, 0.25);
    --accent: #c9a24b;
    --accent-hover: #e0bb63;
    --text: #f3ead8;
}

html, body { height: 100%; margin: 0; }
body {
    font-family: Georgia, 'Times New Roman', serif;
    color: var(--text);
    background: #1b1610;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
}

#backdrop {
    position: fixed;
    inset: -40px;
    z-index: -1;
    background: #1b1610 url('../main.webp') center / cover no-repeat;
    filter: blur(18px) brightness(0.45);
}

header { flex: 0 0 auto; text-align: center; padding: 6px 12px 4px; text-shadow: 0 2px 6px rgba(0, 0, 0, 0.8); position: relative; z-index: 20; }
h1, h2 { font-family: 'IM Fell English SC', Georgia, serif; font-weight: normal; margin: 0; }
h1 { font-size: clamp(1.6em, 5vw, 2.6em); line-height: 1.1; }

#hud { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 6px 12px; margin-top: 4px; font-size: 0.95em; }
.hud-item strong { color: var(--accent-hover); font-variant-numeric: tabular-nums; }
button { font: inherit; cursor: pointer; }
.hud-button {
    background: rgba(0, 0, 0, 0.5); color: var(--text);
    border: 1px solid var(--glass-border); border-radius: 6px; padding: 4px 12px; font-size: 0.9em;
}
.hud-button:hover { background: rgba(255, 255, 255, 0.15); }
.hud-button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent-hover); }

/* Home */
.panel {
    background: var(--glass);
    border: 1px solid var(--glass-border);
    border-radius: 15px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    padding: 20px;
    width: min(760px, calc(100% - 32px));
    margin: 12px auto 16px;
    text-align: center;
    overflow-y: auto;
    max-height: calc(100% - 28px);
}
.intro { margin: 0 0 14px; }
.big-button {
    background: #8a6a24; color: #fff; border: none; border-radius: 8px;
    padding: 12px 26px; font-size: 1.1em; margin-bottom: 18px;
}
.big-button:hover { background: #a8822f; }
.panel h2 { color: var(--accent-hover); font-size: 1.5em; margin-bottom: 10px; }
#level-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
    gap: 10px;
}
.level-tile {
    position: relative;
    padding: 0;
    border: 2px solid transparent;
    border-radius: 8px;
    overflow: hidden;
    background: #000;
    aspect-ratio: 1;
    color: #fff;
}
.level-tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.level-tile .label {
    position: absolute; left: 0; right: 0; bottom: 0;
    background: linear-gradient(transparent, rgba(0, 0, 0, 0.85));
    padding: 14px 4px 4px; font-size: 0.8em; line-height: 1.25;
}
.level-tile .label strong { font-size: 1.15em; }
.level-tile.done { border-color: #67c25d; }
.level-tile.current { border-color: var(--accent); }
.level-tile.locked { cursor: not-allowed; }
.level-tile.locked img { filter: grayscale(1) brightness(0.35) blur(2px); }
.level-tile.locked::after { content: '🔒'; position: absolute; top: 34%; left: 0; right: 0; font-size: 1.6em; }
.level-tile:not(.locked):hover { border-color: var(--accent-hover); }
.links { display: flex; justify-content: center; gap: 22px; margin-top: 18px; }
.links a { color: var(--accent-hover); }

/* Play area: board + tray (tray at the side on wide screens, below on narrow ones) */
#play { flex: 1 1 auto; min-height: 0; display: flex; gap: 10px; padding: 4px 10px 10px; }
#board-wrap {
    flex: 1 1 auto; min-width: 0; min-height: 0;
    container-type: size;
    display: flex; align-items: center; justify-content: center;
}
#board {
    --ar: 1;
    position: relative;
    width: min(100cqw, calc(100cqh * var(--ar)));
    aspect-ratio: var(--ar);
    background: rgba(0, 0, 0, 0.55);
    border-radius: 6px;
    box-shadow: 0 0 0 2px rgba(255, 235, 200, 0.18), 0 10px 40px rgba(0, 0, 0, 0.7);
}
#ghost, #peek { position: absolute; inset: 0; width: 100%; height: 100%; border-radius: 6px; pointer-events: none; }
#ghost { opacity: 0.22; transition: opacity 0.3s; }
#board.no-ghost #ghost { opacity: 0; }
#peek { z-index: 50; opacity: 0.97; }
#board.done #peek { display: block !important; animation: fadeIn 0.8s forwards; opacity: 0; }
@keyframes fadeIn { to { opacity: 1; } }
#loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 1.2em; }

/* Pieces panel: rotation hint on top, scrolling tray below */
#tray-panel { flex: 0 0 260px; min-height: 0; display: flex; flex-direction: column; gap: 6px; }
#tray {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex; flex-wrap: wrap; align-content: flex-start; justify-content: center;
    gap: 4px;
    padding: 8px;
    background: linear-gradient(rgba(222, 190, 132, 0.5), rgba(196, 160, 100, 0.5));
    border: 1px solid var(--accent);
    border-radius: 10px;
    box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.25);
    touch-action: pan-y;
}
/* Wide screens: the pieces panel runs the full height on the right, with the
   rotate hint level with the title; header + board share the left column. */
@media (min-aspect-ratio: 1/1) {
    body.playing { display: grid; grid-template-columns: minmax(0, 1fr) 260px; grid-template-rows: auto minmax(0, 1fr); column-gap: 10px; padding-right: 10px; }
    body.playing header { grid-column: 1; grid-row: 1; }
    body.playing #play { display: contents; }
    body.playing #board-wrap { grid-column: 1; grid-row: 2; padding: 4px 0 10px 10px; }
    body.playing #tray-panel { grid-column: 2; grid-row: 1 / 3; padding: 8px 0 10px; }
}
@media (max-aspect-ratio: 1/1) {
    #play { flex-direction: column; }
    #tray-panel { flex: 0 0 auto; }
    #tray {
        flex: 0 0 auto; height: clamp(96px, 22dvh, 170px);
        flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden;
        align-items: center; justify-content: flex-start;
        touch-action: pan-x;
    }
}

.piece { display: block; flex: 0 0 auto; cursor: grab; filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.6)); }
#tray .piece { touch-action: inherit; }
/* Placed pieces are locked: they never take clicks (their transparent tab
   margins stick out past the board and used to cover the buttons above). */
#board .piece { position: absolute; cursor: default; filter: none; z-index: 1; pointer-events: none; }
#board .piece.snap { animation: snap 0.35s ease-out; }
@keyframes snap { 50% { filter: brightness(1.5) drop-shadow(0 0 8px gold); } }
#drag-layer { position: fixed; inset: 0; pointer-events: none; z-index: 100; }
#drag-layer .piece { position: fixed; cursor: grabbing; filter: drop-shadow(0 8px 12px rgba(0, 0, 0, 0.7)); }
.piece.shake { animation: shake 0.35s; }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
#tray:empty::after { content: 'All pieces placed!'; opacity: 0.7; margin: auto; }
#tray.edges-only .piece:not(.edge) { display: none; }

/* Rotation */
#tray .piece { transition: transform 0.18s ease-out; }
#drag-layer .piece { transition: transform 0.18s ease-out; }
#board .piece { transition: none; }
.rotate-hint {
    flex: 0 0 auto; text-align: center; font-size: 0.9em; padding: 5px 10px; border-radius: 10px;
    background: rgba(201, 162, 75, 0.3); border: 1px solid var(--accent); cursor: help;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}
.piece.shake { animation: none; }
#tray .piece.shake, #drag-layer .piece.shake { animation: shakeRot 0.35s; }
@keyframes shakeRot { 25% { translate: -5px 0; } 75% { translate: 5px 0; } }

/* Toast over the board */
#toast {
    position: absolute; left: 50%; top: 12px; transform: translateX(-50%);
    max-width: 92%; z-index: 60; pointer-events: none;
    background: rgba(0, 0, 0, 0.82); border: 1px solid var(--accent); border-radius: 10px;
    padding: 8px 14px; text-align: center; font-size: 0.95em;
}
#toast.show { animation: toastIn 0.3s ease-out; }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, -8px); } }

/* Balloons */
#balloons { position: fixed; inset: 0; z-index: 300; pointer-events: none; overflow: hidden; }
.balloon {
    position: absolute; bottom: -160px;
    width: var(--size); height: calc(var(--size) * 1.22);
    border-radius: 50% 50% 48% 48% / 55% 55% 45% 45%;
    background: radial-gradient(circle at 32% 28%, rgba(255, 255, 255, 0.75) 0 7%, transparent 22%), var(--color);
    box-shadow: inset -6px -10px 18px rgba(0, 0, 0, 0.25);
    pointer-events: auto; cursor: pointer;
    animation-name: floatUp, sway;
    animation-timing-function: ease-in, ease-in-out;
    animation-iteration-count: 1, infinite;
    animation-direction: normal, alternate;
    animation-fill-mode: forwards, none;
}
.balloon::before {   /* knot */
    content: ''; position: absolute; left: 50%; bottom: -5px; transform: translateX(-50%);
    border: 6px solid transparent; border-bottom-color: var(--color); border-top: 0;
}
.balloon::after {    /* string */
    content: ''; position: absolute; left: 50%; top: 100%; width: 1px; height: calc(var(--size) * 1.3);
    background: rgba(255, 255, 255, 0.55); transform-origin: top; transform: rotate(4deg);
}
@keyframes floatUp { to { bottom: 115%; } }
@keyframes sway { from { translate: calc(var(--sway) * -1) 0; } to { translate: var(--sway) 0; } }
.balloon.pop { animation: pop-balloon 0.25s forwards !important; }
@keyframes pop-balloon { to { transform: scale(1.6); opacity: 0; } }
.news { color: var(--accent-hover); border-top: 1px solid var(--glass-border); padding-top: 10px; }

/* Modal */
.modal { position: fixed; inset: 0; z-index: 200; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(0, 0, 0, 0.45); }
.modal-card {
    background: #241d14; border: 1px solid var(--glass-border); border-radius: 12px;
    padding: 20px 24px; width: min(460px, 100%); text-align: center; box-shadow: 0 0 40px rgba(201, 162, 75, 0.35);
    animation: pop 0.4s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes pop { from { transform: scale(0.6); opacity: 0; } }
.modal-card h2 { color: var(--accent-hover); font-size: 2em; }
.small { font-size: 0.9em; opacity: 0.85; }
.menu-list { display: flex; flex-direction: column; gap: 8px; margin: 14px auto 6px; max-width: 280px; }
.menu-list .action-button { display: block; text-align: left; text-decoration: none; font-size: 1em; padding: 11px 16px; }
.saved-note { color: #9fe39a; margin-bottom: 0; }
.menu-button { font-weight: bold; }
.button-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-top: 14px; }
.action-button { padding: 10px 18px; border: none; border-radius: 6px; background: #4a4a4a; color: #fff; }
.action-button:hover { background: #666; }
.action-button.primary { background: #8a6a24; }
.action-button.primary:hover { background: #a8822f; }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 0.01ms !important; }
}
