:root {
  --felt: #1f5f3f;
  --felt-dark: #174a31;
  --panel: #fdf8ec;
  --ink: #222;
  --muted: #6b6b6b;
  --red: #c62828;
  --black: #222;
  --accent: #e0a526;
  --error: #b00020;
  --card-w: 64px;
  --card-h: 92px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, sans-serif;
  background: radial-gradient(ellipse at center, #2a7a50 0%, var(--felt) 70%);
  background-color: var(--felt);
  color: var(--ink);
  min-height: 100vh;
}

.hidden { display: none !important; }

.panel {
  background: var(--panel);
  border-radius: 10px;
  padding: 16px;
  box-shadow: 0 2px 8px rgb(0 0 0 / 30%);
}

/* Page: the bar on top, then the screen in use fills the rest. */
body { height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
#join, #lobby { overflow-y: auto; }

#bar {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 18px;
  padding: 8px 16px 4px;
  color: #fff;
}
#brand { display: flex; align-items: baseline; gap: 10px; }
#brand-title {
  font-size: 2.6rem;
  font-weight: 900;
  letter-spacing: 0.18em;
  line-height: 1;
  color: var(--accent);
  text-shadow: 0 3px 0 #7a4d24, 0 6px 14px rgb(0 0 0 / 45%);
}
#brand-by { font-style: italic; opacity: 0.85; }

#join, #lobby { width: min(420px, 100% - 32px); margin: 24px auto; }
#invite { margin: 12px 0; }
#invite p { margin: 6px 0; }
#invite button { margin-left: 6px; }
#room-link { color: inherit; font-weight: bold; overflow-wrap: anywhere; }
/* Opened from an invite link: the room is known, only the name is asked. */
#invited { display: none; }
#other-room { display: none; margin-left: 6px; }
#join-form.invited #invited { display: block; }
#join-form.invited #other-room { display: inline-block; }
#join-form.invited .switch, #join-form.invited .by-password { display: none; }
h1 { margin-top: 0; }
label { display: block; margin: 8px 0; }
input, select { font: inherit; padding: 6px; width: 100%; }
.hint { color: var(--muted); font-size: 0.9em; }

/* Host or join: two choices with a thumb that slides to the one picked. */
.switch {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin: 14px 0 6px;
  padding: 4px;
  border-radius: 999px;
  background: #e4dccb;
}
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch label {
  position: relative;
  z-index: 1;
  margin: 0;
  padding: 8px;
  border-radius: 999px;
  text-align: center;
  font-weight: 600;
  cursor: pointer;
}
.switch input:focus-visible + label { outline: 2px solid var(--ink); }
.switch .thumb {
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 4px;
  width: calc(50% - 4px);
  border-radius: 999px;
  background: var(--accent);
  transition: transform 0.2s ease;
}
#mode-join:checked ~ .thumb { transform: translateX(100%); }
@media (prefers-reduced-motion: reduce) { .switch .thumb { transition: none; } }

button {
  font: inherit;
  padding: 8px 14px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #222;
  cursor: pointer;
  margin-top: 8px;
}
button.secondary { background: #ddd; }
button.small { padding: 2px 8px; margin: 0; font-size: 0.85em; }
#lobby-players li { margin: 4px 0; }
button:disabled { opacity: 0.45; cursor: default; }

/* ----- game layout: the table takes all the room the rest leaves ----- */

#game {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 16px;
}

#topbar {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  align-items: center;
  color: #fff;
}
#topbar button { margin: 0; }
#timer {
  font-variant-numeric: tabular-nums;
  font-weight: bold;
  background: var(--felt-dark);
  padding: 2px 10px;
  border-radius: 6px;
}
#timer.low { background: var(--error); }

#players { display: flex; flex-wrap: wrap; gap: 8px; }
.player {
  background: var(--felt-dark);
  color: #fff;
  padding: 4px 10px;
  border-radius: 8px;
  border: 2px solid transparent;
  font-size: 0.9em;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.player.turn { border-color: var(--accent); box-shadow: 0 0 12px rgb(224 165 38 / 60%); }
.player.offline { opacity: 0.5; }
.player.me { font-weight: bold; }
.fan { display: flex; flex-wrap: wrap; margin-top: 3px; min-height: 26px; max-width: 46vw; }
.fan .card { --card-w: 18px; --card-h: 26px; border-radius: 3px; }
.fan .card + .card { margin-left: -11px; }
.fan.open .card { --card-w: 26px; --card-h: 37px; border-radius: 4px; }
.fan.open .card + .card { margin-left: -8px; }
.fan.open .card .pip { display: none; }

/* The table fills the whole board; the deck floats over its top-left corner. */
#board {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  background: var(--felt-dark);
  border-radius: 14px;
  padding: 12px;
  box-shadow: inset 0 0 40px rgb(0 0 0 / 35%);
}

#deck {
  position: absolute;
  z-index: 2;
  top: 18px;
  left: 18px;
  width: var(--card-w);
  height: var(--card-h);
  border-radius: 8px;
  border: 2px dashed rgb(255 255 255 / 25%);
}
#deck .card { position: absolute; top: 0; left: 0; }
#deck .card:nth-child(2) { top: -3px; left: -3px; }
#deck .card:nth-child(3) { top: -6px; left: -6px; }
#deck.can-draw, #deck.can-draw .card { cursor: pointer; }
#deck.can-draw .card:nth-last-child(2) { box-shadow: 0 0 0 3px var(--accent), 0 4px 10px rgb(0 0 0 / 40%); }
.deck-count {
  position: absolute;
  bottom: -22px;
  left: 0;
  right: 0;
  text-align: center;
  color: #fff;
  font-size: 0.85em;
}

/* Free-form table: each meld sits where it was dropped. */
#table {
  position: relative;
  flex: 1;
  min-width: 0;
  border-radius: 10px;
  overflow: auto;  /* scrolls if the melds outgrow the screen */
}
#table.drop-target { background: rgb(255 255 255 / 6%); }

.meld {
  position: absolute;
  display: flex;
  padding: 14px 6px 6px;
  border-radius: 10px;
  background: rgb(255 255 255 / 7%);
  border: 2px solid transparent;
  transition: background 0.15s, border-color 0.15s;
}
/* Cards in a meld overlap like a held hand; the top-left corner stays readable. */
.meld .card + .card { margin-left: calc(var(--card-w) * -0.45); }
.meld.incomplete { border: 2px dashed rgb(255 255 255 / 55%); }
.meld.changed { border-color: var(--accent); }
.meld.invalid { border: 2px solid var(--error); background: rgb(176 0 32 / 18%); }
.meld.drop-target { background: rgb(255 255 255 / 25%); }
.meld.moving { z-index: 40; transition: none; box-shadow: 0 16px 24px rgb(0 0 0 / 45%); }
.grip {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 14px;
  cursor: grab;
  touch-action: none;
  border-radius: 8px 8px 0 0;
  text-align: center;
  font-size: 10px;
  line-height: 14px;
  letter-spacing: 2px;
  color: rgb(255 255 255 / 55%);
}
.grip:hover { background: rgb(255 255 255 / 15%); }
/* Cards another player is dragging, following their pointer. */
.ghost {
  position: absolute;
  z-index: 20;
  display: flex;
  pointer-events: none;
  transform: rotate(-3deg);
  filter: drop-shadow(0 12px 14px rgb(0 0 0 / 45%));
  transition: left 0.09s linear, top 0.09s linear;
}
.ghost .card + .card { margin-left: calc(var(--card-w) * -0.45); }
/* Another player's pointer on the board, with their name. */
.cursor {
  position: absolute;
  z-index: 30;
  pointer-events: none;
  display: flex;
  align-items: flex-start;
  gap: 2px;
  transition: left 0.08s linear, top 0.08s linear;
}
.cursor svg { width: 16px; height: 20px; fill: var(--cursor-color); stroke: #111; stroke-width: 1; }
.cursor span {
  margin-top: 14px;
  padding: 1px 6px;
  border-radius: 8px;
  background: var(--cursor-color);
  color: #111;
  font-size: 12px;
  font-weight: bold;
  white-space: nowrap;
}
.table-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgb(255 255 255 / 35%);
  pointer-events: none;
  text-align: center;
  padding: 16px;
}

/* ----- cards ----- */

.card {
  --card-color: var(--black);
  width: var(--card-w);
  height: var(--card-h);
  flex: 0 0 auto;
  background: linear-gradient(160deg, #fff 0%, #f1ede4 100%);
  border: 1px solid #b9b2a3;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgb(0 0 0 / 35%);
  color: var(--card-color);
  position: relative;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  transition: transform 0.12s, box-shadow 0.12s;
}
.card.hearts, .card.diamonds { --card-color: var(--red); }
/* Colorblind mode: one color per suit. */
.colorblind .card.clubs { --card-color: #1f5fbf; }
.colorblind .card.diamonds { --card-color: #a67c00; }
.card.joker { --card-color: #6a1b9a; }
.card .corner {
  position: absolute;
  font-weight: bold;
  font-size: calc(var(--card-w) * 0.26);
  line-height: 1;
  white-space: pre;
  text-align: center;
}
.card .corner.tl { top: 4px; left: 4px; }
.card .corner.br { bottom: 4px; right: 4px; transform: rotate(180deg); }
.card .pip {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--card-w) * 0.55);
}
.card .joker-word {
  position: absolute;
  top: 4px;
  left: 4px;
  font-weight: bold;
  font-size: calc(var(--card-w) * 0.16);
  writing-mode: vertical-rl;
  letter-spacing: 1px;
}
.card.wild::after {
  content: "WILD";
  position: absolute;
  bottom: -1px;
  left: -1px;
  right: -1px;
  font-size: 9px;
  text-align: center;
  background: var(--accent);
  color: #222;
  border-radius: 0 0 8px 8px;
}
/* Classic card back: a red lattice inside a white margin. */
.card.back {
  background:
    repeating-linear-gradient(45deg, rgb(255 255 255 / 22%) 0 1.5px, transparent 1.5px 7px),
    repeating-linear-gradient(-45deg, rgb(255 255 255 / 22%) 0 1.5px, transparent 1.5px 7px),
    #a3232f;
  border: 1px solid #0a0a0a;
  box-shadow: inset 0 0 0 4px #fdf8ec, inset 0 0 0 5px #7a1520, 0 2px 4px rgb(0 0 0 / 35%);
  cursor: default;
}
.fan .card.back { box-shadow: inset 0 0 0 2px #fdf8ec; }
.fan .card { cursor: default; }
#rack .card:hover, #table .card:hover { transform: translateY(-5px); }
.card.selected, .card.selected:hover {
  transform: translateY(-12px);
  box-shadow: 0 0 0 3px var(--accent), 0 8px 14px rgb(0 0 0 / 40%);
}
.card.from-hand { background: linear-gradient(160deg, #fffbe6 0%, #fff0c0 100%); }
.card.dragging {
  z-index: 50;
  pointer-events: none;
  transition: none;
  box-shadow: 0 0 0 3px var(--accent), 0 16px 24px rgb(0 0 0 / 45%);
}
.card.drawn { animation: drawn-glow 1.6s ease-out; }
@keyframes drawn-glow {
  0%, 50% { box-shadow: 0 0 0 3px #ffe08a, 0 0 22px 6px rgb(255 224 138 / 85%); }
  100% { box-shadow: 0 2px 4px rgb(0 0 0 / 35%); }
}
.card.flying { position: fixed; z-index: 60; pointer-events: none; }

/* ----- rack and controls ----- */

#message { color: #fff; min-height: 1.3em; }
#message.error { color: #ffd0d0; font-weight: bold; }
#lobby-message:empty, #join-message:empty { display: none; }
#join-message.error { color: var(--error); font-weight: bold; }
#lobby-message.error { color: var(--error); font-weight: bold; }

#controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
#controls button { margin: 0; }
.spacer { flex: 1; }

#rack {
  background: linear-gradient(#9a6634, #7a4d24);
  border-radius: 12px;
  padding: 16px 12px 10px;
  min-height: calc(var(--card-h) + 26px);
  max-height: 34vh;
  overflow-y: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  box-shadow: inset 0 -6px 0 rgb(0 0 0 / 25%);
  transition: background 0.15s;
}
.rack-meld {
  display: flex;
  gap: 2px;
  padding: 4px;
  margin-right: 14px;
  border-radius: 10px;
  background: rgb(255 255 255 / 14%);
  box-shadow: inset 0 0 0 2px rgb(255 255 255 / 30%);
}
#rack.drop-target { background: linear-gradient(#b07a45, #8a5a2e); }

/* ----- chat ----- */

#corner { margin-left: auto; display: flex; gap: 8px; }
button.icon {
  position: relative;
  margin: 0;
  width: 42px;
  height: 42px;
  padding: 9px;
  border-radius: 50%;
  background: #ddd;
  box-shadow: 0 2px 8px rgb(0 0 0 / 40%);
  line-height: 0;
}
button.icon svg { width: 100%; height: 100%; fill: none; stroke: #222; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
button.icon:hover { background: #fff; }
#colorblind[aria-pressed="true"] { background: var(--accent); }
#abandon { background: #f1c9c9; }
#solo-win { background: #ffe08a; }
#chat-unread { position: absolute; top: -4px; right: -4px; line-height: 1.3; }
#chat-unread {
  background: var(--error);
  color: #fff;
  border-radius: 10px;
  padding: 0 6px;
  font-size: 0.8em;
}
#chat {
  position: fixed;
  right: 16px;
  top: 64px;
  z-index: 30;
  width: min(300px, calc(100vw - 32px));
  height: 320px;
  display: flex;
  flex-direction: column;
}
#chat-log { overflow-y: auto; flex: 1; font-size: 0.9em; margin-bottom: 8px; }
#chat-log p { margin: 2px 0; word-wrap: break-word; }

/* ----- results ----- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgb(0 0 0 / 50%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.overlay .panel { max-width: 720px; width: 100%; max-height: 90vh; overflow: auto; position: relative; }
#result-title { font-size: 1.6rem; }
#hope { text-align: right; margin: 12px 0 0; font-style: italic; color: var(--muted); }

/* Night/day button: the moon offers night mode, the sun offers day mode. */
#theme .sun, :root[data-theme="dark"] #theme .moon { display: none; }
:root[data-theme="dark"] #theme .sun { display: inline; }

/* ----- night mode: Catppuccin Mocha colors ----- */
:root[data-theme="dark"] {
  color-scheme: dark;
  --felt: #1e1e2e;
  --felt-dark: #181825;
  --panel: #313244;
  --ink: #cdd6f4;
  --muted: #a6adc8;
  --red: #f38ba8;
  --black: #cdd6f4;
  --accent: #cba6f7;
  --error: #f38ba8;
}
:root[data-theme="dark"] body { background: radial-gradient(ellipse at center, #313244 0%, #1e1e2e 70%); background-color: #1e1e2e; }
:root[data-theme="dark"] #bar,
:root[data-theme="dark"] #topbar,
:root[data-theme="dark"] .player,
:root[data-theme="dark"] .deck-count,
:root[data-theme="dark"] #message { color: #cdd6f4; }
:root[data-theme="dark"] #message.error { color: #f38ba8; }
:root[data-theme="dark"] #brand-title { text-shadow: 0 3px 0 #11111b, 0 6px 14px rgb(0 0 0 / 60%); }
:root[data-theme="dark"] button { color: #11111b; }
:root[data-theme="dark"] button.secondary { background: #45475a; color: #cdd6f4; }
:root[data-theme="dark"] .switch { background: #181825; }
:root[data-theme="dark"] .switch input:checked + label { color: #11111b; }
:root[data-theme="dark"] input,
:root[data-theme="dark"] select { background: #1e1e2e; color: #cdd6f4; border: 1px solid #585b70; border-radius: 4px; }
:root[data-theme="dark"] .player.turn { box-shadow: 0 0 12px rgb(203 166 247 / 60%); }
:root[data-theme="dark"] .card { background: linear-gradient(160deg, #45475a 0%, #313244 100%); border-color: #585b70; }
:root[data-theme="dark"] .colorblind .card.clubs { --card-color: #89b4fa; }
:root[data-theme="dark"] .colorblind .card.diamonds { --card-color: #f9e2af; }
:root[data-theme="dark"] .card.joker { --card-color: #cba6f7; }
:root[data-theme="dark"] .card.from-hand { background: linear-gradient(160deg, #585b70 0%, #45475a 100%); }
:root[data-theme="dark"] .card.back {
  background:
    repeating-linear-gradient(45deg, rgb(17 17 27 / 25%) 0 1.5px, transparent 1.5px 7px),
    repeating-linear-gradient(-45deg, rgb(17 17 27 / 25%) 0 1.5px, transparent 1.5px 7px),
    #cba6f7;
  border-color: #11111b;
  box-shadow: inset 0 0 0 4px #313244, inset 0 0 0 5px #11111b, 0 2px 4px rgb(0 0 0 / 50%);
}
:root[data-theme="dark"] .fan .card.back { box-shadow: inset 0 0 0 2px #313244; }
:root[data-theme="dark"] #rack { background: linear-gradient(#313244, #181825); }
:root[data-theme="dark"] #rack.drop-target { background: linear-gradient(#45475a, #313244); }
:root[data-theme="dark"] button.icon { background: #45475a; }
:root[data-theme="dark"] button.icon:hover { background: #585b70; }
:root[data-theme="dark"] button.icon svg { stroke: #cdd6f4; }
:root[data-theme="dark"] #colorblind[aria-pressed="true"],
:root[data-theme="dark"] #theme { background: #cba6f7; }
:root[data-theme="dark"] #abandon { background: #f38ba8; }
:root[data-theme="dark"] #solo-win { background: #f9e2af; }
:root[data-theme="dark"] #colorblind[aria-pressed="true"] svg,
:root[data-theme="dark"] #theme svg,
:root[data-theme="dark"] #abandon svg,
:root[data-theme="dark"] #solo-win svg { stroke: #11111b; }
:root[data-theme="dark"] #chat-unread { color: #11111b; }
:root[data-theme="dark"] .overlay { background: rgb(17 17 27 / 70%); }

/* Phones: smaller cards, a hand that overlaps like cards held in a fan, compact bars. */
@media (max-width: 700px), (max-height: 520px) {
  :root { --card-w: 42px; --card-h: 60px; }
  /* The page scrolls instead of squeezing: the board keeps a playable size, the hand shows whole. */
  body { height: auto; min-height: 100dvh; overflow: visible; }
  #game { flex: none; padding: 6px 16px; gap: 4px; }
  #board { flex: none; height: max(300px, 58vh); }
  #bar { padding: 6px 16px 2px; gap: 4px 10px; }
  #topbar { order: 3; flex-basis: 100%; }  /* status on its own line under the name and icons */
  #brand-title { font-size: 1.7rem; }
  #brand-by { font-size: 0.85em; }
  #topbar { gap: 4px 8px; font-size: 0.85em; }
  .player { padding: 2px 6px; }
  .fan { max-width: 60vw; min-height: 0; }
  .fan .card { --card-w: 12px; --card-h: 18px; }
  .fan .card + .card { margin-left: -8px; }
  .fan.open .card { --card-w: 20px; --card-h: 28px; }
  .fan.open .card + .card { margin-left: -9px; }
  #corner { gap: 6px; }
  button.icon { width: 34px; height: 34px; padding: 7px; }
  #board { padding: 6px; border-radius: 10px; }
  #deck { top: 10px; left: 10px; }
  .meld { padding: 12px 4px 4px; }
  #message { font-size: 0.85em; min-height: 1.1em; }
  #controls { gap: 4px; }
  #controls button { padding: 6px 9px; font-size: 0.85em; }
  /* Left padding makes room for the overlap of a card that starts a wrapped row. */
  #rack { padding: 12px 8px 8px calc(8px + var(--card-w) * 0.38); gap: 6px 0; min-height: calc(var(--card-h) + 20px); max-height: none; }
  #rack > .card, .rack-meld .card + .card { margin-left: calc(var(--card-w) * -0.38); }
  .rack-meld { margin-left: calc(var(--card-w) * -0.38); }
  .rack-meld { margin-right: 24px; gap: 0; }
  .card .pip { font-size: calc(var(--card-w) * 0.45); }
  .card.selected, .card.selected:hover { transform: translateY(-8px); }
}

@media (max-height: 520px) {
  :root { --card-w: 36px; --card-h: 52px; }
}

@media (prefers-reduced-motion: reduce) {
  .card, .player, #rack, .meld { transition: none; }
  .card.drawn { animation: none; }
}
