* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; }

:root {
  --bg: #1c2333;
  --panel: #2a3348;
  --accent: #ffb347;
  --accent2: #47d1ff;
  --good: #58d68d;
  --bad: #ff6b6b;
  --text: #eef2ff;
  --muted: #9aa7c7;
  --search-panel-height: 108px;
}

body {
  font-family: "Nunito Sans", "Segoe UI", system-ui, sans-serif;
  background: radial-gradient(1200px 800px at 50% -10%, #2b3550, var(--bg));
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

.game-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(100vw, 430px);
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  max-height: 100dvh;
  overflow: hidden;
  background: var(--panel);
  box-shadow: 0 0 50px rgba(0,0,0,.35);
}
.panel[hidden] { display: none !important; }

/* ---------- HUD ---------- */
.hud {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border-radius: 14px 14px 0 0;
  padding: 10px 14px;
  box-shadow: 0 4px 20px rgba(0,0,0,.35);
}
.hud[hidden] { display: none !important; }
.hud-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-right: 14px;
  min-width: 58px;
}
.hud-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }
.hud-value { font-size: 20px; font-weight: 700; }
.hud-timer .hud-value.low { color: var(--bad); animation: pulse .6s infinite; }
@keyframes pulse { 50% { opacity: .35; } }
.hud #btn-sound { margin-left: auto; }

.icon-btn {
  background: var(--panel);
  border: 2px solid #3a465f;
  border-radius: 10px;
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
  margin-right: 10px;
  transition: transform .12s;
}
.icon-btn:hover { transform: scale(1.1); }

/* ---------- Раскладка ---------- */
.main {
  position: absolute;
  inset: 0;
  display: flex;
  width: 100%;
  min-width: 0;
  flex-direction: column;
  min-height: 0;
  padding-bottom: calc(var(--search-panel-height) + env(safe-area-inset-bottom));
  gap: 0;
  background: var(--panel);
  overflow: hidden;
}
.game-wrap.menu-open .main { padding-bottom: 0; }
#fly-layer {
  position: fixed;
  inset: 0;
  z-index: 2147483647;
  overflow: visible;
  pointer-events: none;
}

/* ---------- Сцена ---------- */
.scene-frame { position: relative; flex: 1 1 0%; min-width: 0; min-height: 0; padding: 0; }
.scene {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  user-select: none;
  touch-action: none;
  scrollbar-width: none;
  cursor: default;
}
.scene::-webkit-scrollbar { display: none; }

.scene-inner {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #b09a7d; /* фон замка */
  transform-origin: center;
  transform: translate3d(0, 0, 0);
  will-change: transform;
}

/* свой уровень: фон-картинка вместо слоёв замка */
.scene-inner.custom .cz,
.scene-inner.custom .deco { display: none; }
.scene-inner.custom {
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
}

/* маски: один слой фона, вырезанный по всем прямоугольникам ("за колонной") */
.scene-cover {
  position: absolute;
  inset: 0;
  z-index: 8;
  pointer-events: none;
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
}

/* ---------- Слои замка ---------- */
.cz { position: absolute; pointer-events: none; }

/* каменная кладка всей стены */
.cz-stone {
  inset: 0;
  background-color: #b39c7e;
  background-image:
    radial-gradient(120% 90% at 50% 0%, rgba(255,226,160,.25), transparent 55%),
    linear-gradient(180deg, rgba(30,18,8,.18), transparent 18%),
    repeating-linear-gradient(0deg,  rgba(60,38,18,.22) 0 2px, transparent 2px 34px),
    repeating-linear-gradient(90deg, rgba(60,38,18,.16) 0 2px, transparent 2px 58px);
}

/* витраж */
.cz-window {
  top: 5%; left: 37%; width: 26%; height: 21%;
  border: 6px solid #5f412a;
  border-bottom-color: #6f4e31;
  background:
    repeating-linear-gradient(90deg, transparent 0 30%, rgba(70,44,22,.85) calc(30% + 0px) 34%),
    repeating-linear-gradient(0deg, transparent 0 26%, rgba(70,44,22,.85) 28% 33%),
    conic-gradient(from 90deg, #79c7d9 0 25%, #e5a7c8 0 45%, #f4dcA4 0 60%, #a2dab3 0 75%, #b7a6e8 0 100%);
  border-radius: 45% 45% 6% 6% / 55% 55% 6% 6%;
  box-shadow: 0 0 40px rgba(244,220,164,.5), inset 0 0 26px rgba(70,44,22,.35);
}

/* баннеры с гербами */
.cz-banner {
  background: linear-gradient(160deg, #bc3a3a, #8c1f1f);
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% 84%, 0 100%);
  border-radius: 3% 3% 0 0;
}
.cz-banner::after {
  content: ""; position: absolute;
  top: 24%; left: 50%; width: 42%; aspect-ratio: 1;
  transform: translateX(-50%);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #f7d468, #d99a24 70%);
  box-shadow: inset 0 0 0 3px #8c6a15, 0 1px 3px rgba(0,0,0,.4);
}

/* полка книг на 1 ярусе */
.cz-shelf {
  top: 31.5%; left: 2%; width: 96%; height: 3.4%;
  background: linear-gradient(180deg, #7a5230, #5c3d22);
  border-radius: 4px;
  box-shadow: 0 3px 5px rgba(0,0,0,.3);
}

/* балюстрада */
.cz-rail {
  top: 34.5%; left: 0; right: 0; height: 6.5%;
  border-top: 5px solid #6f4c2d;
  border-bottom: 5px solid #6f4c2d;
  background: repeating-linear-gradient(90deg, #7a5433 0 7px, transparent 7px 17px);
}

/* арка среднего зала */
.cz-arch {
  top: 41%; left: 33%; width: 34%; height: 26%;
  border: 9px solid #6f4c2d;
  border-bottom: none;
  background: linear-gradient(180deg, #57492f 0%, #3a3021 55%, #26201594 100%);
  border-radius: 50% 50% 0 0 / 65% 65% 0 0;
  box-shadow: inset 0 0 34px rgba(0,0,0,.65), 0 0 18px rgba(60,38,18,.4);
}

/* река */
.cz-river {
  bottom: 0; left: 0; right: 0; height: 24%;
  background:
    repeating-linear-gradient(118deg, rgba(255,255,255,.22) 0 9px, transparent 9px 30px),
    linear-gradient(180deg, #5fc0d4 0%, #3593ab 60%, #2a7d95 100%);
  border-top: 4px solid #cfeff6;
}
.cz-river::after { /* светлые блики */
  content: ""; position: absolute; inset: 0;
  background: radial-gradient(60% 30% at 30% 20%, rgba(255,255,255,.3), transparent 70%);
}

/* каменный мост через реку */
.cz-bridge {
  bottom: 9%; left: 6%; width: 88%; height: 22%;
  border-top: 16px solid #8a5a33;
  border-radius: 50%;
  transform: rotate(-7deg);
  box-shadow: 0 4px 0 #6d4527, 0 8px 14px rgba(0,0,0,.3);
}

/* тёмные ниши-арки по бокам нижнего яруса */
.cz-niche {
  width: 14%; height: 13%;
  border: 5px solid #6f4c2d;
  border-bottom: none;
  background: linear-gradient(180deg, #372c1e, #201a12);
  border-radius: 50% 50% 0 0 / 60% 60% 0 0;
}

/* виньетка над всем для глубины */
.cz-vignette {
  inset: 0; z-index: 6;
  background: radial-gradient(115% 115% at 50% 48%, transparent 52%, rgba(22,12,5,.5) 100%);
}

/* ---------- Декор-эмодзи ---------- */
.deco {
  position: absolute;
  transform: translate(-50%, -50%);
  opacity: .75;
  filter: saturate(.85);
  pointer-events: none;
  z-index: 2;
  text-shadow: 0 2px 3px rgba(0,0,0,.35);
}
.deco.torch {
  opacity: 1;
  filter: drop-shadow(0 0 10px rgba(255,150,50,.8));
  z-index: 3;
}

/* ---------- Предметы ---------- */
.obj {
  position: absolute;
  transform: translate(-50%, -50%) rotate(var(--rot, 0deg));
  background: none;
  border: none;
  cursor: default;
  font-family: inherit;
  line-height: 1;
  padding: 0;
  z-index: 5;
  transition: none;
  font-size: calc(var(--obj-size, 34px) * var(--k, 1));
  filter: brightness(var(--shade, 1)) drop-shadow(0 2px 2px rgba(0,0,0,.35));
}
.obj.is-decoration { z-index: 4; pointer-events: none; cursor: default; }
.obj.img-obj {
  width: calc(var(--obj-size, 34px) * var(--k, 1));
  height: calc(var(--obj-size, 34px) * var(--k, 1));
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
}
.obj.flip { transform: translate(-50%, -50%) rotate(var(--rot, 0deg)) scaleX(-1); }
.obj:active { opacity: .85; }

.obj.found {
  pointer-events: none;
  animation: found-pop .45s ease forwards;
  z-index: 8;
}
@keyframes found-pop {
  0%   { transform: translate(-50%,-50%) rotate(var(--rot,0deg)) scale(1);   filter: drop-shadow(0 0 0 #58d68d); }
  40%  { transform: translate(-50%,-50%) rotate(var(--rot,0deg)) scale(1.7); filter: drop-shadow(0 0 14px #58d68d); }
  100% { transform: translate(-50%,-50%) rotate(var(--rot,0deg)) scale(0);   opacity: 0; }
}

.obj.wrong { animation: shake .4s; }
@keyframes shake {
  0%,100% { transform: translate(-50%,-50%) rotate(var(--rot,0deg)); }
  25% { transform: translate(calc(-50% - 6px), -50%) rotate(var(--rot,0deg)); }
  75% { transform: translate(calc(-50% + 6px), -50%) rotate(var(--rot,0deg)); }
}

/* подсказка компаса: подсветка найденного */
.obj.hint { animation: hint-glow 1.2s ease 2; }
@keyframes hint-glow {
  50% { filter: drop-shadow(0 0 16px var(--accent2)) brightness(1.3); }
}
.obj.hint-strong { animation: hint-strong 1s ease 3; }
@keyframes hint-strong {
  50% { filter: drop-shadow(0 0 26px var(--accent)) brightness(1.8); transform: translate(-50%,-50%) rotate(var(--rot,0deg)) scale(1.5); }
}

/* ---------- Круглые кнопки как в референсе ---------- */
.scene-controls {
  position: absolute;
  right: 18px;
  bottom: 18px;
  display: flex;
  gap: 12px;
  z-index: 10;
}
.scene-controls[hidden] { display: none !important; }
.scene-controls.hidden { display: none; }
.round-btn {
  position: relative;
  width: 58px; height: 58px;
  border-radius: 50%;
  border: 3px solid #fff;
  background: linear-gradient(160deg, #ffb54d, #ff8a1e);
  font-size: 26px;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(0,0,0,.4), inset 0 2px 4px rgba(255,255,255,.4);
  transition: transform .12s;
}
.round-btn:hover { transform: scale(1.08); }
.round-btn:active { transform: scale(.95); }
.round-btn:disabled { filter: grayscale(.9); opacity: .6; cursor: default; transform: none; }
.round-btn .charges {
  position: absolute;
  top: -4px; right: -4px;
  min-width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--bad);
  color: #fff;
  font-size: 13px; font-weight: 800;
  line-height: 22px;
  box-shadow: 0 2px 4px rgba(0,0,0,.4);
}

/* счётчик 0/50 под сценой */
.found-counter {
  position: absolute;
  left: 16px;
  bottom: 6px;
  font-size: 26px;
  font-weight: 800;
  color: #4a5568;
  background: rgba(255,255,255,.85);
  border-radius: 10px 10px 0 0;
  padding: 2px 12px;
  z-index: 10;
}
.found-counter.hidden { display: none; }
.found-counter[hidden] { display: none !important; }
.found-counter #found-num { color: #ff9a24; }

/* ---------- Нижняя лента поиска ---------- */
.panel {
  position: absolute;
  z-index: 30;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(var(--search-panel-height) + env(safe-area-inset-bottom));
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  border-top: 1px solid #46536c;
  background: #232c42;
  padding: 5px 10px calc(5px + env(safe-area-inset-bottom));
}
.panel-status {
  flex: 0 0 12px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
}
.targets {
  list-style: none;
  display: flex;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  gap: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  overscroll-behavior-x: contain;
  scroll-snap-type: x proximity;
}
.targets::-webkit-scrollbar { display: none; }
.targets li {
  position: relative;
  flex: 0 0 min(76px, calc((100vw - 44px) / 4));
  width: min(76px, calc((100vw - 44px) / 4));
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  scroll-snap-align: center;
  background: #2d374f;
  border-radius: 8px;
  transition: .25s;
}
.targets li .t-emoji { font-size: 42px; line-height: 1; }
.targets li .t-img {
  width: min(60px, calc((100vw - 60px) / 4));
  height: min(60px, calc((100vw - 60px) / 4));
  object-fit: contain;
}
.targets li .t-count {
  position: absolute;
  right: 2px;
  bottom: 2px;
  min-width: 38px;
  padding: 2px 4px;
  border: 1px solid rgba(255,255,255,.2);
  border-radius: 9px;
  background: rgba(16, 20, 32, .92);
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
  pointer-events: none;
}
.targets li.done { opacity: .35; background: #22402f; }

.targets li.done::after {
  content: "✓";
  position: absolute; right: 3px; top: 0;
  color: #9ae7b5; font-size: 11px; font-weight: 800;
}

.found-count { flex: 0 0 auto; text-align: left; font-size: 11px; color: var(--muted); white-space: nowrap; }

/* ---------- Оверлеи ---------- */
.overlay {
  position: absolute; inset: 0;
  border-radius: 0;
  background: rgba(16, 20, 32, .82);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 20;
}
.overlay.visible { display: flex; }
.overlay#overlay-start {
  display: none;
  background: #d6c7b4 url("menu-background.png") center center / cover no-repeat;
  backdrop-filter: none;
}
.overlay#overlay-start.visible { display: flex; }
.menu-play-wrap { position: absolute; z-index: 1; left: 11%; right: 11%; bottom: max(2.6%, env(safe-area-inset-bottom)); text-align: center; }
.menu-play {
  display: block; width: 100%; padding: 0; border: 0; line-height: 0; cursor: pointer;
  background: transparent; filter: drop-shadow(0 6px 5px rgba(54,31,13,.24));
}
.menu-play img { display: block; width: 100%; height: auto; }
.menu-play:active { transform: translateY(2px); }
.menu-play:disabled { opacity: .7; cursor: default; }
.menu-level { margin-top: clamp(4px, .7vh, 11px); color: #074b43; font: 700 clamp(25px, 3vh, 46px)/1.1 "Lora", Georgia, serif; text-shadow: 0 2px 1px rgba(255,239,198,.9); }
.profile-trigger, .settings-trigger {
  position: absolute; z-index: 2; top: max(5%, env(safe-area-inset-top));
  display: grid; place-items: center; width: 64px; height: 64px; flex: 0 0 64px;
  padding: 0; border: 0; border-radius: 50%; cursor: pointer; background: transparent;
  transition: transform .12s ease;
}
.profile-trigger { left: 5.2%; }
.settings-trigger { right: 5.2%; }
.profile-trigger:hover, .settings-trigger:hover { transform: translateY(-2px) scale(1.025); }
.profile-trigger img, .settings-trigger img { display: block; width: 100%; height: 100%; object-fit: contain; }
.settings-modal { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 20px; background: #111827a8; backdrop-filter: blur(5px); }
.settings-modal[hidden] { display: none; }
.settings-card { position: relative; width: min(100%, 340px); padding: 28px 22px 22px; border: 2px solid #d19a4c; border-radius: 20px; color: #f7e5bf; text-align: center; background: linear-gradient(145deg, #164f4b, #123532); box-shadow: 0 16px 45px #0008, inset 0 0 0 3px #1e6a61; }
.settings-card h2 { margin-bottom: 22px; font: 700 27px "Lora", Georgia, serif; }
.settings-close { position: absolute; top: 8px; right: 12px; border: 0; color: #f8d69b; font-size: 30px; background: none; cursor: pointer; }
.settings-reset { width: 100%; min-height: 48px; border: 1px solid #e0a552; border-radius: 12px; color: #fff0d2; font-weight: 700; background: #8c3e2d; cursor: pointer; }
.overlay-card {
  text-align: center;
  padding: 24px 30px;
  max-width: 460px;
  animation: card-in .35s ease;
}
@keyframes card-in { from { transform: translateY(16px) scale(.96); opacity: 0; } to { transform: none; opacity: 1; } }
.overlay-card h1 { font-size: 28px; margin-bottom: 12px; }
.overlay-card h1 { font-family: "Lora", Georgia, serif; }
.overlay-card p { color: var(--muted); line-height: 1.5; margin-bottom: 10px; }

/* ---------- Кнопки ---------- */
.btn {
  border: none;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #ff9a24);
  color: #262010;
  font-weight: 700;
  cursor: pointer;
  transition: transform .12s, box-shadow .12s;
  box-shadow: 0 4px 14px rgba(255, 170, 60, .35);
}
.btn:hover { transform: translateY(-2px); }
.btn:active { transform: translateY(0) scale(.97); }
.btn-big { font-size: 18px; padding: 12px 30px; }
.btn-small { font-size: 14px; padding: 6px 10px; }
.btn-blue { background: linear-gradient(135deg, var(--accent2), #2ba6e0); color: #0d2233; box-shadow: 0 4px 14px rgba(70,190,255,.3); }

/* меню из 2 кнопок */
.menu-buttons { display: flex; flex-direction: column; gap: 12px; margin-top: 10px; }
.menu-buttons .btn-big { display: flex; flex-direction: column; align-items: center; padding: 12px 26px; }
.menu-buttons .sub { font-size: 11px; font-weight: 600; opacity: .75; margin-top: 2px; }

/* свои уровни + ссылка в конструктор */
.menu-custom { margin-top: 16px; }
.menu-custom h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin-bottom: 8px; }
.menu-custom #custom-list { display: flex; flex-direction: column; gap: 8px; }
.menu-custom .btn-small { width: 100%; padding: 9px 12px; }
.menu-editor-link {
  display: block;
  margin-top: 14px;
  font-size: 13px;
  color: var(--muted);
  text-decoration: none;
}
.menu-editor-link:hover { color: var(--accent2); }

/* ---------- Адаптив ---------- */
@media (max-width: 430px) {
  .game-wrap { width: 100vw; }
}
