:root {
  --navy-900: #141f33;
  --navy-800: #1b2942;
  --navy-700: #24354f;
  --navy-600: #2f4260;
  --slate-header: #3e5871;
  --bg: #eef1f5;
  --panel: #ffffff;
  --border: #dde3ea;
  --text: #1f2937;
  --muted: #6b7280;
  --teal: #12a596;
  --teal-dark: #0e8578;
  --teal-bg: #e3f7f4;
  --red: #e5534b;
  --red-dark: #c73a32;
  --red-bg: #fdeceb;
  --radius: 8px;
  --radius-sm: 6px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

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

.icon { width: 18px; height: 18px; flex-shrink: 0; }
.icon-sm { width: 14px; height: 14px; }

/* ---------- layout ---------- */

.topbar {
  height: 60px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 700;
  color: var(--teal-dark);
}
.topbar .brand .icon { width: 24px; height: 24px; }
.topbar .brand .brand-logo { max-height: 36px; max-width: 200px; display: block; }

.topbar .menu-toggle {
  background: none; border: none; color: var(--navy-800); cursor: pointer; padding: 6px; display: none;
}

.topbar .user-area { display: flex; align-items: center; gap: 12px; }
.topbar .user-avatar {
  width: 36px; height: 36px; border-radius: 50%; background: var(--bg);
  display: flex; align-items: center; justify-content: center; color: var(--muted);
}
.topbar .user-info { line-height: 1.3; text-align: right; }
.topbar .user-info .name { font-weight: 600; font-size: 13px; }
.topbar .user-info .email { font-size: 11.5px; color: var(--muted); }
.topbar .logout-btn {
  width: 34px; height: 34px; border-radius: 50%; border: none; background: var(--red-bg); color: var(--red);
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.topbar .logout-btn:hover { background: var(--red); color: white; }

.app-body { display: flex; align-items: flex-start; }

.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--navy-900);
  color: #cbd5e1;
  padding: 14px 0;
  position: sticky;
  top: 60px;
  height: calc(100vh - 60px);
  overflow-y: auto;
}

.sidebar a.nav-link {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 20px; color: #d7dee8; font-size: 13.5px;
}
.sidebar a.nav-link:hover { background: var(--navy-700); text-decoration: none; }
.sidebar a.nav-link.active { background: var(--teal); color: white; }

.sidebar details { border-bottom: 1px solid rgba(255,255,255,0.06); }
.sidebar summary {
  list-style: none;
  cursor: pointer;
  display: flex; align-items: center; gap: 10px;
  padding: 11px 20px;
  font-size: 13px;
  font-weight: 600;
  color: #aeb9c9;
  text-transform: uppercase;
  letter-spacing: .03em;
}
.sidebar summary::-webkit-details-marker { display: none; }
.sidebar summary:hover { color: white; }
.sidebar summary .chev { margin-left: auto; transition: transform .15s; }
.sidebar details[open] summary .chev { transform: rotate(180deg); }
.sidebar details[open] summary { color: white; }
.sidebar .group-items a.nav-link { padding-left: 46px; font-size: 13px; }

.main {
  flex: 1;
  min-width: 0;
  padding: 26px 32px;
}

.breadcrumb { font-size: 12.5px; color: var(--muted); margin-bottom: 6px; }
.breadcrumb a { color: var(--muted); }
.breadcrumb .sep { margin: 0 6px; }
.breadcrumb .current { color: var(--text); }

.page-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 20px; flex-wrap: wrap; gap: 12px; }
.page-head h2 { margin: 0 0 4px; font-size: 22px; }
.page-head .subtitle { color: var(--muted); font-size: 13px; margin: 0; }

/* Cabeçalho da listagem (título + botões + barra de exportação) fixo ao
   fazer scroll, com deslocação horizontal própria para os botões (Novo
   registo, exportar, etc.) ficarem sempre alcançáveis mesmo sem espaço. */
.list-toolbar-sticky {
  position: sticky;
  top: 60px;
  z-index: 15;
  background: var(--bg);
  overflow-x: auto;
  overflow-y: hidden;
  padding-top: 4px;
  padding-bottom: 8px;
}
.list-toolbar-sticky .page-head,
.list-toolbar-sticky .search-toolbar {
  flex-wrap: nowrap;
  width: max-content;
  min-width: 100%;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

/* ---------- buttons ---------- */

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 16px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}
.btn:hover { background: #f1f5f9; text-decoration: none; }
.btn.primary { background: var(--teal); border-color: var(--teal); color: white; }
.btn.primary:hover { background: var(--teal-dark); }
.btn.danger { background: var(--red); border-color: var(--red); color: white; }
.btn.danger:hover { background: var(--red-dark); }
.btn.outline { background: white; }
.btn.small { padding: 5px 12px; font-size: 12px; border-radius: 16px; }
.btn.icon-only { padding: 6px; border-radius: 50%; }

.row-actions { display: flex; gap: 6px; flex-wrap: nowrap; justify-content: center; }
.row-actions .btn.edit { background: var(--teal); border-color: var(--teal); color: white; }
.row-actions .btn.edit:hover { background: var(--teal-dark); }
.row-actions .btn.delete { background: var(--red); border-color: var(--red); color: white; }
.row-actions .btn.delete:hover { background: var(--red-dark); }
.row-actions .btn.ghost { background: white; color: var(--navy-700); }

/* ---------- table ---------- */

/* Barra de deslocação horizontal duplicada por cima da tabela — a nativa do
   .table-wrap só aparece no fundo, que numa lista longa fica bem fora da
   vista. Esta fica logo a seguir ao cabeçalho fixo, sincronizada com o
   scroll da tabela (ver app.js). Fica também fixa (position:sticky) ao
   deslocar os registos para baixo, tal como o cabeçalho da tabela — os
   valores de "top" (--sticky-*) são calculados em runtime pelo app.js,
   porque a altura do cabeçalho da listagem varia consoante o conteúdo. */
.table-scroll-top {
  overflow-x: auto;
  overflow-y: hidden;
  height: 14px;
  margin-bottom: 4px;
  position: sticky;
  top: var(--sticky-scrollbar-top, 60px);
  z-index: 14;
  background: var(--bg);
}
.table-scroll-top-inner { height: 1px; }

/* overflow-x:auto por si só faz o browser tratar overflow-y como "auto"
   também (regra do CSS Overflow) — mas como o scroll vertical continua a
   ser feito na página (não aqui), a posição sticky do cabeçalho da tabela
   nunca "prendia" nada. A correção: dar mesmo um max-height e overflow
   verticais a sério, para o cabeçalho ficar sticky *dentro* desta caixa
   (que ocupa o espaço restante do ecrã, calculado em runtime pelo app.js). */
.table-wrap {
  overflow: auto;
  max-height: var(--table-wrap-max-height, 60vh);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

table.data { width: 100%; border-collapse: collapse; background: var(--panel); font-size: 13px; }
table.data thead th {
  background: var(--slate-header);
  color: white;
  font-weight: 600;
  text-align: center;
  padding: 12px 10px;
  white-space: nowrap;
}
table.data thead th a { color: white; }
table.data thead th a:hover { text-decoration: none; opacity: .85; }

table.data .filter-row td { background: #f7f9fb; padding: 6px 8px; border-bottom: 1px solid var(--border); }
table.data.resizable .filter-row td {
  position: sticky;
  top: var(--sticky-filterrow-top, 41px);
  z-index: 11;
}
table.data .filter-row input {
  width: 100%; padding: 6px 8px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 12.5px;
}

table.data tbody td {
  border-bottom: 1px solid var(--border);
  padding: 10px;
  text-align: center;
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
table.data tbody tr:nth-child(even) { background: #f7fafc; }
table.data tbody tr:hover { background: #eef6f5; }

/* ---------- colunas redimensionáveis ---------- */

table.data.resizable { table-layout: fixed; }
table.data.resizable th { overflow: hidden; text-overflow: ellipsis; }
table.data.resizable th.resizable-col {
  position: sticky;
  top: 0;
  z-index: 12;
  padding-right: 16px;
}
table.data.resizable thead th:last-child {
  position: sticky;
  top: 0;
  z-index: 12;
}
table.data.resizable td { overflow: hidden; text-overflow: ellipsis; max-width: none; }

.col-resizer {
  position: absolute;
  top: 0;
  right: 0;
  width: 8px;
  height: 100%;
  cursor: col-resize;
  user-select: none;
  touch-action: none;
}
.col-resizer:hover, .col-resizer.active { background: rgba(255, 255, 255, 0.35); }
body.col-resizing { cursor: col-resize; user-select: none; }

/* ---------- coluna "Ações" fixa à direita ----------
   Fica sempre visível ("congelada") mesmo com scroll horizontal, tal como
   o cabeçalho fica fixo no topo — os botões nunca ficam cortados nem exigem
   deslocar a tabela para lá chegar. */
table.data.resizable td.actions-col,
table.data.resizable th.actions-col {
  position: sticky;
  right: 0;
}
/* Célula dos botões (corpo): opaca, com uma sombra à esquerda a separar
   visualmente da tabela quando se faz scroll por baixo. */
table.data.resizable tbody td.actions-col {
  z-index: 3;
  background: var(--panel);
  box-shadow: -6px 0 8px -7px rgba(0, 0, 0, 0.28);
}
table.data.resizable tbody tr:nth-child(even) td.actions-col { background: #f7fafc; }
table.data.resizable tbody tr:hover td.actions-col { background: #eef6f5; }
/* Cantos (cabeçalho e linha de filtros): fixos nos dois eixos, por cima de tudo. */
table.data.resizable thead th.actions-col { z-index: 15; }
table.data.resizable .filter-row td.actions-col { z-index: 13; background: #f7f9fb; }

.pw-cell { display: inline-flex; align-items: center; gap: 6px; }
.pw-cell .reveal-btn { font-size: 11px; padding: 3px 9px; }

/* ---------- edição em linha ---------- */

.inline-input {
  width: 100%;
  min-width: 90px;
  padding: 5px 6px;
  border: 1px solid transparent;
  background: transparent;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: inherit;
  color: var(--text);
  text-align: center;
}
.inline-input:hover { border-color: var(--border); background: white; }
.inline-input:focus {
  outline: none; border-color: var(--teal); background: white;
  box-shadow: 0 0 0 3px var(--teal-bg);
}
.print-only { display: none; }

/* ---------- combobox pesquisável ---------- */

.combo { position: relative; width: 100%; }
.combo .combo-input { width: 100%; text-align: left; }
.combo-list {
  display: none;
  position: absolute;
  top: 100%; left: 0; right: 0;
  z-index: 40;
  max-height: 260px;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 10px 30px rgba(0,0,0,0.15);
  margin-top: 2px;
  text-align: left;
}
.combo-item {
  padding: 7px 10px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.combo-item:hover, .combo-item.active { background: var(--teal-bg); }
.combo-more { padding: 6px 10px; font-size: 11.5px; color: var(--muted); font-style: italic; }

/* ---------- pagination ---------- */

.pagination { display: flex; gap: 6px; margin-top: 16px; align-items: center; justify-content: center; flex-wrap: wrap; }
.pagination a {
  min-width: 34px; height: 34px; padding: 0 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: white; color: var(--text);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600;
}
.pagination a:hover { background: #f1f5f9; text-decoration: none; }
.pagination a.active { background: var(--navy-800); border-color: var(--navy-800); color: white; }

/* ---------- forms ---------- */

form.record-form { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px 24px; }
form.record-form .field { display: flex; flex-direction: column; gap: 4px; }
form.record-form .field.wide { grid-column: 1 / -1; }
form.record-form label { font-weight: 600; font-size: 12.5px; color: var(--muted); }
form.record-form input, form.record-form textarea, form.record-form select {
  padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 13px; font-family: inherit;
}
form.record-form input:focus, form.record-form textarea:focus, form.record-form select:focus {
  outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px var(--teal-bg);
}
form.record-form textarea { min-height: 70px; resize: vertical; }
form.record-form .pw-wrap { position: relative; display: flex; gap: 6px; }
form.record-form .pw-wrap input { flex: 1; }

.form-actions { grid-column: 1 / -1; display: flex; gap: 10px; margin-top: 8px; }

.badge { display: inline-block; padding: 3px 10px; border-radius: 12px; font-size: 11px; background: var(--teal-bg); color: var(--teal-dark); font-weight: 600; }

.search-toolbar { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 12px; margin-bottom: 16px; }
.export-bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.export-bar label { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 4px; }

/* ---------- alteração em massa ---------- */
.bulk-bar { margin-bottom: 12px; border: 1px solid var(--border); border-radius: var(--radius); background: #fbfcfe; }
.bulk-bar > summary {
  cursor: pointer; padding: 8px 12px; font-size: 12.5px; font-weight: 600; color: var(--navy-700);
  display: flex; align-items: center; gap: 6px; list-style: none;
}
.bulk-bar > summary::-webkit-details-marker { display: none; }
.bulk-bar[open] > summary { border-bottom: 1px solid var(--border); }
.bulk-bar form { display: flex; align-items: flex-end; gap: 10px; flex-wrap: wrap; padding: 12px; }
.bulk-bar form label { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 3px; }
.bulk-bar select, .bulk-bar input[type="text"], .bulk-bar input[type="password"],
.bulk-bar input[type="number"], .bulk-bar input[type="date"], .bulk-bar input[type="datetime-local"] {
  padding: 6px 8px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 13px; min-width: 160px;
}
.bulk-bar .combo { min-width: 220px; }
.bulk-bar .subtitle { padding: 0 12px 12px; }

/* ---------- print ---------- */

.print-page { max-width: 800px; margin: 0 auto; }
.print-page table { width: 100%; border-collapse: collapse; }
.print-page td { padding: 8px; border-bottom: 1px solid #ddd; vertical-align: top; }
.print-page td.k { font-weight: bold; width: 220px; color: #444; }
.print-toolbar { margin-bottom: 20px; }

@media print {
  .no-print { display: none !important; }
  .topbar, .sidebar { display: none !important; }
  .main { padding: 0; }
  .print-only { display: inline; }
  /* A listagem normal (não a página dedicada print.php) não deve cortar a
     tabela pelo que estava visível no ecrã — imprime sempre tudo. */
  .table-wrap { max-height: none !important; overflow: visible !important; }
  .list-toolbar-sticky, .table-scroll-top { position: static !important; }
}

/* ---------- login ---------- */

.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(160deg, var(--navy-900), var(--navy-700));
}
.login-box {
  background: var(--panel); border-radius: var(--radius);
  padding: 34px; width: 340px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.25);
}
.login-box .brand { display: flex; align-items: center; gap: 8px; color: var(--teal-dark); font-size: 19px; font-weight: 700; margin-bottom: 18px; }
.login-box input {
  width: 100%; padding: 10px 12px; margin-bottom: 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 13px;
}
.login-box input:focus { outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px var(--teal-bg); }
.error-msg { color: var(--red); font-size: 13px; margin-bottom: 10px; }
.hint { color: var(--muted); font-size: 12px; margin-top: 10px; }

.empty-state { color: var(--muted); padding: 40px; text-align: center; }

@media (max-width: 860px) {
  .sidebar { position: fixed; left: -260px; top: 60px; bottom: 0; transition: left .18s; z-index: 30; }
  .sidebar.open { left: 0; }
  .topbar .menu-toggle { display: flex; }
  form.record-form { grid-template-columns: 1fr; }
}
