:root {
  --bg: #1b1420;
  --panel: rgba(20, 14, 26, 0.86);
  --ink: #f6eef4;
  --muted: #b9a9b6;
  --accent: #ff6fa3;
  --accent-2: #8fd3ff;
  --danger: #ff4d5e;
  --line-green: #8de055;
  --chat-bg: #7494c0;
  --radius: 14px;
  --hud-h: 46px;
  --top-h: 34px;
  font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: #0d0a10; color: var(--ink); }
body { overflow: hidden; -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

#app {
  position: relative; margin: 0 auto; width: 100%; max-width: 480px;
  height: 100vh; height: 100dvh; overflow: hidden; background: var(--bg);
  box-shadow: 0 0 40px rgba(0,0,0,.6);
}
.screen { position: absolute; inset: 0; }

/* ---------------- HUD ---------------- */
#hud {
  position: absolute; top: 0; left: 0; right: 0; height: var(--hud-h); z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; align-items: center;
  padding: 6px 8px; padding-top: max(6px, env(safe-area-inset-top));
  background: linear-gradient(#120d16, rgba(18,13,22,.92));
  border-bottom: 1px solid rgba(255,255,255,.08);
}
.bar { position: relative; display: flex; align-items: center; gap: 4px; min-width: 0; }
.bar-icon { font-size: 14px; width: 18px; text-align: center; flex: none; }
.bar-track { flex: 1; height: 8px; border-radius: 4px; background: rgba(255,255,255,.12); overflow: hidden; min-width: 20px; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 4px; transition: width .5s ease; }
.bar[data-key="obsession"] .bar-fill { background: #b06cff; }
.bar[data-key="escape"] .bar-fill { background: #ffb347; }
.bar[data-key="mental"] .bar-fill { background: var(--accent-2); }
.bar-val { font-size: 11px; width: 16px; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; flex: none; }
.bar.danger .bar-fill { background: var(--danger) !important; animation: pulse 1s infinite; }
.bar.flash .bar-track { animation: flash .8s; }
@keyframes flash { 0%, 100% { box-shadow: none; } 30% { box-shadow: 0 0 0 2px #fff, 0 0 12px #fff; } }
@keyframes pulse { 50% { opacity: .55; } }
.pop {
  position: absolute; left: 50%; top: 100%; transform: translateX(-50%);
  font-size: 13px; font-weight: 700; white-space: nowrap; pointer-events: none;
  padding: 2px 7px; border-radius: 10px; background: rgba(0,0,0,.75);
  animation: pop 1.6s ease-out forwards; z-index: 40;
}
.pop.up { color: #ffd2e4; } .pop.down { color: #a9e1ff; }
@keyframes pop { 0% { opacity: 0; transform: translate(-50%, -4px); } 15% { opacity: 1; transform: translate(-50%, 4px); } 80% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, 18px); } }

/* ---------------- Title ---------------- */
#screen-title { display: flex; flex-direction: column; overflow-y: auto; }
.kv {
  flex: 1 1 auto; min-height: 46%; background: #2a2030 center 18% / cover no-repeat;
  position: relative;
}
.kv::after { content: ""; position: absolute; inset: 0; background: linear-gradient(transparent 55%, var(--bg)); }
.t-body { padding: 0 20px 24px; margin-top: -60px; position: relative; z-index: 1; }
#t-title {
  font-size: 25px; line-height: 1.35; margin: 0 0 8px; letter-spacing: .02em;
  text-shadow: 0 2px 12px rgba(0,0,0,.9), 0 0 2px #000;
}
#t-title::first-line { color: #fff; }
.t-sub { color: var(--muted); font-size: 13.5px; margin: 0 0 16px; }
.t-complete { color: var(--accent); font-size: 13px; }
.t-buttons { display: grid; gap: 10px; }
.t-foot { color: #75667a; font-size: 11px; text-align: center; margin-top: 16px; }
.btn {
  display: block; width: 100%; padding: 13px 16px; border-radius: 12px; cursor: pointer;
  border: 1px solid rgba(255,255,255,.18); background: rgba(255,255,255,.07); font-size: 15px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #2a0614; font-weight: 700; }
.btn.ghost { background: transparent; color: var(--muted); }
.btn:active { transform: translateY(1px); }

/* ---------------- Game ---------------- */
#screen-game { top: var(--hud-h); }
.game-top {
  position: absolute; top: 0; left: 0; right: 0; height: var(--top-h); z-index: 25;
  display: flex; align-items: center; gap: 6px; padding: 0 8px; font-size: 12px; color: var(--muted);
  background: rgba(10,8,12,.55);
}
.spacer { flex: 1; }
.mini { background: rgba(255,255,255,.1); border: 1px solid rgba(255,255,255,.15); border-radius: 8px; padding: 3px 8px; font-size: 12px; cursor: pointer; }
.mini:disabled { opacity: .35; cursor: default; }
.mini.on { background: var(--accent); color: #2a0614; }

.layer { position: absolute; inset: var(--top-h) 0 0 0; transition: transform .45s cubic-bezier(.2,.8,.2,1), opacity .45s; }
#layer-novel.under { transform: scale(.96); opacity: .25; }
#layer-chat { transform: translateY(105%); background: var(--chat-bg); display: flex; flex-direction: column; z-index: 5; border-radius: 18px 18px 0 0; overflow: hidden; }
#layer-chat.on { transform: translateY(0); }

#bg { position: absolute; inset: 0; background: #2a2030 center / cover no-repeat; transition: background-image .4s; }
#bg.black { background: #000 !important; }
#stage { position: absolute; left: 0; right: 0; bottom: 120px; top: 0; pointer-events: none; overflow: hidden; }
.sprite {
  position: absolute; bottom: -2%; height: 92%; max-width: none;
  filter: drop-shadow(0 6px 18px rgba(0,0,0,.35)); transition: filter .3s, left .3s;
  left: 50%; transform: translateX(-50%);
  -webkit-mask-image: linear-gradient(to bottom, #000 82%, transparent 100%);
          mask-image: linear-gradient(to bottom, #000 82%, transparent 100%);
}
.sprite[data-pos="left"] { left: 30%; }
.sprite[data-pos="right"] { left: 72%; }
.sprite.dim { filter: brightness(.72) saturate(.8) drop-shadow(0 6px 18px rgba(0,0,0,.35)); }

#textbox {
  position: absolute; left: 8px; right: 8px; bottom: 8px; min-height: 128px; z-index: 6;
  background: var(--panel); border: 1px solid rgba(255,255,255,.14); border-radius: var(--radius);
  padding: 22px 16px 16px; font-size: 16px; line-height: 1.75; cursor: pointer;
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
#textbox.narration { color: #eadde6; }
#textbox.nag { border-color: var(--danger); box-shadow: 0 0 0 1px var(--danger), 0 0 18px rgba(255,77,94,.35); }
#tb-name {
  position: absolute; top: -13px; left: 14px; padding: 2px 12px; border-radius: 8px;
  background: var(--accent); color: #2a0614; font-weight: 700; font-size: 14px;
}
#tb-next { position: absolute; right: 14px; bottom: 6px; font-size: 12px; color: var(--muted); animation: bob 1s infinite; }
@keyframes bob { 50% { transform: translateY(3px); } }

#flood { position: absolute; inset: 0; z-index: 7; pointer-events: none; overflow: hidden; display: flex; flex-wrap: wrap; align-content: flex-start; gap: 2px 8px; padding: 12px; }
#flood span { color: #ff3b5c; font-weight: 800; opacity: 0; animation: appear .2s forwards; text-shadow: 0 0 6px rgba(0,0,0,.8); }
@keyframes appear { to { opacity: var(--o, 1); } }

#app.shake { animation: shake .45s; }
@keyframes shake { 20% { transform: translateX(-6px); } 40% { transform: translateX(6px); } 60% { transform: translateX(-4px); } 80% { transform: translateX(3px); } }

/* choices */
#choices {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 20; display: grid; gap: 8px;
  padding: 10px 10px max(12px, env(safe-area-inset-bottom));
  background: linear-gradient(transparent, rgba(10,6,14,.92) 18%);
}
#layer-novel:not(.under) ~ #choices { bottom: 146px; background: none; }
.choice {
  display: flex; align-items: center; gap: 10px; text-align: left; width: 100%;
  padding: 12px 14px; border-radius: 12px; cursor: pointer; font-size: 15px;
  background: rgba(255,255,255,.93); color: #2a1a26; border: none; box-shadow: 0 4px 14px rgba(0,0,0,.35);
  animation: rise .25s ease-out both;
}
.choice:nth-child(2) { animation-delay: .06s; } .choice:nth-child(3) { animation-delay: .12s; }
.choice .num { flex: none; width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 12px; display: grid; place-items: center; font-weight: 700; }
.choice.continue { justify-content: center; background: var(--accent); color: #2a0614; font-weight: 700; }
.choice:active { transform: scale(.98); }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

/* ---------------- Chat (LINE 風) ---------------- */
.chat-head { flex: none; display: flex; align-items: center; gap: 10px; height: 44px; padding: 0 12px; background: #2f3b52; color: #fff; font-weight: 700; }
.chat-head .back { font-size: 24px; font-weight: 400; }
.chat-head .ico { font-size: 13px; opacity: .8; }
#chat-log { flex: 1; overflow-y: auto; padding: 12px 10px 210px; display: flex; flex-direction: column; gap: 8px; scroll-behavior: smooth; }
.msg { display: flex; align-items: flex-end; gap: 6px; animation: rise .2s ease-out; }
.msg.self { justify-content: flex-end; }
.msg.other { align-items: flex-start; }
.avatar { flex: none; width: 36px; height: 36px; border-radius: 50%; overflow: hidden; display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 15px; background: #ddd; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.col { display: flex; flex-direction: column; gap: 2px; max-width: 74%; }
.who { font-size: 11px; color: #eef2f8; }
.line { display: flex; align-items: flex-end; gap: 4px; }
.bubble { background: #fff; color: #1d1d1d; padding: 8px 12px; border-radius: 16px; font-size: 14.5px; line-height: 1.55; word-break: break-word; position: relative; }
.msg.self .bubble { background: var(--line-green); max-width: 74%; }
.msg.other .bubble { border-top-left-radius: 4px; }
.msg.nagmsg .bubble { box-shadow: 0 0 0 2px var(--danger); }
.meta { display: flex; flex-direction: column; align-items: flex-end; font-size: 10px; color: #eef2f8; }
.time { font-size: 10px; color: #eef2f8; white-space: nowrap; }
.read { font-size: 10px; }
.photo { padding: 0; overflow: hidden; width: 210px; }
.photo-emoji { height: 110px; display: grid; place-items: center; font-size: 46px; background: linear-gradient(135deg, #ffe4ef, #fff6d8); }
.photo-cap { padding: 7px 10px; font-size: 12px; color: #555; }
.burst-bubble { background: #fff; }
.burst-count { font-weight: 700; color: var(--danger); font-size: 13px; margin-bottom: 2px; }
.burst-line { font-size: 13px; color: #444; }
.burst-more { color: #999; }
.msg-place { align-self: center; font-size: 12px; color: #fff; background: rgba(0,0,0,.28); padding: 3px 12px; border-radius: 12px; margin: 4px 0; }
.msg-divider { align-self: center; font-size: 12px; color: #fff; font-weight: 700; margin: 8px 0 2px; }
.msg-narration { align-self: stretch; text-align: center; color: #f4f6fb; font-size: 13.5px; line-height: 1.7; padding: 2px 10px; text-shadow: 0 1px 2px rgba(0,0,0,.35); }
.typing { display: flex; gap: 4px; padding: 10px 14px; background: #fff; border-radius: 16px; align-self: flex-start; margin-left: 42px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: #aaa; animation: dot 1s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes dot { 50% { transform: translateY(-4px); background: #666; } }

/* ---------------- Result ---------------- */
#screen-result { overflow-y: auto; padding: 0 0 32px; }
.r-banner { background: #3a2a48; color: #e9d8ff; font-size: 12px; padding: 8px 14px; }
.r-thumb { height: 220px; background: #000 center / cover no-repeat; position: relative; }
.r-thumb-in { position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: flex-end; padding: 16px; background: linear-gradient(transparent 30%, rgba(0,0,0,.85)); }
.r-no { font-size: 12px; letter-spacing: .2em; color: var(--accent); font-weight: 700; }
#r-title { margin: 4px 0 0; font-size: 24px; }
.r-last { margin: 16px 18px 6px; font-size: 16px; line-height: 1.8; border-left: 3px solid var(--accent); padding-left: 12px; }
.r-note { margin: 8px 18px; font-size: 12px; color: var(--muted); }
.r-bars { display: grid; gap: 8px; margin: 14px 18px; }
.r-bars .bar-label { width: 64px; font-size: 12px; color: var(--muted); flex: none; }
.r-bars .bar-track { height: 10px; }
.r-bars .bar-val { width: 22px; font-size: 13px; }
.r-details { margin: 6px 18px; background: rgba(255,255,255,.05); border-radius: 10px; padding: 8px 12px; }
.r-details summary { cursor: pointer; font-size: 14px; }
#r-route { margin: 8px 0 0; padding-left: 22px; font-size: 13px; display: grid; gap: 6px; }
#r-route .rs { display: block; color: var(--muted); font-size: 11px; }
.r-buttons { display: grid; gap: 8px; margin: 16px 18px 6px; }
.r-url { margin: 4px 18px; font-size: 10.5px; color: #75667a; word-break: break-all; }
.r-h { margin: 22px 18px 8px; font-size: 15px; }
.gallery { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 0 18px; }
.ecard { border-radius: 10px; overflow: hidden; background: rgba(255,255,255,.06); }
.ethumb { height: 74px; background: #000 center / cover no-repeat; position: relative; }
.ecard.unseen .ethumb { background: radial-gradient(circle at 50% 120%, #3b2d44 0 38%, #18121c 39%); }
.ecard.unseen .ethumb::after { content: "？"; position: absolute; inset: 0; display: grid; place-items: center; font-size: 30px; color: #3e3046; font-weight: 800; }
.eno { position: absolute; left: 6px; top: 4px; font-size: 11px; font-weight: 700; background: rgba(0,0,0,.6); padding: 1px 6px; border-radius: 6px; z-index: 1; }
.etitle { font-size: 12.5px; padding: 6px 8px 2px; font-weight: 700; }
.ehint { font-size: 11px; color: var(--muted); padding: 0 8px 8px; line-height: 1.5; }
.ecard.seen .etitle { padding-bottom: 8px; }

/* ---------------- Modal / toast ---------------- */
.modal { position: absolute; inset: 0; z-index: 60; background: rgba(0,0,0,.7); display: grid; place-items: center; padding: 16px; }
.modal-in { width: 100%; max-height: 90%; overflow-y: auto; background: #241b2a; border-radius: 16px; padding: 18px; display: grid; gap: 12px; }
.modal-in h3 { margin: 0; }
.modal-in .gallery { margin: 0; }
fieldset { border: 1px solid rgba(255,255,255,.15); border-radius: 10px; display: grid; gap: 8px; font-size: 14px; }
.small { font-size: 12px; color: var(--muted); margin: 0; }
#toast { position: absolute; left: 50%; bottom: 24px; transform: translateX(-50%); background: rgba(0,0,0,.85); padding: 10px 16px; border-radius: 20px; font-size: 13px; z-index: 80; }

@media (min-width: 481px) { body { display: block; } }
@media (prefers-reduced-motion: reduce) { * { animation-duration: .01ms !important; transition: none !important; } }
