/* PCivil v4 — folha de estilo única (app + login). */

:root {
  --navy: #102136;
  --navy-2: #18304c;
  --red: #cf1d28;
  --red-dark: #bd1721;
  --red-light: #df2530;
  --ink: #172235;
  --muted: #6a788c;
  --line: #dae2eb;
  --line-soft: #edf1f5;
  --bg: #f4f7fa;
  --sidebar: 246px;
  --radius: 12px;
}

* { box-sizing: border-box; }

/* As regras de layout abaixo usam display:flex/grid e sobrepor-se-iam ao
   atributo hidden, por isso este tem de vencer explicitamente. */
[hidden] { display: none !important; }

body {
  margin: 0;
  display: flex;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 "Segoe UI", system-ui, Arial, sans-serif;
}

/* ------------------------------------------------------------ barra lateral */

aside {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--sidebar);
  display: flex;
  flex-direction: column;
  background: var(--navy);
  color: #fff;
}

.brand { padding: 24px 20px; border-bottom: 1px solid #ffffff1f; }
.brand b { display: block; font-size: 28px; letter-spacing: .3px; }
.brand small { color: #b5c1d1; }

nav { flex: 1; padding: 14px 10px; overflow-y: auto; }

.sep {
  margin: 0;
  padding: 10px;
  color: #8495ac;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: .8px;
  text-transform: uppercase;
}

nav button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  margin: 3px 0;
  padding: 11px;
  text-align: left;
  color: #d9e2ed;
  background: transparent;
  border: 0;
  border-radius: 9px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

nav button:hover { background: var(--navy-2); color: #fff; }
nav button.active { background: var(--red); color: #fff; }

nav button em {
  padding: 1px 7px;
  border-radius: 999px;
  background: #ffffff22;
  font-size: 11px;
  font-style: normal;
}

.side-foot {
  padding: 14px 16px;
  border-top: 1px solid #ffffff1f;
  font-size: 12px;
  color: #b5c1d1;
}

.admin-entry {
  display: block;
  width: 100%;
  margin-bottom: 12px;
  padding: 9px;
  border: 1px solid #ffffff33;
  border-radius: 9px;
  background: transparent;
  color: #d9e2ed;
  font-size: 12.5px;
}
.admin-entry:hover { background: var(--navy-2); color: #fff; border-color: #ffffff55; }
.admin-entry.active { background: var(--red); border-color: var(--red); color: #fff; }

.who b { display: block; color: #fff; font-size: 13px; }
.who small { color: #8495ac; }
.who-actions { display: flex; gap: 12px; margin: 8px 0 6px; }
.side-foot .link { color: #d9e2ed; padding: 0; }
.side-foot .link:hover { color: #fff; }
.db-state { display: block; color: #7d8ea6; font-size: 11px; }

/* -------------------------------------------------------------- área central */

main {
  width: calc(100% - var(--sidebar));
  margin-left: var(--sidebar);
  padding: 28px;
}

main > header {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: space-between;
  align-items: center;
  margin: -28px -28px 22px;
  padding: 16px 28px;
  background: linear-gradient(90deg, var(--red-dark), var(--red-light));
  color: #fff;
}

.head-title small { opacity: .85; font-size: 12px; }
.head-title h1 { margin: 2px 0 0; font-size: 30px; }
.head-actions { display: flex; flex-wrap: wrap; gap: 8px; }

button {
  padding: 9px 13px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

button:hover { border-color: #b9c6d6; }
button:disabled { opacity: .55; cursor: default; }

.primary { background: var(--red); color: #fff; border-color: var(--red); }
.primary:hover { background: var(--red-dark); border-color: var(--red-dark); }

button.link {
  padding: 3px 6px;
  border: 0;
  background: none;
  color: var(--red);
  font-weight: 700;
}
button.link:hover { text-decoration: underline; }
button.link.danger { color: #94202a; }

#msg { padding: 11px 14px; margin-bottom: 12px; border-radius: 9px; font-weight: 600; }
.ok { background: #e9f8ef; color: #14532d; }
.error { background: #ffe9eb; color: #8a1520; }

/* ------------------------------------------------------------------- KPIs -- */

.kpis {
  display: grid;
  /* Ajusta-se à largura: os 5 indicadores cabem numa linha até ~650 px. */
  grid-template-columns: repeat(auto-fit, minmax(118px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.kpis article {
  padding: 15px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.kpis b { display: block; font-size: 26px; line-height: 1.1; }
.kpis span {
  color: var(--muted);
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: .5px;
  text-transform: uppercase;
}

/* ---------------------------------------------------------------- filtros -- */

.filters {
  display: grid;
  /* Reorganiza-se sozinho conforme a largura, em vez de colunas fixas que
     partiam para 3 colunas desalinhadas em janelas médias. */
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 9px;
  align-items: end;
  padding: 13px;
  margin-bottom: 14px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.filters #f-q { grid-column: span 2; }

.filters .date {
  display: flex;
  flex-direction: column;
  gap: 3px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}

input, select, textarea {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}

input:focus, select:focus, textarea:focus {
  outline: 2px solid #cf1d2855;
  border-color: var(--red);
}

/* ---------------------------------------------------------------- tabela --- */

/* A tabela desloca-se por dentro, com a altura da janela. Sem isto, com a
   janela estreita as colunas da direita ficavam escondidas e a barra de
   deslocamento horizontal só aparecia depois da última linha (milhares de
   píxeis abaixo). Assim a barra fica sempre à vista e o cabeçalho fixo. */
.table-wrap {
  overflow: auto;
  max-height: calc(100vh - 24px);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

table { width: 100%; min-width: 1240px; border-collapse: collapse; }

th, td {
  padding: 10px 11px;
  border-bottom: 1px solid var(--line-soft);
  text-align: left;
  vertical-align: top;
}

th {
  position: sticky;
  top: 0;
  background: #eef2f6;
  color: var(--muted);
  font-size: 10.5px;
  letter-spacing: .5px;
  text-transform: uppercase;
  white-space: nowrap;
}

tbody tr:hover { background: #fff4f5; }

.col-edoc { white-space: nowrap; font-weight: 700; }

/* O número EDOC é um botão que abre o registo, mas deve ler-se como ligação. */
.edoc-link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--red);
  font: inherit;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
}
.edoc-link:hover { text-decoration: underline; }

.col-edoc .portal {
  margin-left: 4px;
  color: var(--muted);
  font-size: 13px;
  text-decoration: none;
}
.col-edoc .portal:hover { color: var(--red); }
.col-date { white-space: nowrap; }
.col-subject { min-width: 280px; max-width: 420px; }
.col-actions { white-space: nowrap; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  background: #eef2f6;
  color: #41506a;
  font-size: 11.5px;
  font-weight: 700;
  white-space: nowrap;
}

.s-pendente     { background: #fff3d6; color: #7a4c00; }
.s-emanalise    { background: #e4edff; color: #234a9c; }
.s-encaminhado  { background: #e2f0ff; color: #10559a; }
.s-suspenso     { background: #f1e6ff; color: #5b2a94; }
.s-terminado    { background: #e3f7e9; color: #14653a; }
.s-bloqueado    { background: #ffe1e4; color: #9b1622; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; align-items: center; }
.toolbar input[type="search"] { flex: 1; min-width: 220px; }

/* ------------------------------------------------- histórico de alterações */

.muted { color: var(--muted); font-size: 12px; }
.tabela-audit { min-width: 760px; }
.tabela-audit td { font-size: 13px; }
.changes { margin: 0; padding-left: 16px; }
.changes li { margin: 1px 0; overflow-wrap: anywhere; }
.changes del { color: #9b1622; background: #ffe9eb; text-decoration: line-through; padding: 0 3px; border-radius: 3px; }
.changes ins { color: #14653a; background: #e3f7e9; text-decoration: none; padding: 0 3px; border-radius: 3px; }

details.history { margin-top: 16px; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
details.history summary { cursor: pointer; font-weight: 700; color: var(--muted); }
details.history[open] summary { margin-bottom: 10px; }
details.history .table-wrap { max-height: 300px; }

input.input-error { border-color: var(--red); background: #fff5f6; outline-color: #cf1d2866; }

.edoc-warn {
  margin: 2px 0 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: #ffe9eb;
  color: #8a1520;
  font-size: 12.5px;
  font-weight: 600;
  line-height: 1.35;
}

.form-error {
  margin: 14px 0 0;
  padding: 10px 12px;
  border-radius: 9px;
  background: #ffe9eb;
  color: #8a1520;
  font-weight: 600;
}

/* A tabela de utilizadores é bem mais estreita do que a dos registos. */
.tabela-users { min-width: 900px; }
.tabela-users tbody tr.inativo { opacity: .55; }
.tabela-users .eu { color: var(--muted); font-style: normal; font-size: 12px; }

.count { margin: 10px 2px; color: var(--muted); font-size: 12px; }
.count code {
  padding: 1px 6px;
  border-radius: 5px;
  background: #eef2f6;
  color: var(--ink);
  font-weight: 700;
}
.empty, .loading { padding: 48px; text-align: center; color: var(--muted); }

/* -------------------------------------------------------------- diálogos --- */

dialog {
  width: min(880px, 95vw);
  padding: 24px;
  border: 0;
  border-radius: 15px;
  color: var(--ink);
}

dialog::backdrop { background: #0d1b2ba8; }
dialog h2 { margin: 0 0 16px; }

.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.fields.one { grid-template-columns: 1fr; }
.fields .wide { grid-column: 1 / -1; }

label { display: flex; flex-direction: column; gap: 5px; }
label > span { font-size: 12px; font-weight: 700; color: var(--muted); }

textarea { min-height: 76px; resize: vertical; }

.hint { color: var(--muted); font-size: 12px; }

dialog footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

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

.login-page {
  display: grid;
  place-items: center;
  background: linear-gradient(160deg, var(--navy), #0a1624 60%, #2a0d11);
}

.login-box {
  width: min(400px, 92vw);
  padding: 30px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 22px 60px #0000004d;
}

.login-brand { margin-bottom: 22px; }
.login-brand b { display: block; font-size: 30px; color: var(--red); }
.login-brand small { color: var(--muted); }

.login-box h1 { margin: 0 0 16px; font-size: 20px; }
.login-box form { display: flex; flex-direction: column; gap: 13px; }
.login-box button { margin-top: 4px; padding: 11px; }
#login-msg { margin: 16px 0 0; padding: 11px 14px; border-radius: 9px; font-weight: 600; }

/* --------------------------------------------------------------- impressão - */

@media print {
  aside, main > header .head-actions, .filters, .col-actions, .count, #msg { display: none !important; }
  body { display: block; background: #fff; }
  main { width: 100%; margin: 0; padding: 0; }
  main > header { margin: 0 0 12px; padding: 0; background: none; color: #000; }
  .table-wrap { overflow: visible; max-height: none; border: 0; }
  table { min-width: 0; font-size: 10px; }
  th { position: static; }
}

/* ------------------------------------------------------------- responsivo -- */

/* Janelas médias (portátil, meio ecrã): barra lateral e margens mais estreitas
   para dar à tabela o máximo de largura. */
@media (max-width: 1200px) {
  :root { --sidebar: 200px; }
  main { padding: 20px; }
  main > header { margin: -20px -20px 16px; padding: 14px 20px; }
  .head-title h1 { font-size: 26px; }
  .brand { padding: 20px 16px; }
  .brand b { font-size: 24px; }
  .kpis article { padding: 11px 13px; }
  .kpis b { font-size: 22px; }
  .col-subject { min-width: 220px; }
}

@media (max-width: 800px) {
  body { display: block; }
  aside { position: static; width: 100%; }
  main { width: 100%; margin: 0; padding: 18px; }
  main > header { margin: -18px -18px 16px; padding: 14px 18px; }
  .kpis, .filters, .fields { grid-template-columns: 1fr 1fr; }
  .fields { grid-template-columns: 1fr; }
}
