:root {
  --felt: #15603e;
  --felt-dark: #0e4a30;
  --felt-light: #1d7a50;
  --ink: #1c2733;
  --cream: #f4ecd8;
  --gold: #e8b84b;
  --gold-dark: #c9952c;
  --rail: #2b3a47;
  --danger: #d9534f;
  --shadow: rgba(0, 0, 0, 0.35);

  /* pip value colors 0-12 */
  --v0: #6b7280;  --v1: #2563eb;  --v2: #16a34a;  --v3: #dc2626;
  --v4: #7c3aed;  --v5: #ea580c;  --v6: #0891b2;  --v7: #db2777;
  --v8: #4b5563;  --v9: #ca8a04;  --v10: #0d9488; --v11: #9333ea;
  --v12: #b91c1c;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  background:
    radial-gradient(circle at 50% 0%, var(--felt-light), var(--felt) 45%, var(--felt-dark) 100%);
  color: var(--cream);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 1180px;
  margin: 0 auto;
  padding: 14px 16px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 100vh;
}

/* ---------- Top bar ---------- */
#topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid rgba(232, 184, 75, 0.25);
  border-radius: 14px;
  padding: 10px 16px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand .loco { font-size: 34px; filter: drop-shadow(0 2px 3px var(--shadow)); }
.brand h1 { font-size: 22px; letter-spacing: 1px; color: var(--gold); }
.brand .sub { font-size: 11px; letter-spacing: 2px; opacity: 0.7; text-transform: uppercase; }
.meta { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.meta-item { display: flex; flex-direction: column; align-items: center; line-height: 1.1; }
.meta-item .label { font-size: 10px; opacity: 0.6; }
.meta-item .value { font-size: 20px; font-weight: 700; color: var(--gold); }

.ghost-btn {
  background: transparent;
  color: var(--cream);
  border: 1px solid rgba(244, 236, 216, 0.4);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}
.ghost-btn:hover { background: rgba(244, 236, 216, 0.12); border-color: var(--gold); }

.action-btn {
  background: linear-gradient(180deg, var(--gold), var(--gold-dark));
  color: #3a2a06;
  border: none;
  border-radius: 8px;
  padding: 9px 18px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 2px 6px var(--shadow);
  transition: transform 0.1s, filter 0.15s;
}
.action-btn:hover:not(:disabled) { filter: brightness(1.08); transform: translateY(-1px); }
.action-btn:disabled { opacity: 0.35; cursor: not-allowed; filter: grayscale(0.4); }

/* ---------- Opponents ---------- */
#opponents { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.opp-card {
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.opp-card.active {
  border-color: var(--gold);
  box-shadow: 0 0 0 2px rgba(232, 184, 75, 0.35), 0 0 18px rgba(232, 184, 75, 0.25);
}
.opp-avatar {
  width: 42px; height: 42px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; background: rgba(255,255,255,0.1); flex-shrink: 0;
}
.opp-info { flex: 1; min-width: 0; }
.opp-name { font-weight: 600; font-size: 14px; }
.opp-stats { font-size: 12px; opacity: 0.75; margin-top: 2px; }
.tile-badge {
  display: inline-flex; align-items: center; gap: 4px;
  background: rgba(0,0,0,0.3); border-radius: 20px; padding: 2px 9px;
  font-weight: 700; color: var(--gold); font-size: 13px;
}

/* ---------- Board ---------- */
#board {
  background: rgba(0, 0, 0, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 14px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.engine-hub { display: flex; justify-content: center; }
.engine-tile {
  position: relative;
  display: flex; flex-direction: column;
  border-radius: 10px; overflow: hidden;
  box-shadow: 0 4px 10px var(--shadow);
  border: 2px solid var(--gold);
}
.engine-label {
  text-align: center; font-size: 11px; letter-spacing: 1px;
  color: var(--gold); margin-bottom: 4px; opacity: 0.85;
}

#trains { display: flex; flex-direction: column; gap: 9px; }
.train-row {
  display: flex; align-items: center; gap: 10px;
  background: rgba(255,255,255,0.03);
  border: 1px solid transparent;
  border-radius: 10px; padding: 6px 10px;
  min-height: 48px;
  transition: background 0.15s, border-color 0.15s;
}
.train-row.playable {
  cursor: pointer;
  border-color: var(--gold);
  background: rgba(232, 184, 75, 0.12);
}
.train-row.playable:hover { background: rgba(232, 184, 75, 0.22); }
.train-row.active-owner { border-color: rgba(232,184,75,0.45); }
.train-label {
  flex-shrink: 0; width: 132px; font-size: 13px;
  display: flex; align-items: center; gap: 6px;
}
.train-label .who { font-weight: 600; }
.train-label .marker { font-size: 15px; }
.train-tiles {
  display: flex; align-items: center; gap: 2px;
  overflow-x: auto; flex: 1; padding-bottom: 3px;
  scrollbar-width: thin;
  scrollbar-color: rgba(232, 184, 75, 0.5) transparent;
}
/* thin, unobtrusive scrollbar only when a train overflows */
.train-tiles::-webkit-scrollbar { height: 5px; }
.train-tiles::-webkit-scrollbar-track { background: transparent; }
.train-tiles::-webkit-scrollbar-thumb {
  background: rgba(232, 184, 75, 0.45); border-radius: 3px;
}
.train-tiles::-webkit-scrollbar-thumb:hover { background: rgba(232, 184, 75, 0.7); }

/* placed tiles are display-only and compact, so a whole train fits */
.train-tiles .domino.horizontal .half { width: 21px; height: 32px; font-size: 15px; }
.train-end-chip {
  flex-shrink: 0; margin-left: 4px;
  font-size: 11px; padding: 3px 8px; border-radius: 20px;
  background: rgba(0,0,0,0.35); color: var(--gold); font-weight: 700;
}
.open-flag {
  font-size: 11px; color: #ffd; background: rgba(217,83,79,0.35);
  border-radius: 20px; padding: 2px 8px;
}

/* ---------- Domino tiles ---------- */
.domino {
  display: inline-flex;
  background: var(--cream);
  border-radius: 7px;
  box-shadow: 0 2px 5px var(--shadow);
  border: 1px solid rgba(0,0,0,0.25);
  overflow: hidden;
  flex-shrink: 0;
}
.domino.vertical { flex-direction: column; }
.domino .half {
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; color: #fff;
  position: relative;
}
.domino.horizontal .half { width: 33px; height: 50px; font-size: 24px; }
.domino.horizontal .half:first-child { border-right: 2px solid rgba(0,0,0,0.25); }
.domino.vertical .half { width: 50px; height: 33px; font-size: 24px; }
.domino.vertical .half:first-child { border-bottom: 2px solid rgba(0,0,0,0.25); }

/* engine tile bigger */
.engine-tile .domino.vertical .half { width: 66px; height: 40px; font-size: 30px; }

/* number tile readability */
.domino .half { text-shadow: 0 1px 1px rgba(0, 0, 0, 0.35); }

/* play / deal animations */
@keyframes pop {
  0% { transform: scale(0.3); opacity: 0; }
  60% { transform: scale(1.14); }
  100% { transform: scale(1); opacity: 1; }
}
.domino.just-played { animation: pop 0.4s cubic-bezier(0.2, 0.8, 0.3, 1.3); position: relative; z-index: 2; }
@keyframes dealIn {
  from { transform: translateY(46px) rotate(-7deg) scale(0.6); opacity: 0; }
  to { transform: none; opacity: 1; }
}
.hand .domino.deal { animation: dealIn 0.45s ease both; }

/* hover hint on trains */
.train-row.hint-target {
  border-color: rgba(232, 184, 75, 0.7);
  box-shadow: inset 0 0 0 1px rgba(232, 184, 75, 0.45);
  background: rgba(232, 184, 75, 0.07);
}
.train-row.playable .train-end-chip,
.train-row.hint-target .train-end-chip {
  box-shadow: 0 0 10px rgba(232, 184, 75, 0.65);
  background: rgba(232, 184, 75, 0.25);
}

/* AI thinking dots */
.thinking { display: inline-flex; gap: 4px; align-items: center; margin-top: 3px; }
.thinking i { width: 5px; height: 5px; border-radius: 50%; background: var(--gold); animation: tbounce 0.9s infinite; }
.thinking i:nth-child(2) { animation-delay: 0.15s; }
.thinking i:nth-child(3) { animation-delay: 0.3s; }
@keyframes tbounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-5px); opacity: 1; }
}

/* confetti */
#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 60; overflow: hidden; }
.confetti-piece { position: absolute; top: -12vh; width: 9px; height: 14px; border-radius: 2px; will-change: transform; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(118vh) rotate(900deg); } }

/* hand tiles */
.hand .domino { cursor: pointer; transition: transform 0.12s, box-shadow 0.12s; }
.hand .domino.playable { box-shadow: 0 0 0 2px var(--gold), 0 3px 8px var(--shadow); }
.hand .domino.selected { transform: translateY(-10px); box-shadow: 0 0 0 3px var(--gold), 0 6px 14px var(--shadow); }
.hand .domino.dim { opacity: 0.4; }
.hand .domino:hover:not(.dim) { transform: translateY(-5px); }

/* value colors */
.v0 { background: var(--v0); } .v1 { background: var(--v1); } .v2 { background: var(--v2); }
.v3 { background: var(--v3); } .v4 { background: var(--v4); } .v5 { background: var(--v5); }
.v6 { background: var(--v6); } .v7 { background: var(--v7); } .v8 { background: var(--v8); }
.v9 { background: var(--v9); } .v10 { background: var(--v10); } .v11 { background: var(--v11); }
.v12 { background: var(--v12); }
.half.blank { background: #e9e2cd !important; color: #9a8f70; }

/* ---------- Status ---------- */
#statusbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  background: rgba(0,0,0,0.18); border-radius: 10px; padding: 8px 14px;
  min-height: 40px;
}
.turn-indicator { font-weight: 700; color: var(--gold); font-size: 14px; }
.log { font-size: 13px; opacity: 0.85; flex: 1; }

/* ---------- Hand ---------- */
#handArea {
  background: rgba(0,0,0,0.2);
  border: 1px solid rgba(232,184,75,0.2);
  border-radius: 14px; padding: 12px 14px 16px;
}
.hand-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 12px; flex-wrap: wrap; gap: 8px;
}
.hand-header span { font-size: 14px; }
.hand-header em { color: var(--gold); font-style: normal; font-weight: 700; }
.controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.hint { font-size: 13px; color: var(--gold); min-width: 60px; }
.hand {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  min-height: 56px;
}

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 50;
  backdrop-filter: blur(3px);
}
.modal.hidden { display: none; }
.modal-card {
  background: linear-gradient(180deg, #1d3a2a, #14281d);
  border: 1px solid var(--gold); border-radius: 16px;
  padding: 26px 30px; max-width: 520px; width: 92%;
  box-shadow: 0 20px 50px rgba(0,0,0,0.5);
}
.modal-card h2 { color: var(--gold); margin-bottom: 14px; font-size: 22px; }
#modalBody { font-size: 14px; line-height: 1.7; margin-bottom: 20px; }
#modalBody table { width: 100%; border-collapse: collapse; margin-top: 8px; }
#modalBody th, #modalBody td { padding: 6px 10px; text-align: left; border-bottom: 1px solid rgba(255,255,255,0.1); font-size: 14px; }
#modalBody th { color: var(--gold); }
#modalBody .winner-row { color: var(--gold); font-weight: 700; }
#modalBody ul { margin-left: 18px; }
#modalBody li { margin-bottom: 6px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }
.hidden { display: none !important; }

@media (max-width: 720px) {
  #opponents { grid-template-columns: 1fr; }
  .train-label { width: 96px; font-size: 12px; }
  .brand h1 { font-size: 18px; }
  .domino.horizontal .half { width: 24px; height: 42px; font-size: 18px; }
  .domino.vertical .half { width: 42px; height: 24px; font-size: 18px; }
}
