/**
 * Cellular Evolution Simulator
 * Scholarly Instrumental Modernism design
 */

/* ============================================
   CSS Custom Properties
   ============================================ */
:root {
  --ink: #000000;
  --ink-secondary: #333333;
  --paper: #ffffff;
  --paper-subtle: #f8f8f8;
  --border: #e0e0e0;
  --border-strong: #cccccc;
  --muted: #666666;
  --accent: #1a1a1a;
  --font-serif: "EB Garamond", Georgia, "Times New Roman", serif;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --radius: 2px;
}

/* ============================================
   Base Reset
   ============================================ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background: var(--paper);
  color: var(--ink);
  line-height: 1.6;
  font-size: 14px;
}

.container {
  max-width: 1600px;
  margin: 0 auto;
  padding: 10px;
}

/* ============================================
   Welcome Screen
   ============================================ */
#welcome-screen {
  background: var(--paper);
  border: 1px solid var(--border);
  padding: 2rem;
  max-width: 900px;
  margin: 1.5rem auto;
}

#welcome-screen h1 {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: clamp(1.75rem, 3vw, 2.5rem);
  color: var(--ink);
  margin-bottom: 1.5rem;
  text-align: center;
}

.welcome-content {
  max-height: 500px;
  overflow-y: auto;
  padding: 1.25rem;
  background: var(--paper-subtle);
  border: 1px solid var(--border);
  margin-bottom: 1.5rem;
}

.welcome-content p {
  margin-bottom: 0.75rem;
  color: var(--ink-secondary);
}

.welcome-content h2 {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 1.25rem;
  color: var(--ink);
  margin-top: 1.5rem;
  margin-bottom: 0.75rem;
}

.welcome-content h2:first-child {
  margin-top: 0;
}

.welcome-content h3 {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 1rem;
  color: var(--ink-secondary);
  margin-top: 1rem;
  margin-bottom: 0.5rem;
}

.welcome-content ul {
  margin: 0.75rem 0 1rem 1.25rem;
  padding-left: 1rem;
}

.welcome-content li {
  margin-bottom: 0.375rem;
}

.cell-type-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.5rem;
  margin: 1rem 0;
}

.cell-type-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  background: var(--paper);
  border: 1px solid var(--border);
}

.cell-color-box {
  width: 20px;
  height: 20px;
  border: 1px solid var(--border-strong);
  flex-shrink: 0;
}

.welcome-footer {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.welcome-footer .btn {
  margin-left: auto;
}

.welcome-footer label {
  font-weight: 500;
  font-size: 0.875rem;
}

.welcome-footer select {
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.875rem;
  background: var(--paper);
}

/* ============================================
   Buttons
   ============================================ */
.btn {
  padding: 0.5rem 1rem;
  border: 1px solid var(--ink);
  border-radius: var(--radius);
  font-family: var(--font-sans);
  font-size: 0.8125rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 150ms ease, color 150ms ease;
}

.btn-primary {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}

.btn-primary:hover {
  background: var(--ink-secondary);
}

.btn-primary:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

.btn-secondary {
  background: var(--paper);
  color: var(--ink);
  border-color: var(--border-strong);
}

.btn-secondary:hover {
  background: var(--paper-subtle);
  border-color: var(--ink-secondary);
}

.btn-small {
  padding: 0.375rem 0.75rem;
  font-size: 0.75rem;
}

/* ============================================
   Simulation Screen
   ============================================ */
#simulation-screen {
  display: none;
  height: 100vh;
  padding: 0;
  overflow: hidden;
}

#simulation-screen.active {
  display: flex;
  flex-direction: column;
}

/* ============================================
   Top Toolbar
   ============================================ */
.top-toolbar {
  background: var(--paper);
  border-bottom: 1px solid var(--border);
  padding: 0.5rem 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  flex-shrink: 0;
}

.toolbar-title {
  font-family: var(--font-serif);
  font-size: 1.125rem;
  font-weight: 400;
  color: var(--ink);
  margin-right: 0.5rem;
}

.toolbar-divider {
  width: 1px;
  height: 1.25rem;
  background: var(--border);
}

/* ============================================
   Main Layout
   ============================================ */
.main-layout {
  display: grid;
  grid-template-columns: 320px 1fr 300px;
  gap: 0.5rem;
  flex: 1;
  min-height: 0;
  padding: 0.5rem;
  overflow: hidden;
}

@media (max-width: 1600px) {
  .main-layout {
    grid-template-columns: 1fr 300px;
  }
  .legend-panel {
    display: none;
  }
}

@media (max-width: 1100px) {
  .main-layout {
    grid-template-columns: 1fr;
  }
  .right-panel {
    display: none;
  }
}

/* ============================================
   Bottom Panel (Organism Inspector)
   ============================================ */
.bottom-panel {
  background: var(--paper);
  border-top: 1px solid var(--border);
  padding: 0.75rem 1rem;
  display: none;
  flex-shrink: 0;
  height: 200px;
  overflow: hidden;
  position: relative;
}

.bottom-panel.active {
  display: flex;
  gap: 1.25rem;
  align-items: stretch;
}

.bottom-panel-close {
  position: absolute;
  top: 0.5rem;
  right: 0.75rem;
  width: 1.5rem;
  height: 1.5rem;
  border: 1px solid var(--border-strong);
  background: var(--paper);
  color: var(--ink);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.bottom-panel-close:hover {
  background: var(--paper-subtle);
  border-color: var(--ink);
}

.bottom-panel-content {
  display: flex;
  gap: 1.25rem;
  width: 100%;
  height: 100%;
}

/* Organism preview */
.organism-preview {
  flex: 0 0 140px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.organism-preview h4 {
  margin: 0 0 0.375rem 0;
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 0.9375rem;
  color: var(--ink);
}

#organism-shape-canvas {
  background: #1a1a2e;
  width: 120px;
  height: 120px;
}

.organism-preview-label {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.25rem;
}

/* Organism stats */
.organism-stats {
  flex: 0 0 160px;
}

.organism-stats h4 {
  margin: 0 0 0.375rem 0;
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 0.9375rem;
  color: var(--ink);
}

.organism-stats .stats-grid {
  font-size: 0.8125rem;
}

/* Organism composition */
.organism-comp {
  flex: 0 0 180px;
}

.organism-comp h4 {
  margin: 0 0 0.375rem 0;
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 0.9375rem;
  color: var(--ink);
}

/* Neural network container */
.neural-network-container {
  flex: 1;
  min-width: 250px;
  display: flex;
  flex-direction: column;
}

.nn-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.375rem;
}

.nn-header h4 {
  margin: 0;
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 0.9375rem;
  color: var(--ink);
}

.nn-header span {
  font-size: 0.75rem;
  color: var(--muted);
}

#nn-canvas {
  background: #1a1a2e;
  flex: 1;
  width: 100%;
}

/* ============================================
   Legend Panel (Left)
   ============================================ */
.legend-panel {
  background: var(--paper);
  border: 1px solid var(--border);
  padding: 1rem;
  min-height: 0;
  overflow-y: auto;
}

.legend-panel h2 {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 1.125rem;
  color: var(--ink);
  margin-bottom: 0.75rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  margin-bottom: 0.75rem;
  padding: 0.5rem;
  background: var(--paper-subtle);
  border: 1px solid var(--border);
}

.legend-color {
  width: 24px;
  height: 24px;
  border: 1px solid var(--border-strong);
  flex-shrink: 0;
}

.legend-info {
  flex: 1;
}

.legend-info strong {
  display: block;
  color: var(--ink);
  margin-bottom: 0.125rem;
  font-size: 0.875rem;
}

.legend-info span {
  font-size: 0.8125rem;
  color: var(--muted);
}

.legend-section {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.legend-section h3 {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 1rem;
  color: var(--ink);
  margin-bottom: 0.5rem;
}

/* ============================================
   Canvas Panel (Center)
   ============================================ */
.canvas-panel {
  background: transparent;
  min-height: 0;
  overflow: hidden;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

.canvas-hint {
  position: absolute;
  bottom: 0.625rem;
  left: 50%;
  transform: translateX(-50%);
  padding: 0.375rem 0.75rem;
  background: rgba(255, 255, 255, 0.95);
  color: var(--ink-secondary);
  font-size: 0.75rem;
  border: 1px solid var(--border);
  pointer-events: none;
  white-space: nowrap;
}

.canvas-container {
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: auto;
  max-width: 100%;
  max-height: 100%;
}

.canvas-container.zoomed-in {
  justify-content: flex-start;
  align-items: flex-start;
}

#sim-canvas {
  display: block;
  cursor: pointer;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  flex-shrink: 0;
  border: 1px solid var(--border);
}

/* ============================================
   Right Panel (Statistics)
   ============================================ */
.right-panel {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-height: 0;
  overflow-y: auto;
}

.info-panel {
  background: var(--paper);
  border: 1px solid var(--border);
  padding: 0.75rem;
}

.info-panel h2 {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 1.125rem;
  color: var(--ink);
  margin-bottom: 0.5rem;
  padding-bottom: 0.375rem;
  border-bottom: 1px solid var(--border);
}

.info-panel h3 {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 1rem;
  color: var(--ink);
  margin-top: 0.75rem;
  margin-bottom: 0.375rem;
}

.stats-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.125rem 0.75rem;
  font-size: 0.8125rem;
}

.stats-grid dt {
  color: var(--muted);
}

.stats-grid dd {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.chart-container {
  width: 100%;
  height: 100px;
  margin: 0.5rem 0;
  border: 1px solid var(--border);
  background: var(--paper);
}

.organism-composition {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.comp-item {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.8125rem;
}

.comp-color {
  width: 14px;
  height: 14px;
  border: 1px solid var(--border-strong);
}

.cell-dist-bars {
  margin-top: 0.5rem;
}

.cell-bar {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  margin-bottom: 0.375rem;
}

.cell-bar-label {
  width: 55px;
  font-size: 0.75rem;
  font-weight: 500;
}

.cell-bar-bg {
  flex: 1;
  height: 14px;
  background: var(--paper-subtle);
  border: 1px solid var(--border);
  overflow: hidden;
  position: relative;
}

.cell-bar-fill {
  height: 100%;
  transition: width 0.3s;
}

.cell-bar-count {
  width: 40px;
  text-align: right;
  font-size: 0.8125rem;
  font-family: var(--font-mono);
}

/* ============================================
   Control Groups (Toolbar)
   ============================================ */
.control-group {
  display: flex;
  align-items: center;
  gap: 0.375rem;
}

.control-group label {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--ink-secondary);
  white-space: nowrap;
}

.control-group select,
.control-group input[type="number"] {
  padding: 0.25rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.75rem;
  background: var(--paper);
}

.control-group input[type="range"] {
  width: 70px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}

.control-group input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  background: var(--ink);
  border-radius: 50%;
  cursor: pointer;
}

.control-group input[type="range"]::-moz-range-thumb {
  width: 12px;
  height: 12px;
  background: var(--ink);
  border: none;
  border-radius: 50%;
  cursor: pointer;
}

.control-group input[type="checkbox"] {
  width: 14px;
  height: 14px;
  accent-color: var(--ink);
}

.button-group {
  display: flex;
  gap: 0.375rem;
}

.param-value {
  font-size: 0.6875rem;
  font-family: var(--font-mono);
  color: var(--muted);
  min-width: 50px;
}

/* ============================================
   Modal Overlay
   ============================================ */
.modal-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 2000;
  justify-content: center;
  align-items: center;
}

.modal-overlay.active {
  display: flex;
}

.modal-content {
  background: var(--paper);
  border: 1px solid var(--border);
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--paper-subtle);
}

.modal-header h2 {
  margin: 0;
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 1.25rem;
  color: var(--ink);
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--muted);
  padding: 0;
  line-height: 1;
}

.modal-close:hover {
  color: var(--ink);
}

.modal-body {
  padding: 1.25rem;
}

.modal-footer {
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 0.625rem;
}

/* Guide Modal */
#guide-modal .modal-content {
  max-width: 700px;
  width: 90%;
}

#guide-modal .modal-body {
  max-height: 60vh;
  overflow-y: auto;
}

#guide-modal h3 {
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 1.125rem;
  color: var(--ink);
  margin-top: 1.25rem;
  margin-bottom: 0.625rem;
}

#guide-modal h3:first-child {
  margin-top: 0;
}

#guide-modal ul {
  margin-left: 1.25rem;
}

#guide-modal li {
  margin-bottom: 0.375rem;
}

/* Designer Modal */
#designer-modal .modal-content {
  max-width: 800px;
  width: 95%;
}

.designer-layout {
  display: flex;
  gap: 1.25rem;
  align-items: flex-start;
}

.designer-palette {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-width: 100px;
}

.palette-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  background: var(--paper);
  transition: background 150ms ease, border-color 150ms ease;
}

.palette-btn:hover {
  background: var(--paper-subtle);
}

.palette-btn.active,
.palette-btn.selected {
  border-color: var(--ink);
  background: var(--paper-subtle);
}

.palette-color {
  width: 18px;
  height: 18px;
  border: 1px solid var(--border-strong);
}

.designer-grid-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.designer-grid {
  display: grid;
  grid-template-columns: repeat(12, 26px);
  grid-template-rows: repeat(12, 26px);
  gap: 1px;
  background: var(--border);
  padding: 1px;
}

.designer-cell {
  width: 26px;
  height: 26px;
  background: #1a1a2e;
  cursor: pointer;
  transition: opacity 100ms ease;
}

.designer-cell:hover {
  opacity: 0.8;
}

.designer-preview {
  text-align: center;
  min-width: 140px;
}

.designer-preview canvas {
  border: 1px solid var(--border);
  background: #1a1a2e;
}

.designer-stats {
  margin-top: 0.625rem;
  font-size: 0.875rem;
}

.designer-stats .valid {
  color: #16a34a;
}

.designer-stats .invalid {
  color: #dc2626;
}

/* History Modal */
#history-modal .modal-content {
  max-width: 1100px;
  width: 95%;
}

.history-layout {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.history-charts {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.history-chart-container {
  background: var(--paper-subtle);
  padding: 1rem;
  border: 1px solid var(--border);
}

.history-chart-container h4 {
  margin: 0 0 0.625rem 0;
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 0.9375rem;
  color: var(--ink);
}

.history-chart-container canvas {
  width: 100%;
  height: 120px;
  border: 1px solid var(--border);
  background: var(--paper);
}

.history-stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 1rem;
}

.history-stat-section {
  background: var(--paper-subtle);
  padding: 1rem;
  border: 1px solid var(--border);
}

.history-stat-section h4 {
  margin: 0 0 0.75rem 0;
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 0.9375rem;
  color: var(--ink);
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.375rem;
}

.species-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.species-list li {
  padding: 0.5rem 0.625rem;
  margin-bottom: 0.375rem;
  background: var(--paper);
  border: 1px solid var(--border);
  border-left: 3px solid var(--ink);
  font-size: 0.875rem;
}

.species-list li:nth-child(1) { border-left-color: #d97706; }
.species-list li:nth-child(2) { border-left-color: #6b7280; }
.species-list li:nth-child(3) { border-left-color: #9333ea; }

.species-name {
  font-weight: 600;
  color: var(--ink);
}

.species-detail {
  color: var(--muted);
  font-size: 0.8125rem;
  margin-top: 0.125rem;
}

.stat-summary {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.625rem;
}

.stat-item {
  background: var(--paper);
  padding: 0.625rem;
  border: 1px solid var(--border);
  text-align: center;
}

.stat-value {
  font-size: 1.25rem;
  font-weight: 600;
  font-family: var(--font-mono);
  color: var(--ink);
}

.stat-label {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.125rem;
}

/* Species Card */
.species-card {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.625rem;
  margin-bottom: 0.5rem;
  background: var(--paper);
  border: 1px solid var(--border);
  border-left: 3px solid var(--ink);
}

.species-card:nth-child(1) { border-left-color: #d97706; }
.species-card:nth-child(2) { border-left-color: #6b7280; }
.species-card:nth-child(3) { border-left-color: #9333ea; }
.species-card:nth-child(4) { border-left-color: #2563eb; }
.species-card:nth-child(5) { border-left-color: #16a34a; }

.species-card-body {
  flex-shrink: 0;
}

.species-body-preview {
  background: #1a1a2e;
}

.species-card-info {
  flex: 1;
  min-width: 0;
}

.species-composition {
  font-size: 0.8125rem;
  line-height: 1.5;
  margin-bottom: 0.25rem;
}

.species-card .species-detail {
  margin-top: 0;
}

/* ============================================
   Scrollbar Styling
   ============================================ */
.welcome-content::-webkit-scrollbar,
.right-panel::-webkit-scrollbar,
.legend-panel::-webkit-scrollbar {
  width: 6px;
}

.welcome-content::-webkit-scrollbar-track,
.right-panel::-webkit-scrollbar-track,
.legend-panel::-webkit-scrollbar-track {
  background: var(--paper-subtle);
}

.welcome-content::-webkit-scrollbar-thumb,
.right-panel::-webkit-scrollbar-thumb,
.legend-panel::-webkit-scrollbar-thumb {
  background: var(--border-strong);
}

.welcome-content::-webkit-scrollbar-thumb:hover,
.right-panel::-webkit-scrollbar-thumb:hover,
.legend-panel::-webkit-scrollbar-thumb:hover {
  background: var(--muted);
}

/* ============================================
   Reduced Motion
   ============================================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ============================================
   Focus States
   ============================================ */
button:focus-visible,
select:focus-visible,
input:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}
