:root {
  --bg: #12161c;
  --felt: #1b3a2f;
  --felt-2: #224737;
  --panel: #1d232c;
  --line: #2f3a47;
  --text: #e8edf2;
  --muted: #93a1b0;
  --accent: #f2c14e;
  --danger: #e5484d;
  --ok: #46a758;
  --target: #4cc3ff;

  --WKL: #8a939c;
  --NRM: #5bb36a;
  --INT: #4c8dff;
  --DES: #ff8a3d;
  --ULT: #b26bff;
  --GOD: #f2c14e;
  --SPE: #ff6fae;
  --HAZ: #2ec4b6;
  --FORCE: #e5484d;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button {
  font: inherit;
  color: inherit;
  background: #2a3340;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); color: #1a1a1a; border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; }

.muted { color: var(--muted); }

#app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 12px;
  max-width: 1280px;
  margin: 0 auto;
  padding: 12px 16px;
}

@media (max-width: 900px) {
  #app { grid-template-columns: minmax(0, 1fr); }
}

/* ---------- bars ---------- */

.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
}
.who { font-weight: 700; min-width: 48px; }
.stat { color: var(--muted); }
.status { margin-left: auto; color: var(--muted); }
.status.ok { color: var(--ok); }
#player-bar button { margin-left: auto; }

.hp {
  position: relative;
  width: 160px;
  height: 18px;
  background: #3a1d20;
  border-radius: 9px;
  overflow: hidden;
}
.hp-fill { height: 100%; background: linear-gradient(90deg, var(--danger), #ff7b6b); transition: width 0.3s; }
.hp span { position: absolute; inset: 0; text-align: center; font-size: 12px; font-weight: 600; line-height: 18px; }

.backs { display: flex; align-items: center; gap: 2px; }
.backs .back { width: 15px; height: 20px; object-fit: cover; border-radius: 2px; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5); }
.backs span { margin-left: 4px; }

.note-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  margin-top: 6px;
}
.order-note { grid-column: 2; margin: 0; font-size: 12px; color: var(--muted); text-align: center; }
.hub-link { grid-column: 1; justify-self: start; font-size: 12px; color: var(--muted); text-decoration: none; }
.hub-link:hover { color: var(--accent); }
.modal-actions .hub-link { margin-right: auto; align-self: center; font-size: 14px; }

/* ---------- board ---------- */

#board {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
  background: var(--felt);
  border-radius: 10px;
  padding: 8px;
  margin: 6px 0 10px;
}

.lane {
  display: grid;
  grid-template-rows: 1fr auto 1fr;
  gap: 4px;
  border-radius: 8px;
  padding: 4px;
  background: var(--felt-2);
  transition: box-shadow 0.2s;
}
.lane.attacking { box-shadow: 0 0 0 3px var(--accent); }

.slot {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 150px;
  padding: 4px;
  border-radius: 6px;
  border: 1px dashed transparent;
}
.slot.bot { justify-content: flex-end; }
.slot.player { justify-content: flex-start; }
.slot > .muted { margin: auto; }
.slot.target { border-color: var(--target); background: rgba(76, 195, 255, 0.08); cursor: pointer; }

.lane-atk { text-align: center; font-size: 12px; font-weight: 700; color: var(--accent); }

.haz-slot {
  min-height: 56px;
  padding: 4px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.2);
  text-align: center;
  border: 1px dashed transparent;
}
.haz-slot.target { border-color: var(--target); cursor: pointer; }
.lane-no { display: block; font-size: 11px; color: var(--muted); }
.haz-card { border-left: 3px solid var(--HAZ); text-align: left; padding: 2px 4px; }
.haz-art { float: left; width: 34px; height: 34px; object-fit: cover; border-radius: 4px; margin: 0 5px 2px 0; }
.haz-card b { display: block; font-size: 12px; color: var(--HAZ); }
.haz-card small { display: block; font-size: 10px; color: var(--muted); }

.bchar {
  position: relative;
  background: #11161c;
  border-radius: 6px;
  border-left: 4px solid var(--muted);
  padding: 4px 6px;
  font-size: 12px;
  cursor: pointer;
}
.bchar.target { outline: 2px solid var(--target); }
.bart {
  display: block;
  width: 100%;
  aspect-ratio: 2 / 1;
  object-fit: cover;
  object-position: center 40%;
  border-radius: 4px;
  margin-bottom: 3px;
}
.bname { font-weight: 600; overflow-wrap: anywhere; }
.bstats { color: var(--muted); }
.rank { font-weight: 700; font-size: 10px; }
.hurt { color: #ff8b8b; }
.pos {
  position: absolute;
  top: -7px;
  right: 4px;
  font-size: 9px;
  font-weight: 700;
  background: var(--accent);
  color: #1a1a1a;
  border-radius: 4px;
  padding: 0 4px;
}

.chip {
  display: inline-block;
  font-size: 10px;
  margin: 2px 2px 0 0;
  padding: 0 5px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.1);
}

.rank-WKL { border-color: var(--WKL); } .rank-WKL .rank { color: var(--WKL); }
.rank-NRM { border-color: var(--NRM); } .rank-NRM .rank { color: var(--NRM); }
.rank-INT { border-color: var(--INT); } .rank-INT .rank { color: var(--INT); }
.rank-DES { border-color: var(--DES); } .rank-DES .rank { color: var(--DES); }
.rank-ULT { border-color: var(--ULT); } .rank-ULT .rank { color: var(--ULT); }
.rank-GOD { border-color: var(--GOD); } .rank-GOD .rank { color: var(--GOD); }

/* ---------- hand ---------- */

#hand {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 2px 6px;
}

.card {
  flex: 0 0 132px;
  min-height: 150px;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--panel);
  border: 2px solid var(--line);
  border-top: 6px solid var(--muted);
  border-radius: 8px;
  padding: 6px 8px;
  transition: transform 0.15s;
}
.card:hover { transform: translateY(-3px); }
.card.selected { border-color: var(--target); transform: translateY(-6px); }
.card.dim { opacity: 0.55; }
.card-top { display: flex; justify-content: space-between; font-size: 12px; font-weight: 700; }
.cost { color: var(--accent); }
.card-art {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 5px;
}
.card-name { font-weight: 700; }
.card-stats { color: var(--muted); }
.card-text { font-size: 12px; color: var(--muted); }

.card.t-CHAR.rank-WKL { border-top-color: var(--WKL); }
.card.t-CHAR.rank-NRM { border-top-color: var(--NRM); }
.card.t-CHAR.rank-INT { border-top-color: var(--INT); }
.card.t-CHAR.rank-DES { border-top-color: var(--DES); }
.card.t-CHAR.rank-ULT { border-top-color: var(--ULT); }
.card.t-CHAR.rank-GOD { border-top-color: var(--GOD); }
.card.t-SPE { border-top-color: var(--SPE); }
.card.t-HAZ { border-top-color: var(--HAZ); }
.card.t-FORCE { border-top-color: var(--FORCE); }
.card.t-SPE .label { color: var(--SPE); }
.card.t-HAZ .label { color: var(--HAZ); }

/* ---------- side panel ---------- */

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
}
#detail { min-height: 140px; }
.detail-art {
  display: block;
  width: min(100%, 200px);
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 8px;
  margin: 0 auto 10px;
}
#detail h3 { margin: 0 0 4px; }
#detail p { margin: 6px 0; }
.detail-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.hint { color: var(--muted); }
.warn { color: #ff8b8b; }
.notice { color: var(--accent); font-weight: 600; }

.log-title { margin: 12px 0 6px; font-size: 13px; color: var(--muted); }
#log { max-height: 420px; overflow-y: auto; font-size: 12px; }
.entry { padding: 3px 0; border-bottom: 1px solid rgba(255, 255, 255, 0.05); }
.entry.turn { color: var(--accent); font-weight: 700; }
.entry.player { color: #9fd6ff; }
.entry.bot { color: #ffc2a8; }
.entry.death { color: #ff8b8b; }
.entry.haz { color: var(--HAZ); }
.entry.force { color: var(--SPE); }

/* ---------- modal ---------- */

#modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
}
#modal[hidden] { display: none; }
#modal-box {
  width: min(520px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 18px 20px;
}
#modal-box h1, #modal-box h2 { margin: 0 0 6px; }
.modal-art {
  display: block;
  width: min(100%, 220px);
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 10px;
  margin: 0 auto 12px;
}
.rules { padding-left: 18px; color: var(--muted); }
.rules li { margin: 4px 0; }
.check { display: flex; gap: 8px; align-items: flex-start; margin: 12px 0; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }
.choices { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }

.mg-prompt { font-size: 22px; font-weight: 700; margin: 12px 0; }
.mg-form { display: flex; gap: 8px; }
.mg-form input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 18px;
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: #11161c;
  color: var(--text);
}
.mg-time { height: 6px; background: #11161c; border-radius: 3px; margin-top: 12px; overflow: hidden; }
#mg-bar { height: 100%; width: 100%; background: var(--accent); }

.rx-box {
  display: block;
  width: 100%;
  height: 160px;
  margin-top: 12px;
  font-size: 24px;
  font-weight: 700;
  background: #7a2a2d;
  border: none;
}
.rx-box.go { background: var(--ok); }
.rx-box.fail { background: #444; }

/* ---------- fit one screen (desktop) ---------- */

/* The whole table fits the window: the board takes the height left over after the
   bars and the hand, and character art shrinks with its slot (cqh = slot height).
   A slot holding a team-up (.n2) splits that height between two characters. */
@media (min-width: 901px) {
  #app {
    height: 100vh;
    height: 100dvh;
    grid-template-rows: minmax(0, 1fr);
  }
  #table { display: flex; flex-direction: column; min-height: 0; }
  #table > :not(#board) { flex: none; }
  #board { flex: 1 1 0; min-height: 328px; grid-template-rows: minmax(0, 1fr); }
  .lane { grid-template-rows: minmax(0, 1fr) auto minmax(0, 1fr); min-height: 0; }
  .slot { min-height: 0; container-type: size; }
  .bart { aspect-ratio: auto; height: clamp(0px, calc(100cqh - 110px), 120px); }
  /* TRÊN / DƯỚI already marks a team-up, so the chips make room for the art. */
  .slot.n2 .chip { display: none; }
  .slot.n2 .bart { height: clamp(0px, calc(50cqh - 80px), 120px); }
  /* Below ~24px the art would only be a thin strip, so drop it. */
  @container (max-height: 134px) { .slot.n1 .bart { display: none; } }
  @container (max-height: 208px) { .slot.n2 .bart { display: none; } }
  /* Full hazard text stays in the tooltip and the detail panel. */
  .haz-card small { display: none; }

  .card { min-height: 0; }
  .card-art { aspect-ratio: auto; height: clamp(44px, 10vh, 116px); }
  .card-name, .card-text {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .card-name { -webkit-line-clamp: 2; }
  .card-text { -webkit-line-clamp: 3; }

  #side { display: flex; flex-direction: column; min-height: 0; }
  #log { flex: 1 1 0; min-height: 80px; max-height: none; }
}

@media (max-width: 600px) {
  #app { padding: 8px 16px; }
  #board { gap: 3px; padding: 4px; }
  .lane { padding: 2px; }
  .slot { min-height: 110px; padding: 2px; }
  .bchar { font-size: 10px; padding: 3px 4px; }
  .chip { display: none; }
  .haz-card small { display: none; }
  .haz-art { float: none; display: block; width: 100%; height: auto; aspect-ratio: 1; margin: 0 0 2px; }
  .backs .back { display: none; }
  .hp { width: 110px; }
  .card { flex-basis: 116px; }
}
