*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --gold: #c9a84c;
  --gold-light: #e8d5a3;
  --burgundy: #5c1a2e;
  --dark: #0d0a12;
  --text: #f5efe6;
  --box-bg: rgba(13, 10, 18, 0.9);
  --box-border: rgba(201, 168, 76, 0.45);
  --ui-height: 18dvh;
  --ui-padding: clamp(0.5rem, 2vw, 1.25rem);
  --sprite-boost: 1;
  --sprite-sink: clamp(16px, 3dvh, 40px);
  --sprite-h: 78%;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  position: fixed;
  inset: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  background: var(--dark);
  font-family: 'Nunito', sans-serif;
  color: var(--text);
}

body {
  position: fixed;
  inset: 0;
}

/* Полный экран — без чёрных полей */
#game {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  width: 100dvw;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  background: var(--dark);
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -webkit-touch-callout: none;
  cursor: default;
}

#game.is-playing {
  cursor: pointer;
}

#game img {
  -webkit-user-drag: none;
  user-drag: none;
}

#background {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  transition: opacity 0.8s ease;
}

#background.fade {
  opacity: 0;
}

/* Портрет — чуть выше фокус фона */
@media (orientation: portrait) {
  #background {
    background-position: center 38%;
  }
}

/* Ландшафт — центр */
@media (orientation: landscape) {
  #background {
    background-position: center 42%;
  }
}

#sprites {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: calc(var(--ui-height) - var(--sprite-sink));
  pointer-events: none;
  overflow: visible;
  z-index: 2;
}

.sprite-slot {
  position: absolute;
  bottom: 0;
  height: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  overflow: visible;
  pointer-events: none;
}

/* Три персонажа */
.sprites-trio .sprite-slot-left,
#sprites[data-layout="sprites-trio"] .sprite-slot-left { left: 0; width: 33.33%; }
.sprites-trio .sprite-slot-center,
#sprites[data-layout="sprites-trio"] .sprite-slot-center { left: 33.33%; width: 33.34%; }
.sprites-trio .sprite-slot-right,
#sprites[data-layout="sprites-trio"] .sprite-slot-right { right: 0; width: 33.33%; }

/* Два: слева + справа */
.sprites-duo-lr .sprite-slot-left,
#sprites[data-layout="sprites-duo-lr"] .sprite-slot-left { left: 0; width: 50%; }
.sprites-duo-lr .sprite-slot-right,
#sprites[data-layout="sprites-duo-lr"] .sprite-slot-right { right: 0; width: 50%; }

/* Два: слева + центр */
.sprites-duo-lc .sprite-slot-left,
#sprites[data-layout="sprites-duo-lc"] .sprite-slot-left { left: 0; width: 42%; }
.sprites-duo-lc .sprite-slot-center,
#sprites[data-layout="sprites-duo-lc"] .sprite-slot-center { left: 38%; width: 58%; }

/* Два: центр + справа */
.sprites-duo-cr .sprite-slot-center,
#sprites[data-layout="sprites-duo-cr"] .sprite-slot-center { left: 4%; width: 58%; }
.sprites-duo-cr .sprite-slot-right,
#sprites[data-layout="sprites-duo-cr"] .sprite-slot-right { right: 0; width: 42%; }

/* Один в центре */
.sprites-solo-center .sprite-slot-center,
#sprites[data-layout="sprites-solo-center"] .sprite-slot-center { left: 12%; width: 76%; }

.sprites-solo-center,
#sprites[data-layout="sprites-solo-center"] {
  --sprite-h: 82%;
}

/* Один слева / справа */
.sprites-solo-left .sprite-slot-left,
#sprites[data-layout="sprites-solo-left"] .sprite-slot-left { left: 4%; width: 58%; }
.sprites-solo-right .sprite-slot-right,
#sprites[data-layout="sprites-solo-right"] .sprite-slot-right { right: 4%; width: 58%; }

.sprite {
  width: auto;
  height: var(--sprite-h);
  max-height: var(--sprite-h);
  max-width: none;
  object-fit: contain;
  object-position: bottom center;
  transform-origin: bottom center;
  transform: scale(var(--sprite-boost, 1));
  transition: opacity 0.45s ease;
  filter: drop-shadow(0 8px 24px rgba(0, 0, 0, 0.5));
  user-select: none;
  -webkit-user-drag: none;
  flex-shrink: 0;
}

#sprite-left.hidden,
#sprite-center.hidden,
#sprite-right.hidden {
  transform: scale(var(--sprite-boost, 1)) translateY(3%);
}

#overlay {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  background:
    linear-gradient(to top, rgba(5, 4, 8, 0.72) 0%, rgba(5, 4, 8, 0.15) 22%, transparent 45%),
    radial-gradient(ellipse 120% 90% at 50% 40%, transparent 25%, rgba(0, 0, 0, 0.2) 100%);
}

.hidden {
  display: none !important;
}

#title-screen,
#pause-screen,
#ending-screen {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: max(1rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right)) max(1rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
  background: linear-gradient(160deg, rgba(13, 10, 18, 0.96) 0%, rgba(92, 26, 46, 0.82) 100%);
}

#confirm-screen {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(1rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right)) max(1rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
  background: rgba(5, 4, 8, 0.72);
}

.confirm-box {
  width: min(420px, 92vw);
  padding: clamp(1.25rem, 4vw, 2rem);
  border: 1px solid rgba(201, 168, 76, 0.45);
  border-radius: 6px;
  background: linear-gradient(160deg, rgba(13, 10, 18, 0.98) 0%, rgba(40, 18, 32, 0.96) 100%);
  text-align: center;
}

.confirm-text {
  margin-bottom: clamp(1rem, 3dvh, 1.5rem);
  line-height: 1.6;
  font-size: clamp(0.9rem, 2.8vw, 1.05rem);
}

.confirm-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.65rem;
}

#lore-screen {
  position: absolute;
  inset: 0;
  z-index: 21;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: max(1rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right)) max(1rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
  background: linear-gradient(160deg, rgba(13, 10, 18, 0.97) 0%, rgba(40, 18, 32, 0.94) 100%);
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}

.screen-title {
  font-family: 'Cormorant Garamond', serif;
  font-size: clamp(1.6rem, 5.5vw, 2.5rem);
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--gold-light);
  margin-bottom: clamp(1rem, 3dvh, 2rem);
}

.lore-header {
  width: 100%;
  max-width: min(520px, 92vw);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
  flex-shrink: 0;
}

.lore-header .screen-title {
  margin-bottom: 0;
}

.btn-ghost {
  align-self: flex-start;
  padding: 0.45rem 0.85rem;
  font-size: clamp(0.75rem, 2.4vw, 0.88rem);
  background: transparent;
  border-color: rgba(201, 168, 76, 0.45);
  opacity: 0.9;
}

.lore-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(10.5rem, 1fr));
  gap: 1rem;
  width: 100%;
  max-width: min(680px, 94vw);
  padding-bottom: 1rem;
}

.lore-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  min-height: 11.5rem;
  padding: 1rem 0.75rem;
  border: 1px solid rgba(201, 168, 76, 0.32);
  background: rgba(0, 0, 0, 0.28);
  border-radius: 6px;
  cursor: pointer;
  font-family: 'Nunito', sans-serif;
  font-size: clamp(0.85rem, 2.8vw, 1rem);
  font-weight: 600;
  color: var(--gold-light);
  transition: background 0.2s, border-color 0.2s;
  touch-action: manipulation;
}

.lore-card:hover {
  background: rgba(201, 168, 76, 0.14);
  border-color: rgba(201, 168, 76, 0.55);
}

.lore-card img {
  height: clamp(6.5rem, 26vw, 9rem);
  width: auto;
  max-width: 100%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

.lore-empty {
  max-width: min(400px, 88vw);
  text-align: center;
  font-style: italic;
  opacity: 0.75;
  line-height: 1.55;
  font-size: clamp(0.82rem, 2.6vw, 0.95rem);
}

#lore-detail-view {
  width: 100%;
  max-width: min(420px, 92vw);
  text-align: center;
  padding-bottom: 1.5rem;
}

.lore-portrait {
  height: clamp(10rem, 38vw, 14rem);
  width: auto;
  max-width: 100%;
  object-fit: contain;
  margin-bottom: 1rem;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

.lore-name {
  font-family: 'Cormorant Garamond', serif;
  font-size: clamp(1.4rem, 4.5vw, 2rem);
  color: var(--gold-light);
  margin-bottom: 0.35rem;
}

.lore-role {
  font-size: clamp(0.78rem, 2.4vw, 0.92rem);
  color: var(--gold);
  font-style: italic;
  margin-bottom: 1rem;
}

.lore-bio {
  font-size: clamp(0.85rem, 2.6vw, 1rem);
  line-height: 1.65;
  opacity: 0.9;
  text-align: left;
}

#title-screen h1,
#ending-screen h2 {
  font-family: 'Cormorant Garamond', serif;
  font-size: clamp(1.45rem, 5.5vw, 3rem);
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--gold-light);
  text-shadow: 0 0 40px rgba(201, 168, 76, 0.35);
  line-height: 1.15;
  max-width: min(92vw, 28rem);
}

.subtitle {
  display: none;
}

.tagline {
  max-width: min(520px, 92vw);
  margin: clamp(0.75rem, 2dvh, 1.5rem) 0 clamp(1rem, 3dvh, 2.5rem);
  line-height: 1.6;
  opacity: 0.88;
  font-size: clamp(0.82rem, 2.8vw, 1rem);
}

.btn {
  font-family: 'Nunito', sans-serif;
  font-size: clamp(0.85rem, 2.8vw, 1rem);
  font-weight: 600;
  padding: 0.8rem clamp(1.5rem, 5vw, 2.5rem);
  border: 1px solid var(--gold);
  background: rgba(201, 168, 76, 0.12);
  color: var(--gold-light);
  cursor: pointer;
  border-radius: 3px;
  transition: background 0.2s, transform 0.15s;
  letter-spacing: 0.05em;
  touch-action: manipulation;
}

.btn:hover {
  background: rgba(201, 168, 76, 0.28);
  transform: translateY(-2px);
}

.btn-primary {
  background: rgba(201, 168, 76, 0.22);
  border-color: var(--gold-light);
}

.title-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.65rem;
}

.save-hint {
  max-width: min(480px, 90vw);
  margin-top: 0.85rem;
  font-size: clamp(0.78rem, 2.4vw, 0.92rem);
  line-height: 1.5;
  font-style: italic;
  color: rgba(232, 213, 163, 0.72);
}

.save-toast {
  position: absolute;
  top: max(0.85rem, env(safe-area-inset-top));
  left: max(0.85rem, env(safe-area-inset-left));
  padding: 0;
  border: none;
  background: none;
  color: rgba(232, 213, 163, 0.42);
  font-size: clamp(0.65rem, 1.8vw, 0.75rem);
  letter-spacing: 0.04em;
  pointer-events: none;
  z-index: 14;
  animation: save-fade 1.6s ease forwards;
}

@keyframes save-fade {
  0%, 20% { opacity: 0; }
  30%, 65% { opacity: 1; }
  100% { opacity: 0; }
}

.game-menu-btn {
  position: absolute;
  top: max(0.75rem, env(safe-area-inset-top));
  right: max(0.75rem, env(safe-area-inset-right));
  z-index: 15;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 50%;
  border: 1px solid rgba(201, 168, 76, 0.45);
  background: rgba(13, 10, 18, 0.72);
  color: var(--gold-light);
  padding: 0;
  cursor: pointer;
  pointer-events: auto;
  touch-action: manipulation;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.menu-bars {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  width: 0.95rem;
  height: 0.95rem;
}

.menu-bars__line {
  display: block;
  height: 2px;
  width: 100%;
  background: currentColor;
  border-radius: 1px;
}

.game-menu-btn:hover {
  background: rgba(92, 26, 46, 0.65);
  border-color: var(--gold);
}

#ui {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  min-width: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: var(--ui-padding);
  padding-bottom: max(var(--ui-padding), env(safe-area-inset-bottom));
  pointer-events: none;
  box-sizing: border-box;
}

#ui > * {
  pointer-events: auto;
}

#ui.hidden {
  display: none;
}

#dialogue-box {
  background: var(--box-bg);
  border: 1px solid var(--box-border);
  border-radius: 6px;
  --dialogue-pad-y: clamp(0.85rem, 2vw, 1.35rem);
  --dialogue-pad-x: clamp(1rem, 2.8vw, 1.5rem);
  --dialogue-text-size: clamp(1.02rem, 3.2vw, 1.22rem);
  --dialogue-max-h: 30dvh;
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
  padding: var(--dialogue-pad-y) var(--dialogue-pad-x);
  min-height: min(
    calc(
      var(--dialogue-pad-y) * 2 + 1.5rem + 0.3rem + 0.45rem + 1.1em
      + 3 * 1.65 * var(--dialogue-text-size)
    ),
    var(--dialogue-max-h)
  );
  max-height: var(--dialogue-max-h);
  overflow: hidden;
  cursor: pointer;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 -4px 28px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
}

#speaker {
  font-family: 'Cormorant Garamond', serif;
  font-size: clamp(1.2rem, 4vw, 1.6rem);
  font-weight: 600;
  color: var(--gold);
  margin-bottom: 0.3rem;
  flex-shrink: 0;
}

#text {
  font-size: var(--dialogue-text-size);
  line-height: 1.65;
  min-height: calc(1.65em * 3);
  min-width: 0;
  flex: 1 1 0;
  overflow-x: hidden;
  overflow-y: auto;
  overflow-wrap: break-word;
  word-break: break-word;
  hyphens: auto;
  -webkit-hyphens: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

#continue-hint {
  text-align: right;
  font-size: clamp(0.72rem, 2.3vw, 0.88rem);
  line-height: 1.35;
  color: rgba(245, 239, 230, 0.4);
  margin-top: 0.45rem;
  flex-shrink: 0;
  overflow-wrap: anywhere;
  animation: pulse 1.5s ease infinite;
}

#continue-hint .hint-short {
  display: none;
}

@keyframes pulse {
  0%, 100% { opacity: 0.35; }
  50% { opacity: 0.85; }
}

#choices {
  display: flex;
  flex-direction: column;
  gap: clamp(0.35rem, 1dvh, 0.6rem);
  margin-bottom: clamp(0.35rem, 1dvh, 0.75rem);
  max-height: 32dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  scrollbar-width: thin;
}

#choices.hidden {
  display: none;
}

.choice-btn {
  font-family: 'Nunito', sans-serif;
  font-size: clamp(0.95rem, 3vw, 1.1rem);
  line-height: 1.4;
  padding: clamp(0.65rem, 1.5dvh, 0.9rem) clamp(0.8rem, 2.5vw, 1.1rem);
  border: 1px solid rgba(201, 168, 76, 0.5);
  background: rgba(13, 10, 18, 0.94);
  color: var(--text);
  cursor: pointer;
  border-radius: 4px;
  text-align: left;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  touch-action: manipulation;
}

.choice-btn:hover {
  background: rgba(92, 26, 46, 0.65);
  border-color: var(--gold);
}

#ending-screen {
  z-index: 30;
}

#ending-text {
  max-width: min(560px, 92vw);
  margin: clamp(0.75rem, 2dvh, 1.5rem) 0 clamp(1rem, 3dvh, 2rem);
  line-height: 1.65;
  font-size: clamp(1.02rem, 3.2vw, 1.2rem);
  opacity: 0.92;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  max-height: 50dvh;
}

/* Портрет */
@media (orientation: portrait) {
  :root {
    --ui-height: 20dvh;
  }

  #dialogue-box {
    --dialogue-max-h: 36dvh;
  }
}

/* Узкий телефон */
@media (orientation: portrait) and (max-width: 480px) {
  #dialogue-box {
    --dialogue-pad-y: 0.75rem;
    --dialogue-pad-x: 0.85rem;
    --dialogue-text-size: 0.95rem;
    --dialogue-max-h: 38dvh;
  }

  #speaker {
    font-size: 1.15rem;
    margin-bottom: 0.25rem;
  }

  #text {
    line-height: 1.55;
    min-height: calc(1.55em * 3);
  }

  #continue-hint {
    font-size: 0.68rem;
    margin-top: 0.35rem;
  }
}

@media (orientation: portrait) and (max-width: 360px) {
  #continue-hint .hint-full {
    display: none;
  }

  #continue-hint .hint-short {
    display: inline;
  }
}

/* Ландшафт */
@media (orientation: landscape) {
  :root {
    --ui-height: 24dvh;
  }
}

/* Низкий ландшафт */
@media (orientation: landscape) and (max-height: 500px) {
  :root {
    --ui-height: 28dvh;
  }

  #dialogue-box {
    min-height: 76px;
    max-height: 30dvh;
  }
}

@media (hover: none) and (pointer: coarse) {
  .choice-btn,
  .btn {
    min-height: 44px;
  }
}
