*, *::before, *::after {
  box-sizing: border-box;
}

/* ---- Color scheme (light default) ---- */

:root {
  --bg: #f5f5f5;
  --bg-card: #fff;
  --bg-hover: #f0f0f0;
  --bg-active: #e0e0e0;
  --bg-stripe: #fafafa;
  --text: #1a1a1a;
  --text-secondary: #666;
  --text-muted: #999;
  --border: #e5e5e5;
  --border-light: #f0f0f0;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-bg: #eff6ff;
  --error: #dc2626;
  --success: #16a34a;
  --disabled: #94a3b8;
  --shadow: rgba(0, 0, 0, 0.08);
  --shadow-heavy: rgba(0, 0, 0, 0.15);
  --overlay: rgba(0, 0, 0, 0.4);
  /* SVG grid colors */
  --grid-bg: #ffffff;
  --grid-selected: #cce5ff;
  --grid-conflict: #ffcccc;
  --grid-cage-border: #000000;
  --grid-inner-line: #cccccc;
  --grid-cage-label: #666666;
  --grid-digit: #000000;
  --grid-pencil: #666666;
}

[data-theme="dark"] {
  --bg: #1a1a1a;
  --bg-card: #2a2a2a;
  --bg-hover: #333;
  --bg-active: #444;
  --bg-stripe: #252525;
  --text: #e5e5e5;
  --text-secondary: #aaa;
  --text-muted: #777;
  --border: #444;
  --border-light: #333;
  --accent: #5b9cf6;
  --accent-hover: #4a8ae6;
  --accent-bg: #1e2d42;
  --error: #f87171;
  --success: #4ade80;
  --disabled: #555;
  --shadow: rgba(0, 0, 0, 0.3);
  --shadow-heavy: rgba(0, 0, 0, 0.5);
  --overlay: rgba(0, 0, 0, 0.6);
  --grid-bg: #2a2a2a;
  --grid-selected: #1e3a5f;
  --grid-conflict: #5f2020;
  --grid-cage-border: #ccc;
  --grid-inner-line: #444;
  --grid-cage-label: #aaa;
  --grid-digit: #e5e5e5;
  --grid-pencil: #999;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
}

header h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
}

.header-controls {
  display: flex;
  gap: 8px;
}

.header-controls button {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  line-height: 1;
  color: var(--text);
}

.header-controls button:hover {
  background: var(--bg-hover);
}

/* ---- App container ---- */

#app {
  max-width: 500px;
  margin: 0 auto;
  padding: 16px;
}

/* ---- Puzzle picker ---- */

.puzzle-picker {
  display: flex;
  gap: 8px;
  justify-content: center;
  padding: 8px 0 4px;
}

.picker-select {
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  background: var(--bg-card);
  color: var(--text);
  cursor: pointer;
}

/* ---- Stats bar ---- */

.stats-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  font-size: 14px;
  color: var(--text-secondary);
}

.stat-timer {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--text);
}

.stat-moves {
  font-variant-numeric: tabular-nums;
}

/* ---- Grid container ---- */

.grid-container svg {
  width: 100%;
  height: auto;
  display: block;
}

/* ---- Number pad ---- */

.number-pad {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  padding: 12px 0;
}

.number-pad button {
  width: 48px;
  height: 48px;
  font-size: 20px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-card);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
}

.number-pad button:hover {
  background: var(--bg-hover);
}

.number-pad button:active {
  background: var(--bg-active);
}

.number-pad button.pencil-toggle.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ---- Results view ---- */

.results-view {
  text-align: center;
  padding: 24px 0;
}

.results-stats h2 {
  margin: 0 0 16px;
  font-size: 22px;
}

.stat-row {
  display: flex;
  gap: 24px;
  justify-content: center;
  margin-bottom: 24px;
}

.stat {
  background: var(--bg-card);
  border-radius: 8px;
  padding: 16px 24px;
  box-shadow: 0 1px 3px var(--shadow);
  min-width: 120px;
}

.stat-value {
  font-size: 28px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stat-label {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.results-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 16px;
}

/* ---- Buttons ---- */

.btn {
  padding: 10px 24px;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.next-btn {
  background: var(--accent);
  color: #fff;
}

.next-btn:hover {
  background: var(--accent-hover);
}

.share-btn {
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
}

.share-btn:hover {
  background: var(--bg-hover);
}

/* ---- Modal ---- */

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--overlay);
  z-index: 100;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--bg-card);
  border-radius: 12px;
  box-shadow: 0 4px 24px var(--shadow-heavy);
  width: 90%;
  max-width: 420px;
  max-height: 85vh;
  overflow-y: auto;
  color: var(--text);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 0;
}

.modal-header h2 {
  margin: 0;
  font-size: 18px;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 0 4px;
  line-height: 1;
}

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

.modal-body {
  padding: 16px 20px 20px;
}

/* ---- Settings ---- */

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--border-light);
}

.setting-row span {
  font-size: 15px;
}

.setting-row select,
.setting-row input[type="checkbox"] {
  font-size: 14px;
}

.setting-row select {
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-card);
  color: var(--text);
}

.setting-section {
  margin-top: 16px;
}

.setting-section h3 {
  font-size: 15px;
  font-weight: 600;
  margin: 0 0 8px;
  color: var(--text-secondary);
}

#account-info {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

.account-username {
  font-weight: 600;
  color: var(--text);
}

.account-pubkey {
  font-size: 12px;
  font-family: monospace;
  color: var(--text-muted);
  margin-top: 2px;
}

.account-actions {
  display: flex;
  gap: 8px;
}

.account-actions button {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}

.account-actions button:hover {
  background: var(--bg-hover);
}

/* ---- Identity transfer modals ---- */

.step { display: none; }
.step.active { display: block; }

.qr-canvas {
  display: flex;
  justify-content: center;
  margin: 16px 0;
  min-height: 80px;
}

.qr-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

.qr-info button, .copy-qr-data {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}

.copy-qr-data {
  display: block;
  width: 100%;
}

.qr-info button:hover, .copy-qr-data:hover {
  background: var(--bg-hover);
}

#qr-scanner {
  width: 100%;
  max-height: 200px;
  background: #000;
  border-radius: 8px;
  margin: 8px 0;
  display: none;
}

#qr-scanner[src] { display: block; }

#qr-data-input {
  width: 100%;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: monospace;
  resize: vertical;
  background: var(--bg-card);
  color: var(--text);
}

.start-camera, .use-manual, .confirm-import, .cancel-import {
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  margin: 4px 0;
}

.confirm-import {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.cancel-import:hover, .start-camera:hover, .use-manual:hover {
  background: var(--bg-hover);
}

.confirm-import:hover {
  background: var(--accent-hover);
}

/* ---- Registration ---- */

.register-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 12px;
}

.register-form input[type="text"] {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  outline: none;
  background: var(--bg-card);
  color: var(--text);
}

.register-form input[type="text"]:focus {
  border-color: var(--accent);
}

#username-status {
  font-size: 13px;
  min-height: 18px;
}

#username-status.available {
  color: var(--success);
}

#username-status.taken {
  color: var(--error);
}

#register-btn {
  padding: 10px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}

#register-btn:disabled {
  background: var(--disabled);
  cursor: not-allowed;
}

#register-btn:not(:disabled):hover {
  background: var(--accent-hover);
}

/* ---- Leaderboard ---- */

.leaderboard-controls {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.leaderboard-select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  background: var(--bg-card);
  color: var(--text);
}

.leaderboard-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 12px;
}

.leaderboard-tab {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}

.leaderboard-tab.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.leaderboard-table th {
  text-align: left;
  padding: 6px 8px;
  font-weight: 600;
  border-bottom: 2px solid var(--border);
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: uppercase;
}

.leaderboard-table td {
  padding: 8px;
  border-bottom: 1px solid var(--border-light);
}

.leaderboard-table tbody tr:nth-child(even) {
  background: var(--bg-stripe);
}

.leaderboard-table tbody tr.current-user {
  background: var(--accent-bg);
  font-weight: 600;
}

.leaderboard-empty,
.leaderboard-loading,
.leaderboard-error {
  text-align: center;
  color: var(--text-secondary);
  padding: 24px 8px;
}

.leaderboard-error {
  color: var(--error);
}

.leaderboard-snippet {
  margin-top: 16px;
}

/* ---- Rules & About ---- */

.rules-body h3 {
  font-size: 15px;
  margin: 16px 0 6px;
}

.rules-body ul {
  margin: 6px 0;
  padding-left: 20px;
}

.rules-body li {
  margin: 4px 0;
  font-size: 14px;
}

.rules-body p {
  font-size: 14px;
  line-height: 1.5;
}

.rules-footer {
  margin-top: 20px;
  text-align: center;
}

.rules-footer button {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
  text-decoration: underline;
}

.about-body p {
  font-size: 14px;
  line-height: 1.6;
  margin: 0 0 12px;
  text-align: justify;
}

.about-separator {
  border-top: 1px solid var(--border);
  margin: 16px 0;
  height: 0;
}

.about-build {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  text-align: center;
}
