/* MTG Table — TheBigG palette: near-black ground, gold accent, DM type. */

:root {
  --bg: #0a0a0b;
  --panel: #131316;
  --panel-2: #1a1a1f;
  --line: #2a2a31;
  --gold: #d4af55;
  --text: #f0ede6;
  --muted: #8d8a84;
  --good: #6fbf73;
  --bad: #d1605e;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "DM Sans", system-ui, -apple-system, sans-serif;
}

h1, h2 { font-family: "DM Serif Display", Georgia, serif; font-weight: 400; margin: 0 0 12px; }
h1 { font-size: 2rem; color: var(--gold); }
h2 { font-size: 1.15rem; }
.mono { font-family: "DM Mono", ui-monospace, monospace; }
.muted { color: var(--muted); font-size: 0.88rem; }
a { color: var(--gold); }

.btn {
  font: inherit;
  font-size: 0.9rem;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  transition: border-color 0.15s, background 0.15s;
}
.btn:hover { border-color: var(--gold); background: #202027; }
.btn-gold { background: var(--gold); color: #12100b; border-color: var(--gold); font-weight: 500; }
.btn-gold:hover { background: #e0bd68; }
.btn-small { padding: 4px 9px; font-size: 0.8rem; }
.btn-quiet { background: transparent; color: var(--muted); }

/* ───────────────────────────── lobby ───────────────────────────── */

.lobby-wrap { max-width: 720px; margin: 0 auto; padding: 48px 20px 80px; }
.back { font-size: 0.85rem; color: var(--muted); text-decoration: none; }
.lede { color: var(--muted); line-height: 1.6; margin: 0 0 28px; max-width: 54ch; }
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px;
  margin-bottom: 18px;
}
.panel.quiet { background: transparent; }
.panel ul { margin: 0; padding-left: 18px; color: var(--muted); line-height: 1.7; font-size: 0.9rem; }
.join-form { display: flex; gap: 10px; }
.join-form input {
  font-family: "DM Mono", monospace;
  font-size: 1.2rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  padding: 10px 14px;
  width: 200px;
  background: #0d0d10;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
}
.join-form input:focus { outline: none; border-color: var(--gold); }
.banner { padding: 12px 16px; border-radius: 6px; margin-bottom: 18px; font-size: 0.9rem; }
.banner.error { background: #2a1616; border: 1px solid #5a2b2b; color: #f0c7c7; }

/* ───────────────────────────── gate ───────────────────────────── */

.gate {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(8, 8, 10, 0.94);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.gate[hidden] { display: none; }
.gate-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 30px;
  width: min(420px, 100%);
  display: flex; flex-direction: column; gap: 10px;
}
.gate-card label { font-size: 0.8rem; color: var(--muted); margin-top: 8px; }
.gate-card input, .gate-card select {
  font: inherit; padding: 10px 12px;
  background: #0d0d10; border: 1px solid var(--line);
  border-radius: 6px; color: var(--text);
}
.gate-card input:focus, .gate-card select:focus { outline: none; border-color: var(--gold); }
.gate-card .btn { margin-top: 10px; text-align: center; }
.gate-error { color: var(--bad); font-size: 0.85rem; margin: 6px 0 0; }
.room-code {
  font-size: 1.5rem; letter-spacing: 0.25em; color: var(--gold);
}

/* ───────────────────────────── bar ───────────────────────────── */

.bar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 14px; padding: 10px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.bar-left, .bar-mid, .bar-right { display: flex; align-items: center; gap: 10px; }
.bar .room-code { font-size: 1.05rem; }
.status { font-size: 0.78rem; color: var(--muted); }
.status.ok { color: var(--good); }
.status.bad { color: var(--bad); }

/* ───────────────────────────── layout ───────────────────────────── */

.layout {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 14px;
  padding: 14px;
  height: calc(100vh - 58px);
}
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; height: auto; }
  .sidebar { max-height: 60vh; }
}

.video-grid {
  display: grid; gap: 10px; align-content: start;
  grid-template-columns: repeat(2, 1fr);
}
.video-grid[data-count="1"] { grid-template-columns: 1fr; }
.video-grid[data-count="5"], .video-grid[data-count="6"] { grid-template-columns: repeat(3, 1fr); }

.tile {
  position: relative; background: #000;
  border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; aspect-ratio: 16 / 9;
}
.tile.local { border-color: #3a3a44; }
.tile.offline { opacity: 0.45; }
.tile video { width: 100%; height: 100%; object-fit: cover; cursor: crosshair; display: block; }
.tile-label {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; font-size: 0.85rem;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.85));
}
.tile-life { margin-left: auto; color: var(--gold); font-size: 1rem; }
.tile-off { color: var(--bad); font-size: 0.72rem; }
/* Capture size, from the data attribute the client sets on loadedmetadata.
   A tile with no badge is a tile with no stream, which is the useful signal. */
.tile[data-resolution]::after {
  content: attr(data-resolution);
  position: absolute; top: 8px; left: 8px;
  font-family: "DM Mono", monospace; font-size: 0.68rem;
  color: var(--muted); background: rgba(0,0,0,0.55);
  padding: 2px 7px; border-radius: 999px; pointer-events: none;
}
.tile:not([data-resolution])::before {
  content: "no camera"; position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: #50504e; font-size: 0.82rem; pointer-events: none;
  font-family: "DM Mono", monospace; letter-spacing: 0.08em;
  background: repeating-linear-gradient(45deg, #0e0e10, #0e0e10 12px, #121215 12px, #121215 24px);
}
.crosshair {
  position: absolute; width: 26px; height: 26px; margin: -13px 0 0 -13px;
  border: 2px solid var(--gold); border-radius: 50%;
  animation: ping 1.2s ease-out forwards; pointer-events: none;
}
@keyframes ping {
  from { transform: scale(0.4); opacity: 1; }
  to   { transform: scale(1.6); opacity: 0; }
}

/* ───────────────────────────── sidebar ───────────────────────────── */

.sidebar {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); display: flex; flex-direction: column; overflow: hidden;
}
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; padding: 11px 8px; background: none; border: none;
  color: var(--muted); font: inherit; font-size: 0.85rem; cursor: pointer;
  border-bottom: 2px solid transparent;
}
.tab.active { color: var(--gold); border-bottom-color: var(--gold); }
.tab-panel { display: none; overflow-y: auto; padding: 14px; flex: 1; }
.tab-panel.active { display: block; }
.hint { font-size: 0.82rem; color: var(--muted); margin-bottom: 12px; }

.card-list { display: flex; flex-direction: column; gap: 8px; }
.card-item {
  display: flex; gap: 10px; align-items: center; text-align: left;
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px; cursor: pointer; color: inherit; font: inherit;
}
.card-item:hover { border-color: var(--gold); }
.card-item img { width: 46px; border-radius: 3px; display: block; }
.card-meta { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.card-name { font-size: 0.9rem; }
.card-sub { font-size: 0.72rem; color: var(--muted); }

.life-setup { padding-bottom: 12px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.setup-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.setup-row .muted { margin-right: auto; font-size: 0.8rem; }

.life-row { padding: 12px 0; border-bottom: 1px solid var(--line); }
.life-row.is-me .life-name { color: var(--gold); }
.life-row.offline { opacity: 0.5; }
.life-name { font-size: 0.92rem; margin-bottom: 8px; }
.life-controls { display: flex; align-items: center; gap: 6px; }
.life-total { font-size: 1.35rem; min-width: 46px; text-align: center; }
.counters { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.counter {
  font: inherit; font-size: 0.72rem; padding: 3px 8px;
  background: transparent; border: 1px solid var(--line);
  border-radius: 999px; color: var(--muted); cursor: pointer;
}
.counter:hover { border-color: var(--gold); color: var(--text); }

.cmd-damage { margin-top: 14px; }
.cmd-damage summary { cursor: pointer; font-size: 0.85rem; color: var(--muted); }
.cmd-table { border-collapse: collapse; margin-top: 10px; font-size: 0.78rem; width: 100%; }
.cmd-table th, .cmd-table td { border: 1px solid var(--line); padding: 3px; text-align: center; }
.cmd-table th { color: var(--muted); font-weight: 400; }
.cmd-cell {
  font: inherit; width: 100%; background: none; border: none;
  color: var(--text); cursor: pointer; padding: 4px;
}
.cmd-cell:hover { background: var(--panel-2); color: var(--gold); }

.log-list { display: flex; flex-direction: column; gap: 6px; }
.log-row { font-size: 0.82rem; display: flex; gap: 8px; }
.log-who { color: var(--gold); flex-shrink: 0; }
.log-msg { color: var(--muted); }

/* ───────────────────────────── modals ───────────────────────────── */

.modal {
  position: fixed; inset: 0; z-index: 70;
  background: rgba(6, 6, 8, 0.88);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal[hidden] { display: none; }
.modal-inner {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px; max-width: 520px; width: 100%;
  display: flex; flex-direction: column; gap: 12px; align-items: center;
}
.modal-inner img { max-width: 320px; width: 100%; border-radius: 10px; }
.modal-meta { text-align: center; font-size: 0.9rem; }
.pick-options { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.pick-option {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px; width: 140px; cursor: pointer; color: inherit; font: inherit;
  font-size: 0.8rem; text-align: center;
}
.pick-option:hover { border-color: var(--gold); }
.pick-option img { width: 100%; border-radius: 4px; margin-bottom: 6px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--gold);
  color: var(--text); padding: 10px 18px; border-radius: 999px;
  font-size: 0.88rem; z-index: 80;
}
.toast[hidden] { display: none; }


/* ═══════════════════════ view switching ═══════════════════════ */

.bar-mid { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.view-switch { display: flex; gap: 4px; }
.view-switch .btn.active { border-color: var(--gold); color: var(--gold); background: #20202a; }
.view-switch.subtle .btn { font-size: 0.74rem; padding: 3px 8px; color: var(--muted); }
.view-switch.subtle .btn.active { color: var(--gold); }
.view-switch[hidden] { display: none; }

body[data-view="board"] .video-grid { display: none; }
body[data-view="board"] .layout { grid-template-columns: 1fr; }
body[data-view="board"] .sidebar { max-height: none; }
/* In board mode the sidebar *is* the game, so it loses its panel chrome. */
body[data-view="board"] .sidebar { background: transparent; border: none; }
body[data-view="board"] .tab-panel { padding: 0; }

/* Overlay uses `contain` so a marker at (x, y) of the frame lands on the same
   point of the element. `cover` crops, and markers would drift on any camera
   that is not 16:9. */
body[data-view="overlay"] .tile video { object-fit: contain; }

/* ═══════════════════════ overlay markers ═══════════════════════ */

.overlay-layer { position: absolute; inset: 0; pointer-events: none; }
.marker {
  position: absolute; transform: translate(-50%, -50%);
  pointer-events: auto; cursor: pointer;
  background: none; border: none; padding: 0; color: var(--text); font: inherit;
  transition: opacity 0.25s, left 0.35s ease-out, top 0.35s ease-out;
}
/* A card the camera cannot currently see. Say so rather than keep drawing it
   confidently in a place it may no longer be. */
.marker.lost { opacity: 0.32; }

.marker.as-badge { display: flex; gap: 3px; }
.marker.as-badge .pip { font-size: 0.8rem; padding: 2px 8px; }
.marker.as-badge .pip.dot { background: rgba(212, 175, 85, 0.85); }

.marker.as-label {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  background: rgba(10, 10, 12, 0.8); border: 1px solid var(--gold);
  border-radius: 8px; padding: 4px 9px; font-size: 0.74rem;
  max-width: 42%; white-space: nowrap;
}
.marker.as-label:hover { background: rgba(22, 22, 28, 0.96); }
.marker.as-label.tapped { border-color: var(--muted); opacity: 0.8; }
.marker-name { overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.marker-pips { display: flex; gap: 4px; }

/* The heaviest style: the printed card, scaled and rotated to the tracked one. */
.marker.as-card {
  aspect-ratio: 488 / 680;
  filter: drop-shadow(0 6px 14px rgba(0, 0, 0, 0.6));
}
.marker.as-card img { height: 100%; width: 100%; border-radius: 5%; display: block; }
.marker.as-card .marker-pips.on-card {
  position: absolute; left: 50%; bottom: 6%; transform: translateX(-50%);
  display: flex; gap: 3px;
}

.pip {
  background: var(--gold); color: #12100b; border-radius: 999px;
  padding: 0 6px; font-size: 0.68rem; font-family: "DM Mono", monospace;
  font-weight: 500; white-space: nowrap;
}
/* Shrinking counters read as a penalty, so they should not look like a buff. */
.pip-m1m1 { background: var(--bad); color: #1a0b0b; }
.pip-loyalty { background: #6f7ea8; color: #0b0f1a; }

/* ═══════════════════════ the table ═══════════════════════ */

/* The board gets the whole main stage. It lived in the 340px sidebar before,
   which is why no amount of styling stopped it feeling cramped. */
.board-stage { display: none; min-height: 0; }
body[data-view="board"] .board-stage { display: block; }
body[data-view="board"] .video-grid { display: none; }
body[data-view="board"] .layout { grid-template-columns: 1fr 300px; }

.table-felt {
  /* Four rows of cards, two plates and a midline have to fit without
     overlapping, so the card height is derived from what is actually left
     rather than from a flat vh figure. */
  --card-h: clamp(84px, calc((100vh - 290px) / 4), 230px);
  --card-w: calc(var(--card-h) * 0.716);
  height: 100%;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: 8px 14px;
  border-radius: var(--radius);
  border: 1px solid #1d2a22;
  background:
    radial-gradient(ellipse 120% 80% at 50% 50%, #1b2f23 0%, #101d16 55%, #0a120d 100%);
  position: relative; overflow: hidden;
}
/* A faint ring under the middle, like the inlay on a play surface. */
.table-felt::before {
  content: ""; position: absolute; left: 50%; top: 50%;
  width: min(60vh, 46%); aspect-ratio: 1; transform: translate(-50%, -50%);
  border: 1px solid rgba(212, 175, 85, 0.06); border-radius: 50%;
  pointer-events: none;
}

.side {
  display: flex; flex-direction: column; justify-content: space-evenly;
  flex: 1; min-height: 0; gap: 4px;
}
/* Several opponents share the top half, side by side. */
.table-felt:not([data-opponents="1"]):not([data-opponents="0"]) .side.top {
  flex-direction: row; gap: 14px; align-items: stretch;
}
.table-felt:not([data-opponents="1"]):not([data-opponents="0"]) .side.top .seat { flex: 1; min-width: 0; }
.table-felt:not([data-opponents="1"]):not([data-opponents="0"]) { --card-h: clamp(78px, 11vh, 132px); }

.seat { display: flex; flex-direction: column; gap: 6px; }
.seat.offline { opacity: 0.55; }

/* ── the player's plate: avatar centred on their own edge ── */
.plate {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  gap: 12px; padding: 2px 6px;
}
.plate-side { display: flex; align-items: center; gap: 8px; min-width: 0; }
.plate-side.left { justify-content: flex-end; }
.plate-side.right { justify-content: flex-start; flex-wrap: wrap; }
.plate-name {
  font-size: 0.92rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.seat.mine .plate-name { color: var(--gold); }
.plate-off { color: var(--bad); font-size: 0.7rem; }
.plate-zones { color: #52705f; font-size: 0.7rem; font-family: "DM Mono", monospace; }

.avatar {
  position: relative; width: 58px; height: 58px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 35%, #2a3d31, #14211a);
  border: 2px solid #35513f; flex: 0 0 auto;
}
.seat.mine .avatar { border-color: var(--gold); box-shadow: 0 0 18px rgba(212, 175, 85, 0.18); }
.avatar-initial {
  font-family: "DM Serif Display", Georgia, serif; font-size: 1.5rem; color: #cfe0d5;
}
.avatar-life {
  position: absolute; bottom: -10px; left: 50%; transform: translateX(-50%);
  background: #0c140f; border: 1px solid #35513f; border-radius: 999px;
  padding: 1px 10px; font-size: 0.95rem; color: var(--text);
}
.seat.mine .avatar-life { border-color: var(--gold); color: var(--gold); }

/* ── rows of permanents ── */
.row {
  display: flex; gap: 8px; align-items: center; justify-content: center;
  flex-wrap: wrap; min-height: 0; padding: 2px;
}
.row-lands { opacity: 0.94; }
.row-empty {
  color: #33503f; font-size: 0.72rem; font-family: "DM Mono", monospace;
  letter-spacing: 0.12em; text-transform: uppercase;
}
.waiting {
  color: #4a6a57; font-size: 0.85rem; text-align: center; padding: 20px;
  font-family: "DM Mono", monospace;
}

.midline {
  display: flex; align-items: center; gap: 14px; margin: 2px 0;
  color: #33503f; font-family: "DM Mono", monospace; font-size: 0.66rem;
  letter-spacing: 0.34em; flex: 0 0 auto;
}
.midline::before, .midline::after {
  content: ""; flex: 1; height: 1px;
  background: linear-gradient(90deg, transparent, #24362c 35%, #24362c 65%, transparent);
}

/* ── one permanent ── */
.perm {
  background: none; border: none; padding: 0; cursor: pointer;
  width: var(--card-w); flex: 0 0 auto;
  transition: transform 0.16s, filter 0.16s;
}
.perm-frame { position: relative; display: block; }
.perm img {
  width: 100%; height: var(--card-h); display: block;
  border-radius: 5%; object-fit: cover;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.6);
}
.seat.mine .perm img { outline: 1px solid rgba(110, 190, 255, 0.35); outline-offset: -1px; }
.seat.opponent .perm img { outline: 1px solid rgba(225, 120, 100, 0.32); outline-offset: -1px; }
.perm:hover { transform: translateY(-4px); filter: brightness(1.12); z-index: 2; }
/* Tapped is a right angle, as on a real table. The extra side margin keeps the
   rotated card from overlapping its neighbours. */
.perm.tapped { transform: rotate(90deg); margin: 0 calc((var(--card-h) - var(--card-w)) / 2); }
.perm.tapped:hover { transform: rotate(90deg) scale(1.04); }
.perm.lost { filter: grayscale(0.75) brightness(0.65); }
.perm-tap {
  position: absolute; left: 4px; bottom: 4px;
  width: 20px; height: 20px; border-radius: 50%;
  background: rgba(8, 12, 10, 0.85); border: 1px solid #5d7a68;
  color: #cfe0d5; font-size: 0.72rem; line-height: 18px; text-align: center;
}
.perm-pips {
  position: absolute; right: 3px; bottom: 3px;
  display: flex; gap: 2px; flex-wrap: wrap; justify-content: flex-end; max-width: 92%;
}

@media (max-width: 1100px) {
  body[data-view="board"] .layout { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  .table-felt { --card-h: clamp(64px, calc((100vh - 250px) / 4), 120px); }
  .plate { gap: 6px; }
  .avatar { width: 46px; height: 46px; }
}

/* ═══════════════════════ card viewer ═══════════════════════ */

/* One viewer for every card. Clicking a marker, a permanent, or a row in the
   scan history all open this: the card big enough to read, with whatever
   controls apply beside it. The old corner popover showed a 54px thumbnail,
   which told you which card it was but not what it did. */
.viewer {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(5, 6, 6, 0.88);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.viewer[hidden] { display: none; }
.viewer-inner {
  display: flex; gap: 22px; align-items: stretch;
  max-width: min(1000px, 96vw); max-height: 92vh;
}
.viewer-img {
  height: min(88vh, 720px); width: auto; border-radius: 14px; display: block;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.7); background: #12100b;
}
.viewer-side {
  width: 260px; flex: 0 0 auto; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px;
  display: flex; flex-direction: column; gap: 16px;
}
.vs-head { display: flex; gap: 10px; align-items: flex-start; }
.vs-head > div { min-width: 0; }
.vs-name {
  font-family: "DM Serif Display", Georgia, serif; font-size: 1.15rem;
  color: var(--gold); line-height: 1.25;
}
.vs-head .btn { margin-left: auto; }
.vs-block { display: flex; flex-direction: column; gap: 8px; }
.vs-label {
  font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.14em;
  color: var(--muted);
}
.vs-scryfall { margin-top: auto; text-align: center; }
.btn.on { border-color: var(--gold); color: var(--gold); }

.cc-row { display: flex; gap: 6px; flex-wrap: wrap; }
.cc-counters { display: flex; flex-direction: column; gap: 7px; }
.cc-counter { display: flex; align-items: center; gap: 8px; font-size: 0.84rem; }
.cc-counter span:first-child { flex: 1; color: var(--muted); }
.cc-value { min-width: 28px; text-align: center; font-size: 1.05rem; }

@media (max-width: 820px) {
  .viewer-inner { flex-direction: column; align-items: center; overflow-y: auto; }
  .viewer-img { height: auto; width: min(70vw, 340px); }
  .viewer-side { width: min(90vw, 340px); }
}
