/* A story is the entry point; the character and conversation share one stage. */
html:has(.story-chat[open]) { overflow: hidden; }
.story-chat {
  position: fixed; inset: 0; margin: 0; padding: 0; border: 0;
  width: 100vw; max-width: none; height: 100dvh; max-height: none;
  color: #fcf6ef; background: #11131a; font-family: inherit;
  overflow: hidden; --chat-font: 1rem; --chat-accent: #dfab85;
}
.story-chat[open] { display: flex; flex-direction: column; }
.story-chat::backdrop { background: #0d0f16; }
.story-chat [hidden] { display: none !important; }
.story-chat button, .story-chat textarea { font: inherit; }
.story-chat button { color: inherit; cursor: pointer; border: 1px solid #ffffff28; background: #201e25d9; min-height: 44px; }
.story-chat button:disabled { opacity: .42; cursor: default; }
.story-chat button:focus-visible, .story-chat textarea:focus-visible, .story-chat input:focus-visible { outline: 2px solid #ffe1b9; outline-offset: 3px; }
.chat-art, .chat-shade { position: absolute; inset: 0; pointer-events: none; }
.chat-art img { position: absolute; width: 100%; height: 100%; object-fit: cover; }
.chat-shade { background: linear-gradient(#080b14b3, #080b1400 25%, #090b1230 42%, #090b12a3 72%, #090b12e6); }
.chat-top { position: relative; z-index: 2; display: flex; align-items: center; gap: 10px;
  padding: max(14px, env(safe-area-inset-top)) 16px 10px; flex-shrink: 0; }
.chat-icon { display: inline-grid; place-items: center; flex-shrink: 0; width: 44px; height: 44px; border-radius: 50%; font-size: 1.6rem !important; backdrop-filter: blur(12px); }
.chat-identity { flex: 1; min-width: 0; text-shadow: 0 2px 12px #000; }
.chat-identity > span { display: block; font-size: .69rem; color: #eee0ce; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-identity h2 { font-size: 1.08rem; margin: 3px 0 0; font-weight: 600; color: #fff; }
.chat-progress { border: 1px solid #ffffff25; padding: 6px 10px; border-radius: 30px; font-size: .7rem; background: #1718229e; white-space: nowrap; }
.chat-eye { position: absolute; top: 27%; right: 18px; z-index: 3; font-size: 1.2rem !important; }
.chat-feed { position: relative; z-index: 1; flex: 1; min-height: 70px; margin-top: calc(var(--stage-height, 100dvh) * .25); overflow-y: auto;
  overscroll-behavior: contain; scrollbar-width: thin; padding: 24px 18px 10px;
  mask-image: linear-gradient(transparent, #000 18px); overflow-wrap: anywhere; scrollbar-color: #af907855 transparent; }
.chat-message { max-width: 88%; margin: 0 0 16px; font-size: var(--chat-font); line-height: 1.65; }
.chat-message p { margin: 0; white-space: pre-wrap; }
.chat-message-character, .chat-message-user { width: fit-content; padding: 12px 16px; border: 1px solid #ffffff29;
  border-radius: 19px 19px 19px 5px; background: #171921d9; box-shadow: 0 3px 15px #00000017; backdrop-filter: blur(10px); }
.chat-message-user { margin-left: auto; border-radius: 19px 19px 5px 19px; background: #804d4eea; border-color: #ecb8a43b; }
.chat-speaker { display: block; font-size: .66em; color: #f0c4a6; margin-bottom: 3px; letter-spacing: .03em; }
.chat-message-user .chat-speaker { color: #ffe0db; }
.chat-message em { display: block; font-size: .94em; color: #e4cabc; font-style: normal; margin-bottom: 4px; }
.chat-message-narration { border-left: 2px solid #e8d1bca3; padding-left: 12px; margin-left: 5px;
  color: #f8eee6; font-size: calc(var(--chat-font) * .9); text-shadow: 0 1px 5px #000; }
.chat-act-title { color: #f0c4a6; margin: 6px 0 12px; font-size: 1em; font-weight: 600; }
.chat-message-image { display: none; }
.chat-footer { position: relative; z-index: 2; flex-shrink: 0; max-height: 57%; overflow: hidden; display: flex; flex-direction: column;
  padding: 0 16px max(14px, env(safe-area-inset-bottom)); background: linear-gradient(transparent, #10131bba 35%); }
.chat-status { margin: 3px 3px 8px; min-height: 1.3em; font-size: .75rem; color: #f0dbc7; line-height: 1.5; flex-shrink: 0; }
.chat-choices { display: grid; grid-auto-rows: max-content; gap: 8px; margin-bottom: 10px; overflow-y: auto; min-height: 0; scrollbar-width: thin; scrollbar-color: #af907855 transparent; }
.chat-choice { text-align: left; padding: 10px 15px; border-radius: 16px; background: #172333e8 !important;
  border-color: #bb997872 !important; font-size: calc(var(--chat-font) * .9) !important; line-height: 1.5; }
.chat-choice small { display: block; color: #c5ae95; font-size: .71em; margin-bottom: 2px; }
.chat-choice:hover { border-color: #f0c19c !important; background: #233144f2 !important; }
.chat-tools { display: flex; align-items: center; gap: 6px; margin: 6px 0; flex-shrink: 0; flex-wrap: wrap; }
.chat-tools button { border-radius: 20px; padding: 5px 10px; font-size: .72rem; background: #26212b99; }
.chat-tools button[aria-pressed="true"] { color: #ffe0bc; border-color: #ecc9a566; }
.chat-mode-pill { margin-right: auto; font-size: .68rem; color: #e9c1a2; border: 1px solid #e9c1a255; border-radius: 20px; padding: 7px 10px; white-space: nowrap; }
.chat-compose { display: flex; align-items: center; gap: 7px; border: 1px solid #fff3; flex-shrink: 0;
  background: #30272fd9; backdrop-filter: blur(20px); border-radius: 30px; padding: 6px; }
.chat-compose .chat-icon { background: #ffffff09; border: 0; }
.chat-compose textarea { flex: 1; min-width: 0; max-height: 120px; background: transparent; color: #fff8f2;
  padding: 8px 2px; border: 0; resize: none; line-height: 1.5; font-size: max(16px, var(--chat-font)); }
.chat-compose textarea::placeholder { color: #d6c8ca; }
.chat-send { border: 0 !important; border-radius: 50%; width: 44px; height: 44px; flex-shrink: 0;
  background: #e9b188 !important; color: #301e1c !important; font-size: 1.7rem !important; }
.chat-latest { position: absolute; z-index: 4; top: 35%; left: 50%; transform: translateX(-50%); border-radius: 24px; padding: 5px 14px; font-size: .8rem !important; }
.chat-ending { text-align: center; padding: 12px 0 6px; }
.chat-ending span { display: block; margin-bottom: 12px; font-size: .9rem; color: #f5d4b1; }
.chat-ending button, .chat-retry, .chat-primary { border-radius: 18px; padding: 10px 18px; }
.chat-panel { position: absolute; z-index: 8; right: 14px; top: 80px; bottom: max(18px, env(safe-area-inset-bottom));
  width: min(390px, calc(100% - 28px)); overflow-y: auto; padding: 22px; box-sizing: border-box;
  border-radius: 26px; border: 1px solid #ffffff29; background: #242027f5; backdrop-filter: blur(25px); box-shadow: 0 20px 100px #0009; }
.chat-panel header { display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid #ffffff20; padding-bottom: 12px; }
.chat-panel header h3 { margin: 0; font-size: 1.1rem; color: #fff1e9; }
.chat-panel header button { border: none; background: transparent; width: 44px; }
.chat-panel-label { color: #ad9c9f; font-size: .78rem; margin: 23px 0 12px; }
.chat-panel fieldset { padding: 0; border: 0; margin: 0 0 20px; }
.chat-panel legend { font-size: .88rem; margin-bottom: 10px; }
.chat-segments { display: flex; gap: 7px; }
.chat-segments button { flex: 1; border-radius: 14px; padding: 8px; font-size: .85rem; min-width: 0; }
.chat-segments button[aria-pressed="true"] { background: #9b5d58; border-color: #e9ac9c; }
.chat-segments [data-value="small"] { font-size: .8rem; }
.chat-segments [data-value="large"] { font-size: 1.2rem; }
.chat-segments [data-value="largest"] { font-size: 1.4rem; }
.chat-toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; font-size: .87rem; margin: 8px 0; }
.chat-toggle input { appearance: none; flex-shrink: 0; width: 44px; height: 26px; border: 1px solid #ffffff2b; border-radius: 20px; background: #595057; position: relative; cursor: pointer; }
.chat-toggle input::before { content: ''; position: absolute; width: 18px; height: 18px; border-radius: 50%; background: #f6eae2; left: 3px; top: 3px; }
.chat-toggle input:checked { background: #b77268; }
.chat-toggle input:checked::before { transform: translateX(18px); }
.chat-help-link { border: 0 !important; background: transparent !important; color: #e2bca0 !important; text-decoration: underline; }
.chat-help-panel { top: 20%; bottom: auto; max-height: 76%; left: 50%; right: auto; transform: translateX(-50%); }
.chat-help-panel p { line-height: 1.75; font-size: .88rem; }
.chat-help-panel blockquote { margin: 12px 0; border: 1px solid #e1bba933; padding: 16px; border-radius: 16px; font-size: .9rem; line-height: 1.7; color: #efbfad; }
.chat-primary { width: 100%; background: #9b5d58 !important; }
.story-chat:not(.writing) .chat-compose, .story-chat:not(.writing) [data-chat-situation] { display: none; }
.story-chat[data-story-mode="freeform"] .chat-feed { margin-top: calc(var(--stage-height, 100dvh) * .17); }
.story-chat[data-story-mode="freeform"].compact .chat-feed { margin-top: 8px; }
.story-chat[data-story-mode="freeform"] .chat-status { font-size: .7rem; }
.chat-novel-panel { top: 12px; bottom: 12px; width: calc(100% - 28px); background: #f4ebdd; color: #342b28; }
.chat-novel-panel header h3 { color: #342b28; }
.chat-novel-panel button { color: #342b28; }
.chat-novel-content { white-space: pre-wrap; line-height: 1.95; font-size: var(--chat-font); padding: 24px 0; overflow-wrap: anywhere; }
.chat-novel-panel .chat-primary { margin-top: 16px; }
.chat-ending button { margin: 4px; }
.story-chat[data-size="small"] { --chat-font: .9rem; }
.story-chat[data-size="large"] { --chat-font: 1.16rem; }
.story-chat[data-size="largest"] { --chat-font: 1.32rem; }
.story-chat[data-fit="contain"] .chat-art img { object-fit: contain; }
.story-chat[data-mode="chat"] { background: #14151d; }
.story-chat[data-mode="chat"] .chat-feed {
  background: #14151ded; border-radius: 24px 24px 0 0;
}
.story-chat[data-mode="chat"] .chat-message-image { display: block; max-width: 100%; margin: 6px auto 18px; }
.chat-message-image img { width: 100%; max-height: 500px; object-fit: contain; border-radius: 22px; }
.story-chat.art-only .chat-feed, .story-chat.art-only .chat-footer, .story-chat.art-only .chat-latest { visibility: hidden; }
.story-chat.art-only .chat-art { opacity: 1; }
.story-chat.art-only .chat-shade { opacity: .3; }
.story-chat:has(.chat-panel:not([hidden])) .chat-footer { pointer-events: none; }
.story-chat.compact .chat-feed { margin-top: calc(var(--stage-height, 100dvh) * .04); min-height: 50px; }
.story-chat[data-mode="chat"].compact .chat-feed {
  margin-top: clamp(12px, calc((var(--stage-height, 100dvh) - 400px) * .7), 110px);
}
.story-chat.compact .chat-footer { max-height: 60%; }
@media (min-width: 800px) and (min-height: 600px) {
  .story-chat { width: 520px; left: 50%; right: auto; transform: translateX(-50%); box-shadow: 0 0 100px #0009; }
}
@media (max-height: 600px) {
  .chat-feed { margin-top: 4dvh; min-height: 50px; padding-top: 12px; }
  .chat-footer { max-height: 60%; }
  .chat-top { padding-top: 8px; }
  .chat-panel { top: 12px; bottom: 12px; }
}
@media (min-aspect-ratio: 3/2) and (max-height: 600px) {
  .chat-feed, .chat-footer { width: 53%; margin-left: auto; box-sizing: border-box; background-color: #0c102580; }
  .chat-art img { object-position: 25% 30% !important; }
  .chat-eye { left: 20px; right: auto; }
  .story-chat[data-story-mode="freeform"] .chat-choices { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .story-chat[data-story-mode="freeform"] .chat-choice { padding: 8px 10px; }
}
@media (prefers-reduced-motion: reduce) { .story-chat * { scroll-behavior: auto !important; } }
