/* Minesweeper-specific layout. common.css provides tokens, reset, header,
   modal, and shared rules. This file owns the board grid, cell states,
   the American-flag flag-marker, and the magnify overlay. */

/* ── Board layout ─────────────────────────────────────────── */

.board {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: clamp(8px, 1.5vh, 16px) clamp(6px, 1.5vw, 14px);
  align-items: center;
  justify-content: flex-start;
}

.board-scroll {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: visible;
  -webkit-overflow-scrolling: touch;
  display: flex;
  justify-content: center;
}

/* The grid. CSS variables --rows / --cols are set by render(). */
.board-grid {
  display: grid;
  grid-template-rows: repeat(var(--rows), var(--cell));
  grid-template-columns: repeat(var(--cols), var(--cell));
  gap: 2px;
  padding: 6px;
  background: var(--bg-3);
  border: 1px solid var(--gold-faint);
  border-radius: 6px;
  box-shadow:
    inset 0 0 0 1px rgba(0, 0, 0, 0.6),
    0 4px 14px rgba(0, 0, 0, 0.45);
  /* Cell size: scale per difficulty, with floor/ceiling */
  --cell: clamp(22px, 6vw, 38px);
  touch-action: none;     /* we manage gestures ourselves */
  user-select: none;
  -webkit-user-select: none;
}

.board-grid[data-difficulty="intermediate"] { --cell: clamp(20px, 4vw, 32px); }

/* ── Cells ────────────────────────────────────────────────── */

.cell {
  width: var(--cell);
  height: var(--cell);
  display: grid;
  place-items: center;
  font-family: 'Oswald', sans-serif;
  font-weight: 700;
  font-size: calc(var(--cell) * 0.55);
  line-height: 1;
  border-radius: 2px;
  position: relative;
  cursor: pointer;
  user-select: none;
}

.cell.hidden {
  background: linear-gradient(160deg, #4a4068 0%, #2f2750 100%);
  box-shadow:
    inset 1px 1px 0 rgba(255, 255, 255, 0.18),
    inset -1px -1px 0 rgba(0, 0, 0, 0.4);
}
.cell.hidden:hover { background: linear-gradient(160deg, #564a78 0%, #38305c 100%); }

.cell.revealed {
  background: var(--card);
  color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--card-edge);
}

.cell.revealed.n1 { color: #1976d2; }
.cell.revealed.n2 { color: #2e7d32; }
.cell.revealed.n3 { color: #c62828; }
.cell.revealed.n4 { color: #1a237e; }
.cell.revealed.n5 { color: #7b1f1f; }
.cell.revealed.n6 { color: #00838f; }
.cell.revealed.n7 { color: #212121; }
.cell.revealed.n8 { color: #616161; }

.cell.flagged {
  background: linear-gradient(160deg, #4a4068 0%, #2f2750 100%);
  box-shadow:
    inset 1px 1px 0 rgba(255, 255, 255, 0.18),
    inset -1px -1px 0 rgba(0, 0, 0, 0.4);
}

.cell.mine-detonated {
  background: #b22234;
  color: #fff;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.4);
}

.cell.mine-revealed {
  background: rgba(178, 34, 52, 0.35);
  box-shadow:
    inset 1px 1px 0 rgba(255, 255, 255, 0.12),
    inset -1px -1px 0 rgba(0, 0, 0, 0.4);
}

/* SVG marker (flag, mine) sizing inside a cell */
.cell .marker {
  width: 90%;
  height: 90%;
  display: block;
  pointer-events: none;
}

/* ── Magnify overlay ──────────────────────────────────────── */

.magnify {
  position: fixed;
  z-index: 800;
  pointer-events: none;
  background: rgba(15, 18, 38, 0.96);
  border: 1px solid var(--gold);
  border-radius: 6px;
  padding: 8px;
  opacity: 0;
  transform: scale(0.85);
  transition: opacity 0.12s ease, transform 0.12s ease;
  box-shadow:
    0 8px 24px rgba(0, 0, 0, 0.6),
    0 0 0 2px rgba(0, 0, 0, 0.45);
}
.magnify.show { opacity: 1; transform: scale(1); }

.magnify-grid {
  display: grid;
  grid-template-columns: repeat(3, 44px);
  grid-template-rows: repeat(3, 44px);
  gap: 3px;
}

.magnify-cell {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  font-family: 'Oswald', sans-serif;
  font-weight: 700;
  font-size: 24px;
  line-height: 1;
  border-radius: 2px;
}
.magnify-cell.out { background: transparent; }
.magnify-cell.hidden  {
  background: linear-gradient(160deg, #4a4068 0%, #2f2750 100%);
  box-shadow:
    inset 1px 1px 0 rgba(255, 255, 255, 0.18),
    inset -1px -1px 0 rgba(0, 0, 0, 0.4);
}
.magnify-cell.flagged {
  background: linear-gradient(160deg, #4a4068 0%, #2f2750 100%);
  box-shadow:
    inset 1px 1px 0 rgba(255, 255, 255, 0.18),
    inset -1px -1px 0 rgba(0, 0, 0, 0.4);
}
.magnify-cell.revealed { background: var(--card); }
.magnify-cell.mine-detonated { background: #b22234; }
.magnify-cell.focus { outline: 2px solid var(--gold-bright); outline-offset: 2px; }

.magnify-cell .marker { width: 88%; height: 88%; }

@media (prefers-reduced-motion: reduce) {
  .magnify { transition: opacity 0.01ms, transform 0.01ms; }
}

/* ── HUD: difficulty selector + monospace counters ────────── */

.hud .stat .value.mono {
  font-family: 'Oswald', monospace;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.06em;
  font-size: 17px;
  color: var(--gold-bright);
}

.diff-select {
  background: transparent;
  color: var(--card);
  border: 1px solid var(--gold-faint);
  padding: 6px 10px;
  border-radius: 4px;
  font-family: var(--sans);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  transition: all 0.2s ease;
}
.diff-select:hover  { border-color: var(--gold); color: var(--gold-bright); }
.diff-select:focus  { outline: none; border-color: var(--gold); }
.diff-select.confirming {
  border-color: var(--gold);
  color: var(--gold-bright);
  background: rgba(201, 169, 97, 0.15);
  animation: confirm-pulse 1s ease infinite;
}
.diff-select option { background: var(--bg-3); color: var(--card); }

.icon-btn.confirming {
  border-color: var(--gold);
  color: var(--gold-bright);
  background: rgba(201, 169, 97, 0.15);
  animation: confirm-pulse 1s ease infinite;
}
@keyframes confirm-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(201, 169, 97, 0.45); }
  50%      { box-shadow: 0 0 0 6px rgba(201, 169, 97, 0); }
}

/* ── End-of-game overlay (no modal — just brief animated banner) ── */

.end-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  z-index: 800;
  opacity: 0;
  transition: opacity 0.25s ease;
  padding: 20px;
}
.end-overlay.show { opacity: 1; }

.end-overlay .end-panel {
  background:
    radial-gradient(ellipse at top, rgba(82, 60, 130, 0.4) 0%, transparent 70%),
    linear-gradient(180deg, rgba(42, 37, 84, 0.92) 0%, rgba(15, 18, 38, 0.92) 100%);
  border: 1px solid var(--gold);
  padding: 28px 56px;
  border-radius: 4px;
  text-align: center;
  position: relative;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.55);
  transform: scale(0.85);
  transition: transform 0.35s cubic-bezier(0.2, 0.8, 0.3, 1.3);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.end-overlay.show .end-panel { transform: scale(1); }

.end-overlay .end-panel::before,
.end-overlay .end-panel::after {
  content: '';
  position: absolute;
  width: 22px;
  height: 22px;
  border: 1px solid var(--gold);
}
.end-overlay .end-panel::before { top: 6px; left: 6px; border-right: none; border-bottom: none; }
.end-overlay .end-panel::after  { bottom: 6px; right: 6px; border-left: none; border-top: none; }

.end-overlay .end-title {
  font-family: var(--serif);
  font-size: clamp(34px, 7vw, 52px);
  font-style: italic;
  font-weight: 500;
  color: var(--gold-bright);
  letter-spacing: -0.01em;
  line-height: 1;
  margin-bottom: 6px;
}
.end-overlay .ornament-line {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin: 4px auto 10px;
  color: var(--gold-dim);
}
.end-overlay .ornament-line::before,
.end-overlay .ornament-line::after {
  content: '';
  height: 1px;
  width: 36px;
  background: linear-gradient(90deg, transparent, var(--gold-dim), transparent);
}
.end-overlay .ornament-line span {
  font-family: var(--serif);
  font-style: italic;
  font-size: 12px;
  letter-spacing: 0.3em;
  text-transform: uppercase;
}
.end-overlay .end-sub {
  font-family: var(--serif);
  font-style: italic;
  font-size: 16px;
  color: rgba(245, 237, 224, 0.85);
}

/* Loss styling: red border + title */
.end-overlay.lost .end-panel {
  border-color: #b22234;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.55), 0 0 28px rgba(178, 34, 52, 0.45);
}
.end-overlay.lost .end-panel::before,
.end-overlay.lost .end-panel::after { border-color: #b22234; }
.end-overlay.lost .end-title { color: #e0c47a; }
.end-overlay.lost .ornament-line span { color: #d96b6b; }

/* ── Sparks (win celebration) ───────────────────────────── */

.spark {
  position: fixed;
  width: 6px;
  height: 6px;
  pointer-events: none;
  z-index: 999;
  border-radius: 50%;
  animation: spark-fall 2.5s ease-in forwards;
}
@keyframes spark-fall {
  0%   { transform: translate(0, 0) rotate(0deg); opacity: 1; }
  100% { transform: translate(var(--dx), 100vh) rotate(720deg); opacity: 0; }
}

/* ── Board shake (loss) ─────────────────────────────────── */

@keyframes board-shake {
  0%, 100%               { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-7px); }
  20%, 40%, 60%, 80%      { transform: translateX(7px); }
}
.board-grid.shake { animation: board-shake 0.55s cubic-bezier(0.36, 0.07, 0.19, 0.97); }

@media (prefers-reduced-motion: reduce) {
  .end-overlay,
  .end-overlay .end-panel { transition: opacity 0.01ms, transform 0.01ms; }
  .spark { display: none; }
  .board-grid.shake { animation: none; }
}

/* ── Responsive HUD ──────────────────────────────────────── */
@media (max-width: 640px) {
  .hud { gap: 10px; }
  .best-stat { display: none; }
  .diff-select { padding: 5px 7px; font-size: 11px; }
  .magnify-grid { grid-template-columns: repeat(3, 38px); grid-template-rows: repeat(3, 38px); }
  .magnify-cell { width: 38px; height: 38px; font-size: 20px; }
}
@media (max-width: 380px) {
  .hud .stat:not(.always-show) { display: none; }
}

/* ── PWA install banner ──────────────────────────────────── */

#pwa-banner {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: linear-gradient(180deg, rgba(26,31,58,0.97) 0%, rgba(15,18,38,0.97) 100%);
  border-top: 1px solid var(--gold);
  z-index: 950;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  animation: pwa-slide-up 0.35s ease forwards;
}
@keyframes pwa-slide-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
.pwa-row {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 600px;
  margin: 0 auto;
}
.pwa-icon {
  width: 44px; height: 44px; border-radius: 8px;
  flex-shrink: 0;
}
.pwa-text {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; gap: 2px;
}
.pwa-text strong {
  font-family: var(--serif);
  font-size: 15px;
  color: var(--gold-bright);
  font-weight: 500;
  letter-spacing: 0.04em;
}
.pwa-text span {
  font-size: 12px;
  color: rgba(245, 237, 224, 0.7);
  display: flex; align-items: center; gap: 4px;
  flex-wrap: wrap;
}
.pwa-share-icon {
  display: inline-block;
  width: 14px; height: 14px;
  vertical-align: middle;
  color: var(--gold-bright);
}
#pwa-install-btn {
  background: var(--gold);
  color: var(--bg-1);
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
  flex-shrink: 0;
}
#pwa-install-btn:hover { background: var(--gold-bright); }
#pwa-dismiss-btn {
  background: transparent;
  border: none;
  color: rgba(245, 237, 224, 0.5);
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
  flex-shrink: 0;
}
#pwa-dismiss-btn:hover { color: var(--card); }
