/*
 * Mukanda: estilos da aplicação.
 * Mobile-first, sem dependências. As cores estão nas variáveis do :root.
 */

:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --muted: #5b6577;
  --border: #e3e8f0;
  --border-strong: #8b929c; /* limites dos campos com contraste de 3:1 (WCAG 1.4.11) */
  --primary: #1e40af;
  --primary-hover: #1e3a8a;
  --primary-soft: #e8eeff;
  --brand: #14306b;
  --brand-2: #1e3a8a;
  --ok: #166534;
  --ok-soft: #dcfce7;
  --warn: #92400e;
  --warn-soft: #fef3c7;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.05), 0 2px 6px rgba(15, 23, 42, 0.05);
  --shadow-lg: 0 10px 30px rgba(15, 23, 42, 0.14);
  --tap: 44px; /* altura mínima de alvos de toque */
  --sidebar: 248px;
  --appbar: 60px;
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  color: var(--text);
  background: var(--bg);
}

a { color: var(--primary); }
a:hover { color: var(--primary-hover); }

h1 { font-size: 1.6rem; line-height: 1.25; margin: 0.25rem 0 1rem; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; margin: 1.75rem 0 0.75rem; letter-spacing: -0.005em; }
h3 { font-size: 1rem; margin: 1.25rem 0 0.5rem; }
.card h2:first-child, .card h1:first-child { margin-top: 0; }

small, .muted { color: var(--muted); }
.muted { font-size: 0.9rem; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: #f3f4f6; padding: 2px 6px; border-radius: 6px; }
code.big { font-size: 1.35rem; letter-spacing: 0.04em; }

/* --- estrutura: menu lateral, barra de topo, conteúdo ------------------------ */

.skip-link { position: absolute; left: -999px; top: 8px; z-index: 100; background: var(--surface); padding: 8px 12px; border-radius: 8px; }
.skip-link:focus { left: 8px; }
.ic { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.9; vertical-align: middle; display: inline-block; stroke-linecap: round; stroke-linejoin: round; }

.brand { color: #fff; font-weight: 800; font-size: 1.15rem; text-decoration: none; letter-spacing: -0.01em; }
.brand:hover { color: #fff; }

.sidebar {
  position: fixed; inset: 0 auto 0 0; width: var(--sidebar); z-index: 40;
  background: linear-gradient(180deg, var(--brand) 0%, #0f2554 100%); color: #dbe4ff;
  display: flex; flex-direction: column; overflow-y: auto; overscroll-behavior: contain;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 8px; }
.sidebar-school { display: flex; align-items: center; gap: 10px; margin: 4px 12px 8px; padding: 10px; border-radius: 10px; background: rgba(255, 255, 255, 0.08); }
.sidebar-school strong { display: block; color: #fff; font-size: 0.92rem; line-height: 1.25; }
.sidebar-school small { color: #b9c6ef; font-size: 0.78rem; }
.sidebar-school .school-logo.small { height: 34px; background: #fff; padding: 2px; }
.sidenav { padding: 4px 8px 24px; }
.nav-section { padding: 6px 0; border-top: 1px solid rgba(255, 255, 255, 0.08); }
.nav-section:first-child { border-top: 0; }
.nav-heading { margin: 8px 12px 4px; font-size: 0.7rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: #93a6db; }
.nav-link { display: flex; align-items: center; gap: 12px; min-height: 40px; padding: 8px 12px; border-radius: 8px; color: #dbe4ff; text-decoration: none; font-size: 0.93rem; }
.nav-link:hover { background: rgba(255, 255, 255, 0.08); color: #fff; }
.nav-link.active { background: #fff; color: var(--brand); font-weight: 600; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15); }
.nav-link.active .ic { stroke: var(--primary); }
.nav-badge { margin-left: auto; background: #f59e0b; color: #1f1300; border-radius: 999px; padding: 0 7px; font-size: 0.75rem; font-weight: 700; }
.sidebar .nav-link:focus-visible, .sidebar button:focus-visible { outline: 3px solid #fbbf24; outline-offset: 2px; }

.shell { min-height: 100vh; display: flex; flex-direction: column; min-width: 0; }
.has-shell .shell { margin-left: var(--sidebar); }

.appbar {
  position: sticky; top: 0; z-index: 30; height: var(--appbar);
  display: flex; align-items: center; gap: 12px; padding: 0 24px;
  background: rgba(255, 255, 255, 0.92); backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 1px solid var(--border);
}
.appbar-plain { background: var(--brand); border-bottom: 0; color: #fff; }
.appbar-title { font-weight: 600; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.appbar-plain .appbar-title { color: #c7d2fe; font-weight: 500; }
.appbar-search { position: relative; flex: 1; max-width: 520px; display: flex; align-items: center; }
.appbar-search .ic { position: absolute; left: 12px; color: var(--muted); pointer-events: none; }
.appbar-search { min-width: 0; }
.appbar-search input[type="search"] { width: 100%; min-width: 0; min-height: 42px; padding: 8px 14px 8px 40px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface-2); font: inherit; font-size: 0.95rem; }
.appbar-search input[type="search"]:focus { background: var(--surface); border-color: var(--primary); outline: 3px solid var(--primary-soft); }

.icon-button { display: inline-flex; align-items: center; justify-content: center; width: var(--tap); height: var(--tap); padding: 0; border: 0; border-radius: 10px; background: transparent; color: inherit; cursor: pointer; }
.icon-button:hover { background: rgba(15, 23, 42, 0.06); }
.sidebar .icon-button:hover { background: rgba(255, 255, 255, 0.1); }
.nav-open, .sidebar-close { display: none; }

.usermenu { position: relative; margin-left: auto; }
.usermenu summary { list-style: none; cursor: pointer; display: flex; align-items: center; gap: 8px; min-height: var(--tap); padding: 4px 10px 4px 4px; border-radius: 999px; font-size: 0.92rem; font-weight: 600; }
.usermenu summary:hover { background: rgba(15, 23, 42, 0.05); }
.appbar-plain .usermenu summary:hover { background: rgba(255, 255, 255, 0.12); }
.usermenu summary::-webkit-details-marker { display: none; }
.avatar { width: 34px; height: 34px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; background: var(--primary-soft); color: var(--primary); font-weight: 800; }
.usermenu-panel { position: absolute; right: 0; top: calc(100% + 8px); z-index: 50; background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg); min-width: 220px; padding: 8px; }
.usermenu-panel p { margin: 4px 8px 8px; }
.usermenu-panel a, .usermenu-panel button.link { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; padding: 10px 8px; min-height: var(--tap); border-radius: 8px; color: var(--text); text-decoration: none; font-size: 0.95rem; background: none; border: 0; font: inherit; cursor: pointer; }
.usermenu-panel a:hover, .usermenu-panel button.link:hover { background: var(--bg); }

.scrim { position: fixed; inset: 0; z-index: 35; background: rgba(15, 23, 42, 0.45); }

@media (max-width: 1023px) {
  .has-shell .shell { margin-left: 0; }
  .sidebar { transform: translateX(-100%); transition: transform 0.2s ease; box-shadow: var(--shadow-lg); }
  .nav-open-state .sidebar { transform: none; }
  .nav-open, .sidebar-close { display: inline-flex; }
  .appbar { padding: 0 12px; gap: 6px; }
  .usermenu-name { display: none; }
}
@media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }
/* Sem JavaScript, o menu fica visível no topo. */
.no-js .sidebar { position: static; transform: none; width: auto; }

.tabs { display: flex; gap: 4px; overflow-x: auto; border-bottom: 1px solid var(--border); margin-bottom: 16px; scrollbar-width: none; }
.tabs a { padding: 10px 12px; text-decoration: none; color: var(--muted); white-space: nowrap; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

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

.container { width: 100%; max-width: 1200px; margin: 0 auto; padding: 24px 24px 64px; }
@media (max-width: 600px) { .container { padding: 16px 16px 48px; } }
.row-between { display: flex; flex-wrap: wrap; gap: 8px; justify-content: space-between; align-items: center; }
.row-between h1 { margin-bottom: 0.5rem; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.two-cols, .counter-grid { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 860px) {
  .two-cols { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .counter-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
  .counter-grid #panel { position: sticky; top: calc(var(--appbar) + 16px); }
}

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin: 16px 0; }
.card.ok { border-left: 4px solid var(--ok); }
.card.error { border-left: 4px solid var(--danger); }
.auth-card { max-width: 380px; margin: 32px auto; }
.hero { max-width: 560px; margin: 48px auto; text-align: center; }
.hero p { color: var(--muted); font-size: 1.05rem; }

/* --- mensagens ------------------------------------------------------------ */

.messages { list-style: none; padding: 0; margin: 0 0 16px; }
.msg { padding: 10px 14px; border-radius: 8px; margin-bottom: 8px; background: var(--primary-soft); color: var(--primary-hover); }
.msg.success { background: var(--ok-soft); color: var(--ok); }
.msg.error { background: var(--danger-soft); color: var(--danger); }
.msg.warning { background: var(--warn-soft); color: var(--warn); }
.warning { color: var(--warn); background: var(--warn-soft); padding: 10px 14px; border-radius: 8px; }

/* --- botões --------------------------------------------------------------- */

button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--tap); padding: 0 16px; border-radius: 8px; border: 1px solid transparent;
  background: var(--primary); color: #fff; font: inherit; font-weight: 600; text-decoration: none;
  cursor: pointer; white-space: nowrap;
}
button:hover, .button:hover { background: var(--primary-hover); color: #fff; }
button:disabled, button.htmx-request { opacity: 0.6; cursor: progress; }
button.secondary, .button.secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
button.secondary:hover, .button.secondary:hover { background: var(--bg); color: var(--text); }
button.danger, .button.danger { background: var(--danger); }
button.danger:hover { background: #991b1b; }
button.small, .button.small { min-height: 40px; padding: 0 12px; font-size: 0.875rem; }
button.block, .button.block { width: 100%; }
button.link { background: none; border: 0; color: var(--primary); padding: 0; min-height: 0; font-weight: 400; }

/* --- formulários ---------------------------------------------------------- */

input, select, textarea { font: inherit; color: inherit; }
input[type="text"], input[type="search"], input[type="email"], input[type="password"], input[type="number"],
input[type="date"], input[type="month"], input[type="tel"], input[type="url"], input[type="time"],
input[type="datetime-local"], input:not([type]), select, textarea {
  width: 100%; min-height: var(--tap); padding: 8px 12px; border: 1px solid var(--border-strong);
  border-radius: 8px; background: var(--surface);
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); }
/* Foco visível para quem usa o teclado (WCAG 2.4.7). */
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible,
a:focus-visible, summary:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; }
.topbar :focus-visible { outline-color: #fff; }
textarea { min-height: 80px; }
input[type="file"] { width: 100%; padding: 8px; border: 1px dashed var(--border-strong); border-radius: 8px; background: var(--surface-2); font-size: 0.9rem; }
input[type="file"]::file-selector-button { margin-right: 12px; padding: 8px 14px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface); font: inherit; font-weight: 600; cursor: pointer; }
.field { margin: 0 0 14px; }
.field > label { display: block; font-weight: 600; font-size: 0.95rem; margin-bottom: 4px; }

.form { max-width: 560px; }
.form > div, .form > p { margin: 0 0 14px; }
.form fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
.form label, .form legend { display: block; font-weight: 600; font-size: 0.95rem; margin-bottom: 4px; padding: 0; }
.form .helptext { display: block; color: var(--muted); font-size: 0.85rem; margin: -2px 0 6px; }
.form input[type="checkbox"] { width: 22px; height: 22px; vertical-align: middle; }
.form div:has(> input[type="checkbox"]) { display: flex; flex-direction: row-reverse; justify-content: flex-end; align-items: center; gap: 8px; }
.form div:has(> input[type="checkbox"]) label { margin: 0; }
.form div:has(> input[type="checkbox"]) .helptext { display: none; }

/* Botões de escolha (ex.: método de pagamento) */
.form div[id^="id_method"] { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.form div[id^="id_method"] label {
  display: flex; align-items: center; justify-content: center; gap: 8px; min-height: 52px;
  border: 2px solid var(--border-strong); border-radius: 8px; background: var(--surface);
  font-weight: 600; cursor: pointer; margin: 0;
}
.form div[id^="id_method"] label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); color: var(--primary-hover); }
.form div[id^="id_method"] input { width: 18px; height: 18px; margin: 0; }
/* Talão do TPA só aparece quando o método é TPA (sem :has, fica sempre visível). */
.form:has(input[name="method"][value="cash"]:checked) div:has(> #id_tpa_slip) { display: none; }

.errorlist { list-style: none; padding: 8px 12px; margin: 0 0 8px; color: var(--danger); background: var(--danger-soft); border-radius: 8px; font-size: 0.9rem; }
.errorlist.nonfield { margin-bottom: 14px; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; align-items: center; }
.filters input, .filters select { width: auto; flex: 1 1 160px; }
.filters button, .filters .button { flex: 0 0 auto; }
.inline-form { display: flex; gap: 6px; align-items: center; }
.inline-form input { min-width: 120px; }
.inline { margin-bottom: 16px; }

.search { font-size: 1.1rem; min-height: 52px !important; margin: 4px 0 16px; }

/* --- números e estados --------------------------------------------------- */

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px; }
.stat span, .stat small { display: block; color: var(--muted); font-size: 0.85rem; }
.stat strong { display: block; font-size: 1.35rem; margin: 2px 0; font-variant-numeric: tabular-nums; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 0.8rem; font-weight: 600; background: #f3f4f6; color: #4b5563; white-space: nowrap; }
.badge.paid { background: var(--ok-soft); color: var(--ok); }
.badge.overdue { background: var(--danger-soft); color: var(--danger); }
.badge.pending { background: var(--warn-soft); color: var(--warn); }
.badge.cancelled { text-decoration: line-through; }
.badge.unit { background: var(--primary-soft); color: var(--primary-hover); }
details.card summary { cursor: pointer; font-weight: 600; }
details.card[open] summary { margin-bottom: 12px; }
.diff { color: var(--danger); font-weight: 700; }
td.message-text { white-space: normal; min-width: 240px; max-width: 420px; font-size: 0.9rem; }
.ok-text { color: var(--ok); }

/* --- tabelas -------------------------------------------------------------- */

table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
thead th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); background: #f9fafb; }
tbody tr:last-child td { border-bottom: 0; }
tfoot th { border-top: 2px solid var(--border-strong); }
table.summary { max-width: 480px; }
table.summary th { color: var(--muted); font-weight: 500; width: 45%; }

@media (max-width: 700px) {
  /* No telemóvel as tabelas largas deslizam na horizontal... */
  table:not(.summary):not(.responsive) { display: block; overflow-x: auto; white-space: nowrap; }

  /* ...e as tabelas de listas viram cartões (cada célula com a etiqueta da coluna). */
  table.responsive, table.responsive tbody, table.responsive tr, table.responsive td,
  table.responsive tfoot, table.responsive tfoot th { display: block; }
  table.responsive { border: 0; background: none; }
  table.responsive thead { display: none; }
  table.responsive tr { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 10px; padding: 10px 12px; }
  table.responsive td { border: 0; padding: 4px 0; text-align: right; }
  table.responsive td::before { content: attr(data-label); float: left; margin-right: 12px; color: var(--muted); font-size: 0.85rem; text-align: left; }
  table.responsive td::after { content: ""; display: block; clear: both; }
  table.responsive td:first-child { display: block; text-align: left; font-size: 1.02rem; padding-bottom: 6px; }
  table.responsive td:first-child::before, table.responsive td:first-child::after { content: none; }
  table.responsive td:empty { display: none; }
  table.responsive td[colspan] { display: block; text-align: left; }
  table.responsive td[colspan]::before { content: none; }
  table.responsive tfoot tr { background: var(--primary-soft); }

  /* Botões com texto longo quebram a linha em vez de sair do ecrã. */
  button, .button { white-space: normal; text-align: center; line-height: 1.25; padding-top: 8px; padding-bottom: 8px; }
}

/* --- listas e balcão ------------------------------------------------------ */

.list { list-style: none; padding: 0; margin: 0; }
.list li { padding: 10px 0; border-bottom: 1px solid var(--border); }
.results li { padding: 0; }
.results > li > a { display: block; padding: 12px 4px; color: inherit; text-decoration: none; }
.results > li > a:hover { background: var(--surface); }
.results small { display: block; }
#results .results > li { padding: 12px 0; }

.invoice-pick {
  display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 8px 0 0;
  padding: 12px; min-height: var(--tap); border: 1px solid var(--border-strong); border-radius: 8px;
  color: inherit; text-decoration: none; background: var(--surface);
}
.invoice-pick:hover { border-color: var(--primary); color: inherit; box-shadow: var(--shadow); }

/* --- modo offline ------------------------------------------------------------ */

.offline-banner { background: var(--warn-soft); color: var(--warn); text-align: center; padding: 8px 16px; font-size: 0.9rem; font-weight: 600; }
.counter-status { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; font-size: 0.9rem; color: var(--muted); margin-bottom: 4px; min-height: 24px; }
.counter-status.offline { color: var(--warn); }
.counter-status.offline::before { content: "●"; color: var(--danger); }
.counter-status.online::before { content: "●"; color: var(--ok); }

@media print {
  .topbar, .offline-banner, .counter-status, #results, .search, .row-between, button { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; border: 1px solid #000; }
}

/* --- portal do encarregado ---------------------------------------------------- */

.container.narrow { max-width: 640px; }
.portal-invoice { display: flex; justify-content: space-between; gap: 12px; align-items: center; padding: 12px 0; border-top: 1px solid var(--border); margin-top: 10px; }
.portal-invoice-right { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; text-align: right; }
.portal-payment { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px; padding: 8px 0; border-top: 1px dashed var(--border); font-size: 0.9rem; }
.ref-number { font-size: 1.25rem; letter-spacing: 0.06em; font-variant-numeric: tabular-nums; }

/* --- melhorias de usabilidade ---------------------------------------------- */

.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Filtros com etiqueta pequena por cima de cada campo. */
.filters .field { display: flex; flex-direction: column; gap: 2px; flex: 1 1 150px; min-width: 0; }
.filters .field > span { font-size: 0.8rem; font-weight: 600; color: var(--muted); }
.filters .field input, .filters .field select { width: 100%; flex: none; }
.filters { align-items: flex-end; }

/* Atalhos de período. */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 12px; }
.chips a { min-height: 40px; display: inline-flex; align-items: center; padding: 6px 14px; border-radius: 999px; border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); text-decoration: none; font-size: 0.9rem; }
.chips a.active { background: var(--primary); border-color: var(--primary); color: #fff; }

/* Pesquisa do balcão com indicador de actividade. */
.search-box { position: relative; }
.search-box .htmx-indicator { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 0.85rem; opacity: 0; transition: opacity 0.2s; }
.search-box .htmx-request.htmx-indicator, .search-box.htmx-request .htmx-indicator { opacity: 1; }
.kbd { font-family: ui-monospace, monospace; font-size: 0.8rem; border: 1px solid var(--border-strong); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; background: var(--surface); }
.empty-state { color: var(--muted); padding: 24px 12px; text-align: center; border: 1px dashed var(--border-strong); border-radius: var(--radius); }

/* Cabeçalho do balcão: título + estado da caixa. */
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; margin-bottom: 8px; }
.page-head h1 { margin: 0; }

/* Zona de acções perigosas. */
details.danger-zone { border-color: #fecaca; }
details.danger-zone summary { color: var(--danger); }
details.danger-zone .form { margin-top: 8px; }

/* Formulários pequenos dentro de cartões e listas de botões não saem do ecrã. */
.inline-form { flex-wrap: wrap; }
.inline-form input { flex: 1 1 140px; }
.actions > * { max-width: 100%; min-width: 0; }
@media (max-width: 700px) {
  table.responsive td .inline-form { width: 100%; justify-content: flex-end; }
}

/* Pagar vários meses. */
.multi-pick { display: inline-flex; align-items: center; min-height: var(--tap); margin: 8px 0 2px; font-weight: 600; font-size: 0.95rem; }
.month-picker { list-style: none; padding: 0; margin: 0 0 8px; }
.month-picker label { display: flex; align-items: center; gap: 10px; padding: 10px 12px; margin-bottom: 6px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface); cursor: pointer; font-weight: 400; }
.month-picker label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.month-picker label span { flex: 1; }
.month-picker input { width: 20px; height: 20px; margin: 0; }

/* Cantina: grelha de produtos na venda */
.product-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; margin-bottom: 12px; }
.product-pick { display: flex; flex-direction: column; gap: 4px; margin: 0; cursor: pointer; }
.product-pick input { width: 100%; font-size: 1.1rem; text-align: center; }
fieldset.chips { border: 0; padding: 0; }
fieldset.chips label { padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border-strong); background: var(--surface); }

/* Académico: negativas a vermelho e positivas a azul (Art. 37.º do RAA) */
.grade-neg { color: #c00000; font-weight: 700; }
.grade-pos { color: #1f4e9e; }
.absence-warning { color: #b25e00; font-weight: 700; }
.absence-exceeded { text-decoration: underline; }
.absent-check { display: flex; gap: .5rem; align-items: center; }
.table-scroll { overflow-x: auto; }
table.pauta td.num { text-align: center; }
table.marks input[type="text"] { max-width: 5.5em; text-align: center; }

/* Passos de um pedido (matrícula online) */
ol.steps { list-style: none; padding: 0; margin: 16px 0; display: grid; gap: 10px; }
ol.steps li { position: relative; padding-left: 30px; color: var(--muted, #5b6b8c); }
ol.steps li::before { content: ""; position: absolute; left: 0; top: 2px; width: 18px; height: 18px; border-radius: 50%; border: 2px solid currentColor; }
ol.steps li.done { color: #166534; } ol.steps li.done::before { background: currentColor; }
ol.steps li.current { color: #b25e00; font-weight: 600; }
ol.steps li.failed { color: #c8102e; } ol.steps li.failed::before { background: currentColor; }
dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; margin: 0 0 12px; }
dl.facts dt { color: var(--muted, #5b6b8c); } dl.facts dd { margin: 0; }
.qr-card { display: flex; align-items: center; justify-content: center; }
.qr-card svg { width: 160px; height: 160px; }

/* Horários */
table.timetable { width: 100%; border-collapse: collapse; min-width: 640px; }
table.timetable th, table.timetable td { border: 1px solid var(--border, #e3e7ef); padding: 6px; vertical-align: top; text-align: center; }
table.timetable td select { width: 100%; min-width: 70px; }
table.timetable td small { display: block; }
@media print {
  header.topbar, nav, .actions, .messages, form button, .msg { display: none !important; }
  table.timetable select { border: 0; appearance: none; -webkit-appearance: none; background: none; }
}

/* Página pública de preços */
.lead { font-size: 1.1rem; }
.module-picks { display: grid; gap: 8px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.module-pick { display: flex; gap: 10px; align-items: center; border: 1px solid var(--border, #e3e7ef); border-radius: 10px; padding: 8px 10px; cursor: pointer; }
.module-pick:has(input:checked) { border-color: var(--primary, #1e3a8a); background: rgba(30, 58, 138, .05); }
.module-pick input { margin: 0; }
.simulator .quote { margin-top: 12px; }
.packages { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.package .price { font-size: 1.5rem; font-weight: 700; margin: 4px 0; }
.package .price small { font-size: .85rem; font-weight: 400; color: var(--muted, #5b6b8c); }
ul.module-list { list-style: none; padding: 0; margin: 8px 0 0; display: grid; gap: 4px; }
ul.module-list img { vertical-align: middle; }
ul.diffs { padding-left: 18px; display: grid; gap: 6px; }

/* Bloco "Para tratar" (painel e balcão) */
.todo ul { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.todo a { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; padding: 8px; min-height: var(--tap); border-radius: 8px; }
.todo a:hover { background: rgba(30, 58, 138, .06); }
.todo .count { min-width: 2.2em; text-align: center; font-weight: 700; border-radius: 99px; padding: 2px 8px; background: #fff3d6; color: #8a5a00; }
.todo li.urgent .count { background: #fde2e4; color: #b00020; }

/* Telemóvel: o cabeçalho com o menu não fica fixo (ocuparia meio ecrã) e os grupos da
   direita abrem para dentro do ecrã. */
@media (max-width: 860px) {
  .topbar { position: static; }
  .navgroup:nth-last-child(-n+2) .navmenu { left: auto; right: 0; }
}
.grid-3 { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
.absent-check { min-height: var(--tap); }
.absent-check input[type="checkbox"], td > input[type="checkbox"] { width: 22px; height: 22px; }
.todo-compact { margin-top: 16px; }
.todo-compact > summary { cursor: pointer; min-height: var(--tap); display: flex; align-items: center; font-weight: 600; }
@media (max-width: 480px) {
  .portal-invoice { flex-wrap: wrap; }
  .portal-invoice-right { flex: 1 0 100%; flex-direction: row; justify-content: flex-end; align-items: center; gap: 8px; }
}

/* Conversas com as famílias */
.thread { display: grid; gap: 8px; margin: 12px 0; }
.bubble { max-width: 85%; padding: 8px 12px; border-radius: 12px; border: 1px solid var(--border, #e3e7ef); background: var(--surface, #fff); }
.bubble p { margin: 4px 0 0; }
.bubble.school { justify-self: end; background: #eef2ff; }
.bubble.family { justify-self: start; }

/* troco no balcão */
.change-calc { margin: .5rem 0; }
.change-calc input { max-width: 14rem; }
.change-out { font-weight: 600; min-height: 1.4em; margin: .25rem 0 0; }
.change-out.ok { color: var(--ok, #166534); }
.change-out.short { color: var(--danger, #b91c1c); }

/* área do aluno */
.mono { font-family: ui-monospace, "DejaVu Sans Mono", monospace; letter-spacing: .05em; }
.center { text-align: center; }

/* resumos <details> dentro de tabelas: alvo de toque */
td details > summary { min-height: var(--tap, 44px); display: inline-flex; align-items: center; cursor: pointer; color: var(--primary); font-weight: 600; }
.input-narrow { max-width: 6em; }
.health-note { color: var(--danger); font-weight: 600; }
/* calendário: cores próprias, não as dos estados de pagamento */
.badge.event-test { background: #ede9fe; color: #5b21b6; }
.badge.event-no_classes { background: #e0f2fe; color: #075985; }
.badge.event-meeting { background: #fef3c7; color: #92400e; }
.badge.event-activity { background: #dcfce7; color: #166534; }
.badge.event-deadline { background: #fee2e2; color: #991b1b; }

/* portal: atalhos para as secções da página do educando */
.jump { display: flex; gap: .5rem; overflow-x: auto; padding: .25rem 0 .75rem; margin-bottom: .5rem; scrollbar-width: none; }
.jump a { flex: 0 0 auto; padding: .5rem .85rem; min-height: var(--tap, 44px); display: inline-flex; align-items: center; border: 1px solid var(--border, #d6dbe4); border-radius: 999px; background: var(--surface, #fff); text-decoration: none; font-weight: 600; font-size: .9rem; }
[id] { scroll-margin-top: 72px; }
tr.total th, tr.total td { font-weight: 700; border-top: 2px solid var(--border-strong, #8b929c); }

/* primeiros passos */
.steps-list { margin: .5rem 0 0; padding-left: 1.4rem; }
.steps-list li { margin: .35rem 0; }
.steps-list li.done { color: var(--muted, #5b6472); }
.steps-list li.done span[aria-hidden] { color: var(--ok); font-weight: 700; }

.chips-line { display: flex; flex-wrap: wrap; gap: .4rem; margin: .4rem 0; }
.chips-line a.badge { text-decoration: none; }

/* portal: resumos (<details>) com altura de toque; cores das ocorrências */
.portal details > summary { min-height: var(--tap, 44px); display: flex; align-items: center; cursor: pointer; font-weight: 600; color: var(--primary); }
.badge.negative { background: var(--danger-soft); color: var(--danger); }
.badge.positive { background: var(--ok-soft); color: var(--ok); }

/* logótipo da escola */
.school-logo { max-width: 100%; object-fit: contain; vertical-align: middle; }
.school-logo.small { height: 28px; width: auto; margin-right: .4rem; border-radius: 4px; }
.school-logo.large { display: block; height: 72px; width: auto; margin: 0 0 .5rem; }
.school-logo-preview { height: 72px; width: auto; border: 1px solid var(--border); border-radius: 6px; padding: 4px; background: #fff; }

/* --- cabeçalho das páginas, indicadores e painel ------------------------------ */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px 16px; margin-bottom: 16px; }
.page-head h1 { margin: 0; }
.page-sub { margin: 4px 0 0; color: var(--muted); }

.kpis { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); margin: 16px 0 8px; }
.kpi { position: relative; display: flex; flex-direction: column; gap: 2px; padding: 18px 18px 16px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); color: var(--text); text-decoration: none; overflow: hidden; }
.kpi::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 4px; background: var(--border-strong); }
a.kpi:hover { border-color: var(--primary); color: var(--text); }
.kpi-icon { display: inline-flex; align-items: center; justify-content: center; width: 38px; height: 38px; margin-bottom: 8px; border-radius: 10px; background: var(--surface-2); color: var(--muted); }
.kpi-label { font-size: 0.85rem; color: var(--muted); font-weight: 600; }
.kpi-value { font-size: 1.55rem; line-height: 1.2; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.kpi-note { font-size: 0.82rem; color: var(--muted); margin-top: 4px; }
.kpi-ok::before { background: var(--ok); } .kpi-ok .kpi-icon { background: var(--ok-soft); color: var(--ok); }
.kpi-danger::before { background: var(--danger); } .kpi-danger .kpi-icon { background: var(--danger-soft); color: var(--danger); }
.kpi-primary::before { background: var(--primary); } .kpi-primary .kpi-icon { background: var(--primary-soft); color: var(--primary); }
.kpi .progress { margin: 6px 0 0; }

.progress { display: inline-block; width: 100%; max-width: 200px; height: 8px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; overflow: hidden; vertical-align: middle; }
.progress > span { display: block; height: 100%; background: var(--primary); border-radius: 999px; }
.onboarding summary { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; cursor: pointer; }
.onboarding .progress { max-width: 160px; }

.dash-grid { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); align-items: start; }
@media (min-width: 1100px) { .dash-grid { grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); } }
.dash-grid > * > .card:first-child, .dash-grid > .card { margin-top: 16px; }
.mini-stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.mini-stats > div { padding: 10px 12px; border-radius: 10px; background: var(--surface-2); }
.mini-stats strong { display: block; font-size: 1.35rem; line-height: 1.2; }
.mini-stats span { font-size: 0.85rem; color: var(--muted); }
.list.compact li { padding: 6px 0; }
.empty-state p { color: var(--muted); }
.period-total strong { font-size: 1.2rem; }
.table-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.table-card table { margin: 0; }
@media (max-width: 640px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi { padding: 12px 12px 12px 14px; }
  .kpi-icon { display: none; }
  .kpi-label { font-size: 0.78rem; }
  .kpi-value { font-size: 1.12rem; overflow-wrap: anywhere; }
  .kpi-note { font-size: 0.75rem; }
  .page-head .actions { width: 100%; }
  .page-head .actions > .button { flex: 1 1 auto; text-align: center; }
}

/* --- barra do portal e páginas públicas (sem menu lateral) -------------------- */
.topbar { background: linear-gradient(90deg, var(--brand), var(--primary-hover)); color: #fff; position: sticky; top: 0; z-index: 10; box-shadow: 0 1px 0 rgba(0,0,0,.08); }
.topbar-inner { max-width: 760px; margin: 0 auto; padding: 0 16px; min-height: var(--appbar); display: flex; align-items: center; gap: 12px; }
.topbar .school-name { color: #c7d2fe; font-size: 0.92rem; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .usermenu { margin-left: auto; }
.topbar .usermenu button { background: rgba(255,255,255,.12); color: #fff; border-color: rgba(255,255,255,.35); }
.topbar .usermenu button:hover { background: rgba(255,255,255,.22); }
.topbar :focus-visible { outline-color: #fff; }
body.portal { background: var(--bg); }

/* listas de resultados (alunos, balcão) em cartão, com seta de "abrir" */
.results { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-top: 12px; }
.results > li, #results .results > li { padding: 0; }
.results > li:last-child { border-bottom: 0; }
/* Linha que é toda um link (lista de alunos): seta de "abrir". */
.results > li > a:only-child { position: relative; padding: 12px 44px 12px 16px; }
.results > li > a:only-child::after { content: "›"; position: absolute; right: 18px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 1.5rem; line-height: 1; }
.results > li > a:only-child:hover { background: var(--surface-2); }
/* Linha com nome e vários botões (balcão): margem interior. */
.results > li:not(:has(> a:only-child)), #results .results > li:not(:has(> a:only-child)) { padding: 12px 16px; }
.results > li > strong { display: inline-block; margin-bottom: 2px; }
.results small { color: var(--muted); margin-top: 2px; }
.more-menu { position: relative; }
.more-menu > summary { list-style: none; cursor: pointer; }
.more-menu > summary::-webkit-details-marker { display: none; }
.more-menu-panel { position: absolute; right: 0; top: calc(100% + 6px); z-index: 20; min-width: 240px; padding: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 12px 32px rgba(15, 23, 42, .14); }
.more-menu-panel a { display: block; padding: 10px 12px; border-radius: 8px; color: var(--text); text-decoration: none; }
.more-menu-panel a:hover { background: var(--surface-2); }

/* ficha (aluno, funcionário): cabeçalho com avatar, factos rápidos e acções */
.breadcrumb { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 0.9rem; color: var(--muted); margin: 0 0 12px; }
.breadcrumb a { color: var(--muted); }
.breadcrumb a:hover { color: var(--primary); }
.profile-head { display: flex; gap: 16px; align-items: flex-start; margin-bottom: 12px; }
.profile-avatar { flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center; width: 56px; height: 56px; border-radius: 16px; background: var(--primary-soft); color: var(--primary); font-weight: 800; font-size: 1.5rem; }
.profile-main { flex: 1; min-width: 0; }
.profile-main h1 { margin: 0 0 6px; }
.profile-meta { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 12px; }
.profile-main details { margin-top: 10px; }
.chip { display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); font-size: 0.85rem; color: var(--text); }
main h2[id], main .card[id] { scroll-margin-top: calc(var(--appbar) + 12px); }
@media (max-width: 480px) { .profile-avatar { width: 44px; height: 44px; font-size: 1.2rem; border-radius: 12px; } .profile-head { gap: 12px; } }
/* "Para tratar" recolhido no balcão: parece um botão com contador */
.todo-compact > summary { display: inline-flex; align-items: center; gap: 8px; min-height: var(--tap); padding: 6px 14px; border: 1px solid var(--border-strong); border-radius: 999px; background: var(--surface); font-weight: 600; cursor: pointer; list-style: none; }
.todo-compact > summary::-webkit-details-marker { display: none; }
.todo-compact > summary::after { content: "▾"; font-size: .75rem; color: var(--muted); }
.todo-compact[open] > summary::after { content: "▴"; }
@media (max-width: 640px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { padding: 12px; min-width: 0; }
  .stat strong { font-size: 1.12rem; overflow-wrap: anywhere; }
}
.filter-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 0 0 12px; }
.filter-row select, .filter-row input { flex: 1 1 220px; max-width: 360px; width: auto; }

/* --- contrato e termos ---------------------------------------------------------- */
.legal-page { max-width: 780px; }
.legal-meta { color: var(--muted); font-size: .9rem; margin: 0 0 12px; }
.legal-summary { background: var(--primary-soft); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 20px; margin-bottom: 12px; }
.legal-summary h2 { font-size: 1.05rem; margin: 0 0 8px; }
.legal-summary ul { margin: 0; padding-left: 20px; }
.legal-summary li { margin: 4px 0; }
.legal-full { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 20px; margin-bottom: 16px; }
.legal-full > summary { cursor: pointer; font-weight: 600; min-height: var(--tap); display: flex; align-items: center; }
.legal-full h3 { font-size: 1rem; margin: 18px 0 6px; }
.legal-full p { margin: 0 0 8px; line-height: 1.55; }
.legal-hash { color: var(--muted); font-size: .8rem; overflow-wrap: anywhere; }
.legal-sign { margin-top: 8px; }
.legal-sign .check, .check { display: flex; gap: 10px; align-items: flex-start; margin: 0 0 14px; font-weight: 600; }
.check input { width: 22px; height: 22px; flex: 0 0 auto; }
.warning-card { border-color: #fde68a; background: #fffbeb; }
.form input[type="checkbox"] { flex: 0 0 auto; }
.legal-full > summary { list-style: none; gap: 8px; }
.legal-full > summary::-webkit-details-marker { display: none; }
.legal-full > summary::before { content: "▸"; color: var(--primary); transition: transform .15s; }
.legal-full[open] > summary::before { transform: rotate(90deg); }

/* avaliações contínuas */
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 0 12px; }
.wide-form { max-width: none; }
.assess-callout { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; margin: 12px 0; }
.assess-callout p { margin: 4px 0 0; }
.assess-list li { padding: 10px 0; }
.page-head .kpi { min-width: 160px; }
.assess-table td, .assess-table th { white-space: nowrap; }
.assess-table td.wrap { white-space: normal; }
.assess-subject { border-top: 1px solid var(--border); padding: 8px 0; }
.assess-subject > summary { cursor: pointer; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; min-height: var(--tap); }
.assess-score { white-space: nowrap; font-size: 1.05rem; }
.marks input.comment-input { width: 100%; min-width: 220px; }
.assess-subject .row-between { flex-wrap: nowrap; align-items: flex-start; gap: 12px; }

/* Indicadores: 4 por linha no computador, 2 no tablet e no telemóvel; ícone no canto. */
.kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (min-width: 1200px) { .kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.kpi { padding: 16px 16px 14px 20px; min-height: 0; }
.kpi-icon { position: absolute; top: 14px; right: 14px; margin: 0; width: 36px; height: 36px; }
.kpi-label { padding-right: 44px; min-height: 2.3em; display: flex; align-items: flex-end; }
.kpi-value { margin-top: 2px; }
@media (max-width: 640px) { .kpi-label { padding-right: 0; min-height: 0; } }
.dash-grid { margin-top: 16px; }
.dash-grid > * > .card:first-child, .dash-grid > .card, .dash-grid .card { margin-top: 0; margin-bottom: 0; }
.dash-grid > div { display: grid; gap: 16px; align-content: start; }
/* Listas com acção à direita (ex.: trimestres) */
.list.rows > li { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; }
.list.rows > li > form { margin: 0; }
ul.list.card { padding: 4px 16px; }
ul.list.card > li:last-child { border-bottom: 0; }
#results .results .invoice-pick { padding: 10px 14px; gap: 8px; flex-wrap: wrap; }
#results .results .multi-pick { padding: 0 4px; }

/* Servidor de demonstração */
.demo-ribbon { position: fixed; left: 0; right: 0; bottom: 0; z-index: 80; background: #fef3c7; color: #78350f; border-top: 1px solid #fcd34d; text-align: center; font-size: .8rem; font-weight: 600; padding: 5px 12px; pointer-events: none; }
body:has(.demo-ribbon) { padding-bottom: 30px; }
