* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

:root {
  --bg: #263449;
  --surface: #34445a;
  --surface-2: #405570;
  --text: #f1f5f9;
  --muted: #b2becb;
  --ice: #86b7ff;
  --gold: #86b7ff;
  --border: rgba(164, 194, 232, 0.18);
}

html, body {
  background: radial-gradient(1200px 800px at 50% -100px, #34445a 0%, #263449 65%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  height: 100%;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

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

#topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: rgba(38, 52, 73, 0.92);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 10;
  flex-shrink: 0;
}

#online-badge {
  font-size: 14px;
  padding: 6px 12px;
  background: var(--surface);
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--ice);
}

#top-buttons { display: flex; gap: 8px; }

#top-buttons button {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  transition: background 0.15s;
}
#top-buttons button:active { background: var(--surface-2); }

#canvas-wrap {
  flex: 1;
  overflow: hidden;
  position: relative;
  touch-action: none;
  background: radial-gradient(600px 400px at 50% 50%, rgba(134, 183, 255, 0.07) 0%, transparent 60%);
}

#field,
#pixel-grid {
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  will-change: transform;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#field {
  background: #ffffff;
  box-shadow: 0 0 60px rgba(0, 0, 0, 0.32);
}

#pixel-grid {
  pointer-events: none;
  z-index: 1;
}

#bottom {
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0px));
  background: rgba(38, 52, 73, 0.94);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#palette {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.color-btn {
  width: 32px; height: 32px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s, border-color 0.15s;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15);
}
.color-btn.selected {
  border-color: var(--gold);
  box-shadow: 0 0 12px rgba(134, 183, 255, 0.55), inset 0 0 0 1px rgba(0,0,0,0.15);
  transform: scale(1.18);
}

#cooldown-row { display: flex; justify-content: center; }

#cooldown-timer {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  padding: 8px 24px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  min-width: 120px;
  text-align: center;
  transition: all 0.25s;
  letter-spacing: 0.02em;
}
#cooldown-timer.ready {
  color: var(--gold);
  border-color: var(--gold);
  animation: pulse 1.6s infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 16px rgba(134, 183, 255, 0.22); }
  50% { box-shadow: 0 0 28px rgba(134, 183, 255, 0.48); }
}

.modal {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.72);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--surface);
  border-radius: 16px;
  padding: 20px;
  max-width: 420px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 16px;
}
.modal-header h2 {
  font-size: 20px;
  color: var(--gold);
  font-weight: 700;
}
.modal-close {
  background: none; border: none;
  color: var(--muted);
  font-size: 22px;
  cursor: pointer;
  width: 32px; height: 32px;
}

#leaderboard-list { display: flex; flex-direction: column; }

.leader-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 4px;
  border-bottom: 1px solid rgba(164, 194, 232, 0.12);
  font-size: 15px;
}
.leader-row:last-child { border-bottom: none; }
.leader-row .leader-name { color: var(--text); }
.leader-row .leader-score { color: var(--ice); font-weight: 700; }
.leader-row.top-1 .leader-name { color: var(--gold); font-weight: 600; }
.leader-row.top-1 .leader-score { color: var(--gold); }

.admin-section {
  margin-bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.admin-section label { font-size: 13px; color: var(--muted); }
.admin-section input {
  padding: 10px 12px;
  background: #263449;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  width: 100%;
}
.admin-section button {
  padding: 10px 14px;
  background: var(--ice);
  color: #ffffff;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  font-size: 14px;
}
.admin-section button:active { background: #5b8fe5; }

#toasts {
  position: fixed;
  top: calc(60px + env(safe-area-inset-top, 0px));
  right: 12px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 300px;
}
.toast {
  background: linear-gradient(135deg, #86b7ff 0%, #5b8fe5 100%);
  color: #172033;
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.4;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.32);
  animation: slideIn 0.3s ease-out;
}
@keyframes slideIn {
  from { transform: translateX(120%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}
.toast.leaving { animation: slideOut 0.3s ease-in forwards; }
@keyframes slideOut { to { transform: translateX(120%); opacity: 0; } }

#error-overlay {
  position: fixed; inset: 0;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 30px;
  text-align: center;
  z-index: 1000;
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
}
#error-overlay.hidden { display: none; }