:root {
  color-scheme: light;
  --ink: #172033;
  --ink-soft: #39445a;
  --muted: #6b7280;
  --paper: #ffffff;
  --cloud: #f4f6f8;
  --line: #dfe4eb;
  --line-strong: #c9d0db;
  --accent: #a0793d;
  --accent-soft: #f4eadb;
  --blue: #2e86de;
  --orange: #e67e22;
  --success: #237a4b;
  --danger: #b42318;
  --shadow: 0 14px 40px rgba(23, 32, 51, 0.09);
  --radius: 18px;
  --sidebar-width: clamp(300px, 22vw, 390px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: clamp(15px, calc(12px + 0.22vw), 20px);
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body {
  color: var(--ink);
  background: linear-gradient(180deg, #fff 0%, #f7f8fa 100%);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button, input, select, textarea { font: inherit; }
button, select { touch-action: manipulation; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.55; }

.darboux-app { width: 100%; height: 100%; }
.d-app-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  width: 100%;
  height: 100dvh;
  min-height: 0;
}

.d-topbar {
  position: relative;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 0.8rem;
  min-height: 62px;
  padding: 0.55rem 0.9rem;
  background: rgba(255, 255, 255, 0.95);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 6px 26px rgba(23, 32, 51, 0.045);
  backdrop-filter: blur(12px);
}
.d-brand { display: flex; align-items: center; gap: 0.7rem; min-width: 0; flex: 1; }
.d-brand img { width: 40px; height: 40px; object-fit: contain; border-radius: 10px; }
.d-brand-copy { min-width: 0; }
.d-brand-title { margin: 0; font-size: clamp(1.05rem, 1.5vw, 1.45rem); line-height: 1.1; letter-spacing: -0.025em; }
.d-brand-title-short, .d-language-short { display: none; }
.d-brand-subtitle { margin-top: 0.16rem; color: var(--muted); font-size: 0.78rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.d-top-actions { display: flex; align-items: center; gap: 0.45rem; }
.embed-mode #back-link { display: none; }
.embed-mode .d-brand-subtitle { display: none; }
.d-icon-button, .d-button, .d-tab, .d-key, .d-segment button {
  border: 1px solid var(--line-strong);
  background: var(--paper);
  color: var(--ink);
  border-radius: 11px;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease, border-color 0.12s ease;
}
.d-icon-button:hover, .d-button:hover, .d-tab:hover, .d-key:hover, .d-segment button:hover { transform: translateY(-1px); box-shadow: 0 7px 18px rgba(23, 32, 51, 0.09); }
.d-icon-button { min-width: 42px; min-height: 40px; padding: 0.4rem 0.65rem; display: inline-flex; align-items: center; justify-content: center; gap: 0.35rem; }
.d-button { padding: 0.55rem 0.78rem; font-weight: 650; }
.d-button.primary { background: var(--ink); border-color: var(--ink); color: white; }
.d-button.accent { background: var(--accent); border-color: var(--accent); color: white; }
.d-button.soft { background: var(--cloud); }
.d-button.danger { color: var(--danger); }
.d-mobile-only { display: none; }

.d-body {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
}
.d-sidebar {
  position: relative;
  z-index: 30;
  min-width: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0.7rem;
  background: #f5f6f8;
  border-right: 1px solid var(--line);
  scrollbar-width: thin;
}
.d-main {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
  background: #f9fafb;
}

.d-mode-tabs {
  display: flex;
  align-items: stretch;
  gap: 0.35rem;
  padding: 0.48rem 0.65rem;
  overflow-x: auto;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  scrollbar-width: none;
}
.d-mode-tabs::-webkit-scrollbar { display: none; }
.d-tab { flex: 0 0 auto; padding: 0.62rem 0.9rem; font-weight: 750; white-space: nowrap; }
.d-tab[aria-selected="true"] { background: var(--ink); border-color: var(--ink); color: white; box-shadow: 0 8px 20px rgba(23, 32, 51, 0.18); }
.d-workspace { min-height: 0; overflow: auto; padding: 0.65rem; overscroll-behavior: contain; }
.d-mode-panel { display: none; min-height: 100%; }
.d-mode-panel.active { display: block; }

.d-card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 6px 22px rgba(23, 32, 51, 0.05);
  min-width: 0;
}
.d-card-header { display: flex; align-items: center; justify-content: space-between; gap: 0.65rem; padding: 0.68rem 0.82rem; border-bottom: 1px solid var(--line); }
.d-card-header h2, .d-card-header h3 { margin: 0; font-size: 0.96rem; letter-spacing: -0.015em; }
.d-card-body { padding: 0.78rem; }

.d-side-section {
  margin-bottom: 0.62rem;
  background: rgba(255,255,255,0.92);
  border: 1px solid var(--line);
  border-radius: 15px;
  overflow: hidden;
}
.d-side-section > summary {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.7rem 0.75rem;
  font-weight: 750;
  cursor: pointer;
  user-select: none;
  background: #fff;
}
.d-side-section > summary::-webkit-details-marker { display: none; }
.d-side-section > summary::after { content: "⌄"; color: var(--muted); font-size: 1rem; transition: transform .15s ease; }
.d-side-section[open] > summary::after { transform: rotate(180deg); }
.d-side-content { padding: 0 0.72rem 0.75rem; }
.d-label { display: block; margin: 0.5rem 0 0.28rem; font-size: 0.79rem; font-weight: 700; color: var(--ink-soft); }
.d-input, .d-select, .d-textarea {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  padding: 0.46rem 0.56rem;
  background: #fff;
  color: var(--ink);
  outline: none;
}
.d-input:focus, .d-select:focus, .d-textarea:focus { border-color: #5a83ad; box-shadow: 0 0 0 3px rgba(46,134,222,.12); }
.d-row { display: flex; gap: 0.45rem; align-items: center; }
.d-row > * { min-width: 0; }
.d-row.stretch > * { flex: 1; }
.d-grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.45rem; }
.d-grid-3-controls { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.38rem; }
.d-help { margin: 0.45rem 0 0; color: var(--muted); font-size: 0.72rem; line-height: 1.42; }
.d-status { margin-top: 0.48rem; padding: 0.5rem 0.58rem; border-radius: 9px; background: var(--cloud); font-size: 0.76rem; line-height: 1.42; color: var(--ink-soft); overflow-wrap: anywhere; }
.d-status.success { background: #eaf7ef; color: #1f6842; }
.d-status.warning { background: #fff4df; color: #80591f; }
.d-status.error { background: #ffebe9; color: #9b2118; }

.d-formula-preview {
  min-height: 96px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  padding: 0.65rem 0.35rem;
  margin: 0.5rem 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: linear-gradient(135deg, #fff, #fafbfc);
  font-size: 1.08rem;
}
.d-formula-preview mjx-container { margin: 0 !important; max-width: 100%; }
.d-formula-note { color: var(--muted); font-size: 0.7rem; margin: -0.15rem 0 0.45rem; }
.d-keypad { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 0.28rem; margin-top: 0.45rem; }
.d-key { min-height: 36px; padding: 0.25rem 0.2rem; font-weight: 650; font-size: 0.82rem; }
.d-key.function { background: #f7f8fa; }
.d-key.operator { background: #f3eadc; border-color: #ddc8a6; }

.d-segment { display: flex; gap: 0.25rem; padding: 0.18rem; border: 1px solid var(--line); background: var(--cloud); border-radius: 11px; }
.d-segment button { flex: 1; padding: 0.42rem; font-size: 0.78rem; box-shadow: none; }
.d-segment button.active { background: var(--ink); color: #fff; border-color: var(--ink); }

.d-range-wrap { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 0.55rem; }
.d-range { width: 100%; accent-color: var(--blue); }
.d-range-value { min-width: 3.2rem; text-align: right; font-variant-numeric: tabular-nums; font-weight: 750; }

.d-dyadic-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: minmax(390px, 1fr) minmax(310px, 0.78fr);
  gap: 0.65rem;
  min-height: calc(100dvh - 145px);
}
.d-dyadic-grid .d-convergence-card { grid-column: 1 / -1; }
.d-compare-layout, .d-sequence-layout {
  display: grid;
  grid-template-columns: minmax(300px, 0.72fr) minmax(0, 2.28fr);
  gap: 0.65rem;
  min-height: calc(100dvh - 145px);
}
.d-editor-column { display: grid; grid-template-rows: minmax(420px, 0.68fr) minmax(240px, 0.32fr); gap: 0.65rem; min-width: 0; }
.d-partition-card { display: grid; grid-template-rows: auto minmax(0, 1fr); min-height: 0; overflow: hidden; }
.d-comparison-plots {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(330px, 1fr));
  gap: 0.65rem;
  min-width: 0;
}
.d-comparison-plots.only-two { grid-template-rows: minmax(480px, 1fr); }
.d-comparison-plots.only-two .d-plot-card.hidden-by-view { display: none; }
.d-sequence-main {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: minmax(390px, 1.12fr) minmax(300px, 0.88fr);
  gap: 0.65rem;
  min-width: 0;
}
.d-sequence-main .d-history-card { grid-column: 1 / -1; }
.d-plot-card {
  min-width: 0;
  min-height: 300px;
  overflow: hidden;
  position: relative;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
}
.d-plot-summary {
  min-width: 0;
  padding: 0.42rem 0.72rem;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(90deg, #fff, #fafbfc);
  color: var(--ink-soft);
  font-size: 0.76rem;
  font-weight: 650;
  line-height: 1.35;
  text-align: center;
  overflow-wrap: anywhere;
}
.d-plot-summary.muted { color: var(--muted); font-weight: 600; }
.d-plot-summary[hidden] { display: none; }
.d-plot {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  position: relative;
  touch-action: none;
  overscroll-behavior: contain;
}
.d-plot-loading { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(255,255,255,.82); z-index: 4; color: var(--muted); font-size: 0.85rem; pointer-events: none; }
.d-plot-loading.hidden { display: none; }

.d-plot .modebar-container {
  top: 0.34rem !important;
  right: 0.4rem !important;
}
.d-plot .modebar {
  display: flex !important;
  flex-wrap: nowrap !important;
  align-items: center;
  gap: 1px;
  padding: 2px !important;
  border: 1px solid rgba(201,208,219,.8);
  border-radius: 10px;
  background: rgba(255,255,255,.9) !important;
  box-shadow: 0 4px 14px rgba(23,32,51,.08);
  backdrop-filter: blur(6px);
}
.d-plot .modebar-group { display: flex !important; padding-left: 0 !important; opacity: 1 !important; }
.d-plot .modebar-btn {
  width: 30px !important;
  height: 30px !important;
  padding: 5px !important;
  border-radius: 7px;
}
.d-plot .modebar-btn:hover { background: var(--cloud) !important; }
.d-plot .modebar-btn.active { background: var(--accent-soft) !important; }
.d-plot .modebar-btn svg { width: 100% !important; height: 100% !important; }
.d-plot .modebar-btn svg path { fill: rgba(57,68,90,.62) !important; }
.d-plot .modebar-btn:hover svg path,
.d-plot .modebar-btn.active svg path { fill: var(--ink) !important; }

.partition-editor-wrap { position: relative; width: 100%; height: 100%; min-height: 320px; overflow: hidden; background: #fff; border-radius: 0 0 var(--radius) var(--radius); touch-action: none; }
.d-partition-card .partition-editor-wrap { height: auto; min-height: 0; }
.partition-editor-svg { display: block; width: 100%; height: 100%; min-height: inherit; user-select: none; -webkit-user-select: none; cursor: crosshair; }
.partition-editor-svg.is-read-only { cursor: default; }
.partition-zoom-controls { position: absolute; right: 0.55rem; bottom: 0.55rem; display: flex; gap: 0.2rem; padding: 0.18rem; background: rgba(255,255,255,.92); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 5px 18px rgba(23,32,51,.1); }
.partition-zoom-controls button { min-width: 34px; min-height: 32px; border: 0; background: transparent; color: var(--ink); border-radius: 7px; font-weight: 750; }
.partition-zoom-controls button:hover { background: var(--cloud); }

.d-metrics { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.45rem; }
.d-metric { padding: 0.58rem; border: 1px solid var(--line); border-radius: 11px; background: #fff; }
.d-metric-label { color: var(--muted); font-size: 0.7rem; }
.d-metric-value { margin-top: 0.16rem; font-size: 0.93rem; font-weight: 750; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.d-relation { margin-top: 0.55rem; padding: 0.58rem; background: #f8fafc; border: 1px solid var(--line); border-radius: 10px; font-size: 0.76rem; line-height: 1.45; }

.d-dialog { width: min(760px, calc(100vw - 1.5rem)); max-height: min(88dvh, 900px); border: 0; border-radius: 20px; padding: 0; box-shadow: 0 30px 90px rgba(23,32,51,.28); }
.d-dialog::backdrop { background: rgba(15,23,42,.45); backdrop-filter: blur(3px); }
.d-dialog-header { display: flex; align-items: center; justify-content: space-between; gap: 0.6rem; padding: 0.85rem 1rem; border-bottom: 1px solid var(--line); }
.d-dialog-header h2 { margin: 0; font-size: 1.05rem; }
.d-dialog-body { padding: 0.9rem 1rem; overflow-y: auto; max-height: calc(88dvh - 130px); }
.d-dialog-footer { display: flex; justify-content: flex-end; gap: 0.5rem; padding: 0.75rem 1rem; border-top: 1px solid var(--line); }
.d-piecewise-table { display: grid; gap: 0.5rem; }
.d-piecewise-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; gap: 0.42rem; align-items: center; }
.d-details-pre { white-space: pre-wrap; overflow-wrap: anywhere; background: #f7f8fa; border: 1px solid var(--line); border-radius: 10px; padding: 0.7rem; font: 0.78rem/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; }

.d-toast-stack { position: fixed; right: 1rem; bottom: 1rem; z-index: 100; display: grid; gap: 0.5rem; width: min(360px, calc(100vw - 2rem)); pointer-events: none; }
.d-toast { padding: 0.7rem 0.82rem; border-radius: 12px; background: #172033; color: #fff; box-shadow: var(--shadow); font-size: 0.8rem; line-height: 1.4; animation: toast-in .18s ease-out; }
.d-toast.error { background: #8f1d16; }
@keyframes toast-in { from { transform: translateY(12px); opacity: 0; } }

.d-progress { width: 100%; height: 8px; overflow: hidden; border-radius: 999px; background: #e6eaf0; margin-top: 0.45rem; }
.d-progress > span { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--blue), var(--accent)); transition: width .15s ease; }

.d-bottom-actions { display: flex; flex-wrap: wrap; gap: 0.45rem; margin-top: 0.55rem; }
.d-spacer { flex: 1; }
.d-overlay { display: none; }

@media (min-width: 1800px) {
  :root { --sidebar-width: clamp(350px, 20vw, 470px); }
  .d-sidebar { padding: 0.85rem; }
  .d-dyadic-grid { grid-template-rows: minmax(500px, 1fr) minmax(350px, .72fr); }
  .d-comparison-plots { grid-template-rows: repeat(2, minmax(430px, 1fr)); }
  .d-sequence-main { grid-template-rows: minmax(500px, 1.12fr) minmax(350px, .88fr); }
  .d-editor-column { grid-template-rows: minmax(480px, .72fr) minmax(300px, .28fr); }
}

@media (max-width: 1180px) {
  :root { --sidebar-width: clamp(285px, 27vw, 340px); }
  .d-compare-layout, .d-sequence-layout { grid-template-columns: minmax(270px, .8fr) minmax(0, 2.2fr); }
  .d-comparison-plots { grid-template-rows: repeat(2, minmax(300px, 1fr)); }
}

@media (max-width: 900px) {
  html, body { overflow: hidden; }
  .d-mobile-only { display: inline-flex; }
  .d-topbar {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    min-height: 56px;
    padding: 0.45rem 0.55rem;
    gap: 0.4rem;
    overflow: hidden;
  }
  .d-brand img { width: 34px; height: 34px; }
  .d-brand-subtitle { display: none; }
  .d-brand { overflow: hidden; }
  .d-brand-copy { overflow: hidden; }
  .d-brand-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .d-top-actions { min-width: 0; max-width: 48vw; overflow-x: auto; scrollbar-width: none; }
  .d-top-actions::-webkit-scrollbar { display: none; }
  .d-top-actions .d-label-text { display: none; }
  .d-body { grid-template-columns: 1fr; }
  .d-sidebar {
    position: fixed;
    z-index: 70;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(88vw, 390px);
    padding-top: 0.75rem;
    transform: translateX(-105%);
    transition: transform .2s ease;
    box-shadow: 20px 0 60px rgba(23,32,51,.2);
  }
  .d-app-shell.sidebar-open .d-sidebar { transform: translateX(0); }
  .d-overlay { position: fixed; z-index: 65; inset: 0; background: rgba(15,23,42,.38); backdrop-filter: blur(2px); }
  .d-app-shell.sidebar-open .d-overlay { display: block; }
  .d-workspace { padding: 0.48rem; }
  .d-dyadic-grid, .d-compare-layout, .d-sequence-layout, .d-comparison-plots, .d-sequence-main {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: none;
    min-height: 0;
  }
  .d-dyadic-grid .d-convergence-card, .d-sequence-main .d-history-card { grid-column: auto; }
  .d-dyadic-grid .d-convergence-card { order: -1; }
  .d-editor-column { grid-template-rows: minmax(470px, 66dvh) auto; }
  .d-comparison-plots.only-two { grid-template-rows: none; }
  .d-plot-card { min-height: min(68dvh, 560px); }
  .d-convergence-card { min-height: min(58dvh, 500px); }
  .d-history-card, .d-interval-card { min-height: min(56dvh, 480px); }
  .partition-editor-wrap { min-height: 380px; }
  .d-partition-card .partition-editor-wrap { min-height: 0; }
}

@media (max-width: 560px) {
  :root { font-size: 15px; }
  .d-brand { gap: 0.35rem; }
  .d-brand img { width: 29px; height: 29px; border-radius: 7px; }
  .d-brand-title { font-size: 0.96rem; }
  .d-brand-title-full, .d-language-long { display: none; }
  .d-brand-title-short, .d-language-short { display: inline; }
  .d-top-actions { gap: 0.25rem; }
  .d-top-actions > a,
  .d-top-actions #share-button,
  .d-top-actions #install-button,
  .d-top-actions #about-button { display: none !important; }
  .d-icon-button { min-width: 38px; min-height: 36px; padding: 0.32rem 0.48rem; }
  .d-mode-tabs { padding: 0.38rem; }
  .d-tab { padding: 0.55rem 0.68rem; font-size: 0.83rem; }
  .d-keypad { grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 0.22rem; }
  .d-key { min-height: 34px; font-size: 0.76rem; }
  .d-grid-3-controls { grid-template-columns: 1fr 1fr; }
  .d-piecewise-row { grid-template-columns: 1fr; padding: 0.55rem; border: 1px solid var(--line); border-radius: 12px; }
  .d-metrics { grid-template-columns: 1fr; }
  .d-plot-card { min-height: 60dvh; }
  .d-convergence-card { min-height: 54dvh; }
  .d-plot-summary { padding: 0.38rem 0.55rem; font-size: 0.72rem; }
  .d-plot .modebar-container { top: 0.28rem !important; right: 0.3rem !important; }
  .d-plot .modebar { border-radius: 9px; }
  .d-plot .modebar-btn { width: 27px !important; height: 27px !important; padding: 4px !important; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; scroll-behavior: auto !important; }
}
