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

:root {
  --bg-deep: #0d0a08;
  --bg-stone: #1c1814;
  --bg-panel: #25201a;
  --bg-panel-light: #2f2920;
  --border-stone: #4a3f30;
  --border-ruby: #8b2a2a;
  --ruby: #d33b3b;
  --ruby-glow: #ff5555;
  --gold: #d4a849;
  --text: #e8ddc8;
  --text-dim: #9c8e74;
  --text-bright: #fff5dc;
  --grass: #5a8a3a;
  --wood: #8b6239;
  --stone: #7a7a7a;
  --leaves: #3a5e2a;
  --planks: #b08550;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-deep);
  color: var(--text);
  font-family: 'VT323', 'Courier New', monospace;
  font-size: 18px;
  user-select: none;
  -webkit-user-select: none;
}

#loading-screen {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse at center, #2a1814 0%, #0d0a08 70%),
    var(--bg-deep);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  transition: opacity 0.6s ease;
}

.loading-content {
  text-align: center;
  max-width: 500px;
  padding: 20px;
}

.title-glitch {
  font-family: 'Press Start 2P', monospace;
  font-size: 56px;
  color: var(--ruby);
  text-shadow:
    0 0 20px var(--ruby-glow),
    4px 4px 0 var(--bg-deep),
    -2px -2px 0 var(--gold);
  letter-spacing: 4px;
  margin-bottom: 8px;
  animation: pulse-glow 2s ease-in-out infinite;
}

@keyframes pulse-glow {
  0%, 100% { text-shadow: 0 0 20px var(--ruby-glow), 4px 4px 0 var(--bg-deep), -2px -2px 0 var(--gold); }
  50% { text-shadow: 0 0 40px var(--ruby-glow), 0 0 60px var(--ruby), 4px 4px 0 var(--bg-deep), -2px -2px 0 var(--gold); }
}

.subtitle {
  color: var(--text-dim);
  font-size: 22px;
  margin-bottom: 40px;
  letter-spacing: 2px;
}

.loading-bar {
  width: 100%;
  height: 16px;
  background: var(--bg-stone);
  border: 2px solid var(--border-stone);
  margin-bottom: 12px;
  overflow: hidden;
  box-shadow: inset 0 0 8px rgba(0,0,0,0.6);
}

#loading-progress {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, var(--ruby), var(--gold));
  transition: width 0.3s ease;
  box-shadow: 0 0 12px var(--ruby-glow);
}

#loading-text {
  color: var(--text-dim);
  font-size: 18px;
  margin-bottom: 24px;
}

#start-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 14px;
  padding: 16px 32px;
  background: var(--bg-stone);
  color: var(--text-bright);
  border: 3px solid var(--ruby);
  cursor: pointer;
  letter-spacing: 2px;
  transition: all 0.2s ease;
  text-transform: uppercase;
}

#start-btn:not(:disabled):hover {
  background: var(--ruby);
  color: var(--text-bright);
  box-shadow: 0 0 20px var(--ruby-glow);
  transform: scale(1.05);
}

#start-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  border-color: var(--border-stone);
}

#game-container {
  position: fixed;
  inset: 0;
  display: block;
}

#game-container.hidden { display: none; }
.hidden { display: none !important; }

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
}

#top-bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 56px;
  background: linear-gradient(180deg, var(--bg-panel) 0%, var(--bg-stone) 100%);
  border-bottom: 2px solid var(--border-stone);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 10;
  box-shadow: 0 4px 12px rgba(0,0,0,0.6);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: 'Press Start 2P', monospace;
  font-size: 14px;
}

.brand-ruby {
  color: var(--ruby);
  font-size: 22px;
  text-shadow: 0 0 8px var(--ruby-glow);
}

.brand-text {
  color: var(--text-bright);
  letter-spacing: 2px;
}

.resources {
  display: flex;
  gap: 18px;
  align-items: center;
}

.resource {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-panel-light);
  padding: 6px 12px;
  border: 1px solid var(--border-stone);
  font-size: 20px;
  color: var(--text-bright);
  min-width: 70px;
  justify-content: center;
}

.resource .icon { font-size: 18px; }
.resource .muted { color: var(--text-dim); }

.res-icon {
  width: 20px;
  height: 20px;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text);
  padding: 4px;
}
.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--ruby);
  cursor: pointer;
}

#now-playing {
  color: var(--ruby);
  font-style: italic;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: inline-block;
  vertical-align: middle;
}

#btn-music, #btn-sfx {
  font-size: 16px;
}

#btn-music.muted, #btn-sfx.muted {
  opacity: 0.4;
  text-decoration: line-through;
}

.top-actions {
  display: flex;
  gap: 6px;
}

.top-actions button {
  width: 36px;
  height: 36px;
  background: var(--bg-panel-light);
  border: 1px solid var(--border-stone);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
}

.top-actions button:hover {
  background: var(--ruby);
  color: var(--text-bright);
  border-color: var(--ruby-glow);
}

.panel {
  position: absolute;
  top: 70px;
  width: 220px;
  max-height: calc(100vh - 130px);
  background: rgba(28, 24, 20, 0.92);
  backdrop-filter: blur(4px);
  border: 2px solid var(--border-stone);
  padding: 12px;
  z-index: 8;
  overflow-y: auto;
  box-shadow: 0 4px 16px rgba(0,0,0,0.6);
}

#left-panel { left: 8px; }
#right-panel { right: 8px; width: 260px; }

.panel-header h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: var(--gold);
  letter-spacing: 2px;
  margin-bottom: 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border-stone);
}

.palette {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.palette-item {
  position: relative;
  aspect-ratio: 1;
  background: var(--bg-panel-light);
  border: 2px solid var(--border-stone);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  transition: all 0.15s;
  overflow: hidden;
}

.palette-item:hover {
  border-color: var(--gold);
  transform: translateY(-2px);
}

.palette-item.active {
  border-color: var(--ruby);
  background: rgba(211, 59, 59, 0.2);
  box-shadow: 0 0 12px var(--ruby-glow);
}

.palette-item .swatch {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  image-rendering: pixelated;
}

.palette-item .label {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0,0,0,0.7);
  color: var(--text-bright);
  font-size: 12px;
  text-align: center;
  padding: 2px;
  z-index: 2;
  font-family: 'VT323', monospace;
}

.palette-item .hotkey {
  position: absolute;
  top: 2px;
  right: 4px;
  background: rgba(0,0,0,0.7);
  color: var(--gold);
  font-size: 12px;
  padding: 0 4px;
  z-index: 2;
  font-family: 'VT323', monospace;
}

.brush-control {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-panel-light);
  padding: 8px;
  border: 1px solid var(--border-stone);
}

.brush-control input[type="range"] {
  flex: 1;
  accent-color: var(--ruby);
}

.brush-control span {
  min-width: 24px;
  text-align: center;
  color: var(--gold);
  font-size: 20px;
}

.tabs {
  display: flex;
  border-bottom: 2px solid var(--border-stone);
  margin-bottom: 10px;
}

.tab {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 8px 4px;
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  cursor: pointer;
  letter-spacing: 1px;
  transition: all 0.15s;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
}

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

.tab.active {
  color: var(--gold);
  border-bottom-color: var(--ruby);
}

.tab-content { display: none; }
.tab-content.active { display: block; }

.build-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 8px;
}

.build-template {
  background: var(--bg-panel-light);
  border: 2px solid var(--border-stone);
  padding: 8px 4px;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s;
}

.build-template:hover {
  border-color: var(--gold);
  transform: translateY(-2px);
}

.build-template.active {
  border-color: var(--ruby);
  background: rgba(211, 59, 59, 0.2);
  box-shadow: 0 0 12px var(--ruby-glow);
}

.build-template .ico {
  font-size: 28px;
  display: block;
  margin-bottom: 4px;
}

.build-template .name {
  font-size: 14px;
  color: var(--text);
  display: block;
}

.build-template .cost {
  font-size: 12px;
  color: var(--text-dim);
  display: block;
  margin-top: 2px;
}

.hint {
  font-size: 14px;
  color: var(--text-dim);
  font-style: italic;
  padding: 4px;
  line-height: 1.3;
}

.hero-list {
  max-height: 280px;
  overflow-y: auto;
  margin-bottom: 10px;
}

.hero-card {
  background: var(--bg-panel-light);
  border: 1px solid var(--border-stone);
  padding: 8px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  gap: 8px;
}

.hero-card:hover { border-color: var(--gold); }
.hero-card.selected {
  border-color: var(--ruby);
  box-shadow: 0 0 8px var(--ruby-glow);
}

.hero-avatar {
  width: 32px;
  height: 32px;
  background: var(--bg-deep);
  border: 1px solid var(--border-stone);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.hero-info { flex: 1; min-width: 0; }
.hero-name { color: var(--text-bright); font-size: 16px; }
.hero-task { color: var(--text-dim); font-size: 13px; }
.hero-hp-bar {
  width: 100%;
  height: 4px;
  background: var(--bg-deep);
  margin-top: 4px;
  border: 1px solid var(--border-stone);
}
.hero-hp-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--grass), #8bc34a);
  transition: width 0.3s;
}

.hero-actions { display: flex; flex-direction: column; gap: 6px; }

.hero-actions button,
#tab-world button,
#btn-spawn-hero,
#btn-rally {
  width: 100%;
  padding: 8px;
  background: var(--bg-panel-light);
  border: 1px solid var(--border-stone);
  color: var(--text);
  font-family: inherit;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.15s;
}

.hero-actions button:hover,
#tab-world button:hover,
#btn-spawn-hero:hover,
#btn-rally:hover {
  background: var(--ruby);
  color: var(--text-bright);
  border-color: var(--ruby-glow);
}

.world-controls {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.world-controls label {
  font-size: 14px;
  color: var(--text-dim);
  margin-top: 8px;
  letter-spacing: 1px;
}

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

.world-controls select,
.world-controls input[type="number"] {
  width: 100%;
  padding: 6px 8px;
  background: var(--bg-deep);
  border: 1px solid var(--border-stone);
  color: var(--text);
  font-family: inherit;
  font-size: 16px;
}

.seed-row {
  display: flex;
  gap: 6px;
}

.seed-row input { flex: 1; }
.seed-row button { flex: 0 0 auto; padding: 6px 10px; }

#time-label {
  font-size: 16px;
  color: var(--gold);
  text-align: center;
  padding: 2px;
  background: var(--bg-deep);
  border: 1px solid var(--border-stone);
}

#bottom-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 36px;
  background: linear-gradient(180deg, var(--bg-stone) 0%, var(--bg-panel) 100%);
  border-top: 2px solid var(--border-stone);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  font-size: 16px;
  z-index: 10;
  box-shadow: 0 -4px 12px rgba(0,0,0,0.6);
}

.info {
  display: flex;
  gap: 8px;
  color: var(--text-dim);
  align-items: center;
}

.info .sep { color: var(--border-stone); }

#coords, #fps, #selected-block {
  color: var(--gold);
  font-family: 'VT323', monospace;
}

.mode-info {
  color: var(--ruby);
  font-family: 'Press Start 2P', monospace;
  font-size: 11px;
  letter-spacing: 1px;
}

.quick-help {
  color: var(--text-dim);
  font-size: 14px;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}

.modal-content {
  background: var(--bg-panel);
  border: 3px solid var(--border-stone);
  padding: 24px 32px;
  max-width: 600px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 0 40px rgba(0,0,0,0.8);
}

.modal-content h2 {
  font-family: 'Press Start 2P', monospace;
  font-size: 18px;
  color: var(--ruby);
  margin-bottom: 20px;
  text-align: center;
  text-shadow: 0 0 8px var(--ruby-glow);
}

.help-section { margin-bottom: 16px; }
.help-section h4 {
  color: var(--gold);
  font-size: 18px;
  margin-bottom: 6px;
  letter-spacing: 1px;
}
.help-section ul { list-style: none; padding-left: 8px; }
.help-section li {
  color: var(--text);
  font-size: 16px;
  padding: 2px 0;
  line-height: 1.4;
}
.help-section b { color: var(--text-bright); }

#close-help {
  display: block;
  margin: 20px auto 0;
  padding: 10px 24px;
  background: var(--ruby);
  color: var(--text-bright);
  border: 2px solid var(--ruby-glow);
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
  cursor: pointer;
  letter-spacing: 2px;
  transition: all 0.15s;
}

#close-help:hover {
  background: var(--ruby-glow);
  transform: scale(1.05);
}

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: var(--bg-deep); }
::-webkit-scrollbar-thumb {
  background: var(--border-stone);
  border: 1px solid var(--bg-deep);
}
::-webkit-scrollbar-thumb:hover { background: var(--ruby); }

@media (max-width: 900px) {
  .panel { width: 180px; }
  #right-panel { width: 220px; }
  .quick-help { display: none; }
  .resources { gap: 8px; }
  .resource { padding: 4px 8px; min-width: 50px; font-size: 16px; }
  .brand-text { display: none; }
}

@media (max-width: 640px) {
  #left-panel, #right-panel {
    width: calc(50% - 12px);
    max-height: 200px;
    top: 64px;
  }
  #left-panel { left: 4px; }
  #right-panel { right: 4px; }
  .title-glitch { font-size: 36px; }
  .subtitle { font-size: 16px; }
  .top-actions button { width: 32px; height: 32px; font-size: 14px; }
  .resource .icon { font-size: 14px; }
  #bottom-bar { font-size: 12px; padding: 0 8px; }
  .info { gap: 4px; }
}
