:root {
  --bg: #f4ede1;
  --ink: #2c2418;
  --wood: #7a5230;
  --wood-dark: #5c3c22;
  --cream: #fbf6ea;
  --accent: #b5432a;
  --accent-dark: #8f3520;
  --tw: #d8624a;
  --dw: #e8a680;
  --tl: #6f9bb5;
  --dl: #a9c9d8;
  --star: #e8a680;
  --tile: #f1dcae;
  --tile-border: #c9a662;
  --header-h: 68px;
}

* { box-sizing: border-box; }

/* The UA rule for [hidden] loses to any author `display`, so state it here
   where it outranks the element and class rules below. */
[hidden] { display: none !important; }

/* Tuned for iPad: no tap flash, no double-tap zoom delay on controls. */
button, .cell, .rack-tile, select {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body {
  margin: 0;
  font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
}

.site-header {
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--header-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  background: rgba(251, 246, 234, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid #e6d9bd;
  box-shadow: 0 1px 6px rgba(59, 47, 22, 0.07);
}
.home-link {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px 4px 4px;
  margin: 0;
  border: none;
  background: none;
  cursor: pointer;
  border-radius: 10px;
  font-family: Georgia, "Times New Roman", Times, serif;
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--wood-dark);
}
.home-link:hover { background: rgba(122, 82, 48, 0.08); }
.home-link img { height: 52px; width: auto; display: block; }
.home-link em { font-style: italic; font-weight: 400; }

.screen {
  min-height: calc(100vh - var(--header-h));
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px 12px;
}
.card {
  background: var(--cream);
  border-radius: 12px;
  padding: 28px 32px;
  max-width: 720px;
  width: 100%;
  box-shadow: 0 4px 20px rgba(0,0,0,0.12);
}

h1 { margin-top: 0; color: var(--wood-dark); }

.brand { margin: 0 0 18px; text-align: center; }
.brand img { width: 100%; max-width: 560px; height: auto; }
h2 { color: var(--wood-dark); font-size: 1.1rem; margin-bottom: 6px; }
h3 { color: var(--wood-dark); font-size: 0.95rem; margin: 16px 0 6px; }

label {
  display: block;
  margin-bottom: 12px;
  font-size: 0.9rem;
  font-weight: 600;
}

input[type="text"], input[type="password"] {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid #c9b998;
  border-radius: 6px;
  font-size: 1rem;
  background: #fff;
}

.btn {
  cursor: pointer;
  border: none;
  border-radius: 8px;
  padding: 12px 18px;
  min-height: 44px;
  font-size: 1rem;
  font-weight: 600;
  font-family: inherit;
  background: #ddd0b0;
  color: var(--ink);
  margin: 4px 4px 4px 0;
}
.btn:hover { filter: brightness(0.95); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-dark); }
.btn.small { font-size: 0.9rem; padding: 10px 14px; min-height: 40px; }
.btn.danger { background: #7a1f1f; color: #fff; }

.name-picker {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin: 10px 0 16px;
}
.name-btn {
  flex: 1 1 auto;
  min-width: 120px;
  min-height: 52px;
  padding: 12px 20px;
  font-family: inherit;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--ink);
  background: #fff;
  border: 2px solid #e2d5b8;
  border-radius: 10px;
  cursor: pointer;
}
.name-btn:hover { border-color: var(--wood); }
.name-btn.selected { border-color: var(--accent); background: #fdf3e4; }

.mode-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin: 10px 0 18px;
}
@media (max-width: 640px) {
  .mode-cards { grid-template-columns: 1fr; }
}

.mode-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 18px 12px;
  background: #fff;
  border: 2px solid #e2d5b8;
  border-radius: 10px;
  cursor: pointer;
  font-family: inherit;
  color: var(--ink);
  text-align: center;
  transition: border-color 0.12s, transform 0.12s;
}
.mode-card:hover:not(:disabled) { border-color: var(--wood); transform: translateY(-2px); }
.mode-card.selected { border-color: var(--accent); background: #fdf3e4; }
.mode-card:disabled { opacity: 0.45; cursor: not-allowed; }
.mode-icon { font-size: 1.7rem; line-height: 1; }
.mode-title { font-weight: 700; font-size: 0.95rem; }
.mode-sub { font-size: 0.8rem; color: #8a7a52; }

.mode-options {
  background: #fff;
  border: 1px solid #e2d5b8;
  border-radius: 8px;
  padding: 16px 16px 10px;
  margin-bottom: 18px;
}
.mode-options select {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid #c9b998;
  border-radius: 6px;
  font-size: 1rem;
  background: #fff;
}
.mode-options .hint { margin: 8px 0 0; }

.join-section {
  border-top: 1px solid #e2d5b8;
  padding-top: 14px;
  margin-top: 4px;
}
.join-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.join-row input {
  flex: 1 1 140px;
  margin-top: 0;
  padding: 8px 10px;
  border: 1px solid #c9b998;
  border-radius: 6px;
  font-size: 1rem;
  background: #fff;
  width: auto;
}
.join-row .btn { margin: 0; flex: 0 0 auto; }

.room-list { list-style: none; padding: 0; margin: 0; }
.room-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #fff;
  border: 1px solid #e2d5b8;
  border-radius: 6px;
  padding: 8px 10px;
  margin-bottom: 6px;
  font-size: 0.9rem;
}
.room-list button { padding: 4px 10px; font-size: 0.8rem; }

.room-code {
  font-family: monospace;
  font-size: 1.4rem;
  letter-spacing: 3px;
  background: #fff;
  padding: 2px 10px;
  border-radius: 6px;
  border: 1px solid #e2d5b8;
}

.player-list { list-style: none; padding: 0; }
.player-list li {
  padding: 8px 10px;
  background: #fff;
  border: 1px solid #e2d5b8;
  border-radius: 6px;
  margin-bottom: 6px;
  display: flex;
  justify-content: space-between;
}
.player-list li.host::after { content: " (host)"; color: #8a7a52; font-weight: normal; }
.player-list li.disconnected { opacity: 0.5; }

.hint { color: #6b5c3e; font-size: 0.85rem; }

.waiting-status {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 16px 0 10px;
  font-size: 1rem;
  font-weight: 600;
  color: var(--wood-dark);
}
/* A live pulse while the room is still short of players. */
.waiting-status.is-waiting::before {
  content: "";
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  animation: waiting-pulse 1.4s ease-in-out infinite;
}
@keyframes waiting-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.8); }
}
@media (prefers-reduced-motion: reduce) {
  .waiting-status.is-waiting::before { animation: none; }
}

.bot-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin: 14px 0;
  font-size: 0.9rem;
  color: #6b5c3e;
}
.bot-controls select {
  padding: 7px 8px;
  border: 1px solid #c9b998;
  border-radius: 6px;
  background: #fff;
  font-size: 0.9rem;
}

/* Game layout */
.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 20px;
  width: 100%;
  max-width: 1200px;
}
@media (max-width: 980px) {
  .game-layout { grid-template-columns: 1fr; }
}

.turn-banner {
  background: var(--wood);
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  margin-bottom: 12px;
  font-weight: 600;
  text-align: center;
}
.turn-banner.your-turn { background: var(--accent); }

.board {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  grid-template-rows: repeat(15, 1fr);
  gap: 2px;
  background: var(--wood-dark);
  padding: 6px;
  border-radius: 8px;
  aspect-ratio: 1 / 1;
  width: 100%;
  /* Square, so capping width by leftover viewport height keeps the rack and
     controls on screen in iPad landscape without scrolling. */
  max-width: min(680px, calc(100vh - var(--header-h) - 300px));
  margin: 0 auto;
}

.cell {
  background: var(--cream);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.55rem;
  color: #7a6a4a;
  border-radius: 2px;
  position: relative;
  cursor: pointer;
  user-select: none;
  aspect-ratio: 1 / 1;
}
.cell.tw { background: var(--tw); color: #fff; }
.cell.dw { background: var(--dw); }
.cell.tl { background: var(--tl); color: #fff; }
.cell.dl { background: var(--dl); }
.cell.star { background: var(--star); }
.cell.center-star::before {
  content: "★";
  position: absolute;
  color: #fff;
  font-size: 1rem;
}

.cell .tile {
  position: absolute;
  inset: 1px;
  background: linear-gradient(180deg, #fdf6e4 0%, #f6e7c4 55%, #ecd8a8 100%);
  border: none;
  border-radius: 22%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: var(--ink);
  font-size: 0.85rem;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.9),
    inset 0 -1.5px 2px rgba(156, 122, 60, 0.3),
    0 1px 2px rgba(59, 47, 22, 0.32);
}
.cell .tile.pending {
  background: linear-gradient(180deg, #fffaf0 0%, #ffeeb8 55%, #f7dd93 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.95),
    inset 0 -1.5px 2px rgba(190, 145, 40, 0.3),
    0 0 0 2px rgba(181, 67, 42, 0.55),
    0 1px 3px rgba(59, 47, 22, 0.32);
  cursor: pointer;
}
.cell .tile .pts {
  position: absolute;
  bottom: 1px;
  right: 3px;
  font-size: 0.5rem;
  font-weight: 400;
}

.rack-area { margin-top: 16px; }
.rack {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  min-height: 66px;
}
.rack-tile {
  width: 58px;
  height: 58px;
  background: linear-gradient(180deg, #fdf6e4 0%, #f6e7c4 55%, #ecd8a8 100%);
  border: none;
  border-radius: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.6rem;
  color: var(--ink);
  cursor: pointer;
  position: relative;
  user-select: none;
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.95),
    inset 0 -3px 4px rgba(156, 122, 60, 0.28),
    0 1px 2px rgba(59, 47, 22, 0.28),
    0 5px 10px rgba(59, 47, 22, 0.2);
}
.rack-tile.selected {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  transform: translateY(-3px);
}
.rack-tile.exchange-selected { outline: 3px solid var(--tl); }
.rack-tile .pts {
  position: absolute;
  bottom: 4px;
  right: 6px;
  font-size: 0.7rem;
  font-weight: 400;
  opacity: 0.85;
}
.rack-tile.blank { color: #a08a4a; }

.rack-controls { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 10px; justify-content: center; }

.side-panel {
  background: var(--cream);
  border-radius: 10px;
  padding: 16px;
  max-height: calc(100vh - var(--header-h) - 48px);
  display: flex;
  flex-direction: column;
}

.scoreboard { list-style: none; padding: 0; margin: 0 0 8px; }
.scoreboard li {
  display: flex;
  justify-content: space-between;
  padding: 6px 8px;
  border-radius: 6px;
  margin-bottom: 4px;
  background: #fff;
}
.scoreboard li.active { background: #ffe9c7; font-weight: 700; }
.scoreboard li.disconnected { opacity: 0.5; }

.bag-count { font-size: 0.85rem; color: #6b5c3e; margin: 4px 0 12px; }

.history, .chat-log {
  background: #fff;
  border: 1px solid #e2d5b8;
  border-radius: 6px;
  padding: 8px;
  font-size: 0.8rem;
  max-height: 140px;
  overflow-y: auto;
  margin-bottom: 10px;
}
.chat-log .msg { margin-bottom: 4px; }
.chat-log .msg .from { font-weight: 700; }
.chat-log .msg.system { color: #6b5c3e; font-style: italic; }

.chat-form { display: flex; gap: 6px; }
.chat-form input { flex: 1; padding: 6px 8px; border: 1px solid #c9b998; border-radius: 6px; }

.toast {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: #2c2418;
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  z-index: 100;
  font-size: 0.9rem;
  box-shadow: 0 4px 14px rgba(0,0,0,0.3);
}
