:root {
  --felt-1: #0c6b4e;
  --felt-2: #084b3a;
  --felt-3: #06372d;
  --line: rgba(255, 255, 255, 0.17);
  --soft-line: rgba(255, 255, 255, 0.09);
  --text: #f7fff9;
  --muted: rgba(247, 255, 249, 0.7);
  --card: #fbfaf5;
  --card-edge: rgba(30, 26, 18, 0.18);
  --black-card: #151617;
  --red-card: #c42e36;
  --shadow: 0 10px 28px rgba(0, 0, 0, 0.24);
  --gap: clamp(4px, 1.25vw, 10px);
  --screen-pad-x: max(8px, env(safe-area-inset-left), env(safe-area-inset-right));
  --screen-pad-bottom: max(8px, env(safe-area-inset-bottom));
  --card-w: min(78px, calc((100vw - (2 * var(--screen-pad-x)) - 12px - (6 * var(--gap))) / 7));
  --card-h: calc(var(--card-w) * 1.42);
  --radius: calc(var(--card-w) * 0.12);
  --tableau-down-step: calc(var(--card-w) * 0.18);
  --tableau-up-step: calc(var(--card-w) * 0.35);
  --font: ui-rounded, "SF Pro Rounded", "SF Pro Display", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  min-height: 100%;
  overflow: hidden;
  background: var(--felt-3);
  color: var(--text);
  font-family: var(--font);
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
  background:
    radial-gradient(circle at 10% 0%, rgba(255, 255, 255, 0.08), transparent 28%),
    radial-gradient(circle at 100% 30%, rgba(255, 255, 255, 0.05), transparent 32%),
    linear-gradient(145deg, var(--felt-1), var(--felt-2) 48%, var(--felt-3));
}

button {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.13);
  color: var(--text);
  font: 700 clamp(11px, 2.9vw, 14px)/1 var(--font);
  padding: 9px 11px;
  min-height: 34px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.12);
}

button:active { transform: translateY(1px); }
button:disabled { opacity: 0.42; }

.app {
  height: 100dvh;
  padding: max(8px, env(safe-area-inset-top)) var(--screen-pad-x) var(--screen-pad-bottom);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 48px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.13);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  font-size: 21px;
}

h1 {
  margin: 0;
  font-size: clamp(18px, 5.4vw, 28px);
  line-height: 1;
  letter-spacing: -0.035em;
}

.brand p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: clamp(10px, 2.6vw, 12px);
  white-space: nowrap;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(46px, auto));
  gap: 5px;
  flex: 0 0 auto;
}

.stats div {
  min-width: 48px;
  padding: 5px 7px;
  border: 1px solid var(--soft-line);
  border-radius: 13px;
  background: rgba(0, 0, 0, 0.12);
  text-align: center;
}

.stats span {
  display: block;
  font-size: clamp(12px, 3.2vw, 15px);
  font-weight: 800;
  line-height: 1.05;
}

.stats small {
  color: var(--muted);
  font-size: 9px;
  line-height: 1;
}

.table-wrap {
  min-height: 0;
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.controls {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
}

.controls button {
  padding-left: 7px;
  padding-right: 7px;
}

.board {
  position: relative;
  min-height: 0;
  flex: 1 1 auto;
  border: 1px solid var(--soft-line);
  border-radius: 18px;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.035) 1px, transparent 1px),
    linear-gradient(0deg, rgba(255,255,255,0.025) 1px, transparent 1px),
    rgba(0, 0, 0, 0.08);
  background-size: 28px 28px;
  padding: 7px;
  overflow: hidden;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.08), var(--shadow);
  touch-action: none;
}

.top-row,
.tableau-row {
  display: grid;
  grid-template-columns: repeat(7, var(--card-w));
  gap: var(--gap);
  justify-content: center;
}

.top-row { min-height: var(--card-h); }
.tableau-row { margin-top: 10px; align-items: start; }

.top-spacer { width: var(--card-w); height: var(--card-h); }

.pile {
  position: relative;
  width: var(--card-w);
  height: var(--card-h);
  border-radius: var(--radius);
  outline: 1px dashed rgba(255, 255, 255, 0.22);
  background: rgba(0, 0, 0, 0.11);
}

.tableau-pile {
  min-height: calc(var(--card-h) + 7 * var(--tableau-up-step));
  height: min(calc(100dvh - 205px), calc(var(--card-h) + 13 * var(--tableau-up-step)));
}

.foundation-pile::before,
.stock-pile.empty-stock::before {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: rgba(255, 255, 255, 0.32);
  font-size: calc(var(--card-w) * 0.42);
  font-weight: 900;
}

#foundation-spades::before { content: "♠"; }
#foundation-hearts::before { content: "♥"; color: rgba(255, 210, 214, 0.45); }
#foundation-diamonds::before { content: "♦"; color: rgba(255, 210, 214, 0.45); }
#foundation-clubs::before { content: "♣"; }
.stock-pile.empty-stock::before { content: "↻"; }

.card {
  position: absolute;
  width: var(--card-w);
  height: var(--card-h);
  border-radius: var(--radius);
  background: var(--card);
  color: var(--black-card);
  border: 1px solid var(--card-edge);
  box-shadow: 0 5px 12px rgba(0,0,0,0.22), inset 0 1px 0 rgba(255,255,255,0.85);
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding: calc(var(--card-w) * 0.08);
  overflow: hidden;
  cursor: grab;
  will-change: transform, top, left;
  touch-action: none;
}

.card.dragging-source { opacity: 0.28; }
.card.selected { outline: 3px solid rgba(255, 225, 118, 0.9); outline-offset: -2px; }
.card.hinted { outline: 3px solid rgba(142, 231, 255, 0.95); outline-offset: -2px; }
.pile.hinted { outline: 3px solid rgba(142, 231, 255, 0.95); outline-offset: 2px; }
.pile.drop-ready { outline: 3px solid rgba(173, 255, 191, 0.95); outline-offset: 2px; }
.pile.drop-blocked { outline: 3px solid rgba(255, 128, 128, 0.75); outline-offset: 2px; }

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

.card-back {
  background:
    radial-gradient(circle at 50% 50%, rgba(255,255,255,0.22) 0 12%, transparent 13%),
    repeating-linear-gradient(45deg, rgba(255,255,255,0.14) 0 3px, transparent 3px 8px),
    linear-gradient(145deg, #263e96, #142356 72%);
  color: transparent;
  border-color: rgba(255,255,255,0.22);
}

.card-back::after {
  content: "";
  position: absolute;
  inset: 11%;
  border-radius: calc(var(--radius) * 0.72);
  border: 2px solid rgba(255,255,255,0.38);
}

.card-rank {
  font-size: calc(var(--card-w) * 0.24);
  line-height: 1;
  font-weight: 950;
  letter-spacing: -0.04em;
}

.card-suit-center {
  align-self: center;
  justify-self: center;
  font-size: calc(var(--card-w) * 0.48);
  line-height: 1;
  filter: drop-shadow(0 1px 0 rgba(255,255,255,0.35));
}

.card-bottom {
  justify-self: end;
  align-self: end;
  transform: rotate(180deg);
  font-size: calc(var(--card-w) * 0.22);
  font-weight: 950;
  line-height: 1;
}

.drag-layer {
  position: fixed;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  z-index: 1000;
  pointer-events: none;
}

.drag-layer .card {
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0.96;
  box-shadow: 0 13px 32px rgba(0,0,0,0.36);
}

.status-line {
  min-height: 22px;
  padding: 0 4px;
  color: var(--muted);
  font-size: clamp(11px, 3vw, 13px);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dialog {
  width: min(92vw, 430px);
  border: 1px solid rgba(255,255,255,0.18);
  border-radius: 22px;
  background: #fbfaf5;
  color: #141513;
  box-shadow: 0 28px 72px rgba(0,0,0,0.42);
  padding: 0;
}

.dialog::backdrop { background: rgba(0,0,0,0.42); backdrop-filter: blur(4px); }
.dialog form { padding: 20px; }
.dialog h2 { margin: 0 0 10px; font-size: 24px; letter-spacing: -0.04em; }
.dialog p, .dialog li { font-size: 15px; line-height: 1.35; }
.dialog ul { padding-left: 18px; }
.dialog button { color: #fff; background: #0d5f47; border-color: transparent; min-width: 120px; }

@media (orientation: landscape) and (max-height: 470px) {
  :root {
    --card-w: min(58px, calc((100vw - (2 * var(--screen-pad-x)) - 12px - (6 * var(--gap))) / 7));
    --tableau-up-step: calc(var(--card-w) * 0.28);
    --tableau-down-step: calc(var(--card-w) * 0.14);
  }

  .app { gap: 5px; padding-top: max(5px, env(safe-area-inset-top)); }
  .topbar { min-height: 38px; }
  .brand-mark { width: 30px; height: 30px; border-radius: 10px; font-size: 17px; }
  .brand p { display: none; }
  .controls { position: absolute; right: 12px; top: 7px; z-index: 20; width: min(375px, calc(100vw - 180px)); }
  .table-wrap { position: relative; gap: 5px; }
  .board { padding-top: 48px; }
  .tableau-row { margin-top: 8px; }
  .status-line { display: none; }
}

@media (max-width: 360px) {
  .controls { gap: 4px; }
  button { font-size: 10px; padding: 8px 5px; }
  .stats { grid-template-columns: repeat(3, minmax(41px, auto)); }
  .stats div { min-width: 41px; padding-left: 4px; padding-right: 4px; }
}
