:root,
html[data-theme="dark"] {
  --bg: #0f1419;
  --surface: #161d27;
  --surface-2: #1c2533;
  --border: rgba(255, 255, 255, 0.08);
  --text: #e8edf4;
  --muted: #8b9cb3;
  --accent: #5eead4;
  --accent-2: #f472b6;
  --warn: #fbbf24;
  --danger: #f87171;
  --best-color: #5eead4;
  --avg-color: #c084fc;
  --gen-color: #38bdf8;
  --ref-color: #fbbf24;
  --plot-bg: rgba(22, 29, 39, 0.6);
  --footer-bg: rgba(15, 20, 25, 0.85);
  --shadow: rgba(0, 0, 0, 0.35);
  --radius: 14px;
  --font-fa: "Vazirmatn", "Noto Sans Arabic", system-ui, sans-serif;
  --font-ar: "Noto Sans Arabic", "Vazirmatn", system-ui, sans-serif;
  --font-en: "DM Sans", system-ui, sans-serif;
  --sidebar-ratio: 0.34;
  --sidebar-min: 260px;
  --sidebar-max: 560px;
  --gutter-w: 12px;
}

html[data-theme="light"] {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #e2e8f0;
  --border: rgba(15, 23, 42, 0.1);
  --text: #0f172a;
  --muted: #64748b;
  --accent: #0d9488;
  --accent-2: #db2777;
  --warn: #b45309;
  --danger: #dc2626;
  --best-color: #0d9488;
  --avg-color: #7c3aed;
  --gen-color: #0284c7;
  --ref-color: #b45309;
  --plot-bg: rgba(248, 250, 252, 0.95);
  --footer-bg: rgba(255, 255, 255, 0.92);
  --shadow: rgba(15, 23, 42, 0.08);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html[lang="fa"] body {
  font-family: var(--font-fa);
}

html[lang="ar"] body {
  font-family: var(--font-ar);
}

html[lang="en"] body {
  font-family: var(--font-en);
}

body {
  margin: 0;
  min-height: 100vh;
  padding-bottom: 5.5rem;
  background: radial-gradient(1200px 600px at 10% -10%, color-mix(in srgb, var(--accent) 18%, transparent), transparent),
    radial-gradient(800px 400px at 90% 0%, color-mix(in srgb, var(--accent-2) 14%, transparent), transparent),
    var(--bg);
  color: var(--text);
  line-height: 1.55;
  transition: background-color 0.25s ease, color 0.2s ease;
}

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.bismillah {
  margin: 0;
  padding: 0.65rem 1rem 0.15rem;
  text-align: center;
  font-family: "Noto Sans Arabic", "Vazirmatn", serif;
  font-size: clamp(0.78rem, 1.8vw, 0.92rem);
  font-weight: 600;
  letter-spacing: 0.02em;
  color: color-mix(in srgb, var(--accent) 72%, var(--muted));
  text-shadow: 0 0 28px color-mix(in srgb, var(--accent) 22%, transparent);
  user-select: none;
}

.top-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.25rem 1.75rem;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, color-mix(in srgb, var(--surface) 95%, transparent), color-mix(in srgb, var(--bg) 70%, transparent));
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 30;
}

.top-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.65rem;
}

.theme-switch,
.lang-switch {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
  background: var(--surface);
}

.theme-btn,
.lang-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 0.45rem 0.85rem;
  font: inherit;
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
}

.lang-btn {
  padding: 0.5rem 1rem;
}

.theme-btn.active,
.lang-btn.active {
  background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 28%, transparent), color-mix(in srgb, var(--accent-2) 18%, transparent));
  color: var(--text);
}

.brand {
  display: flex;
  gap: 0.85rem;
  align-items: flex-start;
}

.brand-mark {
  font-size: 1.75rem;
  color: var(--accent);
  line-height: 1;
  filter: drop-shadow(0 0 10px color-mix(in srgb, var(--accent) 45%, transparent));
}

.brand-text h1 {
  margin: 0 0 0.25rem;
  font-size: clamp(1.05rem, 2.5vw, 1.35rem);
  font-weight: 700;
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.layout-ga {
  max-width: 1680px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 2rem;
  display: grid;
  gap: 0;
  align-items: stretch;
  direction: ltr;
  min-height: 0;
}

.layout-ga.sidebar-collapsed {
  grid-template-columns: minmax(0, 1fr);
}

.layout-ga.sidebar-collapsed .sidebar-gutter,
.layout-ga.sidebar-collapsed .sidebar-column {
  display: none;
}

.layout-ga.is-resizing {
  cursor: col-resize;
  user-select: none;
}

.layout-ga.is-resizing .sidebar-gutter {
  background: color-mix(in srgb, var(--accent) 35%, transparent);
}

.layout-main {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  min-width: 0;
  padding-inline-end: 1rem;
  direction: inherit;
}

html[dir="rtl"] .layout-main,
html[dir="rtl"] .sidebar-panel {
  direction: rtl;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.35rem;
  box-shadow: 0 12px 40px var(--shadow);
}

.panel h2 {
  margin: 0 0 0.65rem;
  font-size: 1.05rem;
  color: var(--accent);
}

.lead {
  margin: 0 0 0.75rem;
}

.bullets {
  margin: 0 0 0.75rem;
  padding-inline-start: 1.25rem;
  color: var(--muted);
}

.small {
  font-size: 0.85rem;
}

.muted {
  color: var(--muted);
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.88rem;
}

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

.field input[type="number"],
.select-input {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 0.45rem 0.6rem;
  font: inherit;
}

output {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--accent);
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 0.75rem;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 0.55rem 1rem;
  border-radius: 10px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.btn.primary {
  background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 35%, transparent), color-mix(in srgb, var(--accent-2) 22%, transparent));
}

.btn.danger {
  background: color-mix(in srgb, var(--danger) 22%, transparent);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.metrics-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.75rem;
  margin-top: 0.75rem;
}

.metric-tile {
  padding: 0.75rem 1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
}

.metric-tile .label {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
}

.metric-tile .value {
  font-size: 1.15rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.metric-tile.best .value {
  color: var(--best-color);
}

.metric-tile.avg .value {
  color: var(--avg-color);
}

.metric-tile.gen .value {
  color: var(--gen-color);
}

.metric-tile.ref .value {
  color: var(--ref-color);
}

.phase-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 1rem;
}

.phase-chip {
  font-size: 0.72rem;
  padding: 0.3rem 0.55rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}

.phase-chip.active {
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
  color: var(--text);
  font-weight: 600;
}

.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 16px 48px var(--shadow);
}

.viz-header {
  padding: 1rem 1.1rem 0.5rem;
}

.viz-header h2 {
  margin: 0;
  font-size: 1rem;
}

.chart-hint {
  margin: 0.35rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.viz-grid {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 0;
  border-top: 1px solid var(--border);
}

.plot-pane-inner {
  width: 100%;
  min-height: 320px;
}

.plot-main {
  min-height: 380px;
  border-inline-end: 1px solid var(--border);
}

.plot-fitness {
  min-height: 280px;
}

.bit-population {
  padding: 0.75rem 1rem 1rem;
  border-top: 1px solid var(--border);
  overflow-x: auto;
}

.bit-grid {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.bit-row {
  display: flex;
  align-items: center;
  gap: 2px;
}

.bit-row-best {
  outline: 1px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.bit-cell {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  flex-shrink: 0;
}

.bit-1 {
  background: var(--accent);
  box-shadow: 0 0 6px color-mix(in srgb, var(--accent) 50%, transparent);
}

.bit-0 {
  background: color-mix(in srgb, var(--muted) 35%, transparent);
}

.bit-fit {
  margin-inline-start: 0.5rem;
  font-size: 0.72rem;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  min-width: 2.5rem;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 0.45rem 0.75rem;
  border-radius: 999px;
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
}

.btn-icon:hover {
  border-color: color-mix(in srgb, var(--accent) 40%, transparent);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--accent) 12%, transparent);
}

.btn-icon-glyph {
  font-size: 1rem;
  line-height: 1;
  color: var(--accent);
}

.btn-icon-compact {
  padding: 0.2rem 0.45rem;
  border-radius: 8px;
  min-width: 2rem;
  justify-content: center;
  font-size: 1.1rem;
  color: var(--muted);
}

.btn-icon-compact:hover {
  color: var(--text);
}

.sidebar-toggle-btn[aria-pressed="false"] .dock-glyph {
  opacity: 0.65;
}

.action-dock {
  position: fixed;
  left: 50%;
  right: auto;
  bottom: 1.1rem;
  transform: translateX(-50%);
  z-index: 45;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  max-width: min(960px, calc(100vw - 1.5rem));
  padding: 0.5rem 0.7rem;
  border-radius: 18px;
  border: 1px solid color-mix(in srgb, var(--accent) 22%, var(--border));
  background: linear-gradient(
    165deg,
    color-mix(in srgb, var(--surface) 92%, transparent),
    color-mix(in srgb, var(--surface-2) 88%, transparent)
  );
  backdrop-filter: blur(18px) saturate(1.2);
  box-shadow:
    0 4px 24px var(--shadow),
    0 0 0 1px color-mix(in srgb, var(--accent) 8%, transparent),
    inset 0 1px 0 color-mix(in srgb, #fff 6%, transparent);
}

.dock-sep {
  width: 1px;
  align-self: stretch;
  min-height: 1.75rem;
  margin: 0 0.15rem;
  background: var(--border);
  flex-shrink: 0;
}

.dock-btn {
  padding: 0.48rem 0.85rem;
  font-size: 0.82rem;
  white-space: nowrap;
}

.dock-btn-icon {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.48rem 0.7rem;
  border-color: color-mix(in srgb, var(--accent) 35%, var(--border));
  background: color-mix(in srgb, var(--accent) 10%, var(--surface-2));
}

.dock-glyph {
  font-size: 1.05rem;
  line-height: 1;
  color: var(--accent);
}

.dock-label {
  font-weight: 600;
}

@media (max-width: 720px) {
  .action-dock {
    left: 0.65rem;
    right: 0.65rem;
    transform: none;
    max-width: none;
    border-radius: 14px;
    gap: 0.3rem;
    padding: 0.45rem 0.5rem;
  }

  .dock-btn {
    padding: 0.42rem 0.55rem;
    font-size: 0.76rem;
  }

  .dock-label {
    display: none;
  }

  .dock-btn-icon {
    padding: 0.42rem 0.55rem;
  }
}

.sidebar-gutter {
  cursor: col-resize;
  touch-action: none;
  user-select: none;
  background: linear-gradient(
    180deg,
    transparent 0%,
    color-mix(in srgb, var(--border) 85%, var(--accent)) 20%,
    color-mix(in srgb, var(--border) 85%, var(--accent)) 80%,
    transparent 100%
  );
  position: relative;
  z-index: 5;
  border-radius: 6px;
  margin: 0.5rem 0;
  align-self: stretch;
  min-height: 120px;
}

.sidebar-gutter:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.sidebar-gutter::after {
  content: "";
  position: absolute;
  top: 30%;
  bottom: 30%;
  left: 50%;
  width: 4px;
  transform: translateX(-50%);
  border-radius: 3px;
  background: var(--muted);
  opacity: 0.45;
  transition: opacity 0.2s;
}

.sidebar-gutter:hover::after,
.sidebar-gutter:active::after {
  opacity: 0.9;
  background: var(--accent);
}

.sidebar-column {
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding-inline-start: 0.25rem;
}

.sidebar-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1;
  max-height: calc(100vh - 5.5rem);
  position: sticky;
  top: 5.5rem;
  padding: 0;
  overflow: hidden;
}

.sidebar-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 1rem 1rem 0.65rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  direction: inherit;
}

.sidebar-head h2 {
  margin: 0 0 0.25rem;
  font-size: 0.98rem;
}

.sidebar-gen {
  margin: 0;
}

.sidebar-body {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0.75rem 1rem 1rem;
  direction: inherit;
  scrollbar-gutter: stable;
}

.sidebar-body::-webkit-scrollbar {
  width: 6px;
}

.sidebar-body::-webkit-scrollbar-thumb {
  background: color-mix(in srgb, var(--muted) 45%, transparent);
  border-radius: 999px;
}

.sidebar-panel h3 {
  margin: 0 0 0.4rem;
  font-size: 0.88rem;
  color: var(--accent);
}

.step-card {
  margin: 0 0 0.75rem;
  padding: 0.85rem;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.step-desc {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
}

.step-detail {
  font-size: 0.82rem;
}

.step-detail code {
  font-family: ui-monospace, monospace;
  color: var(--accent);
}

.formula-details {
  margin: 0 0 0.55rem;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  overflow: hidden;
}

.formula-details summary {
  padding: 0.55rem 0.75rem;
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--accent);
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.formula-details summary::-webkit-details-marker {
  display: none;
}

.formula-details summary::before {
  content: "▸";
  display: inline-block;
  margin-inline-end: 0.4rem;
  font-size: 0.7rem;
  color: var(--muted);
  transition: transform 0.2s;
}

.formula-details[open] summary::before {
  transform: rotate(90deg);
}

.formula-details .formula-katex {
  padding: 0 0.75rem 0.75rem;
}

.formula-katex {
  font-size: 0.82rem;
  line-height: 1.45;
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
}

.formula-katex .katex-display {
  margin: 0.35rem 0;
  overflow-x: auto;
  overflow-y: hidden;
}

.formula-katex .katex {
  font-size: 0.95em;
  max-width: 100%;
}

.formula-katex.formula-rendered,
.formula-katex .katex {
  font-size: 0.9em;
  max-width: 100%;
}

.formula-katex.formula-rendered {
  overflow-x: auto;
  overflow-y: hidden;
  padding-block: 0.15rem;
}

.stats-details .stats-table-wrap {
  padding: 0 0.75rem 0.75rem;
}

.stats-table-wrap {
  margin-top: 0;
}

.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.stats-table th,
.stats-table td {
  padding: 0.35rem 0.4rem;
  border-bottom: 1px solid var(--border);
  text-align: start;
}

.stats-table .mono {
  font-family: ui-monospace, monospace;
  font-size: 0.7rem;
  max-width: 0;
  width: 55%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stats-table .mono[title] {
  cursor: help;
}

.stats-table tr.row-best td {
  color: var(--best-color);
  font-weight: 600;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.site-footer {
  text-align: center;
  padding: 1.25rem;
  color: var(--muted);
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
  background: var(--footer-bg);
}

@media (max-width: 1100px) {
  .layout-ga {
    grid-template-columns: 1fr;
    direction: inherit;
  }

  .layout-ga:not(.sidebar-collapsed) .sidebar-column {
    display: block;
    padding-inline-start: 0;
    margin-top: 1rem;
  }

  .layout-ga .sidebar-gutter {
    display: none;
  }

  .layout-main {
    padding-inline-end: 0;
  }

  .sidebar-panel {
    position: static;
    max-height: none;
  }

  .viz-grid {
    grid-template-columns: 1fr;
  }

  .plot-main {
    border-inline-end: none;
    border-bottom: 1px solid var(--border);
  }

}
