:root {
  --felt: #0f4f3c;
  --felt-dark: #0b342b;
  --felt-light: #1a6a50;
  --ink: #f9fff9;
  --muted: #b9d7ca;
  --accent: #ffd56b;
  --danger: #e05858;
  --card-w: clamp(34px, 9.1vw, 76px);
  --card-h: calc(var(--card-w) * 1.38);
  --gap: clamp(3px, 0.8vw, 8px);
  --stack-step: clamp(17px, 4.7vw, 32px);
  --closed-step: clamp(8px, 2.2vw, 15px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #09231e;
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
  touch-action: none;
  user-select: none;
}

button {
  color: inherit;
  font: inherit;
}

.game-shell {
  min-height: 100%;
  padding: calc(10px + env(safe-area-inset-top)) clamp(8px, 2vw, 16px) calc(12px + env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 8px;
  background:
    radial-gradient(circle at 28% 18%, rgba(255, 255, 255, 0.08), transparent 32%),
    linear-gradient(155deg, #165c46 0%, #0f4436 44%, #092922 100%);
}

.topbar,
.controls,
.foundation-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar {
  justify-content: space-between;
}

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

.mark {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 7px;
  background: #f8f2dd;
  color: #0e3d32;
  font-size: 22px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.28);
}

h1,
p {
  margin: 0;
}

h1 {
  font-size: clamp(18px, 5vw, 26px);
  line-height: 1;
}

.brand p {
  margin-top: 3px;
  max-width: 44vw;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hud {
  display: flex;
  gap: 6px;
}

.hud-item {
  min-width: 48px;
  padding: 5px 7px;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 7px;
  background: rgba(0, 0, 0, 0.16);
  text-align: center;
}

.hud-item span {
  display: block;
  color: var(--muted);
  font-size: 10px;
}

.hud-item strong {
  display: block;
  margin-top: 1px;
  font-size: 13px;
}

.controls {
  min-height: 42px;
  justify-content: space-between;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  min-width: min(58vw, 300px);
  padding: 3px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.24);
}

.seg,
.icon-btn,
.primary-btn,
.stock {
  min-height: 40px;
  border: 0;
  border-radius: 7px;
  cursor: pointer;
  transition: transform 0.14s ease, filter 0.14s ease, background 0.14s ease;
}

.seg:active,
.icon-btn:active,
.primary-btn:active,
.stock:active {
  transform: translateY(1px) scale(0.98);
}

.seg {
  padding: 0 7px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
}

.seg.active {
  background: #f8f2dd;
  color: #12352c;
  font-weight: 800;
}

.icon-btn,
.primary-btn,
.stock {
  background: rgba(255, 255, 255, 0.13);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.12);
}

.icon-btn {
  width: 42px;
  font-size: 21px;
  font-weight: 800;
}

.skill-btn {
  background: rgba(255, 213, 107, 0.24);
  color: #ffe7a0;
  box-shadow: inset 0 0 0 1px rgba(255, 213, 107, 0.42), 0 0 14px rgba(255, 213, 107, 0.12);
}

.primary-btn {
  padding: 0 14px;
  background: var(--accent);
  color: #1d271c;
  font-weight: 900;
}

.table-zone {
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
  scrollbar-width: thin;
  touch-action: pan-x;
  -webkit-overflow-scrolling: touch;
}

.foundation-row {
  justify-content: space-between;
  min-width: max(100%, calc(var(--card-w) * 10 + var(--gap) * 9));
}

.stock {
  width: calc(var(--card-w) * 3.2);
  min-width: 132px;
  height: var(--card-h);
  padding: 4px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 5px;
}

.stock:disabled {
  opacity: 0.45;
}

.stock-card,
.foundation,
.card {
  border-radius: 7px;
}

.stock-card {
  width: var(--card-w);
  height: var(--card-h);
  display: block;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.22) 0 20%, transparent 20% 40%, rgba(255, 255, 255, 0.16) 40% 60%, transparent 60% 80%, rgba(255, 255, 255, 0.12) 80%),
    #173c66;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.45);
}

#stockText {
  min-width: 62px;
  font-size: 13px;
  font-weight: 900;
  line-height: 1.15;
  text-align: left;
}

.foundations {
  display: flex;
  justify-content: flex-end;
  gap: 4px;
}

.foundation {
  width: var(--card-w);
  height: var(--card-h);
  display: grid;
  place-items: center;
  border: 1px dashed rgba(255, 255, 255, 0.28);
  background: rgba(0, 0, 0, 0.13);
  color: rgba(255, 255, 255, 0.42);
  font-size: calc(var(--card-w) * 0.34);
}

.foundation.filled {
  border-style: solid;
  background: #f8f2dd;
  color: #12352c;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.foundation.completed-pop {
  animation: completedPop 0.62s ease both;
}

.board {
  position: relative;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: var(--gap);
  padding-bottom: 8px;
  min-width: max(100%, calc(var(--card-w) * 10 + var(--gap) * 9));
}

.column {
  position: relative;
  min-width: 0;
  height: 100%;
  border: 1px solid rgba(255, 255, 255, 0.10);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.08);
}

.column.drop-ok {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  background: rgba(255, 213, 107, 0.12);
}

.card {
  position: absolute;
  left: 50%;
  width: var(--card-w);
  height: var(--card-h);
  padding: 4px;
  transform: translateX(-50%);
  background: #fbf7e7;
  color: #111a18;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.36), inset 0 0 0 1px rgba(0, 0, 0, 0.1);
  cursor: grab;
  touch-action: none;
  will-change: transform, opacity;
}

.card.face-down {
  background:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.16) 0 5px, transparent 5px 10px),
    #204a7a;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.36), inset 0 0 0 2px rgba(255, 255, 255, 0.25);
  cursor: default;
}

.card.selected {
  outline: 3px solid var(--accent);
  outline-offset: -1px;
  filter: brightness(1.05);
}

.card.hint {
  animation: pulse 0.95s ease-in-out 2;
}

.card.hint-source {
  filter: brightness(1.2);
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.column.hint-target {
  animation: targetGlow 1.1s ease-in-out 2;
}

.card.drag-source {
  opacity: 0.36;
  filter: saturate(0.7);
}

.drag-layer {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 90;
  pointer-events: none;
  transform: translate3d(-999px, -999px, 0);
  will-change: transform;
}

.drag-layer .drag-card {
  position: absolute;
  transform: none;
  cursor: grabbing;
  animation: none;
  box-shadow: 0 7px 18px rgba(0, 0, 0, 0.34), inset 0 0 0 1px rgba(0, 0, 0, 0.1);
}

.card.completing-source {
  opacity: 0.18;
}

.complete-layer {
  position: fixed;
  inset: 0;
  z-index: 120;
  pointer-events: none;
}

.complete-layer .complete-card {
  position: fixed;
  transform: translate3d(0, 0, 0);
  animation: collectRun 1s cubic-bezier(0.16, 0.9, 0.25, 1) forwards;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.38), inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

.hint-layer {
  position: fixed;
  inset: 0;
  z-index: 115;
  pointer-events: none;
}

.hint-layer .hint-card {
  position: fixed;
  transform: translate3d(0, 0, 0);
  animation: hintMove 1.05s ease-in-out 2;
  box-shadow: 0 8px 22px rgba(255, 213, 107, 0.28), 0 6px 16px rgba(0, 0, 0, 0.34);
}

.win-layer {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: grid;
  place-items: center;
  overflow: hidden;
  background: radial-gradient(circle at center, rgba(255, 213, 107, 0.22), rgba(5, 20, 17, 0.72));
  pointer-events: none;
  animation: winLayerIn 0.24s ease both;
}

.win-layer.leaving {
  animation: winLayerOut 0.42s ease both;
}

.win-burst {
  position: absolute;
  width: min(92vw, 520px);
  aspect-ratio: 1;
  border-radius: 50%;
  background:
    repeating-conic-gradient(from 0deg, rgba(255, 213, 107, 0.9) 0deg 7deg, transparent 7deg 18deg),
    radial-gradient(circle, transparent 0 44%, rgba(255, 255, 255, 0.28) 45% 47%, transparent 48%);
  opacity: 0.65;
  animation: winBurst 1.55s ease-out both;
}

.win-title {
  position: relative;
  z-index: 2;
  margin-top: -160px;
  color: #fff8d7;
  font-size: clamp(46px, 16vw, 92px);
  font-weight: 1000;
  text-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  animation: winTitle 1.35s cubic-bezier(0.16, 1.2, 0.24, 1) both;
}

.win-foundation-fan {
  position: absolute;
  z-index: 3;
  left: 50%;
  top: 52%;
  width: 1px;
  height: 1px;
}

.win-mini-card {
  position: absolute;
  left: -24px;
  top: -34px;
  width: 48px;
  height: 66px;
  display: grid;
  place-items: center;
  border-radius: 7px;
  background: #fbf7e7;
  color: #12352c;
  font-size: 28px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.34);
  animation: winFan 1.35s cubic-bezier(0.2, 1, 0.22, 1) both;
}

.card.deal-in {
  animation: dealIn 0.34s cubic-bezier(0.2, 0.9, 0.2, 1) both;
}

.card.landed {
  animation: landCard 0.24s cubic-bezier(0.22, 1.4, 0.34, 1) both;
}

.card.flipped {
  animation: flipCard 0.34s ease both;
}

.rank {
  display: block;
  font-size: calc(var(--card-w) * 0.28);
  font-weight: 900;
  line-height: 1;
}

.suit {
  position: absolute;
  right: 5px;
  bottom: 3px;
  font-size: calc(var(--card-w) * 0.38);
  line-height: 1;
}

.red {
  color: #b22d35;
}

.black {
  color: #151b1b;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 80;
  max-width: min(92vw, 390px);
  padding: 10px 13px;
  border-radius: 8px;
  background: rgba(16, 26, 24, 0.92);
  color: #fff;
  font-weight: 800;
  text-align: center;
  transform: translate(-50%, 18px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.win-dialog {
  width: min(88vw, 330px);
  border: 0;
  border-radius: 8px;
  padding: 22px;
  background: #fbf7e7;
  color: #122a24;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}

.win-dialog::backdrop {
  background: rgba(0, 0, 0, 0.45);
}

.win-dialog h2 {
  margin: 0 0 8px;
}

.win-dialog p {
  margin-bottom: 18px;
  color: #38564d;
}

@keyframes pulse {
  50% {
    filter: brightness(1.22);
    transform: translateX(-50%) translateY(-4px);
  }
}

@keyframes targetGlow {
  50% {
    background: rgba(255, 213, 107, 0.18);
    box-shadow: inset 0 0 0 2px rgba(255, 213, 107, 0.7), 0 0 18px rgba(255, 213, 107, 0.22);
  }
}

@keyframes hintMove {
  0% {
    opacity: 0;
    transform: translate3d(0, 0, 0) scale(0.96);
  }
  12% {
    opacity: 1;
  }
  58% {
    opacity: 1;
    transform: translate3d(var(--hint-x), var(--hint-y), 0) scale(1.03);
  }
  78% {
    opacity: 1;
    transform: translate3d(var(--hint-x), var(--hint-y), 0) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate3d(var(--hint-x), var(--hint-y), 0) scale(0.98);
  }
}

@keyframes dealIn {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(-28px) rotate(-4deg) scale(0.92);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0) rotate(0) scale(1);
  }
}

@keyframes landCard {
  0% {
    transform: translateX(-50%) translateY(-10px) scale(1.04);
  }
  70% {
    transform: translateX(-50%) translateY(2px) scale(0.99);
  }
  100% {
    transform: translateX(-50%) translateY(0) scale(1);
  }
}

@keyframes flipCard {
  0% {
    transform: translateX(-50%) rotateY(86deg);
    filter: brightness(1.2);
  }
  100% {
    transform: translateX(-50%) rotateY(0);
    filter: brightness(1);
  }
}

@keyframes completedPop {
  0% {
    transform: scale(0.76);
    filter: brightness(1.45);
  }
  70% {
    transform: scale(1.08);
  }
  100% {
    transform: scale(1);
    filter: brightness(1);
  }
}

@keyframes collectRun {
  0% {
    opacity: 1;
    transform: translate3d(0, 0, 0) rotate(0) scale(1);
  }
  42% {
    opacity: 1;
    transform: translate3d(0, calc(var(--collapse-y) * 0.12), 0) rotate(0) scale(0.96);
  }
  72% {
    opacity: 1;
    transform: translate3d(calc(var(--collapse-x) * 0.82), calc(var(--collapse-y) * 0.82), 0) rotate(4deg) scale(0.76);
  }
  100% {
    opacity: 0;
    transform: translate3d(var(--collapse-x), var(--collapse-y), 0) rotate(7deg) scale(0.55);
  }
}

@keyframes winLayerIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes winLayerOut {
  to {
    opacity: 0;
    transform: scale(1.04);
  }
}

@keyframes winBurst {
  from {
    transform: scale(0.2) rotate(0);
    opacity: 0;
  }
  38% {
    opacity: 0.75;
  }
  to {
    transform: scale(1.25) rotate(36deg);
    opacity: 0;
  }
}

@keyframes winTitle {
  0% {
    opacity: 0;
    transform: translateY(24px) scale(0.72);
  }
  45% {
    opacity: 1;
    transform: translateY(0) scale(1.08);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes winFan {
  0% {
    opacity: 0;
    transform: translate3d(0, 34px, 0) rotate(0) scale(0.7);
  }
  60% {
    opacity: 1;
  }
  100% {
    opacity: 1;
    transform: translate3d(var(--fan-x), -8px, 0) rotate(var(--spin)) scale(1);
  }
}

@media (max-width: 760px) {
  :root {
    --gap: 3px;
    --mobile-board-w: min(100vw, 390px);
    --card-w: calc((var(--mobile-board-w) - 30px - (var(--gap) * 9)) / 10);
    --stack-step: clamp(23px, 6.1vw, 28px);
    --closed-step: clamp(8px, 2.2vw, 12px);
  }

  .topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: flex-start;
    gap: 5px;
  }

  .game-shell {
    width: var(--mobile-board-w);
    margin-inline: 0 auto;
  }

  .table-zone {
    overflow-x: hidden;
    touch-action: none;
    scrollbar-width: none;
  }

  .table-zone::-webkit-scrollbar {
    display: none;
  }

  .foundation-row,
  .board {
    min-width: 100%;
    max-width: 100%;
  }

  .foundation-row {
    gap: 4px;
  }

  .foundations {
    flex: 1;
    min-width: 0;
    justify-content: flex-end;
  }

  .hud-item {
    min-width: 36px;
    padding-inline: 5px;
  }

  .hud-item span {
    display: none;
  }

  .hud-item strong {
    font-size: 12px;
  }

  .hud {
    gap: 4px;
  }

  .mark {
    width: 30px;
    height: 30px;
    font-size: 19px;
  }

  h1 {
    font-size: 20px;
  }

  .brand p {
    max-width: 46vw;
    font-size: 11px;
  }

  .controls {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 34px 34px 34px 46px;
    gap: 5px;
  }

  .segmented {
    min-width: 0;
    width: 100%;
  }

  .seg {
    padding-inline: 2px;
    font-size: 12px;
  }

  .icon-btn {
    width: 34px;
    min-height: 36px;
    font-size: 17px;
  }

  .primary-btn {
    min-height: 36px;
    padding-inline: 10px;
    font-size: 13px;
  }

  .stock {
    width: calc(var(--card-w) * 2.35);
    min-width: 0;
    grid-template-columns: var(--card-w) minmax(34px, 1fr);
    padding: 3px;
    gap: 3px;
  }

  #stockText {
    min-width: 34px;
    font-size: 10px;
    line-height: 1.05;
    word-break: keep-all;
  }

  .foundation {
    width: clamp(18px, 5vw, 22px);
    height: calc(var(--card-h) * 0.92);
    font-size: 11px;
  }

  .foundations {
    gap: 2px;
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  :root {
    --card-w: clamp(32px, 6.5vw, 56px);
    --stack-step: clamp(20px, 4.4vw, 26px);
    --closed-step: 8px;
  }

  .game-shell {
    gap: 5px;
    padding-top: calc(6px + env(safe-area-inset-top));
  }

  .brand p,
  .hud-item span {
    display: none;
  }

  .hud-item {
    min-width: 46px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
