:root {
  color-scheme: dark;

  /* WahaNest tokens (mirrors /assets/style.css) */
  --bg: #0b0d12;
  --surface: #12151c;
  --surface-2: #171b24;
  --border: #232833;
  --border-strong: #2e3542;
  --text: #eef0f4;
  --text-muted: #9aa3b2;
  --text-faint: #626b7a;
  --accent-green: #2fb673;
  --accent-gold: #d9b46a;
  --radius-lg: 20px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --gutter: clamp(16px, 4vw, 48px);
  --ease: cubic-bezier(0.2, 0.7, 0.3, 1);

  /* Puzzle sizing: phones fit board + tray without much scrolling */
  --tile: clamp(64px, min(26vw, 12.5svh), 118px);
  --tray-tile: calc(var(--tile) * 0.72);
  --gap: clamp(6px, 1.4vw, 10px);
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(ellipse at 50% -10%, rgba(47, 182, 115, 0.18), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: "Tajawal", "Segoe UI", Tahoma, Arial, sans-serif;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

img {
  display: block;
  max-width: 100%;
  height: auto;
}

/* ---------- topbar ---------- */

.topbar {
  border-bottom: 1px solid var(--border);
}

.topbar__inner {
  max-width: 1240px;
  margin: 0 auto;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 var(--gutter);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text);
  transition: color 0.2s var(--ease);
}

.brand:hover,
.brand:focus-visible {
  color: var(--accent-gold);
}

.brand__icon {
  width: 34px;
  height: 34px;
  object-fit: contain;
}

.brand__name {
  font-weight: 800;
  font-size: 17px;
}

.back-link {
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 700;
  font-size: 14px;
  transition: color 0.2s var(--ease);
}

.back-link:hover,
.back-link:focus-visible {
  color: var(--accent-gold);
}

a:focus-visible,
.btn:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 3px;
  border-radius: var(--radius-sm);
}

/* ---------- game ---------- */

.game {
  flex: 1;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: clamp(16px, 3vw, 40px) var(--gutter) clamp(24px, 4vw, 48px);
  text-align: center;
}

.game__head {
  max-width: 680px;
  margin: 0 auto;
}

.eyebrow {
  margin: 0 0 6px;
  font-size: clamp(13px, 1.2vw, 15px);
  font-weight: 700;
  color: var(--accent-gold);
}

.game__title {
  margin: 0 0 8px;
  font-size: clamp(24px, 4vw, 40px);
  font-weight: 800;
  line-height: 1.2;
}

.game__lead {
  margin: 0 0 16px;
  font-size: clamp(14px, 1.4vw, 17px);
  color: var(--text-muted);
}

.controls {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  margin: 0 0 clamp(14px, 2.5vw, 24px);
}

.btn {
  font-family: inherit;
  font-size: 16px;
  font-weight: 700;
  min-height: 44px;
  padding: 8px 20px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  transition: background-color 0.2s var(--ease), border-color 0.2s var(--ease), color 0.2s var(--ease);
}

.btn--primary {
  background: var(--accent-green);
  border-color: var(--accent-green);
  color: #06140d;
}

.btn--primary:hover {
  background: #3cc983;
}

.btn--ghost {
  background: rgba(18, 21, 28, 0.6);
  color: var(--text);
}

.btn--ghost:hover {
  border-color: var(--accent-gold);
  color: var(--accent-gold);
}

body.is-dragging .controls {
  pointer-events: none;
}

.play-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(14px, 2.5vw, 28px);
}

/* Keep columns LTR so indices match visually */
.board {
  direction: ltr;
  display: grid;
  grid-template-columns: repeat(3, var(--tile));
  grid-template-rows: repeat(3, var(--tile));
  gap: var(--gap);
  padding: var(--gap);
  border: 2px solid var(--accent-gold);
  border-radius: var(--radius-lg);
  background: rgba(18, 21, 28, 0.7);
  box-shadow: 0 16px 40px -20px rgba(0, 0, 0, 0.7);
  touch-action: none;
}

.cell {
  width: var(--tile);
  height: var(--tile);
  border: 2px dashed rgba(238, 240, 244, 0.22);
  border-radius: var(--radius-sm);
  overflow: hidden;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.cell.hot {
  border-color: var(--accent-green);
  background: rgba(47, 182, 115, 0.12);
}

.tray {
  direction: ltr;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-content: flex-start;
  gap: var(--gap);
  width: min(100%, calc(5 * var(--tray-tile) + 4 * var(--gap) + 28px));
  min-height: calc(var(--tray-tile) + 28px);
  padding: 12px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  background: rgba(18, 21, 28, 0.5);
  touch-action: none;
}

.piece {
  width: var(--tray-tile);
  height: var(--tray-tile);
  border-radius: var(--radius-sm);
  background-color: #f5efe0;
  background-size: 300% 300%;
  background-repeat: no-repeat;
  border: 2px solid var(--accent-gold);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.35);
  cursor: grab;
  touch-action: none;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
}

.cell .piece {
  width: 100%;
  height: 100%;
  border-radius: 6px;
}

.piece:active {
  cursor: grabbing;
}

.piece.dragging {
  position: fixed;
  z-index: 9999; /* above shield */
  transform: scale(1.06);
  box-shadow: 0 14px 30px rgba(0, 0, 0, 0.55);
}

.piece.correct {
  border-color: var(--accent-green);
  box-shadow: 0 0 0 2px rgba(47, 182, 115, 0.35);
  animation: pop 0.22s ease;
}

.piece.no-image {
  background-image: linear-gradient(135deg, #0d4a36 0%, #d9b46a 100%) !important;
}

@keyframes pop {
  0% { transform: scale(0.96); }
  100% { transform: scale(1); }
}

/* Fullscreen shield: blocks stray taps while dragging on iOS */
.drag-shield {
  position: fixed;
  inset: 0;
  z-index: 9998;
  display: none;
}

body.is-dragging .drag-shield {
  display: block;
}

/* ---------- overlay (greeting card) ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 9997;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(10px, 2.5vw, 24px);
  background: rgba(5, 7, 10, 0.78);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.overlay-card {
  width: min(100%, 560px);
  max-height: 100%;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: clamp(12px, 2.5vw, 20px);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: 0 24px 60px -20px rgba(0, 0, 0, 0.8);
}

.overlay__title {
  margin: 0;
  font-size: clamp(18px, 2.4vw, 24px);
  font-weight: 800;
}

.invite-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
}

.invite {
  width: auto;
  max-width: 100%;
  max-height: calc(100dvh - 280px); /* room for the name field */
  aspect-ratio: 4 / 5;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  object-fit: contain;
}

.invite-error {
  padding: 16px;
  color: var(--text-muted);
}

.controls--overlay {
  margin: 0;
}

.name-field {
  width: min(100%, 420px);
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: start;
}

.name-field label {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-muted);
}

.name-field input {
  width: 100%;
  min-height: 44px;
  padding: 8px 14px;
  font-family: inherit;
  font-size: 16px; /* 16px+ stops iOS zooming in on focus */
  font-weight: 700;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
  transition: border-color 0.2s var(--ease);
}

.name-field input::placeholder {
  color: var(--text-faint);
}

.name-field input:focus {
  outline: none;
  border-color: var(--accent-gold);
}

.btn--whatsapp {
  gap: 8px;
  background: #25d366;
  border-color: #25d366;
  color: #06140d;
}

.btn--whatsapp:hover {
  background: #3ee07a;
}

.btn--whatsapp:disabled {
  opacity: 0.7;
  cursor: progress;
}

.btn__icon {
  flex: none;
}

@media (max-width: 480px) {
  .controls--overlay .btn {
    flex: 1 1 auto;
    padding: 8px 14px;
  }
  .controls--overlay .btn--whatsapp {
    flex-basis: 100%;
  }
}

.footer {
  border-top: 1px solid var(--border);
  padding: 18px var(--gutter);
  text-align: center;
}

.footer__brand {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  color: var(--text-faint);
}

.footer__domain {
  direction: ltr;
  unicode-bidi: isolate;
}

/* ---------- responsive ---------- */

/* Landscape phones: short viewport, put board and tray side by side */
@media (orientation: landscape) and (max-height: 560px) {
  :root {
    --tile: clamp(52px, 22svh, 96px);
    --tray-tile: calc(var(--tile) * 0.8);
  }
  .game__lead { display: none; }
  .play-area {
    flex-direction: row;
    justify-content: center;
    align-items: flex-start;
  }
  .tray {
    width: calc(3 * var(--tray-tile) + 2 * var(--gap) + 28px);
  }
}

/* Tablets / desktop: board beside tray */
@media (min-width: 900px) {
  :root {
    --tile: clamp(96px, min(11vw, 19vh), 150px);
    --tray-tile: calc(var(--tile) * 0.8);
  }
  .play-area {
    flex-direction: row;
    justify-content: center;
    align-items: flex-start;
  }
  .tray {
    width: calc(3 * var(--tray-tile) + 2 * var(--gap) + 28px);
    min-height: calc(3 * var(--tile) + 4 * var(--gap));
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}
