:root {
  --bg: #0f3d2e;
  --panel: #ffffff;
  --ink: #16211c;
  --muted: #5c6b63;
  --accent: #d4a017;
  --accent-dark: #a87d0d;
  --danger: #b3261e;
  --card-red: #b3261e;
  --card-black: #1a1a1a;
  --radius: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at top, #145c41, var(--bg));
  color: var(--ink);
  font-size: 15px;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.65rem 0.85rem;
  color: #fff;
}

header h1 { margin: 0; font-size: 1.05rem; }

.status {
  font-size: 0.7rem;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  background: rgba(255,255,255,0.15);
}
.status--online { background: #2e7d32; }
.status--offline { background: #6d6d6d; }
.status--reconnecting { background: #a87d0d; }

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 0.6rem 2rem;
}

.panel {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 0.9rem;
  box-shadow: 0 10px 30px rgba(0,0,0,0.25);
}

.hidden { display: none !important; }

label {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 0.7rem;
}

/* Inputs stay >=16px so iOS Safari doesn't zoom in on focus. */
input {
  display: block;
  width: 100%;
  margin-top: 0.3rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid #d7dbd8;
  border-radius: 8px;
  font-size: 1rem;
}

button {
  cursor: pointer;
  border: none;
  border-radius: 8px;
  padding: 0.45rem 0.75rem;
  font-size: 0.82rem;
  font-weight: 600;
  background: var(--accent);
  color: #241a00;
  transition: background 0.15s ease;
}
button:hover { background: var(--accent-dark); }
button:disabled { background: #ccc; color: #777; cursor: not-allowed; }

.lobby-actions { display: flex; flex-direction: column; gap: 0.85rem; margin-top: 0.85rem; }
.join-row { display: flex; gap: 0.5rem; order: 2; }
.join-row input { margin-top: 0; }
#createRoomBtn { order: 1; }
#switchModeBtn { order: 3; }

.link-btn {
  background: none;
  color: var(--muted);
  text-decoration: underline;
  font-weight: 500;
  padding: 0.2rem 0;
  align-self: center;
}
.link-btn:hover { background: none; color: var(--ink); }

/* Arriving via an invite link/QR code: joining is the obvious action, not creating. */
.lobby-actions.joining .join-row { order: 1; }
.lobby-actions.joining .join-row button {
  flex: 1;
  padding: 0.55rem 0.9rem;
  font-size: 0.9rem;
}
.lobby-actions.joining #createRoomBtn { order: 2; display: none; }
.lobby-actions.joining #switchModeBtn { order: 3; }

.error { color: var(--danger); font-size: 0.78rem; min-height: 1.2em; }

.room-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.75rem; gap: 0.5rem; }
.room-header h2 { font-size: 1.1rem; margin: 0; }
.room-header-actions { display: flex; gap: 0.4rem; flex-shrink: 0; }
.room-header-actions button { white-space: nowrap; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 30, 22, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 100;
}
.modal {
  position: relative;
  background: #fff;
  border-radius: var(--radius);
  padding: 1.5rem;
  max-width: 20rem;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.35);
}
.modal h3 { margin: 0 0 1rem; font-size: 1rem; }
.modal-close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  width: 1.8rem;
  height: 1.8rem;
  padding: 0;
  line-height: 1;
  font-size: 1.1rem;
  background: #eef1ef;
  color: var(--ink);
  border-radius: 999px;
}
.modal-close:hover { background: #e0e3e0; }
.qr-code-container { display: flex; justify-content: center; margin-bottom: 0.9rem; }
.qr-code-container svg { width: 100%; height: auto; max-width: 14rem; }
.qr-link { font-size: 0.72rem; color: var(--muted); word-break: break-all; margin: 0; }
#roomCodeDisplay { letter-spacing: 0.12em; color: var(--accent-dark); }

.player-list {
  list-style: none;
  margin: 0 0 0.9rem;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

/* Each row is an independent grid, but they all share this exact column template, so
   fixed-width columns (score/badges/action) line up across rows regardless of what any
   single row's name or badge content looks like - "auto" widths would NOT line up here,
   since each row's grid is sized independently of its siblings. */
.player-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 1.8rem 9rem 6.5rem;
  align-items: center;
  column-gap: 0.5rem;
  padding: 0.4rem 0.6rem;
  background: #f4f6f4;
  border-radius: 8px;
  font-size: 0.85rem;
}

.player-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-score {
  font-size: 0.78rem;
  color: var(--accent-dark);
  font-weight: 700;
  text-align: right;
}

.player-badges {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  flex-wrap: wrap;
}

.player-action {
  justify-self: end;
  white-space: nowrap;
}
.badge {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.12rem 0.4rem;
  border-radius: 999px;
  background: #e5ded0;
  color: #6b5a1f;
  margin-left: 0.4rem;
}
.badge--dealer { background: var(--accent); color: #241a00; }
.badge--you { background: #dfeee5; color: #1f5e3f; }
.badge--disconnected { background: #f3d9d7; color: var(--danger); }

/* Collapsible sections (setup, chat) - native <details>, styled to be compact by default. */
.collapsible {
  margin-bottom: 0.9rem;
}
.collapsible summary {
  cursor: pointer;
  font-weight: 700;
  font-size: 0.82rem;
  padding: 0.5rem 0.6rem;
  background: #eef1ef;
  border-radius: 8px;
  list-style: none;
  user-select: none;
}
.collapsible summary::-webkit-details-marker { display: none; }
.collapsible summary::after {
  content: "▾";
  float: right;
  color: var(--muted);
  transition: transform 0.15s ease;
}
.collapsible[open] summary::after { transform: rotate(180deg); }
.collapsible[open] summary { border-radius: 8px 8px 0 0; margin-bottom: 0; }

.deal-bar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-bottom: 0.6rem;
}

.btn-danger-outline {
  background: none;
  color: var(--danger);
  border: 1px solid #e0b7b4;
}
.btn-danger-outline:hover { background: #fbeceb; }
.btn-danger-outline--armed { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger-outline--armed:hover { background: #93221c; }

.btn-outline {
  background: none;
  color: var(--muted);
  border: 1px solid #cfd5d1;
}
.btn-outline:hover { background: #eef1ef; }

.dealer-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  padding: 0.75rem;
  background: #f4f6f4;
  border-radius: 0 0 8px 8px;
}
.dealer-controls label { margin: 0; width: 11rem; font-size: 0.72rem; }
.deck-info { color: var(--muted); font-size: 0.78rem; }

.hand {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  min-height: 4.2rem;
  padding: 0.6rem;
  background: #f4f6f4;
  border-radius: 8px;
  margin-bottom: 1rem;
}

.revealed-area {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.revealed-block {
  padding: 0.6rem;
  background: #f4f6f4;
  border-radius: 8px;
  border: 2px solid transparent;
}
.revealed-block--winner {
  background: #fbf3da;
  border-color: var(--accent);
}

.revealed-label {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 0.4rem;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.revealed-cards-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.winner-badge {
  font-size: 0.72rem;
  font-weight: 700;
  color: #7a5b00;
  background: var(--accent);
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
}

.card {
  width: 2.5rem;
  height: 3.5rem;
  border-radius: 6px;
  background: #fff;
  border: 1px solid #d7dbd8;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
  user-select: none;
  box-shadow: 0 2px 4px rgba(0,0,0,0.12);
  transition: transform 0.1s ease;
}
.card:hover { transform: translateY(-2px); }
.card.red { color: var(--card-red); }
.card.black { color: var(--card-black); }
.card.revealed { cursor: default; }
.card.revealed:hover { transform: none; }
.card.back {
  background: repeating-linear-gradient(45deg, #1a4f8a, #1a4f8a 4px, #234f7d 4px, #234f7d 8px);
  color: transparent;
  cursor: default;
}

.card.dragging { opacity: 0.4; }
.card.discardable { box-shadow: 0 0 0 2px var(--accent) inset; }
.card.knock-armed { box-shadow: 0 0 0 2px var(--danger) inset; }

.hand-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.hand-header h3 { font-size: 0.9rem; margin: 0.4rem 0; }
.hand-sort { display: flex; gap: 0.35rem; }
.hand-sort button, .gin-actions button {
  font-size: 0.72rem;
  padding: 0.35rem 0.6rem;
  background: #e5ded0;
  color: #4a3f1f;
}
.hand-sort button:hover, .gin-actions button:hover { background: #d8cdb2; }

.gin-table {
  padding: 0.75rem;
  background: #eef4f0;
  border-radius: 8px;
  margin-bottom: 0.9rem;
}

.gin-status { margin-bottom: 0.6rem; }
.opponent-status {
  margin-top: 0.4rem;
  font-size: 0.78rem;
  font-style: italic;
  color: var(--muted);
  min-height: 1.1em;
}
.opponent-status--alert {
  color: var(--danger);
  font-weight: 700;
  font-style: normal;
}
.turn-indicator {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  background: #dfeee5;
  color: #1f5e3f;
}
.turn-indicator.turn-indicator--waiting { background: #f0e6d2; color: #6b5a1f; }
.turn-indicator.turn-indicator--over { background: #f3d9d7; color: var(--danger); }

.gin-piles {
  display: flex;
  gap: 1.25rem;
  margin-bottom: 0.75rem;
}
.pile { text-align: center; }
.pile-label { font-size: 0.68rem; color: var(--muted); margin-bottom: 0.3rem; }

.card-slot {
  width: 2.5rem;
  height: 3.5rem;
  border-radius: 6px;
  border: 1px dashed #c3c9c5;
}

#stockPile.disabled { cursor: default; opacity: 0.5; }

.gin-actions { display: flex; gap: 0.5rem; }
#knockBtn { background: #f3d9d7; color: var(--danger); }
#knockBtn:hover { background: #eac4c1; }

.hint { font-size: 0.74rem; color: var(--muted); margin: 0.4rem 0 0; }

.score-entry {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid #dde3df;
}
.score-entry-rows {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 0.6rem;
}
.score-entry-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
}
.score-entry-row span { font-size: 0.82rem; font-weight: 600; }
.score-entry-actions { display: flex; gap: 0.5rem; }
.score-entry-row input {
  width: 5rem;
  margin-top: 0;
  text-align: right;
}

h3 { font-size: 0.9rem; }

#revealedSection h3 { margin: 0.4rem 0; }

.chat summary { display: flex; justify-content: space-between; align-items: center; }
.chat-log {
  height: 7rem;
  overflow-y: auto;
  background: #fff;
  border: 1px solid #e0e3e0;
  border-top: none;
  border-radius: 0;
  padding: 0.45rem 0.65rem;
  font-size: 0.78rem;
}
.chat-input-row {
  display: flex;
  gap: 0.4rem;
  background: #f4f6f4;
  border: 1px solid #e0e3e0;
  border-top: none;
  border-radius: 0 0 8px 8px;
  padding: 0.5rem;
}
.chat-input-row input { margin-top: 0; }
.chat-message { margin-bottom: 0.25rem; word-break: break-word; }
.chat-message .chat-name { font-weight: 700; margin-right: 0.3rem; }
.chat-message.chat-message--me .chat-name { color: var(--accent-dark); }

/* Scale back up once there's room to spare. */
@media (min-width: 640px) {
  body { font-size: 16px; }
  header { padding: 1rem 1.5rem; }
  header h1 { font-size: 1.4rem; }
  main { padding: 0 1rem 3rem; }
  .panel { padding: 1.5rem; }
  .room-header h2 { font-size: 1.5rem; }
  button { font-size: 0.9rem; padding: 0.55rem 0.9rem; }
  .card, .card-slot { width: 3.4rem; height: 4.8rem; font-size: 1.1rem; }
  .hand-sort button, .gin-actions button { font-size: 0.8rem; padding: 0.4rem 0.7rem; }
  .chat-log { height: 10rem; font-size: 0.85rem; }
}

.chat-toasts {
  position: fixed;
  left: 50%;
  bottom: 1rem;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column-reverse;
  gap: 0.4rem;
  z-index: 200;
  width: calc(100% - 2rem);
  max-width: 22rem;
  pointer-events: none;
}

.chat-toast {
  background: #1f2a24;
  color: #fff;
  padding: 0.6rem 0.85rem;
  border-radius: 10px;
  font-size: 0.82rem;
  box-shadow: 0 10px 25px rgba(0,0,0,0.35);
  animation: chat-toast-in 0.2s ease-out;
  word-break: break-word;
}
.chat-toast.chat-toast--leaving {
  animation: chat-toast-out 0.25s ease-in forwards;
}
.chat-toast .chat-name {
  font-weight: 700;
  color: var(--accent);
  margin-right: 0.35rem;
}

@keyframes chat-toast-in {
  from { opacity: 0; transform: translateY(0.75rem); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes chat-toast-out {
  from { opacity: 1; transform: translateY(0); }
  to { opacity: 0; transform: translateY(0.4rem); }
}
