/* Visual language ported from the iOS app (TienGow). Colours come from
 * Color.tg* in Views/Game/TileView.swift and the "默認" CosmeticTheme
 * (Services/StoreKitService.swift); metrics come from TileSize, TableMetrics
 * and the SwiftUI views themselves. Anything with a magic number here has a
 * matching magic number over there — keep them in sync. */
:root {
  /* Palette — Color.tg* + CosmeticTheme "default" */
  --tg-table: #1c1c2e;        /* tableColor / .tgBackground */
  --tg-surface: #2a2a3e;      /* .tgSurface */
  --tg-accent: #c0392b;       /* .tgAccent */
  --tg-domino-face: #1a1a1a;  /* dominoTopColor */
  --tg-domino-back: #0f0f19;  /* ThemeService.dominoBackColor: table x 0.55 */
  --tg-domino-border: rgba(122, 106, 74, 0.7);   /* .tgDominoBorder */
  --tg-domino-divider: rgba(122, 106, 74, 0.49); /* border x 0.7, as in TileView */
  --tg-pip-white: #f5f5f0;
  --tg-pip-red: #e63946;
  --tg-text: #fff;
  --tg-subtext: rgba(255, 255, 255, 0.55);
  --tg-green: #30d158;        /* SwiftUI .green, dark appearance */
  --tg-red: #ff453a;          /* SwiftUI .red, dark appearance */

  /* Set from JS (mirrors TableMetrics) */
  --tile-w: 44px;             /* local player's tile width */
  --hand-gap: -6.6px;         /* -0.15 x tile width */
  --trick-size: 170px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--tg-table);
  color: var(--tg-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Microsoft JhengHei", sans-serif;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 520px;
  margin: 0 auto;
  background: var(--tg-table);
  /* The SwiftUI layout gets the safe area for free; a full-bleed Mini App
   * has to ask for it, or the hand sits under the home indicator. */
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---------------------------------------------------------------- Top bar */
/* GameTableView.topBar: round + banker on the left, score bar on the right. */

#topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px 0;
  flex-shrink: 0;
}

#round-block { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
#round-info { font-size: 15px; font-weight: 700; white-space: nowrap; }
#banker-info { font-size: 11px; color: var(--tg-subtext); white-space: nowrap; }

/* "連莊×N" — the round's stakes. */
#multiplier-pill {
  font-size: 11px;
  font-weight: 700;
  color: #ffd60a;
  background: rgba(255, 214, 10, 0.15);
  padding: 4px 8px;
  border-radius: 999px;
  white-space: nowrap;
}

#btn-leave-room { font-size: 11px; padding: 4px 10px; }

#scores { display: flex; gap: 10px; margin-left: auto; }
.score-chip { display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 28px; }
.score-chip .score-name { font-size: 11px; color: var(--tg-subtext); white-space: nowrap; }
.score-chip .score-value {
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.score-chip.is-me .score-value { color: var(--tg-accent); }

/* ------------------------------------------------------------------ Table */

#table {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 0;
}
/* SwiftUI Spacer(minLength: 8) above and below the seat block. */
.v-spacer { flex: 1 1 8px; min-height: 8px; }

#mid-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 0 8px;
}

/* -------------------------------------------------------- Opponent seats */
/* OpponentHandView: top seat is a row (莊 · name · N墩) above its fan; the
 * side seats stack fan → arrow → chips → name inside a fixed-width cell so
 * played tiles in the trick area can never cover their labels. */

.seat {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 6px;
  border-radius: 10px;
}
.seat.is-turn { background: rgba(192, 57, 43, 0.15); }

.seat-top { margin: 0 20px; gap: 6px; }
.seat-side { width: 92px; gap: 5px; }

.seat-label { display: flex; align-items: center; justify-content: center; gap: 4px; }
.seat-name {
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.seat.is-turn .seat-name { color: var(--tg-accent); }

/* Side cells wrap so 莊 + N墩 share a line and the name gets its own. */
.seat-side .seat-label { flex-wrap: wrap; }
.seat-side .banker-chip { order: 0; }
.seat-side .tricks-badge { order: 1; }
.seat-side .seat-name { order: 2; flex-basis: 100%; text-align: center; }

/* The "your turn" arrow (SF Symbol arrow.down.circle.fill): an accent disc
 * with the arrow knocked out to the tinted cell colour behind it. */
.seat::after {
  content: "";
  display: none;
  width: 15px;
  height: 15px;
  background: no-repeat center/contain url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><circle cx="8" cy="8" r="8" fill="%23c0392b"/><path d="M8 3.4a.75.75 0 0 1 .75.75v5.03l1.72-1.72a.75.75 0 1 1 1.06 1.06l-3 3a.75.75 0 0 1-1.06 0l-3-3a.75.75 0 0 1 1.06-1.06l1.72 1.72V4.15A.75.75 0 0 1 8 3.4Z" fill="%2335202d"/></svg>');
}
.seat.is-turn::after { display: block; }
.seat-top.is-turn::after { order: 3; }   /* below the fan */
.seat-side.is-turn::after { order: 1; }  /* between fan and labels */
.seat-top .seat-label { order: 1; }
.seat-top .opponent-hand { order: 2; }
.seat-side .opponent-hand { order: 0; }
.seat-side .seat-label { order: 2; }

/* Face-down fan — TileSize.small (26pt) tiles, 7pt apart, left-stacked so the
 * last tile is fully visible and the earlier ones peek out behind it. */
.opponent-hand { position: relative; --tile-w: 26px; }
.seat-top .opponent-hand { width: var(--fan-w); height: 52px; }

.fan {
  position: absolute;
  left: 50%;
  top: 50%;
  display: flex;
  transform: translate(-50%, -50%);
}
.fan .tile-back { margin-left: -19px; }  /* 26 wide, 7 step */
.fan .tile-back:first-child { margin-left: 0; }

/* Side seats rotate only the fan; the wrapper takes the swapped box so the
 * labels below it can't be overlapped. 52px = TileSize.small.height. */
.seat-side .opponent-hand { width: 52px; height: var(--fan-w); }
.seat-left .fan { transform: translate(-50%, -50%) rotate(90deg); }
.seat-right .fan { transform: translate(-50%, -50%) rotate(270deg); }

/* ------------------------------------------------------------- Trick area */
/* TrickAreaView: a square with a felt disc, plays anchored per seat. */

#trick-area {
  position: relative;
  width: var(--trick-size);
  height: var(--trick-size);
  flex-shrink: 0;
}
#trick-circle {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 80%;
  height: 80%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: rgba(42, 42, 62, 0.4);
}
#trick-center {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 20px;
  font-weight: 700;
  color: var(--tg-subtext);
  text-align: center;
}
.trick-slot {
  position: absolute;
  display: flex;
  align-items: center;
  transform: translate(-50%, -50%);
}
/* A declined trick reads lighter than the combo it couldn't beat. */
.discard-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: 700;
  color: var(--tg-subtext);
  background: var(--tg-surface);
}

/* ------------------------------------------------------------------ Tiles */
/* TileView + TileSize: every dimension is a ratio of the tile's width. */

.tile, .tile-back {
  --tile-pad: max(3px, calc(var(--tile-w) * 0.1));
  position: relative;
  flex-shrink: 0;
  width: var(--tile-w);
  height: calc(var(--tile-w) * 2);
  border-radius: calc(var(--tile-w) * 0.155);
  background: var(--tg-domino-face);
  box-shadow: inset 0 0 0 1px var(--tg-domino-border), 0 1px 4px rgba(0, 0, 0, 0.35);
}

/* Face-down tiles sit directly on the felt, so they use an opaque darkened
 * felt rather than a translucent tint (see ThemeService.dominoBackColor). */
.tile-back { background: var(--tg-domino-back); }

.tile-inner { position: absolute; inset: var(--tile-pad); display: flex; flex-direction: column; }
.tile-half { position: relative; flex: 1; }
.tile-divider { height: 1px; background: var(--tg-domino-divider); margin: 0 var(--tile-pad); }

.pip {
  position: absolute;
  width: calc(var(--tile-w) * 0.165);
  height: calc(var(--tile-w) * 0.165);
  border-radius: 50%;
  transform: translate(-50%, -50%);
}
.pip.white { background: var(--tg-pip-white); }
.pip.red { background: var(--tg-pip-red); }

/* 長三 (3-3) is one continuous zigzag across the whole tile rather than two
 * independent halves, so it draws its own divider edge to edge. */
.tile-changsan { position: absolute; inset: 0; }
.tile-changsan .tile-divider { position: absolute; left: 0; right: 0; top: 50%; margin: 0; }

.tile.selectable { cursor: pointer; }
.tile { transition: transform 0.25s cubic-bezier(0.34, 1.3, 0.64, 1); }
.tile.selected {
  transform: translateY(calc(-1 * max(4px, var(--tile-w) * 0.18)));
  box-shadow: inset 0 0 0 2.5px var(--tg-accent), 0 6px 12px rgba(0, 0, 0, 0.35);
}
.tile.disabled { opacity: 0.7; cursor: default; }

/* --------------------------------------------------------------- My hand */
/* PlayerHandView: badges, then action buttons, then the fanned hand. */

#hand-tray {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding-bottom: 4px;
}

#hand-badges { display: flex; align-items: center; gap: 6px; }
#hand-badges .hand-you { font-size: 12px; color: var(--tg-subtext); }

#action-bar { display: flex; gap: 12px; justify-content: center; }

#hand-tiles {
  display: flex;
  justify-content: center;
  padding: 14px 8px 4px;
  width: 100%;
}
#hand-tiles .tile { margin-left: var(--hand-gap); }
#hand-tiles .tile:first-child { margin-left: 0; }

/* --------------------------------------------------------------- Chrome */

.banker-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  background: var(--tg-accent);
  border-radius: 50%;
  padding: 3px;
  min-width: 16px;
  min-height: 16px;
  line-height: 1;
}

.tricks-badge {
  font-size: 10px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.33);
  background: rgba(42, 42, 62, 0.7);
  border-radius: 999px;
  padding: 1px 5px;
  white-space: nowrap;
}
.tricks-badge.has-tricks { color: var(--tg-accent); }

button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 700;
  padding: 8px 22px;
  border-radius: 999px;
  border: none;
  background: var(--tg-accent);
  --icon-knockout: var(--tg-accent);
  color: #fff;
  cursor: pointer;
}
button:disabled { background: rgba(142, 142, 147, 0.4); --icon-knockout: #57575c; cursor: default; }
button.secondary { background: var(--tg-surface); --icon-knockout: var(--tg-surface); }
button.hidden, .hidden { display: none !important; }
.btn-icon { width: 15px; height: 15px; flex-shrink: 0; }
.btn-icon .knockout { fill: var(--icon-knockout); }

/* TurnBannerView — a capsule over the felt, not a full-width bar. */
#status-banner {
  position: absolute;
  left: 50%;
  top: 8px;
  transform: translateX(-50%);
  z-index: 5;
  font-size: 12px;
  color: var(--tg-subtext);
  background: var(--tg-surface);
  border-radius: 999px;
  padding: 4px 12px;
  white-space: nowrap;
  pointer-events: none;
}
#status-banner:empty { display: none; }

/* ----------------------------------------------------------------- Modals */
/* RoundResultView / LobbyView / GameOverView: a tgBackground card with a
 * 20pt radius over a single 60% black dim. */

#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding: 24px;
}
#modal-overlay.hidden { display: none; }
#modal-content {
  background: var(--tg-table);
  border-radius: 20px;
  padding: 24px;
  max-width: 400px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  text-align: center;
}
#modal-content h2 { margin: 0 0 4px; font-size: 22px; font-weight: 700; color: var(--tg-text); }
#modal-content p { font-size: 13px; color: var(--tg-subtext); margin: 8px 0; }
#modal-content table { width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 15px; }
#modal-content td { padding: 5px 6px; border-bottom: 1px solid rgba(255, 255, 255, 0.1); }
#modal-content .delta-pos { color: var(--tg-green); font-weight: 700; }
#modal-content .delta-neg { color: var(--tg-red); font-weight: 700; }
#modal-content .modal-actions { display: flex; gap: 10px; justify-content: center; margin-top: 14px; flex-wrap: wrap; }
#modal-content .modal-status { min-height: 16px; color: var(--tg-accent); font-size: 13px; }

/* HomeView — its own screen, so no card and no dim behind it. */
#modal-overlay.home { background: var(--tg-table); }
#modal-overlay.home #modal-content { background: none; padding: 0 8px; max-width: 420px; }
.home-title { font-size: 52px; font-weight: 900; letter-spacing: 2px; line-height: 1.1; }
.home-subtitle { font-size: 12px; color: var(--tg-subtext); letter-spacing: 4px; margin-top: 6px; }
#modal-content .home-section { margin: 28px 0 10px; }

.menu-block { display: flex; flex-direction: column; gap: 14px; }
.menu-button {
  width: 100%;
  justify-content: flex-start;
  font-size: 17px;
  padding: 16px 20px;
  border-radius: 14px;
  background: var(--tg-surface);
}
.menu-button.accent { background: var(--tg-accent); }
.menu-button .chevron { margin-left: auto; font-size: 20px; opacity: 0.5; }
#modal-content input {
  font-size: 16px;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--tg-domino-border);
  background: var(--tg-surface);
  color: var(--tg-text);
  text-align: center;
  width: 140px;
  letter-spacing: 4px;
}
