:root {
  --bg: #0f1115;
  --panel: rgba(18, 20, 26, 0.86);
  --panel-solid: #16181e;
  --line: rgba(255, 255, 255, 0.09);
  --text: #e9e4d8;
  --muted: #a39d90;
  --accent: #d9a441;
  --canon: #8fc5c9;
  --warn: #e6b35e;
  --danger: #d8604f;
  --good: #7fbf7a;
  --serif: "EB Garamond", Georgia, "Times New Roman", serif;
  --title: "Cinzel", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font-family: var(--sans); font-size: 15px; }
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- Inicio ---------- */
#start { min-height: 100%; display: grid; place-items: center; padding: 24px 16px;
  background: radial-gradient(ellipse at top, #1d212b 0%, var(--bg) 70%); }
.start-box { width: min(560px, 100%); text-align: center; }
.start-box h1 { font-family: var(--title); font-size: clamp(36px, 8vw, 56px); letter-spacing: .08em; margin: 0 0 8px; color: var(--accent); }
.tagline { color: var(--muted); font-family: var(--serif); font-size: 19px; margin: 0 0 32px; }
.packs { display: grid; gap: 12px; }
.pack { text-align: left; background: var(--panel-solid); border: 1px solid var(--line); border-radius: 10px; padding: 16px 18px;
  transition: border-color .15s, transform .15s; }
.pack:focus-visible { border-color: var(--accent); outline: none; }
@media (hover: hover) { .pack:hover { border-color: var(--accent); transform: translateY(-1px); } }
.pack strong { display: block; font-family: var(--title); font-size: 17px; margin-bottom: 4px; }
.pack span { color: var(--muted); font-size: 13px; }
.error { color: var(--danger); margin-top: 16px; }

/* ---------- Partida ---------- */
#game { height: var(--app-h, 100dvh); display: grid; grid-template-columns: minmax(0, 1fr) 340px; }
.stage { position: relative; display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.bg { position: absolute; inset: 0; background: #1a1d24 center / cover no-repeat; transition: background-image .6s; }
.bg::after { content: ""; position: absolute; inset: 0;
  background: linear-gradient(180deg, rgba(10,11,14,.55) 0%, rgba(10,11,14,.05) 30%, rgba(10,11,14,.55) 60%, rgba(10,11,14,.94) 100%); }
.bg.empty { background-image: repeating-linear-gradient(135deg, #191c23 0 22px, #1c2028 22px 44px); }

.topbar { position: relative; display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; padding: 18px 24px; }
.pack-title { color: var(--muted); font-size: 12px; letter-spacing: .12em; text-transform: uppercase; }
.loc-name { font-family: var(--title); font-size: clamp(22px, 3vw, 32px); margin: 4px 0 0; text-shadow: 0 2px 12px rgba(0,0,0,.8); }
.meters { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
.meter { background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; font-size: 13px; white-space: nowrap; }
.meter.hot { border-color: var(--danger); color: #ffb3a7; }
.voice-menu { position: relative; }
.voice-menu summary { list-style: none; cursor: pointer; }
.voice-menu summary::-webkit-details-marker { display: none; }
.voice-menu[open] summary { border-color: var(--accent); }
.voice-panel { position: absolute; right: 0; top: calc(100% + 6px); z-index: 5; width: 240px; display: grid; gap: 10px;
  background: var(--panel-solid); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; font-size: 13px; }
.voice-panel label { display: grid; gap: 4px; }
.voice-panel label.check { display: flex; align-items: center; gap: 8px; }
.voice-panel input[type=range] { width: 100%; accent-color: var(--accent); }
.voice-hint { margin: 0; color: var(--muted); font-size: 12px; }
.log .spoken { cursor: pointer; }
@media (hover: hover) { .log .spoken:hover { text-decoration: underline dotted var(--line); text-underline-offset: 4px; } }

.log { position: relative; margin-top: auto; max-height: 58%; overflow-y: auto; padding: 8px 24px 12px;
  font-family: var(--serif); font-size: 19px; line-height: 1.5; scroll-behavior: smooth; }
.log > * { max-width: 780px; margin: 0 0 12px; text-shadow: 0 1px 6px rgba(0,0,0,.9); white-space: pre-line; }
.log .player { color: var(--accent); font-style: italic; }
.log .player::before { content: "› "; }
.log .canon { color: var(--canon); font-family: var(--sans); font-size: 14px; }
.log .location { color: var(--muted); font-size: 16px; border-left: 2px solid var(--line); padding-left: 12px; }
.log .warning { color: var(--warn); }
.log .system, .log .debug { font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; line-height: 1.45;
  background: rgba(0,0,0,.55); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; text-shadow: none; }
.log .debug { color: #c9b6e8; }
.log .intro { font-size: 20px; }
.log .goal { color: var(--accent); font-family: var(--sans); font-size: 14px; }
.log .thinking { color: var(--muted); }
.log .thinking::after { content: "…"; animation: dots 1s steps(3, end) infinite; display: inline-block; width: 1em; overflow: hidden; vertical-align: bottom; }
@keyframes dots { from { width: 0; } to { width: 1em; } }

.action { position: relative; display: flex; gap: 8px; padding: 12px 24px 20px; }
.action input { flex: 1; min-width: 0; background: rgba(12,13,17,.88); border: 1px solid var(--line); border-radius: 10px;
  padding: 14px 16px; color: var(--text); font-family: var(--serif); font-size: 18px; }
.action input:focus { outline: none; border-color: var(--accent); }
.action button { background: var(--accent); color: #1a1406; border: 0; border-radius: 10px; padding: 0 20px; font-weight: 600; }
.action button:disabled, .action input:disabled { opacity: .5; cursor: progress; }

/* ---------- Panel lateral (cajón en el móvil) ---------- */
.sheet-toggle, .sheet-handle, .sheet-backdrop { display: none; }
.side { background: var(--panel-solid); border-left: 1px solid var(--line); overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 18px; }
.side h3 { font-family: var(--title); font-size: 12px; letter-spacing: .14em; text-transform: uppercase; color: var(--muted); margin: 0 0 8px; font-weight: 500; }

.portrait { position: relative; aspect-ratio: 2 / 3; border-radius: 8px; overflow: hidden; background: #22262f center / cover no-repeat;
  border: 1px solid var(--line); cursor: zoom-in; }
.frame { display: grid; place-items: center; text-align: center; padding: 6px; cursor: default;
  background: repeating-linear-gradient(135deg, #1f232b 0 10px, #232830 10px 20px); color: var(--muted); font-family: var(--title); font-size: 13px; }
.frame b { display: block; font-size: 26px; color: var(--text); opacity: .6; }

.player { display: grid; grid-template-columns: 96px 1fr; gap: 12px; align-items: center; }
.player .name { font-family: var(--title); font-size: 18px; margin-bottom: 8px; }
.bar { height: 8px; background: #2a2e37; border-radius: 99px; overflow: hidden; margin-bottom: 8px; }
.bar div { height: 100%; background: var(--good); transition: width .4s, background .4s; }
.stats { color: var(--muted); font-size: 13px; }

.people { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 10px; }
.person .portrait { transition: box-shadow .3s, filter .3s; }
.person.highlighted .portrait { box-shadow: 0 0 0 2px var(--accent), 0 0 18px rgba(217,164,65,.35); }
.person.dimmed .portrait { filter: grayscale(1) brightness(.55); }
.person .label { font-size: 13px; margin-top: 4px; line-height: 1.25; }
.person .tag { display: inline-block; font-size: 11px; color: var(--warn); }

.things { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 8px; }
.thing { font-size: 11.5px; line-height: 1.2; color: var(--muted); }
.thing .icon { aspect-ratio: 1; border-radius: 8px; border: 1px solid var(--line); background: #22262f center / cover no-repeat; margin-bottom: 4px; cursor: zoom-in; }
.thing .icon.frame { font-size: 20px; cursor: default; }
.empty-note { color: var(--muted); font-size: 13px; font-style: italic; }

.exits { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.exits li { font-size: 13.5px; }
.exits li span { color: var(--muted); }

.commands { margin-top: auto; display: flex; flex-wrap: wrap; gap: 6px; }
.commands button { background: transparent; border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; font-size: 12.5px; color: var(--muted); }
@media (hover: hover) { .commands button:hover { color: var(--text); border-color: var(--muted); } }

/* ---------- Final y lightbox ---------- */
/* Si no cabe de alto, se desplaza sin cortar la parte de arriba. */
.overlay { position: fixed; inset: 0; background: rgba(5,6,8,.88); display: grid; place-items: center; place-items: safe center; overflow-y: auto;
  padding: max(24px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); z-index: 10; }
.ending-box { width: min(900px, 100%); text-align: center; }
.ending-image { aspect-ratio: 3 / 2; border-radius: 12px; border: 1px solid var(--line); background: #1a1d24 center / cover no-repeat; margin-bottom: 20px; }
.ending-image.frame { font-size: 18px; }
.ending-box h2 { font-family: var(--title); font-size: clamp(28px, 5vw, 42px); margin: 0 0 8px; letter-spacing: .08em; }
.ending-box h2.victory { color: var(--accent); }
.ending-box h2.defeat { color: var(--danger); }
.ending-box p { font-family: var(--serif); font-size: 20px; max-width: 640px; margin: 0 auto 24px; }
.ending-box button { background: var(--accent); color: #1a1406; border: 0; border-radius: 10px; padding: 12px 28px; font-weight: 600; }
.lightbox { cursor: zoom-out; }
.lightbox img { max-width: 100%; max-height: 82vh; border-radius: 10px; }
.lightbox p { font-family: var(--title); text-align: center; margin: 12px 0 0; }

/* ---------- Mapa ---------- */
.map-box { width: min(1180px, 100%); max-height: 100%; display: flex; flex-direction: column; gap: 10px;
  background: var(--panel-solid); border: 1px solid var(--line); border-radius: 12px; padding: 16px 20px; }
.map-box header { display: flex; align-items: center; gap: 16px; }
.map-box h2 { font-family: var(--title); font-size: 22px; margin: 0; }
.map-box header > button { margin-left: auto; background: none; border: 0; font-size: 26px; line-height: 1; color: var(--muted); }
@media (hover: hover) { .map-box header > button:hover { color: var(--text); } }
.map-decks { display: flex; flex-wrap: wrap; gap: 6px; }
.map-decks button { background: transparent; border: 1px solid var(--line); border-radius: 999px; padding: 3px 12px; font-size: 12.5px; color: var(--muted); }
.map-decks button.active { border-color: var(--accent); color: var(--text); }
.map-scroll { min-height: 0; }
#map-svg { display: block; width: 100%; height: auto; max-height: 70vh; }
.map-hint, .map-legend { margin: 0; color: var(--muted); font-size: 12.5px; }
.map-legend { display: flex; flex-wrap: wrap; gap: 18px; }
.map-legend .open { color: var(--accent); }
.map-legend .blocked { color: var(--danger); }
.map-dark { fill: #07080b; }
.map-plan { filter: brightness(1.9) contrast(1.05); }
.map-coords { margin: 0; font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; color: #c9b6e8; }
.map-edge { fill: none; stroke-linecap: round; }
.map-edge.remembered { stroke: var(--muted); stroke-width: 2; opacity: .75; }
.map-edge.open { stroke: var(--accent); stroke-width: 3.5; }
.map-edge.blocked { stroke: var(--danger); stroke-width: 2.5; stroke-dasharray: 6 5; }
.map-arrow { fill: var(--accent); }
.map-lock { fill: var(--danger); stroke: var(--danger); stroke-width: 2; }
.map-lock path { fill: none; }
.map-node rect { fill: #1f232c; stroke: var(--line); stroke-width: 1.5; }
.map-node.current rect { stroke: var(--accent); stroke-width: 2.5; fill: #2a2619; }
.map-node circle { fill: #1f232c; stroke: var(--text); stroke-width: 2.5; }
.map-node.current circle { fill: var(--accent); stroke: #fff3d6; }
.map-node text, .map-label text { text-anchor: middle; fill: var(--text); font-family: var(--sans); }
.map-name { font-size: 13px; font-weight: 600; }
.map-npcs { font-size: 11px; fill: var(--canon) !important; }
.map-here { font-size: 11px; fill: var(--accent) !important; letter-spacing: .08em; text-transform: uppercase; font-weight: 700; }
.map-label rect { fill: rgba(10, 11, 15, .82); stroke: var(--line); }
.map-label text { font-size: 11.5px; font-weight: 600; }
.map-label text.sub { font-size: 10.5px; font-weight: 400; fill: var(--muted); }
.map-label.open text { fill: var(--accent); }
.map-label.blocked text:not(.sub) { fill: #ffb3a7; }
.map-label.place rect { fill: rgba(10, 11, 15, .72); stroke: none; }

/* ---------- Tablet ---------- */
@media (min-width: 700px) and (max-width: 1099px) {
  #game { grid-template-columns: minmax(0, 1fr) 280px; }
  .side { padding: 14px; gap: 16px; }
  .player { grid-template-columns: 76px 1fr; }
  .people { grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); }
  .topbar { padding: 14px 18px; }
  .log { padding: 8px 18px 12px; font-size: 18px; }
  .action { padding: 10px 18px 16px; }
}

/* ---------- Móvil ---------- */
@media (max-width: 699px) {
  #game { display: block; }
  .stage { height: 100%; }

  .topbar { flex-wrap: wrap; gap: 8px 12px;
    padding: calc(10px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left)); }
  .loc-name { font-size: 21px; }
  .meters { justify-content: flex-start; align-items: center; gap: 6px; }
  .meter { padding: 4px 10px; font-size: 12.5px; }
  .voice-menu { position: static; }
  .voice-panel { left: 12px; right: 12px; top: auto; width: auto; margin-top: 6px; z-index: 6; }

  /* La historia ocupa lo que quede entre la barra de arriba y la de acción, pegada abajo. */
  .log { flex: 0 1 auto; min-height: 0; max-height: none; font-size: 17px;
    padding: 8px max(16px, env(safe-area-inset-right)) 6px max(16px, env(safe-area-inset-left)); }
  .log .intro { font-size: 18px; }
  .log { -webkit-mask-image: linear-gradient(transparent, #000 20px); mask-image: linear-gradient(transparent, #000 20px); }

  .action { padding: 8px max(12px, env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
  .action input { padding: 12px 14px; font-size: 17px; } /* 16px o más: iOS no hace zoom al escribir */
  .action button { padding: 0 16px; }
  .action .sheet-toggle { position: relative; display: grid; place-items: center; width: 48px; flex: none; padding: 0;
    background: rgba(12,13,17,.88); border: 1px solid var(--line); color: var(--text); }
  .sheet-toggle svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; }
  .action .sheet-toggle[aria-expanded="true"] { border-color: var(--accent); color: var(--accent); }
  .sheet-toggle.news::after { content: ""; position: absolute; top: 7px; right: 7px; width: 9px; height: 9px; border-radius: 50%;
    background: var(--accent); box-shadow: 0 0 0 2px var(--bg); }

  /* Ficha: cajón que sube desde abajo. */
  .side { position: fixed; left: 0; right: 0; bottom: 0; z-index: 8; max-height: calc(var(--app-h, 100dvh) * .85);
    border-left: 0; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0; box-shadow: 0 -12px 40px rgba(0,0,0,.55);
    padding: 0 max(16px, env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    overscroll-behavior: contain; transform: translateY(100%); visibility: hidden; transition: transform .28s ease, visibility 0s .28s; }
  .side.open { transform: translateY(var(--drag, 0px)); visibility: visible; transition: transform .28s ease; }
  .side.dragging { transition: none; }
  .sheet-handle { display: block; position: sticky; top: 0; z-index: 1; margin: 0 -16px -6px; padding: 12px 0 10px;
    background: var(--panel-solid); touch-action: none; cursor: grab; }
  .sheet-handle::before { content: ""; display: block; width: 42px; height: 5px; margin: 0 auto; border-radius: 99px; background: var(--muted); opacity: .5; }
  .sheet-backdrop { display: block; position: fixed; inset: 0; z-index: 7; background: rgba(0,0,0,.5); }
  .player { grid-template-columns: 72px 1fr; }
  .people { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); }
  .commands { margin-top: 4px; }

  /* Mapa a pantalla completa, a su tamaño real y con desplazamiento. */
  .map-overlay { display: block; padding: 0; }
  .map-box { width: 100%; min-width: 0; height: var(--app-h, 100dvh); max-height: none; border: 0; border-radius: 0;
    padding: calc(8px + env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
  .map-box header { flex-wrap: wrap; gap: 8px 12px; }
  .map-box header > button { order: 2; }
  .map-decks { order: 3; width: 100%; }
  .map-scroll { flex: 1; overflow: auto; display: grid; place-items: safe center; border: 1px solid var(--line); border-radius: 8px; background: #0b0c10; }
  #map-svg { width: auto; max-width: none; height: auto; max-height: none; }
  .map-hint { display: none; }
  .map-legend { gap: 6px 14px; }

  .ending-box p { font-size: 18px; }
  .lightbox img { max-height: 75vh; }
}

/* ---------- Pantallas táctiles ---------- */
@media (pointer: coarse) {
  .commands { gap: 8px; }
  .commands button, .map-decks button { padding: 9px 14px; font-size: 14px; }
  .voice-menu summary.meter { padding: 8px 14px; }
  .voice-panel { gap: 14px; font-size: 14px; }
  .voice-panel label.check { min-height: 32px; }
  .voice-panel input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); }
  .map-box header > button { width: 44px; height: 44px; }
  .action button { min-height: 48px; }
}
