:root {
  color-scheme: dark;
  --bg: #08110e;
  --bg-2: #0c1a15;
  --panel: rgba(19, 37, 31, 0.94);
  --panel-solid: #10221c;
  --panel-2: #173029;
  --border: #2b4a3f;
  --border-strong: #456a5b;
  --text: #f3f8f5;
  --muted: #a8c1b6;
  --accent: #f2b84b;
  --accent-ink: #1c1404;
  --focus: #ffe28a;
  --you: #54d6b2;
  --opponent: #ff9b9b;
  --good: #58d68d;
  --bad: #ff8a8a;
  --tile: #fbf2df;
  --tile-2: #e6d5b5;
  --tile-edge: #c7b28d;
  --pip: #1b1b18;
  --felt: #12352a;
  --felt-2: #0d271f;
  --shadow: 0 18px 45px rgba(0, 0, 0, 0.38);
  --radius: 18px;
  --hand-tile: clamp(44px, 11.5vw, 60px);
  --board-tile: clamp(38px, 9vw, 54px);
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #edf3ed;
    --bg-2: #e2ece4;
    --panel: rgba(255, 255, 255, 0.94);
    --panel-solid: #ffffff;
    --panel-2: #f1f7f2;
    --border: #c4d3c9;
    --border-strong: #9db3a5;
    --text: #14231c;
    --muted: #50675b;
    --accent: #8a5500;
    --accent-ink: #fffaf0;
    --focus: #6a4600;
    --you: #0b6f58;
    --opponent: #ad2f2f;
    --good: #18763f;
    --bad: #b32626;
    --tile: #fffaf0;
    --tile-2: #eee0c6;
    --tile-edge: #c9b58f;
    --pip: #1a1a17;
    --felt: #2d6b51;
    --felt-2: #23563f;
    --shadow: 0 18px 42px rgba(23, 48, 36, 0.16);
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100svh;
  background:
    radial-gradient(circle at 15% 0%, rgba(84, 214, 178, 0.09), transparent 34%),
    radial-gradient(circle at 90% 8%, rgba(242, 184, 75, 0.08), transparent 30%),
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.012) 0 2px, transparent 2px 7px),
    var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  line-height: 1.5;
}

a {
  color: var(--accent);
}

button,
select,
input {
  font: inherit;
}

button,
select {
  min-height: 44px;
  min-width: 44px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--text);
  padding: 0.55rem 0.85rem;
}

button {
  cursor: pointer;
  font-weight: 650;
  transition: transform 160ms ease, background-color 160ms ease, border-color 160ms ease;
}

button:hover:not(:disabled) {
  border-color: var(--accent);
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

button.danger {
  border-color: var(--bad);
  color: var(--bad);
}

button.primary.danger {
  background: var(--bad);
  color: #210808;
}

button.quiet {
  background: transparent;
  color: var(--text);
}

button.danger.quiet {
  color: var(--bad);
}

button.large {
  padding: 0.8rem 1.1rem;
  font-size: 1.05rem;
}

button:focus-visible,
select:focus-visible,
a:focus-visible,
[tabindex]:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

select {
  width: 100%;
}

.skip-link {
  position: fixed;
  left: 0.75rem;
  top: -4rem;
  z-index: 100;
  padding: 0.7rem 1rem;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 800;
  text-decoration: none;
}

.skip-link:focus {
  top: 0.75rem;
}

.app {
  width: min(1180px, 100% - 2rem);
  margin: 0 auto;
  padding: 1rem 0 2.5rem;
}

.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  min-width: 0;
}

.brand img {
  flex: none;
  border-radius: 12px;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(1.55rem, 3.2vw, 2.2rem);
  letter-spacing: 0.02em;
}

.subtitle {
  margin: 0.15rem 0 0;
  color: var(--muted);
}

.top-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 0.6rem;
}

.field {
  display: grid;
  gap: 0.25rem;
  font-weight: 650;
  font-size: 0.9rem;
}

.connection {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  min-height: 44px;
  padding: 0.45rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel-2);
  font-size: 0.9rem;
}

.connection .dot {
  width: 0.62rem;
  height: 0.62rem;
  border-radius: 50%;
  background: var(--bad);
}

.connection[data-state="connected"] .dot {
  background: var(--good);
}

.connection[data-state="connecting"] .dot,
.connection[data-state="reconnecting"] .dot {
  background: var(--accent);
}

.panel {
  margin-top: 1rem;
  padding: clamp(1rem, 2.5vw, 1.5rem);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
}

.screen[hidden],
[hidden] {
  display: none !important;
}

.panel h2,
.game-header h2 {
  margin: 0 0 0.35rem;
  font-size: clamp(1.35rem, 2.6vw, 1.8rem);
}

.menu-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 1rem;
  margin-top: 1rem;
}

.card {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding: 1.1rem;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: linear-gradient(160deg, var(--panel-2), var(--panel-solid));
}

.card h3 {
  margin: 0;
  font-size: 1.2rem;
}

.card p {
  margin: 0 0 0.8rem;
  color: var(--muted);
}

.card button {
  margin-top: auto;
  align-self: flex-start;
}

.session-box {
  margin-top: 1.2rem;
  padding: 0.9rem;
  border: 1px dashed var(--border-strong);
  border-radius: 14px;
}

.session-box h3 {
  margin: 0 0 0.6rem;
}

.session-list,
.lobby-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.55rem;
}

.session-list li,
.lobby-list li {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel-2);
}

.session-meta {
  display: grid;
  gap: 0.1rem;
}

.session-meta strong {
  font-size: 1rem;
}

.session-meta span {
  color: var(--muted);
  font-size: 0.9rem;
}

.session-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 1rem;
  align-items: end;
  margin: 1rem 0;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin: 0.8rem 0;
}

.actions.compact {
  margin: 0;
}

.waiting-box {
  margin: 1rem 0;
  padding: 0.9rem;
  border: 1px dashed var(--accent);
  border-radius: 14px;
  background: rgba(242, 184, 75, 0.06);
}

.waiting-box p {
  margin: 0.2rem 0;
}

.muted {
  color: var(--muted);
}

.hint {
  min-height: 1.5em;
  margin: 0.6rem 0 0;
  color: var(--muted);
  text-align: center;
}

.notice {
  margin: 0.8rem 0 0;
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--bad);
  border-radius: 12px;
  background: rgba(255, 138, 138, 0.12);
  color: var(--text);
}

.game-header {
  display: flex;
  flex-wrap: wrap;
  align-items: start;
  justify-content: space-between;
  gap: 0.8rem;
}

.game-header p {
  margin: 0;
}

.status-strip {
  display: grid;
  grid-template-columns: minmax(260px, 0.9fr) minmax(260px, 1.1fr);
  gap: 1rem;
  margin: 1rem 0;
}

.scoreboard {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 0.6rem;
}

.score,
.target {
  display: grid;
  gap: 0.1rem;
  padding: 0.65rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel-2);
}

.score span,
.target span {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 650;
}

.score strong,
.target strong {
  font-size: clamp(1.35rem, 3vw, 1.9rem);
  line-height: 1.1;
}

.score.you {
  border-color: var(--you);
}

.score.you strong {
  color: var(--you);
}

.score.opponent {
  border-color: var(--opponent);
}

.score.opponent strong {
  color: var(--opponent);
}

.target {
  text-align: center;
  align-items: center;
}

.status-text {
  display: grid;
  align-content: center;
  gap: 0.15rem;
}

.status-text p {
  margin: 0;
}

.turn-line {
  font-size: 1.08rem;
  font-weight: 800;
}

.timer-line {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.timer-line[data-warning="true"] {
  color: var(--bad);
  font-weight: 800;
}

.connection-note {
  color: var(--accent);
  font-weight: 650;
}

.board-section {
  margin-top: 1rem;
  padding: clamp(0.8rem, 2.4vw, 1.2rem);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  background:
    radial-gradient(circle at 50% -15%, rgba(255, 255, 255, 0.12), transparent 45%),
    linear-gradient(145deg, var(--felt), var(--felt-2));
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.05);
}

.board-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
}

.board-head h3 {
  margin: 0;
  color: #f7fbf8;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

.open-ends {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.end-chip {
  display: inline-flex;
  align-items: center;
  min-height: 38px;
  padding: 0.25rem 0.7rem;
  border: 1px solid rgba(255, 255, 255, 0.34);
  border-radius: 999px;
  background: rgba(8, 17, 14, 0.55);
  color: #fff7df;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.board-scroll {
  margin-top: 0.8rem;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.9rem 0.4rem;
  border-radius: 12px;
  scrollbar-color: var(--accent) rgba(0, 0, 0, 0.25);
}

.board {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 5px;
  width: max-content;
  min-height: calc(var(--board-tile) + 1rem);
  margin: 0 auto;
}

.board:focus-visible {
  outline: 4px solid var(--focus);
  outline-offset: 6px;
}

.board-empty {
  margin: 0.4rem 0 0;
  color: rgba(255, 255, 255, 0.78);
  text-align: center;
}

.open-ends-line {
  margin: 0.5rem 0 0;
  color: rgba(255, 255, 255, 0.85);
  text-align: center;
  font-weight: 650;
}

.chain-tile {
  flex: none;
}

.chain-tile:focus {
  outline: none;
}

.chain-tile .tile.is-last {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.tile {
  display: flex;
  overflow: hidden;
  border: 1px solid var(--tile-edge);
  border-radius: 9px;
  background: linear-gradient(145deg, var(--tile), var(--tile-2));
  color: var(--pip);
  box-shadow: 0 5px 12px rgba(0, 0, 0, 0.28), inset 0 1px 0 rgba(255, 255, 255, 0.7);
}

.tile.is-horizontal {
  flex-direction: row;
  width: calc(var(--board-tile) * 2);
  height: var(--board-tile);
}

.tile.is-vertical {
  flex-direction: column;
  width: var(--hand-tile);
  height: calc(var(--hand-tile) * 2);
}

.tile .half {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 3%;
  padding: 11%;
}

.tile.is-horizontal .half + .half {
  border-left: 1px solid var(--tile-edge);
}

.tile.is-vertical .half + .half {
  border-top: 1px solid var(--tile-edge);
}

.pip-slot {
  display: grid;
  place-items: center;
}

.pip-slot.on::after {
  content: "";
  width: 74%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: currentColor;
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.3);
}

.hand-section {
  margin-top: 1rem;
}

.hand-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.hand-head h3 {
  margin: 0;
}

.hand {
  list-style: none;
  margin: 0.8rem 0 0;
  padding: 0.35rem;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.7rem;
}

.hand li {
  display: flex;
}

.hand button {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  min-height: 44px;
  padding: 0.25rem;
  border: 0;
  border-radius: 14px;
  background: transparent;
  color: var(--text);
}

.hand button:hover:not([aria-disabled="true"]) .tile {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.hand button .tile {
  transition: transform 150ms ease, box-shadow 150ms ease, border-color 150ms ease;
}

.hand button.playable .tile {
  border-color: var(--you);
  box-shadow: 0 0 0 3px rgba(84, 214, 178, 0.3), 0 6px 14px rgba(0, 0, 0, 0.28);
}

.hand button.selected .tile {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

.hand button[aria-disabled="true"] .tile {
  opacity: 0.55;
  filter: grayscale(0.35);
}

.tile-number {
  font-size: 0.82rem;
  font-weight: 800;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.tile-badge {
  position: absolute;
  top: -0.2rem;
  right: -0.2rem;
  display: grid;
  place-items: center;
  width: 1.4rem;
  height: 1.4rem;
  border-radius: 50%;
  background: var(--you);
  color: #04140f;
  font-size: 0.85rem;
  font-weight: 900;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

.end-choice {
  margin: 0.8rem 0;
  padding: 0.85rem;
  border: 2px solid var(--accent);
  border-radius: 14px;
  background: var(--panel-2);
}

.end-choice p {
  margin: 0;
  font-weight: 750;
}

.end-choice:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

.game-actions {
  justify-content: center;
  margin-top: 1rem;
}

.result {
  margin-top: 1rem;
  padding: 1rem;
  border: 2px solid var(--accent);
  border-radius: 14px;
  background: var(--panel-2);
}

.result:focus-visible {
  outline: 4px solid var(--focus);
  outline-offset: 3px;
}

.result h3 {
  margin: 0 0 0.4rem;
}

.result p {
  margin: 0.3rem 0;
}

.result-next {
  color: var(--accent);
  font-weight: 750;
}

.footer {
  margin-top: 1.2rem;
  padding: 0.8rem 1rem;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

.footer p {
  margin: 0.2rem 0;
}

.modal {
  width: min(720px, calc(100% - 2rem));
  max-height: min(82svh, 900px);
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  background: var(--panel-solid);
  color: var(--text);
  box-shadow: var(--shadow);
}

.modal::backdrop {
  background: rgba(3, 9, 7, 0.78);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.9rem 1rem;
  border-bottom: 1px solid var(--border);
}

.modal-head h2 {
  margin: 0;
  font-size: 1.3rem;
}

.modal-body {
  max-height: 68svh;
  overflow-y: auto;
  padding: 0 1rem 1rem;
}

.modal-body p {
  margin: 0.7rem 0;
}

.confirm {
  width: min(460px, calc(100% - 2rem));
}

.shortcuts-list {
  display: grid;
  gap: 0;
  margin: 0.8rem 0 0;
}

.shortcuts-list > div {
  display: grid;
  grid-template-columns: minmax(80px, 110px) 1fr;
  gap: 0.8rem;
  align-items: baseline;
  padding: 0.45rem 0;
  border-bottom: 1px solid var(--border);
}

.shortcuts-list dt,
.shortcuts-list dd {
  margin: 0;
}

.shortcuts-list dd {
  color: var(--muted);
}

kbd {
  display: inline-block;
  min-width: 2rem;
  padding: 0.12rem 0.45rem;
  border: 1px solid var(--border-strong);
  border-bottom-width: 3px;
  border-radius: 6px;
  background: var(--panel-2);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  text-align: center;
}

@media (max-width: 900px) {
  .status-strip {
    grid-template-columns: 1fr;
  }

  .game-header {
    align-items: stretch;
  }
}

@media (max-width: 640px) {
  .app {
    width: calc(100% - 1rem);
  }

  .topbar {
    flex-direction: column;
    align-items: stretch;
  }

  .top-controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    align-items: end;
  }

  .top-controls .field {
    grid-column: 1 / -1;
  }

  .top-controls .connection {
    grid-column: 1 / -1;
    justify-content: center;
  }

  .scoreboard {
    grid-template-columns: 1fr 1fr;
  }

  .target {
    grid-column: 1 / -1;
    grid-template-columns: auto auto;
    justify-content: center;
    gap: 0.6rem;
  }

  .open-ends {
    width: 100%;
  }

  .end-chip {
    flex: 1;
    justify-content: center;
  }

  .modal-head {
    align-items: start;
    flex-direction: column;
  }

  .shortcuts-list > div {
    grid-template-columns: 1fr;
    gap: 0.1rem;
  }

  .shortcuts-list dd {
    padding-bottom: 0.4rem;
  }
}

@media (max-width: 420px) {
  :root {
    --hand-tile: 43px;
    --board-tile: 36px;
  }

  .actions button {
    flex: 1 1 auto;
  }

  .session-list li,
  .lobby-list li {
    align-items: stretch;
    flex-direction: column;
  }

  .session-actions {
    width: 100%;
  }

  .session-actions button {
    flex: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}

@media (prefers-contrast: more) {
  :root {
    --border: var(--border-strong);
    --panel: var(--panel-solid);
    --muted: var(--text);
  }

  button,
  .score,
  .target,
  .card,
  .session-list li,
  .lobby-list li {
    border-width: 2px;
  }

  .tile {
    border-width: 2px;
  }
}

@media (forced-colors: active) {
  .tile {
    border: 1px solid CanvasText;
    background: Canvas;
    color: CanvasText;
    box-shadow: none;
  }

  .pip-slot.on::after {
    background: CanvasText;
  }

  .hand button.playable .tile,
  .hand button.selected .tile,
  .chain-tile .tile.is-last {
    outline: 3px solid Highlight;
  }

  .tile-badge {
    background: Highlight;
    color: HighlightText;
  }

  .end-chip {
    border: 1px solid CanvasText;
    background: Canvas;
    color: CanvasText;
  }

  .board-section {
    background: Canvas;
  }

  .board-head h3,
  .board-empty,
  .open-ends-line {
    color: CanvasText;
  }
}
