/* style.css — dark, touch-first chrome for the ant colony simulation.
 *
 * Layout: a full-screen <canvas id="sim"> with floating overlays created by
 * ui.js — a bottom control/tool bar, a collapsible stats panel (top-left) and a
 * save/load/preset sheet. Everything is sized for thumbs: interactive targets
 * are >= 44px, the bar scrolls horizontally on narrow phones, and the panels
 * float above the canvas without pushing layout around.
 */

:root {
  --bg:            #161009;
  --panel-bg:      rgba(26, 20, 13, 0.94);
  --panel-border:  rgba(255, 255, 255, 0.10);
  --btn-bg:        #2c2318;
  --btn-bg-hover:  #3a2f20;
  --btn-active:    #e8952b;
  --btn-active-fg: #1a1206;
  --text:          #f0e6d8;
  --text-dim:      #b3a691;
  --danger:        #d84a4a;
  --primary:       #2bb5a8;
  --tap-min:       44px;
  --radius:        10px;
  --shadow:        0 6px 24px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* --- canvas ---------------------------------------------------------------- */
#sim {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;   /* gestures belong to input.js, never the browser */
  background: var(--bg);
}

/* --- shared panel styling -------------------------------------------------- */
.panel {
  position: fixed;
  z-index: 20;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  min-height: var(--tap-min);
  padding: 0 14px;
  background: transparent;
  border: 0;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.panel-head.static { cursor: default; }
.panel-title { font-size: 14px; letter-spacing: 0.02em; }
.chevron { transition: transform 0.18s ease; color: var(--text-dim); }
.collapsed .chevron { transform: rotate(-90deg); }

/* --- stats panel ----------------------------------------------------------- */
.stats-panel {
  top: calc(env(safe-area-inset-top, 0px) + 10px);
  left: 10px;
  width: min(260px, calc(100vw - 20px));
}
.stats-panel.collapsed .stats-body { display: none; }

.stats-body {
  padding: 4px 12px 12px;
  font-size: 13px;
}
.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 10px;
  margin-bottom: 10px;
}
.stat-cell {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 3px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}
.stat-k { color: var(--text-dim); }
.stat-v { font-variant-numeric: tabular-nums; font-weight: 600; }

.colony-list { display: flex; flex-direction: column; gap: 6px; }
.colony-row {
  display: grid;
  grid-template-columns: 14px 1fr auto auto;
  align-items: center;
  gap: 8px;
}
.dot { width: 12px; height: 12px; border-radius: 50%; }
.cname { font-weight: 600; }
.cstat {
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
}
.muted { color: var(--text-dim); font-style: italic; }

/* --- inspector panel (tap-to-inspect + edit) ------------------------------ */
/* Top-right float, mirroring the stats panel (top-left). Phone-first: full
 * touch targets, scrolls internally, never overlaps the bottom bar. */
.inspector-panel {
  top: calc(env(safe-area-inset-top, 0px) + 10px);
  right: 10px;
  width: min(280px, calc(100vw - 20px));
  max-height: calc(100vh - var(--bar-h, 132px) - env(safe-area-inset-bottom, 0px) - 40px);
  display: flex;
  flex-direction: column;
}
.inspector-panel.hidden { display: none; }
.inspector-panel .insp-head { border-bottom: 1px solid var(--panel-border); }
.insp-head-l { display: flex; align-items: center; gap: 8px; min-width: 0; }
.insp-head-l .insp-title {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.insp-dot { flex: 0 0 auto; }

.inspector-body {
  padding: 10px 12px 12px;
  font-size: 13px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.insp-grid { grid-template-columns: 1fr auto; margin-bottom: 8px; }

.insp-params {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 6px 0 12px;
  padding-top: 10px;
  border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.insp-field { display: flex; flex-direction: column; gap: 6px; }
.insp-field-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.insp-field label { color: var(--text-dim); font-size: 12px; }
.insp-val { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 13px; }

.insp-number { width: 100%; }
.insp-range {
  width: 100%;
  height: 44px;                 /* touch target */
  margin: 0;
  accent-color: var(--btn-active);
  background: transparent;
  cursor: pointer;
}

.insp-actions {
  display: flex;
  gap: 8px;
  padding-top: 6px;
  border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.insp-actions .btn { flex: 1 1 auto; min-height: 48px; }

/* --- graphs panel (time-series charts) ------------------------------------ */
/* Bottom-centred sheet like the data/more sheets, but taller with an internal
 * scroll so its stack of small-multiple charts never overlaps the bottom bar. */
.charts-panel {
  left: 50%;
  bottom: calc(var(--bar-h, 132px) + env(safe-area-inset-bottom, 0px) + 12px);
  transform: translateX(-50%);
  width: min(440px, calc(100vw - 20px));
  max-height: calc(100vh - var(--bar-h, 132px) - env(safe-area-inset-bottom, 0px) - 40px);
  display: flex;
  flex-direction: column;
}
.charts-panel.hidden { display: none; }
.charts-panel .panel-head { border-bottom: 1px solid var(--panel-border); }

.charts-body {
  padding: 10px 12px 12px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.chart-block { display: flex; flex-direction: column; gap: 6px; }
.chart-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 4px 10px;
}
.chart-title {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 10px; }
.chart-leg { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; }
.chart-leg-dot { width: 8px; height: 8px; border-radius: 50%; flex: 0 0 auto; }
.chart-leg-k { color: var(--text-dim); }
.chart-leg-v { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--text); }
.chart-canvas {
  width: 100%;
  height: 96px;
  display: block;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.22);
}
.chart-hint { font-size: 11px; color: var(--text-dim); }
.charts-empty { font-size: 12px; padding: 6px 0; }

/* --- Killer -> victim matrix grid (charts.js _drawMatrix) ------------------ */
.chart-matrix { overflow-x: auto; }
.cm-grid {
  display: inline-grid;
  gap: 2px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}
.cm-dot {
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 50%;
  flex: 0 0 auto;
}
.cm-corner, .cm-h {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  padding: 2px;
}
.cm-rowh { justify-content: center; color: var(--text-dim); }
.cm-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  padding: 3px 6px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.22);
  color: var(--text);
  font-weight: 700;
}
.cm-diag { background: transparent; color: var(--text-dim); font-weight: 400; }
.cm-pred { background: rgba(255, 122, 89, 0.16); }

/* --- World Rules panel (live global settings) ----------------------------- */
/* Bottom-centred sheet like the graphs/data sheets: collapsible sections of
 * sliders, internal scroll so it never overlaps the bottom bar. Phone-first. */
.world-rules-panel {
  left: 50%;
  bottom: calc(var(--bar-h, 132px) + env(safe-area-inset-bottom, 0px) + 12px);
  transform: translateX(-50%);
  width: min(440px, calc(100vw - 20px));
  max-height: calc(100vh - var(--bar-h, 132px) - env(safe-area-inset-bottom, 0px) - 40px);
  display: flex;
  flex-direction: column;
}
.world-rules-panel.hidden { display: none; }
.world-rules-panel .panel-head { border-bottom: 1px solid var(--panel-border); gap: 8px; }
.world-rules-panel .panel-title { flex: 1 1 auto; }
.wr-reset { min-height: 36px; padding: 0 12px; font-size: 13px; }

.wr-body {
  padding: 6px 12px 12px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.wr-section { border-bottom: 1px solid rgba(255, 255, 255, 0.06); }
.wr-section:last-child { border-bottom: 0; }
.wr-sec-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 46px;
  padding: 0 2px;
  background: transparent;
  border: 0;
  color: var(--text);
  font: inherit;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  cursor: pointer;
}
.wr-sec-title { color: var(--text-dim); }
.wr-section.collapsed .wr-sec-body { display: none; }
.wr-section.collapsed .chevron { transform: rotate(-90deg); }

.wr-sec-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 6px 0 12px;
}
.wr-field { display: flex; flex-direction: column; gap: 5px; }
.wr-field-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.wr-field label { color: var(--text-dim); font-size: 13px; }
.wr-val {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 13px;
  color: var(--text);
}
.wr-range {
  width: 100%;
  height: 44px;                 /* touch target */
  margin: 0;
  accent-color: var(--btn-active);
  background: transparent;
  cursor: pointer;
}
.wr-note { font-size: 11px; color: var(--text-dim); font-style: italic; }

/* --- Generate World panel (procedural world generator) -------------------- */
/* Bottom-centred sheet like the world-rules/graphs/data sheets: a seed row, a
 * generic list of sliders/selects, a prominent Generate button + seed readout.
 * Internal scroll so it never overlaps the bottom bar. Phone-first. */
.gen-panel {
  left: 50%;
  bottom: calc(var(--bar-h, 132px) + env(safe-area-inset-bottom, 0px) + 12px);
  transform: translateX(-50%);
  width: min(440px, calc(100vw - 20px));
  max-height: calc(100vh - var(--bar-h, 132px) - env(safe-area-inset-bottom, 0px) - 40px);
  display: flex;
  flex-direction: column;
}
.gen-panel.hidden { display: none; }
.gen-panel .panel-head { border-bottom: 1px solid var(--panel-border); }

.gen-body {
  padding: 12px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.gen-seed-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.gen-seed-row .field-label { min-width: 40px; }
.gen-seed-input { flex: 1 1 120px; }
.gen-rand-btn { flex: 0 0 auto; font-size: 13px; }

.gen-controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.gen-field-select {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.gen-field-label { color: var(--text-dim); font-size: 13px; }
.gen-field-select .gen-select { flex: 0 1 200px; }

.gen-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 10px;
  border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.gen-go-btn { min-height: 52px; font-size: 15px; }
.gen-seed-out {
  font-size: 12px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  text-align: center;
  min-height: 16px;
}

/* --- data (save/load) sheet ----------------------------------------------- */
.data-panel {
  left: 50%;
  bottom: calc(var(--bar-h, 132px) + env(safe-area-inset-bottom, 0px) + 12px);
  transform: translateX(-50%);
  width: min(420px, calc(100vw - 20px));
}
.data-panel.hidden { display: none; }
.data-panel .panel-head { border-bottom: 1px solid var(--panel-border); }
.data-body { padding: 12px; display: flex; flex-direction: column; gap: 10px; }

.field-row { display: flex; gap: 8px; align-items: center; }
.field-label { color: var(--text-dim); font-size: 13px; min-width: 48px; }

/* --- form controls --------------------------------------------------------- */
.text-input, .select {
  flex: 1 1 auto;
  min-height: var(--tap-min);
  min-width: 0;
  padding: 0 12px;
  background: #14100a;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  font: inherit;
  font-size: 16px;
}
.text-input.flash { border-color: var(--danger); }
.select { appearance: none; -webkit-appearance: none; padding-right: 12px; }

/* --- buttons --------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--tap-min);
  min-width: var(--tap-min);
  padding: 0 14px;
  background: var(--btn-bg);
  color: var(--text);
  border: 1px solid transparent;
  border-radius: 8px;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  white-space: nowrap;
  transition: background 0.12s ease, color 0.12s ease, border-color 0.12s ease;
}
.btn:hover { background: var(--btn-bg-hover); }
.btn:active { transform: translateY(1px); }
.btn.active {
  background: var(--btn-active);
  color: var(--btn-active-fg);
  border-color: var(--btn-active);
}
.btn.primary { background: var(--primary); color: #06201d; }
.btn.danger { background: #3a1c1c; color: #f0b8b8; }
.btn.danger:hover { background: #4a2222; }
.btn.wide { width: 100%; }

.icon-btn {
  min-width: var(--tap-min);
  min-height: var(--tap-min);
  padding: 0;
  background: transparent;
  border: 0;
  color: var(--text-dim);
  font-size: 18px;
  cursor: pointer;
}
.icon-btn:hover { color: var(--text); }

/* --- bottom control bar ----------------------------------------------------
 * Wrap layout (no horizontal scrolling anywhere): controls flow onto as many
 * rows as needed and stay centred. A safety max-height + vertical scroll keeps
 * the bar usable if it ever grows taller than the screen (tiny landscape). */
.bottom-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  row-gap: 8px;
  column-gap: 10px;
  padding: 10px calc(10px + env(safe-area-inset-right, 0px))
           calc(10px + env(safe-area-inset-bottom, 0px))
           calc(10px + env(safe-area-inset-left, 0px));
  background: linear-gradient(to top, rgba(15, 11, 6, 0.98), rgba(15, 11, 6, 0.82));
  border-top: 1px solid var(--panel-border);
  max-height: 45vh;
  overflow-x: hidden;
  overflow-y: auto;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}
.bottom-bar::-webkit-scrollbar { width: 5px; }
.bottom-bar::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 3px;
}

.group {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
  padding-right: 10px;
  border-right: 1px solid rgba(255, 255, 255, 0.07);
}
.group:last-child { border-right: 0; padding-right: 0; }
.group.context:empty { display: none; }

.grp-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}

/* Playback */
.play-btn { font-size: 20px; min-width: 56px; min-height: 56px; }

/* Compact speed stepper: [−] chip [+] */
.speed-stepper { display: flex; align-items: center; gap: 4px; }
.step-btn {
  min-width: 44px;
  min-height: 48px;
  padding: 0;
  font-size: 22px;
  line-height: 1;
}
.step-btn:disabled {
  opacity: 0.32;
  cursor: default;
}
.step-btn:disabled:hover { background: var(--btn-bg); }
.speed-chip {
  min-width: 56px;
  text-align: center;
  padding: 0 8px;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
}

/* Tools */
.tools { gap: 6px; }
.tool-btn {
  flex-direction: column;
  gap: 3px;
  padding: 6px 10px;
  min-width: 60px;
  min-height: 56px;
}
.tool-icon {
  position: relative;
  font-size: 20px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.tool-label { font-size: 12px; font-weight: 600; letter-spacing: 0.02em; }
.swatch {
  position: absolute;
  right: -8px;
  bottom: -3px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, 0.5);
}

.hint-btn { font-size: 13px; min-height: 48px; }
.hidden { display: none !important; }

/* Primary-row 'More' button and its secondary-controls bottom sheet. */
.more-btn { font-size: 14px; min-height: 56px; }

.more-panel {
  left: 50%;
  bottom: calc(var(--bar-h, 132px) + env(safe-area-inset-bottom, 0px) + 12px);
  transform: translateX(-50%);
  width: min(420px, calc(100vw - 20px));
}
.more-panel.hidden { display: none; }
.more-panel .panel-head { border-bottom: 1px solid var(--panel-border); }
.more-body {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.more-section { display: flex; flex-direction: column; gap: 8px; }
.section-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.btn-row .btn {
  flex: 1 1 auto;
  min-height: 52px;
  font-size: 14px;
}
.action-btn { min-height: 48px; }

/* --- About sheet + "colony starved" notice -------------------------------- */
/* Both reuse the .more-panel bottom-sheet position (above the bar). */
.about-body { padding: 12px 14px 14px; font-size: 15px; line-height: 1.45; }
.about-body p { margin: 0 0 10px; }
.about-body p:last-child { margin-bottom: 0; }
.about-links { display: flex; flex-wrap: wrap; gap: 8px 16px; }
.about-links a {
  color: var(--btn-active);
  display: inline-flex; align-items: center;
  min-height: var(--tap-min);
  text-decoration: underline;
  text-underline-offset: 3px;
}
.starved-panel { z-index: 25; }
