:root {
  --bg: #ffffff;
  --bg-alt: #f5f5f7;
  --surface: #ffffff;
  --border: #e4e4e7;
  --text: #111111;
  --text-muted: #6b7280;
  --accent: #7c3aed;
  --accent-hover: #6d28d9;
  --accent-soft: #ede9fe;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04);
  color-scheme: light dark;
  forced-color-adjust: none;
}

[data-theme="dark"] {
  --bg: #0e0e10;
  --bg-alt: #18181b;
  --surface: #18181b;
  --border: #2a2a2e;
  --text: #f4f4f5;
  --text-muted: #9ca3af;
  --accent: #a78bfa;
  --accent-hover: #c4b5fd;
  --accent-soft: #2e1065;
  --danger: #f87171;
  --danger-soft: #450a0a;
  --ok: #4ade80;
  --ok-soft: #052e16;
  --warn: #fbbf24;
  --warn-soft: #422006;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); text-decoration: underline; }

h1, h2, h3, h4 { margin: 0 0 0.5em; font-weight: 600; line-height: 1.25; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.05rem; }

/* ---------- Layout ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.6rem 1.2rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar .brand {
  font-weight: 700;
  color: var(--accent);
  font-size: 1.05rem;
  letter-spacing: -0.01em;
}
.topbar nav { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.topbar .spacer { flex: 1; }
.topbar .user { color: var(--text-muted); font-size: 0.85rem; }

.container { max-width: 1100px; margin: 0 auto; padding: 1.5rem 1.2rem 4rem; }
.container.wide { max-width: 1400px; }

.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.1rem;
  box-shadow: var(--shadow);
}
.card .metric { font-size: 1.8rem; font-weight: 700; color: var(--text); }
.card .label { color: var(--text-muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; }
.card.accent { border-left: 3px solid var(--accent); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 0.9rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  font-family: inherit;
}
.btn:hover { background: var(--bg-alt); text-decoration: none; color: var(--text); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); color: #fff; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { opacity: 0.9; color: #fff; }
.btn-ghost { background: transparent; }
.btn-sm { padding: 0.3rem 0.6rem; font-size: 0.8rem; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Forms ---------- */
input, select, textarea {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
  border-color: var(--accent);
}
label { display: block; font-size: 0.85rem; color: var(--text-muted); margin-bottom: 0.25rem; font-weight: 500; }
.form-row { margin-bottom: 0.9rem; }
.field-inline { display: flex; gap: 0.5rem; align-items: flex-end; }
.field-inline > div { flex: 1; }

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { text-align: left; padding: 0.55rem 0.7rem; border-bottom: 1px solid var(--border); }
th { color: var(--text-muted); font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.03em; }
tbody tr:hover { background: var(--bg-alt); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Badges ---------- */
.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--accent-soft);
  color: var(--accent);
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.muted { background: var(--bg-alt); color: var(--text-muted); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }

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

/* ---------- Accordion (sanfona) ---------- */
.acc { border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.acc-item { border-bottom: 1px solid var(--border); }
.acc-item:last-child { border-bottom: none; }
.acc-head {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 0.9rem;
  cursor: pointer;
  background: var(--surface);
  user-select: none;
}
.acc-head:hover { background: var(--bg-alt); }
.acc-head .chev { color: var(--text-muted); transition: transform 0.15s; }
.acc-item.open .acc-head .chev { transform: rotate(90deg); }
.acc-body { display: none; padding: 0; }
.acc-item.open .acc-body { display: block; }
.acc-meta { margin-left: auto; display: flex; gap: 1rem; align-items: center; font-variant-numeric: tabular-nums; }
.acc-meta .frac { font-weight: 600; }
.acc-meta .acc { font-size: 0.8rem; color: var(--text-muted); }
.acc-body .acc { border: none; border-radius: 0; }
.acc-body .acc-item { border-bottom: 1px solid var(--border); }

/* nested indent */
.acc-body .acc-head { padding-left: 2.2rem; }
.acc-body .acc-body .acc-head { padding-left: 3.5rem; }
.acc-body .acc-body .acc-body .acc-head { padding-left: 4.8rem; }

/* ---------- Tree selection ---------- */
.tree { border: 1px solid var(--border); border-radius: 10px; padding: 0.5rem; max-height: 60vh; overflow: auto; }
.tree label { display: flex; align-items: center; gap: 0.4rem; padding: 0.3rem 0.4rem; border-radius: 6px; color: var(--text); font-size: 0.92rem; margin: 0; }
.tree label:hover { background: var(--bg-alt); }
.tree input[type=checkbox] { width: auto; }
.tree .indent-1 { padding-left: 1.5rem; }
.tree .indent-2 { padding-left: 2.5rem; }
.tree .indent-3 { padding-left: 3.5rem; }
.tree .indent-4 { padding-left: 4.5rem; }
.tree .indent-5 { padding-left: 5.5rem; }

/* ---------- Contagem ---------- */
.bip-input { font-size: 1.2rem; padding: 0.8rem; }
.item-row {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 0.8rem;
  align-items: center;
  padding: 0.6rem 0.2rem;
  border-bottom: 1px solid var(--border);
}
.item-row .name { font-weight: 500; }
.item-row .code { color: var(--text-muted); font-size: 0.8rem; }
.item-row .qty { font-variant-numeric: tabular-nums; font-weight: 600; min-width: 3ch; text-align: right; }

.muted { color: var(--text-muted); }
.center { text-align: center; }
.right { text-align: right; }
.flex { display: flex; gap: 0.5rem; align-items: center; }
.flex.wrap { flex-wrap: wrap; }
.flex.between { justify-content: space-between; }
.mt { margin-top: 1rem; }
.mt-lg { margin-top: 2rem; }
.mb { margin-bottom: 1rem; }
.alert { padding: 0.7rem 0.9rem; border-radius: 8px; background: var(--accent-soft); color: var(--accent); border: 1px solid var(--accent); }
.alert.warn { background: var(--warn-soft); color: var(--warn); border-color: var(--warn); }
.alert.danger { background: var(--danger-soft); color: var(--danger); border-color: var(--danger); }
.alert.ok { background: var(--ok-soft); color: var(--ok); border-color: var(--ok); }

.kvs { display: grid; grid-template-columns: max-content 1fr; gap: 0.3rem 1rem; font-size: 0.9rem; }
.kvs dt { color: var(--text-muted); }
.kvs dd { margin: 0; font-weight: 500; }

/* theme toggle */
.theme-toggle {
  width: 38px; height: 38px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.05rem;
}

/* small sparkline table */
.spark { display: flex; gap: 2px; align-items: flex-end; height: 40px; }
.spark span { flex: 1; background: var(--accent); border-radius: 2px 2px 0 0; min-width: 2px; }

hr.sep { border: none; border-top: 1px solid var(--border); margin: 1.5rem 0; }

/* ---------- Sync overlay (iniciar contagem) ---------- */
.sync-overlay {
  position: fixed; inset: 0; z-index: 9999;
  display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(2px);
}
.sync-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.6rem 2rem;
  text-align: center;
  max-width: 420px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.18);
}
.sync-spinner {
  width: 38px; height: 38px; margin: 0 auto 0.8rem;
  border: 4px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: sync-spin 0.8s linear infinite;
}
@keyframes sync-spin { to { transform: rotate(360deg); } }
