/* ============================================================================
   Mahjong Zen - adaptacion Readingg (portrait)
   ----------------------------------------------------------------------------
   La geometria del tablero la decide js/main.js: cada ficha mide 52 x 70 px
   (TW / TH), las capas se desplazan 8 px a la derecha (DX) y 10 px arriba (DY).
   Por eso --tile-w y --tile-h NO se tocan: el dibujo de la ficha debe coincidir
   con la posicion que calcula el motor. Todo lo que cambia aqui es cromo, color
   y escala, nunca la logica del juego.
   ========================================================================== */

:root {
  /* Ficha: marfil en cualquier tema, con tintas oscuras para que el dibujo se
     lea igual sobre la mesa clara y sobre la oscura (contraste AA en los indices). */
  --face-bg: #f8f3e6;
  --face-edge: #e7dcc2;
  --face-edge-dark: #b3a17c;
  --face-ink: #2b3033;
  --face-red: #b32d26;
  --face-green: #256b45;
  --face-blue: #24527f;
  --face-gold: #8f5f0a;
  --face-font: var(--rg-font);

  --tile-w: 52px;
  --tile-h: 70px;
  --tile-edge: 6px;
  --tile-radius: 10px;

  /* Mesa: azul grafito derivado de los tokens, nunca verde. */
  --board-top: color-mix(in srgb, var(--rg-primary) 22%, var(--rg-bg));
  --board-bottom: color-mix(in srgb, var(--rg-primary) 4%, var(--rg-bg));
  --board-shade: color-mix(in srgb, var(--rg-text) 6%, transparent);
  /* Dorado legible: el token puro solo funciona sobre fondo oscuro. */
  --gold-ink: var(--rg-gold);
}

[data-theme='light'] {
  --face-bg: #fffbf0;
  --face-edge: #ecdfc4;
  --face-edge-dark: #b9a67f;
  --gold-ink: color-mix(in srgb, var(--rg-gold) 34%, var(--rg-text));
}

/* ============================================================== app frame */

html,
body {
  height: 100%;
}

body.mj {
  overflow: hidden;
  background:
    radial-gradient(120% 80% at 50% 0%, var(--rg-bg-2), var(--rg-bg));
}

.rg-game {
  height: 100dvh;
  gap: 8px;
  padding: calc(var(--rg-safe-top) + 8px) calc(var(--rg-safe-right) + 8px)
    calc(var(--rg-safe-bottom) + 8px) calc(var(--rg-safe-left) + 8px);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ================================================================== topbar */

.topbar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  padding-bottom: 8px;
}

.topbar::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 2px;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--rg-primary), var(--rg-accent));
  opacity: 0.45;
}

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

.brand-mark {
  width: 22px;
  height: 28px;
  flex: none;
  color: var(--gold-ink);
}

.brand-text {
  min-width: 0;
}

.brand .rg-title {
  font-size: 17px;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand .rg-title span {
  color: var(--rg-muted);
  font-weight: 600;
}

.brand-sub {
  margin: 2px 0 0;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--rg-muted);
  white-space: nowrap;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: none;
}

.icon-btn {
  width: 48px;
  height: 48px;
  padding: 0;
  border: 1px solid var(--rg-border);
  border-radius: 12px;
  background: var(--rg-surface);
  color: var(--rg-text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.08s;
}

.icon-btn:active {
  transform: scale(0.96);
}

.icon-btn:focus-visible {
  outline: 2px solid var(--rg-primary);
  outline-offset: 2px;
}

.icon-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  display: block;
}

.icon-btn .icon-off {
  display: none;
}

.icon-btn[aria-pressed='false'] {
  color: var(--rg-muted);
}

.icon-btn[aria-pressed='false'] .icon-on {
  display: none;
}

.icon-btn[aria-pressed='false'] .icon-off {
  display: block;
}

[data-theme='light'] #btn-theme .icon-on {
  display: none;
}

[data-theme='light'] #btn-theme .icon-off {
  display: block;
}

/* ================================================================ controls */

.controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

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

.field {
  flex: 1 1 auto;
  min-width: 0;
  height: 48px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px 0 12px;
  border: 1px solid var(--rg-border);
  border-radius: 13px;
  background: var(--rg-surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.field:focus-within {
  border-color: color-mix(in srgb, var(--rg-primary) 60%, var(--rg-border));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--rg-primary) 22%, transparent);
}

.field-label {
  flex: none;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--rg-muted);
}

.select-wrap {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
}

#layout-select {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  min-width: 0;
  padding: 0 20px 0 0;
  border: 0;
  background: transparent;
  color: var(--rg-text);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  text-overflow: ellipsis;
  cursor: pointer;
}

#layout-select:focus-visible {
  outline: none;
}

#layout-select option {
  background: var(--rg-surface);
  color: var(--rg-text);
}

.select-arrow {
  position: absolute;
  right: 0;
  width: 14px;
  height: 14px;
  fill: none;
  stroke: var(--rg-muted);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

.btn-new {
  flex: none;
  gap: 6px;
  padding: 0 14px;
  font-size: 13px;
}

.btn-new svg {
  width: 15px;
  height: 15px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
}

.controls .rg-btn {
  min-height: 48px;
  border-radius: 13px;
}

.btn-row {
  display: flex;
  gap: 8px;
}

.btn-row .rg-btn {
  flex: 1 1 0;
  min-width: 0;
  padding: 0 8px;
  font-size: 13px;
}

.rg-btn:focus-visible {
  outline: 2px solid var(--rg-primary);
  outline-offset: 2px;
}

/* AA: #4c6df0 con texto blanco da 4.42:1; el tono fuerte sube a 5.4:1. */
.rg-btn--primary {
  background: var(--rg-primary-strong);
  border-color: var(--rg-primary-strong);
  box-shadow: 0 8px 20px color-mix(in srgb, var(--rg-primary) 32%, transparent);
}

/* ===================================================================== hud */

.hud {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.hud-main {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.hud-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  padding: 5px 4px 6px;
  border: 1px solid var(--rg-border);
  border-radius: 12px;
  background: var(--rg-surface);
  font-variant-numeric: tabular-nums;
}

.hud-value {
  font-size: 16px;
  font-weight: 800;
  line-height: 1.1;
  letter-spacing: -0.01em;
  color: var(--rg-text);
}

.hud-label {
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--rg-muted);
}

.hud-meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 2px 10px;
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  color: var(--rg-muted);
  font-variant-numeric: tabular-nums;
}

.hud-meta b {
  color: var(--rg-text);
  font-weight: 800;
}

.hud-meta b:last-child {
  color: var(--gold-ink);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.04em;
}

/* =================================================================== board */

.board {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  border: 1px solid var(--rg-border);
  border-radius: 18px;
  background:
    radial-gradient(125% 90% at 50% 0%, var(--board-top) 0%, var(--board-bottom) 72%),
    var(--rg-bg);
  box-shadow:
    inset 0 0 0 1px var(--board-shade),
    inset 0 -34px 70px rgba(0, 0, 0, 0.22);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.board:focus {
  outline: none;
}

[data-theme='light'] .board {
  box-shadow:
    inset 0 0 0 1px var(--board-shade),
    inset 0 -22px 44px rgba(28, 38, 66, 0.1);
}

.board-inner {
  position: absolute;
  left: 50%;
  top: 50%;
  transform-origin: center center;
  will-change: transform;
}

.board-note {
  position: absolute;
  left: 50%;
  bottom: 8px;
  z-index: 5;
  width: max-content;
  max-width: calc(100% - 20px);
  margin: 0;
  padding: 6px 13px;
  transform: translateX(-50%);
  border: 1px solid var(--rg-border);
  border-radius: 999px;
  background: var(--rg-surface);
  box-shadow: var(--rg-shadow);
  color: var(--rg-text);
  font-size: 11.5px;
  font-weight: 600;
  line-height: 1.35;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.board-note.show {
  opacity: 1;
}

/* ==================================================================== tile */

.tile {
  position: absolute;
  width: var(--tile-w);
  height: var(--tile-h);
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: transform 0.12s ease, opacity 0.2s ease;
}

.tile__side {
  position: absolute;
  inset: 0;
  border-radius: var(--tile-radius);
  background: linear-gradient(150deg, var(--face-edge) 0%, var(--face-edge) 42%, var(--face-edge-dark) 100%);
  box-shadow: -3px 5px 9px rgba(0, 0, 0, 0.42), inset -1px -1px 0 rgba(0, 0, 0, 0.16);
}

.tile__face {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  width: calc(100% - var(--tile-edge));
  height: calc(100% - var(--tile-edge));
  border-radius: calc(var(--tile-radius) - 2px);
  background: linear-gradient(160deg, #fffdf7 0%, var(--face-bg) 58%, #ebe3cf 100%);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.14), inset 1px 1px 0 rgba(255, 255, 255, 0.9);
  overflow: hidden;
}

.tile-face {
  display: block;
  width: 100%;
  height: 100%;
}

[data-theme='light'] .tile__side {
  box-shadow: -2px 4px 8px rgba(28, 38, 66, 0.3), inset -1px -1px 0 rgba(0, 0, 0, 0.18);
}

[data-theme='light'] .tile__face {
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.16), inset 1px 1px 0 rgba(255, 255, 255, 0.95);
}

/* Solo los dispositivos con puntero real levantan la ficha: en tactil el
   :hover se queda pegado tras el toque. */
@media (hover: hover) and (pointer: fine) {
  .tile.free:hover .tile__face,
  .tile.free:focus-visible .tile__face {
    filter: brightness(1.04);
  }

  .tile.free:hover {
    transform: translate(1px, -2px);
  }
}

.tile:focus-visible {
  outline: none;
}

.tile:focus-visible .tile__side {
  box-shadow: 0 0 0 3px var(--rg-primary), -3px 5px 9px rgba(0, 0, 0, 0.42);
}

.tile.blocked {
  cursor: default;
}

.board.dim-blocked .tile.blocked .tile__face {
  filter: brightness(0.68) saturate(0.5);
}

.board.dim-blocked .tile.blocked .tile__side {
  filter: brightness(0.74) saturate(0.6);
}

.tile.selected .tile__side {
  background: linear-gradient(145deg, var(--rg-gold) 0%, color-mix(in srgb, var(--rg-gold) 58%, #5c3d05) 100%);
}

.tile.selected .tile__face {
  box-shadow:
    inset 0 0 0 2px var(--rg-gold),
    inset 0 0 18px color-mix(in srgb, var(--rg-gold) 40%, transparent);
}

.tile.selected {
  transform: translate(1px, -3px);
}

.tile.hinted .tile__face {
  animation: hint-pulse 1s ease-in-out 3;
}

@keyframes hint-pulse {
  0%,
  100% {
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.09);
  }

  50% {
    box-shadow:
      inset 0 0 0 3px var(--rg-success),
      inset 0 0 20px color-mix(in srgb, var(--rg-success) 45%, transparent);
  }
}

.tile.nope {
  animation: nope 0.32s ease;
}

@keyframes nope {
  0%,
  100% {
    transform: translateX(0);
  }

  20% {
    transform: translateX(-4px);
  }

  45% {
    transform: translateX(4px);
  }

  70% {
    transform: translateX(-2px);
  }
}

.tile.gone {
  opacity: 0;
  transform: scale(0.75) translateY(-10px);
  pointer-events: none;
}

.tile.matched .tile__face {
  box-shadow: inset 0 0 0 3px var(--rg-success);
}

@media (prefers-reduced-motion: reduce) {
  .tile,
  .tile__face {
    transition: none;
  }

  .tile.hinted .tile__face {
    animation: none;
    box-shadow: inset 0 0 0 3px var(--rg-success);
  }

  .tile.nope {
    animation: none;
  }
}

/* ================================================================= dialogs */

.dlg {
  display: flex;
  flex-direction: column;
  width: min(100% - 28px, 420px);
  max-height: min(86dvh, 640px);
  margin: auto;
  padding: 20px;
  border: 1px solid var(--rg-border);
  border-radius: 20px;
  background: var(--rg-surface);
  box-shadow: var(--rg-shadow);
  color: var(--rg-text);
  overflow: hidden;
}

/* El `display:flex` de arriba pisaria el `display:none` que el navegador aplica
   a un <dialog> cerrado, dejandolo ocupar alto en la pagina. */
.dlg:not([open]) {
  display: none;
}

/* El dialogo de fin de partida puede llevar cuatro acciones: si no cabe,
   se desplaza entero en vez de recortar botones. */
#dlg-end {
  overflow-y: auto;
}

.dlg--wide {
  width: min(100% - 28px, 480px);
  /* Hoja alta: el cuerpo scrollea y el boton queda fijo abajo. */
  height: min(90dvh, 680px);
  max-height: min(90dvh, 680px);
}

.dlg::backdrop {
  background: rgba(4, 6, 12, 0.66);
  backdrop-filter: blur(6px);
}

.dlg h2 {
  margin: 0 0 8px;
  font-size: 18px;
  letter-spacing: -0.01em;
}

.dlg h3 {
  margin: 16px 0 6px;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--gold-ink);
}

.dlg p {
  margin: 0 0 10px;
  color: var(--rg-muted);
  font-size: 13px;
  line-height: 1.55;
}

.dlg strong {
  color: var(--rg-text);
}

.dlg-actions {
  display: flex;
  flex: none;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 16px;
}

.dlg-actions .rg-btn {
  min-height: 48px;
  padding: 0 20px;
  border-radius: 13px;
  font-size: 13px;
}

.dlg-actions .rg-btn--primary {
  min-width: 150px;
}

.end-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin: 14px 0 0;
}

.end-stats > div {
  padding: 9px 11px;
  border: 1px solid var(--rg-border);
  border-radius: 12px;
  background: var(--rg-surface-2);
}

.end-stats dt {
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--rg-muted);
}

.end-stats dd {
  margin: 2px 0 0;
  font-size: 16px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.help-body {
  flex: 1 1 auto;
  min-height: 0;
  padding-right: 4px;
  overflow-y: auto;
}

.help-body::-webkit-scrollbar {
  width: 6px;
}

.help-body::-webkit-scrollbar-thumb {
  background: var(--rg-border);
  border-radius: 999px;
}

.tile-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(38px, 1fr));
  gap: 6px;
  margin-top: 6px;
}

.gallery-tile {
  position: relative;
  aspect-ratio: 3 / 4;
  border-radius: 7px;
  background: linear-gradient(160deg, #fffdf6, var(--face-bg));
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3), inset 0 0 0 1px rgba(0, 0, 0, 0.12);
  overflow: hidden;
}

/* ============================================================ small screens */

@media (max-width: 370px) {
  .rg-game {
    gap: 6px;
  }

  .brand .rg-title {
    font-size: 15.5px;
  }

  .brand-sub {
    font-size: 9px;
  }

  .hud-value {
    font-size: 15px;
  }

  .hud-meta {
    font-size: 10.5px;
    gap: 2px 8px;
  }

  .btn-row .rg-btn {
    font-size: 12.5px;
  }
}

@media (max-height: 640px) {
  .brand-sub {
    display: none;
  }

  .rg-game {
    gap: 6px;
  }

  .hud-meta {
    display: none;
  }
}
