:root {
  color-scheme: dark;
  --bg: #081116;
  --fg: #eee5d3;
  --muted: #a0aaa8;
  --border: #2a3a40;
  --surface: #111e25;
  --accent: #dea66b;
  --accent-ink: #201a14;
  --mono: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; height: 100%; }
body { background: var(--bg); color: var(--fg); font: 14px/1.5 "Segoe UI", system-ui, sans-serif; overflow: hidden; }
button, input, select { font: inherit; }
button, select, summary, input { -webkit-tap-highlight-color: transparent; }
button, select { color: inherit; }
button, .import-save {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  min-height: 40px; padding: 9px 14px;
  background: transparent; border: 1px solid var(--border); border-radius: 3px;
  color: var(--fg); cursor: pointer;
  transition: background-color 140ms, color 140ms, border-color 140ms;
}
button:hover, .import-save:hover { background: #1b2b32; border-color: #64716f; }
button:active { background: #25363d; }
button:disabled { opacity: .4; cursor: default; }
button:disabled:hover { background: transparent; border-color: var(--border); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.primary:hover { background: #efbd87; border-color: #efbd87; }
select { min-height: 40px; max-width: 100%; padding: 8px 28px 8px 11px; border: 1px solid var(--border); border-radius: 3px; background: var(--surface); cursor: pointer; }
kbd { color: var(--muted); font: 11px/1.6 var(--mono); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
[hidden] { display: none !important; }

#app { display: flex; flex-direction: column; height: 100%; height: 100dvh; }
.bar {
  position: relative; z-index: 2;
  display: flex; flex: 0 0 auto; align-items: center; gap: 20px;
  min-height: 76px; padding: 14px 28px;
  border-bottom: 1px solid var(--border); background: #0b151c;
  animation: shell-in 300ms ease-out both;
}
.bar h1 { display: flex; align-items: center; gap: 12px; margin: 0; font-size: 18px; line-height: 1.2; letter-spacing: .035em; font-weight: 650; white-space: nowrap; }
.brand-mark { width: 24px; height: 28px; color: var(--accent); flex-shrink: 0; shape-rendering: crispEdges; }
.bar h1 small { display: block; margin-top: 6px; color: var(--muted); font: 10px/1.2 var(--mono); letter-spacing: .075em; text-transform: uppercase; }
.bar-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }
.look-label select { font-size: 12px; letter-spacing: .02em; }
.saves-button { margin-left: 8px; font-size: 12px; }
.saves-button kbd { padding-left: 8px; border-left: 1px solid var(--border); }
.control-divider { width: 1px; height: 20px; margin: 0 6px; background: var(--border); }
.icon-button { display: inline-flex; width: 38px; min-height: 38px; padding: 9px; align-items: center; justify-content: center; list-style: none; border: 1px solid transparent; border-radius: 3px; cursor: pointer; color: var(--muted); }
.icon-button svg { display: block; width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: square; stroke-linejoin: miter; }
.icon-button:hover, .icon-button[aria-pressed="true"], .help-menu[open] > summary { background: #1b2b32; color: var(--accent); border-color: var(--border); }
.icon-button[aria-pressed="true"] { box-shadow: inset 0 -2px var(--accent); }
summary::-webkit-details-marker { display: none; }
.help-menu { position: relative; }
.help-panel { position: absolute; top: calc(100% + 16px); right: 0; width: 300px; padding: 24px; background: var(--surface); border: 1px solid var(--border); box-shadow: 0 16px 40px #0006; animation: menu-in 160ms ease-out both; }
.help-panel h2 { margin: 0 0 12px; font-size: 16px; font-weight: 600; }
.help-panel p { color: var(--muted); font-size: 13px; margin: 0 0 20px; }
.help-panel dl { margin: 0 0 20px; font-size: 12px; }
.help-panel dl div { display: flex; justify-content: space-between; gap: 16px; padding: 6px 0; }
.help-panel dd { margin: 0; }
.check { display: flex; align-items: center; gap: 10px; padding-top: 18px; border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); cursor: pointer; }
input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }

main { flex: 1; display: flex; min-height: 0; }
.stage { position: relative; flex: 1; display: flex; align-items: center; justify-content: center; min-width: 0; padding: 16px 20px; overflow: hidden; background: #050b10; }
.frame { position: relative; display: block; flex: 0 0 auto; line-height: 0; animation: scene-in 350ms 80ms ease-out both; }
#screen { display: block; max-width: 100%; max-height: 100%; image-rendering: pixelated; background: #000; cursor: crosshair; touch-action: manipulation; }
#screen:focus-visible { outline-offset: 2px; }
.scanlines { position: absolute; inset: 0; pointer-events: none; mix-blend-mode: multiply; opacity: .35; background: repeating-linear-gradient(to bottom, #0008 0 1px, transparent 1px 3px); }
.status-bar { display: flex; flex: 0 0 auto; align-items: center; justify-content: space-between; gap: 24px; min-height: 36px; padding: 8px 28px; border-top: 1px solid var(--border); background: #0b151c; color: var(--muted); font-size: 11px; }
.stage-hint { color: #b7bcb5; }
.status { text-align: right; }
#app:fullscreen .bar { min-height: 64px; }
#app:fullscreen .stage { padding: 0; }

dialog { width: 520px; max-width: calc(100vw - 32px); max-height: calc(100dvh - 32px); padding: 0; overflow-y: auto; background: var(--surface); color: var(--fg); border: 1px solid #465451; border-radius: 3px; box-shadow: 0 24px 80px #0009; }
dialog[open] { animation: menu-in 180ms ease-out both; }
dialog::backdrop { background: #02080ddd; }
dialog form { padding: 32px; }
.dialog-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; margin-bottom: 28px; }
.dialog-heading .eyebrow { display: block; margin-bottom: 8px; font: 10px/1.4 var(--mono); color: var(--accent); letter-spacing: .16em; text-transform: uppercase; }
dialog h2 { margin: 0; font-size: 27px; line-height: 1.2; font-weight: 550; letter-spacing: -.02em; }
.dialog-close { margin-top: -8px; margin-right: -8px; font-size: 24px; font-weight: 300; line-height: 1; }
.save-label { display: flex; flex-direction: column; gap: 9px; color: var(--muted); font-size: 12px; }
.save-label select { width: 100%; min-height: 46px; color: var(--fg); font-size: 14px; background: #0b171e; }
.save-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.save-note { margin: 14px 0 0; color: var(--muted); font-size: 12px; }
.save-transfer { margin-top: 26px; padding-top: 20px; border-top: 1px solid var(--border); }
.save-transfer summary { display: flex; justify-content: space-between; align-items: center; gap: 16px; color: var(--muted); font-size: 12px; cursor: pointer; list-style: none; }
.save-transfer summary::after { content: '+'; color: var(--accent); font: 18px var(--mono); }
.save-transfer[open] summary::after { content: '−'; }
.save-transfer[open] .transfer-actions { animation: menu-in 160ms ease-out both; }
.transfer-actions { display: flex; flex-direction: column; align-items: stretch; gap: 12px; margin-top: 16px; }
.import-save { flex-direction: column; align-items: flex-start; justify-content: flex-start; gap: 8px; cursor: default; font-size: 12px; color: var(--muted); }
.import-save input { width: 100%; max-width: 100%; font-size: 12px; color: var(--muted); }
.import-save input::file-selector-button { margin-right: 10px; padding: 6px 10px; background: #25363d; color: var(--fg); border: 0; border-radius: 2px; cursor: pointer; }
#save-result { min-height: 20px; margin: 18px 0; color: var(--accent); font-size: 13px; }
.resume-button { width: 100%; border-color: transparent; background: #1d2d34; }

@keyframes shell-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }
@keyframes scene-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes menu-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

@media (max-width: 850px) {
  .bar { padding: 12px 16px; gap: 12px; min-height: 68px; }
  .bar h1 { font-size: 16px; gap: 9px; }
  .bar h1 small { font-size: 9px; }
  .bar-actions { gap: 3px; }
  .saves-button { margin-left: 3px; }
  .saves-button kbd, .control-divider { display: none; }
  .icon-button { width: 34px; min-height: 38px; padding: 8px; }
  .status-bar { padding: 7px 16px; }
  .stage-hint { display: none; }
  .status { width: 100%; text-align: center; }
}
@media (max-width: 620px) {
  .bar { flex-wrap: wrap; justify-content: space-between; padding: 12px; gap: 12px 8px; }
  .bar h1 { font-size: 16px; }
  .brand-mark { width: 20px; height: 24px; }
  .bar h1 small { display: none; }
  .bar-actions { gap: 4px; }
  .look-label select { width: 124px; min-height: 36px; padding-left: 8px; font-size: 11px; }
  .saves-button { min-height: 36px; padding: 7px 10px; }
  .bar-actions .icon-button { width: 32px; min-height: 36px; }
  .stage { padding: 8px; }
  .status-bar { min-height: 30px; font-size: 10px; }
  .help-panel { width: min(300px, calc(100vw - 24px)); top: calc(100% + 12px); }
  dialog form { padding: 24px; }
}
@media (max-width: 520px) {
  .bar h1 { width: 100%; justify-content: center; }
  .bar-actions { width: 100%; justify-content: center; margin: 0; }
  .help-menu { position: static; }
  .help-panel { top: calc(100% - 1px); right: 12px; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .bar { min-height: 44px; padding: 4px 12px; }
  .bar h1 { width: auto; font-size: 14px; }
  .bar h1 small, .status-bar { display: none; }
  .bar-actions { width: auto; margin-left: auto; }
  .brand-mark { width: 17px; height: 21px; }
  .stage { padding: 0 8px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* The title belongs to the game: one illustrated scene, one invitation. */
.title-screen {
  position: absolute; inset: 0; z-index: 1;
  display: flex; align-items: center;
  background: #06132b url('../art/title-mansion.png') center center / cover no-repeat;
  image-rendering: pixelated;
  overflow-y:auto;
}
.title-screen::before { content: ''; position: absolute; inset: 0; pointer-events: none; background: linear-gradient(90deg, #03102cb3, #03102c35 42%, transparent 65%); }
.title-content { position: relative; width: 45%; max-width: 660px; padding: 30px 0 50px clamp(32px, 6vw, 112px); animation: menu-in 500ms 120ms ease-out both; }
.title-edition { margin: 0 0 22px; color: #e6b77c; font-size: 10px; font-weight: 600; letter-spacing: .22em; text-transform: uppercase; }
#game-title { margin: 0; color: #f3e7c9; font: 500 clamp(44px, 5.8vw, 100px)/.91 Georgia, 'Times New Roman', serif; letter-spacing: -.045em; text-transform: uppercase; text-shadow: 0 3px #07142c; }
#game-title span { letter-spacing: -.035em; }
.title-logo { display:block; width:112%; max-width:none; height:auto; margin-left:-4%; image-rendering:pixelated; }
.title-line { margin: 20px 0 28px; color: #eee5d3; font-size: clamp(18px, 1.8vw, 25px); font-weight:600; line-height: 1.35; letter-spacing: -.015em; text-wrap:balance; white-space:pre-line; }
.title-actions { display: flex; flex-direction: row; flex-wrap:wrap; align-items: center; gap: 8px 20px; }
.title-line { white-space: pre-line; }
.title-language { display:flex; align-items:center; gap:14px; margin:0 0 22px; color:var(--muted); font-size:12px; }
.title-language select { min-width:130px; background:#0b192bd9; border-color:#78808066; }
.title-language { margin-bottom:10px; }
.title-voices { display:flex;align-items:center;gap:10px;margin:-8px 0 20px;font-size:12px;color:var(--text);cursor:pointer; }
.title-voices input { width:16px;height:16px;accent-color:var(--accent); }
.title-characters { display:flex; align-items:center; gap:14px; margin:0 0 10px; color:var(--muted); font-size:12px; }
.title-characters > span { width:100px; flex:0 0 100px; }
.title-characters select { width:154px; min-width:0; background:#0b192bd9; border-color:#78808066; }
.title-content { padding-top:20px; padding-bottom:20px; }
.title-edition { margin-bottom:16px; }
.title-line { margin:18px 0 24px; line-height:1.35; }
.title-actions { gap:8px 20px; }
.title-language > span, .title-music > label { width:100px; flex:0 0 100px; }
.title-music { display:flex; align-items:center; gap:14px; margin:0 0 22px; color:var(--muted); font-size:12px; }
.title-music select { width:154px; min-width:0; background:#0b192bd9; border-color:#78808066; }
.title-music small, .music-setting small { display:block; margin-top:3px; font-size:10px; font-weight:400; color:var(--muted); white-space:nowrap; }
.title-music button { min-height:36px; width:36px; padding:9px; flex:0 0 36px; border-color:#78808066; background:#0b192bd9; }
.title-music svg { width:16px; height:16px; fill:currentColor; }
.title-music .stop-preview, .title-music [aria-pressed="true"] .play-preview { display:none; }
.title-music [aria-pressed="true"] .stop-preview { display:block; }
.audio-status { margin:-10px 0 18px; max-width:360px; font-size:12px; line-height:1.5; color:#edb87c; }
.music-setting { display:flex; justify-content:space-between; align-items:center; gap:12px; padding-top:18px; font-size:12px; color:var(--muted); }
.music-setting select { max-width:180px; font-size:12px; }
.language-label select { font-size:12px; min-width:95px; }
.at-title .language-label { display:none; }
.title-actions button { min-height: 48px; padding: 12px 20px; font-size: 13px; }
.title-actions .primary { gap: 24px; }
.title-actions .primary span[aria-hidden] { font-size: 20px; line-height: 1; transition: transform 140ms; }
.title-actions .primary:hover span[aria-hidden] { transform: translateX(3px); }
#title-saves { border-color: transparent; padding-left: 0; color: #c7d0d1; background: transparent; }
#title-saves:hover { color: var(--accent); }
.title-caption { position: absolute; bottom: 28px; right: 32px; color: #c1c8c8; font-size: 10px; letter-spacing: .035em; text-shadow: 0 1px #000; }
.title-notice { margin:12px 0 0; font-size:11px; line-height:1.5; color:#c0cad0; }
.title-notice a, .help-credits { color:var(--accent); text-underline-offset:3px; }
.title-notice a { display:inline-block; padding:6px 0; margin-left:5px; }
.help-credits { display:block; margin-top:18px; padding:12px 0 0; border-top:1px solid var(--border); font-size:12px; }
.at-title .bar { border-bottom-color: #ffffff0f; background: #06142b; }
.at-title .status-bar, .at-title #saves, .at-title #pause, .at-title .control-divider { display: none; }
.at-title .frame { visibility: hidden; }
@media (max-width: 850px) {
  .title-content { width: 50%; padding-left: 32px; }
  .title-edition { font-size: 9px; letter-spacing: .14em; }
  .title-actions button { font-size: 12px; }
  .title-caption { right: 20px; bottom: 20px; font-size: 9px; }
}
@media (max-width: 620px) {
  .title-screen { background-position: 63% center; align-items: flex-end; }
  .title-screen::before { background: linear-gradient(0deg, #03102cf2, #03102c99 48%, #03102c15); }
  .title-content { width: 100%; padding: 28px 24px; }
  #game-title { font-size: clamp(46px, 12vw, 72px); }
  .title-logo { width:100%; margin-left:0; }
  .title-edition { margin-bottom: 16px; }
  .title-line { margin: 16px 0 18px; font-size: 18px; line-height: 1.35; }
  .title-language { margin-bottom:14px; }
  .title-language, .title-music, .title-characters { gap:10px; }
  .title-actions { flex-direction: row; flex-wrap: wrap; align-items: center; gap: 8px 16px; }
  .title-actions button { min-height: 44px; padding: 10px 14px; }
  .title-caption { display: none; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .title-screen { align-items:flex-start; }
  .title-content { width: min(440px, 100%); padding: 12px 24px; }
  #game-title { font-size: clamp(36px, 8vh, 64px); }
  .title-logo { width:auto; height:min(18vh,88px); max-width:112%; object-fit:contain; object-position:left center; }
  .title-edition { margin-bottom: 12px; }
  .title-line { margin: 8px 0; font-size: 12px; line-height: 1.5; }
  .title-language, .title-music, .title-characters { gap:8px; margin-bottom:8px; font-size:11px; }
  .title-language > span, .title-music > label, .title-characters > span { width:92px; flex-basis:92px; }
  .title-language select, .title-music select, .title-characters select { min-height:30px; padding:4px 22px 4px 8px; }
  .title-music button { min-height:30px; height:30px; width:30px; flex-basis:30px; padding:6px; }
  .title-actions { flex-direction:row; flex-wrap:wrap; align-items:center; gap:8px 16px; }
  .title-actions button { min-height: 34px; padding: 6px 12px; }
  .title-caption { display: none; }
  .title-notice { margin-top:5px; font-size:10px; }
}
@media (max-height: 360px) and (orientation: landscape) {
  .title-edition { display:none; }
}
