/* Tema escuro (padrão) */
:root,
[data-theme="dark"] {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface-2: #243044;
  --text: #e8edf4;
  --muted: #8b9cb3;
  --accent: #3d9cf0;
  --accent-dim: #2a6fad;
  --ok: #3ecf8e;
  --warn: #f0b429;
  --danger: #ef5b5b;
  --border: var(--border);
  --border-strong: #3a4a63;
  --code-bg: #0a0e14;
  --input-bg: #0f1419;
  --backdrop: rgba(0, 0, 0, 0.65);
  --row-hover: rgba(61, 156, 240, 0.06);
  --btn-primary-fg: #fff;
  --btn-danger-fg: #ffb4b4;
  --rel-preview-bg: #1a2332;
  --assist-user-bg: rgba(61, 156, 240, 0.18);
  --assist-assistant-bg: var(--surface-2);
  --chart-text: #e8edf4;
  --chart-muted: #8b9cb3;
  --chart-grid: var(--border);
  --radius: 10px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #eef1f6;
  --surface: #ffffff;
  --surface-2: #e8edf4;
  --text: #15202b;
  --muted: #5c6b7e;
  --accent: #2563eb;
  --accent-dim: #1d4ed8;
  --ok: #059669;
  --warn: #d97706;
  --danger: #dc2626;
  --border: #d0d7e2;
  --border-strong: #b8c4d4;
  --code-bg: #f8fafc;
  --input-bg: #ffffff;
  --backdrop: rgba(15, 20, 25, 0.45);
  --row-hover: rgba(37, 99, 235, 0.06);
  --btn-primary-fg: #fff;
  --btn-danger-fg: #b91c1c;
  --rel-preview-bg: #ffffff;
  --assist-user-bg: #dbeafe;
  --assist-assistant-bg: #f1f5f9;
  --chart-text: #15202b;
  --chart-muted: #5c6b7e;
  --chart-grid: #e2e8f0;
  color-scheme: light;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.app-shell {
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 0;
}

.brand {
  padding: 0 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1rem;
}

.brand-mark {
  display: block;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--accent);
  font-weight: 700;
}

.brand-name {
  font-size: 1.15rem;
  font-weight: 600;
}

.nav { flex: 1; display: flex; flex-direction: column; gap: 2px; }

.nav-link {
  color: var(--muted);
  text-decoration: none;
  padding: 0.55rem 1.25rem;
  font-size: 0.9rem;
  border-left: 3px solid transparent;
}

.nav-link:hover { color: var(--text); background: var(--surface-2); }

.nav-link.is-active {
  color: var(--text);
  background: var(--surface-2);
  border-left-color: var(--accent);
}

.sidebar-foot {
  padding: 1rem 1.25rem 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.8rem;
}

.badge {
  display: inline-block;
  background: var(--surface-2);
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  width: fit-content;
}

.main { display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem 1.75rem;
  border-bottom: 1px solid var(--border);
}

.topbar h1 { margin: 0; font-size: 1.35rem; font-weight: 600; }

.api-status {
  font-size: 0.8rem;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}

.api-status.is-ok { color: var(--ok); }
.api-status.is-error { color: var(--danger); }

.content { padding: 1.5rem 1.75rem 2rem; }

.grid.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.grid.charts {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

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

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
}

.card-wide { grid-column: 1 / -1; }

@media (min-width: 960px) {
  .card-wide { grid-column: auto; }
}

.card h3 {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.kpi {
  margin: 0;
  font-size: 2rem;
  font-weight: 700;
  color: var(--accent);
}

.muted { color: var(--muted); font-size: 0.9rem; }

code {
  background: var(--surface-2);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  font-size: 0.85em;
}

.code-block {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  overflow-x: auto;
  font-size: 0.82rem;
}

/* CRUD */
.page-intro { margin: 0 0 1.25rem; }

.crud-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1rem;
}

.crud-filters { display: flex; flex-wrap: wrap; gap: 0.5rem; flex: 1; }

.filter-input {
  background: var(--input-bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.45rem 0.65rem;
  border-radius: 6px;
  font-size: 0.88rem;
  min-width: 140px;
}

.btn {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 0.45rem 0.9rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.88rem;
}

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

.btn-primary {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--btn-primary-fg);
}

.btn-sm { padding: 0.3rem 0.55rem; font-size: 0.8rem; }

.btn-danger { border-color: var(--danger); color: var(--btn-danger-fg); }

.btn-icon {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.4rem;
  cursor: pointer;
  line-height: 1;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

.data-table th,
.data-table td {
  padding: 0.65rem 0.85rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.data-table th {
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 500;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.data-table tr:hover td { background: var(--row-hover); }

.data-table .actions {
  white-space: nowrap;
  display: flex;
  gap: 0.35rem;
}

.badge-status {
  display: inline-block;
  padding: 0.15rem 0.45rem;
  border-radius: 4px;
  font-size: 0.75rem;
  background: var(--surface-2);
  color: var(--muted);
}

.badge-status.ok { background: rgba(62, 207, 142, 0.15); color: var(--ok); }

.crud-modal {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  background: var(--surface);
  color: var(--text);
  max-width: 560px;
  width: calc(100% - 2rem);
}

.crud-modal::backdrop { background: var(--backdrop); }

.crud-modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.crud-modal-head h2 { margin: 0; font-size: 1.1rem; }

.crud-form-body {
  padding: 1rem 1.25rem;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  max-height: 60vh;
  overflow-y: auto;
}

.field { display: flex; flex-direction: column; gap: 0.25rem; }

.field span { font-size: 0.78rem; color: var(--muted); }

.field input,
.field select,
.field textarea {
  background: var(--input-bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.45rem 0.55rem;
  border-radius: 6px;
  font: inherit;
}

.field-wide { grid-column: 1 / -1; }

.crud-modal-foot {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 0.5rem;
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border);
}

.unsaved-badge {
  margin-right: auto;
  font-size: 0.8rem;
  color: var(--warn);
  padding: 0.25rem 0.6rem;
  border: 1px solid var(--warn);
  border-radius: 4px;
}

#btn-salvar-traco.is-dirty {
  box-shadow: 0 0 0 2px rgba(61, 156, 240, 0.45);
  animation: pulse-save 1.5s ease infinite;
}

@keyframes pulse-save {
  50% { box-shadow: 0 0 0 3px rgba(61, 156, 240, 0.55); }
}

.custo-mcc-readonly {
  display: block;
  padding: 0.45rem 0.55rem;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--accent);
  font-weight: 600;
}

.crud-toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  padding: 0.75rem 1.1rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  opacity: 0;
  transform: translateY(8px);
  transition: 0.2s ease;
  z-index: 1000;
  max-width: 320px;
}

.crud-toast.is-visible { opacity: 1; transform: translateY(0); }

.crud-toast--ok { border-color: var(--ok); }
.crud-toast--error { border-color: var(--danger); }

/* Traços */
.tracos-module .traco-editor { margin-top: 1.5rem; }

.tracos-module .traco-editor h3 {
  margin: 0 0 1rem;
  font-size: 1rem;
  color: var(--text);
  text-transform: none;
  letter-spacing: 0;
}

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

.traco-calc { margin-top: 1rem; }

.tabs {
  display: flex;
  gap: 0.35rem;
  margin-bottom: 1rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.5rem;
}

.tab {
  background: none;
  border: none;
  color: var(--muted);
  padding: 0.45rem 0.85rem;
  cursor: pointer;
  font-size: 0.88rem;
  border-radius: 6px 6px 0 0;
}

.tab.is-active {
  color: var(--accent);
  background: var(--surface-2);
}

.tab-panel { margin-bottom: 1rem; }

.composicao-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.composicao-hint { margin: 0 0 0.75rem; font-size: 0.85rem; }

.composicao-table input.filter-input,
.composicao-table select.filter-input {
  min-width: 90px;
  width: 100%;
}

.resumo-composicao { margin-top: 1rem; }

.resumo-composicao .card-wide .muted { margin: 0; font-size: 0.88rem; }

.import-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-bottom: 1rem;
}

.import-tab {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  padding: 0.45rem 0.9rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.88rem;
}

.import-tab.is-active {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--surface-2);
}

.import-panel[hidden] { display: none; }

.import-preview {
  margin-top: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--surface-2);
  border-radius: 8px;
  font-size: 0.9rem;
}

.csv-map-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.5rem;
  margin: 0.5rem 0;
}

.crud-modal-wide {
  max-width: min(920px, 96vw);
}

.nf-module .crud-toolbar { margin-bottom: 0.75rem; }

.curvas-layout,
.fck-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 1rem;
  align-items: start;
}

.curva-lista {
  list-style: none;
  margin: 0.5rem 0 0;
  padding: 0;
  font-size: 0.88rem;
}

.curva-lista li {
  margin-bottom: 0.35rem;
  display: flex;
  gap: 0.35rem;
  align-items: center;
}

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  text-align: left;
  padding: 0;
  font-size: inherit;
}

.link-btn:hover { text-decoration: underline; }

.chart-wrap {
  margin-top: 1rem;
  padding: 0.5rem;
  background: var(--surface-2);
  border-radius: 8px;
}

.granulo-charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1rem;
}

.granulo-charts h3 { margin: 0 0 0.5rem; font-size: 0.95rem; }

.rel-preview {
  width: 100%;
  min-height: 480px;
  margin-top: 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--rel-preview-bg);
}

.kpi-row { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }

.assist-log {
  max-height: 220px;
  overflow-y: auto;
  margin: 0.75rem 0;
  padding: 0.75rem;
  background: var(--surface-2, #f8fafc);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.assist-msg { margin: 0.35rem 0; padding: 0.5rem 0.65rem; border-radius: 6px; font-size: 0.9rem; }
.assist-msg--user { background: var(--assist-user-bg); margin-left: 1.5rem; }
.assist-msg--assistant { background: var(--assist-assistant-bg); margin-right: 1.5rem; }

/* Alternância de tema */
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.theme-switch {
  display: inline-flex;
  padding: 2px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  gap: 2px;
}

.theme-switch button {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
  font-size: 0.78rem;
  cursor: pointer;
  line-height: 1.2;
  transition: background 0.15s, color 0.15s;
}

.theme-switch button:hover {
  color: var(--text);
}

.theme-switch button.is-active {
  background: var(--surface);
  color: var(--accent);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

[data-theme="dark"] .theme-switch button.is-active {
  background: var(--bg);
}

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

@media (max-width: 768px) {
  .curvas-layout,
  .fck-layout { grid-template-columns: 1fr; }
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .crud-form-body { grid-template-columns: 1fr; }
}
