:root {
  --felt-1: #1d5c38;
  --felt-2: #14512f;
  --felt-dark: #0d3b22;
  --card-face: #fdfaf1;
  --card-edge: #d8d2be;
  --red: #c62f2f;
  --black: #1d2a44;
  --gold: #f0b429;
  --gold-2: #ffd766;
  --ink: #f4f1e6;
  --muted: #b9c7b4;
  --card-w: clamp(46px, 13vw, 80px);
  --card-h: calc(var(--card-w) * 1.44);
  --radius: calc(var(--card-w) * 0.09);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: radial-gradient(120% 90% at 50% 0%, var(--felt-1) 0%, var(--felt-2) 55%, var(--felt-dark) 100%);
  color: var(--ink);
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}
#app { display: flex; flex-direction: column; height: 100dvh; max-width: 560px; margin: 0 auto; }

/* ---------- top bar ---------- */
#topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px 4px;
  font-size: 13px; font-weight: 600; letter-spacing: .02em;
}
#topbar .stat { display: flex; flex-direction: column; align-items: center; min-width: 70px; }
#topbar .stat b { font-size: 15px; color: #fff; }
#topbar .stat span { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
#btn-menu {
  background: rgba(255,255,255,.08); border: 1px solid rgba(255,255,255,.15);
  color: var(--ink); border-radius: 10px; padding: 6px 10px; font-size: 15px; cursor: pointer;
}
#challenge-banner {
  display: none; align-items: center; gap: 8px;
  margin: 0 10px 6px; padding: 6px 10px;
  background: rgba(240,180,41,.12); border: 1px solid rgba(240,180,41,.45);
  border-radius: 10px; font-size: 12px; color: var(--gold-2);
}
#challenge-banner .cb-progress { flex: 1; height: 6px; background: rgba(0,0,0,.35); border-radius: 3px; overflow: hidden; }
#challenge-banner .cb-progress i { display: block; height: 100%; background: var(--gold); border-radius: 3px; transition: width .3s; }

/* ---------- table ---------- */
#table { flex: 1; position: relative; padding: 4px 8px; overflow: hidden; }
.row { display: flex; justify-content: space-between; }
#top-row { margin-bottom: 10px; }
.pile { position: relative; width: var(--card-w); height: var(--card-h); }
.pile.slot { border: 2px dashed rgba(255,255,255,.22); border-radius: var(--radius); }
.pile.slot::after {
  content: attr(data-ghost); position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--card-w) * .42); font-weight: 800;
  color: rgba(255,255,255,.14);
  pointer-events: none; /* decorative ghost must never swallow taps aimed at cards */
}
#foundations { display: flex; gap: calc(var(--card-w) * .12); }
#stock-wrap { display: flex; gap: calc(var(--card-w) * .12); }
#waste { margin-left: calc(var(--card-w) * .42); } /* room for the left-growing fan */
#tableau { margin-top: 4px; }
.col { position: relative; width: var(--card-w); flex: 0 0 var(--card-w); }

.card {
  position: absolute; left: 0; top: 0;
  width: var(--card-w); height: var(--card-h);
  border-radius: var(--radius);
  background: linear-gradient(160deg, #fffdf6 0%, var(--card-face) 60%, #f4efdd 100%);
  border: 1px solid #cfc8b0;
  box-shadow: 0 2px 4px rgba(0,0,0,.45), inset 0 0 0 1px rgba(255,255,255,.65);
  font-size: calc(var(--card-w) * .32);
  cursor: pointer;
  transition: transform .12s ease;
}
.card.down {
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,.10) 0 3px, transparent 3px 9px),
    repeating-linear-gradient(-45deg, rgba(255,255,255,.10) 0 3px, transparent 3px 9px),
    linear-gradient(160deg, #2d5da0 0%, #274e8d 55%, #1f3f74 100%);
  border-color: #16305a;
}
.card.down::after {
  content: ""; position: absolute; inset: 9%;
  border: 1.5px solid rgba(255,255,255,.4); border-radius: calc(var(--radius) * .7);
  background:
    repeating-linear-gradient(45deg, transparent 0 7px, rgba(255,255,255,.12) 7px 8px),
    repeating-linear-gradient(-45deg, transparent 0 7px, rgba(255,255,255,.12) 7px 8px);
}
.card .corner { position: absolute; top: 4%; left: 7%; line-height: 1; text-align: center; font-weight: 800; letter-spacing: -.02em; }
.card .corner small { display: block; font-size: .8em; margin-top: 1px; }
.card .corner.br {
  top: auto; left: auto; bottom: 4%; right: 7%;
  transform: rotate(180deg);
}
.card .pip {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--card-w) * .58);
  text-shadow: 0 1px 1px rgba(0,0,0,.08);
}
.card .pip.court { font-size: calc(var(--card-w) * .34); font-weight: 800; flex-direction: column; gap: 0; line-height: 1; }
.card .pip.court .cs { font-size: calc(var(--card-w) * .5); line-height: 1; }
.card.red { color: var(--red); } .card.black { color: var(--black); }
.card.moving { }
.shake { animation: shake .4s ease; }
@keyframes shake {
  15% { transform: translateX(-5px) rotate(-3deg); }
  40% { transform: translateX(5px) rotate(3deg); }
  65% { transform: translateX(-4px) rotate(-2deg); }
  85% { transform: translateX(2px) rotate(1deg); }
}
.card.flipin { animation: flipin .3s ease; }
@keyframes flipin { 0% { transform: rotateY(90deg) scale(.92); } 100% { transform: rotateY(0) scale(1); } }
.card.hint { outline: 3px solid var(--gold); outline-offset: 1px; animation: hintPulse 1s ease infinite; }
.pile.hint { outline: 3px solid var(--gold); outline-offset: 1px; animation: hintPulse 1s ease infinite; }
@keyframes hintPulse { 50% { outline-color: rgba(240,180,41,.25); } }
.card.drag-ghost { transform: scale(1.06); box-shadow: 0 6px 16px rgba(0,0,0,.5); }
.shake { animation: shake .3s ease; }
@keyframes shake { 25% { transform: translateX(-4px);} 75% { transform: translateX(4px);} }

/* ---------- bottom bar ---------- */
#navbar {
  display: flex; justify-content: space-around; align-items: stretch;
  background: rgba(0,0,0,.38); backdrop-filter: blur(6px);
  border-top: 1px solid rgba(255,255,255,.08);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
#navbar button {
  background: none; border: none; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 10px; font-family: inherit; cursor: pointer; padding: 4px 10px; border-radius: 10px;
}
#navbar button .ic { font-size: 20px; line-height: 1; }
#navbar button.active { color: var(--gold-2); }
#navbar button:disabled { opacity: .4; }

/* ---------- overlay screens ---------- */
.screen {
  position: fixed; inset: 0; z-index: 40;
  background: radial-gradient(120% 90% at 50% 0%, #17563a 0%, #0f3d26 60%, #0a2b1b 100%);
  display: none; flex-direction: column; align-items: center;
  overflow-y: auto; padding: 20px 16px 90px; max-width: 560px; margin: 0 auto;
}
.screen.open { display: flex; }
.screen h1 { font-size: 22px; margin: 4px 0 2px; color: #fff; }
.screen h2 { font-size: 15px; margin: 18px 0 8px; color: var(--gold-2); text-transform: uppercase; letter-spacing: .1em; }
.screen .sub { color: var(--muted); font-size: 13px; margin: 0 0 12px; }
.screen .close {
  position: absolute; top: 14px; right: 14px; z-index: 5;
  background: rgba(255,255,255,.08); border: 1px solid rgba(255,255,255,.15);
  color: var(--ink); width: 34px; height: 34px; border-radius: 50%; font-size: 16px; cursor: pointer;
}
.btn {
  background: var(--gold); color: #241a02; border: none; border-radius: 12px;
  padding: 12px 22px; font-size: 15px; font-weight: 700; font-family: inherit; cursor: pointer;
}
.btn.secondary { background: rgba(255,255,255,.12); color: var(--ink); }
.btn.danger { background: #b3402f; color: #fff; }

/* ---------- challenges screen ---------- */
.chal-card {
  width: 100%; max-width: 440px; margin: 0 0 12px;
  background: rgba(255,255,255,.06); border: 1px solid rgba(255,255,255,.12);
  border-radius: 16px; padding: 14px 16px;
}
.chal-card.done { border-color: rgba(240,180,41,.6); background: rgba(240,180,41,.08); }
.chal-card .ch-head { display: flex; align-items: center; gap: 10px; }
.chal-card .ch-icon { font-size: 26px; }
.chal-card .ch-name { font-weight: 700; color: #fff; font-size: 15px; }
.chal-card .ch-reward { margin-left: auto; color: var(--gold-2); font-weight: 700; font-size: 13px; white-space: nowrap; }
.chal-card .ch-desc { color: var(--muted); font-size: 13px; margin: 8px 0 10px; }
.chal-card .ch-bar { height: 8px; background: rgba(0,0,0,.4); border-radius: 4px; overflow: hidden; margin-bottom: 10px; }
.chal-card .ch-bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--gold), var(--gold-2)); border-radius: 4px; transition: width .4s; }
.chal-card .ch-foot { display: flex; align-items: center; justify-content: space-between; font-size: 12px; color: var(--muted); }
.chal-card .ch-foot .btn { padding: 7px 16px; font-size: 13px; }
#chal-countdown { font-size: 12px; color: var(--muted); margin-bottom: 14px; }

/* ---------- stats screen ---------- */
.level-ring { display: flex; align-items: center; gap: 16px; width: 100%; max-width: 440px; margin-bottom: 6px; }
.level-badge {
  width: 74px; height: 74px; border-radius: 50%; flex: 0 0 74px;
  background: radial-gradient(circle at 35% 30%, var(--gold-2), var(--gold) 70%);
  color: #241a02; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-weight: 800; line-height: 1.05; box-shadow: 0 3px 10px rgba(0,0,0,.4);
}
.level-badge small { font-size: 9px; text-transform: uppercase; letter-spacing: .08em; font-weight: 700; }
.level-badge b { font-size: 24px; }
.xp-col { flex: 1; }
.xp-col .xp-label { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.xp-bar { height: 10px; background: rgba(0,0,0,.4); border-radius: 5px; overflow: hidden; }
.xp-bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--gold), var(--gold-2)); }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; width: 100%; max-width: 440px; }
.stat-box { background: rgba(255,255,255,.06); border: 1px solid rgba(255,255,255,.1); border-radius: 14px; padding: 10px 14px; }
.stat-box b { display: block; font-size: 20px; color: #fff; }
.stat-box span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.ach-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; width: 100%; max-width: 440px; }
.ach {
  background: rgba(255,255,255,.05); border: 1px solid rgba(255,255,255,.1);
  border-radius: 14px; padding: 10px 8px; text-align: center;
}
.ach .a-icon { font-size: 24px; }
.ach .a-name { font-size: 11px; font-weight: 700; color: #fff; margin: 4px 0 2px; }
.ach .a-desc { font-size: 10px; color: var(--muted); }
.ach.locked { opacity: .38; filter: grayscale(.8); }
.ach.unlocked { border-color: rgba(240,180,41,.5); }
.streak-line { display:flex; align-items:center; gap:8px; font-size:13px; color: var(--muted); margin: 2px 0 10px; }
.streak-line b { color: var(--gold-2); }

/* ---------- toast / confetti ---------- */
#toast {
  position: fixed; left: 50%; bottom: 92px; transform: translateX(-50%) translateY(20px);
  background: rgba(20,20,20,.92); color: #fff; border: 1px solid rgba(255,255,255,.18);
  padding: 10px 18px; border-radius: 14px; font-size: 14px; z-index: 90;
  opacity: 0; transition: all .25s ease; max-width: 86vw; text-align: center; pointer-events: none;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.gold { border-color: var(--gold); color: var(--gold-2); font-weight: 700; }
.confetti { position: fixed; top: -12px; z-index: 80; font-size: 16px; pointer-events: none; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(105vh) rotate(660deg); opacity: .7; } }

/* win overlay */
#win-overlay {
  position: fixed; inset: 0; z-index: 60; display: none;
  align-items: center; justify-content: center; background: rgba(0,0,0,.55); backdrop-filter: blur(3px);
}
#win-overlay.open { display: flex; }
#win-box {
  background: #173a29; border: 1px solid rgba(240,180,41,.5); border-radius: 20px;
  padding: 26px 30px; text-align: center; max-width: 320px; width: 88%;
  animation: pop .3s ease;
}
@keyframes pop { 0% { transform: scale(1); } 45% { transform: scale(1.07); } 100% { transform: scale(1); } }
#win-box h2 { margin: 0 0 4px; font-size: 24px; color: var(--gold-2); }
#win-box .win-stats { color: var(--muted); font-size: 13px; margin: 8px 0 16px; line-height: 1.7; }
#win-box .win-stats b { color: #fff; }
#win-box .rowbtns { display: flex; gap: 10px; justify-content: center; }

/* modal dialog */
#modal {
  position: fixed; inset: 0; z-index: 70; display: none;
  align-items: center; justify-content: center; background: rgba(0,0,0,.55);
}
#modal.open { display: flex; }
#modal .box {
  background: #173a29; border: 1px solid rgba(255,255,255,.15); border-radius: 18px;
  padding: 22px 24px; max-width: 330px; width: 86%; text-align: center;
}
#modal .box p { color: var(--muted); font-size: 14px; margin: 6px 0 16px; }
#modal .box h3 { margin: 0 0 6px; color: #fff; }
#modal .rowbtns { display: flex; gap: 10px; justify-content: center; }

.settings-row { display: flex; align-items: center; justify-content: space-between; width: 100%; max-width: 440px;
  background: rgba(255,255,255,.05); border: 1px solid rgba(255,255,255,.1); border-radius: 14px; padding: 12px 16px; margin-bottom: 10px; font-size: 14px; }
.seg { display: flex; border: 1px solid rgba(255,255,255,.2); border-radius: 10px; overflow: hidden; }
.seg button { background: none; border: none; color: var(--muted); padding: 6px 14px; font-family: inherit; font-size: 13px; cursor: pointer; }
.seg button.on { background: var(--gold); color: #241a02; font-weight: 700; }
