* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
:root {
    --frame-bottom: 0px; --frame-left: 0px; --frame-right: 0px;
    --accent: #2f7d4f; --accent-dark: #1d5634; --gold: #f59e0b; --ink: #23384a; --shade: 10, 50, 30;
}
html, body { height: 100%; }
/* Inside the site iframe a fixed footer bar (~40px) covers the bottom; the site hides it when height <= 400px. */
html.in-frame { --frame-bottom: 44px; }
@media (max-height: 400px) { html.in-frame { --frame-bottom: 0px; } }

body { font-family: Arial, Helvetica, sans-serif; background: #24543a; color: #fff; overflow: hidden; user-select: none; -webkit-user-select: none; }
#bg { position: fixed; inset: 0; z-index: 0; }
#bg svg { width: 100%; height: 100%; display: block; }
#app {
    position: relative; z-index: 1; display: flex; justify-content: center;
    height: calc(100vh - var(--frame-bottom)); height: calc(100dvh - var(--frame-bottom));
    padding: 0 var(--frame-right) 0 var(--frame-left);
}
#stage { position: relative; width: 100%; max-width: 480px; height: 100%; overflow: hidden; }
.screen { display: none; position: absolute; inset: 0; flex-direction: column; }
.screen.active { display: flex; }

/* ---------- Buttons ---------- */
.btn { display: block; width: 270px; max-width: 100%; border: none; padding: 14px 20px; margin: 6px auto; font-size: 18px; font-weight: bold; border-radius: 50px; cursor: pointer; font-family: inherit; box-shadow: 0 4px 14px rgba(0, 0, 0, .25); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: #fff; color: var(--accent); }
.btn-secondary { background: rgba(var(--shade), .55); color: #fff; border: 2px solid rgba(255, 255, 255, .8); display: flex; align-items: center; justify-content: center; gap: 8px; }
.btn small { display: block; font-size: 12px; font-weight: normal; opacity: .8; margin-top: 1px; }
.btn .tag { font-size: 12px; background: var(--gold); color: #fff; border-radius: 10px; padding: 2px 7px; }
.btn:disabled { opacity: .6; cursor: default; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, .75); background: rgba(var(--shade), .45); color: #fff; font-size: 18px; font-weight: bold; cursor: pointer; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-family: inherit; }
.icon-spacer { width: 40px; flex-shrink: 0; }

/* ---------- Start ---------- */
#start-screen { justify-content: center; align-items: center; padding: 20px; text-align: center; background: linear-gradient(180deg, rgba(8, 40, 24, .15), rgba(8, 40, 24, .55)); }
#start-screen .corner-left { position: absolute; top: 15px; left: 15px; display: flex; gap: 8px; }
.logo svg { width: 120px; height: 120px; filter: drop-shadow(0 8px 14px rgba(0, 0, 0, .3)); }
#start-screen h1 { font-size: 42px; letter-spacing: 2px; margin: 8px 0 6px; text-shadow: 0 3px 8px rgba(0, 0, 0, .45); }
#start-screen .desc { font-size: 16px; line-height: 1.45; max-width: 310px; margin-bottom: 16px; text-shadow: 0 1px 4px rgba(0, 0, 0, .5); }
.start-stats { font-size: 13px; margin-top: 8px; text-shadow: 0 1px 3px rgba(0, 0, 0, .6); min-height: 1.2em; }

/* ---------- Top bar ---------- */
.topbar { height: 54px; flex-shrink: 0; display: flex; align-items: center; gap: 8px; padding: 0 10px; background: linear-gradient(rgba(6, 34, 20, .72), rgba(6, 34, 20, .35)); }
.topbar .title { flex: 1; min-width: 0; text-align: center; font-weight: bold; font-size: 19px; text-shadow: 0 1px 3px rgba(0, 0, 0, .5); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .title small { display: block; font-size: 12px; font-weight: normal; opacity: .9; overflow: hidden; text-overflow: ellipsis; }
.coin-chip { display: flex; align-items: center; gap: 5px; background: rgba(0, 0, 0, .35); border: 2px solid #f3c452; border-radius: 18px; padding: 3px 10px 3px 3px; font-weight: bold; color: #ffe08a; min-width: 66px; }
.coin { width: 24px; height: 24px; border-radius: 50%; background: linear-gradient(#ffe27a, #f0b429); color: #7a4a00; display: inline-flex; align-items: center; justify-content: center; font-size: 14px; font-weight: 900; flex-shrink: 0; }
.coin-chip.pulse, .pill.pulse { animation: pop .4s; }

/* ---------- Info row ---------- */
.inforow { flex-shrink: 0; height: 62px; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 12px; font-weight: bold; font-size: 15px; text-shadow: 0 1px 3px rgba(0, 0, 0, .6); }
.pill { background: rgba(0, 0, 0, .3); border-radius: 14px; padding: 5px 11px; white-space: nowrap; }
#g-time { min-width: 74px; text-align: center; }
#g-time.big { font-size: 22px; padding: 6px 14px; }
#g-time.warn { background: rgba(200, 40, 40, .75); animation: pop .5s; }
.pill.score { font-size: 18px; }
.round-btn { position: relative; width: 50px; height: 50px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, .85); background: rgba(var(--shade), .65); color: #fff; cursor: pointer; font-family: inherit; display: flex; flex-direction: column; align-items: center; justify-content: center; flex-shrink: 0; box-shadow: 0 3px 10px rgba(0, 0, 0, .25); }
.round-btn .ico { font-size: 22px; line-height: 1; }
.round-btn .price { font-size: 11px; font-weight: bold; color: #ffe08a; margin-top: 1px; }
.round-btn.poor { opacity: .55; }
.round-btn.pulse { animation: pop .45s; }

/* ---------- Board ---------- */
.board-wrap { flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 0 12px 12px; position: relative; }
.preview { height: 36px; line-height: 36px; min-width: 44px; padding: 0 16px; margin-bottom: 8px; border-radius: 18px; background: #fff; color: var(--accent-dark); font-size: 21px; font-weight: 900; letter-spacing: 3px; text-align: center; box-shadow: 0 3px 10px rgba(0, 0, 0, .25); visibility: hidden; flex-shrink: 0; }
.preview.show { visibility: visible; }
.preview.good { background: #d8f5e6; color: #0b6b44; }
.preview.bad { background: #fde2e2; color: #c0392b; animation: shake .35s; }
/* little room: the preview floats over the top of the board instead of taking its own row */
.board-wrap.tight .preview { position: absolute; top: 2px; left: 50%; transform: translateX(-50%); margin: 0; z-index: 3; height: 32px; line-height: 32px; font-size: 19px; }
.board-wrap.tight .preview.bad { animation: none; }
.board { background: rgba(255, 255, 255, .95); border-radius: 16px; padding: 8px; box-shadow: 0 10px 28px rgba(0, 0, 0, .3); touch-action: none; flex-shrink: 0; cursor: pointer; }
.board canvas { display: block; touch-action: none; }
.board.shake { animation: shake .3s; }
.chips { width: 100%; margin-top: 12px; display: flex; flex-wrap: wrap; align-content: flex-start; gap: 7px; justify-content: center; flex-shrink: 0; }
.chip { background: rgba(255, 255, 255, .95); color: var(--ink); border-radius: 14px; padding: 6px 12px; font-weight: 900; font-size: 15px; letter-spacing: 1px; box-shadow: 0 2px 6px rgba(0, 0, 0, .2); text-transform: uppercase; transition: background .2s, color .2s; }
.chip.found { color: rgba(35, 56, 74, .45); text-decoration: line-through; text-decoration-thickness: 2px; box-shadow: none; background: rgba(255, 255, 255, .7); }
.chip.hint { background: #fff1c7; color: #8a5a00; }
.chip.pop { animation: pop .4s; }
.chips.small { gap: 5px; }
.chips.small .chip { font-size: 12px; padding: 5px 8px; letter-spacing: .5px; border-radius: 12px; }
.msg { position: absolute; left: 0; right: 0; top: 40%; text-align: center; pointer-events: none; opacity: 0; transition: opacity .2s; z-index: 4; }
.msg span { display: inline-block; background: rgba(0, 0, 0, .75); padding: 8px 14px; border-radius: 16px; font-weight: bold; font-size: 15px; }
.msg.show { opacity: 1; }

/* ---------- Select ---------- */
#select-screen { background: rgba(6, 34, 20, .5); }
.scroll { flex: 1; min-height: 0; overflow-y: auto; padding: 10px 12px 20px; -webkit-overflow-scrolling: touch; }
.chapter-list { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.chapter-card { position: relative; border-radius: 14px; overflow: hidden; border: 2px solid rgba(255, 255, 255, .6); background: #234; cursor: pointer; padding: 0; color: #fff; font-family: inherit; text-align: left; box-shadow: 0 4px 12px rgba(0, 0, 0, .3); aspect-ratio: 4 / 3; }
.chapter-card .art { position: absolute; inset: 0; }
.chapter-card .art svg { width: 100%; height: 100%; display: block; }
.chapter-card .info { position: absolute; left: 0; right: 0; bottom: 0; padding: 6px 8px; background: linear-gradient(transparent, rgba(0, 0, 0, .65)); }
.chapter-card .nm { font-size: 15px; font-weight: bold; text-shadow: 0 1px 3px rgba(0, 0, 0, .7); }
.chapter-card .pr { font-size: 12px; color: #ffe08a; font-weight: bold; }
.chapter-card .num { position: absolute; top: 6px; left: 8px; font-size: 12px; font-weight: bold; background: rgba(0, 0, 0, .4); border-radius: 8px; padding: 1px 7px; }
.chapter-card.locked { cursor: default; }
.chapter-card.locked .art { filter: grayscale(.85) brightness(.6); }
.chapter-card.locked .info::after { content: ' 🔒'; }
.chapter-card.head { display: block; width: 100%; aspect-ratio: auto; height: 120px; margin-bottom: 12px; cursor: default; }
.level-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 9px; }
.level-btn { aspect-ratio: 1; border-radius: 12px; border: 2px solid rgba(255, 255, 255, .7); background: rgba(var(--shade), .6); color: #fff; font-family: inherit; font-size: 18px; font-weight: bold; cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.level-btn .st { font-size: 11px; color: #7dffb0; min-height: 13px; }
.level-btn .st.star { color: #ffd166; font-size: 13px; }
.level-btn.next { background: #fff; color: var(--accent); }
.level-btn.locked { opacity: .4; cursor: default; }

/* ---------- Modals ---------- */
.modal-overlay { position: absolute; inset: 0; background: rgba(0, 0, 0, .55); display: none; align-items: center; justify-content: center; z-index: 400; padding: 14px; }
.modal-overlay.show { display: flex; }
.modal { background: #fff; color: #374151; border-radius: 18px; padding: 20px 18px 0; width: 100%; max-width: 360px; max-height: 100%; overflow-y: auto; text-align: center; box-shadow: 0 15px 35px rgba(0, 0, 0, .4); animation: modalIn .25s ease; }
.modal h2 { font-size: 25px; color: var(--accent-dark); margin: 4px 0 2px; }
.modal .sub { font-size: 14px; color: #6b7280; margin-bottom: 8px; }
.modal .btn-primary { background: var(--accent); color: #fff; width: 100%; max-width: 260px; }
.modal .btn-secondary { background: #fff; color: var(--accent); border-color: var(--accent); width: 100%; max-width: 260px; }
.modal-buttons { position: sticky; bottom: 0; background: #fff; padding: 8px 0 16px; margin-top: 8px; }
.modal.help { text-align: left; }
.modal.help h2 { text-align: center; margin-bottom: 6px; }
.modal.help p { font-size: 14px; line-height: 1.45; margin: 7px 0; }
.burst { font-size: 54px; color: var(--gold); line-height: 1; animation: pop .6s; text-shadow: 0 4px 12px rgba(245, 158, 11, .5); }
.burst.clock { font-size: 46px; }
.stats { display: flex; justify-content: center; gap: 10px; margin: 10px 0; flex-wrap: wrap; }
.stat { background: #eef7f1; border-radius: 12px; padding: 8px 12px; min-width: 88px; }
.stat b { display: block; font-size: 20px; color: var(--accent-dark); }
.stat span { font-size: 12px; color: #6b7280; }
.mchips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; margin: 8px 0 6px; }
.mchips span { border-radius: 12px; padding: 4px 10px; font-weight: bold; font-size: 13px; letter-spacing: 1px; color: #1f2937; text-transform: uppercase; }
.reward { font-size: 15px; font-weight: bold; color: #b7791f; margin: 6px 0; display: flex; align-items: center; justify-content: center; gap: 6px; flex-wrap: wrap; }
.reward:empty { display: none; }
.complete-text { font-size: 15px; font-weight: bold; color: #b7791f; margin: 4px 0; }
.small-text { font-size: 13px; line-height: 1.4; margin: 4px 0; color: #6b7280; }
.small-text:empty { display: none; }

/* ---------- Animations ---------- */
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.22); } 100% { transform: scale(1); } }
@keyframes modalIn { from { transform: scale(.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.aurora { animation: aur 6s ease-in-out infinite alternate; }
@keyframes aur { from { opacity: .6; transform: translateX(-8px); } to { opacity: 1; transform: translateX(8px); } }

@media (max-height: 640px) {
    .logo svg { width: 84px; height: 84px; }
    #start-screen h1 { font-size: 32px; margin: 4px 0; }
    #start-screen .desc { font-size: 14px; margin-bottom: 8px; }
    .btn { padding: 11px 18px; font-size: 16px; margin: 4px auto; }
    .inforow { height: 54px; padding: 4px 12px; }
    .round-btn { width: 44px; height: 44px; }
    .round-btn .ico { font-size: 19px; }
}
