:root {
  --accent: #3ecf8e;
  --accent-dark: #27a86f;
  --noise: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='160'%20height='160'%3E%3Cfilter%20id='n'%3E%3CfeTurbulence%20type='fractalNoise'%20baseFrequency='0.85'%20numOctaves='3'%20stitchTiles='stitch'/%3E%3CfeColorMatrix%20type='saturate'%20values='0'/%3E%3C/filter%3E%3Crect%20width='100%25'%20height='100%25'%20filter='url%28%23n%29'%20opacity='0.05'/%3E%3C/svg%3E");
}

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

html,
body {
  height: 100%;
}

body {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior-y: none;
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: #eaf3ee;
  background-color: #081712;
  background-image:
    var(--noise),
    radial-gradient(circle at 20% -10%, #1f5d45 0%, #123027 45%, #081712 100%);
  background-size: 160px 160px, auto;
  background-repeat: repeat, no-repeat;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  height: calc(100lvh - 100dvh);
  pointer-events: none;
}

body.dragging {
  cursor: grabbing;
}

body.overlay-open {
  background-image: none;
  background-color: #123027;
}

body.menu-open #board {
  visibility: hidden;
}

.statusbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 0;
  background: #15372b;
  pointer-events: none;
  z-index: 60;
}

@media (display-mode: standalone) {
  body {
    padding-top: env(safe-area-inset-top);
  }

  .statusbar {
    height: env(safe-area-inset-top);
  }
}

/* ---------- Top bar ---------- */
.topbar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: clamp(8px, 1.1vw, 10px) var(--gutter, 18px);
  background: #15372b;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  flex: none;
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  cursor: pointer;
  border-radius: 10px;
  transition: opacity 0.15s ease;
}

.brand:hover {
  opacity: 0.72;
}

.brand:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.brand .logo {
  display: block;
  flex: none;
  width: 34px;
  height: 34px;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.45));
}

.brand h1 {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stats {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex: none;
  gap: clamp(6px, 2.2vw, 24px);
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: clamp(30px, 5.5vw, 58px);
}

.stat .label {
  font-size: clamp(7.5px, 1.2vw, 10px);
  text-transform: uppercase;
  letter-spacing: clamp(0.03em, 0.12vw, 0.12em);
  opacity: 0.55;
}

.stat .value {
  font-size: clamp(13px, 1.9vw, 16px);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.actions {
  display: flex;
  flex: none;
  gap: clamp(3px, 1vw, 8px);
}

.actions .btn {
  padding: 8px clamp(2px, 1.2vw, 10px);
}

.actions .btn span {
  display: none;
}

.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.08);
  color: #eaf3ee;
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease, filter 0.15s ease;
  white-space: nowrap;
}

.btn:hover {
  background: rgba(255, 255, 255, 0.16);
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(0);
}

.btn.primary {
  background: linear-gradient(180deg, var(--accent), var(--accent-dark));
  border-color: transparent;
  color: #06281b;
  box-shadow: 0 6px 16px rgba(46, 204, 138, 0.28);
}

.btn.primary:hover {
  filter: brightness(1.06);
}

.btn.big {
  width: 100%;
  padding: 13px 22px;
  font-size: 15px;
  border-radius: 12px;
}

.btn .ico {
  width: 16px;
  height: 16px;
  flex: none;
  display: block;
}

/* ---------- Board ---------- */
#board {
  position: relative;
  flex: 1 1 auto;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior-y: contain;
  scrollbar-width: none;
}

#board::-webkit-scrollbar {
  display: none;
}

#board::after {
  content: "";
  display: block;
  height: var(--scroll-h, 0px);
}

#slots,
#cards {
  position: absolute;
  inset: 0;
}

#slots {
  z-index: 0;
}

#cards {
  z-index: 1;
}

.slot {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cw, 80px);
  height: var(--ch, 114px);
  border-radius: calc(var(--cw, 80px) * 0.09);
  border: 2px dashed rgba(255, 255, 255, 0.16);
  background: rgba(0, 0, 0, 0.12);
}

/* ---------- Cards ---------- */
.topbar,
.slot,
.overlay {
  touch-action: manipulation;
}

.card.down,
.card.lock {
  touch-action: manipulation;
}

#board.zoomed .card {
  touch-action: manipulation;
}

.card {
  position: absolute;
  touch-action: pinch-zoom;
  left: 0;
  top: 0;
  width: var(--cw, 80px);
  height: var(--ch, 114px);
  transform: translate3d(0, 0, 0);
  transition: transform 0.44s cubic-bezier(0.4, 0.2, 0.2, 1);
  will-change: transform;
  perspective: 900px;
}

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.46s cubic-bezier(0.4, 0.2, 0.2, 1);
}

.card.down .card-inner {
  transform: rotateY(180deg);
}

.card.deal {
  transition: transform 0.66s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.card.deal .card-inner {
  transition: transform 0.72s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.card.top .front {
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

.card.top .back {
  box-shadow: inset 0 0 0 var(--card-edge, 2px) rgba(255, 255, 255, 0.85);
}

.card.moving .front {
  box-shadow: 0 20px 34px rgba(0, 0, 0, 0.55), inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

.card.moving .back {
  box-shadow: 0 20px 34px rgba(0, 0, 0, 0.55), inset 0 0 0 var(--card-edge, 2px) rgba(255, 255, 255, 0.85);
}

.face {
  position: absolute;
  inset: 0;
  border-radius: calc(var(--cw, 80px) * 0.09);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  transition: box-shadow 0.22s ease;
}

.front {
  background: linear-gradient(158deg, #ffffff 0%, #eef1f4 100%);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35), inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

.front .rank,
.front .suit {
  position: absolute;
  line-height: 1;
  font-weight: 700;
}

.front .rank {
  font-size: calc(var(--cw, 80px) * 0.23);
}

.front .rank.tl {
  top: 5%;
  left: 6%;
}

.front .rank.br {
  bottom: 5%;
  right: 6%;
}

.front .suit {
  font-size: calc(var(--cw, 80px) * 0.2);
}

.front .suit.tr {
  top: 6%;
  right: 7%;
}

.front .suit.bl {
  bottom: 6%;
  left: 7%;
}

.front .center {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cw, 80px) * 0.42);
  opacity: 0.92;
}


#board.compact {
  --card-edge: 1px;
}

.card.dim .front::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(6, 16, 13, 0.26);
  pointer-events: none;
}

#board.compact .front .rank {
  font-size: calc(var(--cw, 80px) * 0.32);
}

#board.compact .front .rank.tl {
  top: 7%;
  left: 8%;
}

#board.compact .front .suit.tr {
  top: 7%;
  right: 8%;
  font-size: calc(var(--cw, 80px) * 0.32);
}

#board.compact .front .rank.br {
  font-size: calc(var(--cw, 80px) * 0.28);
}

#board.compact .front .suit.bl {
  font-size: calc(var(--cw, 80px) * 0.28);
}

#board.compact .front .center {
  font-size: calc(var(--cw, 80px) * 0.62);
  padding-top: 2%;
}

.red {
  color: #d92b3f;
}

.black {
  color: #1b2230;
}

.back {
  transform: rotateY(180deg);
  background:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.08) 0 6px, transparent 6px 12px),
    linear-gradient(150deg, #2f57a8 0%, #1a2e63 100%);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35), inset 0 0 0 var(--card-edge, 2px) rgba(255, 255, 255, 0.85);
}

.back::after {
  content: "";
  position: absolute;
  inset: 9%;
  border-radius: calc(var(--cw, 80px) * 0.05);
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.18), transparent 62%);
}

.card.lift {
  transition: none !important;
  cursor: grabbing;
}

.card.to-foundation {
  transition: transform 0.62s cubic-bezier(0.45, 0.05, 0.25, 1);
}

.card.lift .face {
  box-shadow: 0 20px 34px rgba(0, 0, 0, 0.55), inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

.card.hint .face {
  box-shadow: 0 0 0 3px #ffd54a, 0 6px 14px rgba(0, 0, 0, 0.45);
}

.card.hint-dest .face {
  animation: pulse 0.7s ease-in-out infinite alternate;
}

@keyframes pulse {
  from {
    box-shadow: 0 0 0 2px rgba(255, 213, 74, 0.5), 0 2px 6px rgba(0, 0, 0, 0.35);
  }
  to {
    box-shadow: 0 0 0 4px rgba(255, 213, 74, 1), 0 2px 6px rgba(0, 0, 0, 0.35);
  }
}

/* ---------- Overlay ---------- */
.overlay {
  position: fixed;
  inset: 0;
  display: none;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: rgba(6, 16, 13, 0.72);
  backdrop-filter: blur(7px);
  -webkit-backdrop-filter: blur(7px);
  z-index: 100;
  padding: 20px;
}

.overlay.show {
  display: flex;
}

.modal {
  width: min(420px, 100%);
  margin: auto;
  background: linear-gradient(180deg, rgba(30, 48, 41, 0.98), rgba(17, 29, 25, 0.99));
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  padding: 28px;
  text-align: center;
  box-shadow: 0 30px 60px rgba(0, 0, 0, 0.55);
  animation: pop 0.28s cubic-bezier(0.22, 0.61, 0.36, 1);
}

@keyframes pop {
  from {
    transform: scale(0.92);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.modal h2 {
  font-size: 22px;
  margin-bottom: 14px;
}

.modal-logo {
  display: block;
  width: 68px;
  height: 68px;
  margin: 0 auto 14px;
  filter: drop-shadow(0 6px 16px rgba(0, 0, 0, 0.45));
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.modal .btn.big + .difficulty {
  margin-top: 18px;
}

.modal-actions .btn {
  flex: 1 1 auto;
  padding: 12px 16px;
  font-size: 14px;
  border-radius: 12px;
  white-space: nowrap;
}

.modal .sub {
  opacity: 0.6;
  font-size: 13px;
  line-height: 1.5;
  margin-bottom: 22px;
}

.modal .about {
  opacity: 0.72;
  font-size: 13.5px;
  line-height: 1.55;
  margin-bottom: 18px;
}

.opt {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 12px;
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.04);
  font-size: 13px;
  line-height: 1.35;
  color: rgba(234, 243, 238, 0.82);
  text-align: left;
  cursor: pointer;
}

.switch {
  position: relative;
  flex: none;
  width: 40px;
  height: 22px;
}

.switch input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.switch .track {
  display: block;
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.16);
  transition: background 0.18s ease;
}

.switch .track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #eaf3ee;
  transition: transform 0.18s ease, background 0.18s ease;
}

.switch input:checked + .track {
  background: var(--accent);
}

.switch input:checked + .track::after {
  transform: translateX(18px);
  background: #0b2b22;
}

.switch input:focus-visible + .track {
  box-shadow: 0 0 0 2px rgba(62, 207, 142, 0.5);
}

.scores {
  margin-bottom: 18px;
  text-align: left;
}

.scores-title {
  font-size: 10.5px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  opacity: 0.55;
  margin-bottom: 8px;
  text-align: center;
}

.score-row {
  display: grid;
  grid-template-columns: 18px 1fr 0.8fr 0.85fr 1.05fr;
  gap: 6px;
  font-size: 12px;
  padding: 3px 0;
  text-align: center;
}

.score-head {
  font-size: 10px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  opacity: 0.45;
}

.score-row.current {
  color: var(--accent);
  font-weight: 700;
}

.difficulty {
  display: grid;
  gap: 10px;
  margin-bottom: 20px;
}

.diff {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.05);
  color: #eaf3ee;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.diff:hover {
  background: rgba(255, 255, 255, 0.1);
}

.diff.selected {
  border-color: var(--accent);
  background: rgba(62, 207, 142, 0.16);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.diff strong {
  font-size: 15px;
}

.diff .suits {
  display: inline-flex;
  gap: 5px;
  font-size: 22px;
  line-height: 1;
}

.diff .suit.black {
  color: #eaf3ee;
}

.diff .suit.red {
  color: #ff6470;
}

.diff > span {
  font-size: 12px;
  opacity: 0.6;
}

.result {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.result > div {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.06);
}

.result span {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  opacity: 0.55;
}

.result strong {
  font-size: 18px;
}

.modal > .btn + .btn {
  margin-top: 10px;
}

.app-version {
  margin-top: 16px;
  font-size: 11px;
  letter-spacing: 0.04em;
  opacity: 0.45;
}

.modal .src {
  display: block;
  margin-top: 10px;
  font-size: 12px;
  color: var(--accent);
  text-decoration: none;
  opacity: 0.85;
}

.modal .src:hover {
  opacity: 1;
  text-decoration: underline;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translate(-50%, 20px);
  background: rgba(20, 30, 26, 0.96);
  border: 1px solid rgba(255, 255, 255, 0.12);
  padding: 12px 18px;
  border-radius: 12px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 200;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}

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

/* ---------- Win-Animation ---------- */
.win-cards {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
}

.win-cards .win-card {
  transition: none;
  will-change: transform, opacity;
}

.overlay .modal {
  position: relative;
  z-index: 1;
}

/* ---------- Responsive ---------- */
@media (max-width: 330px) {
  .stats {
    position: static;
    transform: none;
  }
}

@media (max-height: 520px) {
  .overlay {
    padding: 10px;
  }

  .modal {
    padding: 14px 16px;
    border-radius: 14px;
  }

  .modal-logo {
    width: 38px;
    height: 38px;
    margin-bottom: 8px;
  }

  .modal h2 {
    font-size: 17px;
    margin-bottom: 8px;
  }

  .modal .sub {
    font-size: 11.5px;
    line-height: 1.4;
    margin-bottom: 12px;
  }

  .modal .about {
    font-size: 12px;
    line-height: 1.45;
    margin-bottom: 12px;
  }

  .difficulty {
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    gap: 6px;
    margin-bottom: 12px;
  }

  .diff {
    flex-direction: column;
    gap: 3px;
    padding: 8px 6px;
  }

  .diff .suits {
    font-size: 15px;
    gap: 3px;
  }

  .diff > span {
    font-size: 10px;
  }

  .btn.big {
    padding: 9px 16px;
    font-size: 13.5px;
  }

  .app-version {
    margin-top: 8px;
    font-size: 10px;
  }

  .scores {
    margin-bottom: 10px;
  }

  .scores-title {
    font-size: 9.5px;
    margin-bottom: 4px;
  }

  .score-row {
    font-size: 11px;
    padding: 1px 0;
  }

  .score-head {
    font-size: 9px;
  }

  .scores .score-row:nth-child(n + 6) {
    display: none;
  }

  .result {
    gap: 6px;
    margin-bottom: 12px;
  }

  .result > div {
    padding: 8px;
  }
}

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