:root {
  --bg: #1a1b2e;
  --board: #151a2e;
  --orange: #d97757;
  --orange-dark: #a9502f;
  --text: #f3e9e2;
  --muted: #9d97b0;
  --line: #34364f;
  --font: 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', system-ui, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
}

body {
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

.app {
  height: 100vh;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 12px) 14px calc(env(safe-area-inset-bottom) + 10px);
}

.title {
  grid-column: 2;
  white-space: nowrap;
  text-align: center;
  color: var(--orange);
  font-size: 28px;
  font-weight: 900;
  letter-spacing: 6px;
  text-shadow: 3px 3px 0 #0d0e1a;
}

.title-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 6px;
}

.player-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 4px;
}


#join-btn { flex: none; margin-left: auto; min-height: 32px; color: #efad90; text-decoration: none; }
.round-status { flex: 1; min-width: 0; font-size: 11px; line-height: 1.5; color: #b5aec5; }
.round-status.connection-error { color: #edd1a5; }
.first-drop { position: absolute; top: 43%; left: 8px; right: 8px; text-align: center; pointer-events: none; }
.first-drop b { font-size: clamp(12px, 3.5vw, 17px); }
.first-drop p { font-size: 11px; line-height: 1.6; color: #b5aec5; margin-top: 8px; }
.guide { flex: none; display: flex; align-items: flex-start; gap: 8px; padding: 9px 11px; background: #25283f; border: 1px solid #494354; border-radius: 7px; }
.guide div { flex: 1; }
.guide b { color: #edb298; font-size: 12px; }
.guide p { font-size: 12px; line-height: 1.5; margin-top: 3px; color: #d5cfde; }
.guide button { min-height: 32px; white-space: nowrap; text-decoration: none; }
.local-result { font-size: 12px; line-height: 1.6; margin: 12px 0; color: #e5d9cf; }
.pause-cover { position: absolute; inset: 0; display: grid; place-items: center; padding: 12px; background: #101526d6; cursor: default; }
.pause-panel { width: 100%; max-width: 290px; max-height: 100%; overflow-y: auto; padding: 20px 16px; text-align: center; background: #24273c; border: 1px solid #9d715c; border-radius: 9px; }
.pause-panel h2 { font-size: 25px; color: #e9a789; }
.pause-panel p { font-size: 12px; line-height: 1.7; color: #c3b9ca; margin: 10px 0; }
.pause-panel .btn { width: 100%; min-height: 44px; margin-top: 8px; }
.pause-panel .link-btn { margin-top: 10px; min-height: 36px; }
.retained-progress { padding: 12px; background: #171b2c; border-radius: 6px; width: 100%; font-size: 13px; line-height: 1.7; }
.restart-choice { min-height: 44px; width: 100%; margin-top: 10px; }
.danger-btn { background: #a54f40; box-shadow: 0 3px #74382e; }
.save-row { display: flex; gap: 8px; align-items: center; justify-content: center; flex: none; }
.save-status { font-size: 11px; line-height: 1.5; color: #b5aec5; }
.save-status.warning { color: #efc997; }
#pending-btn { flex: none; min-height: 32px; color: #efad90; }
.player-row:has(#pending-btn:not(.hidden)) { flex-wrap: wrap; }
.pending-note { font-size: 11px; color: #e5caab; line-height: 1.5; padding: 5px 8px; background: #292a3e; border-radius: 5px; }
.upload-card { margin-top: 12px; padding: 12px; border: 1px solid #55516b; background: #24283d; border-radius: 7px; text-align: left; }
.upload-card strong { display: block; font-size: 14px; }
.upload-card p { font-size: 12px; line-height: 1.7; margin-top: 6px; color: #c3bbcf; }
.upload-card .btn { margin-top: 9px; width: 100%; min-height: 38px; font-size: 12px; }
.upload-card[data-tone="success"] { border-color: #538875; }
.upload-card[data-tone="success"] strong { color: #9cd9b9; }
.upload-card[data-tone="warning"] strong { color: #f0cc9e; }
.pending-sheet { width: min(390px, 100%); }
.pending-list { list-style: none; width: 100%; text-align: left; }
.pending-list li { padding: 12px 0; border-bottom: 1px solid var(--line); }
.pending-list b { font-size: 19px; }
.pending-list p { font-size: 12px; line-height: 1.7; color: #c3bbcf; overflow-wrap: anywhere; }
.pending-list button { margin: 8px 8px 0 0; min-height: 38px; }
.board:has(.overlay:not(.hidden)) { touch-action: pan-y; }
#restore-open { flex: none; min-height: 32px; color: #efad90; }
.restore-sheet { position: relative; width: min(360px, 100%); }
.restore-sheet h2 { padding: 8px 14px 0; }
.restore-sheet .sheet-sub { line-height: 1.7; }
.save-card { width: 100%; padding: 14px; margin: 8px 0; background: #171c2e; border: 1px solid #42445b; border-radius: 7px; }
.save-card > b { font-size: 32px; color: #f0bc9d; }
.save-card p { font-size: 12px; line-height: 1.7; color: #c3bbcf; }
.restore-pieces { display: flex; align-items: center; justify-content: center; gap: 10px; margin: 10px 0; font-size: 11px; color: #c3bbcf; }
.btn.ghost.danger-btn { color: white; background: #a54f40; box-shadow: 0 3px #74382e; }
.game-paused .board, .game-paused .board *, .game-paused #score, .game-paused .claw-btn { animation-play-state: paused; }
.btn:disabled { opacity: 0.5; cursor: default; }
#next-round-note { font-size: 11px; line-height: 1.5; }
.help-list { text-align: left; padding-left: 20px; font-size: 14px; line-height: 1.8; }
.help-list li { margin: 8px 0; }
#name-form { position: relative; }
#name-offline { min-height: 40px; }
button:focus-visible, input:focus-visible, .board:focus-visible { outline: 3px solid #f5cb7a; outline-offset: 3px; }

.player-chip {
  display: flex;
  align-items: center;
  gap: 5px;
  max-width: 100%;
  padding: 3px 8px 3px 3px;
  font: inherit;
  font-size: 12px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  border-radius: 999px;
  cursor: pointer;
}

.player-chip .chip-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-chip .avatar,
.player-chip .chip-dot {
  flex: none;
  width: 18px;
  height: 18px;
}

.chip-dot {
  border-radius: 50%;
  background: var(--orange);
}

.avatar {
  border-radius: 50%;
  object-fit: cover;
  background: #2a2d45;
}

.avatar.big {
  width: 64px;
  height: 64px;
}

.tag {
  color: var(--muted);
  font-size: 0.85em;
}

.ld-badge {
  display: inline-block;
  margin-left: 4px;
  padding: 0 4px;
  font-size: 10px;
  font-weight: 800;
  line-height: 15px;
  vertical-align: 1px;
  color: #1c1c1e;
  background: #ffb003;
  border-radius: 4px;
  text-shadow: none;
}

.rank-btn {
  grid-column: 3;
  justify-self: end;
  color: #f7c948;
  border-color: #6b5a2a;
}

.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
}

.score-line {
  font-size: 22px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

#score {
  display: inline-block;
}

#score.bump {
  animation: bump 0.25s ease-out;
}

.best {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.next-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

.actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn {
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  color: #fff;
  background: var(--orange);
  border: none;
  border-radius: 6px;
  padding: 8px 14px;
  box-shadow: 0 3px 0 var(--orange-dark);
  cursor: pointer;
  white-space: nowrap;
}

.btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--orange-dark);
}

.btn.big {
  font-size: 18px;
  padding: 12px 28px;
  margin-top: 22px;
}

.icon-btn {
  position: relative;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  color: var(--muted);
  background: transparent;
  border: 2px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
}

.claw-btn {
  color: #8fe3ff;
  border-color: #3b6f86;
}

.claw-btn:disabled {
  color: var(--line);
  border-color: var(--line);
  cursor: default;
}

.claw-btn.active {
  color: #10131f;
  background: #8fe3ff;
  border-color: #8fe3ff;
  animation: pulse 0.8s ease-in-out infinite;
}

.badge {
  position: absolute;
  top: -7px;
  right: -7px;
  min-width: 17px;
  height: 17px;
  padding: 0 4px;
  border-radius: 9px;
  font-size: 11px;
  font-weight: 800;
  line-height: 17px;
  color: #10131f;
  background: #8fe3ff;
}

.claw-btn:disabled .badge {
  display: none;
}

.icon-btn.muted .waves,
.icon-btn:not(.muted) .mute {
  display: none;
}

.stage {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px;
}

.board {
  position: relative;
  border-radius: 6px;
  overflow: hidden;
  background: var(--board);
  box-shadow:
    0 0 0 3px var(--orange),
    0 12px 30px rgba(0, 0, 0, 0.45);
  touch-action: none;
  cursor: crosshair;
  transition: box-shadow 0.3s;
}

.board.fever {
  box-shadow:
    0 0 0 3px #ffd84d,
    0 0 26px rgba(255, 190, 80, 0.55);
  animation: fever-glow 0.6s ease-in-out infinite alternate;
}

.board.claw-mode {
  cursor: pointer;
  box-shadow:
    0 0 0 3px #8fe3ff,
    0 12px 30px rgba(0, 0, 0, 0.45);
}

.board canvas {
  display: block;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: rgba(13, 16, 39, 0.88);
  padding: 10px;
}

.hidden {
  display: none !important;
}

.panel {
  animation: pop 0.4s cubic-bezier(0.2, 1.6, 0.4, 1);
  width: 100%;
  max-height: 100%;
  overflow-y: auto;
  padding: 10px 4px;
  overscroll-behavior: contain;
}

.panel .over-title { font-size: clamp(22px, 5vw, 32px); margin-bottom: 12px; }
.panel .panel-actions { flex-wrap: wrap; }
.panel .btn.big { font-size: 14px; padding: 10px 14px; margin-top: 14px; }

.over-title {
  font-size: 42px;
  font-weight: 900;
  color: var(--orange);
  text-shadow: 3px 3px 0 #0d0e1a;
  margin-bottom: 18px;
}

.final {
  font-size: 20px;
}

.final b {
  font-variant-numeric: tabular-nums;
}

.final-sub {
  margin-top: 6px;
  font-size: 14px;
  color: var(--muted);
}

.final-rank {
  margin-top: 10px;
  font-size: 15px;
  font-weight: 700;
  color: #8fe3ff;
}

.final-rank.error {
  font-weight: 400;
  font-size: 13px;
  color: var(--muted);
}

.panel-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.btn.ghost {
  color: var(--orange);
  background: transparent;
  box-shadow: inset 0 0 0 2px var(--orange);
}

.link-btn {
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  background: none;
  border: none;
  text-decoration: underline;
  cursor: pointer;
}

/* ---------- dialogs ---------- */

.linuxdo-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #f0f0f0;
  background: #1c1c1e;
  box-shadow:
    inset 0 0 0 2px #ffb003,
    0 3px 0 #0b0b0c;
}

.linuxdo-btn:active {
  box-shadow:
    inset 0 0 0 2px #ffb003,
    0 1px 0 #0b0b0c;
}

.ld-logo {
  flex: none;
}

.divider {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  margin: 6px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--line);
}

.account-sheet {
  position: relative;
}

.sheet-close {
  position: absolute;
  top: 10px;
  right: 10px;
}

.guest-only {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  margin-top: 6px;
}

.rename-row {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}

.rename-row input {
  margin-top: 0;
  min-width: 0;
}

.toast {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 14px);
  z-index: 20;
  max-width: calc(100% - 32px);
  padding: 9px 16px;
  font-size: 14px;
  color: var(--text);
  background: #20223a;
  border-radius: 999px;
  box-shadow:
    0 0 0 2px var(--orange),
    0 8px 24px rgba(0, 0, 0, 0.45);
  transform: translateX(-50%);
  animation: toast-in 0.3s ease-out;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, -12px);
  }
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(8, 9, 20, 0.72);
}

.sheet {
  width: min(340px, 100%);
  max-height: 100%;
  overflow-y: auto; /* short screens (landscape phones): scroll instead of clipping */
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 22px 20px 18px;
  text-align: center;
  background: #20223a;
  border-radius: 10px;
  box-shadow:
    0 0 0 3px var(--orange),
    0 20px 50px rgba(0, 0, 0, 0.5);
  animation: pop 0.35s cubic-bezier(0.2, 1.6, 0.4, 1);
}

.sheet h2 {
  font-size: 22px;
  color: var(--orange);
  text-shadow: 2px 2px 0 #0d0e1a;
}

.sheet-sub,
.hint {
  font-size: 12px;
  color: var(--muted);
}

.sheet input {
  width: 100%;
  margin-top: 6px;
  padding: 11px 12px;
  font: inherit;
  font-size: 17px; /* ≥16px keeps iOS from zooming in */
  text-align: center;
  color: var(--text);
  background: #151a2e;
  border: 2px solid var(--line);
  border-radius: 8px;
  outline: none;
  -webkit-user-select: text;
  user-select: text;
}

.sheet input:focus {
  border-color: var(--orange);
}

.sheet .error {
  min-height: 1.2em;
  font-size: 13px;
  color: #ff7b7b;
}

.sheet .btn.big {
  margin-top: 4px;
  width: 100%;
}

.rank-sheet {
  width: min(380px, 100%);
  height: min(560px, 100%);
  align-items: stretch;
  padding: 16px 14px 12px;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.rank-list {
  flex: 1;
  min-height: 0;
  margin-right: -6px;
  padding-right: 6px; /* room for the scrollbar so scores never get clipped */
  overflow-y: auto;
  overscroll-behavior: contain;
  list-style: none;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: rgba(217, 119, 87, 0.55) transparent;
}

.rank-list::-webkit-scrollbar {
  width: 6px;
}

.rank-list::-webkit-scrollbar-track {
  background: transparent;
}

.rank-list::-webkit-scrollbar-thumb {
  background: rgba(217, 119, 87, 0.45);
  border-radius: 3px;
}

.rank-list::-webkit-scrollbar-thumb:hover {
  background: rgba(217, 119, 87, 0.75);
}

.rank-count {
  margin-top: 2px;
  font-size: 12px;
  text-align: left;
  color: var(--muted);
}

.rank-list li.more {
  display: block;
  padding: 12px 0 6px;
  text-align: center;
  border: none;
}

.rank-list li,
.rank-me {
  min-height: 42px;
  display: grid;
  grid-template-columns: 30px 22px minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 8px;
  padding: 7px 6px;
  font-size: 14px;
  text-align: left;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.rank-list .status {
  display: block;
  padding: 40px 0;
  text-align: center;
  color: var(--muted);
  border: none;
}

.rank-no {
  font-weight: 900;
  text-align: center;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.rank-list li:nth-child(1) .rank-no {
  color: #f7c948;
}

.rank-list li:nth-child(2) .rank-no {
  color: #cfd6e6;
}

.rank-list li:nth-child(3) .rank-no {
  color: #d9905a;
}

.rank-list .avatar,
.rank-list .chip-dot,
.rank-me .avatar,
.rank-me .chip-dot {
  width: 22px;
  height: 22px;
}

.rank-list .chip-dot,
.rank-me .chip-dot {
  opacity: 0.35;
}

.rank-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rank-score {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.rank-list li.me,
.rank-me {
  background: rgba(217, 119, 87, 0.16);
  border-radius: 6px;
}

.rank-me {
  margin-top: 8px;
  padding-top: 20px;
  position: relative;
  border: none;
  box-shadow: 0 0 0 1px rgba(217, 119, 87, 0.45);
}

.rank-me.clickable {
  cursor: pointer;
}

.rank-me-label {
  position: absolute;
  top: 3px;
  left: 8px;
  font-size: 11px;
  color: var(--orange);
}

.rank-me:has(.rank-me-label:only-child) {
  display: block;
  padding: 12px 8px;
}

.rank-me .rank-me-label:only-child {
  position: static;
  color: var(--muted);
}

.rank-list li.flash {
  animation: rank-flash 1.2s ease-out;
}

@keyframes rank-flash {
  0%,
  40% {
    background: rgba(255, 216, 77, 0.35);
  }
}

body.modal-open .board {
  pointer-events: none;
}

.new-best {
  margin-top: 10px;
  color: #f7c948;
  font-weight: 800;
  letter-spacing: 2px;
  animation: blink 0.8s steps(2) infinite;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px 10px;
  font-size: 11px;
  color: var(--muted);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 3px;
  opacity: 0.55;
  transition: opacity 0.3s;
}

.legend-item.locked {
  opacity: 0.35;
}

.legend-item.reached {
  opacity: 1;
  color: var(--text);
}

@keyframes bump {
  40% {
    transform: scale(1.25);
    color: #ffd6b8;
  }
}

@keyframes pop {
  from {
    transform: scale(0.6);
    opacity: 0;
  }
}

@keyframes pulse {
  50% {
    transform: scale(1.08);
  }
}

@keyframes fever-glow {
  to {
    box-shadow:
      0 0 0 3px #ff9f43,
      0 0 40px rgba(255, 190, 80, 0.8);
  }
}

@keyframes blink {
  50% {
    opacity: 0.3;
  }
}

.credit {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 11px;
  color: var(--muted);
  opacity: 0.8;
}

.credit a {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px; /* comfortable tap target around the 14px icon */
  color: var(--muted);
  border-radius: 50%;
  transition: color 0.2s, background 0.2s;
}

.credit a:hover,
.credit a:focus-visible {
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
}

.credit a.migration-link {
  width: auto;
  padding: 0 4px;
  white-space: nowrap;
  border-radius: 4px;
  text-decoration: none;
}

/* Keep the controls readable rather than squeezing labels on narrow phones. */
@media (max-width: 480px) {
  .hud { display: grid; grid-template-columns: 1fr auto; }
  .actions { grid-column: 1 / -1; justify-content: flex-end; }
  .next-box { flex-direction: row; gap: 8px; }
}

@media (max-width: 380px) {
  .app {
    padding-left: 10px;
    padding-right: 10px;
  }
  .score-line {
    font-size: 19px;
  }
  .actions {
    gap: 6px;
  }
  .icon-btn {
    width: 32px;
    height: 32px;
  }
  .btn {
    font-size: 13px;
    padding: 7px 10px;
  }
}

@media (max-width: 340px) {
  .score-label {
    display: inline;
  }
}

@media (max-height: 520px) {
  .guide { padding: 5px 8px; }
  .guide b { display: none; }
  .first-drop p { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .sheet, .panel, .toast { animation: none; }
}

@media (max-height: 640px) {
  .title {
    font-size: 22px;
  }
  .app {
    gap: 6px;
  }
  .legend {
    display: none;
  }
}

@media (max-height: 520px) {
  #name-crab,
  #acc-crab {
    display: none;
  }
  .modal {
    padding: 10px;
  }
  .sheet {
    gap: 6px;
    padding: 14px 16px 12px;
  }
}

/* Landscape phones: HUD in a column on the left, board fills the height. */
@media (orientation: landscape) and (max-height: 540px) {
  .app {
    max-width: none;
    flex-direction: row;
    align-items: stretch;
    gap: 16px;
    padding: calc(env(safe-area-inset-top) + 8px) calc(env(safe-area-inset-right) + 12px)
      calc(env(safe-area-inset-bottom) + 8px) calc(env(safe-area-inset-left) + 12px);
  }
  .top {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-end;
    gap: 10px;
  }
  .title {
    font-size: 22px;
    letter-spacing: 3px;
  }
  .hud {
    flex-direction: column;
    align-items: flex-end;
    gap: 12px;
  }
  .score-box {
    text-align: right;
  }
  .stage {
    flex: 0 0 auto;
    width: auto;
    aspect-ratio: 400 / 640;
  }
  .credit {
    position: fixed;
    left: calc(env(safe-area-inset-left) + 12px);
    bottom: calc(env(safe-area-inset-bottom) + 6px);
  }
  .legend {
    display: flex;
    flex: 1;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: flex-start;
    overflow: hidden;
    gap: 2px;
  }
  .app:has(.guide:not(.hidden)) .legend { display: none; }
  .guide { flex: 1; align-self: center; flex-direction: column; min-width: 100px; max-width: 190px; }
  .guide div { flex: none; }
  .save-row { position: fixed; left: 12px; bottom: 34px; max-width: 33vw; }
}
