:root {
  color-scheme: dark;
  --bg: #11151f;
  --panel: #1a202c;
  --panel-2: #222a36;
  --line: #343d4b;
  --text: #f5f2e8;
  --muted: #aeb7c5;
  --gold: #ddb75a;
  --teal: #45c7bd;
  --danger: #ef6868;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body, #app {
  margin: 0;
  min-height: 100%;
  max-width: 100%;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
}
button, input, select, textarea { font: inherit; }
button {
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  min-height: 42px;
  border-radius: 8px;
  padding: 0 12px;
  font-weight: 750;
  touch-action: manipulation;
}
button.primary { background: var(--gold); color: #11151f; border-color: var(--gold); }
button:disabled {
  opacity: .5;
  cursor: not-allowed;
}
button:active { transform: translateY(1px); }
input, select {
  border: 1px solid var(--line);
  background: #101620;
  color: var(--text);
  border-radius: 8px;
  min-height: 42px;
  padding: 0 12px;
  touch-action: manipulation;
}

.home {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}
.home-card {
  width: min(420px, 100%);
  display: grid;
  gap: 12px;
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  animation: rise-in .28s ease-out;
}
.home-card { place-items: stretch; text-align: center; }
.home-card h1 { margin: 0 0 4px; font-size: 25px; }
.home-card p { margin: 0; color: var(--muted); font-size: 13px; }
.join-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}
.join-row input {
  min-width: 0;
  text-align: center;
  letter-spacing: 2px;
  font-weight: 900;
}
.home-logo {
  width: 112px;
  height: 112px;
  object-fit: cover;
  border-radius: 22px;
  justify-self: center;
  border: 1px solid rgba(255,255,255,.18);
}
.kofi-link {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: var(--text);
  border: 1px solid rgba(221,183,90,.35);
  background: rgba(221,183,90,.08);
  border-radius: 8px;
  padding: 10px 12px;
  text-align: left;
}
.kofi-link span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}
.kofi-link b {
  color: var(--gold);
  font-size: 13px;
}
.setup-page {
  min-height: 100vh;
  display: grid;
  place-items: start center;
  padding: 10px;
  max-width: 100vw;
  overflow-x: hidden;
}
.setup-card {
  width: min(460px, calc(100vw - 20px));
  max-width: 100%;
  display: grid;
  gap: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
}
.setup-head h1 { margin: 0; font-size: 24px; }
.setup-head p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.setup-logo {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 12px;
  float: right;
  margin-left: 10px;
  border: 1px solid rgba(255,255,255,.18);
}
.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 6px;
  min-width: 0;
}
.segmented button.selected, .tab-bar button.selected, .setup-colors button.selected {
  background: var(--gold);
  color: #11151f;
  border-color: var(--gold);
}
.setup-section {
  display: grid;
  gap: 7px;
}
.setup-section.callout {
  border: 1px solid rgba(231, 194, 85, .35);
  background: rgba(231, 194, 85, .08);
  border-radius: 8px;
  padding: 8px;
}
.setup-colors {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.setup-colors button {
  display: grid;
  place-items: center;
  gap: 3px;
  padding: 7px 4px;
  color: var(--rune);
}
.setup-colors img {
  width: 28px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 4px;
}
.setup-colors span { font-size: 12px; font-weight: 850; }
.deck-counts {
  display: grid;
  gap: 6px;
}
.deck-count {
  display: grid;
  grid-template-columns: 1fr 38px 36px 38px;
  align-items: center;
  gap: 6px;
  border-left: 4px solid var(--rune);
  background: rgba(255,255,255,.035);
  border-radius: 8px;
  padding: 6px;
}
.deck-count span, .deck-count b { font-weight: 850; }
.deck-count button { min-height: 34px; padding: 0; }
.slider-counts .deck-count {
  grid-template-columns: minmax(70px, 1fr) 34px minmax(120px, 2fr);
}
.deck-count input[type="range"] {
  min-height: 28px;
  padding: 0;
  accent-color: var(--rune);
}

.table-app {
  min-height: 100vh;
  max-width: 100vw;
  overflow-x: hidden;
  display: grid;
  grid-template-rows: minmax(190px, 31vh) 1fr auto;
  padding-bottom: env(safe-area-inset-bottom);
}
.table-app.linked-play {
  grid-template-rows: 1fr auto;
}

.opponent-zone {
  border-bottom: 1px solid var(--line);
  background: #0c111a;
  display: grid;
  place-items: stretch;
  overflow: hidden;
}
.opponent-flip {
  transform: rotate(180deg);
  display: grid;
  align-content: center;
  gap: 10px;
  padding: 14px 12px;
}
.mini-row, .remote-summary {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}
.remote-summary.multi {
  flex-wrap: wrap;
  gap: 5px;
}
.remote-summary.multi span {
  max-width: 100%;
  white-space: nowrap;
}
.active-turn {
  border-color: var(--gold) !important;
  box-shadow: 0 0 0 1px rgba(221,183,90,.35) inset;
}
.mini-row span, .remote-summary span, .remote-summary strong {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 8px;
  background: rgba(255,255,255,.04);
}
.big-readout {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  text-align: center;
}
.big-readout div {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 6px;
  background: #151b26;
}
.big-readout b { display: block; font-size: clamp(34px, 12vw, 58px); line-height: .95; color: var(--gold); }
.big-readout span { color: var(--muted); font-size: 11px; text-transform: uppercase; font-weight: 800; }
.opponent-colors {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 5px;
}
.op-color {
  display: grid;
  place-items: center;
  gap: 2px;
  color: var(--rune);
  font-weight: 900;
}
.op-color img, .color-ledger-row img, .domain-cost img, .rune-add img, .rune-row img {
  width: 28px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid rgba(255,255,255,.16);
}

.player-zone {
  overflow-y: auto;
  overflow-x: hidden;
  padding: 10px 10px 86px;
  display: grid;
  gap: 10px;
}
.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto auto auto;
  gap: 8px;
  align-items: center;
  max-width: 100%;
}
.topbar input { min-width: 0; width: 100%; font-weight: 800; }
.topbar button { padding: 0 9px; }
.mode-pill {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 9px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}
.panel-title {
  min-height: 32px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}
.panel-title span { color: var(--muted); font-size: 12px; font-weight: 750; }
.panel-title button { min-height: 32px; font-size: 12px; }
.live-table {
  background: #141b25;
}
.live-stack, .lobby-list {
  display: grid;
  gap: 6px;
}
.live-player, .lobby-player {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 56px 56px 56px;
  gap: 7px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255,255,255,.035);
  padding: 7px 8px;
}
.live-player div, .lobby-player span {
  min-width: 0;
}
.live-player b, .lobby-player span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.live-player span, .lobby-player small {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}
.live-player strong, .lobby-player b {
  color: var(--gold);
  font-size: 13px;
  text-align: center;
}
.live-player b {
  display: flex;
  align-items: center;
  gap: 5px;
}
.domain-icons {
  display: inline-flex;
  gap: 2px;
  vertical-align: middle;
}
.domain-icons img {
  width: 13px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 2px;
  border: 1px solid rgba(255,255,255,.15);
}
.active-dots {
  display: flex;
  justify-content: center;
  gap: 2px;
  margin-top: 2px;
  min-height: 6px;
  width: 100%;
}
.active-dots i {
  min-width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--rune);
  color: #fff;
  display: grid;
  place-items: center;
  font-style: normal;
  font-size: 9px;
  line-height: 1;
  box-shadow: 0 0 0 1px rgba(255,255,255,.25) inset;
}
.lobby-player input {
  min-width: 0;
  min-height: 34px;
  padding: 0 8px;
  font-weight: 800;
}
.room-code {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px;
  border: 1px solid rgba(221,183,90,.4);
  border-radius: 8px;
  background: rgba(221,183,90,.08);
  padding: 9px 10px;
}
.room-code span, .room-code small, .hint {
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}
.room-code b {
  color: var(--gold);
  letter-spacing: 3px;
  text-align: center;
  font-size: 22px;
}
.hint {
  margin: 0;
  line-height: 1.35;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 7px;
}
.stats-grid div, .shortfall {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255,255,255,.035);
  padding: 8px 6px;
  text-align: center;
}
.stats-grid b { display: block; font-size: 23px; color: var(--teal); }
.stats-grid span, .shortfall span { color: var(--muted); font-size: 11px; font-weight: 750; }
.shortfall {
  margin-top: 8px;
  display: flex;
  justify-content: space-between;
  text-align: left;
  border-color: var(--gold);
}

.color-ledger {
  display: grid;
  gap: 6px;
  margin-top: 8px;
}
.color-ledger-row {
  display: grid;
  grid-template-columns: 30px 1fr auto auto;
  align-items: center;
  gap: 8px;
  border-left: 4px solid var(--rune);
  background: rgba(255,255,255,.035);
  border-radius: 7px;
  padding: 6px;
}
.color-ledger-row span { font-weight: 800; }
.color-ledger-row small { color: var(--muted); font-weight: 750; }

.cost-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.stepper {
  display: grid;
  grid-template-columns: 42px 1fr 42px;
  align-items: stretch;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}
.stepper button { border: 0; border-radius: 0; min-height: 54px; font-size: 24px; }
.stepper div { display: grid; place-items: center; background: #111822; }
.stepper strong { font-size: 26px; line-height: 1; }
.stepper span { color: var(--muted); font-size: 11px; font-weight: 800; }
.stepper.energy strong { color: var(--gold); }
.stepper.power strong { color: var(--teal); }

.domain-costs, .add-grid, .seal-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  margin-top: 8px;
}
.domain-cost, .rune-add, .seal-add {
  display: grid;
  place-items: center;
  gap: 2px;
  padding: 6px 2px;
  min-height: 58px;
  color: var(--rune);
}
.seal-add { min-height: 42px; font-size: 12px; }
.domain-cost span, .rune-add span, .seal-add span { font-size: 18px; font-weight: 900; }
.button-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}
.tab-bar {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
  gap: 7px;
}
.tab-bar button { min-height: 40px; }
.button-row > * { flex: 1; min-width: 0; }
.room-alert {
  display: grid;
  gap: 6px;
  border: 1px solid rgba(231, 194, 85, .4);
  background: rgba(231, 194, 85, .08);
  border-radius: 8px;
  padding: 8px;
  margin-bottom: 8px;
}
.room-alert span {
  color: var(--muted);
  font-size: 12px;
}
.switches {
  display: grid;
  gap: 7px;
  margin-top: 8px;
}
.toggle {
  min-height: 38px;
  display: flex;
  align-items: center;
  gap: 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: rgba(255,255,255,.035);
  font-weight: 750;
}
.toggle input { min-height: unset; width: 20px; height: 20px; accent-color: var(--gold); }

.tiny-actions { display: flex; gap: 6px; }
.tiny-actions button { min-height: 32px; font-size: 12px; padding: 0 8px; }
.rune-list, .rune-groups {
  display: grid;
  gap: 6px;
  margin-top: 8px;
}
.rune-group {
  display: grid;
  gap: 7px;
  border: 1px solid var(--line);
  border-left: 4px solid var(--rune);
  border-radius: 8px;
  background: rgba(255,255,255,.035);
  padding: 8px;
}
.rune-group-head {
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
}
.rune-group-head img {
  width: 28px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid rgba(255,255,255,.16);
}
.rune-group-head b {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.rune-group-head span {
  color: var(--gold);
  font-size: 13px;
  font-weight: 900;
}
.rune-counts, .rune-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
}
.rune-counts span {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 5px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 850;
  text-align: center;
}
.rune-actions {
  grid-template-columns: repeat(3, 1fr);
}
.rune-actions button {
  min-height: 34px;
  padding: 0 6px;
  font-size: 12px;
}
.rune-row {
  display: grid;
  grid-template-columns: 34px 1fr auto;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-left: 4px solid var(--rune);
  border-radius: 8px;
  padding: 6px;
}
.rune-row span { display: block; color: var(--muted); font-size: 12px; font-weight: 750; }
.status-actions {
  display: grid;
  grid-template-columns: repeat(4, 32px);
  gap: 4px;
}
.status-actions button {
  min-height: 32px;
  padding: 0;
  font-size: 12px;
}
.status-actions button.active { background: var(--rune); color: #11151f; border-color: var(--rune); }
.empty { color: var(--muted); text-align: center; padding: 18px; }

.room-input {
  max-width: 82px;
  text-align: center;
  letter-spacing: 2px;
  font-weight: 900;
}
.linked-list {
  display: grid;
  gap: 5px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}

.control-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: 1.3fr 1fr .8fr .8fr;
  gap: 7px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: rgba(17,21,31,.96);
  backdrop-filter: blur(14px);
}
.control-bar button { min-height: 52px; padding: 0 8px; }
.toast {
  position: fixed;
  left: 50%;
  bottom: 78px;
  transform: translateX(-50%) translateY(16px);
  opacity: 0;
  pointer-events: none;
  background: #05070b;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 10px 14px;
  color: var(--text);
  transition: .18s ease;
  max-width: calc(100vw - 32px);
  text-align: center;
  z-index: 10;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(3,5,9,.72);
}
.report-modal {
  width: min(430px, 100%);
  display: grid;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 12px;
  box-shadow: 0 18px 60px rgba(0,0,0,.38);
}
.choice-modal {
  width: min(390px, 100%);
  display: grid;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 14px;
  box-shadow: 0 18px 60px rgba(0,0,0,.38);
}
.button-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.stepper-control {
  display: grid;
  grid-template-columns: 52px 1fr 52px;
  gap: 8px;
  align-items: center;
}
.held-stepper {
  display: grid;
  gap: 10px;
}
.stepper-control b {
  display: grid;
  place-items: center;
  min-height: 52px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255,255,255,.04);
  color: var(--gold);
  font-size: 28px;
}
.stepper-control button {
  min-height: 52px;
  font-size: 24px;
}
.divider {
  height: 1px;
  background: var(--line);
  opacity: .8;
}
.field-stack {
  display: grid;
  gap: 6px;
  text-align: left;
}
.field-stack span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}
textarea {
  min-height: 120px;
  resize: vertical;
  border: 1px solid var(--line);
  background: #101620;
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font: inherit;
}
textarea.short-textarea {
  min-height: 82px;
}
.celebration {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 20;
  overflow: hidden;
}
.celebration i {
  position: absolute;
  left: calc((var(--i) * 5.5%) + 2%);
  top: -24px;
  width: 9px;
  height: 18px;
  border-radius: 2px;
  background: var(--gold);
  transform: rotate(calc(var(--i) * 18deg));
  animation: burst 1.8s cubic-bezier(.16,.72,.38,1) forwards;
}
.celebration i:nth-child(3n) { background: var(--teal); }
.celebration i:nth-child(3n + 1) { background: #ef6868; }
.celebration.loss i {
  background: var(--danger);
  width: 10px;
  height: 10px;
  border-radius: 50%;
  animation-duration: .8s;
}

@keyframes burst {
  0% { opacity: 0; transform: translateY(-30px) translateX(0) scale(.8) rotate(0deg); }
  8% { opacity: 1; }
  72% { opacity: 1; }
  100% { opacity: 0; transform: translateY(calc(96vh + var(--i) * 3px)) translateX(calc((var(--i) - 9) * 9px)) rotate(560deg); }
}
@keyframes rise-in {
  from { opacity: 0; transform: translateY(10px) scale(.985); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@media (min-width: 760px) {
  .table-app {
    width: min(460px, 100vw);
    margin: 0 auto;
    border-left: 1px solid var(--line);
    border-right: 1px solid var(--line);
  }
  .control-bar {
    left: 50%;
    width: min(460px, 100vw);
    transform: translateX(-50%);
  }
}

@media (max-width: 370px) {
  .button-row { flex-wrap: wrap; }
  .button-row > * { flex-basis: calc(50% - 4px); }
  .color-ledger-row { grid-template-columns: 30px 1fr auto; }
  .color-ledger-row small { grid-column: 2 / -1; }
}

@media (max-width: 430px) {
  .setup-card { padding: 10px; }
  .setup-colors { grid-template-columns: repeat(2, 1fr); }
  .setup-section .segmented button { font-size: 12px; padding: 0 5px; }
  .live-player { grid-template-columns: minmax(0, 1fr) 44px 44px 44px; }
  .topbar {
    grid-template-columns: minmax(0, 1fr) auto;
  }
  .mode-pill {
    grid-column: 1 / 2;
    justify-self: start;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .topbar button {
    min-height: 36px;
    padding: 0 8px;
    font-size: 12px;
  }
  .topbar button:last-child {
    grid-column: 2 / 3;
  }
}
