/* JAMdeck — all color/shape via tokens; themes only redefine tokens. */

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }

:root {
  --font: ui-rounded, "SF Pro Rounded", "Segoe UI Variable Display", "Segoe UI", "Nunito", system-ui, sans-serif;
  --radius: 15px;
  --cw: 80px;
  --ch: 112px;
  --anim-ms: 200ms;
  color-scheme: light;
}

/* ---------- Theme: Felt (default light — rich teal table) ---------- */
:root, :root[data-theme="felt"] {
  --table: radial-gradient(ellipse at 50% 28%, rgba(255, 255, 255, 0.07) 0%, transparent 62%),
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.015) 0 2px, transparent 2px 4px),
    linear-gradient(180deg, #1d8f70, #0f6b52);
  --table-flat: #17795f;
  --surface: #ffffff;
  --surface2: #f4f5f1;
  --ink: #1d2433;
  --muted: #5c6577;
  --border: #e2e3dc;
  --accent: #e2570e;
  --accent-ink: #ffffff;
  --teal: #0a9d90;
  --grape: #6b4de6;
  --danger: #d64545;
  --scrim: rgba(15, 20, 30, 0.45);
  --shadow: 0 6px 20px rgba(15, 25, 20, 0.25);
  --card-face: #fdfcf5;
  --card-edge: #b9bfb6;
  --card-ink: #26262b;
  --card-red: #d6262e;
  --card-blue: #1c5fd6;
  --card-green: #157a34;
  --back-a: #a8352c;
  --back-b: #7e211a;
  --pile-line: rgba(255, 255, 255, 0.4);
  --pile-fill: rgba(255, 255, 255, 0.05);
  --hud-ink: #eaf4ec;
  --glass: rgba(255, 255, 255, 0.92);
  --focus: #ffd34d;
}

/* ---------- Theme: Dark (Popsicle Dark) ---------- */
:root[data-theme="dark"] {
  color-scheme: dark;
  --table: radial-gradient(ellipse at 50% 25%, #2c374022 0%, transparent 60%), linear-gradient(180deg, #1d2b23, #141f19);
  --table-flat: #18251d;
  --surface: #241f33;
  --surface2: #2e2840;
  --ink: #f2eef9;
  --muted: #a69dba;
  --border: #3b3352;
  --accent: #ff8a3d;
  --accent-ink: #2a1206;
  --teal: #2fd4c4;
  --grape: #ab8dff;
  --danger: #ff6b78;
  --scrim: rgba(0, 0, 0, 0.55);
  --shadow: 0 10px 26px rgba(0, 0, 0, 0.42);
  --card-face: #ece9e2;
  --card-edge: #57504a;
  --card-ink: #201f24;
  --card-red: #c3232d;
  --card-blue: #1d63cf;
  --card-green: #157a34;
  --back-a: #4d3f86;
  --back-b: #372c62;
  --pile-line: rgba(255, 255, 255, 0.22);
  --pile-fill: rgba(255, 255, 255, 0.05);
  --hud-ink: #d9e4dc;
  --glass: rgba(36, 31, 51, 0.92);
  --focus: #ffd34d;
}

/* ---------- Theme: Windows 95 nostalgia ---------- */
:root[data-theme="win95"] {
  --font: Tahoma, "MS Sans Serif", "Segoe UI", system-ui, sans-serif;
  --radius: 0px;
  --table: #008000;
  --table-flat: #008000;
  --surface: #c0c0c0;
  --surface2: #d4d0c8;
  --ink: #000000;
  --muted: #444444;
  --border: #808080;
  --accent: #000080;
  --accent-ink: #ffffff;
  --teal: #008080;
  --grape: #800080;
  --danger: #aa0000;
  --scrim: rgba(0, 0, 0, 0.4);
  --shadow: 2px 2px 0 rgba(0, 0, 0, 0.35);
  --card-face: #ffffff;
  --card-edge: #000000;
  --card-ink: #000000;
  --card-red: #ff0000;
  --card-blue: #0000ff;
  --card-green: #008000;
  --back-a: #0000aa;
  --back-b: #000077;
  --pile-line: rgba(255, 255, 255, 0.55);
  --pile-fill: rgba(0, 0, 0, 0.08);
  --hud-ink: #ffffff;
  --glass: #c0c0c0;
  --focus: #ffff00;
}

/* ---------- Theme: Popsicle (light, playful table) ---------- */
:root[data-theme="popsicle"] {
  --table: radial-gradient(ellipse at 50% 20%, #ffe9d833 0%, transparent 55%), linear-gradient(180deg, #f6f5fa, #eceaf6);
  --table-flat: #f1eff8;
  --surface: #ffffff;
  --surface2: #efedf8;
  --ink: #241f33;
  --muted: #6f6885;
  --border: #e2def0;
  --accent: #e2570e;
  --accent-ink: #ffffff;
  --teal: #0a9d90;
  --grape: #6b4de6;
  --danger: #d64545;
  --scrim: rgba(36, 31, 51, 0.4);
  --shadow: 0 6px 20px rgba(60, 45, 90, 0.14);
  --card-face: #ffffff;
  --card-edge: #cfc8e4;
  --card-ink: #241f33;
  --card-red: #d43a44;
  --card-blue: #2456d0;
  --card-green: #0a9d63;
  --back-a: #e2570e;
  --back-b: #b8430a;
  --pile-line: rgba(107, 77, 230, 0.35);
  --pile-fill: rgba(107, 77, 230, 0.06);
  --hud-ink: #241f33;
  --glass: rgba(255, 255, 255, 0.92);
  --focus: #6b4de6;
}

body {
  font-family: var(--font);
  background: var(--table-flat);
  color: var(--ink);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  display: flex;
  flex-direction: column;
  overscroll-behavior: none;
  position: fixed;
  inset: 0;
}

/* ---------------- top bar ---------------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: calc(6px + env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 6px max(10px, env(safe-area-inset-left));
  background: var(--glass);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  z-index: 50;
}
:root[data-theme="win95"] #topbar { border-bottom: 2px solid #808080; box-shadow: inset 0 1px 0 #fff; }

.brand {
  display: flex;
  align-items: center;
  gap: 7px;
  font-weight: 700;
  font-size: 17px;
  letter-spacing: 0.2px;
}
.brand img { width: 24px; height: 24px; image-rendering: pixelated; border-radius: 5px; }
:root[data-theme="win95"] .brand img { border-radius: 0; }

.switcher {
  display: flex;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) - 4px);
  overflow: hidden;
  background: var(--surface2);
}
.switcher button {
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 6px 12px;
  color: var(--muted);
  cursor: pointer;
}
.switcher button.active { background: var(--accent); color: var(--accent-ink); }

.hud {
  display: flex;
  align-items: center;
  gap: 10px;
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-left: auto;
}
.hud .deal-chip {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px;
  color: var(--ink);
  cursor: pointer;
}
.hud span[hidden] { display: none; }

.actions { display: flex; align-items: center; gap: 6px; }

button.tb {
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 7px 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  border-radius: calc(var(--radius) - 4px);
  cursor: pointer;
  min-height: 34px;
}
button.tb:active { transform: translateY(1px); }
button.tb:disabled { opacity: 0.4; cursor: default; }
button.tb.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
:root[data-theme="win95"] button.tb,
:root[data-theme="win95"] .switcher { border: 2px solid; border-color: #fff #6d6d6d #6d6d6d #fff; background: #c0c0c0; box-shadow: 1px 1px 0 #000; border-radius: 0; }
:root[data-theme="win95"] .switcher button.active { background: #000080; color: #fff; }

.lda {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  background: var(--surface2);
}
.lda button {
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 11px;
  font-weight: 700;
  width: 26px;
  height: 26px;
  color: var(--muted);
  cursor: pointer;
}
.lda button.active { background: var(--accent); color: var(--accent-ink); }

/* ---------------- board ---------------- */
#boardwrap { flex: 1; position: relative; overflow: hidden; background: var(--table); }
#board {
  position: absolute;
  inset: 0;
  touch-action: none;
  padding-bottom: env(safe-area-inset-bottom);
}

.placeholder {
  position: absolute;
  width: var(--cw);
  height: var(--ch);
  border: 1.5px solid var(--pile-line);
  border-radius: calc(var(--cw) * 0.09 + 2px);
  background: var(--pile-fill);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--pile-line);
  font-size: calc(var(--cw) * 0.42);
  transition: box-shadow 0.15s, border-color 0.15s;
}
:root[data-theme="win95"] .placeholder { border-radius: 0; }
.placeholder .ph-glyph { opacity: 0.9; }
.placeholder.ph-free::after { content: ""; }
.placeholder.recycle { font-size: calc(var(--cw) * 0.34); }

/* ---------------- cards ---------------- */
.card {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cw);
  height: var(--ch);
  border-radius: calc(var(--cw) * 0.09);
  background: var(--card-face);
  border: 1px solid var(--card-edge);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.18);
  transition: transform var(--anim-ms) cubic-bezier(0.25, 0.9, 0.35, 1);
  will-change: transform;
  cursor: pointer;
  contain: layout style;
}
:root[data-theme="win95"] .card { border-radius: 3px; box-shadow: 1px 1px 0 rgba(0, 0, 0, 0.4); }

.card .face { position: absolute; inset: 0; color: var(--card-ink); }
.card.s1 .face, .card.s2 .face { color: var(--card-red); }
body.fourcolor .card.s0 .face { color: var(--card-green); }
body.fourcolor .card.s1 .face { color: var(--card-blue); }

/* index strip across the top — stays readable at tight stack overlap */
.idx {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 24%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 9%;
  line-height: 1;
}
.idx b { font-size: calc(var(--cw) * 0.30); font-weight: 800; letter-spacing: -0.5px; }
.idx i { font-style: normal; font-size: calc(var(--cw) * 0.28); }
body.cards-large .idx b { font-size: calc(var(--cw) * 0.36); }
body.cards-large .idx i { font-size: calc(var(--cw) * 0.33); }

/* one large pip (or court emblem) on the exposed body */
.bigpip {
  position: absolute;
  left: 0;
  right: 0;
  top: 22%;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.bigpip .art { font-size: calc(var(--cw) * 0.78); }
.bigpip .art.court { font-size: calc(var(--cw) * 0.72); }

.card .back {
  display: none;
  position: absolute;
  inset: 3px;
  border-radius: calc(var(--cw) * 0.06);
}
.card.facedown .face { display: none; }
.card.facedown .back { display: block; }
.card.facedown { cursor: default; }

/* card back designs */
.back { background: var(--back-a); }
body[data-back="classic"] .back {
  background: repeating-linear-gradient(45deg, var(--back-a) 0 4px, var(--back-b) 4px 8px);
}
body[data-back="diamond"] .back {
  background-color: var(--back-b);
  background-image: repeating-linear-gradient(45deg, var(--back-a) 0 6px, transparent 6px 12px), repeating-linear-gradient(-45deg, var(--back-a) 0 6px, transparent 6px 12px);
}
body[data-back="shells"] .back {
  background-color: var(--back-b);
  background-image: radial-gradient(circle at 50% 120%, var(--back-a) 20%, transparent 45%);
  background-size: 14px 14px;
}
body[data-back="pixel"] .back {
  background-color: var(--back-b);
  background-image: conic-gradient(var(--back-a) 25%, transparent 0 50%, var(--back-a) 0 75%, transparent 0);
  background-size: 6px 6px;
  image-rendering: pixelated;
}

/* interaction states */
.card.dragging {
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.35);
  cursor: grabbing;
}
.card.selected {
  box-shadow: 0 0 0 2.5px var(--focus), 0 0 16px 2px color-mix(in srgb, var(--focus) 65%, transparent);
}
.card.kb-cursor { outline: 3px dashed var(--focus); outline-offset: 1px; }
.placeholder.kb-cursor { outline: 3px dashed var(--focus); outline-offset: 1px; }
.droptarget { box-shadow: 0 0 0 2.5px var(--focus), 0 0 14px 1px color-mix(in srgb, var(--focus) 55%, transparent); }
.droptarget.hover { box-shadow: 0 0 0 4px var(--accent), 0 0 18px var(--accent); }
.card.peek { z-index: 900 !important; box-shadow: 0 8px 20px rgba(0,0,0,.4); }

@keyframes shake {
  0%, 100% { translate: 0 0; }
  25% { translate: -5px 0; }
  50% { translate: 5px 0; }
  75% { translate: -3px 0; }
}
.card.shake { animation: shake 0.28s ease; }

/* ---------------- indicators ---------------- */
#banner {
  position: absolute;
  left: 50%;
  bottom: calc(14px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--glass);
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 14px;
  font-size: 13.5px;
  display: flex;
  gap: 10px;
  align-items: center;
  z-index: 60;
  max-width: min(94vw, 480px);
}
#banner[hidden] { display: none; }
#banner .tb { padding: 5px 10px; min-height: 0; }

#solvebadge {
  font-size: 11.5px;
  font-weight: 700;
  border-radius: 999px;
  padding: 3px 9px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--muted);
  cursor: default;
}
#solvebadge.good { color: #fff; background: var(--teal); border-color: var(--teal); }
#solvebadge.bad { color: #fff; background: var(--danger); border-color: var(--danger); }
#solvebadge[hidden] { display: none; }

#btnAuto {
  position: absolute;
  left: 50%;
  bottom: calc(64px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 60;
  box-shadow: var(--shadow);
}
#btnAuto[hidden] { display: none; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(80px);
  background: var(--ink);
  color: var(--surface);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13.5px;
  z-index: 3000;
  transition: transform 0.25s ease;
  display: flex;
  gap: 12px;
  align-items: center;
  max-width: 92vw;
}
#toast.show { transform: translateX(-50%) translateY(0); }
#toast button { background: transparent; border: 0; color: var(--accent); font: inherit; font-weight: 700; cursor: pointer; }
:root[data-theme="dark"] #toast { background: var(--surface2); color: var(--ink); border: 1px solid var(--border); }

/* ---------------- modals ---------------- */
.modal {
  position: fixed;
  inset: 0;
  background: var(--scrim);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}
.modal[hidden] { display: none; }
.modal-card {
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  width: min(560px, 100%);
  max-height: 100%;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding: 18px;
}
.modal-card h2 { font-size: 18px; margin-bottom: 4px; display: flex; align-items: center; gap: 8px; }
.modal-card h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); margin: 18px 0 6px; }
.modal-card p.sub { color: var(--muted); font-size: 13.5px; margin-bottom: 10px; }
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.modal-close { border: 0; background: var(--surface2); color: var(--muted); width: 32px; height: 32px; border-radius: 999px; font-size: 16px; cursor: pointer; flex: none; }

.menu-list { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
.menu-list button {
  display: flex;
  align-items: center;
  gap: 10px;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  text-align: left;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) - 4px);
  background: var(--surface2);
  color: var(--ink);
  cursor: pointer;
  min-height: 44px;
}
.menu-list button:active { background: var(--border); }
.menu-list .mi { width: 22px; text-align: center; }

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14.5px;
  min-height: 44px;
}
.row:last-child { border-bottom: 0; }
.row .lbl { flex: 1; }
.row .lbl small { display: block; color: var(--muted); font-size: 12px; margin-top: 1px; }

/* toggle switch */
.toggle { position: relative; width: 46px; height: 28px; flex: none; }
.toggle input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.toggle .knob {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: var(--border);
  transition: background 0.15s;
  pointer-events: none;
}
.toggle .knob::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: left 0.15s;
}
.toggle input:checked + .knob { background: var(--accent); }
.toggle input:checked + .knob::after { left: 21px; }
.toggle input:focus-visible + .knob { outline: 2px solid var(--focus); }

.seg { display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; flex: none; }
.seg button {
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  border: 0;
  background: var(--surface2);
  color: var(--muted);
  padding: 7px 11px;
  cursor: pointer;
  min-height: 34px;
}
.seg button.active { background: var(--accent); color: var(--accent-ink); }

select.ctl, input.ctl {
  font: inherit;
  font-size: 14px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface2);
  color: var(--ink);
  max-width: 170px;
}

.btnrow { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.btnrow .tb { flex: 1; min-width: 120px; }

/* ---------------- stats ---------------- */
.statgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; margin: 8px 0; }
.stat {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) - 4px);
  padding: 9px 11px;
}
.stat .v { font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .v a { color: var(--accent); text-decoration: none; font-size: 13px; font-weight: 600; }
.stat .k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; margin-top: 2px; }

.chart { width: 100%; height: auto; background: var(--surface2); border: 1px solid var(--border); border-radius: calc(var(--radius) - 4px); margin: 6px 0; }
.chart .bar-fc { fill: var(--accent); }
.chart .bar-kl { fill: var(--teal); }
.chart .line-fc { stroke: var(--accent); fill: none; stroke-width: 2.5; }
.chart circle.line-fc { fill: var(--accent); stroke: none; }
.chart .line-kl { stroke: var(--teal); fill: none; stroke-width: 2.5; }
.chart circle.line-kl { fill: var(--teal); stroke: none; }
.chart-grid { stroke: var(--border); stroke-width: 1; }
.chart-label { fill: var(--muted); font-size: 10px; font-family: var(--font); }
.chart-empty { fill: var(--muted); font-size: 13px; font-family: var(--font); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); align-items: center; }
.legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; }

.recent { margin-top: 6px; }
.recent .rg {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
  font-variant-numeric: tabular-nums;
}
.recent .rg:last-child { border-bottom: 0; }
.recent .res { width: 20px; text-align: center; }
.recent .meta { color: var(--muted); font-size: 12px; margin-left: auto; }
.recent button.replay {
  font: inherit; font-size: 12px; font-weight: 700;
  border: 1px solid var(--border); background: var(--surface2); color: var(--accent);
  border-radius: 999px; padding: 4px 10px; cursor: pointer;
}

/* win dialog */
.win-stats { display: flex; gap: 18px; font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; margin: 10px 0; }

/* celebration canvas */
canvas.celebrate { position: fixed; inset: 0; z-index: 1500; }

/* ---------------- responsive ---------------- */
@media (max-width: 640px) {
  .brand span.bt { display: none; }
  .hud { gap: 7px; font-size: 12px; }
  button.tb { padding: 7px 9px; font-size: 12.5px; }
  #topbar { gap: 6px; }
}
@media (max-width: 400px) {
  .hud .deal-chip { padding: 3px 7px; }
}

@media (prefers-reduced-motion: reduce) {
  .card { transition: none; }
  #toast { transition: none; }
}
