:root {
  /* Pygame Colors mapped to CSS Variables */
  --sky: #9beaf2;
  --earth: #a3afbf;
  --edge: rgba(78, 56, 41, 0.8);
  --hud-text: #f4f1eb;
  --hud-accent: #32b4dc;
  --hud-accent-rgb: 50, 180, 220;
  
  --bar-default: #236e8c;
  --bar-success: #5f9965;
  --bar-fail: #b54d3a;
  
  /* UI Theme Colors */
  --bg-dark: #0f1519; /* Deep cool gray almost black for background */
  --ink: #f4f1eb; /* Use hud_text for main text */
  --muted: rgba(244, 241, 235, 0.6);
  --glass-bg: rgba(26, 36, 43, 0.65);
  --glass-border: rgba(255, 255, 255, 0.08);
  --glass-highlight: rgba(255, 255, 255, 0.15);
  
  --danger: #b54d3a;
  --success: #5f9965;
}

* {
  box-sizing: border-box;
}

.hidden {
  display: none !important;
}

body.dark-theme {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at top right, #1a2a33 0%, var(--bg-dark) 100%);
  color: var(--ink);
  font-family: 'Outfit', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  overflow: auto;
}

body.modal-open {
  overflow: hidden;
}

button,
input {
  font: inherit;
}

.app-shell {
  display: flex;
  height: 100vh;
  min-height: 640px;
  padding: 20px;
  gap: 24px;
}

/* Sidebar Elements */
.glass-sidebar {
  flex: 0 0 320px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  height: calc(100vh - 40px);
  z-index: 10;
}

.sidebar-header {
  margin-bottom: 8px;
}

.sidebar-header h1 {
  margin: 0;
  font-size: 32px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--hud-text) 0%, var(--hud-accent) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: -0.5px;
}

.sidebar-header h2 {
  margin: 0;
  font-size: 26px;
  font-weight: 600;
  color: var(--hud-text);
}

.identity {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 6px;
  font-size: 14px;
  color: var(--muted);
}

.btn-link {
  color: var(--hud-accent);
  text-decoration: none;
  font-weight: 600;
  transition: opacity 0.2s;
}

.btn-link:hover {
  opacity: 0.8;
}

/* Glassmorphism Panels */
.glass-panel {
  background: var(--glass-bg);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--glass-border);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  padding: 16px;
  transition: transform 0.2s ease;
}

/* Status Grid */
.status-panel {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.status-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.status-item span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.status-item strong {
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.accent-text {
  color: var(--hud-accent);
}

/* Toolbar Buttons */
.toolbar {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.toolbar > button:nth-child(3):last-child {
  grid-column: span 2;
}

.controls-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 0.85rem;
}

.controls-list li {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--muted);
}

.controls-list span {
  flex: 1;
}

kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  border-radius: 4px;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.2), inset 0 1px 0 var(--glass-highlight);
  color: var(--muted);
  font-family: inherit;
  font-size: 0.70rem;
  font-weight: 700;
  line-height: 1;
  padding: 3px 6px;
  min-width: 20px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.btn-glass {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--glass-border);
  color: var(--hud-text);
  border-radius: 10px;
  padding: 10px 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-glass:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: var(--glass-highlight);
  transform: translateY(-1px);
}

.btn-glass.active {
  background: rgba(var(--hud-accent-rgb), 0.2);
  border-color: var(--hud-accent);
  color: var(--hud-accent);
}

.btn-primary {
  background: var(--hud-accent);
  color: var(--bg-dark);
  border: none;
  font-weight: 600;
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-primary:hover {
  box-shadow: 0 0 16px rgba(var(--hud-accent-rgb), 0.4);
  transform: translateY(-1px);
}

.btn-primary-large {
  background: var(--hud-accent);
  color: var(--bg-dark);
  border: none;
  font-weight: 600;
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 16px;
  cursor: pointer;
  width: 100%;
  transition: all 0.2s ease;
  margin-top: 8px;
}
.btn-primary-large:hover {
  background: var(--hud-accent);
  box-shadow: 0 0 20px rgba(var(--hud-accent-rgb), 0.5);
  transform: translateY(-2px);
}

.btn-danger {
  color: #ff8a8a;
}
.btn-danger:hover {
  background: rgba(181, 77, 58, 0.2);
  border-color: var(--bar-fail);
}

.btn-success {
  color: #8affaa;
}
.btn-success:hover {
  background: rgba(85, 181, 58, 0.2);
  border-color: var(--bar-success);
}

/* Logging & Scrollbars */
.message-log {
  flex-grow: 1;
  overflow-y: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}

/* Scrollbar restyling for dark theme */
::-webkit-scrollbar {
  width: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.25);
}

/* Stage and Canvas container */
.stage {
  flex: 1;
  position: relative;
  display: grid;
  place-items: center;
  /* background: rgba(0,0,0,0.2); */ /* subtle inset for stage */
  border-radius: 20px;
  /* box-shadow: inset 0 0 40px rgba(0,0,0,0.5); */ /* Inner shadow */
}

.canvas-wrapper {
  background: var(--sky); 
  /* the canvas will be completely drawn over by the JS rendering, 
     but adding a wrapper shadow adds to the glass effect */
  border-radius: 12px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6), 0 0 0 1px rgba(255,255,255,0.05);
  overflow: hidden;
  display: flex;
}

canvas {
  width: min(calc(100vw - 400px), calc(100vh - 40px));
  max-width: 100%;
  aspect-ratio: 1 / 1;
  touch-action: none;
}

/* Modal Join Panel */
.join-panel.glass-modal {
  position: absolute;
  z-index: 50;
  width: min(400px, 90%);
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 32px;
  background: rgba(20, 28, 36, 0.85); /* Slightly darker for contrast */
}

.join-panel h2 {
  margin: 0;
  font-size: 24px;
}
.join-panel p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.join-panel.hidden {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.95);
}

/* Input Fields */
label {
  display: flex;
  flex-direction: column;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}

input {
  min-height: 48px;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--glass-border);
  border-radius: 10px;
  padding: 10px 14px;
  color: var(--hud-text);
  transition: border-color 0.2s, box-shadow 0.2s;
}

input:focus {
  outline: none;
  border-color: var(--hud-accent);
  box-shadow: 0 0 0 2px rgba(var(--hud-accent-rgb), 0.2);
}

input::placeholder {
  color: rgba(244, 241, 235, 0.3);
}

input[type="checkbox"] {
  min-height: auto;
  width: 18px;
  height: 18px;
  accent-color: var(--hud-accent);
}

.checkbox-label {
  flex-direction: row;
  align-items: center;
  color: var(--hud-text);
  line-height: 1.35;
}

.checkbox-label span {
  flex: 1;
}

.error-text {
  margin: 0;
  color: #ff8a8a;
  font-size: 13px;
  min-height: 20px;
}

/* Gallery specifics */
.gallery-layout {
  --hud-accent: #f47eb6;
  --hud-accent-rgb: 244, 126, 182;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding: 20px;
  gap: 20px;
  max-width: 1400px;
  margin: 0 auto;
}

.gallery-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 20px;
  padding: 16px 24px;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 24px;
}

.header-brand h1 {
  margin: 0;
  background: linear-gradient(135deg, var(--hud-text) 0%, var(--hud-accent) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.gallery-form {
  display: flex;
  align-items: center;
  gap: 12px;
}

.gallery-form input {
  padding: 8px 12px;
  border-radius: 6px;
  border: 1px solid var(--glass-border);
  background: rgba(0, 0, 0, 0.2);
  color: var(--hud-text);
}

.gallery-body {
  display: flex;
  gap: 20px;
  flex: 1;
  min-height: 0;
  align-items: stretch;
}

.gallery-sidebar {
  width: 25%;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  border-radius: 12px;
}

.leaderboard-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.leaderboard-message {
  color: var(--muted);
  font-size: 0.9rem;
  padding: 12px 8px;
  text-align: center;
}

.leaderboard-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 8px;
  transition: all 0.2s ease;
  cursor: pointer;
  border: 1px solid transparent;
}

.leaderboard-item:hover {
  background: rgba(var(--hud-accent-rgb), 0.1);
  transform: translateX(2px);
}

.leaderboard-item.active {
  background: rgba(var(--hud-accent-rgb), 0.15);
  border-color: rgba(var(--hud-accent-rgb), 0.5);
  box-shadow: 0 0 10px rgba(var(--hud-accent-rgb), 0.2);
}

.leaderboard-rank {
  font-weight: bold;
  color: var(--muted);
  width: 24px;
}

.leaderboard-item.rank-1 .leaderboard-rank { color: #FFD700; }
.leaderboard-item.rank-2 .leaderboard-rank { color: #C0C0C0; }
.leaderboard-item.rank-3 .leaderboard-rank { color: #cd7f32; }

.leaderboard-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-score {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: bold;
  color: var(--hud-accent);
}

.gallery-grid {
  display: grid;
  flex: 1;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 20px;
  overflow-y: auto;
  padding: 10px;
  margin: -10px;
}

/* Hide scrollbar for cleaner look if desired, but default is fine */
.gallery-grid::-webkit-scrollbar {
  width: 8px;
}
.gallery-grid::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.1);
  border-radius: 4px;
}
.gallery-grid::-webkit-scrollbar-thumb {
  background: var(--glass-border);
  border-radius: 4px;
}

.gallery-card {
  position: relative;
  display: block;
  padding: 0;
  background: rgba(26, 36, 43, 0.78);
  border: 1px solid var(--glass-border);
  border-radius: 12px;
  color: inherit;
  cursor: pointer;
  overflow: hidden;
  transition: all 0.2s ease;
  aspect-ratio: 1; /* Keep it square like a checkerboard */
}

.gallery-card:nth-child(even) {
  background: rgba(20, 29, 35, 0.88);
}

.gallery-card:hover {
  transform: scale(1.02);
  border-color: var(--glass-highlight);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
  z-index: 10;
}

.gallery-card:focus-visible {
  outline: 2px solid var(--hud-accent);
  outline-offset: 3px;
}

.gallery-card.active {
  border-color: var(--hud-accent);
  box-shadow: 0 0 0 2px rgba(var(--hud-accent-rgb), 0.22), 0 10px 20px rgba(0, 0, 0, 0.3);
}

.gallery-card.deleted {
  border-color: rgba(181, 77, 58, 0.7);
  opacity: 0.72;
}

.gallery-card.deleted .card-canvas {
  filter: grayscale(0.85);
}

.card-canvas {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  aspect-ratio: 1 / 1;
}

.card-info {
  position: absolute;
  bottom: 12px;
  left: 12px;
  max-width: calc(100% - 24px);
  padding: 10px 12px;
  background: rgba(15, 21, 25, 0.82);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  text-align: left;
  pointer-events: none;
}

.card-header {
  position: absolute;
  top: 12px;
  left: 12px;
  max-width: calc(100% - 24px);
  padding: 6px 14px;
  background: rgba(15, 21, 25, 0.82);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 20px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  color: var(--hud-text);
  font-size: 0.9rem;
  pointer-events: none;
  z-index: 2;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

.card-header > span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.contributor-badge {
  font-size: 0.9rem;
  line-height: 1.2;
  overflow: visible !important;
}

.card-stats {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--muted);
}

.card-status {
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 700;
  text-transform: uppercase;
  font-size: 0.7rem;
}

.card-status.success {
  background: rgba(95, 153, 101, 0.2);
  color: var(--success);
}

.card-status.fail {
  background: rgba(181, 77, 58, 0.2);
  color: var(--danger);
}

.gallery-empty {
  grid-column: 1 / -1;
  min-height: 220px;
  display: grid;
  place-items: center;
  color: var(--muted);
  text-align: center;
}

/* Expanded Modal for replay */
.replay-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(3, 8, 11, 0.72);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.replay-modal.hidden {
  display: none;
}

.replay-modal-content {
  display: flex;
  gap: 24px;
  background: var(--bg-dark);
  padding: 24px;
  border-radius: 16px;
  border: 1px solid var(--glass-border);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.5);
  position: relative;
  width: min(1100px, 100%);
  max-height: calc(100vh - 48px);
  overflow: auto;
}

.close-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1.5rem;
  cursor: pointer;
  transition: color 0.2s;
  z-index: 10;
}

.close-btn:hover {
  color: var(--danger);
}

.replay-stage {
  width: min(70vmin, 720px);
  height: auto;
  aspect-ratio: 1 / 1;
  max-width: 800px;
  max-height: 800px;
  flex: 0 0 auto;
}

.replay-stage canvas {
  display: block;
  width: 100%;
  height: 100%;
  aspect-ratio: 1 / 1;
}

.replay-controls {
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: 300px;
}

.row-status {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.replay-toolbar {
  display: flex;
  flex-direction: column;
}

/* Responsive adjustments */
@media (max-width: 900px) {
  .app-shell {
    flex-direction: column;
    height: auto;
    min-height: 100vh;
  }
  
  .glass-sidebar {
    height: auto;
    flex: none;
  }
  
  canvas {
    width: min(calc(100vw - 40px), 80vh);
  }

  .gallery-layout {
    padding: 12px;
  }

  .gallery-header,
  .header-brand,
  .gallery-form {
    align-items: stretch;
    flex-direction: column;
  }

  .gallery-body {
    flex-direction: column;
  }

  .gallery-sidebar {
    width: 100%;
    max-height: 220px;
  }

  .gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    padding-right: 0;
  }

  .replay-modal {
    padding: 12px;
    align-items: flex-start;
  }

  .replay-modal-content {
    flex-direction: column;
    gap: 16px;
    padding: 16px;
    max-height: calc(100vh - 24px);
  }

  .replay-stage {
    width: min(calc(100vw - 56px), 70vh);
    height: auto;
  }

  .replay-controls {
    width: 100%;
  }
}
