/* ── Reset & base ──────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: #1a472a;
  color: #e8e0d0;
  overflow: hidden;
  height: 100vh;
  width: 100vw;
}

/* ── Seat selection ───────────────────────────────────────────────── */
.screen {
  max-width: 1200px;
  margin: 0 auto;
  padding: 32px;
}

#seat-select {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  gap: 48px;
}

#seat-select h1 {
  font-size: 5rem;
  color: #f0d060;
  text-shadow: 4px 4px 8px rgba(0,0,0,0.5);
}

#seat-select p {
  font-size: 2.2rem;
  opacity: 0.8;
}

.seat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 32px;
  max-width: 800px;
}

.seat-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 48px 64px;
  background: rgba(255,255,255,0.08);
  border: 3px solid rgba(240,208,96,0.3);
  border-radius: 24px;
  color: #e8e0d0;
  font-size: 2rem;
  cursor: pointer;
  transition: all 0.2s;
}

.seat-btn:hover {
  background: rgba(240,208,96,0.15);
  border-color: #f0d060;
  transform: translateY(-4px);
}

.wind-char {
  font-size: 4rem;
  color: #f0d060;
}

/* ── Game board ───────────────────────────────────────────────────── */
#game-board {
  width: 100vw;
  height: 100vh;
  position: relative;
}

/* ── Table grid — 3×3 ────────────────────────────────────────────── */
#table {
  display: grid;
  grid-template-areas:
    ".      top     ."
    "left   center  right"
    ".      bottom  .";
  grid-template-columns: minmax(100px, 320px) 1fr minmax(100px, 320px);
  grid-template-rows: auto 1fr auto;
  width: 100%;
  height: 100%;
  padding: 8px;
  gap: 4px;
}

/* ── Player labels ────────────────────────────────────────────────── */
.player-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.5rem;
  font-weight: 600;
  color: #c8b888;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 4px 8px;
}

#bottom-label { color: #f0d060; justify-content: center; }

/* ── Top player ───────────────────────────────────────────────────── */
#player-top {
  grid-area: top;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px;
}

.player-hand-row {
  display: flex;
  gap: 2px;
  justify-content: center;
  flex-wrap: nowrap;
}

.player-melds-row {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

/* ── Bottom (human) player ────────────────────────────────────────── */
#player-bottom {
  grid-area: bottom;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 8px 16px 16px;
  background: rgba(0,0,0,0.15);
  border-top: 2px solid rgba(240,208,96,0.15);
}

#bottom-hand {
  display: flex;
  gap: 6px;
  justify-content: center;
  align-items: flex-end;
  min-height: 124px;
  flex-wrap: nowrap;
}

#bottom-extras {
  display: flex;
  gap: 24px;
  align-items: center;
  font-size: 1.6rem;
  flex-wrap: wrap;
  justify-content: center;
}

#bottom-melds { display: flex; gap: 12px; flex-wrap: wrap; }
#bottom-flowers { display: flex; gap: 4px; }
#bottom-shanten { color: #c8b888; font-size: 1.5rem; }

#action-bar {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  justify-content: center;
  min-height: 64px;
}

/* ── Side players (left & right) ──────────────────────────────────── */
#player-left {
  grid-area: left;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px;
}

#player-right {
  grid-area: right;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px;
}

#player-right .player-label { justify-content: flex-end; }

.side-body {
  display: flex;
  gap: 8px;
  flex: 1;
  min-height: 0;
}

.right-body {
  /* Default row direction: discards on left (toward center), hand on right edge */
}

.side-right-part {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
  min-height: 0;
}

/* Vertical hand columns for side players */
.player-hand-col {
  display: flex;
  flex-direction: column;
  gap: 0;
  align-items: center;
  flex-shrink: 0;
}

.player-melds-col {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-start;
}

#player-right .player-melds-col { align-items: flex-end; }

/* ── Center — contains all discard pools ──────────────────────────── */
#center {
  grid-area: center;
  display: flex;
  flex-direction: column;
  background: rgba(0,0,0,0.15);
  border-radius: 16px;
  padding: 8px;
  gap: 4px;
}

#center-row {
  display: flex;
  flex: 1;
  gap: 4px;
  align-items: stretch;
  min-height: 0;
}

#center-middle {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  min-width: 0;
}

#center-info {
  width: 560px;
  max-width: 100%;
  height: 280px;
  max-height: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
}

#center-left {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

#center-wind {
  font-size: 6rem;
  font-weight: 700;
  color: rgba(240,208,96,0.5);
  line-height: 1;
  font-family: "Noto Sans CJK", "Microsoft YaHei", serif;
}

#center-stats {
  font-size: 1.5rem;
  color: #c8b888;
  text-align: center;
  line-height: 1.6;
}

#center-discard {
  min-height: 124px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ── Discard pools — inside center ────────────────────────────────── */
.discard-pool {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
}

.center-discards-top {
  justify-content: center;
}
.center-discards-top .tile { transform: rotate(180deg); }

.center-discards-bottom {
  justify-content: center;
}

.center-discards-left {
  flex-direction: column;
  flex-wrap: wrap;
  align-content: flex-end;
  max-height: 100%;
}
.center-discards-left .tile { transform: rotate(90deg); margin: -10px 10px; }

.center-discards-right {
  flex-direction: column;
  flex-wrap: wrap;
  align-content: flex-start;
  max-height: 100%;
}
.center-discards-right .tile { transform: rotate(-90deg); margin: -10px 10px; }

/* ── Rotation for opponent melds & flowers ────────────────────────── */
.rot-180 { transform: rotate(180deg); }

.rot-90-container .tile { transform: rotate(90deg); margin: -10px 10px; }
.rot-90-container .meld-group { flex-direction: column; }

.rot-270-container .tile { transform: rotate(-90deg); margin: -10px 10px; }
.rot-270-container .meld-group { flex-direction: column; }

/* ── Tiles ────────────────────────────────────────────────────────── */
.tile {
  width: 88px;
  height: 124px;
  border: 2px solid #8a7a5a;
  border-radius: 8px;
  background: linear-gradient(135deg, #fffff0, #f5eed6);
  box-shadow: 2px 4px 6px rgba(0,0,0,0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  flex-shrink: 0;
  user-select: none;
}

.tile .tile-content {
  font-size: 22px;
  line-height: 1.1;
  text-align: center;
  color: #222;
  white-space: pre;
  font-family: "Noto Sans CJK", "Microsoft YaHei", sans-serif;
}

/* Suit colors */
.tile.suit-bamboo .tile-content { color: #228b22; }
.tile.suit-dots .tile-content { color: #1565c0; }
.tile.suit-wan .tile-content { color: #1a1a1a; }
.tile.suit-wind .tile-content { color: #333; }
.tile.dragon-red .tile-content { color: #c62828; }
.tile.dragon-green .tile-content { color: #2e7d32; }
.tile.dragon-white .tile-content { color: #666; }
.tile.suit-flower .tile-content { color: #6a1b9a; }

/* Interactive human tiles */
.tile.clickable { cursor: pointer; }

.tile.clickable:hover {
  transform: translateY(-20px);
  box-shadow: 0 16px 32px rgba(0,0,0,0.4);
  border-color: #f0d060;
  z-index: 2;
}

.tile.selected {
  transform: translateY(-28px);
  box-shadow: 0 0 32px rgba(100,149,237,0.6);
  border-color: #6495ed;
  z-index: 3;
}

.tile.just-drew {
  margin-left: 32px;
}

/* ── Tile sizes ───────────────────────────────────────────────────── */

/* Small — melds, opponent details */
.tile.sm { width: 60px; height: 84px; border-radius: 6px; }
.tile.sm .tile-content { font-size: 16px; }

/* Extra small — discards */
.tile.xs { width: 44px; height: 64px; border-radius: 4px; }
.tile.xs .tile-content { font-size: 14px; }

/* Micro — event log tiles */
.tile.xxs { width: 32px; height: 44px; border-radius: 4px; border-width: 1px; box-shadow: none; }
.tile.xxs .tile-content { font-size: 10px; }

/* ── Tile backs ───────────────────────────────────────────────────── */
.tile-back {
  width: 60px;
  height: 84px;
  border: 2px solid #445;
  border-radius: 6px;
  background: linear-gradient(135deg, #2e5090, #1a3060);
  box-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  flex-shrink: 0;
}

/* Horizontal tile-back for vertical side stacking */
.tile-back-h {
  width: 72px;
  height: 36px;
  border: 2px solid #445;
  border-radius: 4px;
  background: linear-gradient(135deg, #2e5090, #1a3060);
  box-shadow: 0 2px 4px rgba(0,0,0,0.25);
  margin-bottom: 2px;
}

/* Highlight last discard */
.tile.last-discarded {
  border-color: #f0d060;
  box-shadow: 0 0 20px rgba(240,208,96,0.6);
}

/* ── Meld groups ──────────────────────────────────────────────────── */
.meld-group {
  display: flex;
  gap: 2px;
  border: 2px solid rgba(200,184,136,0.2);
  border-radius: 6px;
  padding: 2px;
  background: rgba(0,0,0,0.1);
}

/* Vertical meld group for side players */
.meld-group-v {
  display: flex;
  flex-direction: row;
  gap: 2px;
  border: 2px solid rgba(200,184,136,0.2);
  border-radius: 6px;
  padding: 2px;
  background: rgba(0,0,0,0.1);
}

/* ── Animations ───────────────────────────────────────────────────── */
@keyframes fadeSlideIn {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes fadeSlideInLeft {
  from { opacity: 0; transform: translateX(-24px); }
  to   { opacity: 1; transform: translateX(0); }
}

@keyframes fadeSlideInRight {
  from { opacity: 0; transform: translateX(24px); }
  to   { opacity: 1; transform: translateX(0); }
}

@keyframes popIn {
  0%   { opacity: 0; transform: scale(0.6); }
  60%  { opacity: 1; transform: scale(1.08); }
  100% { opacity: 1; transform: scale(1); }
}

@keyframes glowPulse {
  0%   { box-shadow: 0 0 8px rgba(240,208,96,0.3); }
  50%  { box-shadow: 0 0 32px rgba(240,208,96,0.7); }
  100% { box-shadow: 0 0 8px rgba(240,208,96,0.3); }
}

@keyframes claimFlash {
  0%   { background: rgba(240,208,96,0.0); }
  30%  { background: rgba(240,208,96,0.15); }
  100% { background: rgba(240,208,96,0.0); }
}

.anim-fade-in  { animation: fadeSlideIn 0.35s ease-out both; }
.anim-pop-in   { animation: popIn 0.4s ease-out both; }
.anim-glow     { animation: glowPulse 0.8s ease-in-out; }
.anim-claim    { animation: claimFlash 0.6s ease-out; }

.anim-slide-left  { animation: fadeSlideInLeft 0.35s ease-out both; }
.anim-slide-right { animation: fadeSlideInRight 0.35s ease-out both; }

/* ── Tile inner content patterns ──────────────────────────────────── */

/* Dots grid */
.dots-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 56px;
  height: 80px;
  align-items: center;
  justify-items: center;
}

.dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #1565c0;
}

/* 2×4 grid for 8 dots */
.dots-grid-2x4 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(4, 1fr);
  width: 38px;
  height: 80px;
  align-items: center;
  justify-items: center;
}

/* 7 dots — shallow diagonal + centered 2×2, gaps match 9-circle grid */
.dots-7 {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 56px;
  height: 80px;
}

.dots-7-diag {
  display: flex;
  align-items: flex-end;
  gap: 5px;
}

.dots-7-diag .dot:nth-child(2) { margin-bottom: 4px; }
.dots-7-diag .dot:nth-child(3) { margin-bottom: 8px; }

.dots-7-square {
  display: grid;
  grid-template-columns: auto auto;
  column-gap: 5px;
  row-gap: 13px;
}

.sm .dots-grid { width: 36px; height: 56px; }
.sm .dots-grid-2x4 { width: 24px; height: 56px; }
.sm .dots-7 { width: 36px; height: 56px; gap: 7px; }
.sm .dots-7-diag { gap: 3px; }
.sm .dots-7-diag .dot:nth-child(2) { margin-bottom: 3px; }
.sm .dots-7-diag .dot:nth-child(3) { margin-bottom: 6px; }
.sm .dots-7-square { column-gap: 3px; row-gap: 9px; }
.sm .dot { width: 10px; height: 10px; }
.xs .dots-grid { width: 26px; height: 40px; }
.xs .dots-grid-2x4 { width: 18px; height: 40px; }
.xs .dots-7 { width: 26px; height: 40px; gap: 4px; }
.xs .dots-7-diag { gap: 2px; }
.xs .dots-7-diag .dot:nth-child(2) { margin-bottom: 2px; }
.xs .dots-7-diag .dot:nth-child(3) { margin-bottom: 4px; }
.xs .dots-7-square { column-gap: 2px; row-gap: 6px; }
.xs .dot { width: 6px; height: 6px; }
.xxs .dots-grid { width: 20px; height: 28px; }
.xxs .dots-grid-2x4 { width: 14px; height: 28px; }
.xxs .dots-7 { width: 20px; height: 28px; gap: 3px; }
.xxs .dots-7-diag { gap: 1px; }
.xxs .dots-7-diag .dot:nth-child(2) { margin-bottom: 1px; }
.xxs .dots-7-diag .dot:nth-child(3) { margin-bottom: 2px; }
.xxs .dots-7-square { column-gap: 1px; row-gap: 4px; }
.xxs .dot { width: 4px; height: 4px; }

/* Bamboo grid */
.bamboo-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 56px;
  height: 80px;
  align-items: center;
  justify-items: center;
}

.bamboo-stick {
  font-size: 24px;
  font-weight: 700;
  color: #228b22;
  line-height: 1;
}

.sm .bamboo-grid { width: 36px; height: 56px; }
.sm .bamboo-stick { font-size: 18px; }
.xs .bamboo-grid { width: 26px; height: 40px; }
.xs .bamboo-stick { font-size: 14px; }
.xxs .bamboo-grid { width: 20px; height: 28px; }
.xxs .bamboo-stick { font-size: 10px; }

/* 8 bamboo — crossed sticks (M/W pattern), same grid as 6-bamboo */
.bamboo-cross {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  width: 56px;
  height: 80px;
  align-items: center;
  justify-items: center;
  font-family: monospace;
  font-weight: 700;
  color: #228b22;
  line-height: 1;
  font-size: 26px;
  letter-spacing: -0.05em;
}

.bamboo-cross > div:nth-child(1) { grid-row: 1; }
.bamboo-cross > div:nth-child(2) { grid-row: 3; }

.sm .bamboo-cross { width: 36px; height: 56px; font-size: 19px; }
.xs .bamboo-cross { width: 26px; height: 40px; font-size: 14px; }
.xxs .bamboo-cross { width: 20px; height: 28px; font-size: 10px; }

/* Wan (Characters) tiles */
.wan-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  line-height: 1.2;
}

.wan-num { font-size: 32px; font-weight: 600; }
.wan-label { font-size: 24px; }

.sm .wan-num { font-size: 22px; }
.sm .wan-label { font-size: 16px; }
.xs .wan-num { font-size: 18px; }
.xs .wan-label { font-size: 12px; }
.xxs .wan-num { font-size: 14px; }
.xxs .wan-label { font-size: 10px; }

/* Honor characters (winds & dragons) */
.honor-char { font-size: 44px; font-weight: 700; line-height: 1; }

.sm .honor-char { font-size: 32px; }
.xs .honor-char { font-size: 22px; }
.xxs .honor-char { font-size: 16px; }

/* White dragon frame */
.white-dragon-frame {
  width: 40px;
  height: 56px;
  border: 4px solid #888;
  border-radius: 4px;
}

.sm .white-dragon-frame { width: 28px; height: 40px; border-width: 3px; }
.xs .white-dragon-frame { width: 20px; height: 28px; border-width: 2px; }
.xxs .white-dragon-frame { width: 14px; height: 20px; border-width: 1px; }

/* Flower tiles */
.flower-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  line-height: 1.2;
}

.flower-label { font-size: 28px; font-weight: 600; color: #6a1b9a; }
.flower-num { font-size: 24px; color: #6a1b9a; }

.sm .flower-label { font-size: 20px; }
.sm .flower-num { font-size: 16px; }
.xs .flower-label { font-size: 16px; }
.xs .flower-num { font-size: 12px; }
.xxs .flower-label { font-size: 12px; }
.xxs .flower-num { font-size: 10px; }

/* ── Action buttons ───────────────────────────────────────────────── */
.btn {
  padding: 12px 32px;
  border: 2px solid rgba(240,208,96,0.4);
  border-radius: 12px;
  background: rgba(240,208,96,0.15);
  color: #f0d060;
  font-size: 1.7rem;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}

.btn:hover {
  background: rgba(240,208,96,0.3);
  border-color: #f0d060;
}

.btn.primary {
  background: rgba(240,208,96,0.25);
  border-color: #f0d060;
  font-weight: 600;
}

.btn.hu {
  background: rgba(200,50,50,0.3);
  border-color: #e55;
  color: #ff8888;
}

.btn.hu:hover {
  background: rgba(200,50,50,0.5);
}

.btn.pass { opacity: 0.7; }
.btn:disabled { opacity: 0.3; cursor: default; }

.btn.action-tile-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.action-tiles {
  display: inline-flex;
  gap: 2px;
}

/* ── Events log — inside center ───────────────────────────────────── */
#events-log {
  flex: 1;
  align-self: stretch;
  min-width: 0;
  background: rgba(0,0,0,0.2);
  border: 1px solid rgba(200,184,136,0.1);
  border-radius: 12px;
  padding: 10px 14px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#events-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-y: auto;
  flex: 1;
}

.event-line {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.4rem;
  color: #b0a888;
  line-height: 1.3;
  padding: 4px 0;
  flex-shrink: 0;
  animation: fadeSlideIn 0.3s ease-out both;
}

.event-line.hu { color: #ff8888; font-weight: 600; }

.wind-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  font-size: 20px;
  font-weight: 700;
  flex-shrink: 0;
  font-family: "Noto Sans CJK", "Microsoft YaHei", sans-serif;
}

.wind-badge.east  { background: rgba(34,139,34,0.3); color: #6c6; }
.wind-badge.south { background: rgba(200,50,50,0.3); color: #f88; }
.wind-badge.west  { background: rgba(100,149,237,0.3); color: #8af; }
.wind-badge.north { background: rgba(200,180,60,0.3); color: #dd8; }

.event-text { white-space: nowrap; }

.event-tiles {
  display: inline-flex;
  gap: 2px;
  flex-shrink: 0;
}

/* ── Game over overlay ────────────────────────────────────────────── */
#game-over {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

#game-over-content {
  background: #1a2a1a;
  border: 3px solid #f0d060;
  border-radius: 24px;
  padding: 56px;
  max-width: 900px;
  width: 90%;
  text-align: center;
}

#game-over-title {
  font-size: 3.2rem;
  color: #f0d060;
  margin-bottom: 24px;
}

#game-over-details {
  text-align: left;
  margin-bottom: 40px;
  line-height: 1.7;
  font-size: 1.5rem;
}

.tai-breakdown {
  margin: 20px 0;
  padding: 16px 24px;
  background: rgba(0,0,0,0.3);
  border-radius: 12px;
}

.tai-row {
  display: flex;
  justify-content: space-between;
  font-size: 1.7rem;
}

.payments-table {
  width: 100%;
  margin: 20px 0;
  border-collapse: collapse;
}

.payments-table th, .payments-table td {
  padding: 6px 20px;
  text-align: center;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  font-size: 1.7rem;
}

.payments-table .positive { color: #4caf50; }
.payments-table .negative { color: #f44336; }

.winner-hand {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  justify-content: center;
  margin: 20px 0;
}

/* ── Responsive ───────────────────────────────────────────────────── */
@media (max-width: 1800px) {
  .tile { width: 72px; height: 104px; }
  .tile .tile-content { font-size: 18px; }
  .tile.sm { width: 48px; height: 68px; }
  .tile.sm .tile-content { font-size: 14px; }
  .tile.xs { width: 36px; height: 52px; }
  .tile.xs .tile-content { font-size: 12px; }
  .tile.just-drew { margin-left: 20px; }
  .tile-back { width: 48px; height: 68px; }
  .tile-back-h { width: 60px; height: 28px; margin-bottom: 1px; }
  .event-line { font-size: 1.3rem; }
  .center-discards-left .tile,
  .center-discards-right .tile,
  .rot-90-container .tile,
  .rot-270-container .tile { margin: -8px 8px; }
  #table {
    grid-template-columns: minmax(80px, 240px) 1fr minmax(80px, 240px);
    grid-template-rows: auto 1fr auto;
  }
}
