:root {
  --bg-a: #000000;
  --bg-b: #000000;
  --card: #0a0a0a;
  --ink: #e8eef7;
  --muted: #9ba9bf;
  --line: #1e2430;
  --accent: #4ea1ff;
  --ok: #2ccf7a;
  --bad: #ff5d6c;
  --clear: #ffd166;
  --piece-color: #b76cff;
  --piece-border: #9d54ea;
  --piece-glow: #b76cff66;
  --tile-frame: #3a3d32;
  --preview-valid-fill: #b76cff88;
  --preview-invalid-fill: #ff5d6c66;
  --clear-preview-fill: #ffffff44;
  --clear-preview-border: #ffffffb0;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  height: 100svh;
  background: #000000;
  color: var(--ink);
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  overflow: hidden;
}

.app {
  max-width: 1360px;
  margin: 0 auto;
  padding: 12px;
  height: 100svh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar h1 { margin: 0; font-size: clamp(26px, 4vw, 38px); }

.stats {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.stat {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  padding: 8px 12px;
  min-width: 110px;
}
.auth-stat {
  min-width: 200px;
}
.auth-stat .secondary-btn {
  margin-top: 6px;
  width: 100%;
}
.auth-user {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.2;
}

.label {
  display: block;
  font-size: 12px;
  color: var(--muted);
}

.stat strong {
  display: block;
  line-height: 1.1;
}

.main-layout {
  margin-top: 10px;
  display: grid;
  grid-template-columns: minmax(300px, 1fr) minmax(280px, 440px);
  gap: 10px;
  flex: 1;
  min-height: 0;
}

.board-panel {
  background: #0a0a0a;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 10px;
  min-height: 0;
  display: grid;
  place-items: center;
}

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 3px;
  width: min(100%, calc(100svh - 130px), 760px);
  aspect-ratio: 1/1;
  margin: 0 auto;
}

.cell {
  border: 1px solid #1f2734;
  border-radius: 6px;
  background: #090c11;
  transition: 120ms ease;
  position: relative;
  overflow: hidden;
}

.cell.sub-a { background: #0a0d12; }
.cell.sub-b { background: #1e2229; }
.cell.filled {
  background: var(--fill-color, #edf2fa);
  border-color: var(--tile-frame);
  border-width: 1px;
  box-shadow: 0 0 8px color-mix(in srgb, var(--fill-color, #edf2fa) 45%, transparent),
              inset 0 0 0 1px var(--tile-frame);
}
.cell.preview-valid {
  background: var(--preview-valid-fill) !important;
  border-color: var(--piece-border) !important;
  box-shadow: inset 0 0 0 1px var(--piece-border);
}
.cell.preview-invalid {
  background: var(--preview-invalid-fill) !important;
  border-color: var(--bad) !important;
  box-shadow: inset 0 0 0 1px var(--bad);
}
.cell.clear-preview { box-shadow: inset 0 0 0 3px var(--clear); }
.cell.clear-preview {
  background: var(--clear-preview-fill) !important;
  border-color: var(--clear-preview-border) !important;
  box-shadow: inset 0 0 0 2px var(--clear-preview-border);
}

.side-panel {
  display: grid;
  gap: 10px;
  align-content: start;
  min-height: 0;
  overflow: auto;
  padding-right: 2px;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
}

.switch-row {
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 14px;
}

.mode-row {
  display: block;
  margin-top: 10px;
  margin-bottom: 4px;
  font-size: 13px;
  color: var(--muted);
}

.legal-link {
  margin: 10px 0 0;
  font-size: 12px;
}

.legal-link a {
  color: var(--muted);
  text-decoration: underline;
}

.legal-link a:hover {
  color: var(--ink);
}

select {
  width: 100%;
  border: 1px solid #2d384b;
  border-radius: 8px;
  padding: 8px;
  background: #080b10;
  color: var(--ink);
}

button {
  margin-top: 10px;
  border: 1px solid #2a5d95;
  border-radius: 10px;
  padding: 8px 12px;
  background: linear-gradient(180deg, #17385c, #102845);
  color: #dce9fb;
  cursor: pointer;
  font-weight: 700;
}

button:hover { filter: brightness(1.05); }

.pieces-tray {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.piece {
  border: 1px solid #283244;
  border-radius: 10px;
  background: #0b0f16;
  padding: 6px;
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 4px;
  min-height: 86px;
  user-select: none;
  touch-action: none;
  overflow: hidden;
}

.piece.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px #1f3c63;
}

.piece.used {
  opacity: 0.4;
  filter: grayscale(0.8);
}

.piece.unplaceable {
  opacity: 0.45;
  filter: saturate(0.7) brightness(0.85);
}

.piece.unplaceable.selected {
  opacity: 0.7;
}

.shape-grid {
  display: grid;
  gap: 3px;
  justify-content: center;
}

.shape-cell {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  background: transparent;
}

.shape-cell.on {
  background: var(--piece-color);
  border: 1px solid var(--tile-frame);
  box-shadow: 0 0 8px var(--piece-glow),
              inset 0 0 0 1px var(--tile-frame);
}

.cell.clear-anim {
  z-index: 2;
  animation: clearBurstDrop 420ms cubic-bezier(0.25, 0.65, 0.15, 1) forwards;
}

@keyframes clearBurstDrop {
  0% {
    transform: translate(0, 0) scale(1) rotate(0deg);
    opacity: 1;
    filter: brightness(1);
  }
  35% {
    transform: translate(var(--burst-x, 0px), -9px) scale(1.12) rotate(-4deg);
    opacity: 1;
    filter: brightness(1.45);
  }
  100% {
    transform: translate(calc(var(--burst-x, 0px) * 1.8), 44px) scale(0.5) rotate(13deg);
    opacity: 0;
    filter: brightness(2) blur(1px);
  }
}

.hint {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.highscore-list {
  margin: 0;
  padding-left: 20px;
  display: grid;
  gap: 8px;
  font-size: 13px;
}

.highscore-item {
  line-height: 1.25;
}

.highscore-main {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.highscore-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.highscore-score {
  flex: none;
}

.highscore-date {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 11px;
}

.highscore-columns {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.highscore-mode-title {
  margin: 0 0 6px;
  font-size: 14px;
  color: var(--muted);
}

dialog {
  border: none;
  border-radius: 16px;
  width: min(90vw, 440px);
  box-shadow: 0 20px 60px #10243d40;
}

dialog::backdrop { background: #10243d80; }

.modal-form {
  display: grid;
  gap: 10px;
}

input {
  width: 100%;
  border: 1px solid #2d384b;
  border-radius: 8px;
  padding: 8px;
  background: #080b10;
  color: var(--ink);
}

.submit-status { min-height: 18px; font-size: 13px; color: var(--muted); }

menu {
  margin: 0;
  padding: 0;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.piece small {
  margin-top: 2px;
  font-size: 11px;
  color: var(--muted);
  line-height: 1;
}

.drag-ghost {
  position: fixed;
  z-index: 9999;
  pointer-events: none;
  opacity: 0.9;
  transform: translate(-50%, -50%);
}

#googleAuthContainer > div {
  transform-origin: left center;
  transform: scale(0.95);
}

@media (max-width: 900px) {
  body {
    overflow: auto;
  }

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

  .topbar h1 {
    font-size: 22px;
    margin: 0;
  }

  .stats {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
  }

  .stat,
  .auth-stat {
    min-width: 0;
    padding: 6px 8px;
    border-radius: 10px;
  }

  .auth-stat {
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .label {
    font-size: 11px;
  }

  .stat strong {
    font-size: 18px;
  }

  .auth-stat .label,
  .auth-user {
    display: none;
  }

  .auth-user.is-logged {
    display: block;
    order: 1;
    margin: 0 0 4px;
    font-size: 10px;
    line-height: 1.2;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-align: center;
  }

  .auth-stat .secondary-btn {
    order: 2;
    margin-top: 0;
    padding: 6px 8px;
    font-size: 12px;
  }

  #googleAuthContainer {
    display: grid;
    place-items: center;
    min-height: 24px;
  }

  #googleAuthContainer > div {
    transform: none;
    margin: 0 auto;
  }

  .app {
    height: auto;
    min-height: 100svh;
    overflow: visible;
    padding: 10px;
  }

  .main-layout {
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow: visible;
    min-height: auto;
  }

  .board-panel {
    margin-bottom: 8px;
    overflow: hidden;
    padding: 8px;
  }

  .board {
    width: min(100%, 560px, 62svh);
    max-width: 100%;
  }

  .side-panel {
    overflow: visible;
    padding-right: 0;
    gap: 8px;
  }

  .pieces .hint {
    display: none;
  }

  .pieces h2 {
    display: none;
  }

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