:root {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-alt: #f1f5f9;
  --border: #dde4ee;
  --text: #0f172a;
  --muted: #526174;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --accent: #0f766e;
  --danger: #dc2626;
  --warn-bg: #fffbeb;
  --warn-border: #fcd34d;
  --radius: 10px;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.05);
}

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

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Noto Sans CJK SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  font-size: 14px;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-inner {
  max-width: 1380px;
  margin: 0 auto;
  padding: 10px 18px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  justify-content: space-between;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 8px;
  background: var(--primary); color: #fff;
  display: grid; place-items: center;
  font-weight: 700; font-size: 18px;
}
.brand-title { display: block; font-size: 16px; letter-spacing: -0.02em; }
.brand-sub { display: block; font-size: 11px; color: var(--muted); }

.nav-tabs { display: flex; flex-wrap: wrap; gap: 4px; }
.nav-tab {
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  padding: 7px 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.nav-tab:hover { background: var(--surface-alt); color: var(--text); }
.nav-tab.active {
  background: #eff6ff;
  color: var(--primary);
  border-color: #bfdbfe;
  font-weight: 600;
}

.header-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.quota-pill {
  font-size: 12px;
  color: var(--muted);
  background: var(--surface-alt);
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.container {
  max-width: 1380px;
  margin: 18px auto;
  padding: 0 18px 28px;
}

.workspace-panel { display: none; }
.workspace-panel.active { display: block; }

.panel-grid {
  display: grid;
  grid-template-columns: minmax(340px, 430px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

@media (max-width: 980px) {
  .panel-grid { grid-template-columns: 1fr; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}

.card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}
.card-head h2 { margin: 0; font-size: 16px; }

.form-row { display: block; margin-bottom: 12px; }
.form-row label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); font-weight: 500; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.three-col { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.inline-pair { display: flex; gap: 6px; align-items: center; }
.inline-pair > input, .inline-pair > select { flex: 1; min-width: 0; }

input[type="text"], input[type="email"], input[type="number"], select, textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 13px;
  color: var(--text);
  background: #fff;
  font-family: inherit;
}
textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #93c5fd; border-color: var(--primary); }

.select-sm { width: auto; max-width: 210px; font-size: 12px; padding: 5px 8px; }
.checkbox-label { flex-direction: row !important; align-items: center; gap: 6px !important; padding-top: 18px; color: var(--text) !important; }
.checkbox-label input { width: auto; }
.action-align { display: flex; align-items: flex-end; justify-content: flex-end; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 7px; font-size: 13px; font-weight: 600;
  border: 1px solid transparent; cursor: pointer; white-space: nowrap;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-outline { background: #fff; border-color: var(--border); color: var(--text); }
.btn-outline:hover { background: var(--surface-alt); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-danger { background: #fef2f2; border-color: #fecaca; color: var(--danger); }

.parse-hint { font-size: 11.5px; color: var(--muted); min-height: 16px; margin-top: 3px; }
.toolbar-inline { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 10px; font-size: 12px; color: var(--muted); }
.cache-note { font-size: 12px; color: var(--accent); background: #f0fdfa; border: 1px solid #99f6e4; padding: 8px 10px; border-radius: 7px; margin-bottom: 12px; }

.result-pane { display: flex; flex-direction: column; gap: 14px; }
.empty-state {
  background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius);
  padding: 48px 24px; text-align: center; color: var(--muted);
}

/* Badges & KPIs */
.decision-banner {
  background: #eff6ff; border: 1px solid #bfdbfe; border-radius: var(--radius);
  padding: 12px 14px; display: flex; flex-direction: column; gap: 8px;
}
.badge-row { display: flex; flex-wrap: wrap; gap: 6px; }
.badge {
  display: inline-block; padding: 3px 8px; border-radius: 999px;
  font-size: 11.5px; font-weight: 600;
}
.badge-ok { background: #dcfce7; color: #166534; }
.badge-fail { background: #fee2e2; color: #991b1b; }
.badge-skip { background: #e2e8f0; color: #475569; }

.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.kpi-box { background: var(--surface-alt); border-radius: 8px; padding: 10px 12px; }
.kpi-box span { display: block; font-size: 11.5px; color: var(--muted); }
.kpi-box strong { font-size: 17px; color: var(--text); }
.kpi-box.sig strong { color: var(--danger); }
.kpi-box.ns strong { color: var(--accent); }

.apa-box { background: #f8fafc; border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; }
.apa-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 6px; }
.apa-text { margin: 0; font-size: 13.5px; color: var(--text); }

.warn-box { background: var(--warn-bg); border: 1px solid var(--warn-border); border-radius: 8px; padding: 12px 14px; }
.warn-box ul { margin: 6px 0 0; padding-left: 18px; }
.warn-box li { font-size: 12.5px; margin-bottom: 4px; }

.chart-tabs { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.chart-stage { width: 100%; min-height: 360px; }

/* Responsive tables (modern-web-guidance: responsive-table) */
.table-wrapper {
  overflow: auto;
  max-inline-size: 100%;
  max-block-size: min(420px, 65vh);
  container-type: inline-size;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.data-table, .edit-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 12.5px;
}
.data-table thead, .edit-table thead {
  position: sticky;
  inset-block-start: 0;
  z-index: 1;
  background: var(--surface-alt);
}
.data-table th, .data-table td, .edit-table th, .edit-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}
.edit-table input { padding: 4px 6px; min-width: 68px; font-size: 12.5px; }

/* Collapsible details (modern-web-guidance: accordion) */
details {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
}
summary {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
summary::before {
  content: "▸";
  display: inline-block;
  transition: transform 0.2s;
}
details[open] > summary::before { transform: rotate(90deg); }
details::details-content {
  interpolate-size: allow-keywords;
  overflow: clip;
  contain: layout;
}
details:not([open])::details-content { block-size: 0; }
@media (prefers-reduced-motion: no-preference) {
  details::details-content {
    transition: block-size 0.25s ease, content-visibility 0.25s allow-discrete;
  }
}

.deriv-step { border-top: 1px solid var(--border); padding: 10px 0; }
.deriv-step:first-child { border-top: none; }
.deriv-title { font-weight: 600; font-size: 13px; margin-bottom: 6px; }
.katex-line { overflow-x: auto; padding: 4px 0; }
.code-box {
  background: #0f172a; color: #e2e8f0; border-radius: 7px; padding: 10px 12px;
  overflow-x: auto; font-size: 12px; margin: 8px 0 0;
}

.anova-group-item { border: 1px solid var(--border); border-radius: 8px; padding: 8px; margin-bottom: 8px; background: #fafcff; }
.anova-group-head { display: flex; gap: 6px; margin-bottom: 6px; }

.export-bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: flex-end; }
.toast {
  position: fixed; bottom: 20px; right: 20px; z-index: 40;
  background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 8px;
  font-size: 13px; box-shadow: 0 8px 24px rgba(15, 23, 42, 0.2);
}

.footer {
  max-width: 1380px; margin: 0 auto 24px; padding: 14px 18px 0;
  border-top: 1px solid var(--border);
  display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px;
  font-size: 12px; color: var(--muted);
}
.footer a { color: var(--primary); text-decoration: none; }

/* Native Modal Dialog (modern-web-guidance: dialog) */
dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px 22px;
  width: min(520px, calc(100vw - 24px));
  max-height: 88vh;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.2);
}
dialog[open] { display: flex; flex-direction: column; gap: 12px; }
dialog:modal { overscroll-behavior: contain; }
dialog::backdrop {
  background: rgba(15, 23, 42, 0.45);
  overflow: hidden;
  overscroll-behavior: contain;
}
.dialog-header { display: flex; justify-content: space-between; align-items: center; }
.dialog-header h1 { margin: 0; font-size: 17px; }
.icon-close { border: none; background: transparent; font-size: 22px; cursor: pointer; color: var(--muted); padding: 2px 8px; }
.dialog-desc { margin: 0; font-size: 13px; color: var(--muted); }
.inline-alert { padding: 8px 10px; border-radius: 7px; font-size: 12.5px; background: #fef2f2; border: 1px solid #fecaca; color: #991b1b; }
.inline-alert.ok { background: #f0fdf4; border-color: #bbf7d0; color: #166534; }
.user-banner { display: flex; justify-content: space-between; align-items: center; background: var(--surface-alt); padding: 10px 12px; border-radius: 8px; }
.subhead { margin: 6px 0 4px; font-size: 14px; }
.muted { font-size: 12px; color: var(--muted); }
.key-reveal { background: #eff6ff; border: 1px solid #93c5fd; border-radius: 8px; padding: 10px; font-size: 12.5px; }
.key-reveal code { word-break: break-all; font-size: 12px; }
