/* ==========================================================================
   Mahjong Game Board, Scalable 3D Tiles & Responsive Viewport
   ========================================================================== */

:root {
  /* Internal Coordinate System (Logical Units) */
  --tile-w: 48px;
  --tile-h: 64px;
  --layer-depth: 4px;
  --board-scale: 1;

  /* Theme: Classic Emerald Felt (Default) */
  --table-bg: #0b3d2c;
  --table-border: #14532d;
  --table-pattern: radial-gradient(#155724 15%, transparent 16%);
  
  /* Tile Aesthetics */
  --tile-face-top: #ffffff;
  --tile-face-bot: #f5eedf;
  --tile-border: #d4c8b2;
  --tile-side-top: #3a7d5b;
  --tile-side-bot: #1b4332;
  --tile-shadow: rgba(0, 0, 0, 0.45);
  
  /* Highlights & Feedback */
  --highlight-selected: #f59e0b;
  --highlight-hint: #38bdf8;
  --blocked-tint: rgba(0, 0, 0, 0.12);
}

/* Theme: Midnight Slate */
[data-theme="slate"] {
  --table-bg: #0f172a;
  --table-border: #1e293b;
  --table-pattern: radial-gradient(#1e293b 15%, transparent 16%);
  --tile-side-top: #475569;
  --tile-side-bot: #1e293b;
}

/* Theme: Imperial Crimson */
[data-theme="crimson"] {
  --table-bg: #5c0d18;
  --table-border: #801020;
  --table-pattern: radial-gradient(#741221 15%, transparent 16%);
  --tile-side-top: #8b1e2e;
  --tile-side-bot: #480911;
}

/* --------------------------------------------------------------------------
   Table & Scalable Viewport Container
   -------------------------------------------------------------------------- */
.game-viewport {
  position: relative;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  border-radius: 16px;
  background-color: var(--table-bg);
  background-image: var(--table-pattern);
  background-size: 24px 24px;
  box-shadow: 
    inset 0 0 80px rgba(0, 0, 0, 0.65),
    0 12px 32px rgba(0, 0, 0, 0.35);
  border: 4px solid var(--table-border);
  padding: 1rem 0.5rem 2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  transition: min-height 0.15s ease-out;
  box-sizing: border-box;
}

/* Floating controls inside viewport: Zoom & Rotate */
.viewport-floating-controls {
  position: absolute;
  top: 10px;
  right: 12px;
  display: flex;
  gap: 0.5rem;
  z-index: 80;
}

.floating-btn {
  background: rgba(17, 26, 46, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #ffffff;
  padding: 0.35rem 0.65rem;
  border-radius: 8px;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  backdrop-filter: blur(6px);
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  transition: all 0.15s ease;
}

.floating-btn:hover {
  background: rgba(30, 45, 80, 0.95);
  border-color: var(--highlight-selected);
  transform: translateY(-1px);
}

.floating-btn.active {
  background: var(--highlight-selected);
  color: #000000;
  border-color: var(--highlight-selected);
}

/* Mobile Rotation Hint Banner */
.rotation-tip {
  display: none;
  background: rgba(245, 158, 11, 0.15);
  border: 1px solid rgba(245, 158, 11, 0.4);
  color: #fef3c7;
  padding: 0.4rem 0.75rem;
  border-radius: 8px;
  font-size: 0.78rem;
  font-weight: 600;
  margin-bottom: 0.6rem;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  width: 100%;
}

.rotation-tip.show {
  display: flex;
}

.rotation-tip-close {
  background: none;
  border: none;
  color: #fef3c7;
  font-size: 1.1rem;
  cursor: pointer;
  line-height: 1;
  padding: 0 0.25rem;
}

/* Scaler Container: maintains 744x600 base aspect ratio */
.board-scaler-wrapper {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow: visible;
  transition: height 0.15s ease-out;
}

.board-scaler-wrapper.is-zoomed {
  overflow-x: auto;
  overflow-y: visible;
  justify-content: flex-start;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 8px;
}

.board-scaler {
  position: relative;
  width: 744px;
  height: 600px;
  flex-shrink: 0;
  transform-origin: top center;
  transition: transform 0.15s cubic-bezier(0.2, 0.8, 0.2, 1);
  margin: 0 auto;
}

.board-scaler-wrapper.is-zoomed .board-scaler {
  transform-origin: top left;
  margin: 0 0.5rem;
}

/* Board Arena: Fits standard 30 grid units horizontally, 17 vertically */
.board-arena {
  position: absolute;
  top: 44px;
  left: 12px;
  width: 720px;
  height: 544px;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

/* --------------------------------------------------------------------------
   3D Isometric Tile
   -------------------------------------------------------------------------- */
.mahjong-tile {
  position: absolute;
  width: var(--tile-w);
  height: var(--tile-h);
  border-radius: 6px;
  cursor: pointer;
  box-sizing: border-box;
  transition: transform 0.12s ease-out,
              box-shadow 0.12s ease-out;
  will-change: transform;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

/* 3D Green/Bamboo Backing & Thickness Layer */
.tile-backing {
  position: absolute;
  inset: 0;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--tile-side-top), var(--tile-side-bot));
  box-shadow: 
    inset 0 1px 1px rgba(255, 255, 255, 0.35),
    inset 0 -2px 3px rgba(0, 0, 0, 0.45);
}

/* Ivory Tile Face */
.tile-face {
  position: absolute;
  top: 0;
  left: 0;
  right: calc(var(--layer-depth) * 0.85);
  bottom: calc(var(--layer-depth) * 1.1);
  border-radius: 5px;
  background: linear-gradient(170deg, var(--tile-face-top) 0%, var(--tile-face-bot) 100%);
  border: 1px solid var(--tile-border);
  box-shadow: 
    inset 0 1px 1px #ffffff,
    inset -1px -1px 2px rgba(0, 0, 0, 0.08),
    1px 2px 3px rgba(0, 0, 0, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.tile-svg {
  width: 90%;
  height: 90%;
  pointer-events: none;
}

/* --------------------------------------------------------------------------
   Elevation Shadows by Z Layer
   -------------------------------------------------------------------------- */
.mahjong-tile[data-z="0"] {
  filter: drop-shadow(2px 3px 2px var(--tile-shadow));
}

.mahjong-tile[data-z="1"] {
  filter: drop-shadow(3px 5px 3.5px var(--tile-shadow));
}

.mahjong-tile[data-z="2"] {
  filter: drop-shadow(4px 7px 5px var(--tile-shadow));
}

.mahjong-tile[data-z="3"] {
  filter: drop-shadow(5px 9px 7px var(--tile-shadow));
}

.mahjong-tile[data-z="4"] {
  filter: drop-shadow(6px 12px 9px var(--tile-shadow));
}

/* --------------------------------------------------------------------------
   Interactive States & Animations
   -------------------------------------------------------------------------- */

/* Free / Playable Hover */
@media (hover: hover) {
  .mahjong-tile.is-free:hover {
    transform: translate(-1px, -3px) scale(1.02);
    z-index: 99 !important;
  }
  .mahjong-tile.is-free:hover .tile-face {
    border-color: #f59e0b;
  }
}

/* Selected State */
.mahjong-tile.is-selected {
  transform: translate(-2px, -6px) scale(1.05);
  z-index: 100 !important;
}

.mahjong-tile.is-selected .tile-face {
  border: 2px solid var(--highlight-selected);
  background: linear-gradient(170deg, #fffbeb 0%, #fef3c7 100%);
  box-shadow: 
    0 0 14px rgba(245, 158, 11, 0.75),
    inset 0 0 4px rgba(245, 158, 11, 0.3);
}

/* Blocked / Unplayable State */
.mahjong-tile.is-blocked {
  cursor: default;
}

/* Blocked click shake feedback */
.mahjong-tile.shake {
  animation: tileShake 0.28s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

@keyframes tileShake {
  10%, 90% { transform: translate3d(-2px, 0, 0); }
  20%, 80% { transform: translate3d(2px, 0, 0); }
  30%, 50%, 70% { transform: translate3d(-3px, 0, 0); }
  40%, 60% { transform: translate3d(3px, 0, 0); }
}

/* Hint Glow */
.mahjong-tile.is-hint {
  animation: hintPulse 1.2s infinite alternate ease-in-out;
  z-index: 95 !important;
}

.mahjong-tile.is-hint .tile-face {
  border: 2px solid var(--highlight-hint);
  box-shadow: 0 0 16px rgba(56, 189, 248, 0.85);
}

@keyframes hintPulse {
  0% { transform: scale(1); filter: drop-shadow(0 0 4px var(--highlight-hint)); }
  100% { transform: translateY(-3px) scale(1.05); filter: drop-shadow(0 0 14px var(--highlight-hint)); }
}

/* Removal Match Animation */
.mahjong-tile.is-removing {
  animation: matchDisappear 0.26s forwards ease-in;
  pointer-events: none;
}

@keyframes matchDisappear {
  0% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.8;
    transform: translateY(-8px) scale(1.1);
  }
  100% {
    opacity: 0;
    transform: translateY(-16px) scale(0.6);
  }
}
