:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #cbd5e1;
  --border-light: #e2e8f0;
  --text: #0f172a;
  --text-muted: #475569;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --input-bg: #fef9c3;
  --input-border: #eab308;
  --input-focus: #fde047;
  --calc-green-bg: #c6efce;
  --calc-green-text: #006100;
  --excel-green: #92d050;
  --warn-bg: #ffc7ce;
  --warn-text: #9c0006;
  --warn-red-text: #dc2626;
  --warn-yellow-bg: #ffff00;
  --masked-bg: #1e293b;
  --masked-text: #475569;
  --header-bg: #0f172a;
  --section-header-bg: #1e3a5f;
  --sub-header-bg: #e2e8f0;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.05);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans TC", sans-serif;
  font-size: 13px;
  line-height: 1.4;
  color: var(--text);
  background: var(--bg);
}

/* Top Sticky Toolbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--header-bg);
  color: #fff;
  padding: 10px 18px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.btn-portal-back {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  background: rgba(255, 255, 255, 0.12);
  color: #f1f5f9;
  text-decoration: none;
  font-size: 12px;
  font-weight: 500;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  transition: all 0.15s ease;
}

.btn-portal-back:hover {
  background: rgba(255, 255, 255, 0.25);
  color: #ffffff;
  border-color: rgba(255, 255, 255, 0.4);
}

.brand-title {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.2px;
}

.brand-ver {
  background: #334155;
  color: #cbd5e1;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
}

.primary-key-badge {
  background: #1e293b;
  border: 1px solid #475569;
  color: #93c5fd;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 6px;
}

.toolbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 5px 11px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-secondary {
  background: #334155;
  color: #f8fafc;
  border-color: #475569;
}
.btn-secondary:hover {
  background: #475569;
}

.btn-light {
  background: #fff;
  color: var(--text);
  border-color: var(--border);
}
.btn-light:hover {
  background: #f1f5f9;
}

.btn-accent {
  background: #0f766e;
  color: #fff;
}
.btn-accent:hover {
  background: #0d9488;
}

.btn-danger {
  background: #dc2626;
  color: #fff;
}
.btn-danger:hover {
  background: #b91c1c;
}

.btn-sm {
  padding: 3px 8px;
  font-size: 11.5px;
}

/* Legend & Jump Nav */
.subbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 7px 18px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  font-size: 11.5px;
  color: var(--text-muted);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 1px solid #94a3b8;
  display: inline-block;
}
.swatch-input { background: #ffff00; border-color: #ca8a04; }
.swatch-green { background: #92d050; border-color: #4d7c0f; }
.swatch-warn { background: #ffc7ce; border-color: #9c0006; }

.section-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.section-nav a {
  text-decoration: none;
  color: var(--text-muted);
  background: var(--surface-alt);
  border: 1px solid var(--border-light);
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 500;
}
.section-nav a:hover {
  background: #e0f2fe;
  color: #0369a1;
  border-color: #bae6fd;
}

/* Alert Banner */
.alert-banner {
  margin: 10px 18px 0;
  padding: 8px 14px;
  border-radius: 6px;
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #b91c1c;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.alert-banner.hidden {
  display: none;
}

/* Main Container */
.container {
  max-width: 1560px;
  margin: 0 auto;
  padding: 14px 18px 48px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Section Card */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.card-header {
  background: var(--section-header-bg);
  color: #fff;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.card-header h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.2px;
}

.card-header.internal-header {
  background: #7f1d1d;
}

.card-body {
  padding: 12px 14px;
}

/* Layout Grids inside Cards */
.split-2 {
  display: grid;
  grid-template-columns: minmax(480px, 1.15fr) minmax(360px, 1fr);
  gap: 16px;
  align-items: start;
}

.split-equal {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  align-items: start;
}

.split-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  align-items: start;
}

@media (max-width: 1120px) {
  .split-2, .split-equal, .split-3 {
    grid-template-columns: 1fr;
  }
}

/* Engineering Data Tables */
table.param-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
}

table.param-table th,
table.param-table td {
  border: 1px solid var(--border-light);
  padding: 4px 7px;
  vertical-align: middle;
}

table.param-table thead th {
  background: #e2e8f0;
  color: #1e293b;
  font-weight: 700;
  text-align: left;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

table.param-table .sub-hdr td {
  background: #f1f5f9;
  font-weight: 700;
  color: #334155;
}

.col-desc { min-width: 170px; }
.col-sym { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: #334155; white-space: nowrap; }
.col-val { width: 125px; text-align: right; font-variant-numeric: tabular-nums; }
.col-unit { width: 65px; color: var(--text-muted); white-space: nowrap; }
.col-note { font-size: 11.5px; color: var(--text-muted); }

/* Cell Types */
td.cell-input {
  background: #ffff88;
  padding: 2px 4px !important;
}

td.cell-input input,
td.cell-input select {
  width: 100%;
  background: #ffff00;
  border: 1px solid #d4b106;
  border-radius: 3px;
  padding: 2px 5px;
  font-size: 12.5px;
  font-family: inherit;
  font-weight: 600;
  color: #0f172a;
  text-align: right;
}

td.cell-input select {
  text-align: left;
  text-align-last: left;
}

td.cell-input input:focus,
td.cell-input select:focus {
  outline: 2px solid #2563eb;
  background: #fef08a;
}

td.cell-green {
  background: var(--excel-green);
  font-weight: 700;
  color: #052e16;
}

td.cell-calc {
  background: #f8fafc;
  font-weight: 500;
}

/* Conditional Formatting States */
td.cf-warn-red, span.cf-warn-red {
  background: #ffc7ce !important;
  color: #9c0006 !important;
  font-weight: 700;
}

td.cf-warn-text {
  color: #ff0000 !important;
  font-weight: 700 !important;
}

td.cf-warn-yellow-red {
  background: #ffff00 !important;
  color: #9c0006 !important;
  font-weight: 700 !important;
}
td.cf-warn-yellow-red input {
  background: #ffff00 !important;
  color: #9c0006 !important;
  font-weight: 700 !important;
}

td.cf-ok-green {
  background: #c6efce !important;
  color: #006100 !important;
  font-weight: 700;
}

td.cf-masked,
tr.cf-masked td,
.cf-masked-block {
  position: relative;
  opacity: 0.28;
  filter: grayscale(0.9);
  background: #cbd5e1 !important;
}

.dcm-hidden {
  opacity: 0.22;
  filter: grayscale(1);
  pointer-events: none;
}

.tooltip-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: #e0f2fe;
  color: #0369a1;
  font-size: 10px;
  font-weight: 700;
  margin-left: 4px;
  cursor: help;
  vertical-align: middle;
}

.inline-note {
  font-size: 11.5px;
  color: #475569;
  font-style: italic;
}

.cap-row-icon {
  height: 28px;
  width: auto;
  vertical-align: middle;
  border-radius: 3px;
  border: 1px solid var(--border-light);
}

/* Schematic Containers & Overlays */
.schematic-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.schematic-title {
  font-size: 12px;
  font-weight: 700;
  color: #334155;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.schematic-stage {
  position: relative;
  display: inline-block;
  padding: 20px 52px 24px 52px;
  background: #fff;
  max-width: 100%;
}

.schematic-stage.compact-pad {
  padding: 16px 24px 20px 24px;
}

.schematic-stage.comp-pad {
  padding: 22px 18px 18px 18px;
}

.schematic-img-wrap {
  position: relative;
  display: inline-block;
  line-height: 0;
}

.schematic-img-wrap img {
  max-width: 100%;
  height: auto;
  display: block;
}

.sch-overlay {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  font-size: 11px;
  font-weight: 700;
  color: #b91c1c;
  background: rgba(255, 255, 255, 0.88);
  border-radius: 3px;
  padding: 1px 3px;
  line-height: 1.15;
  white-space: pre-line;
  pointer-events: none;
  box-shadow: 0 0 0 1px rgba(226, 232, 240, 0.85);
}

.sch-overlay.nc {
  color: #94a3b8;
  font-weight: 500;
}

.sch-overlay.blue {
  color: #1d4ed8;
}

.sch-overlay.green {
  color: #15803d;
}

/* PWM Matrix */
.pwm-v {
  background: #dcfce7;
  color: #166534;
  font-weight: 700;
  text-align: center;
}
.pwm-x {
  background: #334155;
  color: #64748b;
  text-align: center;
}

/* PWMVID Indicator Bar */
.pwmvid-indicator {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 8px;
}

.pwmvid-pill {
  padding: 7px 12px;
  border-radius: 6px;
  border: 2px solid var(--border);
  background: #f8fafc;
  color: #64748b;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pwmvid-pill.active {
  border-color: #2563eb;
  background: #eff6ff;
  color: #1e40af;
}

/* Charts Grid */
.charts-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(420px, 1fr));
  gap: 14px;
}

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

.chart-panel {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chart-panel-hdr {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.chart-panel-hdr h3 {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  color: #0f172a;
}

.chart-readout {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  color: #334155;
  background: #f1f5f9;
  padding: 2px 7px;
  border-radius: 4px;
}

.chart-canvas-wrap {
  position: relative;
  width: 100%;
  height: 310px;
}

.chart-canvas-wrap canvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  font-size: 11px;
  color: #334155;
  padding-top: 2px;
}

.chart-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.line-swatch {
  width: 18px;
  height: 3px;
  display: inline-block;
}
.line-swatch.dashed {
  border-top: 2px dashed;
  height: 0;
}

/* Pole Zero Summary Strip */
.pz-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
  padding: 8px 10px;
  background: #f8fafc;
  border: 1px solid var(--border-light);
  border-radius: 6px;
  font-size: 12px;
}
.pz-chip {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 3px 8px;
}
.pz-chip strong {
  color: #1e3a5f;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-backdrop.hidden {
  display: none;
}

.modal {
  background: #fff;
  border-radius: 10px;
  width: min(780px, 92vw);
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
  overflow: hidden;
}

.modal-header {
  background: var(--header-bg);
  color: #fff;
  padding: 11px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-header h3 {
  margin: 0;
  font-size: 15px;
}

.modal-body {
  padding: 14px 16px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 10px 16px;
  border-top: 1px solid var(--border-light);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  background: #f8fafc;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 10px;
}

.form-field {
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.form-field label {
  font-size: 11.5px;
  font-weight: 600;
  color: #334155;
}
.form-field input,
.form-field select,
.form-field textarea {
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 5px 8px;
  font-size: 12.5px;
  font-family: inherit;
}

.hidden-section {
  display: none !important;
}

/* Diagnostics Tabs */
.diag-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
}
.diag-tab {
  padding: 5px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #f8fafc;
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
}
.diag-tab.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
