:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dfe3e8;
  --text: #1c2430;
  --muted: #667085;
  --primary: #1f6feb;
  --primary-soft: #e8f0fe;
  --success: #1a7f37;
  --success-soft: #e6f4ea;
  --invalid: #b26a00;
  --invalid-soft: #fff4e0;
  --failed: #cf222e;
  --failed-soft: #fdecee;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #171d24;
    --surface-2: #1f262f;
    --border: #2d3640;
    --text: #e6edf3;
    --muted: #8b98a5;
    --primary: #4c8dff;
    --primary-soft: #1a2a44;
    --success: #3fb950;
    --success-soft: #12261a;
    --invalid: #e3a33b;
    --invalid-soft: #2e2412;
    --failed: #f85149;
    --failed-soft: #3a1618;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 17px; }
h2 { font-size: 15px; }
h3 { font-size: 13px; }
code { font-family: var(--mono); font-size: 12.5px; }
a { color: var(--primary); }

.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.hidden { display: none !important; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 12px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand p { margin: 0; font-size: 12.5px; }
.logo { font-size: 26px; color: var(--primary); }
.tabs { display: flex; gap: 4px; }
.tab {
  border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 500;
  padding: 8px 14px; border-radius: 8px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.active { background: var(--primary-soft); color: var(--primary); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--invalid); display: inline-block; }

main { max-width: 1280px; margin: 0 auto; padding: 20px 24px 48px; }
.panel { display: flex; flex-direction: column; gap: 16px; }

/* ---------- generic ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px 18px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar, .savebar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.savebar {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 18px; position: sticky; top: 70px; z-index: 5; box-shadow: var(--shadow);
}
.toolbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.btn {
  font: inherit; font-weight: 500; padding: 7px 14px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--primary); }
.btn.primary-outline { color: var(--primary); border-color: var(--primary); }
.btn.danger-outline { color: var(--failed); border-color: var(--failed); }
.btn.small { padding: 3px 10px; font-size: 12.5px; }
.btn.icon { padding: 4px 10px; }

.switch { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; }

input[type=text], input[type=number], input[type=search], select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
textarea { resize: vertical; min-height: 36px; font-family: var(--mono); font-size: 12.5px; }

.alert { background: var(--failed-soft); color: var(--failed); border: 1px solid var(--failed); border-radius: var(--radius); padding: 10px 14px; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 18px; box-shadow: var(--shadow); border-top: 3px solid var(--primary);
}
.kpi.success { border-top-color: var(--success); }
.kpi.failed { border-top-color: var(--failed); }
.kpi-label { color: var(--muted); font-size: 12.5px; font-weight: 500; text-transform: uppercase; letter-spacing: .03em; }
.kpi-value { font-size: 30px; font-weight: 650; font-variant-numeric: tabular-nums; margin: 2px 0; }
.kpi.success .kpi-value { color: var(--success); }
.kpi.failed .kpi-value { color: var(--failed); }
.kpi-sub { color: var(--muted); font-size: 12.5px; }

/* ---------- timeline ---------- */
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12.5px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.sw-success { background: var(--success); }
.sw-invalid { background: var(--invalid); }
.sw-failed { background: var(--failed); }
.timeline svg { width: 100%; height: 160px; display: block; }
.timeline .axis { fill: var(--muted); font-size: 10px; }
.timeline .axis.now { fill: var(--primary); font-weight: 600; }
.timeline .grid { stroke: var(--border); stroke-width: 1; }
.timeline rect.success { fill: var(--success); }
.timeline rect.invalid { fill: var(--invalid); }
.timeline rect.failed { fill: var(--failed); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
.table-wrap.scroll { max-height: 560px; overflow-y: auto; }
.table-wrap.scroll thead th { position: sticky; top: 0; background: var(--surface); z-index: 1; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
.table thead th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.table tbody tr:last-child td, .table tbody tr:last-child th { border-bottom: 0; }
.table.clickable tbody tr { cursor: pointer; }
.table.clickable tbody tr:hover { background: var(--surface-2); }
.table td code { white-space: nowrap; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.SUCCESS { background: var(--success-soft); color: var(--success); }
.badge.INVALID { background: var(--invalid-soft); color: var(--invalid); }
.badge.FAILED { background: var(--failed-soft); color: var(--failed); }

.bar { display: flex; height: 8px; width: 120px; border-radius: 4px; overflow: hidden; background: var(--surface-2); }
.bar span.success { background: var(--success); }
.bar span.invalid { background: var(--invalid); }
.bar span.failed { background: var(--failed); }
.rate { display: inline-flex; align-items: center; gap: 8px; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; }
.filters input { width: 260px; }
.empty { text-align: center; padding: 20px 0 8px; margin: 0; }

/* ---------- rules ---------- */
.presets { display: flex; gap: 8px; flex-wrap: wrap; }
.rule {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin-bottom: 12px;
  background: var(--surface); border-left: 4px solid var(--primary);
}
.rule.disabled { opacity: .6; border-left-color: var(--border); }
.rule-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.rule-head .order { color: var(--muted); font-weight: 600; font-variant-numeric: tabular-nums; min-width: 24px; }
.rule-head input.name { flex: 1; font-weight: 600; }
.rule-section-label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; margin: 10px 0 6px; }

.field-row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 140px; flex: 1; }
.field > span { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.field.narrow { flex: 0 0 130px; min-width: 110px; }
.field.wide { flex: 2; min-width: 240px; }
.hint { font-size: 12.5px; color: var(--muted); margin-top: 6px; }
.hint .badge { margin-left: 4px; }
.hint.warn { color: var(--invalid); white-space: normal; max-width: 760px; }

/* ---------- setup ---------- */
.code {
  font-family: var(--mono); font-size: 12.5px; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px; overflow: auto; white-space: pre; margin: 8px 0;
}
.table th[scope], #tab-setup .table tbody th { width: 160px; color: var(--muted); font-weight: 500; }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.modal-box { background: var(--surface); border-radius: var(--radius); width: min(1200px, 100%); max-height: 92vh; overflow: auto; padding: 18px; border: 1px solid var(--border); }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.meta { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-bottom: 12px; font-size: 13px; }
.meta b { color: var(--muted); font-weight: 500; margin-right: 4px; }
.xml-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.xml-head { display: flex; align-items: center; justify-content: space-between; }
.code.xml { max-height: 60vh; white-space: pre; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 8px; z-index: 60; font-weight: 500;
}

@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .xml-grid { grid-template-columns: 1fr; }
  .savebar { top: 0; position: static; }
}
@media (max-width: 560px) {
  main { padding: 16px; }
  .topbar { padding: 12px 16px; }
  .kpis { grid-template-columns: 1fr; }
  .filters input { width: 100%; }
}
