:root {
  --ink: #f8eadf;
  --muted: #d6c1ad;
  --panel: rgba(25, 15, 17, 0.82);
  --panel-strong: rgba(13, 8, 10, 0.92);
  --line: rgba(255, 232, 205, 0.22);
  --gold: #d8b36a;
  --red: #a83a4c;
  --cold: rgba(67, 78, 95, 0.42);
  --vh: 1vh;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: #0d090a;
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC", sans-serif;
  overflow: hidden;
}

button, textarea { font: inherit; }

.app-shell, .story-stage, .ai-stage {
  position: fixed;
  inset: 0;
  min-height: calc(var(--vh) * 100);
}

.boot-screen {
  position: fixed;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 12px;
  background: radial-gradient(circle at 50% 35%, #2a171b, #0d090a 64%);
  text-align: center;
}

.boot-title { font-size: 34px; font-weight: 800; color: #fff4e8; }
.boot-subtitle { color: var(--muted); }

.story-stage {
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(circle at 50% 25%, rgba(58, 35, 37, .82), transparent 45%),
    #080506;
}

.story-vn-shell {
  position: relative;
  width: 100vw;
  height: min(calc(var(--vh) * 100), 56.25vw);
  min-height: 0;
  max-height: calc(var(--vh) * 100);
  aspect-ratio: 16 / 9;
  overflow: hidden;
  box-shadow: 0 0 0 1px rgba(255, 232, 205, .10), 0 22px 86px rgba(0, 0, 0, .66);
}

.story-stage.has-cg .story-vn-shell {
  width: min(100vw, calc(var(--vh) * 177.78));
  height: min(calc(var(--vh) * 100), 56.25vw);
}

.story-bg, .ai-bg {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  transition: background-image 300ms ease, filter 220ms ease;
}

.story-stage.has-cg .story-bg {
  background-size: contain;
  background-repeat: no-repeat;
  background-color: #080506;
}

.story-shade, .ai-shade {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(0deg, rgba(8, 5, 6, 0.91), rgba(8, 5, 6, 0.05) 46%, rgba(8, 5, 6, 0.46)),
    linear-gradient(90deg, rgba(8, 5, 6, 0.72), rgba(8, 5, 6, 0.10) 50%, rgba(8, 5, 6, 0.38));
  pointer-events: none;
}

.story-stage.has-cg .story-shade {
  background:
    linear-gradient(0deg, rgba(8, 5, 6, .36), transparent 28%, rgba(8, 5, 6, .22)),
    linear-gradient(90deg, rgba(8, 5, 6, .18), transparent 32%, rgba(8, 5, 6, .14));
}

.story-sprite {
  position: absolute;
  right: -20px;
  bottom: 0;
  height: min(88vh, 980px);
  max-width: 58vw;
  object-fit: contain;
  object-position: bottom right;
  filter: drop-shadow(0 24px 46px rgba(0, 0, 0, 0.58));
  transform-origin: bottom center;
  transition: transform 220ms cubic-bezier(.22,1,.36,1), opacity 180ms ease;
}

.story-stage .story-sprite,
.story-sprite[hidden] { display: none; }
.breathing { animation: breathe 4.8s ease-in-out infinite; }
.speaking { animation: speakPulse 1.2s ease-in-out infinite; }
.pose-turn_away { transform: translateX(18px) rotate(.4deg); opacity: .94; }
.pose-look_down { transform: translateX(-8px) scale(.985); }
.pose-look_at_player { transform: translateX(-10px) scale(1.015); }
.pose-step_closer { transform: translateX(-34px) scale(1.055); }
.pose-ring_touch { transform: translateX(-14px) scale(1.01); }

.topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 5;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  padding: max(10px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) 8px max(14px, env(safe-area-inset-left));
}

.brand { font-weight: 800; color: #fff7ec; text-shadow: 0 2px 10px #000; }
.chapter-label { margin-top: 3px; font-size: 12px; color: var(--muted); }
.top-actions { display: flex; gap: 7px; }

.icon-button, .next-button, .choice, .send-button, .mic-button {
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: rgba(92, 48, 50, .76);
  min-height: 44px;
}

.icon-button {
  padding: 8px 10px;
  background: rgba(21, 12, 14, .72);
  font-size: 13px;
}

.icon-button.muted { color: var(--muted); }

.dialogue-panel {
  position: fixed;
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 4;
  max-width: min(980px, calc(100% - 24px));
  margin: auto;
  padding: 10px 13px;
  background: rgba(16, 9, 11, .56);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 24px 72px rgba(0,0,0,.52);
  backdrop-filter: blur(14px);
}

.story-stage.has-cg .dialogue-panel {
  background: rgba(13, 8, 10, .38);
  border-color: rgba(255, 232, 205, .16);
  box-shadow: 0 18px 52px rgba(0,0,0,.42);
}

.action-line {
  min-height: 20px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
  margin-bottom: 5px;
}

.speaker {
  color: var(--gold);
  font-weight: 800;
  font-size: 15px;
  margin-bottom: 7px;
}

.dialogue-text {
  min-height: 3.2em;
  font-size: 18px;
  line-height: 1.5;
  white-space: pre-line;
  text-shadow: 0 2px 6px rgba(0,0,0,.45);
}

.story-stage.has-cg .dialogue-text {
  min-height: 2.9em;
}

.choices {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(250px, 100%), 1fr));
  gap: 8px;
  margin-top: 10px;
}

.choices[hidden] { display: none; }

.choice {
  width: 100%;
  padding: 9px 11px;
  text-align: left;
  background: rgba(47, 27, 30, .56);
}

.choice span {
  display: block;
  font-size: 15px;
  line-height: 1.32;
}

.choice small {
  display: block;
  margin-top: 5px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.choice em {
  float: right;
  margin-left: 8px;
  color: rgba(216, 179, 106, .86);
  font-size: 11px;
  font-style: normal;
}

.choice-protective { border-color: rgba(216, 179, 106, .56); }
.choice-boundary { background: rgba(48, 54, 58, .72); }
.choice-danger { border-color: rgba(182, 64, 76, .54); }

.panel-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 10px;
}

.status-line {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.next-button {
  min-width: 104px;
  padding: 10px 15px;
  background: rgba(151, 45, 62, .86);
}

.next-button[hidden] { display: none; }

.memory-toast {
  position: fixed;
  top: max(62px, calc(env(safe-area-inset-top) + 62px));
  right: max(14px, env(safe-area-inset-right));
  z-index: 7;
  max-width: min(320px, 76vw);
  padding: 9px 11px;
  border: 1px solid rgba(216, 179, 106, .36);
  border-radius: 6px;
  background: rgba(24, 15, 16, .86);
  color: var(--gold);
  font-size: 13px;
  opacity: 0;
  transform: translateY(-8px);
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
}

.memory-toast.show { opacity: 1; transform: translateY(0); }

.ai-stage {
  z-index: 8;
  display: none;
  background: #0d090a;
}

.ai-stage.open { display: block; }
.ai-bg { filter: saturate(.92) brightness(.82); }

.ai-mood {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 72px);
  left: 16px;
  right: 16px;
  z-index: 2;
  color: var(--muted);
  text-align: center;
  font-size: 13px;
  text-shadow: 0 2px 10px #000;
}

.ai-companion-stage {
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: min(100vw, 82vh);
  max-width: 100vw;
  filter: drop-shadow(0 30px 54px rgba(0,0,0,.65));
  transform: translateX(-50%);
  transform-origin: bottom center;
  transition: transform 260ms cubic-bezier(.22,1,.36,1), opacity 180ms ease;
  overflow: hidden;
  pointer-events: none;
}

.cubism-canvas,
.cubism-fallback {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.cubism-canvas {
  z-index: 2;
}

.cubism-canvas[hidden],
.cubism-fallback[hidden] {
  display: none;
}

.cubism-fallback {
  z-index: 1;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.ai-sprite {
  position: absolute;
  left: 50%;
  bottom: max(15vh, calc(env(safe-area-inset-bottom) + 128px));
  width: min(100vw, 720px);
  height: min(82vh, 1120px);
  transform: translateX(-50%);
  object-fit: contain;
  object-position: center bottom;
}

.ai-companion-stage.pose-turn_away { transform: translateX(calc(-50% + 22px)) rotate(.35deg); opacity: .94; }
.ai-companion-stage.pose-look_down { transform: translateX(calc(-50% - 8px)) scale(.985); }
.ai-companion-stage.pose-look_at_player { transform: translateX(calc(-50% - 8px)) scale(1.015); }
.ai-companion-stage.pose-step_closer { transform: translateX(calc(-50% - 18px)) scale(1.045); }
.ai-companion-stage.pose-ring_touch { transform: translateX(calc(-50% - 10px)) scale(1.012); }
.ai-companion-stage.pose-glove_adjust,
.ai-companion-stage.pose-reject { transform: translateX(calc(-50% + 12px)) scale(.985); }
.ai-companion-stage.pose-hair_tuck { transform: translateX(calc(-50% - 14px)) scale(1.02) rotate(-.2deg); }

.ai-companion-stage.speaking {
  animation: speakPulse 1.05s ease-in-out infinite;
}

.ai-companion-stage.touched {
  animation: touchFloat 1.25s ease both;
}

.boundary-overlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(0deg, rgba(23, 31, 42, .62), rgba(20, 18, 20, .18));
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
}

.boundary-overlay.show, .ai-stage.rejected .boundary-overlay { opacity: 1; }

.touch-hotzones {
  position: absolute;
  left: 50%;
  top: 3vh;
  width: min(88vw, 58vh);
  height: 70vh;
  transform: translateX(-50%);
  z-index: 3;
  pointer-events: none;
}

.touch-zone {
  position: absolute;
  border: 0;
  outline: none;
  background: rgba(255,255,255,0);
  padding: 0;
  pointer-events: auto;
  appearance: none;
  -webkit-appearance: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.touch-zone:focus-visible {
  outline: none;
}

.touch-hair { left: 30%; top: 4%; width: 40%; height: 14%; }
.touch-face { left: 32%; top: 17%; width: 36%; height: 13%; }
.touch-shoulder { left: 12%; top: 30%; width: 76%; height: 13%; }
.touch-hand { left: 8%; top: 43%; width: 84%; height: 16%; }
.touch-waist { left: 24%; top: 55%; width: 52%; height: 15%; }
.touch-disabled { cursor: not-allowed; }

.ai-panel {
  position: absolute;
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 4;
  max-width: 980px;
  margin: auto;
  padding: 9px;
  background: rgba(20, 12, 15, .56);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 24px 78px rgba(0,0,0,.56);
  backdrop-filter: blur(14px);
}

.ai-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 6px;
}

.ai-title {
  color: var(--gold);
  font-weight: 800;
  font-size: 16px;
}

.ai-subtitle {
  color: var(--muted);
  font-size: 12px;
  margin-top: 3px;
}

.ai-live {
  min-height: 58px;
  border: 1px solid rgba(255, 232, 205, .13);
  border-radius: 6px;
  background: rgba(8, 5, 6, .28);
  padding: 7px 9px;
}

.ai-speaker {
  color: var(--gold);
  font-weight: 800;
  margin-bottom: 4px;
}

.ai-reply {
  font-size: 14px;
  line-height: 1.42;
  white-space: pre-line;
}

.ai-action {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
  min-height: 1.3em;
}

.ai-log {
  max-height: 5vh;
  overflow: auto;
  margin-top: 6px;
}

.ai-log p {
  margin: 0 0 5px;
  color: #ead8cc;
  font-size: 12px;
  line-height: 1.42;
}

.ai-log b { color: var(--gold); }

.ai-input {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 7px;
  margin-top: 6px;
}

.ai-input textarea {
  width: 100%;
  min-height: 34px;
  max-height: 58px;
  resize: vertical;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: rgba(9, 6, 7, .54);
  color: var(--ink);
  padding: 7px 8px;
  font-size: 14px;
  line-height: 1.35;
}

.send-button, .mic-button {
  padding: 8px 12px;
  background: rgba(151, 45, 62, .86);
}

.mic-button.recording {
  background: rgba(205, 67, 80, .96);
  box-shadow: 0 0 0 4px rgba(205, 67, 80, .16);
}

.ai-status {
  margin-top: 5px;
  color: var(--muted);
  font-size: 12px;
  min-height: 1.3em;
}

.log-drawer {
  position: fixed;
  z-index: 10;
  top: calc(env(safe-area-inset-top) + 12px);
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  width: min(440px, calc(100vw - 24px));
  transform: translateX(calc(100% + 20px));
  transition: transform 220ms ease;
  background: rgba(13, 8, 10, .94);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 22px 72px rgba(0,0,0,.62);
  padding: 12px;
}

.log-drawer.open { transform: translateX(0); }
.log-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.log-title { color: var(--gold); font-weight: 800; }
.log-tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 10px 0; }
.log-tabs button {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(42, 27, 30, .78);
  color: var(--muted);
}
.log-tabs button.active { color: var(--ink); border-color: rgba(216,179,106,.5); }
.log-body { overflow: auto; max-height: calc(100% - 92px); padding-right: 4px; }
.log-body p { margin: 0 0 9px; font-size: 13px; line-height: 1.45; color: #ead8cc; }
.log-body b { display: block; color: var(--gold); margin-bottom: 2px; }

@media (max-width: 720px) {
  .story-stage {
    justify-content: flex-start;
    align-items: center;
    flex-direction: column;
    padding-top: max(42px, env(safe-area-inset-top));
  }

  .story-vn-shell {
    width: 100vw;
    height: 56.25vw;
  }

  .story-stage.has-cg .story-vn-shell {
    width: 100vw;
    height: 56.25vw;
    min-height: 56.25vw;
  }

  .story-sprite {
    right: -86px;
    height: 72vh;
    max-width: 92vw;
  }

  .dialogue-panel {
    left: 8px;
    right: 8px;
    bottom: 8px;
    padding: 8px 9px;
  }

  .story-stage.has-cg .dialogue-panel {
    position: fixed;
    left: 8px;
    right: 8px;
    bottom: max(8px, env(safe-area-inset-bottom));
    width: calc(100vw - 16px);
    margin-top: 0;
    max-height: min(46vh, calc(calc(var(--vh) * 100) - 56.25vw - max(58px, env(safe-area-inset-top))));
    overflow: auto;
  }

  .dialogue-text {
    font-size: clamp(13px, 3.45vw, 16px);
    line-height: 1.42;
    min-height: 2.9em;
  }

  .story-stage.has-cg .dialogue-text {
    min-height: 2.4em;
  }

  .action-line {
    display: none;
  }

  .speaker {
    font-size: 13px;
    margin-bottom: 4px;
  }

  .choices {
    gap: 5px;
    margin-top: 6px;
  }

  .choice {
    min-height: 36px;
    padding: 6px 8px;
  }

  .choice span {
    font-size: clamp(12px, 3.25vw, 14px);
  }

  .choice small,
  .status-line {
    display: none;
  }

  .panel-footer {
    margin-top: 6px;
    justify-content: flex-end;
  }

  .next-button {
    min-width: 82px;
    min-height: 36px;
    padding: 7px 11px;
  }

  .topbar {
    position: fixed;
    padding: 6px 8px 0;
  }

  .brand {
    font-size: 13px;
  }

  .chapter-label {
    font-size: 10px;
  }

  .icon-button {
    min-height: 32px;
    padding: 5px 7px;
    font-size: 12px;
  }

  .top-actions {
    gap: 4px;
  }

  .ai-sprite {
    width: 100vw;
    height: 77vh;
    bottom: max(18vh, calc(env(safe-area-inset-bottom) + 132px));
  }

  .ai-companion-stage {
    width: 100vw;
  }

  .touch-hotzones {
    top: 4vh;
    height: 69vh;
  }

  .ai-panel {
    padding: 8px;
    background: rgba(20, 12, 15, .48);
    max-height: 26vh;
    overflow: auto;
  }

  .ai-live {
    min-height: 42px;
  }

  .ai-reply {
    font-size: 13px;
    line-height: 1.36;
  }

  .ai-log {
    display: none;
  }

  .ai-input {
    grid-template-columns: 1fr auto auto;
  }

  .send-button, .mic-button {
    width: auto;
    min-width: 64px;
  }

  .ai-head {
    margin-bottom: 6px;
  }

  .ai-input textarea {
    min-height: 32px;
    max-height: 44px;
  }

  .ai-subtitle,
  .ai-action,
  .ai-status {
    font-size: 11px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .breathing, .speaking, .ai-companion-stage.touched {
    animation: none;
  }
}

@keyframes breathe {
  0%, 100% { opacity: .98; }
  50% { opacity: 1; }
}

@keyframes speakPulse {
  0%, 100% { filter: drop-shadow(0 24px 46px rgba(0,0,0,.58)); }
  50% { filter: drop-shadow(0 31px 54px rgba(0,0,0,.68)); }
}

@keyframes touchFloat {
  0%, 100% { filter: drop-shadow(0 30px 54px rgba(0,0,0,.65)); }
  45% { filter: drop-shadow(0 34px 62px rgba(216,179,106,.22)); }
}
