* {
  box-sizing: border-box;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

input, textarea {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

html, body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

html:has(#screen-game:not([hidden])),
body:has(#screen-game:not([hidden])) {
  height: 100%;
  overflow: hidden;
}

body[data-motion="reduce"] *,
body[data-motion="reduce"] *::before,
body[data-motion="reduce"] *::after {
  animation: none !important;
  transition: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

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

.screen.home, .screen.settings {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(16px, env(safe-area-inset-top)) 16px max(20px, env(safe-area-inset-bottom));
  background:
    radial-gradient(ellipse at 50% 22%, var(--glow), transparent 52%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg-deep) 100%);
}

.home-inner, .settings-inner {
  width: min(420px, 100%);
  text-align: center;
}

.logo {
  margin: 0 auto 4px;
}

.logo-img {
  width: min(280px, 70vw);
  height: auto;
  border-radius: 36px;
  filter: drop-shadow(0 0 28px var(--glow));
  border: var(--chrome-border) solid var(--stroke);
}

h1 {
  margin: 0 0 8px;
  font-size: 2.4rem;
  letter-spacing: 0.02em;
  color: var(--text);
}

.home-best, .home-progress {
  margin: 0 0 8px;
  font-weight: 800;
  font-size: 1.15rem;
}

.home-best { color: var(--gold); }

.home-progress {
  color: var(--muted);
  margin-bottom: 8px;
}

.level-nav {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin: 0 0 16px;
}

.stars { letter-spacing: 2px; font-size: 22px; margin: 0 0 8px; }
.stars i { color: #8a7560; font-style: normal; }
.stars i.on { color: var(--gold); }
.stars.live { font-size: 28px; margin: 6px 0 0; }
.stars.big { font-size: 40px; text-align: center; margin: 0 0 8px; }
.star-note {
  margin: 2px 16px 0;
  font-weight: 700;
  font-size: 0.95rem;
  line-height: 1.3;
  color: var(--gold);
}

.goal-chip {
  margin: 4px 0 0;
  color: var(--muted);
  font-weight: 700;
  font-size: 1rem;
}

.home-actions {
  display: grid;
  gap: 12px;
}

.install-tip, .build-mark {
  margin: 18px 0 0;
  font-weight: 700;
  color: var(--muted);
}

.build-mark { color: var(--gold); }

.play-btn, .ghost-btn {
  font: inherit;
  font-weight: 800;
  min-height: 56px;
  border-radius: 18px;
  cursor: pointer;
  width: 100%;
  letter-spacing: 0.01em;
}

.play-btn {
  border: var(--btn-border) solid var(--btn-primary-border);
  background: var(--btn-primary-bg);
  color: var(--btn-primary-fg);
  font-size: 1.35rem;
  box-shadow: 0 5px 0 var(--btn-primary-shadow);
}

.ghost-btn {
  border: var(--btn-border) solid var(--btn-secondary-border);
  background: var(--btn-secondary-bg);
  color: var(--btn-secondary-fg);
  font-size: 1.15rem;
}

a.ghost-btn {
  display: grid;
  place-items: center;
  text-decoration: none;
}

.play-btn:focus-visible,
.ghost-btn:focus-visible,
.icon-btn:focus-visible,
.ctrl:focus-visible,
.back-btn:focus-visible {
  outline: var(--focus);
  outline-offset: 4px;
}

.settings-inner {
  display: grid;
  gap: 12px;
  text-align: left;
  background: var(--surface);
  border: var(--chrome-border) solid var(--stroke);
  border-radius: 20px;
  padding: 20px;
}

.settings-inner h1 { text-align: center; }

.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  font-weight: 800;
  color: var(--text);
}

.setting-note {
  color: var(--muted);
  font-size: 0.95rem;
  margin: 4px 0 8px;
}

.screen.game {
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: max(8px, env(safe-area-inset-top)) 12px max(10px, env(safe-area-inset-bottom));
  background:
    radial-gradient(ellipse at 50% 0%, var(--glow), transparent 48%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg-deep) 100%);
  touch-action: none;
}

.screen.game .score-big {
  margin: 2px 0;
  font-size: clamp(1.8rem, 7vw, 3rem);
}

.screen.game .board-panel {
  flex: 0 1 auto;
  min-height: 0;
  width: min(560px, 100%);
  align-items: center;
  justify-content: center;
  container-type: inline-size;
  --board-cell: var(--cell);
}

.screen.game::after {
  content: "";
  flex: 1 1 auto;
  min-height: 0;
}

.screen.game .grid {
  height: max-content;
  align-content: start;
  --fit: calc((min(100cqw, 100dvh - 250px) - 81px) / 10);
  --cell: max(16px, min(var(--board-cell), var(--fit)));
}

.screen.game .hold-hint,
.screen.game .tray-slots,
.screen.game .finish-btn,
.screen.game .game-bar {
  flex: 0 0 auto;
}

.finish-btn {
  width: min(560px, 100%);
  margin-top: 8px;
}

.game-bar {
  width: min(560px, 100%);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.back-btn {
  font: inherit;
  font-weight: 800;
  min-height: 44px;
  min-width: 88px;
  padding: 8px 16px;
  border: var(--btn-border) solid var(--btn-secondary-border);
  border-radius: 12px;
  background: var(--btn-secondary-bg);
  color: var(--btn-secondary-fg);
  cursor: pointer;
}

.best-chip, .level-chip, .combo-chip {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.best-chip {
  color: var(--gold);
  font-size: 1.2rem;
  display: flex;
  gap: 6px;
  align-items: center;
}

.game-tools { display: flex; gap: 8px; }

.icon-btn {
  width: 48px;
  height: 48px;
  border: var(--btn-border) solid var(--btn-secondary-border);
  border-radius: 50%;
  background: var(--icon-bg);
  color: var(--icon-fg);
  font-size: 1.2rem;
  cursor: pointer;
}

.icon-btn:disabled {
  opacity: 0.45;
}

.score-big {
  margin: 8px 0 4px;
  font-size: clamp(3rem, 10vw, 5.5rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--text);
}

.combo-chip {
  margin: 0 0 6px;
  color: var(--combo);
}

.level-chip {
  margin: 10px 0 0;
  font-size: 1.25rem;
  letter-spacing: 0.04em;
  color: var(--diff-relax);
}

.level-chip[data-difficulty="easy"],
.home-progress[data-difficulty="easy"] { color: var(--diff-easy); }

.level-chip[data-difficulty="normal"],
.home-progress[data-difficulty="normal"] { color: var(--diff-normal); }

.level-chip[data-difficulty="hard"],
.home-progress[data-difficulty="hard"] { color: var(--diff-hard); }

.level-chip[data-difficulty="superhard"],
.home-progress[data-difficulty="superhard"] { color: var(--diff-super); }

.level-chip[data-difficulty="relax"] { color: var(--diff-relax); }

.stars {
  letter-spacing: 3px;
  font-size: 1.5rem;
  margin: 0 0 8px;
}

.stars i {
  color: #8ea0c8;
  font-style: normal;
}

.stars i.on { color: var(--gold); }

.stars.big {
  font-size: 2.6rem;
  text-align: center;
  margin: 0 0 8px;
}

.stars.live {
  font-size: 1.7rem;
  margin: 2px 0 0;
  text-align: center;
}

.star-note {
  margin: 0 16px 4px;
  text-align: center;
  font-weight: 800;
  color: var(--gold);
  font-size: 0.95rem;
}

.level-nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 0 0 10px;
}

.hold-hint {
  margin: 10px 12px 0;
  max-width: 22rem;
  text-align: center;
  font-weight: 800;
  color: var(--gold);
}

.place-bar {
  width: min(560px, 100%);
  margin-top: 12px;
  display: grid;
  gap: 10px;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.pad-btn {
  font: inherit;
  font-weight: 800;
  min-height: 52px;
  border-radius: 12px;
  border: var(--btn-border) solid var(--btn-secondary-border);
  background: var(--btn-secondary-bg);
  color: var(--btn-secondary-fg);
  cursor: pointer;
}

.place-btn {
  margin: 0;
}

.board-panel {
  display: flex;
  justify-content: center;
  touch-action: none;
}

.tray-slots {
  margin-top: 18px;
  display: flex;
  justify-content: center;
  gap: 36px;
  flex-wrap: wrap;
  min-height: 110px;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.piece-card {
  appearance: none;
  background: var(--surface);
  color: var(--text);
  border: var(--btn-border) solid transparent;
  border-radius: 16px;
  min-width: 88px;
  min-height: 88px;
  padding: 8px;
  cursor: grab;
  display: grid;
  place-items: center;
  font: inherit;
  touch-action: none;
}

.piece-card[aria-pressed="true"] {
  border-color: var(--focus-ring);
}

.piece-card.is-held {
  opacity: 0.35;
}

.drag-ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 20;
  pointer-events: none;
  padding: 0;
  margin: 0;
  background: transparent;
  filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.4));
  will-change: left, top;
}

.drag-ghost .mini {
  gap: var(--ghost-gap, 5px);
}

.drag-ghost.miss .block {
  background: var(--nofit) !important;
}

.mini {
  display: grid;
  gap: 3px;
}

.mini span {
  display: block;
  width: 20px;
  height: 20px;
  border-radius: 5px;
}

.drag-ghost.mini span {
  width: var(--ghost-cell, var(--cell));
  height: var(--ghost-cell, var(--cell));
  border-radius: 7px;
}

button.ctrl, select.ctrl {
  font: inherit;
  font-weight: 800;
  min-height: 48px;
  padding: 8px 14px;
  border-radius: 10px;
  border: var(--btn-border) solid var(--btn-secondary-border);
  background: var(--btn-secondary-bg);
  color: var(--btn-secondary-fg);
  cursor: pointer;
}

dialog {
  border: var(--btn-border) solid var(--stroke);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
  max-width: 24rem;
  padding: 24px;
  text-align: center;
}

dialog::backdrop { background: rgba(0, 0, 0, 0.62); }

dialog h2 { margin-top: 0; color: var(--text); }

dialog.help-dialog {
  background: #001a4d;
  color: #ffe566;
  border: 4px solid #ffe566;
  text-align: left;
  max-width: 28rem;
  max-height: min(82vh, 680px);
  overflow: auto;
}

dialog.help-dialog h2,
dialog.help-dialog p,
dialog.help-dialog li,
dialog.help-dialog strong {
  color: #ffe566;
  font-weight: 700;
}

dialog.help-dialog ul {
  margin: 0;
  padding-left: 1.2rem;
}

dialog.help-dialog li { margin: 0 0 10px; }

dialog.help-dialog .play-btn {
  background: #001a4d;
  color: #ffe566;
  border: 3px solid #ffe566;
  font-weight: 700;
}

dialog .play-btn, dialog .ghost-btn { margin-top: 10px; }

.name-row {
  display: grid;
  gap: 6px;
  margin-top: 12px;
  font-weight: 800;
  text-align: left;
}

.name-row input {
  min-height: 48px;
  border-radius: 12px;
  border: 3px solid #ffe566;
  padding: 8px 12px;
  font: inherit;
  color: #ffffff;
  caret-color: #ffffff;
  background: #001a4d;
}

.name-row input::placeholder {
  color: #d6e2ff;
}

.end-note {
  color: var(--gold);
  font-weight: 800;
}

body.leaders {
  margin: 0;
  min-height: 100dvh;
  background: #1c3480;
  color: #fff;
}

.leaders-app {
  width: min(440px, 100%);
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
}

.leaders-bar {
  display: flex;
  align-items: center;
  gap: 12px;
}

.leaders-bar h1 {
  margin: 0;
  font-size: 2rem;
}

.leaders-back {
  display: grid;
  place-items: center;
  min-width: 72px;
  min-height: 44px;
  padding: 0 12px;
  border-radius: 12px;
  border: 3px solid #fff;
  background: #17307a;
  color: #fff;
  font-weight: 800;
  text-decoration: none;
}

.leaders-note, .leaders-status {
  font-weight: 800;
}

.leaders-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.leaders-list li {
  display: grid;
  grid-template-columns: 48px 1fr auto;
  align-items: center;
  gap: 8px;
  margin: 8px 0;
  padding: 12px;
  border-radius: 12px;
  border: 2px solid #0b1433;
  background: linear-gradient(180deg, #23409a, #17307a);
  color: #ffe566;
  font-weight: 800;
}

.leaders-list .rank, .leaders-list .score { color: #ffe566; }

.leaders-list .who {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaders-list .mine { border-color: #ffe566; }

.grid, .cell, .piece-card, .board-panel, .grid *, .cell *, .piece-card * {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
}

.play-btn, .ghost-btn, .back-btn, .icon-btn, .ctrl, .pad-btn {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

body.dragging {
  cursor: grabbing;
  overflow: hidden;
  touch-action: none;
}

body.dragging * {
  touch-action: none !important;
}

@media (max-width: 480px) {
  :root {
    --cell: clamp(24px, calc((100vw - 4.8rem) / 10), 34px);
  }

  html, body { font-size: 16px; }

  h1 { font-size: 1.85rem; }

  .logo-img {
    width: min(168px, 44vw);
    border-radius: 28px;
  }

  .score-big {
    margin: 4px 0 2px;
    font-size: clamp(2rem, 11vw, 3rem);
  }

  .level-chip { margin: 6px 0 0; font-size: 1.05rem; }

  .tray-slots {
    margin-top: 10px;
    gap: 14px;
    min-height: 0;
  }

  .mini span {
    width: 16px;
    height: 16px;
  }

  .piece-card {
    min-width: 72px;
    min-height: 72px;
  }
}
