@font-face { font-family: 'Bungee'; src: url(bungee.woff2) format('woff2'); font-display: swap; }

:root {
  --dur: 240ms;
  --bar-h: 58px;
  --ui: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}
body.theme-classic {
  --felt: radial-gradient(ellipse at 50% 38%, #2f8f50 0%, #23733f 45%, #164d2a 100%);
  --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .55 0'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)'/%3E%3C/svg%3E");
  --grain-op: .16;
  --bar: rgba(9, 38, 20, .78);
  --bar-line: rgba(255, 255, 255, .08);
  --text: #f4f8f2;
  --muted: #b9d3c0;
  --btn: rgba(255, 255, 255, .12);
  --btn-hover: rgba(255, 255, 255, .2);
  --btn-text: #ffffff;
  --accent: #ffd34d;
  --slot: rgba(255, 255, 255, .26);
  --slot-bg: rgba(0, 0, 0, .1);
  --panel: #fbfcfa;
  --panel-text: #17221b;
  --panel-muted: #5a6b60;
  --pill: #1f7a43;
  --pill-text: #ffffff;
  --pill-alt: #e9efe9;
  --pill-alt-text: #17221b;
  --title-font: var(--ui);
  --hint: #ffd84a;
}
body.theme-atomic {
  --felt: radial-gradient(ellipse at 50% 35%, #8ccfbb 0%, #5fae9b 50%, #39806f 100%);
  --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.75' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .18 0 0 0 0 .14 0 0 0 0 .1 0 0 0 .7 0'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)'/%3E%3C/svg%3E");
  --grain-op: .22;
  --bar: #2b3031;
  --bar-line: #e2622a;
  --text: #f6ecd6;
  --muted: #b9c9c0;
  --btn: #3c4344;
  --btn-hover: #4b5354;
  --btn-text: #f6ecd6;
  --accent: #f2a14a;
  --slot: rgba(43, 49, 50, .4);
  --slot-bg: rgba(255, 250, 235, .14);
  --panel: #f6ecd6;
  --panel-text: #2b3031;
  --panel-muted: #6c6556;
  --pill: #d9551f;
  --pill-text: #fff8ea;
  --pill-alt: #e6d9bb;
  --pill-alt-text: #2b3031;
  --title-font: 'Bungee', var(--ui);
  --hint: #ff8a3d;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  position: fixed; inset: 0;
  font-family: var(--ui); color: var(--text);
  background: #1d5e33; background: var(--felt);
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
body::before {
  content: ''; position: fixed; inset: 0; pointer-events: none;
  background-image: var(--grain); opacity: var(--grain-op); mix-blend-mode: multiply;
}
body.theme-atomic::after { /* faint CRT scanlines + starbursts */
  content: ''; position: fixed; inset: 0; pointer-events: none; z-index: 1;
  background:
    repeating-linear-gradient(0deg, rgba(0,0,0,.035) 0 1px, transparent 1px 3px),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='420' height='420'%3E%3Cg fill='none' stroke='%23fff6e0' stroke-width='3' stroke-linecap='round' opacity='.16'%3E%3Cpath d='M70 50v40M50 70h40M56 56l28 28M84 56L56 84'/%3E%3Cpath d='M330 300v56M302 328h56M310 308l40 40M350 308l-40 40'/%3E%3Cpath d='M250 90q30-30 60 0' /%3E%3Cpath d='M120 330q20 -24 40 0'/%3E%3C/g%3E%3Cg fill='%23fff6e0' opacity='.14'%3E%3Ccircle cx='200' cy='200' r='4'/%3E%3Ccircle cx='380' cy='60' r='3'/%3E%3Ccircle cx='30' cy='250' r='3'/%3E%3C/g%3E%3C/svg%3E");
}

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; touch-action: manipulation; }
button:focus-visible, input:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- layout ---------- */
#app {
  position: absolute; inset: 0; z-index: 2;
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-rows: var(--bar-h) 1fr;
  grid-template-areas: "m h a" "b b b";
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.barcell { background: var(--bar); display: flex; align-items: center; gap: 8px; padding: 0 10px; border-bottom: 2px solid var(--bar-line); min-width: 0; }
#menuWrap { grid-area: m; }
#hdr { grid-area: h; justify-content: center; gap: clamp(10px, 2.4vw, 30px); }
#actions { grid-area: a; }
#board { grid-area: b; position: relative; overflow: hidden; touch-action: none; }

.tb {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  min-height: 44px; padding: 0 14px; border-radius: 12px;
  background: var(--btn); color: var(--btn-text);
  font-size: 17px; font-weight: 700; letter-spacing: .2px; white-space: nowrap;
  transition: background .15s, transform .1s;
}
.tb:hover { background: var(--btn-hover); }
.tb:active { transform: scale(.96); }
.tb svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.tb.sq { padding: 0 10px; }
.tb .off { display: none; }
.muted .tb .on { display: none; }
.muted .tb .off { display: block; }
.tb[disabled] { opacity: .45; }

.title { display: flex; flex-direction: column; align-items: flex-start; line-height: 1.05; padding: 4px 10px; border-radius: 10px; min-width: 0; }
.title:hover { background: var(--btn); }
#tName { font: 800 20px/1.1 var(--title-font); white-space: nowrap; }
.theme-atomic #tName { font-weight: 400; letter-spacing: .5px; color: var(--accent); }
#tVar { font-size: 14px; color: var(--muted); font-weight: 600; white-space: nowrap; }
#stats { display: flex; gap: clamp(10px, 2vw, 26px); }
.st { display: flex; flex-direction: column; align-items: center; min-width: 46px; line-height: 1.05; }
.st b { font-size: 21px; font-weight: 800; font-variant-numeric: tabular-nums; }
.st span { font-size: 13px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .6px; }

/* phone landscape / short screens */
@media (max-height: 520px) {
  :root { --bar-h: 48px; }
  .tb { min-height: 38px; font-size: 15px; padding: 0 10px; }
  .tb svg { width: 21px; height: 21px; }
  #tName { font-size: 17px; }
  #tVar { font-size: 13px; }
  .st b { font-size: 18px; }
  .st span { font-size: 11px; }
}
/* portrait: stats on top, big action bar at bottom */
@media (max-aspect-ratio: 4/5) {
  #app {
    grid-template-columns: auto 1fr;
    grid-template-rows: 54px 1fr 70px;
    grid-template-areas: "h h" "b b" "m a";
  }
  #hdr { justify-content: space-between; }
  #menuWrap, #actions { border-bottom: 0; border-top: 2px solid var(--bar-line); padding: 6px 8px; }
  #actions { justify-content: space-around; gap: 6px; }
  .tb { flex-direction: column; gap: 2px; min-height: 56px; font-size: 14px; padding: 0 6px; min-width: 62px; flex: 1; }
  #menuWrap .tb { min-width: 72px; }
  .tb.sq { flex: 0 0 52px; min-width: 52px; }
  .tb svg { width: 26px; height: 26px; }
  #stats { gap: 12px; }
  .st { min-width: 40px; }
  .st b { font-size: 19px; }
}
@media (max-aspect-ratio: 4/5) and (min-width: 700px) {
  #app { grid-template-rows: 64px 1fr 84px; }
  .tb { font-size: 17px; min-height: 64px; }
  .st b { font-size: 24px; }
  #tName { font-size: 24px; }
}
@media (max-aspect-ratio: 4/5) and (max-width: 360px) {
  .st span { font-size: 11px; }
  #tName { font-size: 17px; }
}

/* ---------- cards ---------- */
.slot, .card { position: absolute; left: 0; top: 0; width: var(--cw); height: var(--ch); }
.slot {
  border-radius: calc(var(--cw) * .07);
  border: 2px solid var(--slot); background: var(--slot-bg);
  display: flex; align-items: center; justify-content: center;
  color: var(--slot); font: 800 calc(var(--cw) * .42)/1 var(--ui);
}
.slot svg { width: 46%; height: 46%; }
.slot.hidden-slot { display: none; }
.slot.top-label { align-items: flex-start; padding-top: calc(var(--ch) * .05); font-size: calc(var(--cw) * .3); }
.card.buried .fc, .card.buried .bk { box-shadow: none; }
.slot-stock { cursor: pointer; }
.slot.drop, .card.drop .fc { box-shadow: 0 0 0 3px var(--hint), 0 0 16px 3px var(--hint); }
.card {
  transform: translate3d(-500px, -500px, 0);
  transition: transform var(--dur) cubic-bezier(.22, .8, .32, 1) var(--d, 0ms);
  cursor: pointer; perspective: 900px;
}
.ci {
  position: absolute; inset: 0; transform-style: preserve-3d;
  transform: rotateY(180deg);
  transition: transform calc(var(--dur) * 1.25) ease var(--d, 0ms);
}
.card.up .ci { transform: rotateY(0deg); }
.fc, .bk {
  position: absolute; inset: 0; border-radius: calc(var(--cw) * .065);
  background-size: 100% 100%; background-repeat: no-repeat;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .32), 0 2px 5px rgba(0, 0, 0, .12);
}
.bk { transform: rotateY(180deg); background-image: var(--back); }
.card.dragging { transition: none !important; }
.card.dragging .fc { box-shadow: 0 12px 26px rgba(0, 0, 0, .38), 0 2px 6px rgba(0, 0, 0, .25); }
.noanim .card, .noanim .ci { transition: none !important; }
.card.hint .fc, .card.hint .bk, .slot.hint { animation: hintGlow 1s ease-in-out 2; }
@keyframes hintGlow {
  0%, 100% { box-shadow: 0 0 0 0 transparent, 0 1px 2px rgba(0,0,0,.3); }
  50% { box-shadow: 0 0 0 4px var(--hint), 0 0 22px 6px var(--hint); }
}
.card.shake .ci { animation: shake .32s ease; }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-6%); } 50% { transform: translateX(5%); } 75% { transform: translateX(-3%); } }
.card.gone { visibility: hidden; }

/* ---------- toast & banner ---------- */
#toast {
  position: fixed; left: 50%; bottom: 22%; transform: translate(-50%, 20px); z-index: 50;
  background: rgba(20, 24, 22, .92); color: #fff; padding: 14px 22px; border-radius: 14px;
  font-size: 18px; font-weight: 600; max-width: 90vw; text-align: center;
  opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s;
  box-shadow: 0 8px 30px rgba(0,0,0,.3);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.theme-atomic #toast { background: #2b3031; color: #f6ecd6; border: 2px solid #e2622a; }
#banner {
  position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); z-index: 40;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap; justify-content: center;
  background: var(--panel); color: var(--panel-text); padding: 12px 14px 12px 20px; border-radius: 16px;
  font-size: 18px; font-weight: 600; box-shadow: 0 10px 34px rgba(0,0,0,.35); max-width: 94vw;
}
@media (max-aspect-ratio: 4/5) { #banner { bottom: 92px; } }
#banner.hidden { display: none; }
.bbtns { display: flex; gap: 8px; }

/* ---------- modal ---------- */
#modal {
  position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center;
  background: rgba(5, 15, 10, .5); padding: 12px; animation: fadeIn .2s ease;
}
#modal.hidden { display: none; }
#modal.clear { background: rgba(5, 15, 10, .18); }
@keyframes fadeIn { from { opacity: 0; } }
#panel:focus { outline: none; }
#panel {
  background: var(--panel); color: var(--panel-text); border-radius: 22px;
  width: min(640px, 100%); max-height: 100%; overflow: auto; padding: 22px 22px 18px;
  box-shadow: 0 20px 60px rgba(0,0,0,.4); animation: pop .22s cubic-bezier(.2,.9,.3,1.2);
  font-size: 17px; line-height: 1.45; user-select: text; -webkit-user-select: text;
}
.theme-atomic #panel { border: 3px solid #2b3031; box-shadow: 8px 8px 0 #2b3031, 0 20px 60px rgba(0,0,0,.3); }
@keyframes pop { from { transform: scale(.92); opacity: 0; } }
#panel h1 { font: 800 30px/1.1 var(--title-font); margin: 0 0 4px; }
.theme-atomic #panel h1 { font-weight: 400; color: #d9551f; text-shadow: 2px 2px 0 #2b3031; letter-spacing: .5px; }
#panel h2 { font: 800 21px/1.2 var(--title-font); margin: 18px 0 8px; }
.theme-atomic #panel h2 { font-weight: 400; font-size: 19px; }
#panel p { margin: 6px 0; }
.sub { color: var(--panel-muted); margin: 0 0 14px; }
.phead { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.xbtn { width: 46px; height: 46px; border-radius: 50%; font-size: 30px; line-height: 1; background: var(--pill-alt); color: var(--pill-alt-text); flex: none; }
.pill {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 50px; padding: 0 20px; border-radius: 14px; font-size: 18px; font-weight: 700;
  background: var(--pill); color: var(--pill-text); transition: filter .15s, transform .1s;
}
.pill:hover { filter: brightness(1.08); }
.pill:active { transform: scale(.97); }
.pill.alt { background: var(--pill-alt); color: var(--pill-alt-text); }
.pill.ghost { background: transparent; color: var(--panel-muted); min-width: 46px; padding: 0 10px; font-size: 26px; }
.theme-atomic .pill { border: 2px solid #2b3031; box-shadow: 3px 3px 0 #2b3031; }
.theme-atomic .pill.ghost { border: 0; box-shadow: none; }
.prow { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; }
.prow .pill { flex: 1 1 160px; }
.mlist { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 8px; }
.mlist .pill { width: 100%; }
@media (max-width: 420px) { .mlist { grid-template-columns: 1fr; } }

/* game picker */
.picker { display: grid; gap: 12px; margin-top: 10px; }
.gtile {
  display: grid; grid-template-columns: 92px 1fr; gap: 6px 16px; align-items: center;
  padding: 14px; border-radius: 18px; background: var(--pill-alt);
}
.theme-atomic .gtile { border: 2px solid #2b3031; }
.gtile.cur { box-shadow: 0 0 0 3px var(--pill); }
.gicon { grid-row: span 2; position: relative; height: 96px; }
.gicon i { position: absolute; width: 50px; height: 70px; left: 21px; top: 14px; background-size: 100% 100%; border-radius: 4px; box-shadow: 0 2px 5px rgba(0,0,0,.3); }
.gicon i:nth-child(1) { transform: rotate(-16deg) translateX(-14px); }
.gicon i:nth-child(3) { transform: rotate(16deg) translateX(14px); }
.gtile h3 { margin: 0; font: 800 23px/1.1 var(--title-font); }
.theme-atomic .gtile h3 { font-weight: 400; font-size: 21px; }
.gtile p { margin: 2px 0 0; color: var(--panel-muted); font-size: 16px; }
.gtile .prow { grid-column: 1 / -1; margin-top: 4px; }
.gtile .prow .pill { flex: 1 1 100px; min-height: 54px; }
.dealrow { display: flex; gap: 8px; align-items: center; flex: 2 1 200px; }
.dealrow input {
  width: 100%; min-width: 0; min-height: 54px; border-radius: 14px; border: 2px solid var(--panel-muted);
  font: 700 20px var(--ui); padding: 0 12px; background: #fff; color: #111;
}
.dealrow .pill { flex: 0 0 auto; }
@media (max-width: 460px) {
  .gtile { grid-template-columns: 64px 1fr; padding: 12px; }
  .gicon { height: 70px; }
  .gicon i { width: 36px; height: 50px; left: 14px; top: 10px; }
  .gtile h3 { font-size: 21px; }
  .gtile p { font-size: 15px; }
  #panel { padding: 18px 14px 14px; }
}

.gtile .pill.alt { background: var(--tile-btn, #ffffff); box-shadow: inset 0 0 0 2px rgba(0,0,0,.14); }
.theme-atomic .gtile .pill.alt { --tile-btn: #fbf3e0; box-shadow: 3px 3px 0 #2b3031; }
@media (min-width: 760px) {
  #panel.wide { width: min(1000px, 100%); }
  #panel.wide .picker { grid-template-columns: repeat(3, 1fr); align-items: stretch; }
  #panel.wide .gtile { grid-template-columns: 1fr; justify-items: center; text-align: center; align-content: start; padding: 16px 14px; }
  #panel.wide .gicon { grid-row: auto; width: 92px; }
  #panel.wide .gtile p { min-height: 2.9em; }
  #panel.wide .gtile .prow { flex-direction: column; width: 100%; }
  #panel.wide .gtile .prow .pill, #panel.wide .dealrow { flex: none; width: 100%; }
  #panel.wide .gtile .dealrow .pill { width: auto; flex: none; }
  #panel.wide .dealrow input { flex: 1; }
}
@media (min-width: 760px) and (max-height: 560px) {
  #panel.wide { padding: 14px 16px; }
  #panel.wide h1 { font-size: 24px; }
  #panel.wide .sub { margin-bottom: 6px; }
  #panel.wide .gicon, #panel.wide .gtile p { display: none; }
  #panel.wide .gtile { padding: 10px; gap: 4px; }
  #panel.wide .gtile .prow { gap: 7px; }
  #panel.wide .gtile .prow .pill, #panel.wide .dealrow input { min-height: 46px; }
}

/* settings */
.setrow { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-top: 1px solid rgba(0,0,0,.08); flex-wrap: wrap; }
.setrow > span { font-weight: 700; }
.setrow small { display: block; color: var(--panel-muted); font-weight: 500; font-size: 15px; }
.seg { display: inline-flex; background: var(--pill-alt); border-radius: 14px; padding: 4px; gap: 4px; }
.seg button { min-height: 44px; padding: 0 16px; border-radius: 11px; font-weight: 700; font-size: 16px; color: var(--pill-alt-text); }
.seg button.on { background: var(--pill); color: var(--pill-text); }
.gclink { display: inline-block; margin-top: 16px; color: var(--panel-muted); font-size: 15px; text-decoration: none; }
.gclink:hover { text-decoration: underline; }

/* stats */
.stgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; }
.stbox { background: var(--pill-alt); border-radius: 14px; padding: 10px 12px; text-align: center; }
.stbox b { display: block; font-size: 26px; font-variant-numeric: tabular-nums; }
.stbox span { font-size: 14px; color: var(--panel-muted); font-weight: 600; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0 12px; }

/* win */
.win { text-align: center; }
.win h1 { font-size: 40px !important; }
.win .stgrid { margin: 14px 0 6px; }
.badge { display: inline-block; background: var(--accent); color: #2b2100; border-radius: 99px; padding: 4px 14px; font-weight: 800; font-size: 15px; margin: 4px; }
.howto ul { padding-left: 22px; margin: 6px 0; }
.howto li { margin: 4px 0; }
kbd { background: var(--pill-alt); border-radius: 6px; padding: 1px 7px; font: 700 15px var(--ui); }

#fx { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 60; transition: opacity .8s; }
#fx.fade { opacity: 0; }

@media (prefers-reduced-motion: reduce) {
  :root { --dur: 0ms; }
  #panel, #modal { animation: none; }
}
