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

[hidden] {
  display: none !important;
}

:root {
  --color-red: #e63946;
  --color-blue: #457b9d;
  --color-green: #2a9d8f;
  --color-yellow: #d4a017;
  --bg: #fef6e4;
  --surface: #f0e6d3;
  --text: #1a1a2e;
  --text-muted: #6b6b7b;
  --reveal-bg: #e9f7f5;
  --reveal-code-bg: #fff;
  --radius: 8px;
  --card-width: min(340px, 90vw);
}

[data-theme="dark"] {
  --color-red: #ff5c67;
  --color-blue: #5b9ec9;
  --color-green: #3bbfaf;
  --color-yellow: #f0b429;
  --bg: #1a1a2e;
  --surface: #16213e;
  --text: #f1faee;
  --text-muted: #a8b2c1;
  --reveal-bg: #0d2b26;
  --reveal-code-bg: #0a1f1c;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, sans-serif;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#app {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  width: 100%;
}

header {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  width: 100%;
  padding: 1rem 1.5rem;
  flex-shrink: 0;
}

.header-logo-link {
  position: absolute;
  left: 1.5rem;
  display: flex;
}

.header-logo {
  height: 2.25rem;
  width: 2.25rem;
  object-fit: cover;
  border-radius: 50%;
}

.header-title {
  text-decoration: none;
  color: inherit;
}

header h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-align: center;
  color: var(--text);
}

.header-actions {
  position: absolute;
  right: 1.5rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.icon-btn {
  background: none;
  border: 1.5px solid var(--text);
  border-radius: 50%;
  width: 2rem;
  height: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 1rem;
  color: var(--text);
  opacity: 0.6;
  transition: opacity 0.15s;
  padding: 0;
  line-height: 1;
}

.icon-btn:hover {
  opacity: 1;
}

#theme-toggle {
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  line-height: 1;
  padding: 0.25rem;
}

/* Play area — two columns */
.game-subtitle {
  grid-column: 1 / -1;
  margin: 0;
  text-align: center;
  font-size: 0.9rem;
  opacity: 0.6;
}

#play-area {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: min-content;
  gap: 1.5rem;
  padding: 0.5rem 1.5rem 1rem;
  overflow: hidden;
}

@media (max-width: 600px) {
  #play-area {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
  }
}

/* Stack area */
#stack-area {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  min-width: 0;
}

.stack-wrapper {
  position: relative;
  /* width set dynamically by JS */
  height: 40vh;
  transition: width 0.25s ease;
}

.stack-card {
  position: absolute;
  height: 40vh;
  width: calc(40vh*5/7);
  border-radius: 10px;
  border: 2px solid white;
  cursor: pointer;
  transform: var(--fan-transform, none);
  transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
  transform-origin: bottom center;
}

.stack-card:not(.stacked):hover {
  transform: var(--fan-transform, none) translateY(-18px);
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.35);
  border-color: white;
  z-index: 20 !important;
}

.stack-card.stacked {
  transform: var(--stacked-transform, none);
  transition: none;
  cursor: default;
  pointer-events: none;
}

.stack-card:focus-visible {
  outline: 3px solid var(--text);
  outline-offset: 3px;
  z-index: 21 !important;
}

#stack-empty-msg {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  text-align: center;
  color: var(--text-muted);
}

#restart-deck-btn {
  padding: 0.6rem 1.5rem;
  border: 1px solid var(--text-muted);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  font-size: 0.9rem;
  cursor: pointer;
  transition: opacity 0.1s;
}

#restart-deck-btn:active {
  opacity: 0.7;
}

/* Drawn card area */
#drawn-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  min-width: 0;
}

.card-face {
  width: min(340px, calc(60dvh * 5 / 7));
  aspect-ratio: 5 / 7;
  border-radius: var(--radius);
  padding: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: var(--surface);
  transition: background 0.3s;
  position: relative;
  overflow: hidden;
}

#card-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: inherit;
  border-top: 1px solid;
  border-bottom: 1px solid;
}

.card-face.red {
  border: 4px solid var(--color-red);
}

.card-face.blue {
  border: 4px solid var(--color-blue);
}

.card-face.green {
  border: 4px solid var(--color-green);
}

.card-face.yellow {
  border: 4px solid var(--color-yellow);
}

.card-face.red,
.card-face.blue,
.card-face.green,
.card-face.yellow {
  background: #fef9f0;
  color: #111;
}

.card-face.empty {
  border: 2px dashed var(--text-muted);
  background: transparent;
  color: var(--text-muted);
  font-style: italic;
}

#card-text {
  font-size: 1.15rem;
  line-height: 1.6;
}

/* Footer */
#deck-footer {
  height: 68px;
  background: var(--surface);
  border-top: 1px solid color-mix(in srgb, var(--text) 10%, transparent);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 0 1rem;
  flex-shrink: 0;
}

.tab-btn {
  width: 34px;
  aspect-ratio: 5 / 7;
  border-radius: 4px;
  border: 3px solid transparent;
  cursor: pointer;
  transition: transform 0.1s, border-color 0.2s;
  flex-shrink: 0;
}

.tab-btn[data-deck="me-presento"] {
  background: url("/private/img/cartas/me-presento.png") center/cover no-repeat;
}

.tab-btn[data-deck="me-integro"] {
  background: url("/private/img/cartas/me-integro.png") center/cover no-repeat;
}

.tab-btn[data-deck="me-ordeno"] {
  background: url("/private/img/cartas/me-ordeno.png") center/cover no-repeat;
}

.tab-btn[data-deck="te-veo"] {
  background: url("/private/img/cartas/te-veo.png") center/cover no-repeat;
}


.stack-card.me-presento {
  background: url("/private/img/cartas/me-presento.png") center/cover no-repeat;
}

.stack-card.me-integro {
  background: url("/private/img/cartas/me-integro.png") center/cover no-repeat;
}

.stack-card.me-ordeno {
  background: url("/private/img/cartas/me-ordeno.png") center/cover no-repeat;
}

.stack-card.te-veo {
  background: url("/private/img/cartas/te-veo.png") center/cover no-repeat;
}


.tab-btn.active {
  border-color: var(--text);
  transform: scale(1.15);
}

.tab-btn.exhausted {
  opacity: 0.35;
}

.tab-btn:active {
  transform: scale(0.93);
}

/* Shuffle button */
#shuffle-btn {
  padding: 0.5rem 1.5rem;
  border: 1px solid var(--text-muted);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  flex-shrink: 0;
}

#shuffle-btn:hover {
  background: var(--surface);
}

#shuffle-btn.stopping {
  border-color: var(--text);
  color: var(--text);
  font-weight: 600;
}

/* Mode toggle button */
#mode-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--text-muted);
  background: transparent;
  color: var(--text-muted);
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.1s, background 0.2s, color 0.2s, border-color 0.2s;
}

#mode-btn.active {
  border-color: var(--text);
  color: var(--text);
}

#mode-btn:active {
  opacity: 0.8;
}

#restart-game-btn {
  margin-left: auto;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--text-muted);
  background: transparent;
  color: var(--text-muted);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.1s, background 0.2s, color 0.2s, border-color 0.2s;
}

#restart-game-btn.accented {
  border-color: var(--text);
  background: var(--text);
  color: var(--bg);
}

#restart-game-btn:active {
  opacity: 0.8;
}

/* Help button */
#help-btn {
  margin-right: auto;
  border: 1px solid var(--text-muted);
  padding: 8px;
  border-radius: 10px;
  background: transparent;
  color: var(--text-muted);
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.1s, background 0.2s, color 0.2s, border-color 0.2s;
}

#help-btn:active {
  opacity: 0.8;
}

/* Help modal overlay */
#help-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
}

#help-overlay[hidden] {
  display: none !important;
}

#help-modal {
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius);
  width: min(480px, 100%);
  max-height: 85dvh;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

#help-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  background: none;
  border: none;
  font-size: 1rem;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1;
  padding: 0.25rem;
  z-index: 1;
}

#help-pages {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.help-page {
  display: none;
  flex-direction: column;
  gap: 0.85rem;
  padding: 2rem 2rem 1rem;
  overflow-y: auto;
  max-height: calc(85dvh - 64px);
}

.help-page.active {
  display: flex;
}

.help-page h2 {
  font-size: 1.15rem;
  font-weight: 700;
  padding-right: 1.5rem;
}

.help-page h3 {
  font-size: 0.95rem;
  font-weight: 700;
  margin-top: 0.5rem;
}

.help-page p {
  font-size: 0.9rem;
  line-height: 1.6;
  color: var(--text);
}

.help-categories {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.help-categories li {
  font-size: 0.9rem;
  line-height: 1.6;
  padding-left: 1.25rem;
  position: relative;
}

.cat-dot {
  position: absolute;
  left: 0;
  top: 0.45em;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}

.help-note {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.65rem 0.85rem;
  font-size: 0.85rem !important;
}

/* Help nav */
#help-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
  padding: 0.85rem 1rem;
  border-top: 1px solid color-mix(in srgb, var(--text) 10%, transparent);
  flex-shrink: 0;
}

#help-nav button {
  background: none;
  border: 1px solid var(--text-muted);
  border-radius: 50%;
  width: 32px;
  height: 32px;
  font-size: 1rem;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.15s, border-color 0.15s, color 0.15s;
}

#help-nav button:disabled {
  opacity: 0.3;
  cursor: default;
}

#help-nav button:not(:disabled):hover {
  border-color: var(--text);
  color: var(--text);
}

#help-page-indicator {
  font-size: 0.85rem;
  color: var(--text-muted);
  min-width: 3rem;
  text-align: center;
}

/* Animations */
@keyframes fly-out {
  to {
    opacity: 0;
    transform: var(--fan-transform, none) translateY(-80px) scale(0.85);
  }
}

.stack-card.fly-out {
  animation: fly-out 0.2s ease-in forwards;
  pointer-events: none;
}

@keyframes deal-in {
  from {
    opacity: 0;
    transform: translateX(-30px) scale(0.95);
  }

  to {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

.card-face.dealing {
  animation: deal-in 0.25s ease-out forwards;
}

@keyframes shuffle-card {
  0% {
    transform: var(--stacked-transform, none);
  }

  15% {
    transform: translateX(-22px) translateY(-6px) rotate(-5deg);
  }

  30% {
    transform: translateX(26px) translateY(4px) rotate(4deg);
  }

  45% {
    transform: translateX(-16px) translateY(-4px) rotate(-3deg);
  }

  60% {
    transform: translateX(20px) translateY(6px) rotate(3deg);
  }

  80% {
    transform: translateX(-8px) translateY(-2px) rotate(-1deg);
  }

  100% {
    transform: var(--stacked-transform, none);
  }
}

.stack-wrapper.shuffling .stack-card {
  animation: shuffle-card 0.7s ease-in-out infinite;
}

.stack-wrapper.shuffling .stack-card:nth-child(3n+2) {
  animation-delay: -0.23s;
}

.stack-wrapper.shuffling .stack-card:nth-child(3n+3) {
  animation-delay: -0.47s;
}

/* Auth / Landing pages */
.page-centered {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.auth-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 2rem;
  width: min(380px, 100%);
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.auth-title {
  font-size: 1.15rem;
  font-weight: 700;
}

.auth-error {
  color: var(--color-red);
  font-size: 0.9rem;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.auth-label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.auth-input {
  background: var(--bg);
  border: 1px solid color-mix(in srgb, var(--text) 20%, transparent);
  border-radius: var(--radius);
  padding: 0.6rem 0.75rem;
  font-size: 1rem;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}

.auth-input:focus {
  border-color: var(--text);
}

.auth-input-wrap {
  position: relative;
  display: flex;
}

.auth-input-wrap .auth-input {
  flex: 1;
  padding-right: 2.75rem;
}

.auth-input-reveal {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 2.5rem;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.6;
  transition: opacity 0.15s;
}

.auth-input-reveal:hover {
  opacity: 1;
}

.auth-btn {
  display: block;
  text-align: center;
  padding: 0.65rem 1.5rem;
  border: 1px solid var(--text-muted);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s;
}

.auth-btn:hover {
  background: color-mix(in srgb, var(--text) 8%, transparent);
  border-color: var(--text);
}

.landing-tagline {
  font-size: 0.95rem;
  line-height: 1.6;
  color: var(--text-muted);
  text-align: center;
}