/* One Piece: Ecos de la Grand Line
   11.1 Paleta base: tinta, pergamino, oro y acento marino, con paneles
   tecnologicos en el nucleo. HP, EN y Tension se distinguen por etiqueta y
   forma ademas de color (10.5). */

:root {
  --ink: #18243A;
  --bg: #101722;
  --bg-deep: #0A0E14;
  --paper: #F2E3C6;
  --paper-dim: #C9B489;
  --gold: #D8A657;
  --sea: #4F97B3;
  --danger: #D65C59;
  --ok: #7ADFA0;
  --panel: #1B2432;
  --panel-2: #222E3E;
  --line: #2E3C50;

  --hp: #D65C59;
  --en: #4F97B3;
  --tension: #D8A657;

  --radius: 6px;
  --tap: 44px;
  --font: "Courier New", ui-monospace, SFMono-Regular, Menlo, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg-deep);
  color: var(--paper);
  font-family: var(--font);
  /* 10.5 La tipografia debe incluir tildes, ñ y signos españoles. */
  font-variant-ligatures: none;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

body {
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

#app {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 1040px;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
}

/* ------------------------------------------------------------------ banda HUD */

/* `display: flex` le gana al atributo hidden del navegador: hay que decirlo. */
#hud[hidden], #touch[hidden] { display: none !important; }

#hud {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: space-between;
  padding: 6px 10px;
  background: var(--ink);
  border-bottom: 2px solid var(--gold);
  font-size: 13px;
  min-height: 38px;
}

#hud-objective { display: flex; align-items: baseline; gap: 8px; min-width: 0; flex: 1 1 0; }
.hud-label {
  color: var(--gold);
  font-size: 10px;
  letter-spacing: 1px;
  text-transform: uppercase;
  flex: none;
}
#hud-objective-text {
  /* 10.2 El objetivo nunca ocupa mas de dos lineas. */
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.2;
}
#hud-right { display: flex; gap: 6px; align-items: center; flex: 0 1 auto; min-width: 0; }
#hud-right .hud-chip { white-space: nowrap; flex: none; }

.hud-chip {
  background: var(--panel-2);
  color: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 5px 9px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  min-height: 32px;
}
.hud-chip:hover:not(:disabled) { border-color: var(--gold); }
.hud-chip--static { cursor: default; }
#hud-save[data-state="error"] { color: var(--danger); border-color: var(--danger); }
#hud-save[data-state="saving"] { color: var(--gold); }

/* ---------------------------------------------------------------- escenario */

#stage {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background: var(--bg-deep);
  padding: 4px 0;
  /* El lienzo se adapta al hueco real: nunca empuja la pagina a scrollear. */
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

#screen {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  max-width: 100%;
}

#stage-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

/* ------------------------------------------------------------------- paneles */

#panel {
  flex: 0 0 auto;
  padding: 8px 10px 12px;
  background: var(--bg);
  max-height: 46dvh;
  overflow-y: auto;
}

.panel-title {
  color: var(--gold);
  font-size: 11px;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  margin: 0 0 6px;
}

/* Tarjetas de combate */
.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }

.card {
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: 6px;
  font-size: 12px;
  cursor: pointer;
  text-align: left;
  color: inherit;
  font-family: inherit;
  min-height: 92px;
  position: relative;
}
.card:disabled { cursor: default; }
.card[data-selected="true"] { border-color: var(--gold); background: var(--panel-2); }
.card[data-acted="true"] { opacity: 0.62; }
.card[data-ko="true"] { border-color: var(--danger); background: #241820; opacity: 1; }

.card-head { display: flex; justify-content: space-between; align-items: center; gap: 4px; }
.card-name { font-weight: bold; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-tag {
  font-size: 9px;
  padding: 1px 4px;
  border-radius: 3px;
  border: 1px solid currentColor;
  flex: none;
}
.card-tag--acted { color: var(--paper-dim); }
.card-tag--ko { color: var(--danger); }

.bar { position: relative; height: 9px; background: #0D1520; border: 1px solid var(--line); border-radius: 2px; margin-top: 3px; overflow: hidden; }
.bar > i { display: block; height: 100%; transition: width .18s linear; }
.bar--hp > i { background: var(--hp); }
.bar--en > i { background: var(--en); }
.bar--tension > i { background: var(--tension); }
.bar-label {
  display: flex; justify-content: space-between;
  font-size: 9px; color: var(--paper-dim); margin-top: 2px; letter-spacing: .3px;
}
/* 10.5 Cada barra lleva su etiqueta: no dependen solo del color. */
.bar-label b { color: var(--paper); font-weight: normal; }

.card-status { display: flex; gap: 2px; flex-wrap: wrap; margin-top: 4px; min-height: 14px; }
.status-icon {
  width: 12px; height: 12px;
  image-rendering: pixelated;
  background-image: url('../assets/ui/status.png');
  background-repeat: no-repeat;
}

/* Botones de comando */
.commands { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

.cmd {
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  color: var(--paper);
  font: inherit;
  font-size: 13px;
  padding: 8px 12px;
  min-height: var(--tap);
  min-width: 84px;
  cursor: pointer;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.cmd:hover:not(:disabled) { border-color: var(--gold); }
:root[data-input-modality="keyboard"] .cmd:focus { outline: 2px solid var(--gold); outline-offset: 2px; }
.cmd:disabled { opacity: .5; cursor: not-allowed; }
.cmd small { font-size: 10px; color: var(--paper-dim); }
.cmd--primary { border-color: var(--gold); }
.cmd--danger { border-color: var(--danger); }
.cmd--wide { flex: 1 1 100%; }

/* Previsualizacion exacta de la accion (10.3) */
.preview {
  margin-top: 8px;
  padding: 7px 9px;
  background: var(--panel);
  border-left: 3px solid var(--sea);
  border-radius: 3px;
  font-size: 12px;
  line-height: 1.45;
}
.preview b { color: var(--gold); }
.preview .warn { color: var(--danger); }
.preview ul { margin: 4px 0 0; padding-left: 16px; }

/* Registro de combate */
.log {
  margin-top: 8px;
  font-size: 11px;
  color: var(--paper-dim);
  max-height: 58px;
  overflow-y: auto;
  border-top: 1px solid var(--line);
  padding-top: 5px;
  line-height: 1.5;
}
.log.log--open { max-height: 160px; }

/* Panel de objetivo del final (9.6): separado de la vida del jefe */
.console-panel {
  display: flex;
  gap: 10px;
  align-items: center;
  background: #0E1A22;
  border: 2px solid #3FC8C0;
  border-radius: var(--radius);
  padding: 7px 9px;
  margin-bottom: 8px;
  font-size: 12px;
  flex-wrap: wrap;
}
.console-slots { display: flex; gap: 3px; }
.console-slot { width: 16px; height: 12px; background: #1A2A34; border: 1px solid #3FC8C0; }
.console-slot[data-on="true"] { background: #3FC8C0; }
.console-transfer { flex: 1 1 140px; min-width: 120px; }
.console-transfer .bar > i { background: var(--danger); }
.console-seal { color: var(--danger); }
.console-last { color: var(--gold); font-weight: bold; }

/* Intenciones enemigas */
.intents { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.intent {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: 3px solid var(--danger);
  border-radius: 3px;
  padding: 5px 8px;
  font-size: 12px;
  cursor: pointer;
  width: 100%;
  color: inherit;
  font-family: inherit;
  text-align: left;
}
.intent[data-selectable="true"]:hover { border-color: var(--gold); }
.intent[data-dead="true"] { opacity: .4; }
.intent-name { font-weight: bold; }
.intent-hp { flex: 1 1 90px; min-width: 70px; }
.intent-prepare { color: var(--gold); }
.intent-broken { color: var(--ok); }
.intent-rule { font-size: 10px; color: var(--sea); flex-basis: 100%; }

/* --------------------------------------------------------------- controles */

#touch {
  display: none;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
  padding: 8px 12px 14px;
  background: var(--bg);
}
#dpad {
  display: grid;
  grid-template-columns: repeat(3, var(--tap));
  grid-template-rows: repeat(3, var(--tap));
  gap: 2px;
}
.dpad-btn {
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  color: var(--paper);
  font-size: 18px;
  min-width: var(--tap);
  min-height: var(--tap);
  cursor: pointer;
  touch-action: none;
  user-select: none;
}
.dpad-btn[data-dir="up"] { grid-area: 1 / 2; }
.dpad-btn[data-dir="left"] { grid-area: 2 / 1; }
.dpad-btn[data-dir="right"] { grid-area: 2 / 3; }
.dpad-btn[data-dir="down"] { grid-area: 3 / 2; }
.dpad-btn:active { border-color: var(--gold); background: var(--gold); color: var(--ink); }

#touch-actions { display: flex; flex-direction: column; gap: 8px; }
.touch-btn {
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  color: var(--paper);
  font: inherit;
  font-size: 14px;
  padding: 10px 16px;
  min-height: var(--tap);
  min-width: 120px;
  cursor: pointer;
}
.touch-btn--main { border-color: var(--gold); }
.touch-btn:active { background: var(--gold); color: var(--ink); }

/* ----------------------------------------------------------------- modales */

#modal-root {
  position: fixed;
  inset: 0;
  background: rgba(8, 11, 17, .88);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  z-index: 50;
  overflow-y: auto;
}
#modal-root[hidden] { display: none; }

.modal {
  background: var(--panel);
  border: 3px solid var(--gold);
  border-radius: 8px;
  padding: 14px 16px 16px;
  width: min(760px, 100%);
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
}
.modal h2 { margin: 0 0 4px; font-size: 18px; color: var(--gold); }
.modal h3 { margin: 14px 0 6px; font-size: 13px; color: var(--sea); letter-spacing: 1px; text-transform: uppercase; }
.modal p { margin: 6px 0; font-size: 13px; line-height: 1.55; }
.modal .muted { color: var(--paper-dim); font-size: 12px; }
.modal-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }

/* Cuadro de dialogo */
.dialogue {
  width: min(760px, 100%);
  background: var(--ink);
  border: 3px solid var(--gold);
  border-radius: 8px;
  padding: 14px 16px;
}
.dialogue-speaker { color: var(--gold); font-size: 12px; letter-spacing: 1px; text-transform: uppercase; margin-bottom: 6px; }
.dialogue-text { font-size: 15px; line-height: 1.6; min-height: 3.2em; }
.dialogue-hint { margin-top: 10px; font-size: 11px; color: var(--paper-dim); display: flex; justify-content: space-between; gap: 10px; }

/* Grillas de seleccion */
.grid { display: grid; gap: 8px; }
.grid--roster { grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); }
.grid--shop { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
.grid--perks { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

.pick {
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: 8px;
  color: inherit;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-height: var(--tap);
}
.pick:hover:not(:disabled) { border-color: var(--gold); }
.pick[data-picked="true"] { border-color: var(--gold); background: #2C3A2C; }
.pick:disabled { opacity: .45; cursor: not-allowed; }
.pick b { font-size: 13px; }
.pick small { display: block; color: var(--paper-dim); line-height: 1.4; }
.pick-top > div { min-width: 0; }
.pick-top b { display: block; }
.pick-top { display: flex; gap: 8px; align-items: center; }
.pick-order {
  width: 20px; height: 20px; flex: none;
  border: 1px solid var(--gold); border-radius: 50%;
  color: var(--gold); font-size: 11px;
  display: flex; align-items: center; justify-content: center;
}

.portrait {
  width: 48px; height: 48px; flex: none;
  image-rendering: pixelated;
  background-image: url('../assets/characters/portraits.png');
  background-repeat: no-repeat;
  border: 1px solid var(--line);
}
.portrait--locked { filter: grayscale(1) brightness(.45); }
.portrait--sm { width: 32px; height: 32px; background-size: 384px 32px; }

.locked-note { color: var(--gold); font-size: 11px; }

/* Barra de titulo */
.title-screen { text-align: center; padding: 18px 10px 24px; }
.title-screen h1 { font-size: clamp(20px, 5vw, 34px); margin: 8px 0 2px; color: var(--paper); letter-spacing: 1px; }
.title-screen .sub { color: var(--gold); font-size: clamp(11px, 2.4vw, 14px); letter-spacing: 3px; text-transform: uppercase; }
.title-screen .notice { color: var(--paper-dim); font-size: 11px; max-width: 460px; margin: 12px auto 0; line-height: 1.6; }
.title-menu { display: flex; flex-direction: column; gap: 8px; max-width: 280px; margin: 18px auto 0; }

/* Tablas de estado */
.table { width: 100%; border-collapse: collapse; font-size: 12px; }
.table th, .table td { padding: 4px 6px; border-bottom: 1px solid var(--line); text-align: left; }
.table th { color: var(--sea); font-weight: normal; font-size: 10px; text-transform: uppercase; letter-spacing: .8px; }
.table td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Avisos */
#toast-root {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 80;
  pointer-events: none;
  width: min(440px, calc(100vw - 24px));
}
.toast {
  background: var(--ink);
  border: 2px solid var(--gold);
  border-radius: var(--radius);
  padding: 8px 12px;
  font-size: 12px;
  text-align: center;
  animation: toast-in .16s ease-out;
}
.toast--bad { border-color: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* Barra de carga */
.loading { text-align: center; padding: 40px 16px; }
.loading .bar { height: 12px; max-width: 320px; margin: 14px auto 0; }
.loading .bar > i { background: var(--gold); }

/* Ajustes */
.setting { display: flex; align-items: center; gap: 10px; margin: 8px 0; font-size: 13px; flex-wrap: wrap; }
.setting label { flex: 1 1 170px; }
.setting input[type="range"] { flex: 1 1 140px; accent-color: var(--gold); min-height: 32px; }
.seg { display: flex; gap: 4px; }
.seg button {
  background: var(--panel-2); border: 2px solid var(--line); color: var(--paper);
  font: inherit; font-size: 12px; padding: 7px 12px; border-radius: var(--radius);
  cursor: pointer; min-height: 36px;
}
.seg button[aria-pressed="true"] { border-color: var(--gold); background: var(--gold); color: var(--ink); }

/* 10.5 Contraste alto para bordes y estados */
body.high-contrast {
  --line: #6E7E96;
  --panel: #131C28;
  --panel-2: #1B2736;
}
body.high-contrast .card { border-width: 3px; }
body.high-contrast .cmd { border-width: 3px; }

/* 10.5 Movimiento reducido: sin flash, sin vibracion, sin grandes desplazamientos */
body.reduced-motion .toast { animation: none; }
@media (prefers-reduced-motion: reduce) {
  .toast { animation: none; }
  .bar > i { transition: none; }
}

/* --------------------------------------------------------------- responsive */

/* Escritorio horizontal: campo y panel uno debajo del otro, centrados. */
@media (min-width: 900px) {
  #app { height: min(100dvh, 780px); border: 2px solid var(--line); border-radius: 8px; margin: 14px 0; }
}

/* 10.4 Movil: cruceta tactil visible y comandos grandes. */
body.touch #touch { display: flex; }

/* Movil vertical: lienzo arriba, comandos HTML grandes abajo. */
@media (max-width: 720px) {
  /* El objetivo pasa a su propia linea: en vertical no hay ancho para ambos. */
  #hud { font-size: 12px; flex-wrap: wrap; gap: 4px; }
  #hud-objective { flex: 1 1 100%; order: 2; }
  #hud-right { flex: 1 1 100%; order: 1; justify-content: space-between; }
  #hud-right #hud-bag { flex: 1 1 auto; min-width: 0; }
  /* El recorte tiene que ir en el span: el boton no lo propaga solo. */
  #hud-bag-text { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  #hud-save { font-size: 0; padding: 5px; min-width: 18px; }
  #hud-save::after { content: '●'; font-size: 14px; }
  .cards { grid-template-columns: repeat(3, 1fr); gap: 4px; }
  .card { min-height: 84px; padding: 5px; font-size: 11px; }
  .cmd { flex: 1 1 calc(50% - 3px); min-width: 0; font-size: 14px; padding: 10px; }
  .preview { font-size: 12px; }
  .grid--roster { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); }
}

@media (max-width: 420px) {
  .cards { grid-template-columns: repeat(3, 1fr); }
  .card-name { font-size: 11px; }
  .bar-label { font-size: 8px; }
  #dpad { grid-template-columns: repeat(3, 46px); grid-template-rows: repeat(3, 46px); }
}

/* Movil horizontal: conservar campo y panel lado a lado si hay ancho. */
@media (max-height: 460px) and (orientation: landscape) {
  #app { flex-direction: row; flex-wrap: wrap; }
  #hud { width: 100%; }
  #stage { flex: 1 1 58%; }
  #panel { flex: 1 1 40%; min-width: 300px; max-height: calc(100dvh - 40px); overflow-y: auto; }
  #touch { width: 100%; padding: 4px 10px 8px; }
  #dpad { grid-template-columns: repeat(3, 40px); grid-template-rows: repeat(3, 40px); }
}
