/* =========================================================================
   Squares — global styles
   Source of truth: ARCHITECTURE.md
   ========================================================================= */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

:root {
  /* Palette (must match ARCHITECTURE.md) */
  --color-0: #e11d48;
  --color-1: #2563eb;
  --color-2: #16a34a;
  --color-3: #9333ea;
  --color-4: #64748b;
  --color-5: #f59e0b;

  /* Chrome */
  --bg:      #0b1020;
  --bg-2:    #111734;
  --bg-3:    #1a2247;
  --ink:     #e5e7eb;
  --ink-dim: #94a3b8;
  --accent:  #38bdf8;

  --radius-sm: 8px;
  --radius:    14px;
  --radius-lg: 22px;

  --shadow-soft: 0 2px 10px rgba(0,0,0,.35), 0 10px 30px rgba(0,0,0,.25);
  --shadow-tile: inset 0 2px 0 rgba(255,255,255,.18),
                 inset 0 -3px 0 rgba(0,0,0,.20),
                 0 2px 6px rgba(0,0,0,.35);

  --grid-size: 5;

  --font-ui: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
             Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', Arial, sans-serif;

  --safe-top:    env(safe-area-inset-top,    0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left,   0px);
  --safe-right:  env(safe-area-inset-right,  0px);
}

/* ---------- Reset ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font-ui);
  color: var(--ink);
  background:
    radial-gradient(1200px 800px at 15% -10%, rgba(56,189,248,.10), transparent 60%),
    radial-gradient(900px  700px at 110% 110%, rgba(147,51,234,.12), transparent 55%),
    linear-gradient(180deg, var(--bg) 0%, #070a18 100%);
  background-attachment: fixed;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
}
button { font-family: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- App shell ---------- */
#app {
  position: fixed;
  inset: 0;
  display: block;
  overflow: hidden;
}

/* ---------- Screens ---------- */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: opacity .32s ease, transform .32s cubic-bezier(.4,0,.2,1);
  padding-top: var(--safe-top);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  padding-bottom: var(--safe-bottom);
}
.screen.active {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

/* ---------- Title screen ---------- */
.screen-title {
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
}
.title-logo {
  font-size: clamp(56px, 14vmin, 120px);
  font-weight: 800;
  letter-spacing: -0.04em;
  line-height: .95;
  background: linear-gradient(135deg, #fff 0%, var(--accent) 65%, #a78bfa 100%);
  -webkit-background-clip: text;
          background-clip: text;
  color: transparent;
  margin: 0 0 8px;
  text-shadow: 0 6px 40px rgba(56,189,248,.25);
}
.title-sub {
  font-size: 14px;
  color: var(--ink-dim);
  letter-spacing: .18em;
  text-transform: uppercase;
  margin: 0 0 40px;
}
.title-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(340px, 92vw);
}

/* ---------- Buttons ---------- */
.btn {
  border: none;
  border-radius: var(--radius);
  padding: 14px 22px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: .01em;
  min-height: 48px;
  background: linear-gradient(180deg, var(--bg-3) 0%, var(--bg-2) 100%);
  color: var(--ink);
  box-shadow: var(--shadow-soft),
              inset 0 1px 0 rgba(255,255,255,.08);
  transition: transform .12s ease, box-shadow .2s ease, background .2s ease, opacity .2s;
}
.btn:active { transform: scale(.96); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary {
  background: linear-gradient(180deg, #3b82f6 0%, #2563eb 100%);
  color: #fff;
  box-shadow: 0 10px 30px rgba(37,99,235,.35),
              inset 0 1px 0 rgba(255,255,255,.25);
}
.btn-accent {
  background: linear-gradient(180deg, #38bdf8 0%, #0ea5e9 100%);
  color: #041018;
}
.btn-ghost {
  background: transparent;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.12);
}
.btn-icon {
  width: 44px; height: 44px;
  min-height: 44px;
  padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 18px;
  border-radius: 12px;
  background: rgba(255,255,255,.06);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.08);
}

/* ---------- Game screen layout ---------- */
.screen-game {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.hud {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  margin: 8px 10px 0;
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(17,23,52,.85), rgba(17,23,52,.6));
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: var(--shadow-soft);
  flex: 0 0 auto;
}
.hud-left, .hud-right {
  display: flex; align-items: center; gap: 8px;
}
.hud-center {
  display: flex; flex-direction: column; align-items: center; min-width: 0;
}
.hud-level-name {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: .01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 42vw;
}
.hud-moves {
  display: flex;
  gap: 6px;
  font-size: 12px;
  color: var(--ink-dim);
  font-variant-numeric: tabular-nums;
}
.hud-moves .moves-val { color: var(--ink); font-weight: 600; }
.hud-moves .par-val   { color: var(--ink-dim); }

.target-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px 6px 6px;
  border-radius: 999px;
  background: rgba(255,255,255,.04);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.10);
}
.target-chip-label { font-size: 11px; color: var(--ink-dim); letter-spacing: .12em; text-transform: uppercase; }
.target-swatch {
  width: 24px; height: 24px; border-radius: 8px;
  background: var(--color-0);
  box-shadow: var(--shadow-tile);
  animation: pulseChip 2.2s ease-in-out infinite;
}
@keyframes pulseChip {
  0%,100% { transform: scale(1);    box-shadow: var(--shadow-tile), 0 0 0 0 rgba(255,255,255,.0); }
  50%     { transform: scale(1.07); box-shadow: var(--shadow-tile), 0 0 0 6px rgba(255,255,255,.06); }
}

/* ---------- Grid ---------- */
.grid-wrap {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 10px;
  min-height: 0;
  position: relative;
}
.grid {
  display: grid;
  grid-template-columns: repeat(var(--grid-size, 5), 1fr);
  grid-auto-rows: 1fr;
  gap: clamp(4px, 1vmin, 10px);
  width: min(92vmin, 560px);
  max-height: 100%;
  aspect-ratio: 1 / 1;
  position: relative;
}

/* ---------- Tile ---------- */
.tile {
  position: relative;
  border-radius: var(--radius);
  background: var(--color-0);
  box-shadow: var(--shadow-tile);
  transition: background-color .18s cubic-bezier(.4,0,.2,1),
              transform      .18s cubic-bezier(.4,0,.2,1),
              box-shadow     .2s  ease,
              filter         .2s  ease;
  cursor: pointer;
  touch-action: manipulation;
  aspect-ratio: 1 / 1;
  min-width: 32px;
  min-height: 32px;
  /* Responsive size clamp — grid still constrains the outer box */
  width: clamp(42px, calc(92vmin / var(--grid-size, 9)), 80px);
  max-width: 100%;
  max-height: 100%;
  will-change: transform, background-color;
}
.grid .tile {
  /* Inside the grid, let the grid control size */
  width: auto;
}
.tile::after {
  /* inner highlight overlay */
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(180deg, rgba(255,255,255,.10) 0%, rgba(255,255,255,0) 40%);
  pointer-events: none;
}

.tile.c0 { background-color: var(--color-0); }
.tile.c1 { background-color: var(--color-1); }
.tile.c2 { background-color: var(--color-2); }
.tile.c3 { background-color: var(--color-3); }
.tile.c4 { background-color: var(--color-4); }
.tile.c5 { background-color: var(--color-5); }

.tile.flipping { transform: scale(.92); }
.tile.preview {
  box-shadow: var(--shadow-tile),
              inset 0 0 0 3px rgba(255,255,255,.4);
  transform: scale(1.04);
  z-index: 2;
}
.tile.target-match { filter: saturate(1.15) brightness(1.05); }

/* Ripple win wave */
.tile.win-wave {
  animation: winPop .5s cubic-bezier(.4,0,.2,1) both;
}
@keyframes winPop {
  0%   { transform: scale(1);    filter: brightness(1); }
  40%  { transform: scale(1.15); filter: brightness(1.6); box-shadow: var(--shadow-tile), 0 0 20px 4px rgba(255,255,255,.35); }
  100% { transform: scale(1);    filter: brightness(1); }
}

/* Mechanics overlays (for Mechanics.renderOverlay hook to populate) */
.tile .overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60%;
  pointer-events: none;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,.6);
}

/* ---------- Bottom actions (game screen) ---------- */
.game-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  padding: 8px 14px calc(10px + var(--safe-bottom));
  flex: 0 0 auto;
}
.game-actions .btn { flex: 0 1 auto; }

/* ---------- Level select ---------- */
.screen-level-select { padding: 20px; overflow: auto; }
.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px;
  margin-top: 16px;
}
.level-card {
  background: linear-gradient(180deg, var(--bg-3), var(--bg-2));
  border-radius: var(--radius);
  padding: 14px;
  min-height: 96px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  box-shadow: var(--shadow-soft);
  border: 1px solid rgba(255,255,255,.06);
  cursor: pointer;
  transition: transform .15s ease, box-shadow .2s ease;
}
.level-card:active { transform: scale(.97); }
.level-card.locked { opacity: .45; cursor: not-allowed; }
.level-card .level-num {
  font-size: 24px; font-weight: 800; letter-spacing: -0.02em;
}
.level-card .level-name { font-size: 13px; color: var(--ink-dim); }
.level-card .level-best { font-size: 11px; color: var(--accent); margin-top: 4px; }

/* ---------- Generic header bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px 0;
}
.topbar h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.topbar .spacer { flex: 1; }

/* ---------- Modals ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(4,7,18,.66);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
  z-index: 50;
}
.modal-backdrop.active {
  opacity: 1;
  pointer-events: auto;
}
.modal-card {
  background: linear-gradient(180deg, var(--bg-2), var(--bg));
  border-radius: var(--radius-lg);
  padding: 24px;
  width: min(420px, 92vw);
  max-height: 88vh;
  overflow: auto;
  box-shadow: var(--shadow-soft),
              inset 0 1px 0 rgba(255,255,255,.06);
  border: 1px solid rgba(255,255,255,.07);
  transform: translateY(20px) scale(.96);
  transition: transform .3s cubic-bezier(.2,.9,.2,1);
  text-align: center;
}
.modal-backdrop.active .modal-card { transform: translateY(0) scale(1); }

.modal-card h2 {
  margin: 0 0 6px;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: -0.02em;
}
.modal-card p { color: var(--ink-dim); margin: 0 0 16px; }

/* Win modal */
.win-stats {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin: 14px 0 18px;
}
.win-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.win-stat-val {
  font-size: 28px;
  font-weight: 800;
  letter-spacing: -0.02em;
}
.win-stat-label {
  font-size: 11px;
  color: var(--ink-dim);
  letter-spacing: .12em;
  text-transform: uppercase;
}
.share-preview {
  font-family: ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace;
  font-size: 18px;
  line-height: 1.1;
  background: rgba(0,0,0,.25);
  padding: 12px;
  border-radius: var(--radius);
  white-space: pre;
  overflow-x: auto;
  margin: 0 0 16px;
}
.modal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.modal-actions .btn { width: 100%; }
.modal-actions .btn.full { grid-column: 1 / -1; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + var(--safe-bottom));
  transform: translateX(-50%) translateY(20px);
  background: rgba(17,23,52,.95);
  border: 1px solid rgba(255,255,255,.08);
  color: var(--ink);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease, transform .25s ease;
  z-index: 60;
  box-shadow: var(--shadow-soft);
}
.toast.active {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.toast.error { background: #7f1d1d; }
.toast.success { background: #14532d; }

/* ---------- Confetti layer (win celebration) ---------- */
.confetti-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 55;
}
.confetti {
  position: absolute;
  width: 10px; height: 14px;
  top: -20px;
  border-radius: 2px;
  opacity: 0;
  animation: confettiFall 1.6s ease-in forwards;
}
@keyframes confettiFall {
  0%   { opacity: 0; transform: translateY(-10vh) rotate(0deg); }
  10%  { opacity: 1; }
  100% { opacity: 0; transform: translateY(110vh) rotate(720deg); }
}

/* ---------- Skeleton loading ---------- */
.skeleton {
  background: linear-gradient(90deg,
    rgba(255,255,255,.04) 0%,
    rgba(255,255,255,.10) 50%,
    rgba(255,255,255,.04) 100%);
  background-size: 200% 100%;
  animation: skeletonShimmer 1.2s linear infinite;
  border-radius: var(--radius-sm);
  color: transparent;
}
@keyframes skeletonShimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ---------- Paused overlay ---------- */
.paused-overlay {
  position: absolute;
  inset: 0;
  background: rgba(11,16,32,.7);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
}
.paused-overlay.active {
  opacity: 1;
  pointer-events: auto;
}
.paused-overlay .paused-label {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--ink);
}

/* ---------- Utility ---------- */
.hidden { display: none !important; }
.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Small screens tweaks ---------- */
@media (max-width: 420px) {
  .hud-level-name { font-size: 14px; max-width: 38vw; }
  .hud { padding: 8px 10px; }
  .btn { padding: 12px 18px; font-size: 15px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
