/* --- Design Tokens & Styling System --- */
:root {
  /* Core Colors */
  --bg-deep: #070913;
  --bg-surface: rgba(13, 17, 34, 0.65);
  --bg-surface-solid: #0d1122;
  --bg-card: rgba(22, 28, 54, 0.45);
  --border-light: rgba(255, 255, 255, 0.08);
  --border-focus: rgba(59, 130, 246, 0.5);
  
  /* Neon Accents */
  --color-accent: #3b82f6;      /* Electric Blue */
  --color-accent-glow: rgba(59, 130, 246, 0.35);
  --color-herbivore: #10b981;   /* Emerald Green */
  --color-herbivore-glow: rgba(16, 185, 129, 0.3);
  --color-carnivore: #ef4444;   /* Crimson Red */
  --color-carnivore-glow: rgba(239, 68, 68, 0.3);
  --color-warning: #f59e0b;     /* Amber Orange */
  --color-warning-glow: rgba(245, 158, 11, 0.3);
  
  /* Text colors */
  --text-main: #f3f4f6;
  --text-muted: #9ca3af;
  --text-dark: #1f2937;
  
  /* Fonts */
  --font-family-body: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-family-mono: 'Space Mono', monospace;
  
  /* Transitions */
  --transition-fast: 0.15s ease;
  --transition-normal: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  
  color-scheme: dark;
}

/* --- Base Resets & Setup --- */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background-color: var(--bg-deep);
  color: var(--text-main);
  font-family: var(--font-family-body);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* Scrollbar Customization */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.2);
}
::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.12);
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.25);
}

/* --- Main Layout Architecture --- */
#app-container {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  width: 100vw;
}

/* Header styling */
#app-header {
  background: rgba(10, 12, 22, 0.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border-light);
  padding: 0.8rem 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 10;
  
  .logo-area {
    display: flex;
    align-items: center;
    gap: 0.8rem;
    
    h1 {
      font-size: 1.4rem;
      font-weight: 800;
      letter-spacing: -0.025em;
      background: linear-gradient(135deg, #ffffff 40%, #8bb1ff);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
    
    .icon {
      font-size: 1.6rem;
      animation: spin 8s linear infinite;
    }
  }
}

/* Header badge */
.badge {
  font-family: var(--font-family-mono);
  font-size: 0.65rem;
  font-weight: 700;
  padding: 0.2rem 0.5rem;
  background: rgba(59, 130, 246, 0.15);
  color: var(--color-accent);
  border: 1px solid rgba(59, 130, 246, 0.3);
  border-radius: 4px;
  letter-spacing: 0.05em;
  text-shadow: 0 0 8px var(--color-accent-glow);
}

.header-stats {
  display: flex;
  gap: 1.5rem;
}

.header-stat-box {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  
  .label {
    font-size: 0.65rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }
  
  .value {
    font-family: var(--font-family-mono);
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-main);
  }
}

/* App Body Grid */
#app-body {
  display: grid;
  grid-template-columns: 300px 1fr 310px;
  overflow: hidden;
  height: 100%;
}

/* --- Panel Component Styles --- */
aside {
  background: var(--bg-surface);
  backdrop-filter: blur(16px);
  border-inline-end: 1px solid var(--border-light);
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  z-index: 5;
}

#sidebar-stats {
  border-inline-end: none;
  border-inline-start: 1px solid var(--border-light);
}

/* Cards inside Sidebars */
.control-card, .stats-card {
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: 12px;
  padding: 1rem;
  
  h2 {
    font-size: 0.9rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 0.8rem;
    color: var(--text-main);
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    padding-bottom: 0.4rem;
  }
}

/* Inputs and Forms elements */
.control-group {
  margin-bottom: 0.8rem;
  
  &:last-child {
    margin-bottom: 0;
  }
}

.control-header {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  margin-bottom: 0.3rem;
  color: var(--text-muted);
  
  label {
    font-weight: 600;
  }
  
  .control-val {
    font-family: var(--font-family-mono);
    color: var(--color-accent);
    font-weight: 700;
  }
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  outline: none;
  margin: 8px 0;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--color-accent);
  box-shadow: 0 0 8px var(--color-accent-glow);
  cursor: pointer;
  transition: transform var(--transition-fast);
  
  &:hover {
    transform: scale(1.3);
  }
}

.help-text {
  font-size: 0.65rem;
  color: var(--text-muted);
  display: block;
  margin-top: 0.2rem;
  line-height: 1.3;
}

/* Button & Tool Grid Styling */
.button-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.8rem;
}

.btn {
  font-family: var(--font-family-body);
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  transition: all var(--transition-fast);
  outline: none;
}

.btn-primary {
  background: var(--color-accent);
  color: #fff;
  box-shadow: 0 4px 10px rgba(59, 130, 246, 0.2);
  
  &:hover {
    background: #2563eb;
    box-shadow: 0 4px 15px rgba(59, 130, 246, 0.35);
  }
}

.btn-danger {
  background: rgba(239, 68, 68, 0.15);
  border-color: rgba(239, 68, 68, 0.3);
  color: #fca5a5;
  
  &:hover {
    background: var(--color-carnivore);
    color: #fff;
  }
}

.btn-warning {
  background: rgba(245, 158, 11, 0.15);
  border-color: rgba(245, 158, 11, 0.3);
  color: #fcd34d;
  
  &:hover {
    background: var(--color-warning);
    color: var(--text-dark);
  }
}

.btn-block {
  width: 100%;
  padding: 0.6rem;
  font-size: 0.75rem;
  margin-bottom: 0.5rem;
}

.btn-sm {
  padding: 0.3rem 0.6rem;
  font-size: 0.7rem;
  border-radius: 4px;
}

.action-buttons-stack {
  margin-top: 0.8rem;
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  padding-top: 0.8rem;
}

.tool-select-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.4rem;
  margin-bottom: 0.8rem;
}

.tool-btn {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border-light);
  border-radius: 6px;
  color: var(--text-muted);
  padding: 0.5rem 0.3rem;
  font-size: 0.7rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  transition: all var(--transition-fast);
  outline: none;
  
  .tool-icon {
    font-size: 1.1rem;
  }
  
  &:hover {
    background: rgba(255, 255, 255, 0.06);
    color: var(--text-main);
  }
  
  &.active {
    background: rgba(59, 130, 246, 0.12);
    border-color: var(--color-accent);
    color: var(--text-main);
    box-shadow: 0 0 10px rgba(59, 130, 246, 0.2);
  }
  
  &.danger:hover {
    background: rgba(239, 68, 68, 0.15);
    border-color: rgba(239, 68, 68, 0.4);
    color: #fca5a5;
  }
}

/* Focus styles */
.btn:focus-visible, .tool-btn:focus-visible, input:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

/* --- Simulation Petri Dish Container --- */
#petri-container {
  display: flex;
  position: relative;
  background-color: #030408;
  overflow: hidden;
  height: 100%;
}

.canvas-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
  
  canvas {
    display: block;
    width: 100%;
    height: 100%;
    cursor: crosshair;
  }
}

/* Overlay overlay */
#canvas-overlay-message {
  position: absolute;
  top: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(239, 68, 68, 0.15);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: #fca5a5;
  font-family: var(--font-family-mono);
  font-size: 0.8rem;
  font-weight: 700;
  padding: 0.4rem 1rem;
  border-radius: 20px;
  letter-spacing: 0.1em;
  pointer-events: none;
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
  transition: opacity 0.3s ease;
  z-index: 2;
  
  &.hidden {
    opacity: 0;
  }
}

/* Floating controls */
#floating-canvas-controls {
  position: absolute;
  bottom: 1.5rem;
  right: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  z-index: 2;
}

.float-btn {
  background: rgba(20, 26, 46, 0.7);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border-light);
  color: var(--text-main);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
  outline: none;
  
  &:hover {
    background: rgba(59, 130, 246, 0.2);
    border-color: var(--color-accent);
    transform: translateY(-2px);
  }
  
  &:focus-visible {
    outline: 2px solid var(--color-accent);
  }
}

/* --- Ecological Progress Bars --- */
.eco-bars {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  margin-bottom: 0.8rem;
}

.eco-bar-container {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.eco-bar-label {
  display: flex;
  justify-content: space-between;
  font-size: 0.7rem;
  color: var(--text-muted);
  font-weight: 600;
}

.progress-track {
  width: 100%;
  height: 6px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.3s ease;
  
  &.herbivore {
    background: var(--color-herbivore);
    box-shadow: 0 0 6px var(--color-herbivore-glow);
  }
  
  &.carnivore {
    background: var(--color-carnivore);
    box-shadow: 0 0 6px var(--color-carnivore-glow);
  }
  
  &.energy {
    background: linear-gradient(90deg, #ef4444, #f59e0b, #10b981);
  }
  
  &.age {
    background: #3b82f6;
  }
}

/* Grid stats layout */
.grid-stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  padding-top: 0.8rem;
  margin-top: 0.4rem;
}

.grid-stat-item {
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid rgba(255, 255, 255, 0.03);
  padding: 0.4rem;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  
  .stat-lbl {
    font-size: 0.6rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.02em;
  }
  
  .stat-val {
    font-family: var(--font-family-mono);
    font-size: 0.8rem;
    font-weight: 700;
    margin-top: 0.1rem;
  }
}

/* --- Inspector Styling --- */
#inspector-placeholder {
  color: var(--text-muted);
  font-size: 0.75rem;
  line-height: 1.5;
  text-align: center;
  padding: 1.5rem 0;
}

#inspector-content {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.creature-header {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  background: rgba(255, 255, 255, 0.03);
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.04);
  
  h3 {
    font-size: 0.85rem;
    font-weight: 800;
    letter-spacing: -0.01em;
  }
  
  .subtext {
    font-size: 0.65rem;
    color: var(--text-muted);
    font-family: var(--font-family-mono);
  }
}

.creature-color-indicator {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.2);
  box-shadow: 0 0 10px rgba(255,255,255,0.1);
}

.inspect-vitals {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  background: rgba(0, 0, 0, 0.1);
  padding: 0.5rem;
  border-radius: 8px;
}

.vital-row {
  display: grid;
  grid-template-columns: 45px 1fr 65px;
  align-items: center;
  gap: 0.5rem;
  
  .vital-lbl {
    font-size: 0.65rem;
    font-weight: 600;
    color: var(--text-muted);
  }
  
  .progress-track.vital {
    height: 8px;
  }
  
  .vital-val {
    font-family: var(--font-family-mono);
    font-size: 0.65rem;
    font-weight: 700;
    text-align: right;
  }
}

.inspect-table-wrapper {
  border: 1px solid rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  overflow: hidden;
}

.inspect-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.7rem;
  text-align: left;
  
  th, td {
    padding: 0.35rem 0.5rem;
  }
  
  thead th {
    background: rgba(255, 255, 255, 0.03);
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.6rem;
    letter-spacing: 0.02em;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  }
  
  tbody tr {
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
    
    &:last-child {
      border-bottom: none;
    }
    
    td:last-child {
      font-family: var(--font-family-mono);
      text-align: right;
      font-weight: 700;
    }
  }
}

.inspector-actions {
  display: flex;
  justify-content: space-between;
  gap: 0.3rem;
  
  .btn {
    flex: 1;
  }
}

/* Neural Net Visualizer Layout */
.nn-visualizer-container {
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.04);
  border-radius: 10px;
  padding: 0.6rem;
  margin-top: 0.3rem;
  
  h4 {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    margin-bottom: 0.4rem;
  }
  
  canvas {
    background: #020306;
    border-radius: 6px;
    display: block;
    width: 100%;
    height: auto;
  }
}

/* --- Bottom Dashboard / Footer --- */
#app-footer {
  background: rgba(10, 12, 22, 0.95);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border-light);
  display: grid;
  grid-template-columns: 1fr 310px;
  height: 140px;
  overflow: hidden;
  z-index: 10;
}

.footer-panel {
  padding: 0.6rem 1.2rem;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  
  .panel-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 0.4rem;
    
    h2 {
      font-size: 0.75rem;
      font-weight: 800;
      text-transform: uppercase;
      letter-spacing: 0.05em;
      color: var(--text-main);
    }
    
    .help-text {
      margin-top: 0;
    }
  }
}

#species-chart-panel {
  border-inline-end: 1px solid var(--border-light);
}

.chart-wrapper {
  flex: 1;
  position: relative;
  
  canvas {
    width: 100%;
    height: 100%;
    display: block;
  }
}

/* Species List Panel styling */
#species-list-panel {
  padding-inline-start: 1rem;
}

.species-chips-container {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  overflow-y: auto;
  flex: 1;
  padding-bottom: 0.5rem;
  
  .no-species {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-style: italic;
    align-self: center;
    width: 100%;
    text-align: center;
  }
}

/* Species Chip UI */
.species-chip {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 20px;
  padding: 0.2rem 0.5rem;
  cursor: pointer;
  transition: all var(--transition-fast);
  outline: none;
  
  .species-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
  }
  
  .species-name {
    font-size: 0.65rem;
    font-weight: 700;
  }
  
  .species-count {
    font-family: var(--font-family-mono);
    font-size: 0.6rem;
    background: rgba(255, 255, 255, 0.08);
    color: var(--text-muted);
    padding: 0.05rem 0.3rem;
    border-radius: 10px;
  }
  
  &:hover {
    background: rgba(255, 255, 255, 0.08);
    border-color: rgba(255, 255, 255, 0.15);
  }
  
  &.active-filter {
    background: rgba(59, 130, 246, 0.12);
    border-color: var(--color-accent);
    box-shadow: 0 0 8px rgba(59, 130, 246, 0.15);
  }
  
  &:focus-visible {
    outline: 2px solid var(--color-accent);
  }
}

/* --- Animations --- */
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* Utility Hidden utility class */
.hidden {
  display: none !important;
}

/* --- Responsive Adjustments --- */
@media (max-width: 1100px) {
  #app-body {
    grid-template-columns: 260px 1fr 270px;
  }
}
