/* Orbix — structure, layout and motion. All colour lives in skins.css. */

*,
*::before,
*::after {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  background-color: var(--bg);
  background-image: var(--backdrop);
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--font-stack);
  font-size: 16px;
  line-height: 1.45;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

/* The board is driven by touch; letting the page pan fights every swipe.
 *
 * `manipulation` is NOT enough. It only turns off double-tap zoom — panning
 * still works, so a swipe that starts anywhere outside the board (the
 * scoreboard sits directly above it) scrolls the page instead of moving
 * tiles. Measured on a 390x680 viewport: the document came out 698px tall
 * against a 680px window, and those 18 spare pixels were enough to make every
 * swipe near the score jerk the whole page.
 *
 * `none` stops panning outright. It has to be on the ELEMENTS, not just the
 * body, because touch-action is resolved from the element under the finger. */
body.is-playing {
  touch-action: none;
}

body.is-playing .scoreboard,
body.is-playing .stage,
body.is-playing .modes,
body.is-playing .topbar {
  touch-action: none;
}

/* Sheets and dialogs still need to scroll — their content can be taller than
   the screen, and a user who cannot scroll a settings panel is stuck. The
   host is the scrolling container, so it and everything inside it opt back
   in. */
body.is-playing .sheet-host,
body.is-playing .sheet-host * {
  touch-action: auto;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: 6px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ------------------------------------------------------------------ shell */

/* The column has to FIT the window, not merely fill it.
 *
 * `min-height` alone let the content grow past the viewport — measured at
 * 690px inside a 680px window on a 390x680 phone. Ten spare pixels do not
 * look like a bug, but they make the page scrollable, and a scrollable page
 * turns a swipe near the edge into a scroll instead of a move. That is the
 * complaint this layout has to answer.
 *
 * `max-height` with `overflow: hidden` caps it. The board is the flexible
 * part (min-height: 0 lets it shrink below its content size, which flex items
 * otherwise refuse to do), so a short screen takes space from the grid rather
 * than pushing the dock off the bottom. */
.app {
  width: min(560px, 100%);
  min-height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 1.8vh, 16px);
  padding: calc(env(safe-area-inset-top, 0px) + clamp(10px, 2vh, 20px))
    clamp(14px, 4vw, 22px)
    calc(env(safe-area-inset-bottom, 0px) + clamp(10px, 2vh, 18px));
}

/* The board absorbs whatever is left over. Without min-height: 0 a flex item
   refuses to shrink below its content, and the overflow reappears at the
   bottom of the column. */
.app > .stage {
  min-height: 0;
  flex: 1 1 auto;
}

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

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: inherit;
}

.brand-mark {
  width: 34px;
  height: 34px;
  flex: none;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.05;
}

.brand-name {
  font-size: 1.32rem;
  font-weight: 700;
  letter-spacing: 0.055em;
}

.brand-tag {
  font-size: 0.62rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.topbar-actions {
  display: flex;
  gap: 6px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  transition: background 150ms ease, transform 150ms ease;
}

.icon-btn:hover {
  background: var(--surface-strong);
}

.icon-btn:active {
  transform: scale(0.94);
}

.icon-btn svg {
  width: 19px;
  height: 19px;
}

/* ------------------------------------------------------------------ modes */

.modes {
  display: flex;
  gap: 6px;
  padding: 5px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.mode-chip {
  flex: 1;
  padding: 8px 10px;
  border-radius: 11px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  transition: background 160ms ease, color 160ms ease;
}

.mode-chip[aria-selected='true'] {
  background: var(--accent);
  color: var(--accent-ink);
}

/* ------------------------------------------------------------- scoreboard */

/* Three cards on one row, kept deliberately short.
 *
 * Every pixel here pushes the board down, and once the page is taller than
 * the window a swipe can scroll it instead of moving a tile. On a 390x680
 * viewport the document used to come out 698px against 680px of window — the
 * whole overflow was padding in this strip.
 *
 * The label is the part to shrink: "SCORE" above the number is decoration,
 * the number is the information. Tightened padding and a smaller label buy
 * back roughly 20px without making anything harder to read. */
.scoreboard {
  display: grid;
  grid-template-columns: 1.4fr 1fr 1fr;
  gap: 8px;
}

.score-card {
  position: relative;
  padding: 6px 12px 8px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  overflow: visible;
}

.score-label {
  display: block;
  font-size: 0.55rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
  line-height: 1.3;
}

.score-value {
  display: block;
  font-size: clamp(1.15rem, 5.4vw, 1.6rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}

.score-card--primary .score-value {
  font-size: clamp(1.4rem, 6.6vw, 1.95rem);
}

/* The "+48" that floats off the score when tiles fuse. */
.score-pop {
  position: absolute;
  right: 12px;
  top: 6px;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--accent);
  opacity: 0;
  pointer-events: none;
}

.score-pop.is-live {
  animation: score-float 900ms ease-out;
}

@keyframes score-float {
  0% { opacity: 0; transform: translateY(4px); }
  18% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-26px); }
}

/* ------------------------------------------------------------------ board */

.stage {
  position: relative;
  flex: 1;
  display: grid;
  place-items: center;
  min-height: 0;
}

.board {
  --n: 4;
  /*
   * --tile and --gap are written in pixels by BoardView on every resize.
   * They cannot be derived in CSS from a percentage: `transform: translate()`
   * resolves percentages against the moving element itself rather than its
   * container, and a percentage `font-size` means "share of the parent's font",
   * so a percentage tile size silently breaks both tile placement and the
   * numerals on them.
   */
  --tile: 0px;
  --gap: 0px;
  --move-dur: 115ms;
  --move-ease: cubic-bezier(0.22, 0.68, 0.24, 1);

  position: relative;
  width: min(100%, 56vh, 480px);
  aspect-ratio: 1;
  border-radius: var(--board-radius);
  background: var(--board);
  border: 1px solid var(--board-border);
  backdrop-filter: blur(var(--board-blur));
  -webkit-backdrop-filter: blur(var(--board-blur));
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  overflow: hidden;
}

.cells {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  gap: var(--gap);
  padding: var(--gap);
}

.cell {
  border-radius: calc(var(--tile) * var(--tile-radius-ratio));
  background: var(--cell);
}

.tiles {
  position: absolute;
  inset: 0;
}

.tile {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--tile);
  height: var(--tile);
  transform: translate(
    calc(var(--gap) + var(--col) * (var(--tile) + var(--gap))),
    calc(var(--gap) + var(--row) * (var(--tile) + var(--gap)))
  );
  transition: transform var(--move-dur) var(--move-ease);
  will-change: transform;
}

/* A window resize moves every tile at once; that is a relayout, not a move. */
.board.is-resizing .tile {
  transition: none;
}

/* Tiles being swallowed slide under the tile that replaces them. */
.tile.is-ghost {
  z-index: 0;
}

.tile-face {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  border-radius: calc(var(--tile) * var(--tile-radius-ratio));
  background: linear-gradient(148deg, var(--tile-from), var(--tile-to));
  color: var(--tile-ink);
  box-shadow: 0 6px 18px -10px rgba(0, 0, 0, 0.55), 0 0 26px var(--tile-glow);
  overflow: hidden;
}

.tile-num {
  position: relative;
  z-index: 1;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  font-size: calc(var(--tile) * 0.42);
}

.tile[data-len='3'] .tile-num { font-size: calc(var(--tile) * 0.34); }
.tile[data-len='4'] .tile-num { font-size: calc(var(--tile) * 0.27); }
.tile[data-len='5'] .tile-num { font-size: calc(var(--tile) * 0.22); }
.tile[data-len='6'] .tile-num { font-size: calc(var(--tile) * 0.185); }

/* Milestone tiles get a light sweep; skins.css opts individual values in. */
.tile-shine {
  position: absolute;
  inset: -40%;
  opacity: 0;
  pointer-events: none;
  background: linear-gradient(
    100deg,
    transparent 38%,
    rgba(255, 255, 255, 0.55) 50%,
    transparent 62%
  );
}

@keyframes tile-shimmer {
  from { transform: translateX(-70%); }
  to { transform: translateX(70%); }
}

@keyframes tile-spectrum {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

[data-skin='prism'] .tile-shine {
  background: conic-gradient(
    from 0deg,
    rgba(255, 120, 180, 0.42),
    rgba(120, 200, 255, 0.42),
    rgba(160, 255, 190, 0.42),
    rgba(255, 230, 140, 0.42),
    rgba(255, 120, 180, 0.42)
  );
}

/* Newly dealt and freshly fused tiles wait for the slide to finish. */
.tile.is-spawned .tile-face {
  animation: tile-spawn 210ms var(--move-dur) cubic-bezier(0.22, 1.35, 0.4, 1) both;
}

.tile.is-merged .tile-face {
  animation: tile-fuse 260ms var(--move-dur) cubic-bezier(0.22, 1.4, 0.36, 1) both;
}

@keyframes tile-spawn {
  0% { transform: scale(0.25); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes tile-fuse {
  0% { transform: scale(0.55); opacity: 0; }
  55% { transform: scale(calc(1.12 + var(--bounce) * 0.16)); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* ---------------------------------------------------------------- overlay */

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  border-radius: var(--board-radius);
  background: var(--overlay);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  animation: overlay-in 260ms ease both;
  z-index: 3;
}

.overlay[hidden] {
  display: none;
}

@keyframes overlay-in {
  from { opacity: 0; transform: scale(1.02); }
  to { opacity: 1; transform: scale(1); }
}

.overlay-card {
  width: 100%;
  max-width: 320px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.overlay-title {
  margin: 0;
  font-size: clamp(1.5rem, 7vw, 2rem);
  font-weight: 700;
}

.overlay-sub {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.overlay-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 2px 0 4px;
}

.overlay-stat {
  padding: 8px 4px;
  border-radius: 13px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.overlay-stat b {
  display: block;
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
}

.overlay-stat span {
  font-size: 0.62rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.overlay-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---------------------------------------------------------------- buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 18px;
  border-radius: 14px;
  font-size: 0.92rem;
  font-weight: 600;
  background: var(--surface-strong);
  border: 1px solid var(--border);
  color: var(--text);
  transition: transform 140ms ease, filter 140ms ease;
}

.btn svg {
  width: 17px;
  height: 17px;
}

.btn:active {
  transform: scale(0.97);
}

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

.btn--primary:hover {
  filter: brightness(1.06);
}

.btn--ghost {
  background: transparent;
}

/* ------------------------------------------------------------------- dock */

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

.dock-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 6px 9px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 0.72rem;
  font-weight: 600;
  transition: background 150ms ease, transform 140ms ease, opacity 150ms ease;
}

.dock-btn svg {
  width: 20px;
  height: 20px;
}

.dock-btn:active {
  transform: scale(0.96);
}

.dock-btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.dock-btn--primary {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-ink);
}

.pill {
  position: absolute;
  top: 6px;
  right: 8px;
  min-width: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 0.64rem;
  line-height: 18px;
  text-align: center;
}

.dock-btn--primary .pill {
  background: var(--accent-ink);
  color: var(--accent);
}

.hint {
  margin: 0;
  text-align: center;
  font-size: 0.74rem;
  color: var(--text-muted);
}

/* ----------------------------------------------------------------- sheets */

.sheet-host {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: none;
  align-items: flex-end;
  justify-content: center;
  background: rgba(4, 6, 16, 0.5);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

.sheet-host.is-open {
  display: flex;
}

.sheet {
  width: min(560px, 100%);
  max-height: 86dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 18px clamp(16px, 4vw, 22px)
    calc(env(safe-area-inset-bottom, 0px) + 22px);
  border-radius: 26px 26px 0 0;
  background: var(--bg);
  background-image: var(--backdrop);
  border-top: 1px solid var(--border);
  color: var(--text);
  animation: sheet-up 240ms cubic-bezier(0.22, 0.9, 0.3, 1);
}

@media (min-width: 640px) {
  .sheet-host {
    align-items: center;
  }
  .sheet {
    border-radius: 26px;
    max-height: 80dvh;
    animation-name: sheet-in;
  }
}

@keyframes sheet-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

@keyframes sheet-in {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.sheet-title {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.sheet-section {
  margin: 0 0 16px;
}

.sheet-section > h3 {
  margin: 0 0 8px;
  font-size: 0.66rem;
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.option-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 14px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  margin-bottom: 7px;
}

.option-row p {
  margin: 2px 0 0;
  font-size: 0.74rem;
  color: var(--text-muted);
}

.segmented {
  display: flex;
  gap: 5px;
  padding: 4px;
  border-radius: 13px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.segmented button {
  flex: 1;
  padding: 8px 6px;
  border-radius: 10px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
  transition: background 150ms ease, color 150ms ease;
}

.segmented button[aria-pressed='true'] {
  background: var(--accent);
  color: var(--accent-ink);
}

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

.skin-card {
  padding: 10px;
  border-radius: 16px;
  border: 2px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 0.72rem;
  font-weight: 600;
  text-align: left;
  transition: border-color 160ms ease, transform 140ms ease;
}

.skin-card[aria-pressed='true'] {
  border-color: var(--accent);
}

.skin-card:active {
  transform: scale(0.97);
}

.skin-swatch {
  height: 46px;
  border-radius: 11px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px;
  padding: 5px;
}

.skin-swatch i {
  border-radius: 6px;
}

/* Switch */
.switch {
  position: relative;
  width: 46px;
  height: 27px;
  flex: none;
  border-radius: 14px;
  background: var(--border);
  transition: background 180ms ease;
}

.switch[aria-checked='true'] {
  background: var(--accent);
}

.switch::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 21px;
  height: 21px;
  border-radius: 50%;
  background: #fff;
  transition: transform 180ms cubic-bezier(0.3, 1.3, 0.4, 1);
}

.switch[aria-checked='true']::after {
  transform: translateX(19px);
}

/* Stats + achievements */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.stat-cell {
  padding: 11px 13px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.stat-cell b {
  display: block;
  font-size: 1.25rem;
  font-variant-numeric: tabular-nums;
}

.stat-cell span {
  font-size: 0.68rem;
  color: var(--text-muted);
}

.badge-list {
  display: grid;
  gap: 7px;
}

.badge {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 10px 13px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  opacity: 0.45;
}

.badge.is-earned {
  opacity: 1;
  border-color: var(--accent);
}

.badge-icon {
  width: 34px;
  height: 34px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 11px;
  background: var(--surface-strong);
  font-size: 1.05rem;
}

.badge b {
  display: block;
  font-size: 0.86rem;
}

.badge span {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.how-list {
  margin: 0;
  padding-left: 18px;
  display: grid;
  gap: 7px;
  font-size: 0.88rem;
}

.how-list strong {
  color: var(--accent);
}

/* ----------------------------------------------------------------- toasts */

.toast-host {
  position: fixed;
  left: 50%;
  /* Clear of the dock (~76px) plus the hint line beneath it. */
  bottom: calc(env(safe-area-inset-bottom, 0px) + 152px);
  transform: translateX(-50%);
  z-index: 60;
  display: grid;
  gap: 7px;
  pointer-events: none;
  width: min(430px, calc(100% - 28px));
}

.toast {
  padding: 10px 15px;
  border-radius: 13px;
  background: var(--surface-strong);
  border: 1px solid var(--border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  font-size: 0.83rem;
  text-align: center;
  animation: toast-in 220ms ease both;
}

.toast.is-leaving {
  animation: toast-out 200ms ease both;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toast-out {
  to { opacity: 0; transform: translateY(-6px); }
}

/* ------------------------------------------------------------ preferences */

/* Bumps every ink colour to the tile's own text colour at full strength and
   drops the decorative glow, for players who need flatter contrast. */
body.pref-contrast .tile-face {
  box-shadow: none;
  border: 2px solid var(--tile-ink);
}

body.pref-contrast .tile-shine {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  .board {
    --move-dur: 1ms;
  }

  .tile-shine,
  .score-pop.is-live {
    animation: none !important;
  }

  .tile.is-spawned .tile-face,
  .tile.is-merged .tile-face,
  .overlay,
  .sheet,
  .toast {
    animation-duration: 1ms !important;
  }
}

body.pref-no-motion .board {
  --move-dur: 1ms;
}

body.pref-no-motion .tile-shine {
  animation: none !important;
}

/* Short viewports (landscape phones) — reclaim vertical space. */
@media (max-height: 620px) {
  .app {
    gap: 8px;
  }
  .brand-tag,
  .hint {
    display: none;
  }
  .board {
    width: min(100%, 62vh, 480px);
  }
  .toast-host {
    bottom: calc(env(safe-area-inset-bottom, 0px) + 104px);
  }
}
