:root {
  --bg: #f4f8fb;
  --surface: rgba(255,255,255,0.92);
  --surface-strong: #ffffff;
  --surface-soft: #edf7f8;
  --text: #15303b;
  --muted: #67808a;
  --line: #d9e6ea;
  --primary: #167f87;
  --primary-2: #4eb6ae;
  --primary-soft: #dff4f2;
  --accent: #f39a68;
  --danger: #d34f64;
  --shadow: 0 18px 50px rgba(31, 73, 86, 0.12);
  --radius: 20px;
  --sidebar: 390px;
}

body.dark {
  --bg: #0e1c22;
  --surface: rgba(20, 40, 48, 0.94);
  --surface-strong: #152a32;
  --surface-soft: #18363d;
  --text: #eaf7f7;
  --muted: #99b4bc;
  --line: #2d4a52;
  --primary: #58c7bf;
  --primary-2: #6fdbd1;
  --primary-soft: #173e40;
  --accent: #ffac7c;
  --danger: #ff8090;
  --shadow: 0 18px 50px rgba(0,0,0,0.28);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at 10% 5%, rgba(78,182,174,0.15), transparent 26%),
    radial-gradient(circle at 90% 5%, rgba(243,154,104,0.13), transparent 24%),
    var(--bg);
  transition: background .25s ease, color .25s ease;
}
button, input, select, textarea { font: inherit; }
button { color: inherit; }

.app-shell { min-height: 100vh; }
.topbar {
  height: 78px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 28px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface-strong) 82%, transparent);
  backdrop-filter: blur(16px);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 13px; }
.brand h1 { margin: 0; font-size: 20px; letter-spacing: -0.02em; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.brand-mark {
  width: 44px; height: 44px; border-radius: 14px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  box-shadow: 0 10px 24px rgba(22,127,135,.25);
}
.brand-mark svg { width: 29px; height: 29px; fill: none; stroke: white; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.top-actions, .export-menu, .inline-actions { display: flex; align-items: center; gap: 9px; }

.workspace { display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); min-height: calc(100vh - 78px); }
.sidebar {
  padding: 22px 18px 34px;
  border-right: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface-strong) 76%, transparent);
  overflow-y: auto;
  max-height: calc(100vh - 78px);
  position: sticky;
  top: 78px;
}
.sidebar-heading { display: flex; justify-content: space-between; align-items: flex-start; padding: 0 4px 14px; }
.sidebar-heading h2 { margin: 4px 0 0; font-size: 22px; letter-spacing: -.03em; }
.eyebrow { text-transform: uppercase; font-weight: 800; font-size: 10px; letter-spacing: .16em; color: var(--primary); }
.status-pill { display: inline-flex; align-items: center; gap: 6px; font-size: 10px; padding: 7px 9px; border-radius: 999px; background: var(--primary-soft); color: var(--primary); font-weight: 700; }
.status-pill span { width: 7px; height: 7px; border-radius: 50%; background: var(--primary-2); box-shadow: 0 0 0 4px color-mix(in srgb, var(--primary-2) 20%, transparent); }

details {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 15px;
  margin-bottom: 11px;
  overflow: hidden;
  box-shadow: 0 5px 18px rgba(31,73,86,.045);
}
summary { cursor: pointer; padding: 14px 15px; font-size: 13px; font-weight: 800; user-select: none; list-style: none; display: flex; justify-content: space-between; }
summary::-webkit-details-marker { display: none; }
summary::after { content: "+"; color: var(--primary); font-size: 18px; font-weight: 500; line-height: .8; }
details[open] summary::after { content: "−"; }
details[open] summary { border-bottom: 1px solid var(--line); }
.section-body { padding: 15px; }
.input-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 11px; }
.input-grid.compact { gap: 8px; }
.full { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span { color: var(--muted); font-size: 11px; font-weight: 700; line-height: 1.35; }
.field small { font-weight: 500; }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface-strong);
  color: var(--text);
  border-radius: 10px;
  padding: 9px 10px;
  outline: none;
  transition: border .18s ease, box-shadow .18s ease, background .2s ease;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary-2); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary-2) 17%, transparent); }
textarea { resize: vertical; min-height: 160px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 10.5px; line-height: 1.45; }
.mode-panel { margin-top: 14px; }
.hidden { display: none !important; }
.shape-card { margin-top: 11px; padding: 12px; background: var(--surface-soft); border-radius: 12px; border: 1px solid color-mix(in srgb, var(--line) 78%, transparent); }
.shape-title { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; font-size: 11px; }
.mini-select { width: auto; max-width: 145px; padding: 6px 8px; font-size: 10px; }
.helper { margin: 0 0 10px; font-size: 10.5px; line-height: 1.5; color: var(--muted); }
.helper code { color: var(--primary); }
.top-gap { margin-top: 12px; }
.file-button, .text-button {
  font-size: 10.5px; font-weight: 800; color: var(--primary); cursor: pointer;
  border: 0; background: transparent; padding: 7px 0;
}
.file-button input { display: none; }

.run-card { margin-top: 15px; padding: 14px; border-radius: 16px; background: linear-gradient(145deg, var(--surface-strong), var(--primary-soft)); border: 1px solid var(--line); box-shadow: var(--shadow); }
.primary-button {
  width: 100%; border: 0; border-radius: 12px; padding: 13px 16px;
  display: flex; align-items: center; justify-content: center; gap: 10px;
  font-weight: 850; font-size: 13px; color: white; cursor: pointer;
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  box-shadow: 0 10px 22px rgba(22,127,135,.22);
  transition: transform .16s ease, filter .16s ease;
}
.primary-button:hover { transform: translateY(-1px); filter: saturate(1.08); }
.primary-button:disabled { opacity: .65; cursor: progress; transform: none; }
.button-icon { font-size: 11px; }
.progress-track { height: 5px; margin-top: 12px; border-radius: 99px; overflow: hidden; background: color-mix(in srgb, var(--line) 72%, transparent); }
.progress-track div { height: 100%; width: 0; border-radius: 99px; background: linear-gradient(90deg, var(--primary), var(--accent)); transition: width .18s ease; }
.run-meta { display: flex; justify-content: space-between; margin-top: 7px; font-size: 9.5px; color: var(--muted); }

.results { padding: 24px 27px 38px; min-width: 0; }
.hero-card { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 17px; }
.hero-card h2 { margin: 4px 0 5px; font-size: clamp(24px, 3vw, 34px); letter-spacing: -.04em; }
.hero-card p { margin: 0; font-size: 12px; color: var(--muted); }
.ghost-button, .icon-button {
  border: 1px solid var(--line); background: var(--surface); color: var(--text); cursor: pointer;
  border-radius: 11px; font-weight: 750; font-size: 11px; padding: 9px 12px;
  transition: border .16s ease, transform .16s ease, background .16s ease;
}
.ghost-button:hover, .icon-button:hover { border-color: var(--primary-2); transform: translateY(-1px); background: var(--primary-soft); }
.icon-button { width: 38px; height: 38px; padding: 0; font-size: 18px; display: grid; place-items: center; }
.metrics-grid { display: grid; grid-template-columns: repeat(5, minmax(120px, 1fr)); gap: 11px; margin-bottom: 18px; }
.metric-card { padding: 14px 15px; background: var(--surface); border: 1px solid var(--line); border-radius: 15px; box-shadow: 0 6px 22px rgba(31,73,86,.05); }
.metric-card span { display: block; font-weight: 800; font-size: 11px; color: var(--primary); }
.metric-card strong { display: block; margin: 5px 0 1px; font-size: 24px; letter-spacing: -.04em; font-variant-numeric: tabular-nums; }
.metric-card small { color: var(--muted); font-size: 9px; }
.tabs { display: flex; gap: 4px; padding: 5px; width: fit-content; max-width: 100%; overflow-x: auto; background: var(--surface-soft); border: 1px solid var(--line); border-radius: 13px; margin-bottom: 12px; }
.tab { border: 0; background: transparent; color: var(--muted); font-size: 11px; font-weight: 800; padding: 9px 14px; border-radius: 9px; cursor: pointer; white-space: nowrap; }
.tab.active { background: var(--surface-strong); color: var(--primary); box-shadow: 0 4px 12px rgba(31,73,86,.08); }
.tab-panel { display: none; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.tab-panel.active { display: block; }
.plot-toolbar { min-height: 54px; display: flex; align-items: center; flex-wrap: wrap; gap: 14px; padding: 10px 15px; border-bottom: 1px solid var(--line); }
.plot-toolbar label { display: flex; align-items: center; gap: 7px; font-size: 10.5px; font-weight: 800; color: var(--muted); }
.plot-toolbar select { width: auto; min-width: 200px; padding: 7px 9px; font-size: 10.5px; }
.plot { width: 100%; min-height: 520px; }
.plot.large { height: min(68vh, 720px); }
.switch-label input { display: none; }
.switch { width: 34px; height: 19px; background: var(--line); border-radius: 99px; position: relative; cursor: pointer; }
.switch::after { content: ""; width: 13px; height: 13px; border-radius: 50%; background: white; position: absolute; top: 3px; left: 3px; transition: left .18s ease, background .18s ease; box-shadow: 0 2px 6px rgba(0,0,0,.18); }
.switch-label input:checked + .switch { background: var(--primary-2); }
.switch-label input:checked + .switch::after { left: 18px; }
.diagnostic-grid { padding: 18px; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.diagnostic-card { background: var(--surface-soft); border: 1px solid var(--line); border-radius: 14px; padding: 16px; }
.diagnostic-card.wide { grid-column: 1 / -1; }
.diagnostic-card h3 { margin: 0 0 12px; font-size: 14px; }
dl { margin: 0; display: grid; grid-template-columns: 1fr auto; gap: 8px 18px; font-size: 11px; }
dt { color: var(--muted); } dd { margin: 0; font-weight: 800; font-variant-numeric: tabular-nums; }
.scientific-note { color: var(--muted); font-size: 12px; line-height: 1.65; }
.scientific-note strong { color: var(--text); }

#equationsDialog { max-width: 720px; width: calc(100% - 32px); border: 1px solid var(--line); border-radius: 20px; background: var(--surface-strong); color: var(--text); box-shadow: 0 30px 100px rgba(0,0,0,.32); padding: 0; }
#equationsDialog::backdrop { background: rgba(7,24,30,.55); backdrop-filter: blur(4px); }
.dialog-header { display: flex; align-items: flex-start; justify-content: space-between; padding: 20px 22px; border-bottom: 1px solid var(--line); }
.dialog-header h2 { margin: 4px 0 0; font-size: 22px; }
.equation-content { padding: 20px 22px 24px; color: var(--muted); line-height: 1.6; font-size: 13px; }
.equation { margin: 10px 0; padding: 12px 14px; border-radius: 11px; background: var(--surface-soft); color: var(--text); font-family: Georgia, "Times New Roman", serif; text-align: center; font-size: 15px; }
.note-box { margin-top: 16px; padding: 13px 15px; background: color-mix(in srgb, var(--accent) 12%, var(--surface-soft)); border-left: 3px solid var(--accent); border-radius: 9px; }
#toast { position: fixed; right: 22px; bottom: 22px; z-index: 100; max-width: 360px; padding: 12px 15px; border-radius: 12px; background: #15303b; color: white; font-size: 11px; font-weight: 700; box-shadow: 0 14px 40px rgba(0,0,0,.25); opacity: 0; transform: translateY(12px); pointer-events: none; transition: opacity .2s ease, transform .2s ease; }
#toast.show { opacity: 1; transform: translateY(0); }
#toast.error { background: var(--danger); }

@media (max-width: 1180px) {
  :root { --sidebar: 350px; }
  .metrics-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 860px) {
  .topbar { padding: 0 16px; }
  .workspace { display: block; }
  .sidebar { position: static; max-height: none; border-right: 0; border-bottom: 1px solid var(--line); }
  .results { padding: 20px 16px 30px; }
  .metrics-grid { grid-template-columns: repeat(2, 1fr); }
  .hero-card { flex-direction: column; }
  .export-menu { flex-wrap: wrap; }
}
@media (max-width: 520px) {
  .brand p, #equationsBtn { display: none; }
  .input-grid { grid-template-columns: 1fr; }
  .full { grid-column: auto; }
  .metrics-grid { grid-template-columns: 1fr 1fr; }
  .plot.large { height: 560px; }
  .diagnostic-grid { grid-template-columns: 1fr; }
  .diagnostic-card.wide { grid-column: auto; }
}
