:root {
  --sidebar-width: 240px;
  --bg: #ffffff;
  --sidebar-bg: #f7f7f5;
  --text: #37352f;
  --text-muted: #91918e;
  --hover: rgba(55, 53, 47, 0.08);
  --border: rgba(55, 53, 47, 0.09);
  --shadow: 0 0 0 1px rgba(15, 15, 15, 0.05), 0 3px 6px rgba(15, 15, 15, 0.1), 0 9px 24px rgba(15, 15, 15, 0.2);
  --ease: 220ms cubic-bezier(0.2, 0, 0, 1);
  --accent: #2383e2;
  --accent-hover: #0077d4;
  --danger: #eb5757;
  --input-bg: rgba(242, 241, 238, 0.6);
  --title-accent: #b08600;
  --state-draft: #d9779c;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #191919;
  --sidebar-bg: #202020;
  --text: #e3e3e3;
  --text-muted: #9b9b9b;
  --hover: rgba(255, 255, 255, 0.06);
  --border: rgba(255, 255, 255, 0.08);
  --input-bg: rgba(255, 255, 255, 0.04);
  --title-accent: #f3d97a;
  --state-draft: #f4b6cc;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: ui-sans-serif, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ---------- Sidebar ---------- */
.sidebar {
  position: relative;
  flex-shrink: 0;
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: width var(--ease), transform var(--ease), opacity var(--ease);
  z-index: 20;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 45px;
  padding: 0 8px 0 12px;
}

.workspace {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  font-weight: 600;
  font-size: 14px;
}

.workspace-icon {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 4px;
  background: var(--text);
  color: var(--bg);
  font-size: 12px;
}

.workspace-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Trocar de empresa: a lista do sistema fica invisível por cima do nome, que a abre ao clicar */
.workspace { position: relative; border-radius: 6px; padding: 2px 4px; margin-left: -4px; }
.workspace:has(.workspace-switch:not([hidden])):hover { background: var(--hover); }
.workspace-chevron { flex-shrink: 0; color: var(--text-muted); }
.workspace-switch { position: absolute; inset: 0; width: 100%; opacity: 0; cursor: pointer; }

/* Password do login com o botão do olho por dentro do campo, à direita */
.password-wrap { position: relative; display: flex; }
.password-wrap input { flex: 1; min-width: 0; padding-right: 40px; }
.password-toggle { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); }

.icon-btn {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.icon-btn:hover { background: var(--hover); color: var(--text); }

/* Como no Notion: o botão de recolher só aparece com o rato sobre a barra */
.collapse-btn { opacity: 0; transition: opacity 120ms; }
.sidebar:hover .collapse-btn,
.collapse-btn:focus-visible { opacity: 1; }

.nav {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 12px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 30px;
  padding: 0 8px;
  border-radius: 6px;
  color: var(--text);
  font-size: 14px;
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
}

.nav-item:hover { background: var(--hover); }
.nav-item.active { background: var(--hover); font-weight: 600; }

.nav-icon {
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
  color: var(--text-muted);
}

.nav-item:hover .nav-icon,
.nav-item.active .nav-icon { color: var(--text); }

/* ---------- Grupo em persiana (Definições) ---------- */
.nav-group-toggle {
  width: 100%;
  border: none;
  background: transparent;
  font: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}

.chevron {
  margin-left: auto;
  color: var(--text-muted);
  transition: transform var(--ease);
}

.nav-group.open > .nav-group-toggle .chevron { transform: rotate(90deg); }

.nav-group-items {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows var(--ease);
}

.nav-group.open > .nav-group-items { grid-template-rows: 1fr; }

.nav-group-inner { overflow: hidden; }

.nav-subitem { padding-left: 28px; }
.nav-subitem-2 { padding-left: 48px; }

.nav-section {
  margin: 16px 8px 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

.resizer {
  position: absolute;
  top: 0;
  right: -3px;
  width: 6px;
  height: 100%;
  cursor: col-resize;
}

.resizer:hover,
.app.resizing .resizer { background: rgba(35, 131, 226, 0.4); }

.app.resizing { cursor: col-resize; user-select: none; }
.app.resizing .sidebar { transition: none; }

/* ---------- Estado recolhido ---------- */
.app.collapsed .sidebar {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  transform: translateX(-100%);
  opacity: 0;
  pointer-events: none;
}

/* Espreitar: barra flutuante ao passar o rato sobre o botão ☰ */
.app.collapsed.peek .sidebar {
  top: 50px;
  height: calc(100% - 100px);
  border: none;
  border-radius: 0 8px 8px 0;
  box-shadow: var(--shadow);
  transform: translateX(0);
  opacity: 1;
  pointer-events: auto;
}

.app.collapsed .resizer { display: none; }

/* ---------- Main ---------- */
.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 45px;
  padding: 0 12px;
}

.open-btn { display: none; }
.app.collapsed .open-btn { display: grid; }

.open-btn .icon-expand { display: none; }
.open-btn:hover .icon-menu { display: none; }
.open-btn:hover .icon-expand { display: block; }

.breadcrumb { font-size: 14px; }

.content {
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  padding: 48px 24px;
  overflow-y: auto;
  line-height: 1.6;
}

.content h1 { font-size: 40px; margin: 0 0 16px; }

kbd {
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--sidebar-bg);
  font-family: inherit;
  font-size: 0.85em;
}

/* Ecrãs pequenos: a barra aberta fica por cima do conteúdo */
@media (max-width: 640px) {
  .app:not(.collapsed) .sidebar {
    position: absolute;
    height: 100%;
    box-shadow: var(--shadow);
  }
  .collapse-btn { opacity: 1; }
}

/* ---------- Aspecto: escolha de tema ---------- */
.setting-title { font-size: 16px; margin: 32px 0 4px; }
.setting-desc { margin: 0 0 16px; font-size: 14px; color: var(--text-muted); }

.theme-options { display: flex; flex-wrap: wrap; gap: 16px; }

.theme-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.theme-option input { position: absolute; opacity: 0; pointer-events: none; }

.theme-preview {
  display: flex;
  width: 160px;
  height: 100px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  outline: 2px solid transparent;
  outline-offset: 2px;
  transition: outline-color 120ms;
}

.theme-option:hover .theme-preview { outline-color: var(--border); }
.theme-option input:checked + .theme-preview { outline-color: #2383e2; }
.theme-option input:focus-visible + .theme-preview { outline-color: #2383e2; outline-style: dashed; }

.tp-side { width: 32%; }
.tp-main { flex: 1; display: flex; flex-direction: column; gap: 6px; padding: 14px 12px; }
.tp-main span { height: 6px; border-radius: 3px; }
.tp-main span:first-child { width: 60%; height: 10px; }
.tp-main span:nth-child(3) { width: 80%; }

.theme-preview-light { background: #ffffff; }
.theme-preview-light .tp-side { background: #f7f7f5; border-right: 1px solid rgba(55, 53, 47, 0.09); }
.theme-preview-light .tp-main span { background: rgba(55, 53, 47, 0.25); }

.theme-preview-dark { background: #191919; }
.theme-preview-dark .tp-side { background: #202020; border-right: 1px solid rgba(255, 255, 255, 0.08); }
.theme-preview-dark .tp-main span { background: rgba(255, 255, 255, 0.3); }

.theme-label { font-size: 14px; }

[hidden] { display: none !important; }

/* ---------- Botões e campos ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover { background: var(--hover); }
.btn .nav-icon { width: 16px; height: 16px; color: currentColor; }

.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { color: var(--danger); }
.btn-block { width: 100%; }

.field { display: flex; flex-direction: column; gap: 6px; flex: 1; min-width: 0; margin: 0 0 16px; padding: 0; border: none; }
.field > span, .field > legend { font-size: 13px; font-weight: 500; color: var(--text-muted); padding: 0; margin-bottom: 6px; }
.field > span { margin-bottom: 0; }

.field input,
.field select {
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
}

.field select { padding-right: 6px; }

/* A lista que abre nas combobox é desenhada pelo sistema: as cores têm de ser explícitas,
   senão no modo escuro fica texto claro sobre fundo branco */
select option,
select optgroup {
  background-color: var(--bg);
  color: var(--text);
}

select option:disabled { color: var(--text-muted); }
.dialog .field input[readonly] { background: transparent; border-style: dashed; color: var(--text-muted); cursor: default; }
.dialog .field input[readonly]:focus { outline: none; border-color: var(--border); }

.field input:focus,
.field select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.field-hint { font-size: 12px; color: var(--text-muted); }
.field-row { display: flex; gap: 16px; }

.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: -4px 0 20px;
  font-size: 14px;
  cursor: pointer;
}

.checkbox input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }

.form-error { min-height: 1em; margin: 0 0 12px; font-size: 13px; color: var(--danger); }
.form-error:empty { display: none; }

/* Escolha múltipla em forma de "chips" */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }

.chip { position: relative; cursor: pointer; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }

.chip span {
  display: inline-block;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px;
  user-select: none;
}

.chip:hover span { background: var(--hover); }
.chip input:checked + span { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Login ---------- */
.login {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 16px;
  background: var(--sidebar-bg);
}

.login-card {
  width: 100%;
  max-width: 360px;
  padding: 32px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  box-shadow: 0 8px 30px rgba(15, 15, 15, 0.08);
}

.login-card h1 { font-size: 22px; margin: 16px 0 24px; }
.login-logo { width: 36px; height: 36px; font-size: 18px; border-radius: 8px; }

.company-picker-hint { margin: -12px 0 16px; }
.company-picker-list { display: grid; gap: 8px; margin-bottom: 8px; }
.company-picker-list .btn { justify-content: flex-start; gap: 10px; text-align: left; }
.company-picker-list .workspace-icon { flex: none; }

/* ---------- Rodapé da barra lateral (utilizador ativo) ---------- */
.sidebar-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 8px 8px 12px;
  border-top: 1px solid var(--border);
}

.current-user { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }

.avatar {
  display: grid;
  place-items: center;
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--hover);
  font-size: 12px;
  font-weight: 600;
}

.current-user-info { display: flex; flex-direction: column; min-width: 0; }
.current-user-name, .current-user-email { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.current-user-name { font-size: 13px; font-weight: 600; }
.current-user-email { font-size: 12px; color: var(--text-muted); }

/* ---------- Utilizadores ---------- */
.content:has(.page[data-view="Utilizadores"]:not([hidden])),
.content:has(.page[data-view="Clientes"]:not([hidden])),
.content:has(.page[data-view="Contactos"]:not([hidden])),
.content:has(.page[data-view="Fornecedores"]:not([hidden])) { max-width: 1040px; }

/* A listagem de projetos ocupa toda a área à direita do menu lateral, em qualquer vista */
.content:has(.page[data-view="Projetos"]:not([hidden])) { max-width: none; padding: 32px 32px 48px; }
/* A pesquisa de projetos fica na barra do topo, ao lado do caminho, e só aparece na listagem */
.topbar .topbar-search { display: none; flex: 0 1 420px; max-width: 420px; min-width: 0; margin-left: 12px; }
/* O botão "Novo projeto" fica na mesma linha, à direita */
.topbar .topbar-action,
.topbar .topbar-views { display: none; }
body:has(.page[data-view="Projetos"]:not([hidden])) .topbar .topbar-search { display: block; }
body:has(.page[data-view="Projetos"]:not([hidden])) .topbar .topbar-action,
body:has(.page[data-view="Projetos"]:not([hidden])) .topbar .topbar-views { display: inline-flex; }
body:has(.page[data-view="Projetos"]:not([hidden])) .topbar .topbar-views { margin-right: 6px; }

/* Título "Projetos" no topo: negrito e amarelo */
body:has(.page[data-view="Projetos"]:not([hidden])) .breadcrumb { font-weight: 700; color: var(--title-accent); }
/* Alinha o botão com a margem direita da listagem */
body:has(.page[data-view="Projetos"]:not([hidden])) .topbar { padding-right: 32px; height: 56px; }
.content:has(.page[data-view="Projetos"]:not([hidden])) { padding-top: 8px; }

/* Na listagem de projetos não se mostra o título "Projetos" (fica só no topo, no caminho) */
.content:has(.page[data-view="Projetos"]:not([hidden])) > h1 { display: none; }

.page-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.page-toolbar .setting-desc { margin: 0; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; }

.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 10px 12px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
.table th { font-size: 12px; font-weight: 600; color: var(--text-muted); white-space: nowrap; }
.table tbody tr:last-child td { border-bottom: none; }

/* Cabeçalhos que ordenam a tabela */
.th-sort {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 0;
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

.th-sort:hover, .th-sort.is-active { color: var(--text); }
.th-sort-arrow { font-size: 11px; opacity: 0.4; }
.th-sort:hover .th-sort-arrow, .th-sort.is-active .th-sort-arrow { opacity: 1; }
.table tbody tr:hover { background: var(--hover); }
.table td:last-child { width: 1%; }
.table .empty { text-align: center; color: var(--text-muted); padding: 32px; }

.tags { display: flex; flex-wrap: wrap; gap: 4px; }
.tag { padding: 1px 8px; border-radius: 4px; background: var(--hover); font-size: 12px; white-space: nowrap; }
.tag-admin { background: var(--accent); color: #fff; }

/* ---------- Diálogo ---------- */
.dialog {
  width: min(600px, calc(100vw - 32px));
  padding: 0;
  border: none;
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  box-shadow: var(--shadow);
}

.dialog::backdrop { background: rgba(15, 15, 15, 0.45); }

.dialog-header, .dialog-footer { display: flex; align-items: center; gap: 8px; padding: 16px 20px; }
.dialog-header { justify-content: space-between; border-bottom: 1px solid var(--border); }
.dialog-header h2 { margin: 0; font-size: 16px; }
.dialog-body { padding: 20px 20px 4px; max-height: 65vh; overflow-y: auto; }
.dialog-footer { border-top: 1px solid var(--border); }
.spacer { flex: 1; }

@media (max-width: 640px) {
  .field-row { flex-direction: column; gap: 0; }
  .page-toolbar { flex-direction: column; align-items: stretch; }
}

/* ---------- Projetos ---------- */
.table-clickable tbody tr { cursor: pointer; }
.table .num { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.table .nowrap { white-space: nowrap; }

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 48px 16px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 14px;
}

.empty-state p { margin: 0; }

.empty-icon {
  width: 32px;
  height: 32px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  white-space: nowrap;
  background: var(--hover);
}

.badge-alta { background: rgba(217, 115, 13, 0.18); color: #c4650a; }
.badge-urgente { background: rgba(235, 87, 87, 0.18); color: var(--danger); }
.badge-baixa { color: var(--text-muted); }
/* Cor de cada estado: a mesma na listagem, nos cartões e na folha do projeto */
.state-draft { --pill-bg: #f6c1d4; --pill-border: #fadbe6; --pill-text: #6e2445; }
.state-analysis { --pill-bg: #bfdcf5; --pill-border: #dcecfa; --pill-text: #1d4a72; }

.badge-state {
  padding: 2px 9px;
  border: 1px solid var(--pill-border, var(--border));
  background: var(--pill-bg, transparent);
  color: var(--pill-text, var(--text));
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.dialog .field-hint { display: block; margin: -4px 0 12px; }
.table .strong { font-weight: 500; }
.table .project-name { font-size: 15px; font-weight: 700; color: var(--title-accent); }

/* Folha em ecrã inteiro (projetos) */
/* Abre sem ser modal, para o menu lateral continuar visível e utilizável.
   Fica abaixo da barra lateral (z-index 20) para o "espreitar" aparecer por cima. */
.dialog-full {
  position: fixed;
  inset: 0;
  z-index: 15;
  width: auto;
  max-width: none;
  height: auto;
  max-height: none;
  margin: 0;
  border-radius: 0;
  box-shadow: none;
}

/* Com o menu lateral fixo, a folha começa onde o menu acaba */
@media (min-width: 641px) {
  .app:not(.collapsed) ~ .dialog-full { left: var(--sidebar-width); }
}

.dialog-full[open] { display: flex; }
.dialog-full form { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.dialog-full .dialog-header { padding: 12px 24px; }
.dialog-full .dialog-header h2 { font-size: 18px; }
.dialog-full .dialog-body { flex: 1; max-height: none; padding: 12px 24px 32px; }
.dialog-full .dialog-body > * { max-width: 960px; margin-left: auto; margin-right: auto; }
.dialog-full .dialog-footer { padding: 12px 24px; }

/* ---------- Tabelas das Definições ---------- */
.list-editor { max-width: 520px; }

.list-editor-items {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.list-editor-items:empty { display: none; }

.list-editor-item {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px 6px 4px 4px;
  border-bottom: 1px solid var(--border);
}

.list-editor-item:last-child { border-bottom: none; }

.list-editor-item input,
.list-editor-add input {
  flex: 1;
  min-width: 0;
  height: 32px;
  padding: 0 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 14px;
}

.list-editor-item input:hover { background: var(--hover); }
.list-editor-item input:focus,
.list-editor-add input:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: var(--input-bg); }

.list-editor-item .icon-btn:disabled { opacity: 0.3; cursor: default; background: transparent; }

.list-editor-empty {
  margin: 0 0 12px;
  padding: 24px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  text-align: center;
  font-size: 14px;
  color: var(--text-muted);
}

.list-editor-add { display: flex; gap: 8px; margin-bottom: 8px; }
.list-editor-add input { border-color: var(--border); background: var(--input-bg); height: 32px; }

/* ---------- Tabelas da Gestão de Serviços ---------- */
.search-input {
  flex: 1;
  max-width: 320px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
}

.search-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.record-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  column-gap: 16px;
}

.record-fields .field-wide { grid-column: 1 / -1; }

.field textarea {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  resize: vertical;
}

.field textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

/* ---------- Texto formatado (descrição do projeto) ---------- */
.rich-editor {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input-bg);
}

.rich-editor:focus-within { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

.rich-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px;
  padding: 4px;
  border-bottom: 1px solid var(--border);
}

.rich-toolbar .icon-btn { font-size: 14px; font-family: Georgia, serif; }
.rich-toolbar .nav-icon { width: 16px; height: 16px; }
.rich-sep { width: 1px; height: 18px; margin: 0 4px; background: var(--border); }

.rich-popover {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 6px;
  border-bottom: 1px solid var(--border);
}

.field .rich-popover input,
.field .rich-popover select { flex: 1; min-width: 180px; height: 32px; }
.rich-popover .btn { height: 32px; }

.rich-text {
  min-height: 110px;
  max-height: 420px;
  overflow-y: auto;
  padding: 8px 10px;
  font-size: 14px;
  line-height: 1.5;
  outline: none;
  overflow-wrap: anywhere;
}

.rich-text:empty::before { content: "Escreve a descrição…"; color: var(--text-muted); }
.rich-text p { margin: 0 0 8px; }
.rich-text ul, .rich-text ol { margin: 4px 0 8px; padding-left: 24px; }
.rich-text a { color: var(--accent); text-decoration: underline; }
.rich-text a.project-link { text-decoration: none; padding: 0 3px; border-radius: 4px; background: var(--hover); }
.rich-text a.project-link::before { content: "↗ "; font-size: 12px; }

@media (max-width: 640px) {
  .record-fields { grid-template-columns: 1fr; }
  .search-input { max-width: none; }
}

/* ---------- Favoritos ---------- */
.star-btn {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.star-btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linejoin: round;
}

.star-btn:hover { background: var(--hover); color: var(--text); }
.star-btn[aria-pressed="true"] { color: #f5b301; }
.star-btn[aria-pressed="true"] svg { fill: #f5b301; }

.table .star-col { width: 1%; padding: 4px 0 4px 8px; vertical-align: middle; }
.dialog-title { display: flex; align-items: center; gap: 6px; min-width: 0; }

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

/* ---------- Imagem do projeto ---------- */
.image-field { display: flex; align-items: flex-start; gap: 16px; flex-wrap: wrap; }

.image-preview {
  display: grid;
  place-items: center;
  width: 240px;
  height: 160px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: var(--input-bg);
  overflow: hidden;
}

.image-preview img { width: 100%; height: 100%; object-fit: contain; }

.image-empty {
  width: 40px;
  height: 40px;
  fill: none;
  stroke: var(--text-muted);
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.image-actions { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; }

/* ---------- Filtros da listagem de projetos ---------- */
.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px 12px;
  margin-bottom: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.filter { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.filter > span { font-size: 12px; font-weight: 500; color: var(--text-muted); }

.filter input,
.filter select {
  height: 30px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  max-width: 180px;
}

.filter input:focus,
.filter select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.filter-check { flex-direction: row; align-items: center; gap: 6px; height: 30px; cursor: pointer; }
.filter-check input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }
.filter-check > span { font-size: 13px; color: var(--text); }

.filters .btn { height: 30px; font-size: 13px; }
.filters .btn:disabled { opacity: 0.5; cursor: default; background: transparent; }
.projects-summary { margin: 0 0 8px; }

@media (max-width: 640px) {
  .filter { flex: 1 1 140px; }
  .filter input, .filter select { max-width: none; }
}

/* ---------- Vistas da listagem de projetos ---------- */
.projects-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-bottom: 12px;
}

.projects-bar .projects-summary { margin: 0; flex: 1; }
.filter-inline { flex-direction: row; align-items: center; gap: 6px; }

.view-switch {
  display: inline-flex;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.view-switch button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 10px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

.view-switch button:hover { color: var(--text); }
.view-switch button[aria-checked="true"] { background: var(--hover); color: var(--text); font-weight: 500; }
.view-switch .nav-icon { width: 16px; height: 16px; color: currentColor; }

/* Cartões: um projeto por linha */
.project-cards { display: flex; flex-direction: column; gap: 12px; }

.project-card {
  position: relative;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  overflow: hidden;
  cursor: pointer;
  transition: box-shadow 120ms, border-color 120ms;
}

.project-card:hover { box-shadow: 0 4px 16px rgba(15, 15, 15, 0.12); }
.project-card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.project-card-image {
  display: grid;
  place-items: center;
  aspect-ratio: 16 / 9;
  background: var(--sidebar-bg);
  border-bottom: 1px solid var(--border);
}

/* A imagem preenche o espaço do cartão sem o esticar (sem isto, a altura da foto ganhava à proporção) */
.project-card-image { position: relative; overflow: hidden; }
.project-card-image img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.project-card-image .image-empty { width: 32px; height: 32px; }

.project-card-star { position: absolute; top: 6px; right: 6px; }
.project-card-star .star-btn { background: var(--bg); box-shadow: 0 1px 3px rgba(15, 15, 15, 0.2); }

.project-card-body { display: flex; flex-direction: column; gap: 4px; padding: 10px 12px 12px; min-width: 0; }
.project-card-meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-muted); }
.project-card-meta .num { font-weight: 600; color: var(--text); font-variant-numeric: tabular-nums; }
.project-card-title { margin: 0; font-size: 15px; font-weight: 700; line-height: 1.3; overflow-wrap: anywhere; color: var(--title-accent); }
.project-card-line { margin: 0; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.project-card-line.muted { color: var(--text-muted); }
.project-card-badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }

.project-row { flex-direction: row; align-items: center; gap: 18px; padding: 14px 18px; }
.project-row-image { flex: none; width: 60px; height: 60px; border-radius: 8px; overflow: hidden; background: var(--sidebar-bg); }
.project-row-image img { width: 100%; height: 100%; object-fit: cover; }
.project-row-body { display: flex; flex-direction: column; gap: 4px; flex: 1; min-width: 0; }
.project-row-title {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--title-accent);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.project-row-title .num { color: var(--text); font-variant-numeric: tabular-nums; }
.project-row-details {
  margin: 0;
  font-size: 13px;
  text-transform: uppercase;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.project-row-details .sep { opacity: 0.6; }
.project-row-side { display: flex; align-items: center; gap: 10px; flex: none; }
.project-row-resp {
  padding: 3px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.project-row-resp .muted { font-weight: 400; }

@media (max-width: 760px) {
  .project-row { flex-wrap: wrap; gap: 12px; padding: 12px; }
  .project-row-body { flex-basis: calc(100% - 72px); }
  .project-row-title, .project-row-details { white-space: normal; }
  .project-row-side { width: 100%; justify-content: flex-end; }
}

/* Kanban */

.kanban {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 8px;
}

.kanban-column {
  flex: 0 0 272px;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 260px);
  border-radius: 10px;
  background: var(--sidebar-bg);
  border: 1px solid var(--border);
}

.kanban-column-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 600;
}

.kanban-count {
  min-width: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--hover);
  font-size: 12px;
  font-weight: 500;
  text-align: center;
  color: var(--text-muted);
}

.kanban-column-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 0 8px 8px;
  overflow-y: auto;
}

/* Os cartões não encolhem para caber na coluna: a coluna passa a ter deslocamento */
.kanban-column-body > .project-card { flex-shrink: 0; }
.project-card-compact .project-card-image { aspect-ratio: 2 / 1; }

@media (max-width: 640px) {
  .view-switch span { display: none; }
}

@media (max-width: 640px) {
  .content:has(.page[data-view="Projetos"]:not([hidden])) { padding: 24px 16px 32px; }
}

/* Campos só de leitura do projeto no cabeçalho da folha, na linha do título */
/* Data e "Criado por" junto do título; o Estado fica encostado à direita */
.dialog-header-fields {
  flex: 1;
  justify-content: flex-start;
  gap: 12px;
  margin: 0 8px 0 24px;
  min-width: 0;
}

.dialog-header-fields .field.header-state { margin-left: auto; align-self: center; }

/* Estado: pílula na cor do estado, em maiúsculas e negrito, do tamanho do texto */
.dialog-header-fields .field.header-state { flex: 0 0 auto; }

.dialog .dialog-header-fields .header-state input {
  field-sizing: content;
  height: auto;
  padding: 5px 14px;
  border: 2px solid var(--pill-border, var(--border));
  border-radius: 999px;
  background: var(--pill-bg, var(--hover));
  color: var(--pill-text, var(--text));
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-align: center;
  cursor: default;
}

.dialog-header-fields .field { flex: 0 1 160px; margin: 0; gap: 2px; }
.dialog-header-fields .field > span { font-size: 11px; }
.dialog-header-fields .field input { height: 28px; font-size: 13px; padding: 0 8px; }
.dialog-full .dialog-header .dialog-title { flex-shrink: 0; }

@media (max-width: 900px) {
  .dialog-full .dialog-header { flex-wrap: wrap; }
  .dialog-header-fields { order: 3; flex-basis: 100%; flex-direction: row; flex-wrap: wrap; margin: 8px 0 0; justify-content: flex-start; }
  .dialog-header-fields .field { flex: 1 1 120px; }
}

/* ---------- Bloqueio da folha de projeto ---------- */
.field > label { font-size: 13px; font-weight: 500; color: var(--text-muted); }

.field-with-action { display: flex; align-items: center; gap: 6px; }
.field-with-action input { flex: 1; min-width: 0; }

.lock-btn { width: 36px; height: 36px; flex-shrink: 0; border: 1px solid var(--border); }
.lock-btn[aria-pressed="true"] { color: var(--title-accent); }
.lock-btn[aria-pressed="false"] { color: var(--accent); }

/* Bloqueado: os campos leem-se normalmente, mas não se editam */
.is-locked .dialog-body input:disabled,
.is-locked .dialog-body select:disabled,
.is-locked .dialog-body textarea:disabled {
  opacity: 1;
  color: var(--text);
  background: transparent;
  border-color: var(--border);
  cursor: default;
}

.is-locked .dialog-body select:disabled { appearance: none; }
.is-locked .rich-editor { background: transparent; }
.is-locked .rich-text:empty::before { content: "—"; }

/* Folha bloqueada: o nome e a descrição destacam-se do resto, como na listagem */
.is-locked .dialog-body #projectName:disabled {
  height: 42px;
  color: var(--title-accent);
  font-size: 20px;
  font-weight: 700;
}

.is-locked .rich-editor { border-left: 3px solid var(--title-accent); border-radius: 0 6px 6px 0; }
.is-locked .rich-text { min-height: 0; padding: 10px 14px; font-size: 15px; line-height: 1.6; color: var(--text); }
.btn:disabled,
.btn.is-disabled { opacity: 0.45; cursor: default; pointer-events: none; }

/* Botões + e − por cima da imagem do projeto */
.image-preview { position: relative; }

.image-overlay-actions {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  gap: 4px;
}

.image-action-btn {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 6px;
  background: rgba(15, 15, 15, 0.6);
  color: #fff;
  cursor: pointer;
}

.image-action-btn:hover { background: rgba(15, 15, 15, 0.8); }
.image-action-btn .nav-icon { width: 16px; height: 16px; color: currentColor; stroke-width: 2.25; }
.image-action-remove:hover { background: var(--danger); }

/* Com a folha bloqueada, os botões da imagem veem-se mas não funcionam */
.is-locked .image-action-btn { opacity: 0.5; cursor: not-allowed; }
.is-locked .image-action-btn:hover { background: rgba(15, 15, 15, 0.6); }

/* Imagem do projeto à esquerda das duas primeiras linhas de campos, logo abaixo do nome */
/* A imagem acompanha toda a coluna de campos (até ao fim da descrição) */
.project-main {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  column-gap: 20px;
  align-items: stretch;
}

.project-image-field { margin-bottom: 16px; }
.project-image-field .image-field { display: flex; flex: 1; min-height: 0; align-items: stretch; }
.project-image-field .image-preview { width: 100%; height: auto; min-height: 220px; flex: 1; }
/* A imagem preenche a caixa toda (pode cortar as margens se tiver outras proporções) */
.project-image-field .image-preview img { object-fit: cover; }

@media (max-width: 760px) {
  .project-main { grid-template-columns: 1fr; }
  .project-image-field .image-preview { height: 160px; }
}

/* ---------- Caixa "Análise Financeira" na folha de projeto ---------- */
.section-box {
  margin: 4px 0 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.section-box-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 8px 8px 14px;
  border-bottom: 1px solid var(--border);
}

.section-box-header h3 { margin: 0; font-size: 14px; font-weight: 600; }
.section-box-info { font-size: 12px; color: var(--text-muted); }
.section-box-header .lock-btn { width: 32px; height: 32px; }

.financial-row {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 8px 24px;
  padding: 12px 14px 14px;
}

.financial-row .financial-opinion { flex: 1 1 320px; margin: 0; }
.financial-row .checkbox { margin: 0; height: 36px; white-space: nowrap; }

.done-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

.done-btn .nav-icon { width: 16px; height: 16px; color: currentColor; stroke-width: 2.5; }
.done-btn:hover:not(:disabled) { background: var(--hover); color: var(--text); }
.done-btn[aria-pressed="true"] { border-color: #2e9e5b; background: rgba(46, 158, 91, 0.15); color: #2e9e5b; font-weight: 500; }
.done-btn:disabled { cursor: default; }
.done-btn:disabled:not([aria-pressed="true"]) { opacity: 0.5; }
.lock-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Caixa bloqueada: os campos leem-se normalmente, mas não se editam */
.section-box.is-locked input[type="text"]:disabled {
  opacity: 1;
  color: var(--text);
  background: transparent;
  cursor: default;
}

.section-box.is-locked .checkbox { cursor: default; }
.section-box.is-locked .checkbox input:disabled { opacity: 0.8; }

/* Botão "Avançar para análise", ao lado de Guardar */
.btn-advance { background: #2e9e5b; border-color: #2e9e5b; color: #fff; font-weight: 500; }
.btn-advance:hover { background: #268a4e; border-color: #268a4e; }

.muted { color: var(--text-muted); }

/* ---------- OneDrive na ficha da empresa ---------- */
.onedrive-fields .field-row .field { flex: 1; }
.onedrive-form { max-width: 520px; }
.onedrive-test { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.onedrive-test .ok { color: #2e9e5b; }
.onedrive-test .fail { color: var(--danger); }

/* ---------- Ficheiros do projeto ---------- */
/* Com a janela aberta, da folha só fica o nome do projeto */
.dialog-body.files-open > :not(.project-name-field, .files-panel) { display: none; }
.files-btn[aria-pressed="false"] { color: var(--text-muted); }
.files-btn[aria-pressed="true"] { background: var(--hover); color: var(--accent); border-color: var(--accent); }

.files-panel { display: flex; flex-direction: column; gap: 12px; }
.files-panel[hidden] { display: none; }

.files-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.files-toolbar .nav-icon { width: 16px; height: 16px; color: currentColor; }
.files-count { color: var(--text-muted); font-size: 14px; }

.files-list { display: flex; flex-direction: column; gap: 8px; }
.files-list.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }

.file-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.file-icon { flex: none; width: 32px; font-size: 26px; line-height: 1; text-align: center; }
.file-info { display: flex; flex-direction: column; gap: 2px; flex: 1; min-width: 0; }
.file-name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-meta { color: var(--text-muted); font-size: 12px; }
.file-actions { display: flex; gap: 6px; }
.file-actions .icon-btn { width: 32px; height: 32px; border: 1px solid var(--border); }
.file-actions .nav-icon { width: 16px; height: 16px; color: currentColor; }
.file-actions .file-preview { color: var(--accent); }
.file-actions .file-delete { color: var(--danger); }

/* Mosaico: miniatura (ou ícone do tipo) com o nome e o tamanho por baixo */
.file-open {
  display: flex;
  align-items: center;
  gap: 14px;
  flex: 1;
  min-width: 0;
  padding: 0;
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.file-open:hover .file-name { text-decoration: underline; }
.file-actions .icon-btn:disabled { opacity: 0.5; cursor: progress; }

.file-open:has(.file-info[hidden]) { flex: none; }
form.file-rename-form { display: flex; flex-direction: row; flex: 1; min-width: 0; }
.file-rename-form input {
  flex: 1;
  min-width: 0;
  padding: 6px 10px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--input-bg);
  color: inherit;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
}

.files-list.grid .file-item { flex-direction: column; align-items: stretch; gap: 0; padding: 0; overflow: hidden; }
.files-list.grid .file-open { flex-direction: column; align-items: stretch; gap: 0; }
.files-list.grid .file-icon {
  display: grid;
  place-items: center;
  width: auto;
  aspect-ratio: 1;
  font-size: 48px;
  background: var(--input-bg);
}
.files-list.grid .file-icon img { width: 100%; height: 100%; object-fit: cover; }
.files-list.grid .file-info { padding: 8px 10px; }
.files-list.grid .file-actions { padding: 0 10px 10px; }
.files-list.grid .file-actions .icon-btn { width: 28px; height: 28px; }
.files-list.grid .file-rename-form { flex: none; padding: 8px 10px 10px; }

/* Janela de carregar ficheiros */
.upload-drop {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 28px 16px;
  border: 1px dashed var(--border);
  border-radius: 10px;
  color: var(--text-muted);
  font-size: 14px;
  text-align: center;
  cursor: pointer;
}
.upload-drop:hover, .upload-drop.dragging { border-color: var(--accent); color: var(--text); }
.upload-drop[hidden] { display: none; }
.upload-list { display: flex; flex-direction: column; gap: 6px; margin: 12px 0 0; padding: 0; list-style: none; }
.upload-list:empty { display: none; }
.upload-list li { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; }
.upload-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-status { font-size: 13px; white-space: nowrap; }
.upload-status.ok { color: #2e9e5b; }
.upload-status.fail { color: var(--danger); white-space: normal; }
