* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  height: 100dvh;
  background: #07070d;
  color: #d6d6e6;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  font-size: 14px;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}

body {
  display: flex;
  flex-direction: column;
}

/* ----- Intro header + floating toggle -------------------------------------- */

.header-toggle-fab {
  position: fixed;
  z-index: 200;
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  width: 44px;
  height: 44px;
  margin: 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #3a3a5a;
  border-radius: 10px;
  background: rgba(26, 26, 46, 0.94);
  color: #ecdc8a;
  line-height: 0;
  cursor: pointer;
  box-shadow: 0 2px 14px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.header-toggle-fab:hover {
  background: rgba(34, 34, 58, 0.96);
  border-color: #55557a;
  color: #ffcd92;
}

.header-toggle-fab:active {
  background: rgba(40, 40, 68, 0.98);
}

.header-toggle-fab:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px rgba(255, 159, 74, 0.45), 0 2px 14px rgba(0, 0, 0, 0.45);
}

body.header-collapsed .header-toggle-fab__icon--collapse,
body:not(.header-collapsed) .header-toggle-fab__icon--expand {
  display: none;
}

.header-toggle-fab__icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-toggle-fab__icon svg {
  display: block;
  flex-shrink: 0;
}

.top-chrome {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  padding-left: calc(max(22px, env(safe-area-inset-left)) + 52px);
  padding-right: max(22px, env(safe-area-inset-right));
}

.top-chrome[hidden] {
  display: none !important;
}

/* Aligns with .panels below (same columns / gap). */
.panel-headers-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: #18182a;
  flex-shrink: 0;
}

.site-header {
  padding: 14px 0 12px;
  border-bottom: 1px solid #18182a;
  flex-shrink: 0;
}

.site-header h1 {
  margin: 0 0 4px;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.site-header p {
  margin: 0;
  font-size: 12.5px;
  color: #8a8aa6;
  max-width: 760px;
  line-height: 1.45;
}

.site-header code {
  background: #14142a;
  color: #d8c990;
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.92em;
}

/* KaTeX inline tweaks: tighten the line so paragraphs with math don't get
 * artificially tall, and use the page's warm accent for the math glyphs. */
.site-header .katex {
  font-size: 1.02em;
  color: #ecdc8a;
}

.site-header .katex .mord,
.site-header .katex .mbin,
.site-header .katex .mrel,
.site-header .katex .mopen,
.site-header .katex .mclose,
.site-header .katex .mpunct,
.site-header .katex .minner {
  color: inherit;
}

/* ----- Links ----- */

a {
  color: #f3b277;
  text-decoration: underline;
  text-decoration-color: rgba(243, 178, 119, 0.35);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
  border-radius: 2px;
  transition: color 120ms ease, text-decoration-color 120ms ease,
    background-color 120ms ease;
}

a:hover {
  color: #ffcd92;
  text-decoration-color: #ffcd92;
}

a:focus-visible {
  outline: none;
  background: rgba(255, 159, 74, 0.12);
  text-decoration-color: #ffcd92;
  box-shadow: 0 0 0 2px rgba(255, 159, 74, 0.35);
}

a:visited {
  color: #d99c6a;
}

a:visited:hover {
  color: #ffcd92;
}

/* ----- Controls ----- */

.controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 16px 24px;
  padding: 14px 0 18px;
  border-bottom: 1px solid #18182a;
  background: #0a0a16;
  flex-shrink: 0;
}

.slider-control {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  min-width: 280px;
  max-width: 560px;
}

.slider-control .row {
  display: flex;
  align-items: baseline;
  gap: 10px;
  font-size: 11.5px;
  color: #9a9ab5;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.slider-control .name {
  font-weight: 600;
  color: #c0c0d4;
}

.slider-control output {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: #ecdc8a;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
  min-width: 24px;
}

.slider-control input[type="range"] {
  width: 100%;
  margin: 0;
  accent-color: #ff9f4a;
  background: transparent;
}

.slider-control datalist {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: #5a5a78;
  margin-top: -3px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  padding: 0 2px;
}

.slider-control datalist option {
  padding: 0;
}

.grid-toggle {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex-shrink: 0;
}

.grid-toggle .row {
  display: flex;
  align-items: baseline;
  gap: 10px;
  font-size: 11.5px;
  color: #9a9ab5;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.grid-toggle .name {
  font-weight: 600;
  color: #c0c0d4;
}

.segmented {
  display: inline-flex;
  border: 1px solid #2a2a44;
  border-radius: 4px;
  overflow: hidden;
  background: #14142a;
}

.seg-btn {
  background: transparent;
  color: #b0b0c8;
  border: none;
  padding: 7px 16px;
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.seg-btn + .seg-btn {
  border-left: 1px solid #2a2a44;
}

.seg-btn:hover:not(.is-active) {
  background: #1d1d33;
  color: #d6d6e6;
}

.seg-btn.is-active {
  background: #ff9f4a;
  color: #1a1010;
  font-weight: 600;
}

#reset-btn,
#export-btn,
#export-loop-btn {
  background: #1a1a2e;
  color: #d6d6e6;
  border: 1px solid #2a2a44;
  padding: 8px 16px;
  border-radius: 4px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  align-self: flex-end;
  -webkit-tap-highlight-color: transparent;
}

#reset-btn:hover,
#export-btn:hover,
#export-loop-btn:hover {
  background: #22223a;
  border-color: #44446a;
}

#reset-btn:active,
#export-btn:active,
#export-loop-btn:active {
  background: #262640;
}

#export-loop-btn:disabled {
  opacity: 0.6;
  cursor: progress;
}

/* ----- Panels ----- */

.panels {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: #18182a;
  min-height: 0;
}

.panel {
  display: flex;
  flex-direction: column;
  background: #07070d;
  min-height: 0;
  min-width: 0;
}

.panel-header {
  padding: 7px 14px;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #8a8aa6;
  border-bottom: 1px solid #18182a;
  background: #0a0a16;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  flex-shrink: 0;
}

.panel-header .title {
  font-weight: 600;
  color: #c0c0d4;
}

.panel-header .hint {
  font-size: 10.5px;
  color: #555573;
  text-transform: none;
  letter-spacing: 0;
}

.panel canvas {
  flex: 1;
  display: block;
  width: 100%;
  height: 100%;
  cursor: default;
  user-select: none;
  touch-action: none;
}

footer {
  padding: 6px 22px;
  padding-left: max(22px, env(safe-area-inset-left));
  padding-right: max(22px, env(safe-area-inset-right));
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  border-top: 1px solid #18182a;
  background: #0a0a16;
  font-size: 11.5px;
  color: #6a6a88;
  flex-shrink: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  min-height: 26px;
}

/* ----- Narrow / touch screens ------------------------------------------------ */

@media (max-width: 720px) {
  .top-chrome {
    padding-top: max(12px, env(safe-area-inset-top));
  }

  .site-header h1 {
    font-size: 16px;
  }

  .site-header p {
    font-size: 12px;
    max-width: none;
  }

  .controls {
    gap: 14px 16px;
    align-items: stretch;
  }

  .slider-control {
    flex: 1 1 100%;
    min-width: 0;
    max-width: none;
  }

  .grid-toggle {
    flex: 1 1 100%;
    min-width: 0;
    max-width: none;
    width: 100%;
    align-self: stretch;
  }

  .grid-toggle .segmented {
    display: flex;
    width: 100%;
  }

  #reset-btn,
  #export-btn,
  #export-loop-btn {
    flex: 1 1 auto;
    min-height: 44px;
    align-self: stretch;
    padding: 10px 14px;
    font-size: 13px;
  }

  .seg-btn {
    flex: 1 1 0;
    min-width: 0;
    min-height: 44px;
    padding: 10px 12px;
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .panel-headers-row {
    grid-template-columns: 1fr;
  }

  .panels {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 1fr;
    flex: 1;
    min-height: 0;
  }

  .panel {
    min-height: 200px;
  }

  .panel-header {
    flex-wrap: wrap;
    gap: 4px 10px;
    padding: 8px 12px;
  }

  .panel-header .hint {
    flex: 1 1 100%;
    font-size: 10px;
    line-height: 1.35;
  }
}
