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

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: #7f8cbb;
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Arial Rounded MT Bold", system-ui, sans-serif;
  user-select: none; -webkit-user-select: none;
  overscroll-behavior: none;
}

/* портрет 9:16: на широких экранах — колонка по центру, на узких — весь экран */
#stage {
  position: relative; margin: 0 auto;
  height: 100dvh; width: min(100vw, calc(100dvh * 9 / 16));
  overflow: hidden; background: linear-gradient(180deg, #afcbe6 0%, #91a0d0 100%);
}
#stage canvas { display: block; width: 100%; height: 100%; touch-action: none; }

.vignette {
  position: absolute; inset: 0; pointer-events: none;
  background: none;
}

#hud {
  position: absolute; left: 0; right: 0; top: calc(env(safe-area-inset-top) + 18px);
  display: flex; flex-direction: column; align-items: center; gap: 10px; pointer-events: none;
}
#lvl-name { font-size: 26px; font-weight: 800; color: #2f3a66; letter-spacing: .02em; }
.bar {
  width: 56%; height: 14px; border-radius: 99px; background: rgba(255,255,255,.7);
  box-shadow: inset 0 2px 4px rgba(120,70,40,.18), 0 2px 0 rgba(255,255,255,.6); overflow: hidden;
}
.bar i {
  display: block; height: 100%; width: 0; border-radius: 99px;
  background: linear-gradient(90deg, #8fc45a, #c3dc6a);
  transition: width .35s cubic-bezier(.2,1.4,.4,1);
}

#chips { position: absolute; inset: 0; pointer-events: none; }
.chip {
  position: absolute; left: 0; top: 0; min-width: 44px; padding: 5px 10px;
  border-radius: 99px; text-align: center; font-weight: 800; font-size: 15px;
  box-shadow: 0 3px 0 rgba(90,50,30,.18), inset 0 2px 0 rgba(255,255,255,.3);
  opacity: .75; transition: opacity .2s;
}
.chip.on { opacity: 1; }
.chip.done { opacity: .45; }

#win {
  position: absolute; inset: 0; display: grid; place-items: center;
  background: rgba(145,160,208,.55); /* без backdrop-filter: на телефонах он заставляет заново размывать канвас каждый кадр */
  opacity: 0; pointer-events: none; transition: opacity .35s;
}
#win.show { opacity: 1; pointer-events: auto; }
#win .card {
  padding: 28px 40px 30px; border-radius: 32px; background: #fffaf3; text-align: center;
  box-shadow: 0 10px 0 rgba(120,70,40,.14), 0 24px 50px rgba(120,70,40,.2);
  transform: scale(.7); transition: transform .45s cubic-bezier(.2,1.6,.4,1);
}
#win.show .card { transform: scale(1); }
#win .big { font-size: 44px; font-weight: 900; color: #5f8a34; margin-bottom: 18px; }
#again {
  font: inherit; font-size: 20px; font-weight: 800; color: #fff; border: 0; cursor: pointer;
  padding: 14px 34px; border-radius: 99px; background: #7fb548;
  box-shadow: 0 5px 0 #5f8a34;
}
#again:active { transform: translateY(3px); box-shadow: 0 2px 0 #5f8a34; }

#loading {
  position: absolute; inset: 0; display: grid; place-items: center;
  background: linear-gradient(180deg, #afcbe6 0%, #91a0d0 100%); color: #2f3a66; font-weight: 700; font-size: 18px;
}

/* ---------- настройки ---------- */
.set-btn {
  position: absolute; z-index: 5; right: 14px; top: calc(env(safe-area-inset-top) + 14px);
  width: 46px; height: 46px; border: 0; border-radius: 50%; padding: 0; cursor: pointer;
  display: grid; place-items: center; color: #8a6552; background: #fffaf3;
  box-shadow: 0 4px 0 rgba(120,70,40,.16), 0 8px 18px rgba(120,70,40,.14);
  transition: transform .15s, box-shadow .15s, color .2s;
}
.set-btn svg { transition: transform .45s cubic-bezier(.2,1.5,.4,1); }
.set-btn.open { color: #5f8a34; }
.set-btn.open svg { transform: rotate(90deg); }
.set-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(120,70,40,.16); }

.set-panel {
  position: absolute; z-index: 4; right: 14px; top: calc(env(safe-area-inset-top) + 70px);
  width: min(290px, calc(100% - 28px)); padding: 16px 18px 18px;
  border-radius: 26px; background: rgba(255,250,243,.97);
  box-shadow: 0 8px 0 rgba(120,70,40,.12), 0 20px 44px rgba(120,70,40,.22);
  transform-origin: calc(100% - 23px) -10px;
  transform: scale(.6); opacity: 0; pointer-events: none;
  transition: transform .32s cubic-bezier(.2,1.5,.4,1), opacity .18s;
}
.set-panel.open { transform: scale(1); opacity: 1; pointer-events: auto; }

.set-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.set-title { font-size: 20px; font-weight: 800; color: #6b4a3a; }
.set-close {
  width: 30px; height: 30px; border: 0; border-radius: 50%; cursor: pointer;
  font: inherit; font-size: 13px; font-weight: 800; color: #a07a66; background: #f3e4d6;
}
.set-close:active { background: #ead5c3; }

.set-row { display: block; margin-bottom: 14px; }
.set-row-top { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.set-label { font-size: 15px; font-weight: 700; color: #7d5a48; }
.set-val {
  font-size: 13px; font-weight: 800; color: #5f8a34; background: #e9f3d4;
  padding: 2px 9px; border-radius: 99px; font-variant-numeric: tabular-nums;
}

.set-range {
  -webkit-appearance: none; appearance: none; display: block; width: 100%; height: 26px; margin: 0;
  background: transparent; cursor: pointer; touch-action: none; --p: 50%;
}
.set-range::-webkit-slider-runnable-track {
  height: 12px; border-radius: 99px;
  background: linear-gradient(90deg, #8fc45a var(--p), #efdfd0 var(--p));
  box-shadow: inset 0 2px 3px rgba(120,70,40,.16);
}
.set-range::-moz-range-track {
  height: 12px; border-radius: 99px;
  background: linear-gradient(90deg, #8fc45a var(--p), #efdfd0 var(--p));
  box-shadow: inset 0 2px 3px rgba(120,70,40,.16);
}
.set-range::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 26px; height: 26px; margin-top: -7px;
  border-radius: 50%; background: #fff; border: 3px solid #7fb548;
  box-shadow: 0 3px 0 rgba(95,138,52,.35), 0 4px 10px rgba(120,70,40,.2);
}
.set-range::-moz-range-thumb {
  width: 20px; height: 20px; border-radius: 50%; background: #fff; border: 3px solid #7fb548;
  box-shadow: 0 3px 0 rgba(95,138,52,.35), 0 4px 10px rgba(120,70,40,.2);
}
.set-range:active::-webkit-slider-thumb { transform: scale(1.12); }

.set-actions { display: flex; gap: 10px; margin-top: 18px; }
.set-action {
  flex: 1; font: inherit; font-size: 15px; font-weight: 800; border: 0; cursor: pointer;
  padding: 11px 0; border-radius: 99px; color: #8a6552; background: #f3e4d6;
  box-shadow: 0 4px 0 #e0c9b6;
}
.set-action.primary { color: #fff; background: #7fb548; box-shadow: 0 4px 0 #5f8a34; }
.set-action:active { transform: translateY(3px); box-shadow: 0 1px 0 #e0c9b6; }
.set-action.primary:active { box-shadow: 0 1px 0 #5f8a34; }

.restart-btn { right: 70px; }
.restart-btn:active svg { transform: rotate(-90deg); }

/* ---------- переключение уровней ---------- */
.lvl-row { display: flex; align-items: center; gap: 12px; }
.lvl-arrow {
  pointer-events: auto; width: 34px; height: 34px; border: 0; border-radius: 50%; padding: 0 0 3px;
  font: inherit; font-size: 24px; font-weight: 800; line-height: 1; color: #8a6552; cursor: pointer;
  background: #fffaf3; box-shadow: 0 3px 0 rgba(120,70,40,.16), 0 6px 14px rgba(120,70,40,.12);
}
.lvl-arrow:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(120,70,40,.16); }

.set-info {
  margin-top: 14px; padding: 9px 11px; border-radius: 12px; background: #f3e4d6;
  font: 600 11px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; color: #7d5a48;
  white-space: pre-line; font-variant-numeric: tabular-nums;
}
.set-info:empty { display: none; }

.set-toggle-row { display: flex; align-items: center; justify-content: space-between; margin: 2px 0 14px; }
.set-switch {
  position: relative; width: 52px; height: 30px; border: 0; border-radius: 99px; padding: 0; cursor: pointer;
  background: #efdfd0; box-shadow: inset 0 2px 3px rgba(120,70,40,.16); transition: background .2s;
}
.set-switch::after {
  content: ''; position: absolute; left: 3px; top: 3px; width: 24px; height: 24px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 0 rgba(120,70,40,.2), 0 3px 8px rgba(120,70,40,.18);
  transition: transform .22s cubic-bezier(.2,1.5,.4,1);
}
.set-switch.on { background: #8fc45a; }
.set-switch.on::after { transform: translateX(22px); }
