:root {
  --sim-bg: #f4f1e8;
  --sim-paper: #fffdf7;
  --sim-card: #ffffff;
  --sim-ink: #26332c;
  --sim-muted: #6b756f;
  --sim-green: #2f6b4f;
  --sim-green-dark: #204c38;
  --sim-green-soft: #eaf4ed;
  --sim-rust: #a64b35;
  --sim-gold: #b88932;
  --sim-blue: #376b8c;
  --sim-border: #c9d8cc;
  --sim-shadow: 0 18px 50px rgba(28, 54, 40, 0.12);
}

* { box-sizing: border-box; }

html { color-scheme: light; }

body {
  margin: 0;
  min-width: 320px;
  background:
    radial-gradient(circle at top left, rgba(47, 107, 79, 0.11), transparent 28rem),
    var(--sim-bg);
  color: var(--sim-ink);
  font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input, select { font: inherit; }

.sim-shell {
  width: min(1440px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: clamp(14px, 2vw, 28px);
}

.sim-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  padding: clamp(15px, 2.2vw, 26px);
  border: 1px solid var(--sim-border);
  border-top: 5px solid var(--sim-green);
  border-radius: 20px;
  background: rgba(255, 253, 247, 0.94);
  box-shadow: var(--sim-shadow);
}

.sim-kicker {
  margin-bottom: 8px;
  color: var(--sim-rust);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.sim-title {
  margin: 0;
  color: var(--sim-green-dark);
  font-family: "Lora", Georgia, serif;
  font-size: clamp(1.65rem, 4vw, 3rem);
  line-height: 1.08;
}

.sim-subtitle {
  max-width: 850px;
  margin: 12px 0 0;
  color: var(--sim-muted);
  font-family: "Lora", Georgia, serif;
  font-size: clamp(0.95rem, 1.5vw, 1.1rem);
  line-height: 1.65;
}

.sim-source {
  flex: 0 0 auto;
  max-width: 260px;
  padding: 10px 13px;
  border-radius: 999px;
  background: var(--sim-green-soft);
  color: var(--sim-green-dark);
  font-size: 0.75rem;
  font-weight: 700;
  line-height: 1.4;
  text-align: center;
}

.sim-layout {
  display: grid;
  grid-template-columns: minmax(250px, 330px) minmax(0, 1fr);
  gap: 18px;
  margin-top: 18px;
  align-items: start;
}

.sim-panel {
  min-width: 0;
  padding: 18px;
  border: 1px solid var(--sim-border);
  border-radius: 18px;
  background: var(--sim-card);
  box-shadow: 0 9px 28px rgba(28, 54, 40, 0.07);
}

.sim-controls {
  align-self: start;
}

.sim-sidebar {
  position: sticky;
  top: 14px;
  display: grid;
  min-width: 0;
  gap: 12px;
}

.sim-sidebar .metrics-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.sim-sidebar .metrics-grid .sim-panel-title {
  grid-column: 1 / -1;
  margin-bottom: 2px;
}

.collapsible-panel > summary {
  cursor: pointer;
  list-style: none;
}

.collapsible-panel > summary::-webkit-details-marker { display: none; }
.collapsible-panel:not([open]) > :not(summary) { display: none; }

.collapsible-panel > summary::after {
  content: "+";
  margin-left: auto;
  font-size: 1.2rem;
  line-height: 1;
}

.collapsible-panel[open] > summary::after { content: "−"; }
.collapsible-panel[open] > summary.sim-panel-title { margin-bottom: 14px; }

.sim-panel-title {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 0 0 14px;
  color: var(--sim-green-dark);
  font-size: 1rem;
}

.sim-panel-title i { color: var(--sim-rust); }

.control-group {
  display: grid;
  gap: 6px;
  margin-bottom: 15px;
}

.control-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.control-label {
  color: var(--sim-ink);
  font-size: 0.83rem;
  font-weight: 700;
}

.control-value {
  min-width: 62px;
  padding: 3px 7px;
  border-radius: 7px;
  background: var(--sim-green-soft);
  color: var(--sim-green-dark);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.78rem;
  font-weight: 700;
  text-align: center;
}

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

select {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--sim-border);
  border-radius: 9px;
  background: #fff;
  color: var(--sim-ink);
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.sim-button {
  appearance: none;
  min-height: 40px;
  padding: 9px 13px;
  border: 1px solid var(--sim-green);
  border-radius: 9px;
  background: var(--sim-green);
  color: #fff;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 800;
  transition: transform 0.16s ease, background 0.16s ease;
}

.sim-button:hover { background: var(--sim-green-dark); transform: translateY(-1px); }
.sim-button:focus-visible { outline: 3px solid rgba(184, 137, 50, 0.35); outline-offset: 2px; }
.sim-button.secondary { border-color: var(--sim-border); background: #fff; color: var(--sim-green-dark); }
.sim-button.danger { border-color: var(--sim-rust); background: var(--sim-rust); }

.sim-workspace {
  display: grid;
  gap: 18px;
}

.sim-workspace > .insight-box,
.sim-workspace > .status-box { order: 3; }

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(145px, 1fr));
  gap: 10px;
}

.metric {
  min-width: 0;
  padding: 12px;
  border: 1px solid #dfe8e1;
  border-radius: 12px;
  background: #fbfdfb;
}

.metric-label {
  color: var(--sim-muted);
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.metric-label .case-sensitive {
  text-transform: none;
}

.metric-value {
  margin-top: 5px;
  color: var(--sim-green-dark);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: clamp(0.9rem, 2vw, 1.12rem);
  font-weight: 800;
  overflow-wrap: anywhere;
}

.canvas-wrap {
  position: relative;
  min-height: 300px;
  overflow: hidden;
  border: 1px solid var(--sim-border);
  border-radius: 14px;
  background: #fffefb;
}

.canvas-wrap.tall { min-height: 420px; }

canvas {
  display: block;
  width: 100%;
  height: 100%;
  min-height: inherit;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 10px;
  color: var(--sim-muted);
  font-size: 0.75rem;
}

.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend-swatch { width: 18px; height: 3px; border-radius: 99px; background: var(--swatch); }

.equation-box,
.insight-box,
.status-box {
  padding: 13px 15px;
  border-radius: 12px;
  line-height: 1.55;
}

.equation-box {
  width: 100%;
  min-width: 0;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  border: 1px solid #ead8b4;
  background: #fff9ed;
  color: #5f491f;
  font-family: "Lora", Georgia, serif;
  text-align: center;
}

.equation-panel {
  min-width: 0;
  padding: 0;
  overflow: hidden;
}

.equation-panel summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 15px 18px;
  color: var(--sim-green-dark);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 800;
  list-style: none;
}

.equation-panel summary::-webkit-details-marker { display: none; }
.equation-panel summary::after { content: "+"; font-size: 1.2rem; line-height: 1; }
.equation-panel[open] summary::after { content: "−"; }
.equation-panel[open] summary { border-bottom: 1px solid var(--sim-border); }
.equation-panel .equation-box { border: 0; border-radius: 0; text-align: left; }

.insight-box {
  border-left: 4px solid var(--sim-blue);
  background: #eef6fa;
  color: #34556a;
  font-size: 0.86rem;
}

.status-box {
  border-left: 4px solid var(--sim-rust);
  background: #fff5f1;
  color: #743c2f;
  font-size: 0.86rem;
}

.sim-footer {
  margin-top: 18px;
  padding: 12px 16px;
  color: var(--sim-muted);
  font-size: 0.75rem;
  line-height: 1.5;
  text-align: center;
}

.histogram {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.hist-row { display: grid; grid-template-columns: 58px 1fr 70px; align-items: center; gap: 8px; font-size: 0.78rem; }
.hist-track { height: 12px; overflow: hidden; border-radius: 99px; background: #e9eeea; }
.hist-fill { height: 100%; border-radius: inherit; background: var(--sim-green); transition: width 0.25s ease; }

@media (max-width: 640px) {
  body { padding-bottom: 76px; }
  .sim-header { flex-direction: column; }
  .sim-subtitle, .sim-source { display: none; }
  .sim-layout { grid-template-columns: 1fr; }
  .sim-sidebar { position: static; display: contents; }
  .sim-workspace { order: 1; }
  .sim-sidebar > .metrics-grid { order: 2; }
  .sim-sidebar > .equation-panel { order: 3; }
  .sim-controls {
    position: fixed;
    z-index: 30;
    right: 8px;
    bottom: max(8px, env(safe-area-inset-bottom));
    left: 8px;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px 12px;
    width: auto;
    max-height: 46dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    box-shadow: 0 16px 44px rgba(28, 54, 40, 0.26);
  }
  .sim-controls .sim-panel-title,
  .sim-controls .button-row { grid-column: 1 / -1; }
  .sim-controls .insight-box { display: none; }
  .sim-controls .control-group { margin-bottom: 0; }
  .sim-controls.has-mobile-numerics {
    padding-right: calc(50% + 8px);
  }
  .sim-controls > .mobile-numerics {
    position: absolute;
    top: 52px;
    right: 12px;
    bottom: 12px;
    display: grid;
    grid-template-columns: 1fr;
    align-content: start;
    gap: 7px;
    width: calc(50% - 18px);
    min-width: 0;
    padding: 0 0 0 10px;
    overflow-y: auto;
    border: 0;
    border-left: 1px solid var(--sim-border);
    border-radius: 0;
    background: transparent;
    box-shadow: none;
  }
  .sim-controls > .mobile-numerics > .sim-panel-title {
    grid-column: 1;
    padding: 0 0 3px;
    font-size: 0.76rem;
    line-height: 1.25;
  }
  .sim-controls > .mobile-numerics .metric {
    padding: 7px 8px;
  }
  .sim-controls > .mobile-numerics .metric-label {
    font-size: 0.66rem;
    line-height: 1.25;
  }
  .sim-controls > .mobile-numerics .metric-value {
    margin-top: 2px;
    overflow-wrap: anywhere;
    font-size: 0.82rem;
    line-height: 1.25;
  }
}

@media (max-width: 520px) {
  .sim-shell { padding: 8px; }
  .sim-header, .sim-panel { padding: 14px; border-radius: 14px; }
  .canvas-wrap { min-height: 260px; }
  .canvas-wrap.tall { min-height: 360px; }
  .hist-row { grid-template-columns: 48px 1fr 55px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
