/* ═══════════════════════════════════════════════════════════════════════════
   main.css — App layout, header, controls, status bar, modals, dialogs
   ═══════════════════════════════════════════════════════════════════════════ */

/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  background: var(--color-bg);
  color: var(--color-text);
  transition: background 0.4s ease, color 0.2s ease;
}

body {
  display: flex;
  flex-direction: column;
  background-image: none; /* overridden by bg classes */
  background-size: cover;
  background-position: center;
}

/* Scrollbars */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 3px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }

/* ── Typography ── */
h1 { font-size: 1.4rem; font-weight: 700; letter-spacing: 0.05em; }
h2 { font-size: 1.25rem; font-weight: 600; margin-bottom: 1.2rem; }
h3 { font-size: 0.85rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.08em; color: var(--color-text-muted); margin-bottom: 0.75rem; }

/* ── Utility ── */
.hidden { display: none !important; }

/* ── Header ── */
#app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 52px;
  flex-shrink: 0;
  background: var(--header-bg);
  border-bottom: 1px solid var(--header-border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 100;
}

.app-title {
  background: linear-gradient(90deg, var(--color-accent), var(--color-accent-2));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

#header-left, #header-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* ── Icon buttons ── */
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--color-text);
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}
.icon-btn:hover { background: var(--ctrl-bg-hover); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg { width: 20px; height: 20px; }

#btn-music-mute.active {
  background: var(--ctrl-bg-hover);
  color: var(--color-accent);
}

.music-icon-mute {
  opacity: 0;
}

#btn-music-mute.active .music-icon-wave {
  opacity: 0;
}

#btn-music-mute.active .music-icon-mute {
  opacity: 1;
}

/* ── Controls bar ── */
#controls-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
  flex-shrink: 0;
  background: var(--header-bg);
  border-bottom: 1px solid var(--header-border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.ctrl-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border: 1px solid var(--ctrl-border);
  border-radius: 8px;
  background: var(--ctrl-bg);
  color: var(--color-text);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s, opacity 0.2s;
  white-space: nowrap;
}
.ctrl-btn:hover { background: var(--ctrl-bg-hover); border-color: var(--color-border-strong); }
.ctrl-btn:active { transform: scale(0.95); }
.ctrl-btn:disabled { opacity: 0.35; cursor: not-allowed; transform: none; }
.ctrl-btn svg { width: 16px; height: 16px; flex-shrink: 0; }

/* ── Board area ── */
#board-wrapper {
  flex: 1;
  overflow: hidden;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

#board-container {
  width: 100%;
  height: 100%;
  overflow: auto;
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 20px;
  scroll-behavior: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: auto;
}

#board {
  position: relative;
  flex-shrink: 0;
  margin: auto;
  /* width/height set by renderer */
}

/* Loading overlay */
#board-loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: var(--color-bg);
  opacity: 0.9;
  z-index: 10;
  color: var(--color-text-muted);
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--color-border-strong);
  border-top-color: var(--color-accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Status bar ── */
#status-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 32px;
  padding: 8px 16px;
  height: 48px;
  flex-shrink: 0;
  background: var(--status-bg);
  border-top: 1px solid var(--status-border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.status-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--color-text-muted);
}
.status-item svg { width: 16px; height: 16px; flex-shrink: 0; }
.status-item span { font-variant-numeric: tabular-nums; }

#status-time.warning { color: var(--color-accent); }
#status-time.warning svg { animation: pulse-color 1s ease-in-out infinite alternate; }
@keyframes pulse-color {
  from { opacity: 0.6; }
  to   { opacity: 1; }
}

.status-shake {
  animation: status-shake 0.4s ease;
}
@keyframes status-shake {
  0%,100% { transform: translateX(0); }
  25%      { transform: translateX(-4px); }
  75%      { transform: translateX(4px); }
}

/* ── Modal overlays ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--modal-overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  padding: 16px;
  animation: fade-in 0.15s ease;
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.modal-box {
  background: var(--modal-bg);
  border: 1px solid var(--modal-border);
  border-radius: 16px;
  padding: 28px 28px 24px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--modal-shadow);
  animation: modal-slide-in 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-height: calc(100vh - 32px);
  overflow-y: auto;
}

.modal-wide { max-width: 520px; }

@keyframes modal-slide-in {
  from { transform: scale(0.94) translateY(8px); opacity: 0; }
  to   { transform: scale(1) translateY(0); opacity: 1; }
}

/* ── Forms ── */
.form-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}
.form-row label {
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--color-text);
  flex-shrink: 0;
}
.form-row input[type="number"],
.form-row select {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--color-border-strong);
  border-radius: 8px;
  background: var(--color-surface-2);
  color: var(--color-text);
  font-size: 0.9rem;
  appearance: none;
  -webkit-appearance: none;
  max-width: 160px;
}
.form-row select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 28px;
}
.form-row input[type="number"]:focus,
.form-row select:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 2px var(--color-accent-glow);
}

.form-row input[type="range"] {
  width: 100%;
  accent-color: var(--color-accent);
}

.input-unit {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  max-width: 200px;
}
.input-unit input { max-width: 80px; }
.input-unit span { font-size: 0.8rem; color: var(--color-text-muted); white-space: nowrap; }

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  flex: 1;
  justify-content: flex-start;
}
.checkbox-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--color-accent);
  cursor: pointer;
  flex-shrink: 0;
}

/* ── Buttons ── */
.form-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
}

.btn-primary, .btn-secondary {
  padding: 10px 22px;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: transform 0.1s, box-shadow 0.1s, background 0.15s;
}
.btn-primary:active, .btn-secondary:active { transform: scale(0.96); }

.btn-primary {
  background: var(--color-accent);
  color: #fff;
  box-shadow: 0 4px 16px var(--color-accent-glow);
}
.btn-primary:hover { background: var(--color-accent-hover); box-shadow: 0 6px 20px var(--color-accent-glow); }

.btn-secondary {
  background: var(--ctrl-bg);
  color: var(--color-text);
  border: 1px solid var(--ctrl-border);
}
.btn-secondary:hover { background: var(--ctrl-bg-hover); }

/* ── Toggle switch ── */
.toggle {
  display: inline-flex;
  cursor: pointer;
  flex-shrink: 0;
}
.toggle input { display: none; }
.toggle-track {
  width: 44px;
  height: 24px;
  background: var(--color-surface-2);
  border: 1px solid var(--color-border-strong);
  border-radius: 12px;
  position: relative;
  transition: background 0.2s, border-color 0.2s;
}
.toggle-track::after {
  content: '';
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--color-text-muted);
  top: 2px;
  left: 2px;
  transition: transform 0.2s, background 0.2s;
}
.toggle input:checked + .toggle-track { background: var(--color-accent); border-color: var(--color-accent); }
.toggle input:checked + .toggle-track::after { transform: translateX(20px); background: #fff; }

.toggle-row { gap: 16px; }

.volume-row {
  align-items: center;
}

.range-control {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.range-control span {
  width: 44px;
  text-align: right;
  color: var(--color-text-muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

/* ── Settings grid ── */
.settings-grid {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.settings-section {
  border: 1px solid var(--color-border);
  border-radius: 12px;
  padding: 16px;
}
.settings-section .form-row:last-child { margin-bottom: 0; }

@media (max-width: 520px) {
  .volume-row {
    align-items: flex-start;
  }

  .range-control {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
  }

  .range-control span {
    width: auto;
    text-align: left;
  }
}

/* Background picker */
.bg-picker {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}
.bg-preset {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 2px solid transparent;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
  flex-shrink: 0;
}
.bg-preset:hover { transform: scale(1.1); }
.bg-preset.active { border-color: var(--color-accent); box-shadow: 0 0 0 1px var(--color-accent); }
.bg-upload-btn {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 2px dashed var(--color-border-strong);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--color-text-muted);
  transition: border-color 0.15s, color 0.15s;
  flex-shrink: 0;
}
.bg-upload-btn:hover { border-color: var(--color-accent); color: var(--color-accent); }
.bg-upload-btn svg { width: 18px; height: 18px; }

/* ── Game Over modal ── */
.modal-gameover { text-align: center; }
.go-icon {
  font-size: 3rem;
  margin-bottom: 8px;
}
.go-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 16px 0;
}
.stat-item {
  background: var(--color-surface-2);
  border-radius: 10px;
  padding: 12px;
  text-align: center;
}
.stat-label {
  display: block;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 4px;
}
.stat-value {
  display: block;
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--color-text);
}
.personal-best {
  display: inline-block;
  padding: 6px 16px;
  background: linear-gradient(90deg, var(--color-accent), var(--color-accent-2));
  border-radius: 20px;
  font-size: 0.9rem;
  font-weight: 600;
  color: #fff;
  margin-bottom: 4px;
  animation: pulse-best 1.5s ease-in-out infinite;
}
@keyframes pulse-best {
  0%,100% { box-shadow: 0 0 8px var(--color-accent-glow); }
  50%      { box-shadow: 0 0 20px var(--color-accent-glow); }
}

/* ── Credits ── */
.credits-pre {
  font-family: inherit;
  font-size: 0.85rem;
  line-height: 1.6;
  white-space: pre-wrap;
  color: var(--color-text);
  max-height: 60vh;
  overflow-y: auto;
  margin-bottom: 0.5rem;
}

/* ── High scores ── */
#scores-content {
  max-height: 60vh;
  overflow-y: auto;
}
.score-group {
  margin-bottom: 16px;
}
.score-group-title {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--color-text-muted);
  margin-bottom: 8px;
}
.score-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}
.score-table th {
  text-align: left;
  padding: 6px 10px;
  color: var(--color-text-muted);
  font-weight: 600;
  border-bottom: 1px solid var(--color-border);
}
.score-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--color-border);
}
.score-table tr:last-child td { border-bottom: none; }
.score-rank-1 td { color: var(--color-accent); font-weight: 600; }
.scores-empty { text-align: center; color: var(--color-text-muted); padding: 32px 0; }

/* ── Toast ── */
#toast {
  position: fixed;
  bottom: 64px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--color-surface-2);
  color: var(--color-text);
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 0.9rem;
  font-weight: 500;
  border: 1px solid var(--color-border-strong);
  opacity: 0;
  pointer-events: none;
  z-index: 2000;
  transition: opacity 0.2s, transform 0.2s;
  white-space: nowrap;
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Welcome / empty state ── */
#board-welcome {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--color-text-muted);
  pointer-events: none;
  user-select: none;
}
#board-welcome h2 { color: var(--color-text); font-size: 1.6rem; }
#board-welcome p { font-size: 0.95rem; }

/* ══════════════════════════════════════════════════════════════════════════
   Responsive
   ══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 600px) {
  #app-header { height: 48px; padding: 0 12px; }
  h1 { font-size: 1.2rem; }

  #controls-bar { padding: 5px 8px; gap: 6px; }
  .ctrl-btn { padding: 6px 10px; font-size: 0.8rem; }
  .ctrl-btn svg { width: 14px; height: 14px; }

  #status-bar { gap: 20px; padding: 6px 12px; height: 44px; }
  .status-item { font-size: 0.82rem; }

  .modal-box { padding: 20px 18px 18px; border-radius: 14px; }
  .modal-wide { max-width: 100%; }

  .go-stats { grid-template-columns: 1fr 1fr; gap: 8px; }
}

@media (max-width: 380px) {
  .ctrl-btn span { display: none; }
  .ctrl-btn { padding: 8px; min-width: 40px; justify-content: center; }
  #status-bar { gap: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
