/* === Reset & Base === */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --app-height: 100dvh;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --text-color: #f2eee7;
  --text-shadow: 0 1px 3px rgba(0, 0, 0, 0.82);
  --box-bg: rgba(10, 9, 12, 0.82);
  --accent: #d9c1a1;
  --accent-glow: rgba(217, 193, 161, 0.28);
  --accent-cool: #9bb6cc;
  --panel-edge: rgba(255, 236, 211, 0.12);
}

html, body {
  width: 100%;
  height: var(--app-height);
  height: -webkit-fill-available;
  overflow: hidden;
  background: #090a0d;
  color: var(--text-color);
  font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic Medium", "Noto Sans JP", sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  overscroll-behavior: none;
}

/* === Game Container === */
#game-container {
  position: relative;
  width: 100%;
  height: var(--app-height);
  height: -webkit-fill-available;
  overflow: hidden;
}

/* === Screen States === */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.6s ease, visibility 0.6s ease;
}

.screen.active {
  opacity: 1;
  visibility: visible;
}

/* === Title Screen === */
#title-screen {
  background:
    radial-gradient(110% 85% at 50% 25%, rgba(116, 142, 168, 0.18) 0%, rgba(18, 22, 28, 0) 55%),
    linear-gradient(180deg, #06070a 0%, #101319 48%, #1a1513 78%, #100d0d 100%);
  z-index: 10;
  overflow: hidden;
}

/* Title particles canvas */
#title-particles {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}

.title-content {
  text-align: center;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  position: relative;
  z-index: 1;
}

.title-text {
  font-size: clamp(1.8rem, 6vw, 3rem);
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", serif;
  font-weight: 500;
  letter-spacing: 0.18em;
  text-shadow: 0 0 18px rgba(204, 224, 240, 0.13), 0 0 28px rgba(120, 98, 76, 0.15), var(--text-shadow);
  margin-bottom: 0.5rem;
  animation: titlePulse 3s ease-in-out infinite;
}

.title-subtitle {
  font-size: clamp(0.9rem, 3vw, 1.2rem);
  opacity: 0.78;
  letter-spacing: 0.26em;
  color: #c7d2dc;
  margin-bottom: 3rem;
}

@keyframes titlePulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.85; }
}

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

/* === Menu Buttons === */
.menu-btn {
  background: linear-gradient(180deg, rgba(255, 247, 236, 0.06) 0%, rgba(24, 20, 19, 0.58) 100%);
  border: 1px solid rgba(217, 193, 161, 0.24);
  color: var(--text-color);
  font-family: inherit;
  font-size: clamp(0.95rem, 2.5vw, 1.1rem);
  padding: 0.8em 2.5em;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, transform 0.1s, box-shadow 0.2s;
  min-width: 200px;
  letter-spacing: 0.14em;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.02), 0 8px 20px rgba(0, 0, 0, 0.18);
  touch-action: manipulation;
}

.menu-btn:active {
  background: linear-gradient(180deg, rgba(255, 245, 230, 0.12) 0%, rgba(40, 29, 23, 0.68) 100%);
  border-color: var(--accent);
  transform: scale(0.97);
}

@media (hover: hover) {
  .menu-btn:hover {
    background: linear-gradient(180deg, rgba(255, 245, 230, 0.10) 0%, rgba(37, 28, 24, 0.66) 100%);
    border-color: rgba(217, 193, 161, 0.45);
    box-shadow: inset 0 0 0 1px rgba(255, 250, 240, 0.03), 0 10px 24px rgba(0, 0, 0, 0.22);
  }
}

/* === Game Screen === */
#game-screen {
  z-index: 5;
  touch-action: manipulation;
}

/* Background Layer */
#background-layer {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, #1a1a3e 0%, #2a1a3e 100%);
  transition: background 1.2s ease, background-image 1.2s ease, filter 0.8s ease;
  filter: saturate(1.02) contrast(1.02);
  z-index: 1;
}

/* Character Layer */
#character-layer {
  position: absolute;
  inset: 0;
  display: block;
  padding-bottom: 25%;
  z-index: 2;
  pointer-events: none;
}

#character-layer .character {
  position: absolute;
  left: 50%;
  bottom: 0;
  max-height: 58%;
  width: auto;
  opacity: 0;
  --slot-x: -50%;
  --focus-scale: 1;
  transform: translate3d(var(--slot-x), 12px, 0) scale(var(--focus-scale));
  transform-origin: bottom center;
  transition: opacity 0.45s ease, transform 0.45s ease, filter 0.35s ease;
  filter: brightness(0.9) saturate(0.9) drop-shadow(0 12px 20px rgba(0, 0, 0, 0.35));
  z-index: 2;
}

#character-layer .character.visible {
  opacity: 0.95;
  transform: translate3d(var(--slot-x), 0, 0) scale(var(--focus-scale));
}

#character-layer .character.slot-left {
  --slot-x: -92%;
}

#character-layer .character.slot-right {
  --slot-x: -8%;
}

#character-layer .character.slot-center {
  --slot-x: -50%;
  max-height: 60%;
}

#character-layer .character.is-active {
  --focus-scale: 1.02;
  opacity: 1;
  filter: brightness(1.03) saturate(1.02) drop-shadow(0 18px 28px rgba(0, 0, 0, 0.42));
  z-index: 3;
}

#character-layer .character.is-idle {
  --focus-scale: 0.96;
  opacity: 0.58;
  filter: brightness(0.72) saturate(0.78) blur(0.15px) drop-shadow(0 8px 16px rgba(0, 0, 0, 0.28));
  z-index: 2;
}

/* Still CG Layer (full-screen image overlay, text UI remains above) */
#still-layer {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.45s ease;
  background: transparent;
}

#still-layer.active {
  opacity: 1;
}

#still-layer::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(4, 6, 10, 0.18) 0%, rgba(4, 6, 10, 0.06) 30%, rgba(4, 6, 10, 0.22) 100%);
  opacity: 0;
  transition: opacity 0.45s ease;
}

#still-layer.active::after {
  opacity: 1;
}

#still-layer .still-image {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transform: scale(1.02);
  filter: saturate(1.03) contrast(1.02);
  transition: opacity 0.45s ease, transform 0.9s ease;
}

#still-layer .still-image.visible {
  opacity: 1;
  transform: scale(1);
}

/* UI Layer */
#ui-layer {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  z-index: 4;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}

/* Text box top gradient fade */
#text-box-gradient {
  height: 40px;
  background: linear-gradient(to bottom, transparent, rgba(0, 0, 0, 0.55));
  pointer-events: none;
}

/* Text Box */
#text-box {
  background: var(--box-bg);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--panel-edge);
  box-shadow: 0 -18px 40px rgba(0, 0, 0, 0.28);
  padding: 1rem 1.2rem 1.5rem;
  min-height: 150px;
  position: relative;
  cursor: pointer;
  touch-action: manipulation;
}

#speaker-name {
  font-size: clamp(0.8rem, 2vw, 0.95rem);
  color: var(--accent);
  font-weight: 700;
  margin-bottom: 0.5rem;
  min-height: 1.2em;
  letter-spacing: 0.09em;
  text-shadow: 0 0 8px rgba(217, 193, 161, 0.14);
}

#message-text {
  font-size: clamp(0.95rem, 2.5vw, 1.05rem);
  line-height: 1.85;
  text-shadow: var(--text-shadow);
  min-height: 3.6em;
  white-space: pre-line;
}

#tap-indicator {
  position: absolute;
  bottom: 0.6rem;
  right: 1rem;
  font-size: 0.8rem;
  opacity: 0;
  transition: opacity 0.3s ease;
  color: var(--accent);
}

#tap-indicator.visible {
  opacity: 0.7;
  animation: tapBounce 1s ease-in-out infinite;
}

@keyframes tapBounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(4px); }
}

/* Choice Layer */
#choice-layer {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  padding:
    max(1.2rem, calc(1rem + var(--safe-top)))
    max(1.2rem, calc(1rem + var(--safe-right)))
    max(1.2rem, calc(1rem + var(--safe-bottom)))
    max(1.2rem, calc(1rem + var(--safe-left)));
  background: rgba(0, 0, 0, 0);
  z-index: 6;
  pointer-events: none;
  transition: background 0.4s ease;
}

#choice-layer.active {
  background: radial-gradient(circle at 50% 40%, rgba(20, 18, 22, 0.45) 0%, rgba(8, 8, 10, 0.76) 100%);
  pointer-events: auto;
}

.choice-btn {
  background: linear-gradient(180deg, rgba(31, 28, 31, 0.92) 0%, rgba(16, 14, 16, 0.96) 100%);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(217, 193, 161, 0.26);
  color: var(--text-color);
  font-family: inherit;
  font-size: clamp(0.95rem, 2.5vw, 1.1rem);
  padding: 1em 2em;
  border-radius: 10px;
  cursor: pointer;
  width: 100%;
  max-width: 400px;
  text-align: center;
  transition: background 0.2s, border-color 0.2s, transform 0.15s, box-shadow 0.2s;
  letter-spacing: 0.07em;
  opacity: 0;
  transform: translateY(15px);
  animation: choiceEnter 0.4s ease forwards;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.18);
  touch-action: manipulation;
  min-height: 48px;
}

.choice-btn:nth-child(1) { animation-delay: 0.1s; }
.choice-btn:nth-child(2) { animation-delay: 0.25s; }
.choice-btn:nth-child(3) { animation-delay: 0.4s; }
.choice-btn:nth-child(4) { animation-delay: 0.55s; }

@keyframes choiceEnter {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.choice-btn:active {
  background: linear-gradient(180deg, rgba(55, 41, 30, 0.94) 0%, rgba(24, 18, 15, 0.96) 100%);
  border-color: var(--accent);
  transform: scale(0.97);
  box-shadow: 0 0 15px var(--accent-glow);
}

@media (hover: hover) {
  .choice-btn:hover {
    background: linear-gradient(180deg, rgba(44, 36, 32, 0.94) 0%, rgba(20, 18, 18, 0.96) 100%);
    border-color: rgba(217, 193, 161, 0.52);
    box-shadow: 0 0 10px var(--accent-glow);
  }
}

/* Menu Button */
#menu-button {
  position: absolute;
  top: calc(var(--safe-top) + 0.5rem);
  right: calc(var(--safe-right) + 0.5rem);
  z-index: 5;
  font-size: 1.5rem;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(12, 11, 14, 0.58);
  color: var(--text-color);
  border: 1px solid rgba(217, 193, 161, 0.18);
  border-radius: 50%;
  cursor: pointer;
  opacity: 0.65;
  transition: opacity 0.3s, border-color 0.2s, background 0.2s;
  padding: 0;
  line-height: 1;
  appearance: none;
  -webkit-appearance: none;
  touch-action: manipulation;
}

#menu-button:active {
  opacity: 1;
}

@media (hover: hover) {
  #menu-button:hover {
    opacity: 0.95;
    border-color: rgba(217, 193, 161, 0.32);
    background: rgba(18, 15, 17, 0.72);
  }
}

.menu-btn:focus-visible,
.choice-btn:focus-visible,
#menu-button:focus-visible,
.save-slot:focus-visible {
  outline: 2px solid rgba(217, 193, 161, 0.78);
  outline-offset: 2px;
}

/* === Ending Screen === */
#ending-screen {
  z-index: 10;
  text-align: center;
  padding:
    max(1.2rem, calc(1.2rem + var(--safe-top)))
    max(1rem, calc(1rem + var(--safe-right)))
    max(1.2rem, calc(1.2rem + var(--safe-bottom)))
    max(1rem, calc(1rem + var(--safe-left)));
}

/* Ending variants */
#ending-screen.ending-true {
  background:
    radial-gradient(90% 80% at 50% 18%, rgba(249, 227, 194, 0.18) 0%, rgba(0, 0, 0, 0) 60%),
    linear-gradient(135deg, #141822 0%, #263653 35%, #5b4f67 70%, #b8837a 100%);
}

#ending-screen.ending-bad {
  background:
    radial-gradient(95% 80% at 50% 20%, rgba(126, 84, 112, 0.15) 0%, rgba(0, 0, 0, 0) 55%),
    linear-gradient(135deg, #05060b 0%, #0a0d1a 38%, #180f19 74%, #12090d 100%);
}

#ending-screen.ending-normal {
  background:
    radial-gradient(88% 72% at 50% 22%, rgba(170, 192, 214, 0.14) 0%, rgba(0, 0, 0, 0) 60%),
    linear-gradient(135deg, #0a1118 0%, #152231 40%, #223445 72%, #3b4853 100%);
}

#ending-screen.ending-default {
  background:
    radial-gradient(88% 72% at 50% 22%, rgba(170, 192, 214, 0.14) 0%, rgba(0, 0, 0, 0) 60%),
    linear-gradient(135deg, #0a1118 0%, #152231 40%, #223445 72%, #3b4853 100%);
}

.ending-content {
  opacity: 0;
  transform: translateY(20px);
  animation: fadeInUp 1.2s ease 0.3s forwards;
}

#ending-title {
  font-size: clamp(1.5rem, 5vw, 2.5rem);
  font-weight: 300;
  letter-spacing: 0.1em;
  margin-bottom: 1.5rem;
  text-shadow: 0 0 20px var(--accent-glow);
}

#ending-screen.ending-true #ending-title {
  color: #f7ead8;
  text-shadow: 0 0 18px rgba(240, 210, 160, 0.18), 0 0 30px rgba(255, 184, 140, 0.12);
}

#ending-screen.ending-normal #ending-title,
#ending-screen.ending-default #ending-title {
  color: #dbe8f4;
  text-shadow: 0 0 18px rgba(143, 181, 217, 0.14);
}

#ending-screen.ending-bad #ending-title {
  color: #e8dce7;
  text-shadow: 0 0 20px rgba(170, 110, 160, 0.14);
}

#ending-text {
  font-size: clamp(0.9rem, 2.5vw, 1.05rem);
  line-height: 2;
  opacity: 0.8;
  margin-bottom: 3rem;
  max-width: 500px;
  white-space: pre-line;
}

#ending-screen.ending-true #ending-text {
  color: rgba(248, 239, 229, 0.88);
}

#ending-screen.ending-normal #ending-text,
#ending-screen.ending-default #ending-text {
  color: rgba(224, 237, 248, 0.82);
}

#ending-screen.ending-bad #ending-text {
  color: rgba(228, 216, 230, 0.78);
}

@keyframes fadeInUp {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* === Overlay === */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0);
  z-index: 20;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  pointer-events: none;
  opacity: 0;
  transition: background 0.3s ease, opacity 0.3s ease;
}

.overlay.active {
  background: rgba(0, 0, 0, 0.7);
  pointer-events: auto;
  opacity: 1;
}

.overlay-panel {
  background: rgba(15, 15, 35, 0.95);
  backdrop-filter: blur(15px);
  -webkit-backdrop-filter: blur(15px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  padding: 2rem;
  width: 90%;
  max-width: 380px;
  max-height: 85vh;
  max-height: 85dvh;
  overflow-y: auto;
  text-align: center;
  transform: scale(0.95);
  transition: transform 0.3s ease;
}

.overlay.active .overlay-panel {
  transform: scale(1);
}

.overlay-panel h2 {
  font-size: 1.2rem;
  font-weight: 400;
  letter-spacing: 0.15em;
  margin-bottom: 1.5rem;
}

.overlay-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  margin-bottom: 1.5rem;
}

.overlay-buttons .menu-btn {
  min-width: unset;
  width: 100%;
}

/* Save Slots */
#save-slots {
  display: none;
  flex-direction: column;
  gap: 0.6rem;
}

#save-slots.active {
  display: flex;
}

.save-slot {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 0.8rem 1rem;
  cursor: pointer;
  text-align: left;
  color: var(--text-color);
  font-family: inherit;
  font-size: 0.85rem;
  transition: background 0.2s, border-color 0.2s;
  touch-action: manipulation;
  min-height: 52px;
}

.save-slot:active {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.25);
}

@media (hover: hover) {
  .save-slot:hover {
    background: rgba(255, 255, 255, 0.08);
    border-color: rgba(255, 255, 255, 0.2);
  }
}

.save-slot .slot-label {
  font-weight: 600;
  margin-bottom: 0.2rem;
}

.save-slot .slot-info {
  opacity: 0.6;
  font-size: 0.8rem;
  line-height: 1.35;
}

.save-slot.empty {
  opacity: 0.5;
}

/* Save toast */
.save-toast {
  position: fixed;
  top: calc(var(--safe-top) + 1rem);
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background: rgba(110, 198, 255, 0.15);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(110, 198, 255, 0.3);
  border-radius: 8px;
  padding: 0.6rem 1.5rem;
  color: var(--accent);
  font-size: 0.85rem;
  letter-spacing: 0.05em;
  z-index: 100;
  opacity: 0;
  animation: toastIn 0.3s ease forwards, toastOut 0.3s ease 1.2s forwards;
  pointer-events: none;
}

@keyframes toastIn {
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

@keyframes toastOut {
  to {
    opacity: 0;
    transform: translateX(-50%) translateY(-10px);
  }
}

/* === Landscape hint === */
@media (orientation: landscape) and (max-height: 500px) {
  #text-box {
    min-height: 100px;
    padding: 0.6rem 1rem 1rem;
  }

  #text-box-gradient {
    height: 20px;
  }

  #message-text {
    font-size: 0.85rem;
    line-height: 1.6;
    min-height: 2.4em;
  }

  #character-layer {
    padding-bottom: 15%;
  }

  #character-layer .character {
    max-height: 72%;
  }
}

@media (pointer: coarse) {
  .menu-btn {
    min-height: 48px;
    font-size: max(16px, 1rem);
    padding: 0.9em 1.4em;
  }

  #text-box {
    min-height: 166px;
    padding: 1rem 1rem 1.65rem;
  }

  #speaker-name {
    font-size: 0.9rem;
    margin-bottom: 0.45rem;
  }

  #message-text {
    font-size: 1rem;
    line-height: 1.9;
    min-height: 4em;
    letter-spacing: 0.01em;
    overflow-wrap: anywhere;
  }

  #tap-indicator {
    font-size: 0.95rem;
    right: 0.85rem;
    bottom: 0.5rem;
  }

  #choice-layer {
    gap: 0.8rem;
  }

  .choice-btn {
    min-height: 52px;
    font-size: 1rem;
    line-height: 1.45;
    padding: 0.95em 1.1em;
    max-width: min(92vw, 420px);
  }

  #menu-button {
    width: 50px;
    height: 50px;
    opacity: 0.78;
  }

  .save-slot {
    min-height: 56px;
    padding: 0.95rem 1rem;
  }

  .save-slot .slot-label {
    font-size: 0.92rem;
  }

  .save-slot .slot-info {
    font-size: 0.84rem;
  }

  #ending-text {
    font-size: 1rem;
    line-height: 1.9;
  }
}

@media (max-width: 430px) {
  .title-content {
    width: 100%;
    padding-left: max(0.9rem, calc(0.9rem + var(--safe-left)));
    padding-right: max(0.9rem, calc(0.9rem + var(--safe-right)));
  }

  .title-subtitle {
    letter-spacing: 0.18em;
    margin-bottom: 2.2rem;
  }

  #text-box {
    padding-inline: 0.95rem;
  }

  #message-text {
    font-size: 1rem;
    line-height: 1.85;
  }

  #choice-layer {
    gap: 0.75rem;
  }

  .choice-btn {
    padding-inline: 1rem;
  }

  .overlay-panel {
    width: 94%;
    padding: 1.25rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .title-text,
  #tap-indicator.visible,
  .ending-content,
  .choice-btn {
    animation: none !important;
  }

  .screen,
  #background-layer,
  #still-layer,
  #still-layer::after,
  #still-layer .still-image,
  #character-layer .character,
  .overlay,
  .overlay-panel {
    transition-duration: 0.01s !important;
  }
}
