@font-face {
  font-family: Fredoka;
  src: url("/fonts/fredoka.woff") format("woff");
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #f6ead9;
  --bg-2: #efe0ca;
  --peach: #f0c3a0;
  --cream: #fff7ea;
  --ink: #4a2f22;
  --muted: #8a6d5c;
  --purple: #6d3ec4;
  --purple-d: #532a9e;
  --yellow: #ffe27a;
  --green: #5cbf5a;
  --green-d: #3f9a42;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  height: 100%; height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family: Fredoka, "Trebuchet MS", "Segoe UI", sans-serif;
  overflow: hidden;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; cursor: pointer; }
a { color: inherit; }

.app {
  position: fixed; inset: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  display: grid;
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 1fr;
  max-width: 820px;
  margin: 0 auto;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 2px solid var(--peach);
  z-index: 5;
}

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  height: 36px; padding: 0 12px; border-radius: 999px;
  border: 0; background: #fff;
  font-weight: 700; font-size: 14px;
  box-shadow: 0 2px 0 rgba(120,70,30,0.14), 0 3px 8px rgba(120,70,30,0.08);
  white-space: nowrap; text-decoration: none;
}
.back-btn { background: rgba(255,255,255,0.85); padding: 0 11px 0 8px; }
.back-btn svg { width: 17px; height: 17px; flex: 0 0 auto; }

.level-btn {
  margin: 0 auto;
  background: linear-gradient(#8a5ef0, var(--purple));
  color: #fff;
  box-shadow: 0 3px 0 var(--purple-d), 0 4px 10px rgba(80,40,160,0.2);
  min-width: 0; max-width: 48vw;
  padding: 0 12px;
}
.level-btn .lvl-tag { font-weight: 800; }
.level-btn .lvl-name {
  font-weight: 500; font-size: 12.5px; opacity: 0.9;
  overflow: hidden; text-overflow: ellipsis;
}
.level-btn svg { width: 16px; height: 16px; flex: 0 0 auto; }
@media (max-width: 430px) {
  .level-btn .lvl-name { display: none; }
}

.top-right { display: flex; align-items: center; gap: 6px; }
.moves-pill { background: #fff7ea; border: 2px solid #f0d7b8; height: 36px; padding: 0 11px; gap: 5px; }
.moves-pill .lbl { font-size: 12px; font-weight: 600; color: var(--muted); }
.moves-pill strong { font-size: 15px; font-variant-numeric: tabular-nums; }

.icon-btn {
  width: 36px; height: 36px; border-radius: 50%; border: 0;
  background: rgba(255,255,255,0.8);
  display: grid; place-items: center; padding: 0; flex: 0 0 auto;
  box-shadow: 0 2px 0 rgba(120,70,30,0.12);
}
.icon-btn svg { width: 20px; height: 20px; }

/* ---------- stage ---------- */
.stage {
  position: relative;
  min-height: 0; min-width: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 4px 8px;
}

#boardCanvas {
  display: block;
  width: 100%; height: 100%;
  touch-action: none;
}

.hint-banner {
  position: absolute;
  left: 50%; bottom: 10px;
  transform: translateX(-50%);
  background: rgba(74, 47, 34, 0.92);
  color: #fff;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13.5px; font-weight: 600;
  white-space: nowrap; max-width: 92%;
  overflow: hidden; text-overflow: ellipsis;
  box-shadow: 0 6px 16px rgba(60,30,20,0.25);
  pointer-events: none;
  animation: fadeSlide 240ms ease;
}
@keyframes fadeSlide { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ---------- bottom bar ---------- */
.bottombar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 8px 12px 12px;
}

.act-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 44px; padding: 0 18px;
  border-radius: 999px; border: 0;
  background: #fff;
  font-weight: 700; font-size: 15px;
  box-shadow: 0 3px 0 rgba(120,70,30,0.16), 0 5px 12px rgba(120,70,30,0.1);
  transition: transform 90ms ease;
}
.act-btn:active:not(:disabled) { transform: translateY(2px); }
.act-btn:disabled { opacity: 0.45; cursor: default; }
.act-btn svg { width: 20px; height: 20px; flex: 0 0 auto; }
.act-help { background: var(--cream); border: 2px solid #ecd2b4; padding: 0 14px; }

/* ---------- iPhone landscape / short wide screen ---------- */
@media (orientation: landscape) and (max-height: 540px) {
  .app {
    max-width: none;
    grid-template-rows: auto 1fr;
    grid-template-columns: 168px 1fr;
    grid-template-areas:
      "top stage"
      "bot stage";
  }
  .topbar {
    grid-area: top;
    flex-direction: column;
    align-items: stretch;
    border-bottom: 0;
    border-right: 2px solid var(--peach);
    padding: 8px 10px 6px;
    gap: 7px;
  }
  .level-btn { margin: 0; max-width: none; justify-content: space-between; }
  .level-btn .lvl-name { display: none; }
  .top-right { justify-content: space-between; }
  .moves-pill { flex: 1 1 auto; justify-content: center; }

  .bottombar {
    grid-area: bot;
    flex-direction: column;
    justify-content: flex-end;
    align-items: stretch;
    border-right: 2px solid var(--peach);
    padding: 6px 10px 10px;
    gap: 7px;
  }
  .act-btn { height: 38px; font-size: 13.5px; padding: 0 10px; }

  .stage { grid-area: stage; padding: 4px 10px; }
  .hint-banner { bottom: 6px; font-size: 12.5px; padding: 6px 12px; }
}

/* ---------- fx & modals ---------- */
#fxCanvas {
  position: fixed; inset: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 40;
}

.modal-bg {
  position: fixed; inset: 0;
  background: rgba(50, 26, 16, 0.46);
  z-index: 60;
  display: flex; align-items: center; justify-content: center;
  padding: 14px;
  animation: fadeBg 180ms ease;
}
.modal-bg[hidden] { display: none; }
@keyframes fadeBg { from { opacity: 0; } }

.modal-card {
  width: min(460px, 100%);
  max-height: 88dvh;
  overflow: auto;
  background: var(--cream);
  border-radius: 26px;
  padding: 18px 18px 20px;
  box-shadow: 0 16px 40px rgba(50, 24, 12, 0.34);
  border: 3px solid #fff;
  animation: popCard 220ms cubic-bezier(.2,1.35,.5,1);
}
@keyframes popCard { from { transform: translateY(20px) scale(0.95); opacity: 0; } }

.modal-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.modal-head h2 { margin: 0; font-size: 22px; color: var(--purple); flex: 1 1 auto; }
.modal-x {
  width: 36px; height: 36px; border-radius: 50%; border: 0;
  background: #f0e0ca; color: var(--muted); font-size: 24px; line-height: 1;
}
.modal-sub { margin: 0 0 12px; color: var(--muted); font-size: 14px; font-weight: 500; }

.levels-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
}
@media (max-width: 400px) { .levels-grid { grid-template-columns: repeat(4, 1fr); } }

.lvl-cell {
  position: relative;
  aspect-ratio: 1;
  border-radius: 16px;
  border: 2px solid #ebd2b5;
  background: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-weight: 800; font-size: 18px;
  box-shadow: 0 3px 0 rgba(120,70,30,0.1);
}
.lvl-cell.cur { border-color: var(--purple); background: #f2ebff; color: var(--purple); }
.lvl-cell.done { background: #eaf8e6; border-color: #9be07f; color: #2f7a32; }
.lvl-cell.locked { background: #e8ded1; border-color: #d6c8b6; color: #a79684; box-shadow: none; cursor: default; }
.lvl-cell small { font-size: 11px; line-height: 1; margin-top: 2px; color: #f0a800; }

.help-list { list-style: none; padding: 0; margin: 4px 0 14px; display: flex; flex-direction: column; gap: 9px; }
.help-list li {
  display: flex; align-items: center; gap: 10px;
  background: #fff; border-radius: 16px; padding: 8px 10px;
  font-size: 13.5px; line-height: 1.3;
}
.help-list li b { display: block; font-size: 14.5px; margin-bottom: 1px; }
.hi {
  width: 38px; height: 38px; border-radius: 12px; flex: 0 0 auto;
  display: grid; place-items: center; font-weight: 800; font-size: 17px;
  background: #f5e6d2;
}
.hi-q { background: #6b5e7a; color: #fff; }
.hi-ice { background: #cfefff; color: #1f74a8; }
.hi-curt { background: #ffe0e6; }
.hi-bar { background: #ffe5bd; }

.end-card { text-align: center; padding: 24px 20px 22px; }
.end-badge {
  width: 72px; height: 72px; border-radius: 50%; margin: 0 auto 10px;
  background: linear-gradient(#ffe885, #ffbe26);
  border: 4px solid #fff;
  box-shadow: 0 6px 16px rgba(210, 140, 10, 0.35);
  display: grid; place-items: center;
  font-size: 36px; color: #8c4d00;
}
.end-badge.stuck { background: linear-gradient(#ffb09c, #f06d58); color: #fff; box-shadow: 0 6px 16px rgba(190, 60, 40, 0.3); }
.end-card h2 { margin: 0 0 4px; font-size: 26px; color: var(--purple); }
.end-stats { display: flex; justify-content: center; gap: 14px; margin: 10px 0 16px; }
.end-stat { background: #fff; border-radius: 16px; padding: 8px 16px; min-width: 96px; }
.end-stat small { display: block; font-size: 11.5px; color: var(--muted); font-weight: 600; }
.end-stat b { font-size: 20px; }
.end-actions { display: flex; flex-direction: column; gap: 8px; }

.primary-btn {
  height: 46px; padding: 0 22px; border-radius: 999px; border: 0;
  background: linear-gradient(#74d46e, var(--green-d));
  color: #fff; font-weight: 700; font-size: 16px;
  box-shadow: 0 4px 0 #2d7330;
}
.primary-btn.purple { background: linear-gradient(#8a5ef0, var(--purple)); box-shadow: 0 4px 0 var(--purple-d); }
.primary-btn.full { width: 100%; }
.sec-btn {
  height: 42px; padding: 0 18px; border-radius: 999px; border: 2px solid #e6ceb0;
  background: #fff; font-weight: 700; font-size: 15px;
}
