:root {
  --bg: #15131f;
  --panel: #262236;
  --panel2: #353050;
  --line: #0b0a12;
  --ink: #f4ecd8;
  --muted: #a49cbc;
  --accent: #ffcc33;
  --accent-ink: #2a1e00;
  --danger: #ff5d73;
  --felt: #2f7d4f;
  --felt2: #286c44;
  --cream: #f7f1e3;
  --card-ink: #2a2840;
  --red: #d63c3c;
  --edge: #1a1626;
  --back-edge: #6e93e6;
  --back-img: none;
  --px: 'Press Start 2P', ui-monospace, monospace;
  --word: 'Pixelify Sans', system-ui, sans-serif;
  --notch: polygon(0 2px, 2px 2px, 2px 0, calc(100% - 2px) 0, calc(100% - 2px) 2px, 100% 2px, 100% calc(100% - 2px), calc(100% - 2px) calc(100% - 2px), calc(100% - 2px) 100%, 2px 100%, 2px calc(100% - 2px), 0 calc(100% - 2px));
  --notch4: polygon(0 4px, 4px 4px, 4px 0, calc(100% - 4px) 0, calc(100% - 4px) 4px, 100% 4px, 100% calc(100% - 4px), calc(100% - 4px) calc(100% - 4px), calc(100% - 4px) 100%, 4px 100%, 4px calc(100% - 4px), 0 calc(100% - 4px));
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--word);
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; }
button:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }

svg.px { display: block; width: 100%; height: auto; image-rendering: pixelated; }

#app { height: 100vh; height: 100dvh; position: relative; overflow: hidden; }

.screen { display: none; height: 100%; }
.screen.active { display: flex; flex-direction: column; }

.btn {
  font-family: var(--px);
  font-size: 11px;
  line-height: 1.2;
  text-transform: uppercase;
  color: var(--ink);
  background: var(--panel2);
  border: 0;
  padding: 11px 14px;
  margin: 3px;
  cursor: pointer;
  box-shadow:
    inset -3px -3px 0 rgba(0, 0, 0, 0.35),
    inset 3px 3px 0 rgba(255, 255, 255, 0.14),
    0 0 0 3px var(--line);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn:active { transform: translateY(2px); box-shadow: inset 3px 3px 0 rgba(0, 0, 0, 0.35), 0 0 0 3px var(--line); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.small { font-size: 9px; padding: 9px 11px; }
.btn [data-icon] { width: 14px; display: inline-block; }
.btn[hidden] { display: none; }

.icon-btn {
  width: 38px;
  height: 38px;
  flex: none;
  border: 0;
  background: var(--panel2);
  color: var(--ink);
  cursor: pointer;
  padding: 10px;
  box-shadow: inset -3px -3px 0 rgba(0, 0, 0, 0.35), inset 3px 3px 0 rgba(255, 255, 255, 0.12), 0 0 0 3px var(--line);
  margin: 3px;
}
.icon-btn:active { transform: translateY(2px); }
.icon-btn [data-icon] { display: block; }

#screen-home {
  overflow-y: auto;
  padding: calc(16px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  background:
    radial-gradient(circle at 50% -10%, rgba(255, 204, 51, 0.08), transparent 55%),
    var(--bg);
}
.home-inner { width: 100%; max-width: 1040px; margin: 0 auto; }

.hero { text-align: center; padding: 10px 0 18px; }
.hero h1 {
  font-family: var(--px);
  font-size: clamp(22px, 7vw, 40px);
  line-height: 1.25;
  margin: 16px 0 10px;
  color: var(--accent);
  text-shadow: 3px 3px 0 #b5462f, 6px 6px 0 var(--line);
  letter-spacing: 1px;
}
.tagline { margin: 0; color: var(--muted); font-size: 17px; }

.hero-cards { display: flex; justify-content: center; height: 98px; }
.hero-cards .mini { --cw: 62px; --ch: 87px; margin: 0 -10px; }
.hero-cards .mini:nth-child(1) { transform: translateY(8px); }
.hero-cards .mini:nth-child(3) { transform: translateY(8px); }
.hero-cards .mini:nth-child(2) { z-index: 1; }

.modes {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 20px;
  margin: 8px 3px 0;
}

.mode {
  background: var(--panel);
  box-shadow: 0 0 0 3px var(--line), 0 6px 0 3px rgba(0, 0, 0, 0.45);
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.mode-preview {
  height: 118px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background-color: var(--felt);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='4' height='4' shape-rendering='crispEdges'%3E%3Crect width='1' height='1' fill='%23000' fill-opacity='.1'/%3E%3Crect x='2' y='2' width='1' height='1' fill='%23000' fill-opacity='.1'/%3E%3C/svg%3E");
  background-size: 8px 8px;
  box-shadow: inset 0 -3px 0 var(--line);
}
.mode-preview .mini { --cw: 64px; --ch: 90px; }
.mode-preview .mini:nth-child(2) { transform: translateY(10px); }

.mode-body { padding: 16px 16px 18px; display: flex; flex-direction: column; gap: 10px; flex: 1; }
.mode h2 { font-family: var(--px); font-size: 15px; margin: 0; color: var(--ink); }
.mode p { margin: 0; color: var(--muted); font-size: 16px; line-height: 1.3; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 3px; }
.chips button {
  font-family: var(--px);
  font-size: 9px;
  padding: 8px 9px;
  border: 0;
  cursor: pointer;
  background: var(--bg);
  color: var(--muted);
  box-shadow: 0 0 0 2px var(--line), inset 0 0 0 2px var(--panel2);
}
.chips button.on { background: var(--ink); color: var(--card-ink); box-shadow: 0 0 0 2px var(--line); }

.mode-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: auto; padding-top: 4px; }
.mode-actions .btn { flex: 1; }

.home-foot { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin: 28px 0 6px; }


.hud {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(6px + env(safe-area-inset-top)) calc(8px + env(safe-area-inset-right)) 6px calc(8px + env(safe-area-inset-left));
  background: var(--panel);
  box-shadow: 0 3px 0 var(--line);
  position: relative;
  z-index: 5;
}
.hud-title { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.hud-title span { font-family: var(--px); font-size: 11px; color: var(--accent); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-title small { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-stats { margin-left: auto; display: flex; gap: 12px; }
.hud-stat { text-align: right; }
.hud-stat small { display: block; font-family: var(--px); font-size: 7px; color: var(--muted); text-transform: uppercase; margin-bottom: 4px; white-space: nowrap; }
.hud-stat b { font-family: var(--px); font-size: 12px; font-weight: 400; }
.hud-stat.warn b { color: var(--danger); animation: blink 0.6s steps(2) infinite; }

.board {
  flex: 1;
  position: relative;
  overflow: hidden;
  isolation: isolate; /*keep card z-indexes inside the board*/
  z-index: 0;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  background-color: var(--felt);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='4' height='4' shape-rendering='crispEdges'%3E%3Crect width='1' height='1' fill='%23000' fill-opacity='.1'/%3E%3Crect x='2' y='2' width='1' height='1' fill='%23000' fill-opacity='.1'/%3E%3C/svg%3E");
  background-size: 8px 8px;
  box-shadow: inset 0 0 0 4px var(--felt2);
  --cw: 60px;
  --ch: 84px;
  margin: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

.toolbar {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 8px calc(8px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(8px + env(safe-area-inset-left));
  background: var(--panel);
  box-shadow: 0 -3px 0 var(--line);
  position: relative;
  z-index: 5;
}
.tool {
  flex: 1;
  max-width: 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  font-family: var(--px);
  font-size: 9px;
  text-transform: uppercase;
  border: 0;
  padding: 9px 4px 8px;
  margin: 3px;
  background: var(--panel2);
  cursor: pointer;
  box-shadow: inset -3px -3px 0 rgba(0, 0, 0, 0.35), inset 3px 3px 0 rgba(255, 255, 255, 0.12), 0 0 0 3px var(--line);
}
.tool [data-icon] { width: 18px; display: block; }
.tool:active { transform: translateY(2px); }
.tool:disabled { opacity: 0.4; cursor: default; transform: none; }
.tool[hidden] { display: none; }
#btn-auto { background: var(--accent); color: var(--accent-ink); }

.slot {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cw);
  height: var(--ch);
  background: rgba(0, 0, 0, 0.14);
  clip-path: var(--notch4);
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.28);
  cursor: default;
}
.slot::after {
  content: '';
  position: absolute;
  inset: 4px;
  border: 2px dashed rgba(255, 255, 255, 0.16);
}
.slot svg.px { width: 34%; }
.slot-a { font-family: var(--px); font-size: calc(var(--cw) * 0.26); }
.slot-stock { cursor: pointer; }
.slot-f svg.px { width: 30%; }

.card {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cw);
  height: var(--ch);
  transition: transform 0.2s cubic-bezier(0.2, 0.8, 0.3, 1), opacity 0.3s 0.2s;
  will-change: transform;
  cursor: grab;
}
.board.instant .card { transition: none !important; }
.card.dragging { transition: none; cursor: grabbing; filter: drop-shadow(4px 6px 0 rgba(0, 0, 0, 0.35)); }
.card.gone { opacity: 0; pointer-events: none; }
.card::before {
  content: '';
  position: absolute;
  inset: 2px -2px -2px 2px;
  background: rgba(0, 0, 0, 0.25);
  clip-path: var(--notch);
}
.card .ci {
  position: absolute;
  inset: 0;
  padding: 2px;
  background: var(--edge);
  clip-path: var(--notch);
}
.card .face, .card .back {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  clip-path: var(--notch);
}
.card:not(.up) .face, .card.up .back { display: none; }
.card .back {
  background-color: var(--back-edge);
  background-image: var(--back-img);
  background-size: calc(var(--cw) * 0.24) calc(var(--cw) * 0.24);
  background-position: center;
  image-rendering: pixelated;
  box-shadow: inset 0 0 0 calc(var(--cw) * 0.05) var(--back-edge), inset 0 0 0 calc(var(--cw) * 0.05 + 2px) var(--edge);
}

.card.flip .ci { animation: flip 0.24s steps(4); }
.card.shake .ci { animation: shake 0.32s steps(6); }
.card.hint::after, .slot.hint::before, .card.drop::after, .slot.drop::before {
  content: '';
  position: absolute;
  inset: -4px;
  box-shadow: 0 0 0 3px var(--accent);
  pointer-events: none;
  z-index: 2;
}
.card.hint::after, .slot.hint::before { animation: blink 0.3s steps(2) 5; }
.slot.hint, .slot.drop { clip-path: none; }

@keyframes flip { 0% { transform: scaleX(0); } 100% { transform: scaleX(1); } }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}
@keyframes blink { 50% { opacity: 0; } }

/* Classic faces */
.cf { position: relative; width: 100%; height: 100%; }
.cf.classic { background: var(--cream); color: var(--card-ink); }
.cf.classic.red { color: var(--red); }
.cf.classic .rk {
  position: absolute;
  left: 7%;
  top: 6%;
  font-family: var(--px);
  font-size: calc(var(--cw) * 0.2);
  line-height: 1;
  letter-spacing: -0.06em;
}
.cf.classic.ten .rk { letter-spacing: -0.14em; font-size: calc(var(--cw) * 0.18); }
.cf.classic .st { position: absolute; right: 7%; top: 6%; width: calc(var(--cw) * 0.22); }
.cf.classic .mid { position: absolute; left: 22%; right: 22%; top: 36%; bottom: 10%; display: flex; align-items: center; justify-content: center; }
.cf.classic .mid svg { width: 100%; }
.cf.classic .mid svg.ace { width: 115%; max-width: none; }
.cf.classic.is-court .mid { left: 14%; right: 14%; top: 30%; bottom: 6%; align-items: flex-end; }
.cf.classic.is-court .mid svg { width: 100%; }

/* Category faces */
.cf.catc { background: var(--cc); color: var(--ct); }
.cf.catc::before {
  content: '';
  position: absolute;
  inset: calc(var(--cw) * 0.05);
  border: 2px solid currentColor;
  opacity: 0.35;
}
.cc-name, .it-name {
  position: absolute;
  left: 5%;
  right: 5%;
  top: 4%;
  font-family: var(--word);
  font-weight: 700;
  font-size: calc(var(--cw) * 0.155);
  line-height: 1.02;
  text-align: center;
  overflow-wrap: break-word;
  hyphens: manual;
}
.cc-name { top: 7%; }
.cc-name.sm, .it-name.sm { font-size: calc(var(--cw) * 0.13); }
.cc-name.xs, .it-name.xs { font-size: calc(var(--cw) * 0.11); }
.cc-icon { position: absolute; left: 30%; right: 30%; top: 47%; }
.cc-count {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 9%;
  text-align: center;
  font-family: var(--px);
  font-size: calc(var(--cw) * 0.12);
  line-height: 1;
}
.card.on-slot .cc-count { color: var(--ct); }

.cf.item { background: var(--cream); color: var(--card-ink); }
.it-icon { position: absolute; left: 36%; right: 36%; bottom: 12%; color: #c9bfa6; }

/* Static mini cards on the home screen */
.card.mini { position: relative; display: inline-block; width: var(--cw); height: var(--ch); transition: none; cursor: default; flex: none; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(10, 8, 18, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal[hidden] { display: none; }
.window {
  width: 100%;
  max-width: 440px;
  max-height: 88vh;
  overflow-y: auto;
  background: var(--panel);
  padding: 22px 20px 18px;
  box-shadow: 0 0 0 3px var(--line), 0 0 0 7px var(--panel2), 0 0 0 10px var(--line), 0 14px 0 10px rgba(0, 0, 0, 0.4);
  animation: pop 0.18s steps(3);
}
@keyframes pop { from { transform: scale(0.85); } to { transform: scale(1); } }
.window h2 { font-family: var(--px); font-size: 15px; margin: 0 0 16px; text-align: center; color: var(--accent); line-height: 1.4; }
.window h3 { font-family: var(--px); font-size: 10px; margin: 18px 0 8px; color: var(--accent); }
.window p { margin: 0 0 10px; line-height: 1.4; font-size: 17px; }
.window .muted { color: var(--muted); }
.window-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 18px; }
.window-actions .btn { flex: 1 1 auto; }
.kbd { display: inline-block; padding: 1px 6px; background: var(--bg); box-shadow: 0 0 0 2px var(--line); font-size: 14px; vertical-align: middle; }
.kbd svg.px { width: 14px; display: inline-block; vertical-align: -2px; color: var(--accent); }

.stat-rows { margin: 0; display: grid; gap: 8px; }
.stat-rows div { display: flex; justify-content: space-between; background: var(--bg); padding: 10px 12px; box-shadow: 0 0 0 2px var(--line); }
.stat-rows dt { color: var(--muted); }
.stat-rows dd { margin: 0; font-family: var(--px); font-size: 12px; }

.stars { display: flex; justify-content: center; gap: 10px; margin: 0 0 18px; }
.stars span { width: 42px; color: #4a4362; }
.stars span.on { color: var(--accent); animation: pop 0.3s steps(3) both; }
.stars span.on:nth-child(2) { animation-delay: 0.15s; }
.stars span.on:nth-child(3) { animation-delay: 0.3s; }

.table-wrap { overflow-x: auto; }
table.stats { width: 100%; border-collapse: collapse; font-size: 15px; }
table.stats th, table.stats td { padding: 8px 6px; text-align: right; white-space: nowrap; }
table.stats thead th { font-family: var(--px); font-size: 7px; color: var(--muted); font-weight: 400; }
table.stats tbody th { text-align: left; font-weight: 600; }
table.stats tbody tr:nth-child(odd) { background: var(--bg); }

.set-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 2px solid var(--line); }
.set-row.col { flex-direction: column; align-items: flex-start; }
.set-row small { display: block; color: var(--muted); font-size: 14px; margin-top: 2px; }
.switch {
  font-family: var(--px);
  font-size: 10px;
  min-width: 64px;
  padding: 9px 10px;
  border: 0;
  cursor: pointer;
  background: var(--bg);
  color: var(--muted);
  box-shadow: 0 0 0 3px var(--line);
}
.switch.on { background: #4cd07d; color: #0d2a17; }
.swatches { display: flex; gap: 12px; flex-wrap: wrap; padding: 3px; }
.swatch {
  width: 40px;
  height: 40px;
  border: 0;
  cursor: pointer;
  background: linear-gradient(135deg, var(--a) 50%, var(--b) 50%);
  box-shadow: 0 0 0 3px var(--line);
}
.swatch.on { box-shadow: 0 0 0 3px var(--line), 0 0 0 6px var(--accent); }

.toast {
  position: fixed;
  left: 50%;
  top: calc(64px + env(safe-area-inset-top));
  transform: translate(-50%, -20px);
  z-index: 45;
  background: var(--panel);
  color: var(--ink);
  font-family: var(--px);
  font-size: 10px;
  line-height: 1.5;
  padding: 10px 14px;
  max-width: calc(100vw - 32px);
  text-align: center;
  box-shadow: 0 0 0 3px var(--line), 0 4px 0 3px rgba(0, 0, 0, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s steps(3);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.big {
  top: 38%;
  font-size: 14px;
  padding: 16px 20px;
  color: var(--accent-ink);
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--line), 0 0 0 6px var(--ink), 0 0 0 9px var(--line), 0 8px 0 9px rgba(0, 0, 0, 0.4);
}

.fx { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 40; image-rendering: pixelated; }
#fx-trail { opacity: 0; transition: opacity 0.35s; }
#fx-trail.on { opacity: 1; pointer-events: auto; cursor: pointer; }

@media (min-width: 700px) {
  .hud { padding-left: 16px; padding-right: 16px; }
  .hud-stats { gap: 22px; }
  .hud-stat b { font-size: 14px; }
  .hud-title span { font-size: 13px; }
  .tool { font-size: 10px; }
}

@media (max-width: 380px) {
  .hud-stats { gap: 8px; }
  .hud-stat b { font-size: 10px; }
  .hud-title small { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .card, .toast { transition: none; }
  .card.flip .ci, .card.shake .ci, .window { animation: none; }
}
