:root {
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --font: ui-rounded, -apple-system, "SF Pro Rounded", "SF Pro Text", system-ui, sans-serif;
  --serif: ui-serif, "New York", "Iowan Old Style", Georgia, serif;
}
#app[data-theme="jade"] {
  --bg0: #06191a; --bg1: #0e3b31; --bg2: #1b5a45;
  --accent: #e9c46a; --accent2: #f4a259; --ink: #f3ecd8; --muted: rgba(243,236,216,.62);
  --glass: rgba(10, 34, 30, .55); --line: rgba(233,196,106,.22);
}
#app[data-theme="night"] {
  --bg0: #05071a; --bg1: #121a46; --bg2: #26307a;
  --accent: #a9c1ff; --accent2: #ff9ec7; --ink: #eef1ff; --muted: rgba(238,241,255,.6);
  --glass: rgba(12, 16, 48, .55); --line: rgba(169,193,255,.22);
}
#app[data-theme="sakura"] {
  --bg0: #1a0610; --bg1: #4a1630; --bg2: #7a2a4c;
  --accent: #ffc4d6; --accent2: #ffd98a; --ink: #fff0f4; --muted: rgba(255,240,244,.62);
  --glass: rgba(48, 12, 30, .5); --line: rgba(255,196,214,.24);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #06191a;
  overscroll-behavior: none; touch-action: manipulation;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%;
}
body { position: fixed; inset: 0; font-family: var(--font); color: #f3ecd8; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; }

#app { position: fixed; inset: 0; color: var(--ink); background: var(--bg0); }
#bg {
  position: absolute; inset: 0; pointer-events: none;
  background:
    radial-gradient(120% 70% at 50% 0%, var(--bg2) 0%, transparent 60%),
    radial-gradient(90% 60% at 100% 100%, color-mix(in srgb, var(--bg2) 60%, transparent) 0%, transparent 70%),
    linear-gradient(180deg, var(--bg1), var(--bg0));
}
#bg::after {
  content: ""; position: absolute; inset: 0; opacity: .07;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='60' height='30' viewBox='0 0 60 30'%3E%3Cg fill='none' stroke='%23fff' stroke-width='1.2'%3E%3Cpath d='M0 30a30 30 0 0160 0M10 30a20 20 0 0140 0M20 30a10 10 0 0120 0'/%3E%3Cpath d='M-30 15a30 30 0 0160 0M-20 15a20 20 0 0140 0M-10 15a10 10 0 0120 0M30 15a30 30 0 0160 0M40 15a20 20 0 0140 0M50 15a10 10 0 0120 0'/%3E%3C/g%3E%3C/svg%3E");
  background-size: 60px 30px;
  mask-image: linear-gradient(180deg, #000 0%, transparent 55%, #000 100%);
  -webkit-mask-image: linear-gradient(180deg, #000 0%, transparent 55%, #000 100%);
}

.screen {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  opacity: 0; pointer-events: none; transform: scale(.985);
  transition: opacity .35s ease, transform .35s ease;
}
.screen.active { opacity: 1; pointer-events: auto; transform: none; }

/* ---------- Home ---------- */
#home { padding: calc(var(--safe-t) + 24px) 22px calc(var(--safe-b) + 20px); justify-content: space-between; }
.home-top { text-align: center; margin-top: 4vh; }
#logoTiles { width: 300px; height: 150px; max-width: 80vw; }
h1 {
  font-family: var(--serif); font-weight: 700; font-size: 46px; line-height: 1; margin: 8px 0 8px; letter-spacing: .5px;
  background: linear-gradient(180deg, #fff8e1, var(--accent) 70%, var(--accent2));
  -webkit-background-clip: text; background-clip: text; color: transparent;
  text-shadow: 0 2px 30px color-mix(in srgb, var(--accent) 25%, transparent);
}
h1 span { font-style: italic; }
.tagline { margin: 0; color: var(--muted); font-size: 15px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat {
  background: var(--glass); border: 1px solid var(--line); border-radius: 18px; padding: 12px 8px; text-align: center;
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
}
.stat b { display: block; font-size: 22px; color: var(--accent); font-variant-numeric: tabular-nums; }
.stat small { color: var(--muted); font-size: 12px; }
.home-actions { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 12px; }
.row > * { flex: 1; }

.btn {
  height: 56px; border-radius: 18px; font-weight: 700; font-size: 17px; letter-spacing: .2px;
  transition: transform .12s ease, filter .2s ease;
}
.btn:active { transform: scale(.96); }
.btn.primary {
  height: 74px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: #2a1a05; font-size: 22px;
  background: linear-gradient(180deg, #fff1c1 0%, var(--accent) 45%, var(--accent2) 100%);
  box-shadow: 0 10px 30px -8px color-mix(in srgb, var(--accent) 70%, transparent), inset 0 1px 0 rgba(255,255,255,.7), inset 0 -3px 0 rgba(0,0,0,.12);
}
.btn.primary small { font-size: 13px; font-weight: 600; opacity: .7; }
.btn.secondary { background: var(--glass); border: 1px solid var(--line); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); }
.btn.ghost { height: 44px; color: var(--muted); font-weight: 600; }

/* ---------- Levels ---------- */
.bar { display: flex; align-items: center; gap: 10px; padding: calc(var(--safe-t) + 8px) 14px 10px; }
.bar h2 { margin: 0; flex: 1; font-family: var(--serif); font-size: 26px; }
.bar-right { color: var(--accent); font-weight: 700; }
.level-grid {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; align-content: start;
  padding: 6px 16px calc(var(--safe-b) + 24px);
}
.lv {
  position: relative; aspect-ratio: 3 / 4; border-radius: 14px;
  background: linear-gradient(160deg, #fffdf5, #ece2c6);
  box-shadow: 4px 5px 0 var(--bg2), 4px 5px 12px rgba(0,0,0,.4);
  color: #1b2748; display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.lv b { font-family: var(--serif); font-size: 26px; }
.lv .st { font-size: 11px; letter-spacing: 1px; color: #c99a2e; height: 14px; }
.lv .st i { font-style: normal; opacity: .25; }
.lv .st i.on { opacity: 1; }
.lv .mods { position: absolute; top: 4px; right: 5px; font-size: 11px; }
.lv.cur { outline: 3px solid var(--accent); outline-offset: 2px; }
.lv.boss { background: linear-gradient(160deg, #fff3cf, #e9c46a); }
.lv.locked { background: rgba(255,255,255,.08); color: var(--muted); box-shadow: none; border: 1px dashed var(--line); }
.lv:active:not(.locked) { transform: scale(.94); }

/* ---------- Game ---------- */
.hud {
  display: flex; align-items: center; gap: 10px;
  padding: calc(var(--safe-t) + 4px) 12px 6px;
}
.icon-btn {
  width: 44px; height: 44px; border-radius: 14px; display: grid; place-items: center; flex: none;
  background: var(--glass); border: 1px solid var(--line);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
}
.icon-btn svg, .tool svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:active { transform: scale(.92); }
.hud-center { flex: 1; min-width: 0; }
.lvl { font-family: var(--serif); font-size: 21px; line-height: 1.1; }
.lvl b { color: var(--accent); }
.badges { display: flex; gap: 5px; margin-top: 3px; flex-wrap: wrap; }
.badge { font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 99px; background: rgba(255,255,255,.1); border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.badge.boss { color: #2a1a05; background: var(--accent); border-color: transparent; }
.badge.fog { color: #bfe3ff; }
.badge.time { color: #ffb3a0; }
.hud-score { text-align: right; }
.hud-score b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; color: var(--accent); line-height: 1; }
.hud-score b.bump { animation: bump .35s ease; }
@keyframes bump { 40% { transform: scale(1.25); } }
.hud-score small { color: var(--muted); font-size: 11px; }
.hud2 { display: flex; align-items: center; gap: 8px; padding: 0 12px 4px; }
.progress { flex: 1; height: 6px; border-radius: 6px; background: rgba(255,255,255,.1); overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; border-radius: 6px; background: linear-gradient(90deg, var(--accent2), var(--accent)); transition: width .35s ease; }
.chip { font-size: 12px; font-weight: 700; padding: 3px 9px; border-radius: 99px; background: rgba(255,255,255,.08); font-variant-numeric: tabular-nums; color: var(--muted); }
.chip.warn { color: #ffd2c4; background: rgba(255,90,60,.3); animation: pulse 1s infinite; }
.chip.zero { color: #ffd2c4; background: rgba(255,90,60,.25); }
@keyframes pulse { 50% { opacity: .55; } }
.board-wrap { flex: 1; position: relative; min-height: 0; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.toolbar {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
  padding: 6px 12px calc(var(--safe-b) + 6px);
}
.tool {
  position: relative; height: 58px; border-radius: 16px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  background: var(--glass); border: 1px solid var(--line);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  transition: transform .12s ease, opacity .2s;
}
.tool span { font-size: 11px; font-weight: 600; color: var(--muted); }
.tool em {
  position: absolute; top: -5px; right: -3px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px;
  font-style: normal; font-size: 12px; font-weight: 800; line-height: 20px; color: #2a1a05; background: var(--accent);
}
.tool:active { transform: scale(.92); }
.tool.off { opacity: .4; }
.tool.off em { background: rgba(255,255,255,.3); }

/* ---------- Modal ---------- */
.modal {
  position: absolute; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center;
  background: rgba(0,0,0,.45); opacity: 0; pointer-events: none; transition: opacity .25s;
}
.modal.show { opacity: 1; pointer-events: auto; }
.sheet {
  width: 100%; max-width: 520px; max-height: 88%; overflow-y: auto;
  margin: 0 8px calc(var(--safe-b) + 8px); padding: 22px 20px 18px; border-radius: 28px;
  background: color-mix(in srgb, var(--bg1) 88%, #000); border: 1px solid var(--line);
  box-shadow: 0 30px 80px rgba(0,0,0,.6);
  transform: translateY(40px); transition: transform .35s cubic-bezier(.2,.9,.3,1.2);
}
.modal.show .sheet { transform: none; }
.sheet h3 { margin: 0 0 6px; text-align: center; font-family: var(--serif); font-size: 30px; color: var(--accent); }
.sheet p { margin: 0 0 14px; text-align: center; color: var(--muted); line-height: 1.45; }
.sheet .btn { width: 100%; margin-top: 10px; }
.sheet .btn.primary { height: 58px; font-size: 19px; }
.stars { display: flex; justify-content: center; gap: 10px; margin: 6px 0 12px; }
.stars svg { width: 54px; height: 54px; fill: rgba(255,255,255,.12); transform: scale(.4); opacity: .3; transition: transform .45s cubic-bezier(.2,1.6,.4,1), opacity .3s, fill .3s; }
.stars svg.mid { width: 68px; height: 68px; margin-top: -10px; }
.stars svg.on { fill: var(--accent); transform: none; opacity: 1; filter: drop-shadow(0 0 12px color-mix(in srgb, var(--accent) 70%, transparent)); }
.stars svg.shown { transform: none; opacity: 1; }
.result { display: grid; grid-template-columns: 1fr auto; gap: 6px 10px; margin: 6px 4px 8px; font-size: 15px; }
.result span { color: var(--muted); }
.result b { text-align: right; font-variant-numeric: tabular-nums; }
.result .total { font-size: 20px; color: var(--accent); border-top: 1px solid var(--line); padding-top: 8px; }
.setting { display: flex; align-items: center; justify-content: space-between; padding: 12px 2px; border-bottom: 1px solid var(--line); font-size: 16px; }
.setting small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.switch { width: 52px; height: 32px; border-radius: 16px; background: rgba(255,255,255,.18); position: relative; transition: background .2s; flex: none; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 2px 6px rgba(0,0,0,.3); }
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(20px); }
.themes { display: flex; gap: 8px; }
.theme { width: 40px; height: 40px; border-radius: 12px; border: 2px solid transparent; }
.theme.on { border-color: #fff; }
.theme[data-t="jade"] { background: linear-gradient(135deg, #1b5a45, #06191a); }
.theme[data-t="night"] { background: linear-gradient(135deg, #26307a, #05071a); }
.theme[data-t="sakura"] { background: linear-gradient(135deg, #7a2a4c, #1a0610); }
.rules { color: var(--muted); line-height: 1.5; font-size: 15px; padding-left: 18px; margin: 0 0 6px; }
.rules li { margin-bottom: 6px; }
.rules b { color: var(--ink); }
.danger { color: #ff9c8a !important; }

#toast {
  position: absolute; left: 50%; top: calc(var(--safe-t) + 92px); z-index: 30; max-width: 88%;
  transform: translate(-50%, -12px); opacity: 0; pointer-events: none;
  padding: 10px 16px; border-radius: 16px; font-size: 14px; font-weight: 600; text-align: center;
  background: rgba(0,0,0,.72); border: 1px solid var(--line);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  transition: opacity .25s, transform .25s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 700px) {
  .toolbar { max-width: 520px; margin: 0 auto; width: 100%; }
  #home { max-width: 520px; margin: 0 auto; }
}
