:root {
  --bg: #f4f5f7;
  --bg-soft: #eef0f2;
  --card: #ffffff;
  --border: #e6e8ec;
  --border-soft: #eef0f3;
  --text: #1a1d23;
  --muted: #6b7280;
  --muted-soft: #94989f;
  --primary: #1c3552;
  --primary-light: #2f527d;
  --primary-dark: #10213a;
  --primary-soft: #e9eef6;
  --accent: #b8860b;
  --danger: #c0392b;
  --danger-soft: #fbeae8;
  --ok: #2f7a3d;
  --ok-soft: #e7f6e9;
  --warn: #a15c00;
  --warn-soft: #fdf2d0;
  --radius: 14px;
  --radius-sm: 9px;
  --radius-lg: 18px;
  --shadow-sm: 0 1px 2px rgba(20, 24, 30, 0.05), 0 1px 1px rgba(20, 24, 30, 0.03);
  --shadow-md: 0 4px 14px rgba(20, 24, 30, 0.07), 0 1px 3px rgba(20, 24, 30, 0.04);
  --shadow-lg: 0 12px 32px rgba(20, 24, 30, 0.1), 0 2px 6px rgba(20, 24, 30, 0.05);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  line-height: 1.45;
  overscroll-behavior-y: contain;
  -webkit-tap-highlight-color: transparent;
}

a, button, .btn, .nav-item, .nav-subitem, .bottom-nav-item { -webkit-tap-highlight-color: transparent; }

h1, h2, h3 { letter-spacing: -0.01em; }
h1 { font-size: 1.6rem; font-weight: 700; }
h2 { font-size: 1.12rem; font-weight: 600; }

a { color: var(--primary); text-decoration: none; transition: color 0.12s ease; }
a:hover { color: var(--primary-dark); text-decoration: underline; }

/* ---------- Topbar ---------- */
.topbar {
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  color: white;
  padding: 14px 26px;
  padding-top: calc(14px + env(safe-area-inset-top, 0px));
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  box-shadow: 0 2px 10px rgba(15, 30, 25, 0.18);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar .brand { display: flex; align-items: center; gap: 12px; font-weight: 700; font-size: 1.02rem; letter-spacing: -0.01em; }
.topbar .brand-logo { height: 20px; width: auto; display: block; }
.topbar .brand-sep { width: 1px; height: 18px; background: rgba(255,255,255,0.28); }
.topbar .brand-text { opacity: 0.92; font-weight: 600; }
.topbar .right { display: flex; align-items: center; gap: 16px; font-size: 0.88rem; opacity: 0.95; }
.topbar form { display: inline; }

.menu-toggle-btn {
  display: none; background: rgba(255,255,255,0.12); border: 1px solid rgba(255,255,255,0.22); color: #fff;
  width: 36px; height: 36px; border-radius: var(--radius-sm); align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0; padding: 0;
}
.menu-toggle-btn svg { width: 20px; height: 20px; }

.sidebar-backdrop {
  display: none; position: fixed; inset: 0; background: rgba(16, 20, 26, 0.5); z-index: 29;
  opacity: 0; transition: opacity 0.18s ease;
}
.sidebar-backdrop.open { display: block; opacity: 1; }
.topbar button.linklike {
  background: rgba(255,255,255,0.12); border: 1px solid rgba(255,255,255,0.22); color: #fff; cursor: pointer;
  font-size: 0.82rem; padding: 5px 12px; border-radius: 999px; text-decoration: none;
  transition: background 0.15s ease;
}
.topbar button.linklike:hover { background: rgba(255,255,255,0.22); text-decoration: none; }

.app-shell { display: flex; align-items: flex-start; min-height: calc(100vh - 55px); }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--card);
  border-right: 1px solid var(--border-soft);
  min-height: calc(100vh - 55px);
  padding: 20px 12px;
  position: sticky;
  top: 55px;
}

.sidebar-group-label {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted-soft);
  margin: 20px 12px 8px;
}
.sidebar-group-label:first-child { margin-top: 4px; }

.sidebar .nav-item {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.89rem;
  font-weight: 500;
  margin-bottom: 2px;
  transition: background 0.12s ease, color 0.12s ease;
}
.sidebar .nav-item svg, .sidebar .nav-subitem svg { color: var(--muted); transition: color 0.12s ease; }
.sidebar a.nav-item:hover { background: var(--bg-soft); text-decoration: none; }
.sidebar a.nav-item.active { background: var(--primary); color: white; box-shadow: var(--shadow-sm); }
.sidebar a.nav-item.active svg { color: white; }
.sidebar .nav-item svg, .sidebar .nav-subitem svg { width: 18px; height: 18px; flex-shrink: 0; }
.sidebar .nav-item .notif-dot { margin-left: auto; }
.sidebar .nav-subitem {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 7px 11px 7px 36px;
  border-radius: var(--radius-sm);
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 500;
  margin-bottom: 1px;
  transition: background 0.12s ease, color 0.12s ease;
}
.sidebar a.nav-subitem:hover { background: var(--bg-soft); text-decoration: none; color: var(--text); }
.sidebar a.nav-subitem.active { background: var(--primary); color: white; }
.sidebar a.nav-subitem.active svg { color: white; }

.sidebar .nav-group-toggle { cursor: pointer; user-select: none; color: var(--muted-soft); font-weight: 600; }
.sidebar .nav-group-toggle:hover { background: var(--bg-soft); color: var(--text); }
.sidebar .nav-group-toggle .chevron { margin-left: auto; width: 16px; height: 16px; flex-shrink: 0; color: var(--muted-soft); transition: transform 0.15s ease; }
.sidebar .nav-group-toggle.open .chevron { transform: rotate(90deg); }
.sidebar .nav-subgroup { overflow: hidden; max-height: 600px; transition: max-height 0.2s ease; }
.sidebar .nav-subgroup.collapsed { max-height: 0; }

.main-area { flex: 1; min-width: 0; }

.container { max-width: 1120px; margin: 0 auto; padding: 28px 28px 60px; }

/* ---------- Cards ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 20px 22px;
  margin-bottom: 18px;
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.15s ease;
}

.card h2, .card h3 { margin-top: 0; }
.card h2 { margin-bottom: 12px; }

.sig-pad-wrap {
  margin-top: 10px;
}
.sig-pad {
  display: block;
  width: 100%;
  max-width: 600px;
  height: 180px;
  background: #fff;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius);
  touch-action: none;
  cursor: crosshair;
}
.sig-pad-actions {
  margin-top: 8px;
}
.sig-signature-img {
  max-width: 400px;
  width: 100%;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  background: #fff;
  padding: 6px;
}

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

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -2px; }
table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border-soft); }
tr:last-child td { border-bottom: none; }
th { color: var(--muted-soft); font-weight: 600; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; }
td a { font-weight: 500; }

/* ---------- Badges ---------- */
.badge {
  display: inline-block;
  padding: 3px 11px;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}
.badge.novo { background: var(--danger-soft); color: var(--danger); }
.badge.atribuido { background: var(--warn-soft); color: var(--warn); }
.badge.em_curso { background: #dbeafe; color: #1e40af; }
.badge.resolvido { background: var(--ok-soft); color: var(--ok); }
.badge.simulado { background: var(--bg-soft); color: var(--muted); }
.badge.emitido { background: var(--ok-soft); color: var(--ok); }
.badge.prioridade-baixa { background: var(--ok-soft); color: var(--ok); }
.badge.prioridade-normal { background: var(--bg-soft); color: var(--muted); }
.badge.prioridade-urgente { background: var(--danger-soft); color: var(--danger); }
.badge.pendente { background: var(--warn-soft); color: var(--warn); }
.badge.concluida { background: var(--ok-soft); color: var(--ok); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-block;
  background: var(--primary);
  color: white;
  border: none;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  font-size: 0.89rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background 0.14s ease, box-shadow 0.14s ease, transform 0.08s ease;
}
.btn:hover { background: var(--primary-dark); text-decoration: none; box-shadow: var(--shadow-md); }
.btn:active { transform: translateY(1px); }
.btn.secondary { background: white; color: var(--primary); border: 1px solid var(--border); box-shadow: none; }
.btn.secondary:hover { background: var(--primary-soft); border-color: var(--primary-light); box-shadow: none; }
.btn.small { padding: 6px 12px; font-size: 0.8rem; }
.btn.danger { background: white; color: var(--danger); border: 1px solid var(--danger); box-shadow: none; }
.btn.danger:hover { background: var(--danger-soft); box-shadow: none; }

label { display: block; font-size: 0.83rem; font-weight: 600; color: var(--muted); margin-bottom: 5px; margin-top: 12px; }
input[type=text], input[type=email], input[type=password], input[type=month], input[type=date], input[type=number], textarea, select {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 0.92rem;
  font-family: inherit; background: var(--card); color: var(--text);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--primary-light); box-shadow: 0 0 0 3px var(--primary-soft);
}
textarea { min-height: 80px; resize: vertical; }

.muted { color: var(--muted); font-size: 0.88rem; }
.notice {
  background: var(--warn-soft); border: 1px solid #ecd48f; padding: 11px 15px; border-radius: var(--radius-sm);
  font-size: 0.85rem; margin-bottom: 16px; color: #6b4e00;
}
.error { color: var(--danger); font-size: 0.88rem; margin-top: 8px; }
.success { color: var(--ok); font-size: 0.88rem; margin-top: 8px; }

/* ---------- Faturas/transacoes agrupadas por ano ("pastas") ---------- */
.pasta-ano { border: 1px solid var(--border); border-radius: var(--radius-sm); margin-bottom: 10px; overflow: hidden; }
.pasta-ano + .pasta-ano { margin-top: 10px; }
.pasta-ano summary {
  cursor: pointer; list-style: none; padding: 10px 14px; font-weight: 600;
  background: var(--bg-soft); display: flex; align-items: center; gap: 8px;
}
.pasta-ano summary::-webkit-details-marker { display: none; }
.pasta-ano summary::before {
  content: '\25B8'; display: inline-block; transition: transform 0.15s ease; color: var(--muted);
}
.pasta-ano[open] summary::before { transform: rotate(90deg); }
.pasta-ano table { margin: 0; }
.pasta-ano table tr:first-child { border-top: 1px solid var(--border); }

/* ---------- Login ---------- */
.login-wrap {
  display: flex; align-items: center; justify-content: center; min-height: 100vh;
  background: radial-gradient(circle at 20% 10%, #e9eef6 0%, var(--bg) 55%);
}
.login-card { width: 360px; box-shadow: var(--shadow-lg); }
.login-logo { text-align: center; margin-bottom: 22px; }
.login-logo img { height: 32px; width: auto; }
.demo-accounts { font-size: 0.8rem; color: var(--muted); margin-top: 18px; line-height: 1.5; }

.comment { border-top: 1px solid var(--border-soft); padding: 12px 0; }
.comment .meta { font-size: 0.78rem; color: var(--muted); }

.notif-dot {
  background: var(--danger); color: white; border-radius: 999px; font-size: 0.7rem;
  padding: 1px 7px; margin-left: 4px; font-weight: 600;
}

/* ---------- Barra de navegacao inferior (so mobile) ---------- */
.bottom-nav { display: none; }

/* ---------- Secoes recolhiveis dentro de formularios ---------- */
.form-section { border: 1px solid var(--border-soft); border-radius: var(--radius-sm); margin-top: 16px; overflow: hidden; }
.form-section summary {
  cursor: pointer; list-style: none; padding: 10px 14px; font-weight: 600; font-size: 0.92rem;
  background: var(--bg-soft); display: flex; align-items: center; gap: 8px;
}
.form-section summary::-webkit-details-marker { display: none; }
.form-section summary::before {
  content: '\25B8'; display: inline-block; transition: transform 0.15s ease; color: var(--muted);
}
.form-section[open] summary::before { transform: rotate(90deg); }
.form-section .form-section-body { padding: 4px 14px 14px; }

/* ---------- Botao a carregar (feedback de submissao) ---------- */
.btn.is-loading { opacity: 0.75; cursor: default; pointer-events: none; }

/* ---------- Scrollbar polish (webkit) ---------- */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #d4d7dc; border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: #bfc3ca; }

@media (max-width: 760px) {
  .menu-toggle-btn { display: flex; }
  .topbar {
    padding: 12px 14px;
    padding-top: calc(12px + env(safe-area-inset-top, 0px));
  }
  .topbar .brand-text { display: none; }
  .topbar .brand-sep { display: none; }
  .topbar .right { gap: 10px; font-size: 0.82rem; }

  .app-shell { display: block; }
  .main-area { width: 100%; }

  /* O menu lateral passa a gaveta: fica escondido fora do ecra e desliza
     para dentro quando se toca no botao hamburguer (ver footer.ejs). Comeca
     abaixo da barra de estado do telemovel (notch/relogio), para nao ficar
     tapada nem impossivel de tocar quando a app corre instalada em ecra inteiro. */
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 30; width: 268px; max-width: 82vw;
    transform: translateX(-100%); transition: transform 0.2s ease; box-shadow: var(--shadow-lg);
    overflow-y: auto; padding-top: calc(64px + env(safe-area-inset-top, 0px));
  }
  .sidebar.open { transform: translateX(0); }

  .container { padding: 16px 14px calc(84px + env(safe-area-inset-bottom, 0px)); }
  h1 { font-size: 1.32rem; }

  .grid.cols-2 { grid-template-columns: 1fr; }
  .card { padding: 16px; }

  /* Cartoes de estatistica do painel: grelha compacta 2x2, tudo visivel
     de uma vez, sem ser preciso deslizar para o lado. */
  .grid.cols-2.stat-strip {
    display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 18px;
  }
  .grid.cols-2.stat-strip .card {
    padding: 12px 13px; margin-bottom: 0;
  }
  .grid.cols-2.stat-strip .card h3 { font-size: 0.72rem; }
  .grid.cols-2.stat-strip .card p { font-size: 1.15rem !important; margin: 3px 0 !important; }
  .grid.cols-2.stat-strip .card p.muted { font-size: 0.72rem !important; }

  /* Tabelas: nao espremer colunas -- mostra tudo a tamanho legivel e
     desliza-se horizontalmente dentro da faixa .table-scroll. */
  .table-scroll table { min-width: 540px; }
  th, td { padding: 9px 10px; font-size: 0.86rem; }

  /* Impede o zoom automatico do iOS ao tocar num campo (exige >=16px). */
  input[type=text], input[type=email], input[type=password], input[type=month],
  input[type=date], input[type=number], textarea, select { font-size: 16px; }

  .btn { padding: 11px 16px; }
  .sidebar a.nav-item, .sidebar .nav-subitem { padding-top: 10px; padding-bottom: 10px; }

  /* Feedback de toque, para parecer mais uma app nativa e menos uma pagina web. */
  .btn:active, .bottom-nav-item:active, .sidebar a.nav-item:active, .sidebar a.nav-subitem:active {
    transform: scale(0.97); transition: transform 0.08s ease;
  }

  /* ---------- Barra de navegacao inferior ---------- */
  .bottom-nav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
    background: var(--card); border-top: 1px solid var(--border-soft);
    box-shadow: 0 -2px 12px rgba(20, 24, 30, 0.06);
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom, 0px));
  }
  .bottom-nav-item {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 1px;
    background: none; border: none; color: var(--muted); font-size: 0.62rem; font-weight: 600;
    padding: 4px 2px; cursor: pointer; font-family: inherit;
  }
  .bottom-nav-item svg { width: 17px; height: 17px; stroke-width: 1.8; flex-shrink: 0; }
  .bottom-nav-item.active { color: var(--primary); }
  .bottom-nav-item:hover { text-decoration: none; color: var(--primary); }
}
