:root {
  color-scheme: dark;
  --night: #101021;
  --ink: #f5f1ff;
  --muted: #b5aecb;
  --line: #38324e;
  --coral: #ffad9e;
  --lilac: #c1b0ff;
  --mint: #a7efda;
  --digits: "Segoe UI Variable Display", "Avenir Next", "Segoe UI", system-ui, sans-serif;
  --body: "PingFang SC", "Microsoft YaHei UI", "Noto Sans CJK SC", system-ui, sans-serif;
  font-family: var(--body);
  background: var(--night);
  color: var(--ink);
}

*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 280px; min-height: 100svh; }
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background:
    radial-gradient(ellipse at 15% 24%, #42305460, transparent 52%),
    radial-gradient(ellipse at 92% 88%, #272d5c70, transparent 52%),
    var(--night);
}
[hidden] { display: none !important; }
a { color: inherit; text-decoration: none; }
button, input { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: .48; }
:focus-visible { outline: 2px solid var(--mint); outline-offset: 4px; }

.shell { width: min(100% - 72px, 1056px); margin: 0 auto; }
.masthead {
  min-height: 91px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 1.3rem; font-weight: 800; letter-spacing: .09em; }
.connection {
  display: inline-flex;
  gap: 9px;
  align-items: center;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: .875rem;
}
.connection i { width: 8px; height: 8px; border-radius: 50%; background: var(--lilac); }
.connection[data-state="live"] i { background: var(--mint); }
.connection[data-state="offline"] i { background: var(--coral); }
.layout {
  min-height: min(680px, calc(100svh - 165px));
  display: grid;
  grid-template-columns: minmax(0, .78fr) minmax(0, 1fr);
  align-items: center;
  gap: clamp(42px, 7vw, 112px);
  padding: 42px 0 32px;
}
.intro h1, .room h1 { margin: 0; letter-spacing: -.045em; font-size: clamp(2rem, 4.25vw, 3.35rem); line-height: 1.16; }
.intro h1 strong {
  display: block;
  margin: 2px 0 10px -.05em;
  font: 850 clamp(6.1rem, 12vw, 10.1rem)/.99 var(--digits);
  color: var(--mint);
  letter-spacing: -.09em;
  text-shadow: 0 0 56px #a7efda38;
}
.intro p { margin: 0; font-size: 1rem; color: var(--muted); }
.room-entry { margin-top: 27px; }
.mode-picker {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  min-width: 0;
  margin: 0 0 12px;
  padding: 0;
  border: 0;
}
.mode-picker legend { padding: 0; margin-bottom: 7px; color: var(--muted); font-size: .875rem; }
.mode-picker label { position: relative; display: grid; margin: 0; cursor: pointer; }
.mode-picker input { position: absolute; width: 1px; height: 1px; min-height: 0; opacity: 0; }
.mode-picker label > span {
  display: block;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #211a33;
}
.mode-picker label:hover > span { border-color: var(--lilac); }
.mode-picker input:checked + span { background: #302749; border-color: var(--lilac); }
.mode-picker input:focus-visible + span { outline: 2px solid var(--mint); outline-offset: 3px; }
.mode-picker strong { color: var(--ink); display: block; }
.mode-picker small { font-size: .8125rem; }
.primary, .secondary {
  min-height: 47px;
  padding: 9px 15px;
  border: 1px solid transparent;
  border-radius: 11px;
  font-weight: 750;
  transition: background .17s ease, border-color .17s ease;
}
.primary { background: var(--mint); color: #132a2b; }
.primary:hover:not(:disabled) { background: #cbfae9; }
.secondary { background: #29233f; color: var(--ink); border-color: #4a4066; }
.secondary:hover:not(:disabled) { background: #3a3055; border-color: var(--lilac); }
#create { width: 100%; }
.divider { display: flex; align-items: center; gap: 14px; margin: 23px 0 16px; color: var(--muted); font-size: .875rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
label, .label { display: block; margin-bottom: 7px; color: var(--muted); font-size: .875rem; }
.join-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; }
input {
  width: 100%;
  min-height: 47px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #111023;
  color: var(--ink);
}
input::placeholder { color: #aaa2c0; }
.message { min-height: 1.35em; margin: 10px 0 0; color: var(--coral); font-size: .875rem; line-height: 1.45; }

.room-heading { display: flex; flex-wrap: wrap; align-items: baseline; gap: 10px 16px; margin-bottom: 21px; }
.room h1 { font-size: clamp(1.75rem, 3.4vw, 2.8rem); }
.round { font-size: .875rem; color: var(--muted); }
.seats { display: grid; gap: 8px; }
.seat {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  min-height: 66px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: #1d1930;
}
.seat.active { border-color: var(--lilac); background: #28213c; }
.seat:first-child.active { border-color: var(--coral); }
.avatar { width: 35px; height: 35px; display: grid; place-items: center; border-radius: 9px; }
.coral { background: var(--coral); color: #612d45; }
.lilac { background: var(--lilac); color: #3d2d6b; }
.avatar i { width: 20px; height: 15px; border: 2px solid; border-top: 0; border-radius: 4px 4px 11px 11px; }
.avatar i::before, .avatar i::after { content: ""; width: 3px; height: 4px; border-radius: 3px; background: currentColor; display: block; float: left; margin: 1px 2px 0 2px; }
.seat small { display: block; color: var(--muted); margin-top: 2px; font-size: .8125rem; }
.seat-turn { visibility: hidden; font-size: .8125rem; color: var(--lilac); }
.seat:first-child .seat-turn { color: var(--coral); }
.seat.active .seat-turn { visibility: visible; }
.invite { margin-top: 18px; padding: 15px; border: 1px dashed #615480; border-radius: 13px; background: #171429; }
.invite code { display: block; overflow-wrap: anywhere; margin: 0 0 12px; color: var(--mint); user-select: all; font-size: clamp(.86rem, 1.8vw, 1.08rem); letter-spacing: .02em; }
.invite .secondary { width: 100%; min-height: 42px; font-size: .9rem; }
.invite p { color: var(--muted); margin: 10px 0 0; font-size: .875rem; }
.room-actions { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 14px; margin-top: 12px; }
.text-button {
  padding: 7px 0;
  min-height: 36px;
  color: var(--lilac);
  font-size: .875rem;
  text-align: start;
  background: transparent;
  border: 0;
}
.text-button:hover:not(:disabled) { text-decoration: underline; }
#leave { color: var(--muted); }

.game {
  width: 100%;
  max-width: 520px;
  justify-self: end;
  border: 1px solid #473e5e;
  border-radius: 28px;
  background: linear-gradient(145deg, #29233e, #1c1932 70%);
  padding: clamp(12px, 2vw, 22px);
  box-shadow: 0 24px 65px #09081880, inset 0 1px #f5f1ff12;
}
.board-picker { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; margin-bottom: 10px; padding: 3px; border: 1px solid var(--line); border-radius: 10px; background: #16132c; }
.board-picker button { min-height: 32px; padding: 5px; border-radius: 7px; text-align: center; }
.board-picker [aria-pressed="true"] { background: #3a3055; color: var(--ink); }
.scoreboard { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-bottom: 13px; }
.scoreboard > div { display: grid; gap: 3px; justify-items: center; padding: 7px 3px 8px; background: #16132c; border: 1px solid #39304f; border-radius: 12px; }
.scoreboard span { font-size: .875rem; color: var(--muted); white-space: nowrap; }
.scoreboard strong { font: 760 clamp(1.2rem, 2.65vw, 1.8rem)/1.1 var(--digits); font-variant-numeric: tabular-nums; }
.board {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  aspect-ratio: 1;
  gap: clamp(5px, 1.05vw, 11px);
  padding: clamp(6px, 1.05vw, 11px);
  border: 1px solid #443a61;
  background: #161329;
  border-radius: 15px;
  touch-action: none;
  user-select: none;
  overflow: hidden;
  container-type: inline-size;
}
.tile {
  display: grid;
  place-items: center;
  border-radius: clamp(5px, 1.15vw, 12px);
  min-width: 0;
  background: #27213c;
  box-shadow: inset 0 1px #b6a1ff13;
  font: 800 clamp(1.05rem, 8cqi, 2.9rem)/1 var(--digits);
  letter-spacing: -.05em;
  color: #20182f;
}
.tile.filled { box-shadow: inset 0 1px #ffffff53, inset 0 -3px #26113c26; }
.tile.v2 { background: #e8e0fb; }
.tile.v4 { background: #cbb9ff; }
.tile.v8 { background: #a88af9; }
.tile.v16 { background: #ffad9e; }
.tile.v32 { background: #ff957f; }
.tile.v64 { background: #f47da4; }
.tile.v128 { background: #f6c78e; }
.tile.v256 { background: #ffdf99; }
.tile.v512 { background: #b4dfbd; }
.tile.v1024 { background: #7de0c4; }
.tile.v2048 { background: #d0ffe9; box-shadow: inset 0 0 0 2px #fff, 0 0 18px #a7efdaa6; }
.tile.long { font-size: clamp(.9rem, 6.5cqi, 2.15rem); }
.tile.spawned { animation: settle .26s ease-out both; }
@keyframes settle {
  from { transform: scale(.76); opacity: .54; }
  to { transform: scale(1); opacity: 1; }
}
.game-footer { min-height: 55px; margin-top: 13px; display: flex; justify-content: space-between; align-items: center; gap: 9px; }
.game-status { min-width: 0; display: flex; align-items: center; gap: 10px; }
.turn-mark {
  flex: 0 0 auto;
  display: block;
  width: 13px;
  height: 13px;
  background: var(--mint);
  border-radius: 4px;
  transform: rotate(45deg);
}
.turn-mark[data-seat="0"] { background: var(--coral); }
.turn-mark[data-seat="1"] { background: var(--lilac); }
.game-status strong { font-size: .96rem; }
.game-status p { margin: 3px 0 0; font-size: .8125rem; color: var(--muted); }
.dpad { display: grid; flex: 0 0 auto; grid-template: repeat(2, 36px) / repeat(3, 37px); gap: 3px; }
.dpad button { display: grid; place-items: center; padding: 0; background: #30274a; color: var(--ink); border: 1px solid #55466d; border-radius: 7px; }
.dpad button:first-child { grid-column: 2; }
.dpad button:nth-child(2) { grid-column: 1; }
.dpad button:hover:not(:disabled) { background: #59497c; }
.dpad span { width: 8px; height: 8px; border: solid currentColor; border-width: 2px 0 0 2px; transform: translateY(2px) rotate(45deg); }
.dpad [data-direction="left"] span { transform: translateX(2px) rotate(-45deg); }
.dpad [data-direction="down"] span { transform: translateY(-2px) rotate(225deg); }
.dpad [data-direction="right"] span { transform: translateX(-2px) rotate(135deg); }
.help {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  padding: 17px 0 22px;
  gap: 5px 11px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: .875rem;
  line-height: 1.6;
}
.help span { width: 4px; height: 4px; border-radius: 50%; background: #9384ae; }

dialog {
  width: min(100% - 28px, 410px);
  color: var(--ink);
  background: #211a36;
  border: 1px solid #55466d;
  border-radius: 17px;
  padding: 24px;
  box-shadow: 0 18px 70px #0b061eaa;
}
dialog::backdrop { background: #100b22c5; }
dialog h2 { margin: 0 0 8px; font-size: 1.3rem; }
dialog p { color: var(--muted); line-height: 1.6; }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: end; margin-top: 20px; }

@media (max-width: 760px) {
  .shell { width: min(100% - 34px, 520px); }
  .masthead { min-height: 62px; }
  .brand { font-size: 1.15rem; }
  .brand img { width: 34px; height: 34px; }
  .layout { display: flex; flex-direction: column; align-items: stretch; gap: 16px; padding: 20px 0; }
  .intro h1 { font-size: clamp(1.6rem, 4.5vw, 2.4rem); }
  .intro h1 strong { display: inline-block; margin: 0 0 0 4px; font-size: clamp(2.7rem, 8vw, 4.1rem); }
  .intro p { margin-top: 6px; }
  .room-entry { margin-top: 20px; }
  .divider { margin: 12px 0 8px; }
  .message:empty { margin: 0; min-height: 0; }
  .game { border-radius: 20px; }
  .dpad { grid-template: repeat(2, 42px) / repeat(3, 42px); }
  .layout[data-playing="true"] .game { order: -1; }
  .room-heading { margin-bottom: 10px; }
}

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