:root {
  --ink: #2b2a33;
  --paper: #f4ecd8;
  --night: #1b2440;
  --red: #c8443c;
  --gold: #e9b949;
  --font-hand: "Segoe Print", "Comic Sans MS", "Marker Felt", "Bradley Hand", cursive;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--night);
  color: var(--ink);
  font-family: var(--font-hand);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

body { position: fixed; inset: 0; }

#game {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

/* Слой интерфейса: сам клики не ловит, ловят только его дети. */
#ui {
  position: absolute;
  inset: 0;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
  pointer-events: none;
}
#ui > * { pointer-events: auto; }

/* ===== Интерфейс: листки блокнота (E1–E3) ===== */
:root {
  --night-rgb: 27, 36, 64;
  --r-paper: 14px 18px 12px 20px / 18px 12px 20px 14px;
  --r-paper2: 20px 12px 18px 14px / 12px 20px 14px 18px;
  --shadow-paper: 0 3px 0 rgba(43, 42, 51, .18), 0 6px 14px rgba(21, 28, 54, .35);
  --fs: clamp(15px, 1.2vw + 1.6vh + 6px, 22px);
}

/* Общая кнопка-листок */
.pbtn {
  font: inherit;
  font-family: var(--font-hand);
  font-size: var(--fs);
  color: var(--ink);
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: var(--r-paper);
  min-width: 44px;
  min-height: 44px;
  padding: 6px 14px;
  box-shadow: var(--shadow-paper);
  cursor: pointer;
  touch-action: manipulation;
  line-height: 1.1;
}
.pbtn:hover { background: #f9f3e3; }
.pbtn:active, .pbtn.pressed {
  transform: translate(1px, 1px);
  background: #e3d8bc;
  box-shadow: 0 1px 0 rgba(43, 42, 51, .18), 0 2px 6px rgba(21, 28, 54, .3);
}
.pbtn:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.pbtn.accent { background: var(--gold); }
.pbtn.accent:active { background: #cf9f32; }
.pbtn.danger { background: var(--red); color: var(--paper); }
.pbtn.danger:active { background: #a8352e; }

/* ----- Диалог ----- */
#ui > .dlg {
  position: absolute;
  inset: 0;
  display: none;
  pointer-events: auto;
  touch-action: manipulation;
}
#ui > .dlg.open { display: block; }
.dlg-panel {
  position: absolute;
  left: 50%;
  bottom: clamp(6px, 2vh, 20px);
  transform: translateX(-50%);
  width: min(640px, calc(100% - 16px));
  min-height: clamp(72px, 20vh, 150px);
  max-height: 50vh;
  overflow: visible;
  padding: clamp(10px, 2vh, 18px) clamp(12px, 3vw, 22px) clamp(14px, 2.4vh, 22px);
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: var(--r-paper);
  box-shadow: var(--shadow-paper);
  font-family: var(--font-hand);
  font-size: var(--fs);
  line-height: 1.35;
  cursor: pointer;
}
.dlg-name {
  position: absolute;
  left: 14px;
  top: 0;
  transform: translateY(-100%);
  margin-top: 2px;
  padding: 3px 14px 2px;
  background: var(--gold);
  border: 2px solid var(--ink);
  border-bottom: 0;
  border-radius: 12px 16px 0 0 / 14px 12px 0 0;
  font-weight: bold;
  font-size: .9em;
}
.dlg-name[hidden] { display: none; }
.dlg-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.dlg-next {
  position: absolute;
  right: 14px;
  bottom: 6px;
  font-size: .75em;
  color: var(--red);
  visibility: hidden;
  animation: dlg-blink 1s steps(2, jump-none) infinite;
}
.dlg-next.on { visibility: visible; }
@keyframes dlg-blink { 0% { opacity: 1; } 100% { opacity: .25; } }

/* ----- Тосты ----- */
#ui > .toasts {
  position: absolute;
  top: calc(var(--safe-t) + 8px);
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}
.toast {
  max-width: min(520px, calc(100% - 140px));
  padding: 8px 16px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: var(--r-paper2);
  box-shadow: var(--shadow-paper);
  font-family: var(--font-hand);
  font-size: calc(var(--fs) * .9);
  text-align: center;
  pointer-events: none;
  animation: toast-life var(--ms, 2400ms) ease forwards;
}
@keyframes toast-life {
  0% { opacity: 0; transform: translateY(-24px) rotate(-1.5deg); }
  8% { opacity: 1; transform: translateY(0) rotate(-.5deg); }
  85% { opacity: 1; transform: translateY(0) rotate(-.5deg); }
  100% { opacity: 0; transform: translateY(-8px) rotate(-.5deg); }
}

/* ----- Инвентарь ----- */
#ui > .inv {
  position: absolute;
  left: calc(var(--safe-l) + 8px);
  bottom: calc(var(--safe-b) + 8px);
  max-width: calc(100% - 140px);
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 6px;
  pointer-events: none;
}
#ui > .inv.hidden { display: none; }
.inv > * { pointer-events: auto; }
.inv-stars {
  min-width: 52px;
  height: 44px;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: var(--r-paper2);
  box-shadow: var(--shadow-paper);
  font-family: var(--font-hand);
  font-size: var(--fs);
  color: var(--ink);
  white-space: nowrap;
}
.inv-stars .star { font-size: 1.15em; color: #9a968a; }
.inv-stars.has .star { color: var(--gold); text-shadow: 0 0 1px var(--ink), 0 0 1px var(--ink), 0 0 6px rgba(233, 185, 73, .8); }
.inv-slot {
  width: 52px;
  height: 52px;
  padding: 0;
  display: block;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: var(--r-paper);
  box-shadow: var(--shadow-paper);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .12s;
}
.inv-slot canvas { display: block; width: 100%; height: 100%; pointer-events: none; }
.inv-slot:active { transform: translate(1px, 1px); background: #e3d8bc; }
.inv-slot.sel {
  border-color: var(--gold);
  outline: 3px solid var(--gold);
  outline-offset: -1px;
  transform: translateY(-4px);
}
.inv-slot.pop { animation: inv-pop .5s ease-out; }
@keyframes inv-pop {
  0% { transform: scale(.2) rotate(-12deg); }
  60% { transform: scale(1.25) rotate(4deg); }
  100% { transform: scale(1) rotate(0); }
}

/* ----- HUD ----- */
#ui > .hud {
  position: absolute;
  inset: 0;
  padding: inherit;
  pointer-events: none;
}
.hud > * { pointer-events: auto; }
.hud-top {
  position: absolute;
  top: calc(var(--safe-t) + 8px);
  right: calc(var(--safe-r) + 8px);
  display: flex;
  gap: 8px;
  pointer-events: none;
}
.hud-top > * { pointer-events: auto; }
.hud-bottom {
  position: absolute;
  right: calc(var(--safe-r) + 8px);
  bottom: calc(var(--safe-b) + 8px);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  pointer-events: none;
}
.hud-bottom > * { pointer-events: auto; }
.hud-rot { display: flex; gap: 8px; }
#ui > .hud.hidden { display: none; }
.hud-bottom.hidden { display: none; }
.hud-btn { width: 48px; height: 48px; padding: 0; font-size: 26px; display: flex; align-items: center; justify-content: center; position: relative; }
.hud-extra[hidden] { display: none; }
.hud-snd.muted::after {
  content: "";
  position: absolute;
  left: 8px; right: 8px; top: 50%;
  border-top: 3px solid var(--red);
  transform: rotate(-45deg);
}

/* ----- Меню ----- */
#ui > .menu {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(var(--safe-t) + 8px) calc(var(--safe-r) + 8px) calc(var(--safe-b) + 8px) calc(var(--safe-l) + 8px);
  background: radial-gradient(ellipse at 50% 30%, rgba(63, 111, 176, .55), rgba(var(--night-rgb), .88));
  overflow: hidden;
  pointer-events: auto;
  touch-action: manipulation;
  animation: menu-in .25s ease-out;
}
.menu.ending {
  align-items: flex-end;
  background: rgba(var(--night-rgb), .06);
}
@keyframes menu-in { from { opacity: 0; } to { opacity: 1; } }
.menu-card {
  position: relative;
  z-index: 1;
  width: min(440px, 100%);
  max-height: 100%;
  overflow: auto;
  padding: clamp(12px, 3vh, 26px) clamp(14px, 4vw, 28px);
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: var(--r-paper);
  box-shadow: var(--shadow-paper);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: clamp(6px, 1.6vh, 12px);
  font-family: var(--font-hand);
  font-size: var(--fs);
  text-align: center;
}
.menu.ending .menu-card { margin-bottom: clamp(4px, 3vh, 24px); width: min(520px, 100%); }
.menu-title { margin: 0; font-size: clamp(30px, 8vw + 2vh, 56px); line-height: 1; color: var(--red); text-shadow: 2px 2px 0 rgba(43, 42, 51, .15); }
.menu-sub { margin: 0 0 4px; font-size: 1.1em; color: var(--ink); opacity: .8; }
.menu-h { margin: 0; font-size: 1.4em; }
.menu-end-title { margin: 0; font-size: clamp(28px, 7vw + 2vh, 52px); line-height: 1.05; color: var(--gold); text-shadow: 0 0 1px var(--ink), 2px 2px 0 var(--ink); }
.menu-text { margin: 0; line-height: 1.35; }
.menu-help { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; text-align: left; font-size: .9em; }
.menu-help li { display: flex; gap: 10px; align-items: center; }
.menu-help .g {
  flex: 0 0 38px; width: 38px; height: 38px;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
  background: #fff8e6;
  border: 2px solid var(--ink);
  border-radius: var(--r-paper2);
}
.menu-btns { display: flex; flex-direction: column; gap: 8px; }
.menu-row { display: flex; gap: 8px; }
.menu-row > .pbtn { flex: 1; }

/* Снежинки на фоне меню (чистый CSS) */
.flake {
  position: absolute;
  top: -20px;
  left: var(--x);
  font-size: var(--s);
  color: #f7f9fb;
  opacity: .8;
  pointer-events: none;
  animation: flake-fall var(--d) linear var(--dl) infinite;
}
@keyframes flake-fall {
  0% { transform: translate(0, -5vh) rotate(0); }
  50% { transform: translate(18px, 55vh) rotate(180deg); }
  100% { transform: translate(-10px, 110vh) rotate(360deg); }
}

@media (max-height: 420px) {
  .dlg-panel { min-height: 60px; max-height: 55vh; }
  .menu-card { gap: 5px; padding: 8px 14px; }
  .menu-title { font-size: 32px; }
}
@media (prefers-reduced-motion: reduce) {
  .flake, .dlg-next { animation: none; }
}

/* Контекстная кнопка (например, «Выйти на берег»): по центру снизу, крупная, мягко пульсирует. */
.hud-extra {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-b) + clamp(64px, 11vh, 96px));
  transform: translateX(-50%);
  padding: 10px 22px;
  font-size: calc(var(--fs) * 1.1);
  font-weight: bold;
  white-space: nowrap;
  z-index: 2;
  animation: extra-pulse 1.6s ease-in-out infinite;
}
.hud-extra:active { transform: translateX(-50%) translateY(1px); }
@keyframes extra-pulse {
  0%, 100% { box-shadow: var(--shadow-paper), 0 0 0 0 rgba(233, 185, 73, .75); }
  50% { box-shadow: var(--shadow-paper), 0 0 0 12px rgba(233, 185, 73, 0); }
}
@media (max-height: 420px) and (orientation: landscape) {
  .hud-extra { bottom: calc(var(--safe-b) + 10px); }
}
