:root {
  --berry: #d94f70;
  --berry-dark: #a8314f;
  --choco: #5a3a2e;
  --choco-soft: #7a5546;
  --cream: #fff6ea;
  --cream-2: #fde9d4;
  --gold: #f6b93b;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: ui-rounded, "Hiragino Sans", "Hiragino Kaku Gothic ProN", system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--cream); color: var(--choco); }
body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; overscroll-behavior: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; }
button:disabled { opacity: .4; cursor: default; }
#app { position: fixed; inset: 0; max-width: 520px; margin: 0 auto; overflow: hidden; }

.screen { position: absolute; inset: 0; display: none; flex-direction: column; }
.screen.active { display: flex; animation: fadeIn .25s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } }

.pill {
  background: rgba(90, 58, 46, .12); border-radius: 999px; padding: 6px 12px;
  font-weight: 700; font-size: 14px; white-space: nowrap; font-variant-numeric: tabular-nums;
}
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  background: #fff; box-shadow: 0 2px 0 rgba(90, 58, 46, .2); font-size: 20px; font-weight: 700;
}

/* ---------- ホーム ---------- */
#screen-home {
  background:
    radial-gradient(circle at 20% 15%, #ffe3ec 0, transparent 40%),
    radial-gradient(circle at 85% 30%, #fff0c9 0, transparent 40%),
    repeating-linear-gradient(90deg, #fff6ea 0 28px, #fdeee0 28px 56px);
}
.home-deco { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.home-deco span { position: absolute; bottom: -60px; opacity: .55; animation: floatUp linear infinite; }
@keyframes floatUp {
  from { transform: translateY(0) rotate(0); }
  to { transform: translateY(-120vh) rotate(360deg); }
}
.home-top { position: relative; display: flex; gap: 8px; align-items: center; padding: calc(var(--safe-top) + 12px) 16px 0; }
.home-top .icon-btn { margin-left: auto; }
.home-top .pill { background: rgba(255, 255, 255, .8); }
.logo { position: relative; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.logo-sub { font-weight: 800; letter-spacing: .3em; color: var(--berry); font-size: 14px; }
.logo-main {
  font-size: 56px; line-height: 1.05; font-weight: 900; color: #fff;
  -webkit-text-stroke: 3px var(--choco); paint-order: stroke fill;
  text-shadow: 0 5px 0 var(--berry);
  margin: 6px 0 18px;
}
.logo-showcase { display: flex; gap: 6px; font-size: 44px; }
.logo-showcase span { display: inline-block; animation: bob 2s ease-in-out infinite; filter: drop-shadow(0 4px 2px rgba(0, 0, 0, .15)); }
@keyframes bob { 50% { transform: translateY(-8px); } }

.home-actions { position: relative; display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 0 24px 24px; }
.stage-tag {
  background: var(--berry-dark); color: #fff; font-weight: 800; padding: 4px 18px; border-radius: 10px 10px 0 0;
  margin-bottom: -14px; z-index: 1; letter-spacing: .05em;
}
.btn-start {
  width: 100%; max-width: 340px; padding: 18px 0; border-radius: 28px;
  background: linear-gradient(#ef6f8f, var(--berry)); color: #fff;
  font-size: 44px; font-weight: 900; letter-spacing: .05em;
  border: 4px solid #ffd27a; box-shadow: 0 6px 0 var(--berry-dark), 0 10px 20px rgba(168, 49, 79, .35);
  text-shadow: 0 3px 0 var(--berry-dark);
}
.btn-start:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--berry-dark); }
.btn-daily {
  width: 100%; max-width: 300px; padding: 10px 0; border-radius: 18px; background: #fff;
  border: 3px solid #f0d7b8; box-shadow: 0 4px 0 #e6c9a5; display: flex; flex-direction: column; gap: 4px; align-items: center;
}
.btn-daily .daily-title { font-weight: 800; font-size: 17px; }
.btn-daily .daily-sub { background: var(--choco); color: #fff; font-size: 13px; padding: 3px 14px; border-radius: 999px; font-variant-numeric: tabular-nums; }
.btn-daily.done .daily-sub { background: #5aa469; }
.btn-daily:active { transform: translateY(3px); box-shadow: 0 1px 0 #e6c9a5; }

.tabbar {
  position: relative; display: flex; background: var(--berry); padding: 6px 8px calc(var(--safe-bottom) + 6px);
  border-top: 3px solid #ffd27a;
}
.tabbar button { flex: 1; display: flex; flex-direction: column; align-items: center; color: #fff; font-weight: 800; font-size: 12px; padding: 4px 0; border-radius: 14px; }
.tabbar button span { font-size: 26px; }
.tabbar button.active { background: rgba(255, 255, 255, .18); }

/* ---------- ゲーム ---------- */
#screen-game { background: linear-gradient(#fdeee0, var(--cream)); }
.game-top {
  display: flex; align-items: center; gap: 8px; padding: calc(var(--safe-top) + 8px) 12px 10px;
  background: var(--berry); color: #fff; border-bottom: 3px solid #ffd27a;
}
.game-top .pill { background: rgba(0, 0, 0, .22); color: #fff; }
.game-level { display: flex; flex-direction: column; align-items: center; line-height: 1; min-width: 56px; }
.game-level small { font-size: 11px; font-weight: 700; }
.game-level b { font-size: 24px; color: #ffe08a; font-variant-numeric: tabular-nums; }
.game-count { font-size: 16px; }
.game-count b { color: #ffe08a; }
.game-top .close { margin-left: auto; background: #fff; color: var(--berry); }

.rules { display: flex; gap: 6px; margin: 10px 10px 0; padding: 8px; background: #fff; border-radius: 14px; border: 2px solid #f0d7b8; }
.rule { flex: 1; display: flex; align-items: center; gap: 5px; font-size: 10.5px; font-weight: 700; line-height: 1.25; }
.rule-ico { display: grid; grid-template-columns: repeat(3, 11px); gap: 1px; flex: none; }
.rule-ico i { width: 11px; height: 11px; font-style: normal; font-size: 8px; line-height: 11px; text-align: center; background: #f3e2cc; border-radius: 2px; color: var(--choco-soft); font-weight: 900; }
.rule-ico i.alt { background: #cfe9b9; }

.board-wrap { flex: 1; display: flex; align-items: center; justify-content: center; padding: 10px; min-height: 0; }
#board {
  --size: min(calc(100vw - 20px), 500px, calc(100vh - var(--safe-top) - var(--safe-bottom) - 300px));
  width: var(--size); height: var(--size);
  display: grid; background: var(--choco); padding: 3px; border-radius: 12px;
  box-shadow: 0 6px 0 rgba(90, 58, 46, .25); touch-action: none;
}
.cell {
  position: relative; display: grid; place-items: center; border-style: solid; border-color: rgba(90, 58, 46, .25);
  font-size: calc(var(--size) / var(--n) * .62); line-height: 1; overflow: visible; min-width: 0; min-height: 0;
}
.cell.bt { border-top-color: var(--choco); }
.cell.bb { border-bottom-color: var(--choco); }
.cell.bl { border-left-color: var(--choco); }
.cell.br { border-right-color: var(--choco); }
.cell .x { color: rgba(90, 58, 46, .6); font-weight: 900; font-size: .7em; }
.cell .dot { width: 18%; height: 18%; border-radius: 50%; background: rgba(90, 58, 46, .3); }
.cell .sw { display: block; filter: drop-shadow(0 2px 1px rgba(0, 0, 0, .25)); }
.cell.bad::after { content: ''; position: absolute; inset: 2px; border-radius: 4px; background: rgba(230, 30, 60, .35); box-shadow: inset 0 0 0 2px #e0284a; pointer-events: none; }
.cell.hinted::before { content: ''; position: absolute; inset: 0; box-shadow: inset 0 0 0 3px #fff, 0 0 12px 4px #ffe066; z-index: 2; animation: pulse 1s ease-in-out infinite; pointer-events: none; }
@keyframes pulse { 50% { opacity: .4; } }
.cell.pop .sw { animation: pop .25s ease-out; }
@keyframes pop { from { transform: scale(.3); } 70% { transform: scale(1.2); } }
.cell.shake { animation: shake .35s; }
@keyframes shake { 20%, 60% { transform: translateX(-3px); } 40%, 80% { transform: translateX(3px); } }
.cell.win .sw { animation: winJump .6s ease both; }
@keyframes winJump { 40% { transform: translateY(-30%) scale(1.3); } }

.howto { margin: 0; text-align: center; font-size: 11px; color: var(--choco-soft); }
.game-tools { display: flex; justify-content: center; gap: 14px; padding: 10px 12px calc(var(--safe-bottom) + 12px); }
.tool {
  position: relative; width: 84px; padding: 8px 0 6px; border-radius: 16px; background: #fff;
  border: 2px solid #f0d7b8; box-shadow: 0 4px 0 #e6c9a5; font-size: 12px; font-weight: 800;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.tool:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 1px 0 #e6c9a5; }
.tool-ico { font-size: 26px; }
.tool .badge { position: absolute; top: -6px; right: -6px; min-width: 22px; height: 22px; border-radius: 11px; background: var(--berry); color: #fff; font-size: 12px; line-height: 22px; padding: 0 5px; }
.tool.empty .badge { background: #aaa; }

/* ---------- サブ画面共通 ---------- */
.sub-top { display: flex; align-items: center; gap: 10px; padding: calc(var(--safe-top) + 10px) 14px 10px; background: var(--berry); color: #fff; border-bottom: 3px solid #ffd27a; }
.sub-top h2 { flex: 1; margin: 0; font-size: 20px; text-align: center; }
.sub-top .icon-btn { color: var(--berry); font-size: 28px; }
.sub-top .pill { background: rgba(0, 0, 0, .22); color: #fff; min-width: 40px; }
#screen-collection, #screen-stages, #screen-settings { background: var(--cream); }

.collection-grid { flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; padding: 14px 14px calc(var(--safe-bottom) + 20px); align-content: start; }
.card { background: #fff; border-radius: 16px; padding: 10px 4px; text-align: center; border: 3px solid #f0d7b8; box-shadow: 0 3px 0 #e6c9a5; }
.card-e { font-size: 40px; }
.card-name { font-size: 12px; font-weight: 800; margin-top: 4px; min-height: 2.5em; display: grid; place-items: center; line-height: 1.25; }
.card-sub { font-size: 10px; color: var(--choco-soft); }
.card.r2 { border-color: #9ec5f0; background: linear-gradient(#fff, #eef6ff); }
.card.r3 { border-color: var(--gold); background: linear-gradient(#fff, #fff4d6); }
.card.locked .card-e { filter: brightness(0) opacity(.18); }
.card.locked { background: #f6ece0; box-shadow: none; }

.stages-list { flex: 1; overflow-y: auto; padding: 4px 14px calc(var(--safe-bottom) + 20px); }
.stages-list h3 { margin: 16px 4px 8px; font-size: 15px; color: var(--berry-dark); }
.stage-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.stage { aspect-ratio: 1; border-radius: 14px; background: #fff; border: 2px solid #f0d7b8; box-shadow: 0 3px 0 #e6c9a5; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.stage b { font-size: 18px; }
.stage span { font-size: 10px; color: var(--gold); letter-spacing: -1px; }
.stage span i { font-style: normal; color: #e3d3bf; }
.stage.clear { background: #fff8e6; }
.stage.now { background: var(--berry); color: #fff; border-color: #ffd27a; }
.stage.locked { background: #efe3d4; box-shadow: none; opacity: .7; }

.settings { padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.setting { display: flex; align-items: center; gap: 12px; background: #fff; padding: 14px 16px; border-radius: 14px; font-weight: 700; border: 2px solid #f0d7b8; }
.setting span { flex: 1; }
.setting small { display: block; font-weight: 400; font-size: 11px; color: var(--choco-soft); margin-top: 3px; }
.setting input { width: 24px; height: 24px; accent-color: var(--berry); }
.btn-danger { margin-top: 20px; padding: 14px; border-radius: 14px; background: #fff; color: #c0392b; font-weight: 800; border: 2px solid #f3c4bd; }
.version { text-align: center; font-size: 12px; color: var(--choco-soft); }

/* ---------- モーダル ---------- */
.modal { position: absolute; inset: 0; background: rgba(60, 30, 20, .55); display: grid; place-items: center; z-index: 20; animation: fadeIn .2s; }
.modal[hidden] { display: none; }
.modal-card { width: min(86vw, 340px); background: var(--cream); border-radius: 24px; padding: 22px 18px 18px; text-align: center; border: 4px solid #ffd27a; box-shadow: 0 10px 30px rgba(0, 0, 0, .3); animation: popIn .35s cubic-bezier(.2, 1.5, .5, 1); }
@keyframes popIn { from { transform: scale(.6); opacity: 0; } }
.clear-title { font-size: 34px; font-weight: 900; color: var(--berry); -webkit-text-stroke: 1px #fff; }
.clear-stars { font-size: 44px; letter-spacing: 4px; }
.clear-stars span { color: #e3d3bf; display: inline-block; }
.clear-stars span.on { color: var(--gold); animation: starIn .5s cubic-bezier(.2, 1.8, .5, 1) both; text-shadow: 0 3px 0 #d48a0f; }
@keyframes starIn { from { transform: scale(0) rotate(-90deg); } }
.clear-sweet { font-size: 84px; line-height: 1.1; animation: bob 1.6s ease-in-out infinite; }
.clear-new { display: inline-block; background: var(--berry); color: #fff; font-weight: 900; padding: 2px 12px; border-radius: 999px; font-size: 14px; }
.clear-new[hidden] { display: none; }
.clear-name { font-weight: 800; font-size: 18px; margin-top: 6px; }
.clear-meta { font-size: 13px; color: var(--choco-soft); margin-top: 8px; line-height: 1.6; }
.clear-meta b { color: var(--berry); }
.modal-actions { display: flex; gap: 10px; margin-top: 18px; }
.modal-actions button { flex: 1; padding: 13px 0; border-radius: 16px; font-weight: 900; font-size: 17px; }
.btn-main { background: var(--berry); color: #fff; box-shadow: 0 4px 0 var(--berry-dark); }
.btn-sub { background: #fff; border: 2px solid #f0d7b8; box-shadow: 0 4px 0 #e6c9a5; }
.btn-main[hidden], .btn-sub[hidden] { display: none; }
.rarity { font-weight: 800; font-size: 13px; color: var(--choco-soft); }
.rarity.r2 { color: #3b7dd8; }
.rarity.r3 { color: #d48a0f; }
.sweet-note { font-size: 14px; line-height: 1.6; margin: 10px 0 0; }
.confirm-text { font-weight: 700; line-height: 1.6; margin: 6px 0 0; }

.toast { position: absolute; left: 50%; bottom: calc(var(--safe-bottom) + 110px); transform: translateX(-50%); background: rgba(60, 30, 20, .9); color: #fff; padding: 10px 18px; border-radius: 999px; font-size: 14px; font-weight: 700; white-space: nowrap; z-index: 30; }
.toast.show { animation: fadeIn .2s; }
.toast[hidden] { display: none; }

#fx { position: absolute; inset: 0; pointer-events: none; z-index: 25; overflow: hidden; }
#fx span { position: absolute; top: -40px; animation: fall linear forwards; }
@keyframes fall { to { transform: translate(var(--dx), 110vh) rotate(var(--rot)); } }
