:root {
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  background: #000;
  display: flex;
  justify-content: center;
  align-items: center;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

canvas {
  display: block;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

/* --- Overlays --- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  background: rgba(10, 10, 15, 0.95);
  color: #fff;
  font-family: monospace;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.overlay.hidden {
  display: none;
}

/* --- Gallery --- */

.gallery-header {
  position: sticky;
  top: 0;
  z-index: 2;
  background: rgba(10, 10, 15, 0.98);
  padding: 16px 16px 12px;
  padding-top: max(16px, env(safe-area-inset-top, 16px));
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.gallery-close {
  position: absolute;
  top: 12px;
  right: 14px;
  background: none;
  border: none;
  color: #fff;
  font-size: 28px;
  cursor: pointer;
  opacity: 0.5;
  line-height: 1;
}

.gallery-filters {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.gallery-sorts,
.gallery-time-filters {
  display: flex;
  gap: 6px;
}

.gallery-sorts button,
.gallery-time-filters button {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.5);
  font-family: monospace;
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
}

.gallery-sorts button.active,
.gallery-time-filters button.active {
  background: rgba(255, 204, 102, 0.15);
  border-color: rgba(255, 204, 102, 0.4);
  color: #ffcc66;
}

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

.gallery-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 16px 24px;
  padding-bottom: max(24px, env(safe-area-inset-bottom, 24px));
}

.lb-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.03);
  cursor: pointer;
  transition: background 0.15s;
}

.lb-row:active {
  background: rgba(255, 204, 102, 0.1);
}

.lb-row-me {
  background: rgba(255, 204, 102, 0.06);
  border-left: 2px solid rgba(255, 204, 102, 0.4);
}

.lb-rank {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.35);
  min-width: 28px;
  text-align: right;
}

.lb-row:nth-child(1) .lb-rank { color: #ffcc66; }
.lb-row:nth-child(2) .lb-rank { color: #cccccc; }
.lb-row:nth-child(3) .lb-rank { color: #cc9966; }

.lb-thumb {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  object-fit: cover;
  image-rendering: pixelated;
  flex-shrink: 0;
}

.lb-thumb-empty {
  background: rgba(255, 255, 255, 0.04);
}

.lb-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.lb-name {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.7);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lb-details {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.35);
}

.gallery-empty,
.gallery-loading {
  text-align: center;
  padding: 60px 20px;
  color: rgba(255, 255, 255, 0.3);
  font-size: 14px;
}

/* --- Name input overlay --- */

#name-overlay {
  justify-content: center;
  align-items: center;
  background: rgba(10, 10, 15, 0.9);
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
}

.name-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: min(280px, 80vw);
}

.name-form input {
  width: 100%;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  color: #fff;
  font-family: monospace;
  font-size: 16px;
  padding: 10px 14px;
  text-align: center;
  outline: none;
}

.name-form input:focus {
  border-color: rgba(255, 204, 102, 0.5);
}

.name-buttons {
  display: flex;
  gap: 10px;
}

.name-save,
.name-cancel {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 0.7);
  font-family: monospace;
  font-size: 14px;
  padding: 8px 20px;
  border-radius: 6px;
  cursor: pointer;
}

.name-save {
  background: rgba(255, 204, 102, 0.15);
  border-color: rgba(255, 204, 102, 0.4);
  color: #ffcc66;
}

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

.settings-panel {
  max-width: 360px;
  width: 100%;
  margin: 0 auto;
  padding: 24px 20px;
  padding-top: max(24px, env(safe-area-inset-top, 24px));
  padding-bottom: max(40px, env(safe-area-inset-bottom, 40px));
}

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 28px;
}

.settings-title {
  font-size: 20px;
  color: rgba(255, 255, 255, 0.8);
  letter-spacing: 0.05em;
}

.settings-close {
  background: none;
  border: none;
  color: #fff;
  font-size: 28px;
  cursor: pointer;
  opacity: 0.5;
  line-height: 1;
  padding: 4px 8px;
}

.settings-close:active {
  opacity: 0.8;
}

.settings-section {
  margin-bottom: 8px;
}

.settings-section-title {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.35);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 12px;
}

.settings-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.settings-label {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
}

.settings-toggle {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: rgba(255, 255, 255, 0.4);
  font-family: monospace;
  font-size: 12px;
  padding: 5px 14px;
  border-radius: 4px;
  cursor: pointer;
  min-width: 48px;
  text-align: center;
  transition: all 0.15s;
}

.settings-toggle.active {
  background: rgba(255, 204, 102, 0.15);
  border-color: rgba(255, 204, 102, 0.4);
  color: #ffcc66;
}

.settings-desc {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.25);
  line-height: 1.6;
  margin-top: 8px;
}

.settings-desc code {
  background: rgba(255, 255, 255, 0.06);
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 11px;
  color: rgba(255, 204, 102, 0.7);
}

.settings-link-hint {
  color: rgba(255, 255, 255, 0.15);
  font-style: italic;
}

.settings-name-input {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 4px;
  color: #fff;
  font-family: monospace;
  font-size: 14px;
  padding: 6px 12px;
  width: 120px;
  text-align: right;
  outline: none;
}

.settings-name-input:focus {
  border-color: rgba(255, 204, 102, 0.5);
}

.settings-api-key-input {
  width: 150px;
  font-size: 12px;
}

.settings-custom-url-input {
  width: 190px;
  font-size: 11px;
}

.settings-select {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 4px;
  color: #fff;
  font-family: monospace;
  font-size: 13px;
  padding: 5px 8px;
  outline: none;
  cursor: pointer;
}

.settings-select:focus {
  border-color: rgba(255, 204, 102, 0.5);
}

.settings-select option {
  background: #1a1a24;
  color: #fff;
}

.settings-tryit-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 10px;
}

.settings-tryit {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.6);
  font-family: monospace;
  font-size: 13px;
  padding: 10px 14px;
  border-radius: 6px;
  cursor: pointer;
  text-align: left;
  transition: all 0.15s;
}

.settings-tryit:active {
  background: rgba(255, 204, 102, 0.1);
  border-color: rgba(255, 204, 102, 0.3);
  color: #ffcc66;
}

.settings-tryit-output {
  margin-top: 8px;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 4px;
  font-size: 11px;
  color: rgba(255, 204, 102, 0.7);
  font-family: monospace;
  line-height: 1.5;
  max-height: 120px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

.settings-tryit-output.hidden {
  display: none;
}

.settings-divider {
  height: 1px;
  background: rgba(255, 255, 255, 0.06);
  margin: 16px 0;
}

/* Collapsible settings section (AI Player) */

.settings-collapsible {
  border: none;
  outline: none;
}

.settings-collapsible-summary {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.35);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 0;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 0;
  user-select: none;
  -webkit-user-select: none;
}

.settings-collapsible-summary::-webkit-details-marker {
  display: none;
}

.settings-collapsible-summary::after {
  content: '+';
  font-size: 14px;
  color: rgba(255, 255, 255, 0.25);
  line-height: 1;
}

.settings-collapsible[open] .settings-collapsible-summary::after {
  content: '\2212'; /* − */
}

.settings-collapsible[open] .settings-collapsible-summary {
  margin-bottom: 12px;
}
