:root {
  --c-black: #211f1c;
  --c-blue: #1a56db;
  --c-red: #c1272d;
  --c-orange: #f08c1a;

  --bg: #0e5c3f;
  --panel: #ffffff;
  --ink: #211f1c;
  --muted: #8a8478;
  --tile-face: #f3ecd8;
  --tile-face-edge: #d8cca8;
  --accent: #1e88e5;
  --accent-2: #43a047;
  --danger: #e53935;
  --cell-border: #d5cfc0;
  --gold: #e8b93a;

  --tile-w: 40px;
  --tile-h: 52px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.hidden { display: none !important; }

/* --- Join / Waiting / End screens --- */

#screen-join, #screen-waiting {
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.join-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border-radius: 20px;
  padding: 32px 24px;
  text-align: center;
  box-shadow: 0 8px 30px rgba(0,0,0,0.25);
}

.join-card h1 {
  margin: 0 0 4px;
  font-size: 2rem;
  letter-spacing: 0.02em;
}

.subtitle {
  color: var(--muted);
  margin: 0 0 24px;
  font-size: 0.95rem;
}

.field-label {
  display: block;
  text-align: left;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  margin: 0 0 6px;
}

.or-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 18px 0 14px;
  color: var(--muted);
  font-size: 0.8rem;
}
.or-divider::before, .or-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--cell-border);
}

#name-input, #table-code-input {
  width: 100%;
  padding: 14px 16px;
  font-size: 1rem;
  border: 2px solid var(--cell-border);
  border-radius: 12px;
  margin-bottom: 12px;
  outline: none;
}
#name-input:focus, #table-code-input:focus { border-color: var(--accent); }
#table-code-input { text-transform: uppercase; }

button {
  font-family: inherit;
  font-size: 1rem;
  border: none;
  border-radius: 12px;
  padding: 14px 18px;
  cursor: pointer;
}
button:disabled { opacity: 0.5; cursor: default; }

#create-btn, #join-btn {
  display: block;
  width: 100%;
}

.primary {
  background: var(--c-black);
  color: #fff;
  font-weight: 600;
}
.primary:active { opacity: 0.85; }
.primary:disabled { background: #c9c3b4; cursor: default; }

.status { min-height: 1.2em; color: var(--danger); font-size: 0.9rem; margin-top: 12px; }

.contact-link {
  margin-top: 20px;
  color: var(--muted);
  font-size: 0.8rem;
  text-decoration: underline;
}

.seat-list { list-style: none; padding: 0; margin: 0; text-align: left; }
.seat-list li {
  padding: 10px 14px;
  border-radius: 10px;
  background: #f0ece0;
  margin-bottom: 8px;
  font-weight: 500;
}
.seat-list li.empty { color: var(--muted); font-style: italic; font-weight: normal; }

.table-code-box {
  margin: 16px 0 0;
  font-size: 0.95rem;
}
.table-code-box strong {
  font-size: 1.2rem;
  letter-spacing: 0.08em;
}
.invite-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 10px;
}
.secondary.invite-btn {
  padding: 8px 14px;
  font-size: 0.85rem;
  width: auto;
  flex: none;
  background: #fbe3c7;
  color: #7a4a12;
  border: 1px solid #f0c98a;
}

.change-name-link {
  display: inline-block;
  margin-top: 16px;
  color: var(--muted);
  font-size: 0.85rem;
  text-decoration: underline;
}
.change-name-link.small {
  display: block;
  text-align: center;
  margin: 6px 0;
  font-size: 0.75rem;
  color: rgba(255,255,255,0.75);
}

.game-table-code {
  text-align: center;
  margin: 0;
  font-size: 0.7rem;
  color: rgba(255,255,255,0.5);
}
.game-table-code strong {
  letter-spacing: 0.06em;
  color: rgba(255,255,255,0.75);
}

#end-scores { white-space: pre-line; font-size: 1.1rem; color: var(--ink); }

/* --- Game screen --- */

#screen-game {
  position: relative;
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  gap: 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--cell-border);
}

.player-badge {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 6px 8px;
  border-radius: 12px;
  transition: background 0.2s;
}
.player-badge.active {
  background: rgba(30, 136, 229, 0.12);
  box-shadow: inset 0 0 0 2px var(--accent);
}
.player-badge.offline .p-name {
  color: var(--danger);
}
.p-name { font-size: 0.85rem; font-weight: 600; }
.p-score { font-size: 1.4rem; font-weight: 700; }
.p-hand-count { font-size: 0.7rem; color: var(--muted); }

.bag-badge {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 64px;
  color: var(--muted);
}
.bag-count { font-size: 1.2rem; font-weight: 700; color: var(--ink); }
.bag-label { font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.05em; }

.turn-banner {
  text-align: center;
  margin: 8px 0 4px;
  font-weight: 600;
  min-height: 1.2em;
  color: #fff;
}
.turn-banner.mine { color: var(--gold); }
.turn-banner.theirs { color: rgba(255,255,255,0.7); }

#table-wrap {
  flex: 1;
  overflow: auto;
  min-height: 0;
  background: var(--bg);
  background-image: radial-gradient(rgba(255,255,255,0.04) 1px, transparent 1px);
  background-size: 14px 14px;
  touch-action: pan-x pan-y;
  padding: 14px 10px 24px;
}

#table-board {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: max-content;
  min-width: 100%;
  transition: transform 0.25s ease;
}

.set-row {
  display: flex;
  align-items: center;
  padding: 5px 6px;
  border-radius: 10px;
  min-height: calc(var(--tile-h) + 10px);
  background: rgba(255,255,255,0.05);
}
.set-row.incomplete {
  box-shadow: inset 0 0 0 2px rgba(232, 185, 58, 0.6);
}
.set-row.valid {
  box-shadow: inset 0 0 0 2px rgba(67, 160, 71, 0.6);
}
.set-row.invalid {
  box-shadow: inset 0 0 0 2px var(--danger);
}
.set-row.locked-row {
  opacity: 0.55;
}
.set-row.new-row-slot {
  position: relative;
  background: rgba(255,255,255,0.03);
  border: 1px dashed rgba(255,255,255,0.3);
  cursor: default;
  min-height: calc(var(--tile-h) + 10px);
}
.set-row.new-row-slot.active {
  cursor: pointer;
  border-color: var(--accent);
  background: rgba(30, 136, 229, 0.12);
}
.new-row-hint {
  color: rgba(255,255,255,0.5);
  font-size: 0.8rem;
  margin-left: 10px;
  pointer-events: none;
}
.set-row.new-row-slot.active .new-row-hint {
  color: #fff;
  font-weight: 600;
}

.gap {
  width: 14px;
  align-self: stretch;
  border-radius: 4px;
  flex: none;
}
/* Antippbare Lücken sind bewusst deutlich breiter als ihre Trennlinie
   optisch wirken lässt — auf dem Handy braucht ein Finger mehr Fläche,
   als eine feine gestrichelte Linie zeigt. */
.gap.active {
  width: 36px;
  background: rgba(30, 136, 229, 0.25);
  border-left: 2px dashed var(--accent);
  border-right: 2px dashed var(--accent);
  cursor: pointer;
}
.gap.active:active {
  background: rgba(30, 136, 229, 0.45);
}
.gap.locked {
  width: 20px;
  border-left: 2px dashed rgba(229, 72, 77, 0.5);
  border-right: 2px dashed rgba(229, 72, 77, 0.5);
  cursor: not-allowed;
}
.gap.splittable {
  width: 26px;
  border-left: 1px dashed rgba(255,255,255,0.4);
  border-right: 1px dashed rgba(255,255,255,0.4);
  cursor: pointer;
}
.gap.splittable:active {
  width: 32px;
  background: rgba(232, 185, 58, 0.3);
  border-left: 2px dashed var(--gold);
  border-right: 2px dashed var(--gold);
}

.tile {
  width: var(--tile-w);
  height: var(--tile-h);
  flex: none;
  border-radius: 6px;
  background: var(--tile-face);
  border: 1px solid var(--tile-face-edge);
  box-shadow: 0 2px 3px rgba(0,0,0,0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  font-weight: 700;
  user-select: none;
}
.tile.c-black { color: var(--c-black); }
.tile.c-blue { color: var(--c-blue); }
.tile.c-red { color: var(--c-red); }
.tile.c-orange { color: var(--c-orange); }
.tile.joker { color: var(--gold); font-size: 1.3rem; }

.tile.liftable { cursor: pointer; }
.tile.liftable:active { transform: translateY(-2px); }
.tile.blocked-by-selection { cursor: not-allowed; opacity: 0.7; }
.tile.placed-now {
  box-shadow: 0 0 0 2px var(--accent-2), 0 2px 3px rgba(0,0,0,0.3);
}

.msg-line {
  text-align: center;
  min-height: 1.4em;
  color: var(--danger);
  font-size: 0.85rem;
  margin: 2px 0;
  padding: 0 12px;
}

#hand-area {
  background: var(--panel);
  border-top: 1px solid var(--cell-border);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
}

.hand {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  padding: 4px 0 10px;
  min-height: calc(var(--tile-h) + 8px);
  align-items: center;
}

.hand .tile {
  cursor: pointer;
  border: 3px solid transparent;
  transition: transform 0.1s;
  background: var(--tile-face);
}
.hand .tile.selected {
  border-color: var(--accent);
  transform: translateY(-4px);
}
.hand .tile.new-tile {
  box-shadow: 0 0 0 3px rgba(232, 185, 58, 0.5), 0 2px 3px rgba(0,0,0,0.3);
}

.held-badge {
  width: 100%;
  text-align: center;
  background: rgba(30, 136, 229, 0.1);
  color: var(--accent);
  border: 1px dashed var(--accent);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 0.85rem;
  font-weight: 600;
}

.controls {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.secondary {
  background: #f0ece0;
  color: var(--ink);
  font-weight: 600;
  flex: 1;
}
.secondary:active { opacity: 0.8; }
#confirm-btn { flex: 2; }

/* --- Rundenende: Overlay statt eigenem Bildschirm, damit das Brett mit dem
   Endergebnis sichtbar stehen bleibt, statt abrupt zu verschwinden. --- */
#end-overlay {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(14, 33, 25, 0.78);
  backdrop-filter: blur(1px);
}
.end-panel {
  width: 100%;
  max-width: 340px;
  background: rgba(255, 255, 255, 0.97);
  border-radius: 20px;
  padding: 28px 24px;
  text-align: center;
  box-shadow: 0 10px 34px rgba(0,0,0,0.45);
}
.end-panel h2 {
  margin: 0 0 4px;
  font-size: 1.5rem;
}
.end-panel #end-scores {
  margin-bottom: 20px;
}
#rematch-btn { width: 100%; }
