:root {
  --felt: #0d5c3d;
  --felt-dark: #09482f;
  --wood: #5c3a1e;
  --panel: #1a2332;
  --text: #f2f5f0;
  --muted: #a8b5a8;
  --accent: #f0c14b;
  --danger: #e85d5d;
  --ok: #6bcf7f;
  --tile-w: 42px;
  --tile-h: 56px;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: #0a1620; color: var(--text);
}
#app {
  display: grid;
  grid-template-columns: 1fr 300px;
  grid-template-rows: auto 1fr;
  height: 100vh; max-height: 100vh;
  gap: 0;
}
#topbar {
  grid-column: 1 / -1;
  display: flex; align-items: center; gap: 12px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid #2a3a4a;
}
#topbar h1 { font-size: 16px; margin: 0; font-weight: 600; }
#topbar .spacer { flex: 1; }
button, .btn {
  background: #2a4050; color: var(--text);
  border: 1px solid #3a5568; border-radius: 6px;
  padding: 6px 12px; cursor: pointer; font-size: 13px;
}
button:hover:not(:disabled) { background: #355568; }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button.primary { background: #2d6a4f; border-color: #40916c; }
button.danger { background: #6a2d2d; border-color: #914040; }
button.accent { background: #6a5a2d; border-color: #c9a227; color: #fff8e0; }
button.ghost { background: transparent; }

#table-wrap {
  position: relative;
  background: radial-gradient(ellipse at center, var(--felt) 0%, var(--felt-dark) 70%);
  overflow: hidden;
  min-height: 0;
}
#table {
  position: absolute; inset: 8px;
  display: grid;
  grid-template-rows: 90px 1fr 140px;
  grid-template-columns: 100px 1fr 100px;
  gap: 6px;
}
.seat {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  min-height: 0;
}
/* Clip only opponent seats — player hand (#seat-south) may wrap large tiles */
#seat-north, #seat-west, #seat-east {
  overflow: hidden;
}
.seat-label {
  font-size: 11px; color: var(--muted); letter-spacing: 0.05em;
}
.seat-label.active { color: var(--accent); font-weight: 700; }
#seat-north { grid-row: 1; grid-column: 2; }
#seat-west { grid-row: 2; grid-column: 1; justify-content: center; }
#seat-east { grid-row: 2; grid-column: 3; justify-content: center; }
#seat-south { grid-row: 3; grid-column: 1 / -1; }
#center {
  grid-row: 2; grid-column: 2;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; min-height: 0;
  position: relative;
  z-index: 5; /* above overflowing opponent decorative tiles */
}
#discards {
  display: flex; flex-wrap: wrap; gap: 3px;
  max-width: 420px; max-height: 160px; overflow: auto;
  justify-content: center; padding: 6px;
  background: rgba(0,0,0,0.15); border-radius: 8px;
}
#wall-info { font-size: 12px; color: var(--muted); }
#actions {
  display: flex; flex-wrap: wrap; gap: 6px; justify-content: center;
}
#player-hand {
  display: flex; flex-wrap: wrap; gap: 4px; justify-content: center;
  padding: 6px; max-width: 100%;
}
#player-melds, .melds {
  display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; min-height: 20px;
}
.meld {
  display: flex; gap: 2px;
  padding: 2px; background: rgba(0,0,0,0.2); border-radius: 4px;
}

.tile {
  width: var(--tile-w); height: var(--tile-h);
  background: linear-gradient(160deg, #fffef5, #e8e0c8);
  color: #1a1a1a;
  border: 1px solid #c0b898;
  border-radius: 4px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700;
  cursor: default; user-select: none;
  box-shadow: 0 2px 3px rgba(0,0,0,0.25);
  line-height: 1.1;
  position: relative;
}
.tile .sub { font-size: 9px; color: #666; font-weight: 500; }
.tile.back {
  background: linear-gradient(160deg, #1e4d8c, #163a6a);
  border-color: #0f2a4d; color: transparent;
}
.tile.selected {
  outline: 2px solid var(--accent);
  transform: translateY(-8px);
}
.tile.clickable { cursor: pointer; }
.tile.clickable:hover { filter: brightness(1.05); transform: translateY(-4px); }
.tile.dots { color: #1a5fb4; }
.tile.bams { color: #2d6a4f; }
.tile.craks { color: #c1121f; }
.tile.dragon-r { color: #c1121f; }
.tile.dragon-g { color: #2d6a4f; }
.tile.dragon-wh { color: #333; border-width: 2px; }
.tile.wind { color: #1a1a1a; }
.tile.flower { color: #9b2226; background: linear-gradient(160deg, #fff5f5, #f0d0d0); }
.tile.joker {
  color: #6a4c93; background: linear-gradient(160deg, #f5e6ff, #e0d0f0);
  border-color: #9b7ebd;
}
.tile.tiny { width: 28px; height: 36px; font-size: 10px; }
.tile.tiny .sub { display: none; }

.tile.flash,
.tile.just-drawn,
.tile.just-discarded {
  z-index: 3;
  animation: tile-flash 1.25s ease;
}
.tile.just-drawn {
  box-shadow: 0 0 0 3px #6bcf7f, 0 0 18px rgba(107, 207, 127, 0.85);
  outline: 2px solid #6bcf7f;
}
.tile.just-discarded {
  box-shadow: 0 0 0 3px #f0c14b, 0 0 18px rgba(240, 193, 75, 0.95);
  outline: 2px solid #f0c14b;
}
.tile.back.just-drawn {
  box-shadow: 0 0 0 3px #6bcf7f, 0 0 14px rgba(107, 207, 127, 0.7);
}
@keyframes tile-flash {
  0% { transform: scale(1.18); filter: brightness(1.2); }
  60% { transform: scale(1.06); }
  100% { transform: scale(1); filter: brightness(1); }
}

#draw-flash {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px;
  padding: 8px 14px;
  background: rgba(0,0,0,0.45);
  border: 1px solid rgba(107, 207, 127, 0.45);
  border-radius: 8px;
}
#draw-flash[hidden] { display: none !important; }
#draw-flash .draw-flash-label {
  font-size: 16px; color: var(--ok); font-weight: 700;
  letter-spacing: 0.04em;
}

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

#side {
  background: var(--panel);
  border-left: 1px solid #2a3a4a;
  display: flex; flex-direction: column; min-height: 0;
  overflow: hidden;
}
#guide {
  padding: 12px;
  background: #243040;
  border-bottom: 1px solid #2a3a4a;
  min-height: 120px;
}
#guide h2 { margin: 0 0 6px; font-size: 13px; color: var(--accent); }
#guide p { margin: 0 0 8px; font-size: 13px; line-height: 1.45; color: #dde5dd; }
#guide .guide-actions { display: flex; gap: 6px; flex-wrap: wrap; }
#card-panel {
  flex: 1; overflow: auto; padding: 10px;
}
#card-panel h2 { margin: 0 0 8px; font-size: 13px; color: var(--muted); }
.card-line {
  padding: 8px; margin-bottom: 6px;
  background: #222c3a; border-radius: 6px;
  border-left: 3px solid #3a5568;
  font-size: 12px;
}
.card-line.hint {
  border-left-color: var(--accent);
  background: #2a3420;
}
.card-line.win {
  border-left-color: var(--ok);
  background: #1a3020;
}
.card-line .name { font-weight: 600; margin-bottom: 2px; }
.card-line .pat { color: var(--muted); font-size: 11px; }
.card-line .pts { float: right; color: var(--accent); }

#log {
  max-height: 120px; overflow: auto;
  padding: 8px 10px;
  font-size: 11px; color: var(--muted);
  border-top: 1px solid #2a3a4a;
  background: #121820;
}
#log div { margin-bottom: 2px; }

#bubble {
  position: absolute;
  left: 50%; top: 40%; transform: translate(-50%, -50%);
  background: #1a2332ee; border: 1px solid var(--accent);
  border-radius: 10px; padding: 16px 20px; max-width: 420px;
  z-index: 20; box-shadow: 0 8px 32px rgba(0,0,0,0.5);
  display: none;
}
#bubble.show { display: block; }
#bubble h3 { margin: 0 0 8px; font-size: 15px; color: var(--accent); }
#bubble p { margin: 0 0 12px; font-size: 13px; line-height: 1.5; }
#overlay-end {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.55);
  display: none; align-items: center; justify-content: center;
  z-index: 30;
}
#overlay-end.show { display: flex; }
#overlay-end .box {
  background: var(--panel); padding: 24px 32px; border-radius: 12px;
  text-align: center; max-width: 400px;
}
#overlay-end h2 { margin: 0 0 8px; }
.toggle {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--muted);
}
.hint-bar {
  font-size: 12px; color: var(--accent); text-align: center; min-height: 18px;
}
#status-hint {
  position: relative;
  z-index: 1;
  max-width: 100%;
  padding: 4px 10px;
  line-height: 1.35;
  white-space: normal;
  overflow-wrap: anywhere;
  text-shadow: 0 1px 2px rgba(0,0,0,0.85);
  background: rgba(9, 72, 47, 0.72);
  border-radius: 6px;
}

/* Mobile tab bar: hidden on desktop, shown in ≤900px media below */
#mobile-tabs { display: none; }

/* —— Mobile / narrow (portrait phones + small tablets) —— */
@media (max-width: 900px) {
  :root {
    --tile-w: 44px;
    --tile-h: 58px;
  }

  html, body {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
  }

  #app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto;
    height: 100dvh;
    max-height: 100dvh;
  }

  #topbar {
    padding: 6px 10px;
    gap: 8px;
    flex-wrap: wrap;
  }
  #topbar h1 { font-size: 14px; }
  #topbar .spacer { display: none; }
  #phase-label {
    order: 10;
    width: 100%;
    text-align: left;
    font-size: 11px;
    min-height: 14px;
  }
  #topbar button,
  #topbar .toggle {
    min-height: 36px;
    font-size: 12px;
    padding: 6px 10px;
  }

  #table-wrap {
    min-height: 0;
  }

  #table {
    inset: 4px;
    grid-template-rows: 56px minmax(0, 1fr) minmax(150px, 32%);
    grid-template-columns: 56px 1fr 56px;
    gap: 4px;
  }

  #seat-north { gap: 2px; }
  #seat-west, #seat-east {
    gap: 2px;
  }
  .seat-label { font-size: 10px; }

  /* Opponent backs: one compact row (north) / clipped column (sides)
     so a wrap leftover never sits on #status-hint (Courtesy bug). */
  #seat-north .hand-row {
    flex-wrap: nowrap;
    max-width: 100%;
    overflow: hidden;
  }
  #seat-north .tile.tiny {
    width: 16px;
    height: 22px;
    flex-shrink: 0;
  }
  #seat-west .hand-row,
  #seat-east .hand-row {
    flex-direction: column;
    flex-wrap: nowrap;
    max-height: 100%;
    overflow: hidden;
  }
  #seat-west .tile.tiny,
  #seat-east .tile.tiny {
    width: 16px;
    height: 20px;
    flex-shrink: 0;
  }

  #center { gap: 4px; }
  #status-hint {
    font-size: 12px;
    padding: 6px 8px;
    margin: 0 4px;
  }
  #discards {
    max-width: 100%;
    max-height: 72px;
    padding: 4px;
    gap: 2px;
  }
  #wall-info { font-size: 11px; }
  #actions {
    gap: 8px;
    padding: 2px 0 4px;
  }
  #actions button {
    min-height: 44px;
    min-width: 64px;
    padding: 8px 14px;
    font-size: 14px;
  }

  #seat-south {
    justify-content: flex-end;
    gap: 4px;
  }
  #player-hand {
    gap: 3px;
    padding: 4px 2px 8px;
    /* wrap to 2 rows if needed; keep tap size */
  }
  .tile.clickable {
    /* enlarge hit area slightly without shrinking visual too much */
    touch-action: manipulation;
  }
  .tile.clickable:hover { transform: none; filter: none; }
  .tile.selected { transform: translateY(-6px); }
  .tile.tiny { width: 22px; height: 28px; font-size: 9px; }

  /* Side → bottom sheet with tabs */
  #side {
    border-left: none;
    border-top: 1px solid #2a3a4a;
    max-height: 38dvh;
    display: flex;
    flex-direction: column;
  }

  #mobile-tabs {
    display: flex;
    flex-shrink: 0;
    background: #121820;
    border-bottom: 1px solid #2a3a4a;
  }
  #mobile-tabs button {
    flex: 1;
    border: none;
    border-radius: 0;
    border-bottom: 2px solid transparent;
    background: transparent;
    padding: 10px 8px;
    min-height: 44px;
    font-size: 13px;
    color: var(--muted);
  }
  #mobile-tabs button.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
    background: #1a2332;
  }

  #guide {
    min-height: 0;
    flex: 1;
    overflow: auto;
    padding: 10px;
  }
  #guide h2 { font-size: 12px; }
  #guide p { font-size: 12px; }
  #guide .guide-actions button {
    min-height: 40px;
  }

  #card-panel {
    flex: 1;
    overflow: auto;
    padding: 8px 10px;
  }
  #log {
    max-height: none;
    flex: 1;
    overflow: auto;
  }

  /* tab panels: hide inactive on mobile */
  #side[data-tab="guide"] #card-panel,
  #side[data-tab="guide"] #log { display: none; }
  #side[data-tab="card"] #guide,
  #side[data-tab="card"] #log { display: none; }
  #side[data-tab="log"] #guide,
  #side[data-tab="log"] #card-panel { display: none; }

  #bubble {
    left: 50%;
    top: auto;
    bottom: 42%;
    transform: translateX(-50%);
    max-width: calc(100% - 24px);
    width: min(420px, calc(100% - 24px));
    padding: 14px 16px;
  }
  #bubble button {
    min-height: 44px;
    width: 100%;
  }

  #overlay-end .box {
    margin: 16px;
    max-width: calc(100% - 32px);
    padding: 20px;
  }
  #overlay-end button {
    min-height: 44px;
    width: 100%;
  }
}

/* —— Landscape phone: keep playable, squeeze vertical —— */
@media (max-width: 900px) and (orientation: landscape) {
  :root {
    --tile-w: 36px;
    --tile-h: 48px;
  }

  #app {
    grid-template-columns: 1fr minmax(200px, 34vw);
    grid-template-rows: auto 1fr;
  }

  #topbar {
    grid-column: 1 / -1;
  }
  #phase-label {
    width: auto;
    order: 0;
  }
  #topbar .spacer { display: block; flex: 1; }

  #table-wrap {
    grid-column: 1;
  }
  #side {
    grid-column: 2;
    grid-row: 2;
    max-height: none;
    border-top: none;
    border-left: 1px solid #2a3a4a;
  }

  #table {
    grid-template-rows: 48px 1fr minmax(100px, 38%);
    grid-template-columns: 48px 1fr 48px;
  }

  #actions button {
    min-height: 40px;
    padding: 6px 10px;
    font-size: 12px;
  }

  #bubble {
    bottom: auto;
    top: 50%;
    transform: translate(-50%, -50%);
  }
}

/* very narrow portrait */
@media (max-width: 380px) {
  :root {
    --tile-w: 40px;
    --tile-h: 52px;
  }
  #table {
    grid-template-columns: 40px 1fr 40px;
  }
}
