* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --cols: 9;
  --gap: 1px;
  /* cell size shrinks with the viewport so the board fits the width (min 19px, then the wrapper scrolls) */
  --cell: clamp(19px, calc((100vw - 16px - 8px - 6px - 2px - var(--cols) * 1px) / var(--cols)), 34px);
}

body {
  font-family: 'Arial', sans-serif;
  background: linear-gradient(135deg, #1e3c72, #2a5298);
  background-attachment: fixed;
  color: white;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 8px;
}

.header { text-align: center; margin-bottom: 14px; }
.header h1 { font-size: 2.2em; margin-bottom: 6px; text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5); }

.levels { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin-bottom: 14px; }
.lvl {
  padding: 8px 14px;
  min-height: 44px;
  background: #27ae60;
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  touch-action: manipulation;
}
.lvl small { display: block; font-weight: normal; font-size: 11px; opacity: 0.9; }
.lvl.active { background: #f39c12; }

.panel {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-bottom: 12px;
}
.stat {
  text-align: center;
  background: rgba(255, 255, 255, 0.1);
  padding: 8px 14px;
  border-radius: 10px;
  min-width: 84px;
  font-size: 0.85em;
}
.stat-value { font-size: 1.6em; font-weight: bold; color: #ffd700; font-variant-numeric: tabular-nums; }

.face {
  width: 56px;
  height: 56px;
  font-size: 30px;
  line-height: 1;
  border: 3px solid #fff;
  border-radius: 50%;
  background: #4a90e2;
  cursor: pointer;
  touch-action: manipulation;
}
.face:active { background: #2980b9; }

.board-wrap {
  max-width: 100%;
  overflow-x: auto;
  padding: 2px;
}
.board {
  display: grid;
  width: max-content;
  margin: 0 auto;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-auto-rows: var(--cell);
  gap: var(--gap);
  border: 3px solid #fff;
  padding: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  background: #4a90e2;
  border: 1px solid #2980b9;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: calc(var(--cell) * 0.58);
  line-height: 1;
  border-radius: 3px;
  overflow: hidden;
  cursor: pointer;
}
.cell.open { background: #ecf0f1; color: #2c3e50; cursor: default; border-color: #bdc3c7; }
.cell.open.chordable { cursor: pointer; }
.cell.flag { background: #f39c12; }
.cell.mine { background: #e74c3c; }
.cell.boom { background: #c0392b; box-shadow: inset 0 0 0 2px #ffd700; }
.cell.wrong { background: #7f8c8d; }
.over .cell { cursor: default; }

.n1 { color: #1f6fd0; }
.n2 { color: #1e8449; }
.n3 { color: #d62c1a; }
.n4 { color: #7d3c98; }
.n5 { color: #b9770e; }
.n6 { color: #138d75; }
.n7 { color: #34495e; }
.n8 { color: #707b7c; }

@media (hover: hover) {
  .board:not(.over) .cell:not(.open):hover { background: #5ba0f2; }
  .board:not(.over) .cell.flag:hover { background: #f5b041; }
  .lvl:hover { background: #2ecc71; }
  .lvl.active:hover { background: #f5b041; }
  .btn:hover:not(:disabled) { background: #2ecc71; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3); }
  .btn.secondary:hover:not(:disabled) { background: #3a85d0; }
  .face:hover { background: #5ba0f2; }
}

.controls {
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: 100%;
}
.buttons { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.btn {
  padding: 12px 22px;
  min-height: 44px;
  background: #27ae60;
  color: white;
  border: none;
  border-radius: 25px;
  cursor: pointer;
  font-size: 16px;
  font-weight: bold;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  touch-action: manipulation;
}
.btn.secondary { background: #2a6fb5; }
.btn.flagmode { background: #f39c12; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:focus-visible, .lvl:focus-visible, .face:focus-visible, input:focus-visible { outline: 3px solid #ffd700; outline-offset: 2px; }

.message {
  padding: 10px 20px;
  background: rgba(46, 204, 113, 0.2);
  border-radius: 10px;
  border: 1px solid #2ecc71;
  min-height: 20px;
  max-width: 600px;
  text-align: center;
}
.instructions { font-size: 0.82em; color: #bdc3c7; text-align: center; max-width: 520px; }

/* Dialogs */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  z-index: 10;
}
.overlay[hidden] { display: none; }
.dialog {
  background: #1e3c72;
  border: 2px solid #fff;
  border-radius: 14px;
  padding: 20px;
  width: 100%;
  max-width: 480px;
  max-height: 92vh;
  overflow-y: auto;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}
.dialog h2 { text-align: center; margin-bottom: 12px; }
.dialog p { margin-bottom: 10px; text-align: center; }
.dialog input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  font-size: 16px; /* >=16px prevents iOS zoom on focus */
  border-radius: 8px;
  border: 2px solid #4a90e2;
  margin-bottom: 8px;
}
.dialog .err { color: #ff9c9c; min-height: 1.2em; font-size: 0.9em; }
.dialog .actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 8px; }
.dialog .note { font-size: 0.85em; opacity: 0.85; margin-top: 10px; }
.tabs { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; margin-bottom: 10px; }
.tab {
  padding: 8px 12px;
  min-height: 40px;
  background: #2a6fb5;
  color: white;
  border: none;
  border-radius: 18px;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  touch-action: manipulation;
}
.tab.active { background: #f39c12; }

.table-wrap { overflow-x: auto; }
table.scores { width: 100%; border-collapse: collapse; font-size: 0.92em; }
table.scores th, table.scores td { padding: 6px 6px; text-align: center; border-bottom: 1px solid rgba(255, 255, 255, 0.2); }
table.scores td.name { text-align: left; word-break: break-word; }
table.scores th { color: #ffd700; }
table.scores tr.me td { background: rgba(255, 215, 0, 0.25); font-weight: bold; }

@media (max-width: 768px) {
  .header h1 { font-size: 1.7em; }
  .header { margin-bottom: 10px; }
  .stat { min-width: 76px; padding: 6px 10px; }
  .lvl { padding: 6px 10px; }
}
