/* =============================================================
   Gestão de Sócios — direção visual "Terminal Nocturno"
   -------------------------------------------------------------
   Escuro por omissão (a identidade da app). Fundo quase preto
   azulado, um único acento ciano usado com parcimónia, números
   e identificadores em tipo monospaçado. Continua a ser uma
   ferramenta de trabalho séria — só que noturna.

   Temas (alternador na barra de topo: sistema → claro → escuro):
   - :root                          → escuro (identidade, também no modo "sistema")
   - [data-theme="light"]           → variante "dia" (opção explícita do utilizador)
   - [data-theme="dark"]            → escuro (opção explícita)

   --accent  = ciano decorativo (foco, separador ativo, ações primárias)
   --danger / --stamp = negativos (cotas em atraso, saídas, erros, remoções)
   ============================================================= */

:root {
  color-scheme: dark;

  --bg: #0b0e14;
  --surface: #121722;
  --surface-2: #1a2130;
  --field: #0e141e;
  --line: #1e2733;
  --line-strong: #2c3849;
  --ink: #e4e9f0;
  --ink-soft: #9aa7b8;
  --ink-faint: #69748a;
  --zebra: #0f141d;
  --hover: #172234;

  --accent: #38bdf8;
  --accent-on: #04121b;
  --accent-hover: #6bccf8;
  --accent-glow: rgba(56, 189, 248, 0.35);

  --topbar: #0a0f18;
  --topbar-ink: #cdd7e3;

  --ok: #4ade80;
  --warn: #fbbf24;
  --danger: #f2555f;
  --danger-on: #1a0508;
  --danger-hover: #ff6b74;
  --stamp: #f2555f;

  --focus: #38bdf8;
  --shadow: 0 24px 48px -28px rgba(0, 0, 0, 0.75);
  --radius: 6px;

  --font-ui: 'Inter', -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "JetBrains Mono", "Source Code Pro",
               Menlo, Consolas, "DejaVu Sans Mono", monospace;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #eceff4;
  --surface: #ffffff;
  --surface-2: #e3e8f0;
  --field: #ffffff;
  --line: #d4dbe4;
  --line-strong: #b7c1ce;
  --ink: #10151d;
  --ink-soft: #47525f;
  --ink-faint: #7a8592;
  --zebra: #f3f5f9;
  --hover: #e7ecf3;
  --accent: #0a6ebd;
  --accent-on: #ffffff;
  --accent-hover: #085a9c;
  --accent-glow: rgba(10, 110, 189, 0.25);
  --topbar: #0a0f18;
  --topbar-ink: #cdd7e3;
  --ok: #1f7a3d;
  --warn: #96650a;
  --danger: #c62f3b;
  --danger-on: #ffffff;
  --danger-hover: #a5242f;
  --stamp: #c62f3b;
  --focus: #0a6ebd;
  --shadow: 0 18px 36px -26px rgba(16, 21, 29, 0.4);
}

:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
  font-family: var(--font-ui);
  font-weight: 700;
  letter-spacing: -0.01em;
}

.hidden { display: none !important; }

/* ---- Barra de topo (barra de comando) ------------------------------- */
.topbar {
  background: var(--topbar);
  color: var(--topbar-ink);
  padding: 11px 22px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  border-bottom: 1px solid #000;
  box-shadow: 0 1px 0 var(--accent-glow);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 1.05rem;
  letter-spacing: 0.01em;
}
.brand-badge {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  font-size: 1rem;
  border-radius: var(--radius);
  background: linear-gradient(150deg, var(--accent), color-mix(in srgb, var(--accent) 55%, #0b3350));
  box-shadow: 0 0 14px -2px var(--accent-glow);
}

.topbar-right { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.topbar-right .hint { color: rgba(205, 215, 227, 0.6); }
#utilizador-atual { font-family: var(--font-mono); font-size: 0.78rem; }

.topbar .btn.small {
  background: rgba(205, 215, 227, 0.08);
  border-color: rgba(205, 215, 227, 0.16);
  color: var(--topbar-ink);
}
.topbar .btn.small:hover {
  background: rgba(205, 215, 227, 0.16);
  border-color: rgba(205, 215, 227, 0.28);
}

/* Indicador de ligação ao WhatsApp */
.wa-indicador {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(205, 215, 227, 0.07);
  border: 1px solid rgba(205, 215, 227, 0.16);
  padding: 5px 11px;
  border-radius: 999px;
  font-family: var(--font-mono);
  font-size: 0.74rem;
}
.wa-indicador .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: #7d8794;
  transition: background 0.2s;
}
.wa-indicador.wa-ligado .dot { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.wa-indicador.wa-desligado .dot { background: var(--danger); }
.wa-indicador.wa-aguarda_qr .dot { background: var(--warn); box-shadow: 0 0 8px var(--warn); }
.wa-indicador.wa-a_iniciar .dot { background: var(--warn); }
.wa-indicador.wa-erro .dot { background: var(--danger); box-shadow: 0 0 8px var(--danger); }

/* ---- Ecrã de início de sessão ------------------------------------- */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background:
    radial-gradient(720px 360px at 50% -12%, var(--accent-glow), transparent 70%),
    linear-gradient(rgba(120, 160, 200, 0.05) 1px, transparent 1px) 0 0 / 100% 32px,
    linear-gradient(90deg, rgba(120, 160, 200, 0.05) 1px, transparent 1px) 0 0 / 32px 100%,
    var(--bg);
}
.login-box {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-top: 2px solid var(--accent);
  border-radius: var(--radius);
  padding: 30px 28px;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: var(--shadow);
}
.login-box .brand { color: var(--ink); font-size: 1.2rem; justify-content: center; }
.login-box .hint { text-align: center; margin: 0; font-family: var(--font-mono); font-size: 0.78rem; }
.login-box label { display: flex; flex-direction: column; gap: 5px; font-size: 0.82rem; font-weight: 500; }
.login-erro { color: var(--stamp); font-size: 0.82rem; min-height: 1.1em; font-family: var(--font-mono); }

/* ---- Separadores -------------------------------------------------- */
.tabs {
  display: flex;
  gap: 2px;
  padding: 0 22px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.tab-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: none;
  background: transparent;
  cursor: pointer;
  padding: 11px 15px;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  letter-spacing: 0.01em;
  color: var(--ink-faint);
  border-bottom: 2px solid transparent;
  transition: color 0.15s;
}
.tab-btn:hover { color: var(--ink-soft); }
.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  box-shadow: 0 1px 0 var(--accent), 0 6px 16px -8px var(--accent-glow);
}

main { padding: 26px 22px; max-width: 1120px; margin: 0 auto; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* Cabeça-corrente: assinatura de terminal no topo de cada página */
.runhead {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin: 0 0 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 8px;
}
.runhead::before {
  content: "";
  width: 6px; height: 6px;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent-glow);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 18px;
}
.panel-header h2 { margin: 0; font-size: 1.4rem; }
.panel-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---- Campos de formulário --------------------------------------- */
input, select, textarea, button { font-family: inherit; font-size: 0.9rem; }

input[type=text], input[type=search], input[type=date], input[type=time],
input[type=number], input[type=email], input[type=password], input[type=month],
select, textarea {
  padding: 8px 11px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--field);
  color: var(--ink);
}
input[type=search], input[type=text], input[type=number], input[type=email],
input[type=password] { font-family: var(--font-mono); font-size: 0.84rem; }
input::placeholder, textarea::placeholder { color: var(--ink-faint); }
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
input:disabled, select:disabled, textarea:disabled {
  background: var(--surface-2);
  color: var(--ink-faint);
  cursor: not-allowed;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 15px;
  border-radius: var(--radius);
  border: 1px solid var(--line-strong);
  background: var(--surface-2);
  color: var(--ink);
  cursor: pointer;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s, box-shadow 0.15s;
}
.btn:hover { background: var(--hover); border-color: var(--ink-faint); }
.btn.primary {
  background: var(--accent);
  color: var(--accent-on);
  border-color: var(--accent);
  font-weight: 600;
}
.btn.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  box-shadow: 0 0 18px -4px var(--accent-glow);
}
.btn.danger { background: var(--danger); color: var(--danger-on); border-color: var(--danger); font-weight: 600; }
.btn.danger:hover { background: var(--danger-hover); border-color: var(--danger-hover); }
.btn.small { padding: 5px 10px; font-size: 0.78rem; }
a.btn { display: inline-flex; text-decoration: none; }

/* ---- Tabelas (livro-razão) ------------------------------------- */
.table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
table { width: 100%; border-collapse: collapse; min-width: 640px; }
th, td {
  text-align: left;
  padding: 10px 13px;
  border-bottom: 1px solid var(--line);
  font-size: 0.88rem;
}
th {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
  color: var(--ink-faint);
  background: var(--surface-2);
  border-bottom: 1px solid var(--line-strong);
}
tbody td { font-variant-numeric: tabular-nums; color: var(--ink-soft); }
tbody td:nth-child(2) { color: var(--ink); font-weight: 500; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:nth-child(even) { background: var(--zebra); }
tbody tr:hover td { background: var(--hover); color: var(--ink); }

/* Nº de sócio e outros identificadores em monospace */
#tabela-socios tbody td:first-child,
#tabela-cotas tbody td:first-child,
#tabela-utilizadores tbody td:first-child {
  font-family: var(--font-mono);
  color: var(--ink-faint);
}

/* Cabeçalho fixo ao percorrer as listas longas */
#tab-socios .table-wrap,
#tab-cotas .table-wrap,
#tab-pagamentos .table-wrap,
#tab-dinheiro .table-wrap,
#tab-log .table-wrap {
  max-height: calc(100vh - 240px);
  overflow-y: auto;
}
#tab-socios thead th,
#tab-cotas thead th,
#tab-pagamentos thead th,
#tab-dinheiro thead th,
#tab-log thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  box-shadow: inset 0 -1px 0 var(--line-strong);
}

/* ---- Etiquetas de estado -------------------------------------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 3px;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  border: 1px solid currentColor;
  background: color-mix(in srgb, currentColor 12%, transparent);
}
.badge::before {
  content: "";
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
}
.badge.pago, .badge.ativo, .badge.em_dia, .badge.entrada { color: var(--ok); }
.badge.pendente { color: var(--warn); }
.badge.atrasado, .badge.saida { color: var(--stamp); }
.badge.inativo, .badge.sem_registo { color: var(--ink-faint); }

#tabela-dinheiro td.valor-neg { color: var(--stamp); font-weight: 600; }

/* ---- Cartões ------------------------------------------------------ */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  position: relative;
  overflow: hidden;
}
.card::before {
  content: "";
  position: absolute;
  top: 0; left: 0;
  width: 26px; height: 2px;
  background: var(--accent);
  box-shadow: 0 0 10px var(--accent-glow);
}
.card h3 { margin-top: 0; margin-bottom: 8px; font-size: 1rem; }

.wa-qr-wrap { margin-top: 12px; text-align: center; }
.wa-qr-wrap img {
  max-width: 220px;
  padding: 8px;
  background: #fff;
  box-shadow: 0 0 0 1px var(--line-strong), 0 0 28px -8px var(--accent-glow);
  border-radius: var(--radius);
}
.wa-qr-wrap.hidden { display: none; }

.resultado-lista { margin-top: 10px; font-family: var(--font-mono); font-size: 0.8rem; max-height: 160px; overflow-y: auto; }
.resultado-lista .linha { padding: 4px 0; border-bottom: 1px dashed var(--line); }
.resultado-lista .ok { color: var(--ok); }
.resultado-lista .falha { color: var(--stamp); }

/* ---- Formulário de configurações ------------------------------ */
.form-config {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-width: 560px;
  background: var(--surface);
  padding: 22px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
}
.form-config label { display: flex; flex-direction: column; gap: 6px; font-weight: 500; font-size: 0.88rem; }
.form-config small { color: var(--ink-soft); font-weight: 400; font-family: var(--font-mono); font-size: 0.74rem; }

.hint { color: var(--ink-soft); font-size: 0.83rem; }

#fieldset-permissoes {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
#fieldset-permissoes legend {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
  color: var(--ink-soft);
  padding: 0 4px;
}

/* ---- Janelas modais ------------------------------------------ */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(4, 6, 10, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
  backdrop-filter: blur(3px);
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line-strong);
  border-top: 2px solid var(--accent);
  border-radius: var(--radius);
  padding: 20px 24px;
  width: 100%;
  max-width: 620px;
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}
.modal-content h3 { margin-top: 0; margin-bottom: 12px; font-size: 1.1rem; }
.modal-content form { display: flex; flex-direction: column; gap: 9px; }
.modal-content form label { display: flex; flex-direction: column; gap: 3px; font-size: 0.86rem; font-weight: 500; }
.modal-content form label.checkbox { flex-direction: row; align-items: center; gap: 8px; }
.modal-content form textarea { min-height: 32px; }
.modal-content form small { font-size: 0.75rem; line-height: 1.25; color: var(--ink-soft); font-family: var(--font-mono); }
.modal-content form small:empty { display: none; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; }
.form-row-align { align-items: end; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }

/* ---- Ícones e foco --------------------------------------------- */
.ico { width: 16px; height: 16px; flex: none; pointer-events: none; }
.btn.small .ico { width: 14px; height: 14px; }
.tab-btn .ico { width: 15px; height: 15px; }

.btn:focus-visible,
.tab-btn:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
a:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* ---- Pagamentos e Log: uma linha por célula ------------------- */
#tabela-pagamentos th, #tabela-pagamentos td,
#tabela-log th,        #tabela-log td {
  white-space: nowrap;
  vertical-align: middle;
}
#tabela-log td:first-child,
#tabela-pagamentos td:first-child { font-family: var(--font-mono); color: var(--ink-faint); }

/* ---- Caixa: cabe sem barra horizontal ------------------------- */
#tabela-dinheiro { min-width: 0; table-layout: fixed; }
#tabela-dinheiro th, #tabela-dinheiro td { vertical-align: middle; overflow-wrap: anywhere; padding: 10px 8px; }
#tabela-dinheiro thead th:nth-child(1) { width: 7em; }
#tabela-dinheiro thead th:nth-child(2) { width: 5.5em; }
#tabela-dinheiro thead th:nth-child(3) { width: 6em; }
#tabela-dinheiro td:nth-child(1) { font-size: 0.8rem; font-family: var(--font-mono); }
#tabela-dinheiro .badge { padding: 1px 6px; gap: 0; font-size: 0.64rem; }
#tabela-dinheiro .badge::before { display: none; }
#tabela-dinheiro thead th:nth-child(4) { width: 10%; }
#tabela-dinheiro thead th:nth-child(6),
#tabela-dinheiro thead th:nth-child(7) { width: 6.5em; }
#tabela-dinheiro thead th:nth-child(8) { width: 5.5em; }
#tabela-dinheiro td:nth-child(1),
#tabela-dinheiro td:nth-child(2) { white-space: nowrap; }
#tabela-dinheiro th:nth-child(3), #tabela-dinheiro td:nth-child(3) { text-align: right; white-space: nowrap; }
#tabela-dinheiro td:nth-child(6),
#tabela-dinheiro td:nth-child(7) { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#tabela-dinheiro td.col-acoes { white-space: nowrap; text-align: right; padding-left: 2px; padding-right: 8px; }
#tabela-dinheiro td.col-acoes .btn.small { padding: 5px 6px; }

#tabela-log td:nth-child(4) { max-width: 520px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#tabela-pagamentos td:nth-child(2) { max-width: 240px; overflow: hidden; text-overflow: ellipsis; }
#tabela-pagamentos .hint { margin-left: 6px; font-size: 0.76rem; }
#tabela-pagamentos td:nth-child(3) { font-size: 0.8rem; color: var(--ink-faint); font-family: var(--font-mono); }

/* ---- Responsivo ---------------------------------------------- */
@media (max-width: 640px) {
  main { padding: 16px 14px; }
  .panel-header { flex-direction: column; align-items: flex-start; }
}
@media (max-width: 560px) {
  .form-row { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
