/* ════════════════════════════════════════
   Família Mendes — Design System
   ════════════════════════════════════════ */

@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:ital,wght@0,400;0,500;0,600;0,700;1,400&display=swap');

/* ── Tokens "Dossier Privado" (iguais ao CRM Somnium) ──
   Claro em :root, escuro em [data-theme="dark"]. Os nomes antigos
   (--bg-*, --text-*, --accent-light) mantêm-se porque os templates os usam inline. */
:root {
  --surface: #F5F5F0;
  --surface-raised: #FCFCF9;
  --surface-muted: #ECEBE4;
  --ink: #171715;
  --ink-muted: #6B6962;
  --line: #E0DED6;
  --line-soft: rgba(23,23,21,0.05);

  --bg-primary: var(--surface);
  --bg-secondary: var(--surface-raised);
  --bg-card: var(--surface-raised);
  --bg-tertiary: var(--surface-muted);
  --bg-glass: rgba(23,23,21,0.04);
  --border: var(--line);
  --border-hover: #C9C6BA;

  --accent: #C9A84C;
  --accent-rgb: 201,168,76;
  --accent-ink: #856924;
  --accent-light: var(--accent-ink);
  --accent-fg: #171715;
  --accent-dim: rgba(201,168,76,0.14);
  --accent-secondary: #3B6E8F;
  --focus-ring: #C9A84C;
  --success: #247A4C;
  --success-dim: rgba(36,122,76,0.12);
  --danger: #B42318;
  --danger-dim: rgba(180,35,24,0.10);
  --warning: #A86810;
  --warning-dim: rgba(168,104,16,0.12);
  --gold: #C9A84C;
  --gold-dim: rgba(201,168,76,0.14);

  --text-1: var(--ink);
  --text-2: #3A3935;
  --text-3: var(--ink-muted);
  --text-4: #8F8C83;

  --font-display: 'IBM Plex Sans', system-ui, -apple-system, sans-serif;
  --font-body: 'IBM Plex Sans', system-ui, -apple-system, sans-serif;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 12px;
  --radius-xl: 16px;

  --sidebar-width: 240px;
  --sidebar-width-collapsed: 68px;
  --topbar-height: 56px;

  --shadow-sm: 0 1px 2px rgba(23,23,21,0.05);
  --shadow-md: 0 4px 12px rgba(23,23,21,0.07);
  --shadow-lg: 0 10px 28px rgba(23,23,21,0.10);
  --card-shadow: 0 1px 2px rgba(23,23,21,0.04);
  --overlay: rgba(23,23,21,0.45);

  /* Movimento (mesmas durações/curvas do CRM) */
  --dur-fast: 120ms;
  --dur-base: 200ms;
  --dur-slow: 320ms;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --ease-in: cubic-bezier(0.4, 0, 1, 1);

  color-scheme: light;
}

[data-theme="dark"] {
  --surface: #0C0C0B;
  --surface-raised: #171715;
  --surface-muted: #20201D;
  --ink: #EDECE7;
  --ink-muted: #A3A199;
  --line: #2E2D29;
  --line-soft: rgba(237,236,231,0.05);

  --bg-glass: rgba(237,236,231,0.05);
  --border-hover: #45443E;

  --accent-ink: #DEC47A;
  --accent-fg: #121210;
  --accent-dim: rgba(201,168,76,0.16);
  --accent-secondary: #7FB0CF;
  --focus-ring: #D6B862;
  --success: #5CBE84;
  --success-dim: rgba(92,190,132,0.14);
  --danger: #F47064;
  --danger-dim: rgba(244,112,100,0.14);
  --warning: #E6AC46;
  --warning-dim: rgba(230,172,70,0.14);

  --text-2: #CFCDC5;
  --text-4: #75736B;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 10px 28px rgba(0,0,0,0.5);
  --card-shadow: none;
  --overlay: rgba(0,0,0,0.65);

  color-scheme: dark;
}

/* ── Reset ─────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-body);
  background: var(--bg-primary);
  color: var(--text-2);
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
  text-rendering: optimizeLegibility;
}

/* Foco: anel dourado só com teclado */
:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}
::selection { background: rgba(var(--accent-rgb),0.25); color: inherit; }
td, th, .kpi-value, .legend-pct { font-variant-numeric: tabular-nums; }

/* Movimento reduzido: anula animações e transições */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

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

h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-display);
  color: var(--text-1);
  font-weight: 600;
  letter-spacing: -0.01em;
}

/* ── Layout ────────────────────────────── */
.app-layout {
  display: flex;
  min-height: 100vh;
}

.main-content {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: margin-left 0.3s ease;
}

.page-body {
  flex: 1;
  padding: 20px 28px 40px;
  max-width: 1400px;
  width: 100%;
}

/* ── Sidebar ───────────────────────────── */
.sidebar {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform 0.3s ease;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 99;
  backdrop-filter: blur(4px);
}
.sidebar-overlay.show { display: block; }

.sidebar-logo {
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--border);
}

.logo-mark {
  display: flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
}

.logo-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent-fg);
  flex-shrink: 0;
  overflow: hidden;
}

.logo-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.logo-text {
  font-family: var(--font-display);
  font-size: 16px;
  font-weight: 700;
  color: var(--text-1);
  line-height: 1.2;
}
.logo-text span {
  display: block;
  font-size: 11px;
  font-weight: 400;
  color: var(--text-4);
  font-family: var(--font-body);
}

.sidebar-nav {
  flex: 1;
  padding: 16px 12px;
  overflow-y: auto;
}

.nav-section-label {
  display: block;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--text-4);
  padding: 16px 12px 6px;
}
.nav-section-label:first-child { padding-top: 0; }

.nav-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: var(--radius-md);
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text-3);
  transition: all 0.15s;
  text-decoration: none;
}
.nav-item:hover {
  background: var(--bg-glass);
  color: var(--text-1);
}
.nav-item.active {
  background: var(--bg-tertiary);
  color: var(--text-1);
  font-weight: 600;
}
.nav-item.active::before {
  content: '';
  position: absolute;
  left: -12px;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 20px;
  border-radius: 0 3px 3px 0;
  background: var(--accent);
}

.nav-icon { width: 18px; height: 18px; flex-shrink: 0; opacity: 0.7; }
.nav-item.active .nav-icon { opacity: 1; color: var(--accent-ink); }

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
  text-decoration: none;
}
.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--accent);
  color: var(--accent-fg);
  font-size: 13px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.user-meta {
  display: flex;
  flex-direction: column;
  line-height: 1.3;
  min-width: 0;
}
.user-meta strong {
  font-size: 13px;
  color: var(--text-1);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user-meta span {
  font-size: 11px;
  color: var(--text-4);
}
.collapse-btn {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  border: none;
  background: transparent;
  color: var(--text-3);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}
.collapse-btn:hover {
  background: var(--bg-glass);
  color: var(--text-1);
}

/* ── Sidebar colapsada (apenas desktop) ── */
@media (min-width: 769px) {
  .app-layout.collapsed .sidebar { width: var(--sidebar-width-collapsed); }
  .app-layout.collapsed .main-content { margin-left: var(--sidebar-width-collapsed); }

  .app-layout.collapsed .sidebar-logo { padding: 20px 0 16px; }
  .app-layout.collapsed .logo-mark { justify-content: center; }
  .app-layout.collapsed .logo-text { display: none; }

  .app-layout.collapsed .nav-section-label { display: none; }
  .app-layout.collapsed .nav-label { display: none; }
  .app-layout.collapsed .nav-item { justify-content: center; padding: 9px 0; gap: 0; }
  .app-layout.collapsed .nav-item.active::before { left: 0; }

  .app-layout.collapsed .sidebar-footer { flex-direction: column; gap: 10px; padding: 12px 0; }
  .app-layout.collapsed .user-chip { justify-content: center; flex: none; }
  .app-layout.collapsed .user-meta { display: none; }
}

/* ── Top Bar ───────────────────────────── */
.top-bar {
  height: var(--topbar-height);
  padding: 0 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
  position: sticky;
  top: 0;
  z-index: 50;
}

.page-title {
  font-size: 18px;
  font-weight: 700;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.topbar-date {
  font-size: 12px;
  color: var(--text-4);
}

.topbar-btn {
  width: 34px;
  height: 34px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-3);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s;
}
.topbar-btn:hover {
  background: var(--bg-glass);
  color: var(--text-1);
  border-color: var(--border-hover);
}

.menu-btn { display: none; }

/* ── Cards ─────────────────────────────── */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 20px 24px;
  box-shadow: var(--card-shadow);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.card-title {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 600;
  color: var(--text-1);
}

/* ── KPI Cards ─────────────────────────── */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 20px;
}

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 20px;
  position: relative;
  overflow: hidden;
  box-shadow: var(--card-shadow);
}
.kpi-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: var(--kpi-color, var(--accent));
}

.kpi-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.kpi-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.kpi-icon {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  background: var(--kpi-bg, var(--accent-dim));
  color: var(--kpi-color, var(--accent));
  display: flex;
  align-items: center;
  justify-content: center;
}

.kpi-value {
  font-family: var(--font-display);
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text-1);
  line-height: 1.2;
  margin-bottom: 6px;
}

.kpi-sub {
  font-size: 12px;
  color: var(--text-4);
  display: flex;
  align-items: center;
  gap: 4px;
}

.badge-up { color: var(--success) !important; }
.badge-down { color: var(--danger) !important; }

/* ── Grid Layouts ──────────────────────── */
.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.grid-3 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
}

.grid-main-side {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 16px;
}

/* ── Buttons ───────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius-md);
  font-family: var(--font-body);
  font-size: 13px;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
  text-decoration: none;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-fg);
  box-shadow: var(--shadow-sm);
}
.btn-primary:hover {
  background: #B8973F;
  color: var(--accent-fg);
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }

.btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-2);
  border: 1px solid var(--border);
}
.btn-secondary:hover {
  background: var(--bg-glass);
  border-color: var(--border-hover);
  color: var(--text-1);
}

.btn-ghost {
  background: transparent;
  color: var(--text-3);
  padding: 6px 10px;
}
.btn-ghost:hover {
  background: var(--bg-glass);
  color: var(--text-1);
}

.btn-danger {
  background: var(--danger-dim);
  color: var(--danger);
}
.btn-danger:hover {
  background: var(--danger);
  color: #fff;
}
.btn-danger-solid { background: var(--danger); color: #fff; }
.btn-danger-solid:hover { filter: brightness(0.92); color: #fff; }

.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-icon { padding: 6px; }

/* ── Forms ─────────────────────────────── */
.form-group { display: flex; flex-direction: column; gap: 6px; }

.form-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.form-control {
  padding: 9px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-1);
  font-family: var(--font-body);
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
  width: 100%;
}
.form-control:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}
.form-control[aria-invalid="true"] { border-color: var(--danger); }
.form-control[aria-invalid="true"]:focus { box-shadow: 0 0 0 3px var(--danger-dim); }
.form-control::placeholder { color: var(--text-4); }

select.form-control {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%2394a3b8' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}

/* ── Tables ────────────────────────────── */
.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

thead th {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-4);
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}

tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line-soft);
  font-size: 13px;
  color: var(--text-2);
}

tbody tr:hover {
  background: var(--bg-glass);
}

tbody tr:last-child td {
  border-bottom: none;
}

/* ── Tabs ──────────────────────────────── */
.tabs {
  display: flex;
  gap: 2px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-md);
  padding: 3px;
}

.tab-btn {
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  border: none;
  background: transparent;
  color: var(--text-3);
  font-family: var(--font-body);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.tab-btn:hover { color: var(--text-1); }
.tab-btn.active {
  background: var(--bg-card);
  color: var(--text-1);
  box-shadow: var(--shadow-sm);
}

/* ── Empty States ──────────────────────── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  text-align: center;
}

.empty-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--bg-tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-4);
  margin-bottom: 12px;
}

.empty-title {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 600;
  color: var(--text-3);
  margin-bottom: 4px;
}

.empty-sub {
  font-size: 12.5px;
  color: var(--text-4);
  max-width: 280px;
}

/* ── Legend ─────────────────────────────── */
.legend-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.legend-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12.5px;
}

.legend-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.legend-color {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: block;
  flex-shrink: 0;
}

.legend-name {
  color: var(--text-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.legend-pct { color: var(--text-3); font-weight: 600; font-family: var(--font-display); }

/* ── Drop Zone ─────────────────────────── */
.drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-xl);
  padding: 40px 24px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
}
.drop-zone:hover, .drop-zone.dragover {
  border-color: var(--accent);
  background: var(--accent-dim);
}

.drop-zone-icon {
  color: var(--text-4);
  margin-bottom: 12px;
}
.drop-zone-title {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 600;
  color: var(--text-2);
  margin-bottom: 4px;
}
.drop-zone-sub {
  font-size: 12px;
  color: var(--text-4);
}

/* ── AI Box ────────────────────────────── */
.ai-box {
  background: var(--accent-dim);
  border: 1px solid rgba(var(--accent-rgb),0.3);
  border-radius: var(--radius-xl);
  padding: 24px;
}

.ai-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent-ink);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
}

.ai-label-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.ai-text {
  font-size: 14px;
  color: var(--text-2);
  line-height: 1.7;
}

/* ── Pagination ────────────────────────── */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 16px;
}

.pagination button {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-3);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}
.pagination button:hover { background: var(--bg-glass); color: var(--text-1); }
.pagination button.active {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}

/* ── Toast ─────────────────────────────── */
.toast-container {
  position: fixed;
  top: calc(16px + env(safe-area-inset-top));
  right: 16px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 12px 14px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  font-size: 13px;
  color: var(--text-1);
  box-shadow: var(--shadow-lg);
  min-width: 280px;
  max-width: min(420px, calc(100vw - 32px));
  pointer-events: auto;
  animation: toastIn var(--dur-base) var(--ease-out);
}
.toast.saindo { animation: toastOut var(--dur-fast) var(--ease-in) forwards; }
.toast-dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 6px; flex-shrink: 0; background: var(--text-3); }
.toast.success .toast-dot { background: var(--success); }
.toast.error .toast-dot { background: var(--danger); }
.toast.warning .toast-dot { background: var(--warning); }
.toast-msg { flex: 1; min-width: 0; line-height: 1.45; }
.toast-close { border: none; background: none; color: var(--text-4); cursor: pointer; padding: 2px; line-height: 0; border-radius: 4px; }
.toast-close:hover { color: var(--text-1); }

@keyframes toastIn {
  from { transform: translateY(-6px) scale(0.98); opacity: 0; }
  to { transform: none; opacity: 1; }
}
@keyframes toastOut {
  to { transform: translateX(12px); opacity: 0; }
}

/* ── Modal Overlay ─────────────────────── */
/* Visível/oculto por opacidade (não display) para animar a saída. */
.modal-overlay {
  display: flex;
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: 200;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  align-items: center;
  justify-content: center;
  transition: opacity var(--dur-fast) var(--ease-in), visibility 0s linear var(--dur-fast);
}
.modal-overlay.show {
  visibility: visible;
  opacity: 1;
  pointer-events: auto;
  transition: opacity var(--dur-base) var(--ease-out), visibility 0s;
}

.modal-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 28px;
  width: 100%;
  max-width: 460px;
  margin: 16px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  transform: translateY(8px) scale(0.98);
  transition: transform var(--dur-fast) var(--ease-in);
}
.modal-overlay.show .modal-box {
  transform: none;
  transition: transform var(--dur-slow) var(--ease-out);
}
.modal-box:focus { outline: none; }
/* Variante para conteúdo longo: alinhada ao topo e com scroll no overlay */
.modal-overlay.modal-top { align-items: flex-start; padding: 40px 16px; overflow-y: auto; }
.modal-overlay.modal-top .modal-box { max-height: none; overflow: visible; margin: 0; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}

.modal-title {
  font-family: var(--font-display);
  font-size: 18px;
  font-weight: 600;
}

/* ── Diálogo da plataforma (confirmar / pedirValor) ── */
.dlg-box { max-width: 400px; padding: 22px 22px 18px; }
.dlg-title { font-size: 16px; font-weight: 600; color: var(--text-1); margin-bottom: 6px; }
.dlg-msg { font-size: 13.5px; color: var(--text-2); line-height: 1.55; white-space: pre-line; }
.dlg-input { margin-top: 14px; }
.dlg-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

/* ── Estado de erro ────────────────────── */
.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 32px 20px;
  gap: 6px;
}
.error-state-icon {
  width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--danger-dim); color: var(--danger);
  margin-bottom: 6px;
}
.error-state-title { font-size: 14px; font-weight: 600; color: var(--text-1); }
.error-state-sub { font-size: 12.5px; color: var(--text-3); max-width: 340px; }
.error-state .btn { margin-top: 10px; }

/* ── Entrada de página: subida curta em cascata (máx. 6 blocos) ── */
@keyframes entrar {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}
.page-body > * { animation: entrar var(--dur-slow) var(--ease-out) both; }
.page-body > *:nth-child(2) { animation-delay: 30ms; }
.page-body > *:nth-child(3) { animation-delay: 60ms; }
.page-body > *:nth-child(4) { animation-delay: 90ms; }
.page-body > *:nth-child(5) { animation-delay: 120ms; }
.page-body > *:nth-child(n+6) { animation-delay: 150ms; }

/* Linha dourada de progresso enquanto há pedidos à API */
.fetch-bar {
  position: fixed; top: env(safe-area-inset-top); left: 0; height: 2px; width: 0;
  background: var(--accent); z-index: 1001; opacity: 0;
  transition: width 400ms var(--ease-out), opacity 200ms;
}
.fetch-bar.on { opacity: 1; width: 70%; transition: width 4s var(--ease-out), opacity 100ms; }
.fetch-bar.fim { opacity: 0; width: 100%; transition: width 200ms, opacity 300ms 150ms; }

/* ── Valor formatting ──────────────────── */
.val-pos { color: var(--success); font-weight: 600; }
.val-neg { color: var(--danger); font-weight: 600; }

/* ── Categoria badge ───────────────────── */
.cat-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 20px;
  font-weight: 500;
}

.cat-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* ── Anti-overflow global (mobile-first) ─── */
html, body {
  max-width: 100vw;
  overflow-x: hidden;
}
.card, .card * {
  /* Quebrar palavras longas em vez de cortar */
  overflow-wrap: anywhere;
  word-wrap: break-word;
}
/* Excepção: valores monetários nunca devem partir-se */
.kpi-value, [class*="val-"], [style*="font-family:var(--font-display)"] {
  overflow-wrap: normal;
  word-break: keep-all;
}

/* ── Responsive ────────────────────────── */
@media (max-width: 1024px) {
  .grid-main-side {
    grid-template-columns: 1fr;
  }
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .kpi-hero-grid {
    grid-template-columns: 1fr 1fr !important;
  }
}

@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
    width: 86vw;
    max-width: 320px;
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .main-content {
    margin-left: 0;
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
  }
  .menu-btn {
    display: flex;
  }
  .page-body {
    padding: 10px;
    max-width: 100vw;
  }
  .top-bar {
    padding: 0 10px;
    height: 50px;
  }
  .page-title {
    font-size: 15px;
  }
  .topbar-date {
    display: none;
  }
  .grid-2,
  .grid-main-side {
    grid-template-columns: 1fr !important;
  }
  /* Grid items têm min-width:auto por defeito → expandem além da track 1fr
     quando o conteúdo (tabelas, widgets) não encolhe, sendo cortados pelo
     overflow:hidden global. min-width:0 deixa-os respeitar a largura do ecrã. */
  .grid-2 > *,
  .grid-3 > *,
  .grid-main-side > *,
  .kpi-grid > *,
  .kpi-hero-grid > *,
  [style*="display:grid"] > *,
  [style*="display: grid"] > * {
    min-width: 0;
  }
  .kpi-grid,
  .kpi-hero-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
  }
  /* Grids inline com 4 colunas → 2 em mobile */
  [style*="grid-template-columns:repeat(4,1fr)"],
  [style*="grid-template-columns: repeat(4, 1fr)"] {
    grid-template-columns: 1fr 1fr !important;
  }
  /* Grids inline proporcionais → stack vertical */
  [style*="grid-template-columns:2fr 1fr"],
  [style*="grid-template-columns: 2fr 1fr"],
  [style*="grid-template-columns:1.4fr 0.7fr 1fr"],
  [style*="grid-template-columns: 1.4fr 0.7fr 1fr"],
  [style*="grid-template-columns:1fr 1fr 1fr"],
  [style*="grid-template-columns: 1fr 1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }
  /* Card padding mais compacto em mobile (afecta inline styles também) */
  .card {
    padding: 12px 14px !important;
  }
  /* Cards com padding inline grande (>=20px) — reduzir */
  .card[style*="padding:22px"],
  .card[style*="padding: 22px"],
  .card[style*="padding:20px"],
  .card[style*="padding: 20px"],
  .card[style*="padding:24px"],
  .card[style*="padding: 24px"],
  .card[style*="padding:18px"],
  .card[style*="padding: 18px"] {
    padding: 14px 14px !important;
  }
  /* Modal cobrir quase ecrã todo */
  .modal-box {
    margin: 8px;
    padding: 16px 14px;
    max-height: calc(100vh - 16px);
    border-radius: 12px;
  }
  /* Tabelas com scroll horizontal forçado */
  .table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  /* Botões — touch target 40px+ */
  .btn-sm {
    min-height: 32px;
    padding: 6px 10px;
  }
  .btn {
    min-height: 40px;
  }
  /* KPIs valor */
  .kpi-value {
    font-size: 22px;
  }
  /* Form controls maiores para touch */
  .form-control {
    font-size: 14px;
    padding: 9px 10px;
    min-height: 40px;
  }
  /* Tabs com scroll horizontal se demasiadas */
  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
  }
  .tabs .tab-btn {
    flex-shrink: 0;
    white-space: nowrap;
  }
  /* Spans que serviam como subtitle dentro de label devem quebrar linha */
  .card > div > span[title] {
    display: inline-block;
  }
  /* Headers com subtitle entre parêntesis: forçar wrap em linha nova */
  .card > div:first-child > span:not(.kpi-value) {
    display: inline;
  }
}

@media (max-width: 480px) {
  /* Grids 2x1 em mobile mas 1x1 em ecrãs muito pequenos só para hero */
  .kpi-hero-grid {
    grid-template-columns: 1fr !important;
  }
  /* KPI grid mantém 2x2 — funciona bem mesmo a 320px */
  .kpi-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 6px !important;
  }
  .kpi-card {
    padding: 10px !important;
  }
  /* Grids inline 4 cols stack vertical em ecrãs muito pequenos */
  [style*="grid-template-columns:repeat(4,1fr)"],
  [style*="grid-template-columns: repeat(4, 1fr)"] {
    grid-template-columns: 1fr 1fr !important;
  }
  .kpi-value {
    font-size: 18px;
  }
  .page-body {
    padding: 8px;
  }
  .top-bar {
    height: 48px;
    padding: 0 8px;
  }
  /* Esconde data no topo para libertar espaço */
  .topbar-date,
  .topbar-right .topbar-btn[title="Atualizar dados"] {
    display: none;
  }
  /* Reduzir font-display em valores muito grandes */
  [style*="font-size:38px"],
  [style*="font-size: 38px"],
  [style*="font-size:36px"],
  [style*="font-size: 36px"],
  [style*="font-size:34px"],
  [style*="font-size: 34px"],
  [style*="font-size:32px"],
  [style*="font-size: 32px"] {
    font-size: 22px !important;
    line-height: 1.1 !important;
  }
  [style*="font-size:30px"],
  [style*="font-size: 30px"],
  [style*="font-size:28px"],
  [style*="font-size: 28px"] {
    font-size: 20px !important;
    line-height: 1.1 !important;
  }
  [style*="font-size:24px"],
  [style*="font-size: 24px"],
  [style*="font-size:22px"],
  [style*="font-size: 22px"] {
    font-size: 18px !important;
  }
  /* Sidebar largura total mas não toca margens */
  .sidebar {
    width: 92vw;
  }
  /* Cards padding ainda mais reduzido */
  .card {
    padding: 10px 12px !important;
  }
  .card[style*="padding"] {
    padding: 10px 12px !important;
  }
  /* Sub-textos longos em headers — forçar wrap */
  .card .card-header,
  .card > div:first-child {
    flex-wrap: wrap;
  }
  /* Filter rows e form rows wrap melhor */
  [style*="display:flex"][style*="gap"]:not(.tabs) {
    flex-wrap: wrap;
  }
  /* Botões em filtros não devem encolher */
  .btn-sm {
    flex-shrink: 0;
    font-size: 11px;
  }
  /* Lucide icons SVG não deve crescer */
  svg {
    flex-shrink: 0;
  }
  /* Tabela texto reduzido */
  table {
    font-size: 11px;
  }
  th, td {
    padding: 5px 6px !important;
  }
  /* Form labels em inline para poupar espaço */
  .form-label {
    font-size: 10px;
  }
  /* Modal pequenos margins */
  .modal-box {
    margin: 4px;
    padding: 12px;
    max-height: calc(100vh - 8px);
  }
  /* Topbar buttons spacing */
  .topbar-right {
    gap: 6px;
  }
}

@media (max-width: 380px) {
  /* iPhone 12/13 mini, SE — ainda mais compacto */
  .page-body {
    padding: 6px;
  }
  .card {
    padding: 8px 10px !important;
    border-radius: 10px;
  }
  [style*="font-size:38px"],
  [style*="font-size: 38px"],
  [style*="font-size:36px"],
  [style*="font-size: 36px"],
  [style*="font-size:32px"],
  [style*="font-size: 32px"] {
    font-size: 20px !important;
  }
}

@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }


/* ── iPhone (app instalada, ecra inteiro) ─────────────────────────────
   Com viewport-fit=cover e a barra de estado translucida, o conteudo vai ate
   ao notch e a barra inferior: as margens seguras afastam-no. */
.top-bar {
  padding-top: env(safe-area-inset-top);
  height: calc(var(--topbar-height) + env(safe-area-inset-top));
}
.sidebar {
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}
.page-body {
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
}
@media (max-width: 768px) {
  .top-bar {
    height: calc(50px + env(safe-area-inset-top));
    padding-left: max(10px, env(safe-area-inset-left));
    padding-right: max(10px, env(safe-area-inset-right));
  }
  /* abaixo de 16px o Safari faz zoom ao escrever num campo */
  .form-control, input, select, textarea {
    font-size: 16px;
  }
  /* alvos de toque com pelo menos 40px */
  .btn-sm, .topbar-btn {
    min-height: 40px;
  }
}

@media (max-width: 480px) {
  .modal-box.dlg-box { margin: 16px; padding: 18px 16px 14px; }
  .dlg-footer .btn { flex: 1; justify-content: center; }
}
