:root {
  --bg0: #e0e5ec;
  --ink: #4a5568;
  --muted: #718096;
  --accent-2: #2b6cb0;
  --danger: #e53e3e;
  --panel: #e0e5ec;
  --light-sq: #f0f4f8;
  --dark-sq: #cbd5e0;
  --hl: rgba(66, 153, 225, 0.5);
  --last: rgba(66, 153, 225, 0.2);
  --font-display: "Inter", sans-serif;
  --font-ui: "Inter", sans-serif;
  --shadow-out: 9px 9px 16px rgb(163, 177, 198, 0.6), -9px -9px 16px rgba(255, 255, 255, 0.5);
  --shadow-in: inset 5px 5px 10px #bec3c9, inset -5px -5px 10px #ffffff;
  --shadow-btn: 5px 5px 10px #bec3c9, -5px -5px 10px #ffffff;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  color: var(--ink);
  font-family: var(--font-ui);
  background: var(--bg0);
}

.bg { display: none; }

#app {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1rem;
  padding-bottom: max(1rem, env(safe-area-inset-bottom));
}

.panel {
  width: min(420px, 100%);
  background: var(--panel);
  border-radius: 24px;
  padding: 2rem 1.5rem;
  box-shadow: var(--shadow-out);
  animation: rise 420ms ease both;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

.brand-mark {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(2.4rem, 10vw, 3.2rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.05;
  color: #2d3748;
}

.brand-sub {
  margin: 0.55rem 0 2rem;
  color: var(--muted);
  font-size: 0.98rem;
  line-height: 1.4;
}

.actions {
  display: grid;
  gap: 1.2rem;
  margin-bottom: 1.5rem;
}

.btn {
  appearance: none;
  border: none;
  background: var(--panel);
  color: var(--ink);
  font: 600 1rem var(--font-ui);
  padding: 1rem 1.2rem;
  border-radius: 12px;
  cursor: pointer;
  box-shadow: var(--shadow-btn);
  transition: transform 0.1s ease, box-shadow 0.1s ease, color 0.2s ease;
}
.btn:hover { color: var(--accent-2); }
.btn:active {
  transform: scale(0.98);
  box-shadow: var(--shadow-in);
}
.btn.primary {
  color: var(--accent-2);
  font-weight: 700;
}
.btn.danger { color: var(--danger); }
.btn.small { padding: 0.6rem 1rem; font-size: 0.9rem; border-radius: 10px; }
.btn.wide { width: 100%; margin-top: 1.5rem; }
.btn:disabled { opacity: 0.5; cursor: default; box-shadow: none; }

.join-form label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.6rem;
  font-weight: 600;
}
.row { display: flex; gap: 1rem; }
.row.gap { flex-wrap: wrap; }
.join-form input {
  flex: 1;
  min-width: 0;
  border-radius: 12px;
  border: none;
  background: var(--panel);
  box-shadow: var(--shadow-in);
  color: var(--ink);
  font: 600 1.05rem var(--font-ui);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 1rem;
}
.join-form input:focus { outline: none; }

.error { color: var(--danger); margin: 1rem 0 0; font-size: 0.92rem; font-weight: 600; }
.hint { color: var(--muted); font-size: 0.88rem; margin: 0.8rem 0 0; }

.setup h1, .waiting h1 {
  font-family: var(--font-display);
  font-size: 1.7rem;
  margin: 0.2rem 0 1.5rem;
  color: #2d3748;
}
fieldset {
  border: 0;
  margin: 0 0 1.5rem;
  padding: 0;
}
legend {
  color: var(--muted);
  font-size: 0.85rem;
  margin-bottom: 0.8rem;
  font-weight: 600;
}
.segmented {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
  gap: 0.8rem;
}
.seg {
  border: none;
  background: var(--panel);
  box-shadow: var(--shadow-btn);
  color: var(--ink);
  border-radius: 10px;
  padding: 0.8rem 0.4rem;
  font: 600 0.88rem var(--font-ui);
  cursor: pointer;
  transition: all 0.2s ease;
}
.seg.active {
  box-shadow: var(--shadow-in);
  color: var(--accent-2);
}

.linkback {
  background: none;
  border: 0;
  color: var(--muted);
  font: 600 0.95rem var(--font-ui);
  cursor: pointer;
  padding: 0;
  margin-bottom: 1rem;
}
.linkback:hover { color: var(--ink); }

.code-label { color: var(--muted); margin: 0; font-weight: 600; }
.code-big {
  font-family: var(--font-display);
  font-size: 3.5rem;
  letter-spacing: 0.15em;
  margin: 0.5rem 0 1.5rem;
  color: #2d3748;
  font-weight: 700;
  text-shadow: 3px 3px 6px #bec3c9, -3px -3px 6px #ffffff;
}

.game {
  width: min(560px, 100%);
  display: grid;
  gap: 1.2rem;
  animation: rise 420ms ease both;
}
.game-top, .game-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0 0.5rem;
}
.clock {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  min-width: 7rem;
  background: var(--panel);
  padding: 0.6rem 1rem;
  border-radius: 12px;
  box-shadow: var(--shadow-btn);
  transition: box-shadow 0.3s ease;
}
.clock.active { box-shadow: var(--shadow-in); }
.clock-name { color: var(--muted); font-size: 0.82rem; font-weight: 600; }
.clock-time {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 1.35rem;
  color: var(--ink);
}
.clock.active .clock-time { color: var(--accent-2); }
.status-line {
  text-align: right;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
}
.game-actions { display: flex; flex-wrap: wrap; gap: 0.8rem; justify-content: flex-end; }

.board-wrap {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 16px;
  padding: 12px;
  background: var(--panel);
  box-shadow: var(--shadow-out);
  position: relative;
}
.board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  user-select: none;
  touch-action: manipulation;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow-in);
}
.pieces-layer {
  position: absolute;
  inset: 12px;
  pointer-events: none;
}
.piece {
  position: absolute;
  width: 12.5%;
  height: 12.5%;
  background-size: 90%;
  background-repeat: no-repeat;
  background-position: center;
  z-index: 10;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.15));
  transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.2s ease;
  will-change: transform;
}
.sq {
  position: relative;
  display: grid;
  place-items: center;
  cursor: pointer;
}
.sq.light { background: var(--light-sq); }
.sq.dark { background: var(--dark-sq); }
.sq.selected { outline: 3px solid var(--hl); outline-offset: -3px; z-index: 2; }
.sq.last { box-shadow: inset 0 0 0 100px var(--last); }
.sq.target::after {
  content: "";
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(66, 153, 225, 0.4);
  position: absolute;
}
.sq.target.capture::after {
  width: 86%;
  height: 86%;
  background: transparent;
  border: 4px solid rgba(66, 153, 225, 0.4);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(224, 229, 236, 0.8);
  backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  padding: 1rem;
  z-index: 20;
}
.modal[hidden],
.toast[hidden] {
  display: none !important;
}
.modal-card {
  width: min(380px, 100%);
  background: var(--panel);
  border-radius: 24px;
  padding: 2rem;
  box-shadow: var(--shadow-out);
  animation: rise 280ms ease both;
  text-align: center;
}
.modal-card h2 {
  font-family: var(--font-display);
  margin: 0 0 1rem;
  color: #2d3748;
}
.modal-card p { color: var(--muted); margin: 0 0 2rem; font-size: 1.1rem; }
.modal-card .row { justify-content: center; }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(1.5rem, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--panel);
  box-shadow: var(--shadow-out);
  color: var(--ink);
  padding: 1rem 1.5rem;
  border-radius: 999px;
  z-index: 30;
  font-size: 0.95rem;
  font-weight: 600;
  animation: rise 200ms ease both;
}

@media (max-width: 420px) {
  .game-top, .game-bottom { align-items: flex-start; }
  .status-line { font-size: 0.85rem; }
}
