@font-face {
  font-family: 'Nunito';
  src: url('../fonts/Nunito-Regular.woff2') format('woff2');
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: 'Nunito';
  src: url('../fonts/Nunito-Bold.woff2') format('woff2');
  font-weight: 700;
  font-display: swap;
}
@font-face {
  font-family: 'Nunito';
  src: url('../fonts/Nunito-ExtraBold.woff2') format('woff2');
  font-weight: 800;
  font-display: swap;
}

:root {
  --bg: #4a4547;
  --ink: #3a2546;
  --accent: #7a3cc2;
  --accent-dark: #5f2b9e;
  --sx: 0px; --sy: 0px; --sw: 100vw; --sh: 56.25vw;
  --dx: 0px; --dy: 0px; --dw: 100vw; --dh: 56.25vw;
  --fs: clamp(16px, calc(var(--sw) * 0.019), 26px);
  --fs-big: clamp(17px, calc(var(--sw) * 0.024), 32px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
}
body {
  position: fixed;
  inset: 0;
  height: 100vh;
  height: 100dvh;
  font-family: 'Nunito', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-weight: 700;
  color: var(--ink);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
img, canvas { -webkit-user-drag: none; -webkit-touch-callout: none; }

#stage {
  position: absolute;
  left: var(--sx);
  top: var(--sy);
  width: var(--sw);
  height: var(--sh);
}
#cv {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}
#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

/* Реплики-«облака» */
.bubble {
  position: absolute;
  display: none;
  background: #fff;
  color: var(--ink);
  border: 3px solid #d9b8ee;
  border-radius: 22px;
  padding: 0.55em 0.95em;
  font-size: var(--fs);
  line-height: 1.3;
  text-align: center;
  box-shadow: 0 6px 22px rgba(90, 40, 120, 0.18);
  max-width: min(46%, 560px);
}
.bubble.show { display: block; animation: pop 0.25s ease-out; }
.bubble::after {
  content: '';
  position: absolute;
  width: 18px;
  height: 18px;
  background: #fff;
  border: 3px solid #d9b8ee;
  border-top: 0;
  border-left: 0;
}
.bubble.big { font-size: var(--fs-big); font-weight: 800; padding: 0.7em 1.1em; }

#bubbleStart { left: 41%; top: 13%; max-width: 46%; }
#bubbleStart::after { left: -11px; bottom: 30%; transform: rotate(135deg); }

#say { transform: translate(-50%, -100%); white-space: normal; width: max-content; max-width: min(70%, 420px); }
#say::after { left: calc(50% - 9px); bottom: -11px; transform: rotate(45deg); }
#say[data-pos='top']::after { display: none; }

#bubbleEnd { left: 50%; top: 5%; transform: translateX(-50%); width: max-content; max-width: 80%; }
#bubbleEnd.show { animation: popc 0.35s ease-out; }
#bubbleEnd::after { left: 26%; bottom: -11px; transform: rotate(45deg); }

@keyframes pop { from { opacity: 0; scale: 0.85; } to { opacity: 1; scale: 1; } }
@keyframes popc { from { opacity: 0; transform: translateX(-50%) scale(0.85); } to { opacity: 1; transform: translateX(-50%) scale(1); } }

/* Кнопки */
.btn {
  display: none;
  font: inherit;
  font-weight: 800;
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
  touch-action: manipulation;
  pointer-events: auto;
}
.btn.show { display: inline-flex; align-items: center; justify-content: center; gap: 0.35em; }
.btn-main {
  background: var(--accent);
  color: #fff;
  font-size: var(--fs-big);
  padding: 0.55em 1.6em;
  box-shadow: 0 6px 0 var(--accent-dark), 0 10px 24px rgba(90, 40, 140, 0.3);
}
.btn-main:hover { background: #8646d0; }
.btn-main:active { transform: translateY(3px); box-shadow: 0 3px 0 var(--accent-dark); }
.btn-back {
  background: rgba(255, 255, 255, 0.92);
  color: var(--accent-dark);
  font-size: var(--fs);
  padding: 0.35em 1em;
  border: 3px solid #d9b8ee;
  box-shadow: 0 4px 14px rgba(90, 40, 120, 0.18);
}
.btn-back:hover { background: #fff; }
.btn:focus-visible { outline: 3px solid #ffb3d1; outline-offset: 3px; }

#dock {
  position: fixed;
  left: var(--dx);
  top: var(--dy);
  width: var(--dw);
  height: var(--dh);
  pointer-events: none;
}
body:not([data-layout='tall']) #btnBack { position: absolute; left: 12px; top: 12px; }
body:not([data-layout='tall']) .btn-main { position: absolute; left: 50%; bottom: 5%; transform: translateX(-50%); }
body:not([data-layout='tall']) .btn-main:active { transform: translate(-50%, 3px); }

/* iPad вертикально: сцена сверху, крупные кнопки снизу */
body[data-layout='tall'] #dock {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 16px;
}
body[data-layout='tall'] .btn-main { font-size: 28px; padding: 0.6em 2em; }
body[data-layout='tall'] .btn-back { font-size: 24px; padding: 0.55em 1.8em; min-height: 64px; }

/* Панель кодового замка */
#codePanel {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(40, 18, 60, 0.45);
  z-index: 20;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#codePanel.show { display: flex; }
.panel {
  --key: clamp(48px, 9.5vh, 76px);
  --key: clamp(48px, 9.5dvh, 76px);
  background: linear-gradient(#fffaff, #f3e7fa);
  border: 4px solid #e8b94f;
  border-radius: 26px;
  padding: 16px;
  display: grid;
  gap: 14px;
  box-shadow: 0 18px 50px rgba(40, 10, 60, 0.4);
  animation: pop 0.2s ease-out;
}
.panel-title { text-align: center; font-size: 18px; font-weight: 800; color: var(--accent-dark); margin-bottom: 8px; }
.code-display { display: flex; gap: 6px; justify-content: center; }
.code-display span {
  width: calc(var(--key) * 0.62);
  height: calc(var(--key) * 0.82);
  min-width: 34px;
  border-radius: 10px;
  background: #5a3d10;
  color: #fff4d6;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: max(22px, calc(var(--key) * 0.45));
  font-weight: 800;
  box-shadow: inset 0 3px 0 rgba(0, 0, 0, 0.3);
}
.code-display span.filled { background: #7a5418; }
.keys { display: grid; grid-template-columns: repeat(3, auto); gap: 8px; justify-content: center; }
.keys button {
  width: calc(var(--key) * 1.45);
  height: var(--key);
  min-width: 82px;
  min-height: 48px;
  border: 0;
  border-radius: 14px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 800;
  font-size: max(22px, calc(var(--key) * 0.4));
  box-shadow: 0 4px 0 #d9c3e6;
  cursor: pointer;
  touch-action: manipulation;
}
.keys button:hover { background: #fbf3ff; }
.keys button:active { transform: translateY(2px); box-shadow: 0 2px 0 #d9c3e6; }
.keys .key-text { font-size: 16px; color: var(--accent-dark); padding: 0 4px; }
.panel.shake { animation: shake 0.5s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 50%, 90% { transform: translateX(-12px) rotate(-1deg); }
  30%, 70% { transform: translateX(12px) rotate(1deg); }
}
@media (max-height: 520px) {
  .panel { grid-template-columns: auto auto; align-items: center; }
  .code-display span { width: 36px; min-width: 36px; height: 50px; font-size: 24px; }
}

/* Экран «Поверните телефон» */
#rotate {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: linear-gradient(#d9c6f2, #f6e1ef);
  z-index: 50;
  text-align: center;
  padding: 24px;
}
body[data-layout='rotate'] #rotate { display: flex; }
#rotate p { font-size: 26px; font-weight: 800; margin: 16px 0 0; }
#rotate small { font-size: 16px; font-weight: 700; color: #5a4466; }
.phone-icon {
  width: 64px;
  height: 108px;
  border: 6px solid var(--accent);
  border-radius: 14px;
  animation: turn 2.2s ease-in-out infinite;
}
@keyframes turn { 0%, 30% { transform: rotate(0); } 60%, 100% { transform: rotate(-90deg); } }

/* Индикатор загрузки */
#loader {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  gap: 14px;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  z-index: 60;
  color: #fff;
  background: var(--bg);
}
#loader.show { display: flex; }
.spinner {
  width: 46px;
  height: 46px;
  border: 5px solid #d9b8ee;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .phone-icon, .bubble.show { animation: none; }
}
