*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
[hidden] { display: none !important; }

:root {
  --bg: #f5f0e8;
  --bg-warm: #ede7db;
  --surface: #fffdf8;
  --ink: #2c2826;
  --ink-light: #8a8279;
  --ink-muted: #b8b0a4;
  --line: #ddd5c8;
  --accent: #2f5ec4;
  --accent-hover: #264fa8;
  --accent-ink: #fff;
  --frame: #2f5ec4;
  --frame-edge: #234a9e;
  --hole: #e6dfd2;
  --red: #d8443a;
  --red-edge: #b0322a;
  --yellow: #f2b630;
  --yellow-edge: #cf9518;
  --ok: #3f9a6b;
  --bad: #c45d3e;
  --radius: 6px;
  --bw: max(250px, min(calc(100vw - 2rem), 540px, calc((100dvh - 15.5rem) * 7 / 6)));
  color-scheme: light;
}

html.dark {
  --bg: #1a1714;
  --bg-warm: #222019;
  --surface: #23201c;
  --ink: #e8e2d8;
  --ink-light: #8a8279;
  --ink-muted: #5a544e;
  --line: #38332d;
  --accent: #5a85e0;
  --accent-hover: #6f96ea;
  --accent-ink: #0c1424;
  --frame: #2a4d9a;
  --frame-edge: #1f3d7e;
  --hole: #12100d;
  --red: #e0554a;
  --red-edge: #b23a31;
  --yellow: #f0bb3e;
  --yellow-edge: #c8931c;
  --ok: #4fb683;
  --bad: #e0704f;
  color-scheme: dark;
}

html { background: var(--bg); }

body {
  font-family: 'DM Mono', ui-monospace, Menlo, Consolas, monospace;
  background: var(--bg);
  color: var(--ink);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: env(safe-area-inset-top, 0) max(1rem, env(safe-area-inset-right, 0)) env(safe-area-inset-bottom, 0) max(1rem, env(safe-area-inset-left, 0));
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

button, input { font: inherit; color: inherit; }

button {
  padding: 0.5rem 0.95rem;
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  font-size: 0.85rem;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.6; cursor: default; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-accent { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 500; }
.btn-accent:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.wide { width: 100%; }
button.small { padding: 0.25rem 0.6rem; font-size: 0.75rem; }

input {
  padding: 0.5rem 0.7rem;
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 0.9rem;
  min-width: 0;
}

/* ── Top bar ── */
.top-bar {
  width: 100%;
  max-width: 960px;
  padding: 0.85rem 0 0.4rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}
.title { border: 0; background: none; padding: 0; }
.title:hover:not(:disabled) { border: 0; }
h1 {
  font-family: 'Fraunces', Georgia, serif;
  font-weight: 700;
  font-size: 1.45rem;
  letter-spacing: -0.02em;
}
h1 span { color: var(--accent); }
.top-actions { display: flex; align-items: center; gap: 0.75rem; }
.badge { font-size: 0.72rem; color: var(--ink-light); }
.icon-btn { border: 0; background: none; padding: 0.2rem; font-size: 1.05rem; line-height: 1; color: var(--ink-light); }

main { width: 100%; max-width: 960px; flex: 1; }

/* ── Menu ── */
.menu { max-width: 440px; margin: 0 auto; padding-block: 1rem 2rem; display: flex; flex-direction: column; gap: 1rem; }
.tagline { font-family: 'Fraunces', Georgia, serif; font-size: 1.25rem; line-height: 1.3; color: var(--ink); }
.resume { display: flex; flex-direction: column; gap: 0.5rem; }
.resume:empty { display: none; }
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.panel h2 { font-family: 'Fraunces', Georgia, serif; font-size: 1.15rem; display: flex; align-items: center; gap: 0.6rem; }
.live { display: inline-flex; align-items: center; gap: 0.35rem; font-family: 'DM Mono', monospace; font-size: 0.7rem; font-weight: 400; color: var(--ink-light); }
.live i { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); }
.row { display: flex; gap: 0.5rem; }
.row > * { flex: 1; }
.seg { display: flex; border: 1.5px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.seg button { flex: 1; border: 0; border-radius: 0; background: transparent; }
.seg button + button { border-left: 1.5px solid var(--line); }
.seg button.active { background: var(--accent); color: var(--accent-ink); }
.record { font-size: 0.75rem; color: var(--ink-light); }
.err { font-size: 0.8rem; color: var(--bad); }
.rules { font-size: 0.82rem; color: var(--ink-light); line-height: 1.55; }
.rules summary { cursor: pointer; color: var(--ink); }
.rules p { margin-top: 0.6rem; }
kbd { font-family: inherit; font-size: 0.75rem; padding: 0 0.3rem; border: 1px solid var(--line); border-radius: 4px; }

/* ── Game ── */
.game { padding-block: 0.5rem 2rem; display: flex; flex-direction: column; align-items: center; }

.room-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem 0.9rem;
  font-size: 0.8rem;
  color: var(--ink-light);
  margin-bottom: 0.6rem;
}
.room-bar b { color: var(--ink); font-weight: 500; letter-spacing: 0.12em; font-size: 0.95rem; }
.opp-status::before { content: ''; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--ink-muted); margin-right: 0.35rem; }
.opp-status.on::before { background: var(--ok); }

.players {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  width: var(--bw);
}
.player {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.7rem;
  border: 1.5px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.82rem;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.player span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player#p2 { flex-direction: row-reverse; text-align: right; }
.player.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.player.active span { font-weight: 500; }
.vs { font-size: 0.72rem; color: var(--ink-light); }
.chip { flex: none; width: 1rem; height: 1rem; border-radius: 50%; }
.chip.red { background: var(--red); box-shadow: inset 0 -2px 0 var(--red-edge); }
.chip.yellow { background: var(--yellow); box-shadow: inset 0 -2px 0 var(--yellow-edge); }

.status {
  min-height: 2.6rem;
  display: grid;
  place-items: center;
  text-align: center;
  font-family: 'Fraunces', Georgia, serif;
  font-size: 1.05rem;
  padding-block: 0.4rem;
}
.status.alert { color: var(--accent); }

/* Board: a frame with round holes on top of a layer of discs, so discs fall behind the frame. */
.board-wrap { width: var(--bw); --pad: calc(var(--bw) * 0.025); }
.preview {
  height: calc((var(--bw) - 2 * var(--pad)) / 7);
  padding-inline: var(--pad);
  position: relative;
}
.preview .disc {
  inset: 10% auto auto var(--pad);
  width: calc((var(--bw) - 2 * var(--pad)) / 7 * 0.8);
  aspect-ratio: 1;
  margin-left: calc((var(--bw) - 2 * var(--pad)) / 70);
  transform: translateX(calc(var(--col, 3) * 125%));
  transition: transform 0.12s ease-out, opacity 0.15s;
  opacity: 0;
}
.preview.show .disc { opacity: 1; }

.board {
  position: relative;
  padding: var(--pad);
  background: var(--frame);
  border-radius: 14px;
  box-shadow: inset 0 -4px 0 var(--frame-edge), 0 10px 30px rgba(20, 30, 60, 0.18);
  outline: none;
  cursor: default;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.board.can-drop { cursor: pointer; }
.board:focus-visible { box-shadow: inset 0 -4px 0 var(--frame-edge), 0 0 0 3px var(--bg), 0 0 0 5px var(--accent); }
.discs {
  position: relative;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-template-rows: repeat(6, 1fr);
  aspect-ratio: 7 / 6;
  background: var(--hole);
  overflow: hidden;
}
.slot { position: relative; }
.frame {
  position: absolute;
  inset: var(--pad);
  pointer-events: none;
  background:
    radial-gradient(circle closest-side, transparent 76%, rgba(0, 0, 0, 0.22) 77%, var(--frame) 81%)
    0 0 / calc(100% / 7) calc(100% / 6);
}

.disc {
  position: absolute;
  inset: 9%;
  border-radius: 50%;
}
.disc.red { background: var(--red); box-shadow: inset 0 0 0 calc(var(--bw) * 0.008) var(--red-edge), inset 0 calc(var(--bw) * -0.008) 0 calc(var(--bw) * 0.012) var(--red-edge); }
.disc.yellow { background: var(--yellow); box-shadow: inset 0 0 0 calc(var(--bw) * 0.008) var(--yellow-edge), inset 0 calc(var(--bw) * -0.008) 0 calc(var(--bw) * 0.012) var(--yellow-edge); }

.disc.fall { animation: fall var(--t, 0.4s) both; }
@keyframes fall {
  0% { transform: translateY(var(--from)); animation-timing-function: cubic-bezier(0.55, 0, 1, 0.45); }
  76% { transform: translateY(0); animation-timing-function: ease-out; }
  88% { transform: translateY(-9%); animation-timing-function: ease-in; }
  100% { transform: translateY(0); }
}

.disc.last::before {
  content: '';
  position: absolute;
  inset: 41%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.7);
}
.disc.win::after {
  content: '';
  position: absolute;
  inset: 24%;
  border-radius: 50%;
  border: calc(var(--bw) * 0.007 + 1px) solid rgba(255, 255, 255, 0.9);
  animation: pulse 1.2s ease-in-out infinite alternate;
}
.disc.win.last::before { display: none; }
@keyframes pulse { from { transform: scale(0.85); } to { transform: scale(1.05); } }
.board.over .disc:not(.win) { opacity: 0.45; }

.col-labels {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  padding: 0.3rem var(--pad) 0;
  font-size: 0.65rem;
  color: var(--ink-light);
  text-align: center;
}

.actions { display: flex; justify-content: center; flex-wrap: wrap; gap: 0.6rem; margin-top: 1rem; }
.actions:empty { display: none; }

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(20, 16, 12, 0.45);
  animation: fade 0.25s ease;
}
.modal-box {
  width: min(340px, 100%);
  background: var(--surface);
  border-radius: 14px;
  padding: 1.6rem 1.4rem 1.3rem;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  animation: rise 0.3s ease;
}
.modal-title { font-family: 'Fraunces', Georgia, serif; font-weight: 700; font-size: 1.8rem; }
.modal-title.won { color: var(--accent); }
.modal-title.lost { color: var(--bad); }
.modal-sub { font-size: 0.8rem; color: var(--ink-light); margin-top: 0.5rem; line-height: 1.6; }
.modal-actions { display: flex; gap: 0.6rem; justify-content: center; margin-top: 1.2rem; }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(12px); opacity: 0; } }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(1.2rem + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  max-width: calc(100vw - 2rem);
  padding: 0.55rem 1rem;
  border-radius: 999px;
  background: var(--ink);
  color: var(--bg);
  font-size: 0.8rem;
  z-index: 30;
  overflow-wrap: anywhere;
}

#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 40; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
