:root {
  color-scheme: dark;
  --bg: #0d0e12;
  --panel: #1a1c23;
  --control: #2b2f3a;
  --fg: #f5f5f7;
  --muted: #a6a8b3;
  --accent: #e2b44c;
  --gap: 5px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }

/* ---------- Board ---------- */

#board {
  position: fixed;
  inset: 0;
  display: grid;
  gap: var(--gap);
  padding:
    calc(env(safe-area-inset-top) + var(--gap))
    calc(env(safe-area-inset-right) + var(--gap))
    calc(env(safe-area-inset-bottom) + var(--gap))
    calc(env(safe-area-inset-left) + var(--gap));
}

.cell {
  position: relative;
  overflow: hidden;
  border-radius: 16px;
  background-color: var(--pc);
  background-image: linear-gradient(160deg, rgba(255,255,255,.12), rgba(0,0,0,.28));
  transition: filter .3s, background-color .3s;
}
.cell.dead { filter: grayscale(.85) brightness(.55); }
.cell.chosen { animation: chosen .6s ease-in-out 6 alternate; }
@keyframes chosen {
  to { box-shadow: inset 0 0 0 6px var(--accent); filter: brightness(1.35); }
}

/* Rotated content frame; width/height are set from JS so rotated panels fill their cell. */
.inner {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) rotate(var(--rot, 0deg));
  container-type: size;
}

.tap { position: absolute; top: 0; bottom: 0; width: 50%; z-index: 1; transition: background .15s; }
.tap.minus { left: 0; }
.tap.plus { right: 0; }
.tap.press { background: rgba(255,255,255,.09); }

.hint {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  font-size: max(20px, 12cqmin);
  font-weight: 300;
  opacity: .4;
  pointer-events: none;
}
.hint.l { left: 6%; }
.hint.r { right: 6%; }

.life-wrap {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.life {
  font-size: min(46cqh, 32cqw);
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.03em;
  text-shadow: 0 2px 14px rgba(0,0,0,.35);
}
.delta, .status {
  height: 1.3em;
  font-size: max(14px, 8cqmin);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.delta.up { color: #b6f5c4; }
.delta.down { color: #ffc2c2; }
.status { text-transform: uppercase; letter-spacing: .12em; font-size: max(12px, 6cqmin); }

.name {
  position: absolute;
  top: 5%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  max-width: 62%;
  padding: .25em .8em;
  border-radius: 999px;
  background: rgba(0,0,0,.2);
  font-size: clamp(13px, 7cqmin, 24px);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Commander tax: top-left of each player's panel, shown only in Commander mode. */
.tax {
  position: absolute;
  top: 4%;
  left: 4%;
  z-index: 2;
  display: none;
  flex-direction: column;
  align-items: center;
  min-width: clamp(56px, 20cqmin, 96px);
  padding: .35em .6em;
  border-radius: 12px;
  background: rgba(0,0,0,.28);
  line-height: 1.15;
  transition: background .15s;
}
.cell.cmdr .tax { display: flex; }
.cell.cmdr .name { max-width: 40%; }
.tax.press { background: rgba(0,0,0,.45); }
.tax-v { font-size: clamp(13px, 6cqmin, 22px); font-weight: 700; font-variant-numeric: tabular-nums; }
.tax-l { font-size: clamp(10px, 4cqmin, 14px); opacity: .75; }

.badges {
  position: absolute;
  left: 4%;
  bottom: 5%;
  z-index: 2;
  max-width: 72%;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  pointer-events: none;
}
.badge {
  font-size: clamp(11px, 5cqmin, 16px);
  font-weight: 600;
  padding: .15em .55em;
  border-radius: 999px;
  background: rgba(0,0,0,.3);
}

.more {
  position: absolute;
  right: 4%;
  bottom: 5%;
  z-index: 2;
  width: clamp(34px, 13cqmin, 52px);
  height: clamp(34px, 13cqmin, 52px);
  border-radius: 50%;
  background: rgba(0,0,0,.25);
  font-size: clamp(18px, 7cqmin, 28px);
  line-height: 1;
}

/* ---------- Per-player counters sheet ---------- */

.sheet {
  --sheet-pad: max(10px, 4cqmin);
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: .7em;
  padding: var(--sheet-pad);
  overflow: auto;
  background: rgba(10,11,15,.95);
  font-size: clamp(13px, 5cqmin, 17px);
  touch-action: pan-y;
}
.sheet[hidden] { display: none; }
/* Pinned to the top of the sheet; bleeds into the sheet's padding so rows don't peek above it. */
.sheet-head {
  position: sticky;
  top: 0;
  z-index: 1;
  flex-shrink: 0;
  display: flex;
  gap: .5em;
  margin: calc(-1 * var(--sheet-pad)) calc(-1 * var(--sheet-pad)) 0;
  padding: var(--sheet-pad) var(--sheet-pad) .6em;
  background: #0b0c10;
  box-shadow: 0 6px 10px -6px rgba(0,0,0,.8);
}
.sheet-head input {
  flex: 1;
  min-width: 0;
  font: inherit;
  color: inherit;
  background: #23262f;
  border: 1px solid #3a3e4a;
  border-radius: 8px;
  padding: .4em .6em;
  user-select: text;
  -webkit-user-select: text;
}
.swatches { display: flex; flex-wrap: wrap; gap: .45em; }
.swatch { width: 1.9em; height: 1.9em; border-radius: 50%; background: var(--c); border: 2px solid transparent; }
.swatch.sel { border-color: #fff; }
.counters { display: flex; flex-direction: column; gap: .45em; }
.row { display: grid; grid-template-columns: 1fr auto 2.6em auto; align-items: center; gap: .4em; }
.row .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .v { text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.step { width: 2.3em; height: 2.3em; border-radius: 8px; background: var(--control); font-size: 1.1em; font-weight: 700; }
.step:active { background: #3a3f4d; }
.sec { margin-top: .3em; font-size: .78em; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.dot { display: inline-block; width: .7em; height: .7em; margin-right: .4em; border-radius: 50%; background: var(--c); }

/* ---------- Buttons ---------- */

.btn { background: var(--control); border-radius: 10px; padding: .6em 1em; font-weight: 600; }
.btn:active { background: #3a3f4d; }
.btn.primary { background: var(--accent); color: #1a1404; }
.btn.wide { display: block; width: 100%; margin-top: 12px; padding: .8em 1em; }

/* ---------- Menu ---------- */

#menuBtn {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 10;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: #14161c;
  border: 4px solid var(--bg);
  box-shadow: 0 2px 12px rgba(0,0,0,.5);
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(0,0,0,.6);
}
.overlay[hidden] { display: none; }

.menu-panel {
  width: min(440px, 100%);
  max-height: 100%;
  overflow: auto;
  padding: 4px 18px 18px;
  border-radius: 18px;
  background: var(--panel);
  box-shadow: 0 10px 40px rgba(0,0,0,.6);
}
.menu-panel h2 {
  margin: 18px 0 10px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--muted);
}
.label { margin: 10px 0 6px; font-size: 14px; color: var(--muted); }

.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button, .seg input {
  flex: 1;
  min-width: 44px;
  padding: 10px 0;
  border-radius: 10px;
  background: var(--control);
  font-weight: 600;
  text-align: center;
}
.seg input {
  min-width: 90px;
  border: 0;
  font: inherit;
  font-weight: 600;
  color: inherit;
  user-select: text;
  -webkit-user-select: text;
}
.seg .sel { background: var(--accent); color: #1a1404; }

.tools { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.toggle {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--control);
  cursor: pointer;
}
.toggle input { width: 22px; height: 22px; accent-color: var(--accent); }
.note { display: block; margin: 4px 0 0; font-size: 13px; color: var(--muted); }
.note:empty { display: none; }
.note.warn { color: #ffc2c2; }
.note.ok { color: #b6f5c4; }

#toast {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 30;
  max-width: calc(100% - 32px);
  padding: 22px 30px;
  border-radius: 18px;
  background: rgba(15,16,20,.96);
  box-shadow: 0 10px 40px rgba(0,0,0,.6);
  font-size: 26px;
  font-weight: 700;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(.9);
  transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }
