/* Nian — midnight-blue sibling of the Svit table. Moonlight, not gold. */

@font-face {
  font-family: 'Limelight';
  src: url('../assets/limelight.woff2') format('woff2');
  font-weight: 400;
  font-display: swap;
}

:root {
  --felt: #12233c;
  --felt-lo: #0b1524;
  --line: rgba(154, 183, 224, 0.25);
  --moon: #9ab7e0;
  --moon-hi: #cfe0f5;
  --bone: #f2efe6;
  --ink: #232936;
  --red: #c94f4f;
  --card-back: #26436b;
  --card-back-line: #35578a;
  --danger: #e05252;
  --win: #7fd6a4;
  --card-w: clamp(64px, calc((100vw - 16px - 2 * 10px) / 3 - 14px), 108px);
  --card-h: calc(var(--card-w) * 1.42);
  --radius: calc(var(--card-w) * 0.1);
  font-size: 16px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; overscroll-behavior: none; }

body {
  background:
    radial-gradient(140% 90% at 50% 0%, var(--felt) 0%, var(--felt-lo) 78%),
    var(--felt-lo);
  color: var(--bone);
  font-family: 'Avenir Next', 'Segoe UI', 'Helvetica Neue', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  display: flex;
  justify-content: center;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.05;
  background-image: repeating-conic-gradient(rgba(255,255,255,0.6) 0% 25%, transparent 0% 50%);
  background-size: 3px 3px;
}

#app {
  width: 100%;
  max-width: 470px;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(env(safe-area-inset-top), 8px) 8px calc(env(safe-area-inset-bottom) + 10px);
  position: relative;
}

/* ---------- top bar ---------- */

#topbar {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 4px 2px 10px;
}

.wordmark {
  font-family: 'Limelight', cursive;
  font-weight: 400;
  font-size: 1.7rem;
  letter-spacing: 0.06em;
  color: var(--moon);
  text-shadow: 0 1px 0 rgba(0,0,0,0.5);
  line-height: 1;
}

#countdown-wrap {
  margin-left: auto;
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.count-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.22em;
  color: rgba(242, 239, 230, 0.55);
}

#countdown {
  font-size: 1.55rem;
  font-variant-numeric: tabular-nums;
  color: var(--moon-hi);
  font-weight: 600;
  line-height: 1;
}

#countdown.low { color: var(--win); }

.icon-btn {
  background: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--bone);
  font-size: 1rem;
  width: 34px;
  height: 34px;
  align-self: center;
  cursor: pointer;
  flex: none;
}

/* ---------- the nine ---------- */

#grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  justify-items: center;
  align-content: center;
  margin: auto 0;
  padding: 4px 2px;
}

.pile {
  position: relative;
  width: var(--card-w);
  height: var(--card-h);
  border: none;
  background: none;
  padding: 0;
  cursor: default;
  font-family: inherit;
}

/* buried-card edges give the pile depth */
.pile .under {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  background: #cec9b8;
  box-shadow: 0 1px 2px rgba(0,0,0,0.4);
}

.pile .under.u1 { transform: translate(2px, -3px) rotate(0.8deg); }
.pile .under.u2 { transform: translate(-2px, -6px) rotate(-1deg); background: #bfb9a6; }

.pile .top-card {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  background: linear-gradient(168deg, #faf6ea 0%, var(--bone) 55%, #e7e2d2 100%);
  box-shadow:
    0 1px 0 rgba(255,255,255,0.35) inset,
    0 -2px 4px rgba(110, 105, 80, 0.22) inset,
    0 3px 6px rgba(0, 0, 0, 0.5);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.pile .rank {
  font-size: calc(var(--card-w) * 0.34);
  font-weight: 700;
  line-height: 1;
}

.pile .suit { font-size: calc(var(--card-w) * 0.32); line-height: 1.1; }

.pile.red .top-card { color: var(--red); }

.pile .depth {
  position: absolute;
  top: 5px;
  right: 7px;
  font-size: 0.62rem;
  font-variant-numeric: tabular-nums;
  color: rgba(35, 41, 54, 0.45);
}

.pile.full .top-card {
  filter: saturate(0.45) brightness(0.72);
  box-shadow: 0 0 0 2px rgba(154, 183, 224, 0.3), 0 2px 5px rgba(0,0,0,0.5);
}

.pile .seal {
  position: absolute;
  bottom: 6px;
  left: 50%;
  transform: translateX(-50%) rotate(-6deg);
  font-size: 0.56rem;
  letter-spacing: 0.24em;
  text-transform: uppercase;
  color: var(--moon-hi);
  background: rgba(11, 21, 36, 0.82);
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 2px 6px 1px 8px;
  pointer-events: none;
}

.pile .depth.maxed { color: var(--moon); font-weight: 700; }

.pile.legal { cursor: pointer; }
.pile.legal .top-card {
  box-shadow:
    0 0 0 2px var(--moon),
    0 1px 0 rgba(255,255,255,0.35) inset,
    0 4px 10px rgba(0, 0, 0, 0.55);
  animation: breathe 2.4s ease-in-out infinite;
}

@keyframes breathe {
  50% { box-shadow: 0 0 0 2px var(--moon-hi), 0 0 16px rgba(154, 183, 224, 0.4), 0 4px 10px rgba(0,0,0,0.55); }
}

.pile.legal:active .top-card { transform: scale(0.95); }

.pile .top-card.landed { animation: land 0.26s cubic-bezier(0.2, 1.4, 0.4, 1); }
@keyframes land {
  from { transform: translateY(30px) scale(0.7); opacity: 0.4; }
}

/* ---------- hand (current / next / burn) ---------- */

#hand {
  display: flex;
  align-items: flex-end;
  gap: 14px;
  padding: 14px 4px 6px;
  border-top: 1px solid var(--line);
}

.hand-label {
  display: block;
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.2em;
  color: rgba(242, 239, 230, 0.5);
  margin-bottom: 5px;
  text-align: center;
}

#deck-visual {
  width: calc(var(--card-w) * 0.52);
  height: calc(var(--card-h) * 0.52);
  border-radius: 6px;
  background: repeating-linear-gradient(45deg, var(--card-back) 0 5px, var(--card-back-line) 5px 6px);
  border: 2px solid #1b3252;
  box-shadow: 2px 2px 0 #162943, 4px 4px 0 #101f33, 0 4px 8px rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  align-self: center;
}

#deck-n {
  font-size: 0.78rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: rgba(242, 239, 230, 0.85);
  text-shadow: 0 1px 2px rgba(0,0,0,0.6);
}

#current-wrap { flex: none; }

#current-card {
  width: calc(var(--card-w) * 0.92);
  height: calc(var(--card-h) * 0.92);
  border-radius: var(--radius);
  background: linear-gradient(168deg, #faf6ea 0%, var(--bone) 55%, #e7e2d2 100%);
  box-shadow: 0 5px 14px rgba(0, 0, 0, 0.55), 0 0 0 2px rgba(154,183,224,0.35);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

#current-card.red { color: var(--red); }
#current-card .rank { font-size: calc(var(--card-w) * 0.34); font-weight: 700; line-height: 1; }
#current-card .suit { font-size: calc(var(--card-w) * 0.32); line-height: 1.1; }

#current-card.drawn { animation: draw 0.28s cubic-bezier(0.2, 1.3, 0.4, 1); }
@keyframes draw {
  from { transform: translateX(-30px) rotateY(70deg); opacity: 0; }
}

#current-card.dead {
  box-shadow: 0 5px 14px rgba(0,0,0,0.55), 0 0 0 2px var(--danger);
  animation: deadPulse 1s ease-in-out infinite;
}

@keyframes deadPulse {
  50% { box-shadow: 0 5px 14px rgba(0,0,0,0.55), 0 0 16px rgba(224, 82, 82, 0.5), 0 0 0 2px var(--danger); }
}

#next-wrap { flex: none; opacity: 0.75; }

#next-card {
  width: calc(var(--card-w) * 0.6);
  height: calc(var(--card-h) * 0.6);
  border-radius: 8px;
  background: linear-gradient(168deg, #f4f0e2, #e3ddca);
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.45);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

#next-card.red { color: var(--red); }
#next-card .rank { font-size: calc(var(--card-w) * 0.22); font-weight: 700; line-height: 1; }
#next-card .suit { font-size: calc(var(--card-w) * 0.2); line-height: 1.1; }
#next-card.empty { background: none; box-shadow: 0 0 0 1px var(--line) inset; }

#burn-btn {
  margin-left: auto;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.03);
  color: var(--bone);
  padding: 10px 14px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  transition: transform 0.08s ease, opacity 0.15s ease;
}

#burn-btn:active:not(:disabled) { transform: scale(0.95); }
#burn-btn:disabled { opacity: 0.28; cursor: default; }

.burn-flame { font-size: 1rem; }
.burn-word {
  text-transform: uppercase;
  letter-spacing: 0.18em;
  font-size: 0.68rem;
}
#burn-count { font-size: 0.75rem; font-variant-numeric: tabular-nums; color: var(--moon-hi); }

#burn-btn.urge {
  border-color: var(--danger);
  animation: urgeGlow 1.2s ease-in-out infinite;
}

@keyframes urgeGlow {
  50% { box-shadow: 0 0 14px rgba(224, 82, 82, 0.45); }
}

/* ---------- toast + shake ---------- */

#toast {
  position: absolute;
  left: 0; right: 0;
  bottom: calc(var(--card-h) * 0.92 + 60px);
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  font-family: 'Limelight', cursive;
  font-size: 1.4rem;
  letter-spacing: 0.08em;
  opacity: 0;
}

#toast.show-good { animation: toastIn 1.1s ease; color: var(--win); }
#toast.show-bad { animation: toastIn 1.1s ease; color: var(--danger); }
@keyframes toastIn {
  8% { opacity: 1; transform: scale(1.06); }
  25% { transform: scale(1); }
  70% { opacity: 1; }
  100% { opacity: 0; }
}

#app.shake { animation: shake 0.4s ease; }
@keyframes shake {
  20% { transform: translateX(-7px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(2px); }
}

/* ---------- overlay ---------- */

#overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 10, 18, 0.8);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 20px;
}

#overlay[hidden] { display: none; }

#overlay-panel {
  background: linear-gradient(175deg, #16294a, #0d1a30);
  border: 1px solid var(--line);
  border-radius: 16px;
  max-width: 380px;
  width: 100%;
  max-height: 86dvh;
  overflow-y: auto;
  padding: 22px 22px 18px;
  box-shadow: 0 18px 50px rgba(0,0,0,0.6);
  animation: panelIn 0.28s cubic-bezier(0.2, 1.3, 0.4, 1);
}

@keyframes panelIn {
  from { transform: translateY(26px) scale(0.96); opacity: 0; }
}

#overlay-panel h2 {
  font-family: 'Limelight', cursive;
  font-weight: 400;
  color: var(--moon);
  font-size: 1.5rem;
  margin-bottom: 12px;
  letter-spacing: 0.05em;
}

#overlay-panel h2.won { color: var(--win); }
#overlay-panel h2.lost { color: var(--danger); }

#overlay-panel p, #overlay-panel li {
  font-size: 0.88rem;
  line-height: 1.5;
  color: rgba(242, 239, 230, 0.88);
  margin-bottom: 8px;
}

#overlay-panel ul { list-style: none; margin-bottom: 10px; }
#overlay-panel li { padding-left: 1.1em; position: relative; }
#overlay-panel li::before {
  content: '♦';
  position: absolute;
  left: 0;
  color: var(--moon);
  font-size: 0.75em;
  top: 0.25em;
}

.s-red { color: var(--red); }

#overlay-panel .verdict {
  font-family: 'Limelight', cursive;
  font-size: 2rem;
  text-align: center;
  line-height: 1.15;
  margin: 6px 0 2px;
}

#overlay-panel .verdict.won { color: var(--win); }
#overlay-panel .verdict.lost { color: var(--danger); }

#overlay-panel .subline {
  text-align: center;
  color: rgba(242, 239, 230, 0.7);
  margin-bottom: 6px;
}

#overlay-panel .streak-line {
  text-align: center;
  color: var(--moon-hi);
  font-weight: 600;
  margin: 4px 0;
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 14px;
  margin: 14px 0;
}

.stat-grid div {
  display: flex;
  justify-content: space-between;
  font-size: 0.82rem;
  color: rgba(242, 239, 230, 0.85);
  border-bottom: 1px dotted rgba(154, 183, 224, 0.2);
  padding-bottom: 3px;
}

.stat-grid b { font-variant-numeric: tabular-nums; color: var(--bone); }

.overlay-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 16px;
}

.overlay-actions button {
  border-radius: 10px;
  padding: 12px;
  font-size: 0.95rem;
  cursor: pointer;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.04);
  color: var(--bone);
  font-family: inherit;
  letter-spacing: 0.04em;
}

.overlay-actions button.primary {
  background: linear-gradient(170deg, var(--moon-hi), var(--moon));
  color: #0c1830;
  border: none;
  font-weight: 700;
}

.deal-id {
  text-align: center;
  font-size: 0.68rem;
  color: rgba(242, 239, 230, 0.4);
  margin-top: 12px;
  font-variant-numeric: tabular-nums;
}

@media (min-width: 500px) {
  #app {
    border-left: 1px solid rgba(154, 183, 224, 0.14);
    border-right: 1px solid rgba(154, 183, 224, 0.14);
    box-shadow: 0 0 80px rgba(0,0,0,0.5);
  }
}
