:root {
  --page-bg: #0b0e14;
  --surface: rgba(255, 255, 255, 0.05);
  --surface-strong: rgba(255, 255, 255, 0.09);
  --surface-hover: rgba(255, 255, 255, 0.14);
  --line: rgba(255, 255, 255, 0.10);
  --text: #eef1f6;
  --text-dim: #9aa3b2;
  --accent: #ff7a2f;
  --accent-soft: rgba(255, 122, 47, 0.16);
  --accent-glow: rgba(255, 122, 47, 0.35);
  --radius: 14px;
  --radius-sm: 10px;
  --frame-stone: #2b2722;
  --frame-stone-dark: #17140f;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--page-bg);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

body {
  background:
    radial-gradient(900px 500px at 50% -10%, rgba(255, 122, 47, 0.13), transparent 70%),
    radial-gradient(700px 500px at 100% 100%, rgba(80, 110, 255, 0.08), transparent 70%),
    var(--page-bg);
}

button, select {
  font: inherit;
  color: inherit;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* Yläosa: nimi, tilannepalkki ja valinnat */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 16px 4px;
}

.brandRow {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 22px;
  margin-bottom: 10px;
}

/* Paluulinkki upotetussa versiossa (mobileapps.fi) */
.backLink {
  position: absolute;
  left: 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  text-decoration: none;
}

.backLink:hover {
  color: var(--text);
}

.brand {
  margin: 0;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  user-select: none;
}

.stat {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 48px;
  padding: 0 14px;
  border-radius: var(--radius);
  background: rgba(0, 0, 0, 0.25);
}

.stat:last-child {
  justify-self: end;
}

.statIcon {
  font-size: 18px;
  line-height: 1;
}

.digit {
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  font-size: 22px;
  letter-spacing: 0.04em;
  min-width: 3ch;
}

.hudCenter {
  display: flex;
  align-items: center;
  gap: 8px;
}

.smiley,
.modeBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  background: var(--surface-strong);
  cursor: pointer;
  transition: transform 120ms ease, background 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
}

.smiley {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  font-size: 26px;
  box-shadow: 0 0 0 4px var(--accent-soft);
}

.modeBtn {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  font-size: 20px;
}

.modeBtn.isFlag {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.smiley:hover,
.modeBtn:hover {
  background: var(--surface-hover);
}

.smiley:active,
.modeBtn:active {
  transform: scale(0.94);
}

.subbar {
  margin-top: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.select,
.smallBtn {
  height: 40px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background-color: var(--surface);
  color: var(--text);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 120ms ease, border-color 120ms ease;
}

.select {
  flex: 1;
  min-width: 0;
  appearance: none;
  -webkit-appearance: none;
  padding-right: 34px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%239aa3b2' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  text-overflow: ellipsis;
}

.select option {
  background: #161b25;
  color: var(--text);
}

.smallBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  white-space: nowrap;
}

.select:hover,
.smallBtn:hover {
  background-color: var(--surface-hover);
}

.select:focus-visible,
.smallBtn:focus-visible,
.smiley:focus-visible,
.modeBtn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#leaderboardBtn {
  background: var(--accent);
  border-color: var(--accent);
  color: #1a0d04;
}

#leaderboardBtn:hover {
  background: #ff8f4f;
}

.smallBtn.isActive {
  background: var(--accent-soft);
  border-color: var(--accent);
}

a.smallBtn {
  text-decoration: none;
}

.status {
  min-height: 18px;
  margin-top: 8px;
  font-size: 13px;
  color: var(--text-dim);
  text-align: center;
}

/* Pelilauta */
.main {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.boardWrap {
  flex: 1;
  overflow: auto;
  padding: 12px 12px max(24px, env(safe-area-inset-bottom));
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overscroll-behavior: contain;
}

.boardFrame {
  width: fit-content;
  padding: 10px;
  border-radius: 18px;
  background:
    linear-gradient(160deg, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0) 40%),
    linear-gradient(180deg, var(--frame-stone), var(--frame-stone-dark));
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.10),
    inset 0 0 0 1px rgba(0, 0, 0, 0.4),
    0 24px 60px rgba(0, 0, 0, 0.55),
    0 0 80px rgba(255, 122, 47, 0.08);
}

.boardFrame > .row:first-child > .cell:first-child { border-top-left-radius: 8px; }
.boardFrame > .row:first-child > .cell:last-child { border-top-right-radius: 8px; }
.boardFrame > .row:last-child > .cell:first-child { border-bottom-left-radius: 8px; }
.boardFrame > .row:last-child > .cell:last-child { border-bottom-right-radius: 8px; }

.row {
  display: flex;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  flex: 0 0 var(--cell);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #3a3632;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  font-weight: 800;
  font-size: calc(var(--cell) * 0.58);
  line-height: 1;
  user-select: none;
  cursor: pointer;
  touch-action: manipulation;
}

.cell.startHint {
  outline: 3px solid #2ecc71;
  outline-offset: -3px;
}

.cell.revealed {
  background: #cfc6b4;
  cursor: default;
}

.cell.exploded {
  background: #f33;
}

/* Kursorit (tools/blender/cursors.py): hakku kaivaessa, lippu liputtaessa. Luvut = kärkipiste. */
.boardFrame .cell:not(.revealed) {
  cursor: url('assets/cursors/pickaxe.png') 8 3, pointer;
  cursor: -webkit-image-set(url('assets/cursors/pickaxe.png') 1x, url('assets/cursors/pickaxe@2x.png') 2x) 8 3, pointer;
  cursor: image-set(url('assets/cursors/pickaxe.png') 1x, url('assets/cursors/pickaxe@2x.png') 2x) 8 3, pointer;
}

.boardFrame.flagMode .cell:not(.revealed) {
  cursor: url('assets/cursors/flag.png') 9 30, pointer;
  cursor: -webkit-image-set(url('assets/cursors/flag.png') 1x, url('assets/cursors/flag@2x.png') 2x) 9 30, pointer;
  cursor: image-set(url('assets/cursors/flag.png') 1x, url('assets/cursors/flag@2x.png') 2x) 9 30, pointer;
}

.boardFrame.gameOver .cell {
  cursor: default;
}

@media (hover: hover) {
  .boardFrame:not(.gameOver) .cell:not(.revealed):hover {
    filter: brightness(1.15);
  }
}

/* Kivilaatat: Blenderillä renderöity atlas (tools/blender/stone_tiles.py), 12 kehystä x 3 varianttia */
.boardFrame.stone .cell {
  position: relative;
  border: none;
  background-color: #8a857c;
  background-image: url('assets/tiles/stone_tiles.png');
  background-size: calc(var(--cell) * 12) calc(var(--cell) * 3);
  background-position: 0 calc(var(--v, 0) * var(--cell) * -1);
}

.boardFrame.stone .cell.revealed {
  background-position: calc(var(--cell) * -11) calc(var(--v, 0) * var(--cell) * -1);
}

.boardFrame.stone .cell.exploded {
  box-shadow: inset 0 0 0 var(--cell) rgba(255, 32, 0, 0.69);
}

.boardFrame.stone .cell.opening {
  animation: stoneOpen 260ms steps(11, end) both;
}

.boardFrame.stone .cell.opening > span {
  animation: stoneContent 260ms linear both;
  animation-delay: inherit;
}

@keyframes stoneOpen {
  from { background-position-x: 0; }
  to { background-position-x: calc(var(--cell) * -11); }
}

.boardFrame.stone .flagSprite {
  position: absolute;
  inset: 0;
  background-image: url('assets/tiles/flag.png');
  background-size: calc(var(--cell) * 12) var(--cell);
  animation: stoneFlag 900ms steps(12, end) infinite;
  animation-delay: calc(var(--v, 0) * -300ms);
  pointer-events: none;
}

.boardFrame.stone .boom {
  position: absolute;
  left: calc(var(--cell) * -1);
  top: calc(var(--cell) * -1);
  width: calc(var(--cell) * 3);
  height: calc(var(--cell) * 3);
  z-index: 5;
  pointer-events: none;
  background-image: url('assets/tiles/explosion.png');
  background-repeat: no-repeat;
  background-size: calc(var(--cell) * 48) calc(var(--cell) * 3);
  animation: stoneBoom 650ms steps(16, end) forwards;
}

@keyframes stoneFlag {
  from { background-position-x: 0; }
  to { background-position-x: calc(var(--cell) * -12); }
}

@keyframes stoneBoom {
  from { background-position-x: 0; }
  to { background-position-x: calc(var(--cell) * -48); }
}

@keyframes stoneContent {
  0%, 70% { opacity: 0; }
  100% { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .boardFrame.stone .cell.opening,
  .boardFrame.stone .cell.opening > span,
  .boardFrame.stone .flagSprite {
    animation: none;
  }

  .boardFrame.stone .boom {
    display: none;
  }
}

/* Ikkunat: tulostaulut, nimen kysely */
.modalOverlay {
  position: fixed;
  inset: 0;
  background: rgba(4, 6, 10, 0.66);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 16px;
  z-index: 20;
}

.modal {
  width: min(560px, 100%);
  margin-top: 24px;
  padding: 16px;
  background: #121722;
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
}

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

.modalTitle {
  font-weight: 800;
  font-size: 17px;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
}

.modalBody {
  max-height: min(70vh, 520px);
  overflow: auto;
  scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
  scrollbar-width: thin;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.modalBody input,
.modalBody textarea {
  border-radius: var(--radius-sm);
  font: inherit;
}

.listRow {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
}

.listRow:last-child {
  border-bottom: none;
}

.listRow strong {
  display: inline-block;
  min-width: 34px;
  font-variant-numeric: tabular-nums;
}

.badge {
  font-weight: 800;
  background: var(--accent-soft);
  border: 1px solid rgba(255, 122, 47, 0.45);
  color: #ffb98f;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

/* Numerot avatulla hiekkakivellä: klassiset värit, kevyt kohokuvio */
.numbers-1, .numbers-2, .numbers-3, .numbers-4,
.numbers-5, .numbers-6, .numbers-7, .numbers-8 {
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.45);
}
.numbers-1 { color: #1d4ed8; }
.numbers-2 { color: #15803d; }
.numbers-3 { color: #dc2626; }
.numbers-4 { color: #1e3a8a; }
.numbers-5 { color: #7f1d1d; }
.numbers-6 { color: #0f766e; }
.numbers-7 { color: #111827; }
.numbers-8 { color: #4b5563; }

@media (max-width: 420px) {
  .brandRow:has(.backLink) {
    justify-content: flex-end;
  }

  .topbar {
    padding-left: 12px;
    padding-right: 12px;
  }

  .stat {
    padding: 0 10px;
    gap: 6px;
  }

  .digit {
    font-size: 19px;
  }

  .select,
  .smallBtn {
    padding-left: 10px;
    font-size: 13px;
  }

  .select {
    padding-right: 28px;
    background-position: right 9px center;
  }
}
