:root {
  --ink: #2a1f2d; --paper: #fbf8ef; --blue: #3d7fd1; --blue-l: #9fc6f0; --red: #e85a5a; --gold: #ffd34d;
  --muted: #6b6474; --ok: #2f8a52; --bad: #c0392b;
  --pix: ui-rounded, "SF Pro Rounded", "Segoe UI Variable Display", "Segoe UI", system-ui, sans-serif;
  --ease: cubic-bezier(.2,.8,.2,1);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #8fd0f5; color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
#game { position: fixed; inset: 0; }
#game canvas { display: block; touch-action: none; }
.loading { position: fixed; inset: 0; display: grid; place-items: center; background: linear-gradient(#8fd0f5, #bfe6c0); z-index: 3; }
.loading p { font: 800 18px var(--pix); background: var(--paper); border: 3px solid var(--ink); border-radius: 14px; padding: .8rem 1.2rem; }
button, input, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* Ventanas con doble borde, estilo RPG portátil */
.win { background: rgba(255,255,255,.97); border: 4px solid #2f6fd6; border-radius: 20px;
  box-shadow: 0 0 0 3px #fff, 0 10px 30px rgba(20,40,90,.3); }
.win-head { display: flex; justify-content: space-between; align-items: start; gap: .6rem; padding: .9rem 1rem .5rem; }
.win-head h2, .card h2 { font-family: var(--pix); font-weight: 800; font-size: 17px; line-height: 1.3; margin: 0; letter-spacing: .02em; }
.win-head p { margin: .35rem 0 0; font-size: 13px; }
button.px { font-family: var(--pix); font-weight: 800; font-size: 13px; letter-spacing: .02em; background: #fff; color: var(--ink);
  border: 3px solid var(--ink); border-radius: 999px; padding: .55rem .7rem; min-height: 40px; cursor: pointer;
  box-shadow: 0 3px 0 var(--ink); transition: transform .12s var(--ease), box-shadow .12s var(--ease); }
button.px:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
button.px.primary { background: var(--gold); }
button.px:disabled { opacity: .55; cursor: default; }

/* HUD */
.hud { position: fixed; top: max(8px, env(safe-area-inset-top)); left: 8px; right: 8px; display: flex; justify-content: space-between; gap: 8px; pointer-events: none; z-index: 5; flex-wrap: wrap; }
.hud > * { pointer-events: auto; }
.hud-box { background: rgba(255,255,255,.92); border: 3px solid #2f6fd6; border-radius: 14px; padding: .45rem .6rem; box-shadow: 0 3px 0 rgba(0,0,0,.25); }
.hud-box p { margin: 0; font-family: var(--pix); font-weight: 800; font-size: 12px; line-height: 1.6; }
.hud-box .hud-small { font-family: inherit; font-size: 11px; color: var(--muted); line-height: 1.4; }
.hud-box .hud-small.stale { color: var(--bad); font-weight: 600; }
.hud-btns { display: flex; gap: 6px; align-items: start; flex-wrap: wrap; justify-content: flex-end; }
@media (max-width: 500px) { .hud-btns button.px { font-size: 8px; padding: .45rem .5rem; min-height: 36px; } .hud-box .hud-small { font-size: 10px; } }
.zone { position: fixed; top: 92px; left: 8px; margin: 0; font-family: var(--pix); font-weight: 800; font-size: 14px; background: var(--paper);
  border: 3px solid var(--ink); border-radius: 6px; padding: .6rem .8rem; transform: translateX(-120%); z-index: 4; pointer-events: none; }
.zone.show { animation: zone 2.6s var(--ease) forwards; }
@keyframes zone { 0% { transform: translateX(-120%); } 12%, 82% { transform: translateX(0); } 100% { transform: translateX(-120%); } }

/* Diálogo */
.dialog { position: fixed; left: 0; right: 0; bottom: max(8px, env(safe-area-inset-bottom)); display: flex; justify-content: center; padding: 0 8px; z-index: 8; }
.d-box { width: min(760px, 100%); background: rgba(255,255,255,.97); border: 4px solid #2f6fd6; border-radius: 22px;
  box-shadow: 0 0 0 3px #fff, 0 10px 30px rgba(20,40,90,.35); padding: 1rem 1.1rem;
  display: grid; grid-template-columns: 1fr auto; gap: .8rem; align-items: start; animation: pop .16s var(--ease); }
@keyframes pop { from { transform: translateY(10px); opacity: 0; } }
#d-text { margin: 0; font-family: var(--pix); font-weight: 700; font-size: 18px; line-height: 1.55; min-height: 2.4em; }
.d-menu { display: grid; gap: 4px; border-left: 3px solid var(--ink); padding-left: .7rem; }
.d-menu button { font-family: var(--pix); font-weight: 800; font-size: 15px; background: none; border: 0; text-align: left; padding: .45rem .2rem .45rem 1.1rem; cursor: pointer; position: relative; min-height: 34px; }
.d-menu button.sel::before { content: "▶"; position: absolute; left: 0; top: .45rem; font-size: 9px; }
@media (max-width: 520px) { .d-box { grid-template-columns: 1fr; } .d-menu { border-left: 0; border-top: 3px solid var(--ink); padding: .5rem 0 0; grid-template-columns: 1fr 1fr; } }

/* Cruceta y botón A (solo pantallas táctiles) */
.dpad, .btn-a, .btn-fly { display: none; }
@media (pointer: coarse) {
  .dpad { display: grid; position: fixed; left: 14px; bottom: calc(18px + env(safe-area-inset-bottom)); width: 138px; height: 138px;
    grid-template: repeat(3, 1fr) / repeat(3, 1fr); z-index: 6; opacity: .88; touch-action: none; }
  .dpad button { background: var(--paper); border: 3px solid var(--ink); border-radius: 8px; font-size: 16px; color: var(--ink); touch-action: none; user-select: none; }
  .dpad button:active { background: var(--gold); }
  .d-up { grid-area: 1 / 2; } .d-left { grid-area: 2 / 1; } .d-right { grid-area: 2 / 3; } .d-down { grid-area: 3 / 2; }
  .btn-a { display: block; position: fixed; right: 18px; bottom: calc(40px + env(safe-area-inset-bottom)); width: 68px; height: 68px; border-radius: 50%;
    background: var(--red); color: #fff; border: 3px solid var(--ink); font-family: var(--pix); font-weight: 900; font-size: 22px; box-shadow: 0 4px 0 var(--ink); z-index: 6; opacity: .92; }
  .btn-a:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
  .btn-fly { display: block; position: fixed; right: 26px; bottom: calc(124px + env(safe-area-inset-bottom)); width: 58px; height: 58px; border-radius: 50%;
    background: #f4c64e; color: #5a1010; border: 3px solid var(--ink); font: 900 11px var(--pix); box-shadow: 0 4px 0 var(--ink); z-index: 6; opacity: .95; }
  .btn-fly[aria-pressed="true"] { background: #c81821; color: #fff; }
  .hud-btns #btn-fly { display: none; }
  body:has(#dialog:not([hidden])) .dpad, body:has(#dialog:not([hidden])) .btn-a,
  body:has(#panel:not([hidden])) .dpad, body:has(#panel:not([hidden])) .btn-a,
  body:has(#dialog:not([hidden])) .btn-fly, body:has(#panel:not([hidden])) .btn-fly { display: none; }
}

/* Panel del bot */
.panel { position: fixed; right: 10px; top: 10px; bottom: 10px; width: min(460px, calc(100vw - 20px)); display: flex; flex-direction: column; z-index: 10; animation: slide .2s var(--ease); }
@keyframes slide { from { transform: translateX(20px); opacity: 0; } }
@media (max-width: 700px) {
  .panel { left: 6px; right: 6px; width: auto; top: auto; height: 84dvh; bottom: 6px; animation-name: rise; }
  @keyframes rise { from { transform: translateY(20px); opacity: 0; } }
}
@media (prefers-reduced-motion: reduce) { .panel, .d-box, .zone.show { animation: none; } .zone.show { transform: none; } }
.tabs { display: flex; gap: 6px; padding: 0 1rem .6rem; }
.tabs button { flex: 1; font-family: var(--pix); font-weight: 800; font-size: 13px; background: transparent; border: 3px solid transparent; border-radius: 6px; padding: .55rem .2rem; cursor: pointer; color: var(--muted); min-height: 38px; }
.tabs button[aria-selected="true"] { color: var(--ink); border-color: var(--ink); background: #fff; }
.pane { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 0 1rem 1rem; overflow: auto; }
.log { list-style: none; margin: 0; padding: .2rem; flex: 1; overflow: auto; display: flex; flex-direction: column; gap: .5rem; }
.log li { padding: .55rem .75rem; border-radius: 8px; white-space: pre-wrap; word-break: break-word; max-width: 92%; border: 2px solid var(--ink); }
.log .user { align-self: flex-end; background: #dcebff; }
.log .assistant { align-self: flex-start; background: #fff; }
.log .tool { align-self: flex-start; font-size: 12px; color: var(--muted); border: 0; padding: 0 .2rem; }
.log .error { align-self: stretch; color: var(--bad); font-size: 13px; border-color: var(--bad); }
.composer { display: flex; gap: .5rem; margin-top: .6rem; align-items: end; }
textarea, input { background: #fff; border: 3px solid var(--ink); border-radius: 6px; padding: .55rem .7rem; width: 100%; }
.composer textarea { resize: none; min-height: 46px; max-height: 160px; }
.row { display: flex; gap: .5rem; margin-bottom: .3rem; }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .45rem; }
.list li { border: 2px solid var(--ink); border-radius: 8px; padding: .55rem .7rem; display: flex; gap: .6rem; align-items: center; background: #fff; }
.list li .grow { flex: 1; min-width: 0; }
.list li .t { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list li.current { border-color: var(--blue); box-shadow: inset 0 0 0 2px var(--blue-l); }
.info { display: grid; grid-template-columns: auto 1fr; gap: .35rem .8rem; margin: 0 0 1rem; }
.info dt { color: var(--muted); }
.info dd { margin: 0; word-break: break-word; }
h3 { font-family: var(--pix); font-weight: 800; font-size: 12px; margin: .3rem 0 .6rem; color: var(--muted); }
.pill { font-size: 12px; padding: .1rem .5rem; border-radius: 99px; border: 2px solid var(--ink); white-space: nowrap; }
.pill.ok { color: var(--ok); } .pill.bad { color: var(--bad); }
.err { color: var(--bad); min-height: 1.2em; margin: .3rem 0; font-size: 14px; }

/* Modales */
.modal { position: fixed; inset: 0; background: rgba(20,24,40,.55); display: grid; place-items: center; z-index: 20; padding: 1rem; }
.modal > .win { width: min(420px, 100%); max-height: 86dvh; display: flex; flex-direction: column; }
.goto-list { list-style: none; margin: 0; padding: 0 1rem 1rem; overflow: auto; }
.goto-list .goto-h { font-family: var(--pix); font-weight: 800; font-size: 12px; color: var(--muted); padding: .8rem 0 .3rem; }
.goto-list button { width: 100%; text-align: left; background: #fff; border: 2px solid var(--ink); border-radius: 6px; padding: .5rem .6rem; margin: .2rem 0; cursor: pointer; display: flex; align-items: center; gap: .4rem; min-height: 40px; }
.goto-list button:hover { background: #fff6d6; }
.goto-list small { margin-left: auto; color: var(--muted); }
.dot { width: 10px; height: 10px; border-radius: 2px; border: 2px solid var(--ink); flex: none; }
.s-trabajando { background: #4fc27a; } .s-esperando { background: var(--gold); } .s-error { background: var(--red); } .s-inactivo { background: #dfe6f5; } .s-sin_conexion { background: #9aa0aa; }
.card { padding: 1.2rem; display: flex; flex-direction: column; gap: .45rem; }
.card button { margin-top: .4rem; }

/* Bemia dentro del juego */
.bemia { position: fixed; inset: 8px; z-index: 15; display: flex; flex-direction: column; background: #fff; border: 4px solid #2f6ff6; border-radius: 18px; overflow: hidden; box-shadow: 0 0 0 3px #fff, 0 12px 40px rgba(20,40,90,.4); animation: pop .2s var(--ease); }
.bemia-head { display: flex; align-items: center; gap: .6rem; padding: .5rem .7rem; background: linear-gradient(90deg, #2f6ff6, #4f8dff); color: #fff; }
.bemia-head h2 { margin: 0; font: 900 16px var(--pix); letter-spacing: .04em; flex: 1; }
.bemia-head .px { min-height: 36px; }
.px-link { color: #fff; font: 800 13px var(--pix); text-decoration: underline; padding: .4rem; }
#bemia-frame { flex: 1; width: 100%; border: 0; background: #f4f7fb; }
@media (max-width: 520px) { .bemia { inset: 0; border-radius: 0; border-width: 0; } }
#btn-fly[aria-pressed="true"] { background: #c81821; color: #fff; }
