:root {
  --ink: #eaf0fb;
  --muted: #8b96b0;
  --accent: #6d7cff;
  --accent-2: #22d3ee;
  --green: #34d399;
  --cell: clamp(26px, 8.6vw, 44px);
  --radius: 20px;
  --glass: rgba(255, 255, 255, 0.04);
  --glass-border: rgba(255, 255, 255, 0.09);
  --glass-shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  overflow-x: hidden;
  /* Solid dark base fills every area incl. the notch / home-indicator safe zones,
     so the browser never shows white bars. The gradient lives on .bg-canvas. */
  background-color: #090d18;
  min-height: 100vh;
  min-height: 100dvh;
}

/* Frosted blur bars over the top/bottom safe areas. Fixed to the viewport so
   content scrolls underneath them with an iOS-style frosted fade. */
.safe-blur {
  position: fixed;
  left: 0;
  right: 0;
  z-index: 5;
  pointer-events: none;
  backdrop-filter: blur(12px) saturate(130%);
  -webkit-backdrop-filter: blur(12px) saturate(130%);
}
.safe-blur.top {
  top: 0;
  height: calc(env(safe-area-inset-top) + 14px);
  background: linear-gradient(to bottom, rgba(9, 13, 24, 0.7), rgba(9, 13, 24, 0));
  -webkit-mask-image: linear-gradient(to bottom, #000 55%, transparent);
          mask-image: linear-gradient(to bottom, #000 55%, transparent);
}
.safe-blur.bottom {
  bottom: 0;
  height: calc(env(safe-area-inset-bottom) + 18px);
  background: linear-gradient(to top, rgba(9, 13, 24, 0.7), rgba(9, 13, 24, 0));
  -webkit-mask-image: linear-gradient(to top, #000 55%, transparent);
          mask-image: linear-gradient(to top, #000 55%, transparent);
}
@media (min-width: 760px) {
  .safe-blur { display: none; } /* no status bar on desktop */
}

/* Flowing red-string animation canvas, behind everything.
   The decorative gradient is painted here (fixed to the viewport) and shows
   through the transparent canvas bitmap, with the red strings drawn on top. */
.bg-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(1200px 700px at 15% -10%, rgba(99, 102, 241, 0.18), transparent 60%),
    radial-gradient(1000px 700px at 100% 0%, rgba(34, 211, 238, 0.10), transparent 55%),
    linear-gradient(160deg, #0a0f1e 0%, #0b1120 50%, #090d18 100%);
}

.app {
  position: relative;
  z-index: 1;
  max-width: 940px;
  margin: 0 auto;
  /* base padding + iOS safe-area insets so nothing hides under the notch/home bar */
  padding: calc(18px + env(safe-area-inset-top)) calc(18px + env(safe-area-inset-right))
           calc(18px + env(safe-area-inset-bottom)) calc(18px + env(safe-area-inset-left));
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

/* ---- Glass panel base ---- */
.stat, .wordlist, .board, .win-card {
  background: var(--glass);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  backdrop-filter: blur(16px) saturate(140%);
  -webkit-backdrop-filter: blur(16px) saturate(140%);
  box-shadow: var(--glass-shadow);
}

/* ---- Top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 44px;
  height: 44px;
  border-radius: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.35rem;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 6px 20px rgba(109, 124, 255, 0.45);
}
.brand-text h1 {
  margin: 0;
  font-size: clamp(1.3rem, 4.5vw, 1.7rem);
  letter-spacing: 0.3px;
  font-weight: 800;
}
.subtitle { margin: 2px 0 0; font-size: 0.8rem; color: var(--muted); }

.stats { display: flex; gap: 10px; }
.stat {
  padding: 8px 16px;
  text-align: center;
  min-width: 70px;
}
.stat-label { display: block; font-size: 0.65rem; color: var(--muted); text-transform: uppercase; letter-spacing: 1.2px; }
.stat-value { display: block; font-size: 1.15rem; font-weight: 800; }

/* ---- Progress bar ---- */
.progress {
  height: 6px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.06);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 99px;
  background: linear-gradient(90deg, var(--green), #6ee7b7);
  box-shadow: 0 0 12px rgba(52, 211, 153, 0.6);
  transition: width 0.4s ease;
}

/* ---- Board + grid ---- */
.board {
  position: relative;
  display: flex;
  justify-content: center;
  min-width: 0; /* let the grid item shrink to the column, not the grid's content width */
  padding: 14px;
  /* Vertical swipes scroll the page; horizontal drags select words (words are
     left-to-right only, so this never conflicts with selection). */
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}
.grid {
  display: grid;
  gap: 3px;
  position: relative;
  z-index: 1; /* above the highlight layer */
}
.cell {
  width: var(--cell);
  height: var(--cell);
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.035);
  border: 1px solid rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  font-weight: 700;
  font-size: calc(var(--cell) * 0.42);
  color: #cdd6ea;
  transition: background 0.12s, color 0.12s, transform 0.05s, box-shadow 0.12s;
  cursor: pointer;
}
.cell:hover { background: rgba(255, 255, 255, 0.08); }
.cell.selecting {
  background: linear-gradient(135deg, var(--accent), #8b5cff);
  color: #fff;
  border-color: transparent;
  box-shadow: 0 0 16px rgba(109, 124, 255, 0.7);
  transform: scale(1.03);
}
.cell.found { color: #fff; font-weight: 800; }

/* Highlight "string" capsules sit behind the letters (z below .grid) */
.lines {
  position: absolute;
  pointer-events: none;
  z-index: 0;
}

/* ---- Word list ---- */
.wordlist { padding: 14px 18px; min-width: 0; }
.wordlist h2 {
  margin: 0 0 12px;
  font-size: 0.9rem;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 8px;
}
.wordlist h2::before { content: "≣"; color: var(--accent); font-size: 1.1rem; }
#words {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 8px 16px;
}
#words li {
  font-size: 0.98rem;
  letter-spacing: 0.5px;
  padding: 3px 0;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
  transition: color 0.2s, opacity 0.2s;
}
#words li::before {
  content: "";
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 1.5px solid rgba(255, 255, 255, 0.2);
  flex: 0 0 auto;
}
#words li.found { color: var(--green); font-weight: 700; }
#words li.found::before {
  content: "";
  border: none;
  background:
    var(--green)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 6.5 L5 9 L9.5 3.5' fill='none' stroke='%2306281c' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E")
    center / 11px no-repeat;
}

/* ---- Controls ---- */
.controls { display: flex; justify-content: center; }
.btn {
  color: #fff;
  border: none;
  border-radius: 14px;
  padding: 13px 28px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  background: linear-gradient(135deg, var(--accent), #8b5cff);
  box-shadow: 0 8px 24px rgba(109, 124, 255, 0.45);
  transition: transform 0.05s, filter 0.15s;
  touch-action: manipulation;
}
.btn:hover { filter: brightness(1.1); }
.btn:active { transform: scale(0.97); }

/* ---- Win banner ---- */
.win-banner {
  position: fixed;
  inset: 0;
  background: rgba(6, 10, 20, 0.6);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding: 20px;
}
.win-banner.hidden { display: none; }
.win-card {
  padding: 30px;
  text-align: center;
  max-width: 340px;
}
.win-card h2 { margin: 0 0 8px; }
.win-card p { color: var(--muted); margin: 0 0 20px; }

/* ---- Promo ribbon ---- */
.promo {
  text-align: center;
  font-size: 0.9rem;
  color: #ffe8b0;
  background: linear-gradient(135deg, rgba(245, 158, 11, 0.16), rgba(109, 124, 255, 0.16));
  border: 1px solid rgba(245, 158, 11, 0.3);
  border-radius: 14px;
  padding: 10px 14px;
}
.promo strong { color: #ffd479; }

/* ---- Win / result banner extras ---- */
.win-card h2 { margin: 6px 0 10px; font-size: 1.35rem; }
.win-time { font-size: 0.85rem; opacity: 0.75; }
#winMsg { color: var(--ink); font-size: 0.98rem; line-height: 1.5; }
#winMsg .handle { color: var(--accent-2); font-weight: 700; }

/* ---- Handle gate ---- */
.gate {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(6, 10, 20, 0.72);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.gate.hidden { display: none; }
.gate-card { max-width: 380px; padding: 30px 26px; }
.gate-logo { width: 52px; height: 52px; border-radius: 15px; font-size: 1.6rem; margin: 0 auto 6px; }
.gate-lead { color: var(--muted); line-height: 1.55; margin: 0 0 18px; }
.gate-lead strong { color: var(--ink); }
.gate-input {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--glass-border);
  border-radius: 12px;
  padding: 0 14px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.gate-input:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(109, 124, 255, 0.25);
}
.gate-input .at { color: var(--muted); font-size: 1.1rem; font-weight: 600; }
.gate-input input {
  flex: 1;
  min-width: 0;
  background: transparent;
  border: none;
  outline: none;
  color: var(--ink);
  font-size: 1.05rem;
  padding: 14px 0;
  letter-spacing: 0.3px;
}
.gate-input input::placeholder { color: rgba(139, 150, 176, 0.6); }
.handle-error {
  color: #fb7185;
  font-size: 0.82rem;
  min-height: 1.1em;
  margin: 8px 0 4px;
  text-align: left;
}
.btn-block { width: 100%; }
.gate-fine { color: var(--muted); font-size: 0.75rem; margin: 16px 0 0; line-height: 1.4; }

/* ---- Desktop: grid and word list side by side ---- */
@media (min-width: 760px) {
  .app {
    grid-template-columns: 1fr 260px;
    grid-template-areas:
      "promo promo"
      "top   top"
      "prog  prog"
      "board words"
      "ctrl  ctrl";
  }
  .promo { grid-area: promo; }
  .topbar { grid-area: top; }
  .progress { grid-area: prog; }
  .board { grid-area: board; align-items: flex-start; }
  .wordlist { grid-area: words; align-self: start; }
  .controls { grid-area: ctrl; }
}
