:root {
  --bg: #f1f1ef; --ink: #111; --mut: #5a5a5a; --line: #dcdcd8; --line2: #c9c9c4;
  --paper: #fff; --panel: #f6f6f4; --stage: #4b4b4b;
  --ok: #3f7a24; --okbg: #eaf3e3; --bad: #b3261e; --badbg: #fbe9e7; --badink: #8f1d17;
  --ins: #d9efc9; --del: #f7d4cf;
  --mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo;
}
* { box-sizing: border-box; min-width: 0; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.5 Montserrat, system-ui, sans-serif; }
button, input { font-family: inherit; }
a { color: inherit; }
.page { padding: 22px clamp(16px, 4vw, 44px) 48px; }
.wrap { max-width: 1240px; margin: 0 auto; display: flex; flex-direction: column; gap: 20px; }
.mono { font-family: var(--mono, monospace), monospace; }
.muted { color: var(--mut); }
.small { font-size: 13px; }
h2 { margin: 0 0 10px; font-size: 16px; letter-spacing: -0.01em; }

/* topo */
.top { display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; }
.logo { font-weight: 700; font-size: 22px; letter-spacing: -0.02em; }
.title { font-weight: 600; font-size: 18px; color: var(--mut); }
.top-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.btn-plain { display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 14px; border: 1px solid var(--line2); border-radius: 8px; background: #fff; color: var(--ink); font-size: 15px; font-weight: 600; cursor: pointer; }
.btn-plain .muted { font-weight: 500; }
.menu-wrap { position: relative; }
.menu { position: absolute; right: 0; top: 52px; z-index: 5; min-width: 300px; background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 16px 36px -16px rgba(0,0,0,.3); padding: 6px; display: flex; flex-direction: column; }
.menu button { display: flex; justify-content: space-between; gap: 12px; align-items: center; min-height: 44px; padding: 0 12px; border: 0; border-radius: 6px; background: #fff; color: var(--ink); font-size: 15px; font-weight: 500; text-align: left; cursor: pointer; }
.menu button[aria-selected="true"] { background: var(--bg); }
.menu button[aria-selected="true"]::after { content: "✓"; font-weight: 700; }
.menu button:hover { background: var(--panel); }

/* grelha */
.grid { display: grid; grid-template-columns: minmax(0, 1.75fr) minmax(320px, 1fr); gap: 20px; align-items: start; }
@media (max-width: 920px) { .grid { grid-template-columns: minmax(0, 1fr); } }
.card { background: var(--paper); border: 1px solid var(--line); border-radius: 10px; }
.card.pad { padding: 16px; }
.input { overflow: hidden; }
.side { display: flex; flex-direction: column; gap: 16px; }

/* barra do input */
.input-bar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.tabs { display: flex; gap: 4px; background: #ececea; border-radius: 8px; padding: 3px; }
.tabs button { min-height: 38px; padding: 0 16px; border: 0; border-radius: 6px; background: transparent; color: var(--ink); font-size: 14px; font-weight: 600; cursor: pointer; }
.tabs button[aria-selected="true"] { background: var(--ink); color: #fff; }
.file-actions { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; font-size: 13px; }
.btn-file { display: inline-flex; align-items: center; min-height: 38px; padding: 0 12px; border: 1px solid var(--line2); border-radius: 6px; font-size: 14px; font-weight: 600; cursor: pointer; background: #fff; }
.btn-file input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.btn-file:focus-within { outline: 2px solid var(--ink); outline-offset: 2px; }
.btn-file.light { min-height: 44px; padding: 0 18px; border: 0; border-radius: 8px; font-size: 15px; color: var(--ink); }

/* visualizador */
.viewer-area { background: var(--stage); }
.empty { padding: 28px clamp(12px, 3vw, 32px); }
.empty-box { max-width: 620px; min-height: 720px; margin: 0 auto; border: 2px dashed #8a8a8a; border-radius: 10px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 32px; color: #f1f1ef; text-align: center; }
.empty-title { font-weight: 700; font-size: 18px; }
.empty-sub { font-size: 14px; color: #d6d6d2; max-width: 34ch; }
.empty-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.btn-ghost-dark { min-height: 44px; padding: 0 18px; border: 1px solid #d6d6d2; border-radius: 8px; background: transparent; color: #fff; font-size: 15px; font-weight: 600; cursor: pointer; }
.pdf-view { padding: 28px clamp(12px, 3vw, 32px); max-height: 900px; overflow: auto; }
.pages { display: flex; flex-direction: column; gap: 18px; align-items: center; }
.page-wrap { position: relative; box-shadow: 0 10px 30px rgba(0,0,0,.35); background: #fff; }
.page-wrap canvas { display: block; }
.hl { position: absolute; border-radius: 2px; pointer-events: none; }
.hl.new { background: rgba(114, 184, 74, .35); outline: 2px solid #3f7a24; }
.hl.old { background: transparent; }
.hl-tag { position: absolute; transform: translateY(-100%); margin-top: -6px; white-space: nowrap; font: 600 12px/1.3 Montserrat, sans-serif; background: #fff; border: 1px solid var(--line2); border-radius: 6px; padding: 4px 8px; box-shadow: 0 6px 16px -8px rgba(0,0,0,.4); }
.hl-tag del { background: var(--del); }
.hl-tag ins { background: var(--ins); text-decoration: none; }
.text-view { background: #fafaf9; padding: 24px; min-height: 776px; max-height: 900px; overflow: auto; }
.text-note { margin: 0 0 14px; font-size: 13px; color: var(--mut); }
.text-body { margin: 0; font: 13.5px/1.7 var(--mono, monospace), monospace; white-space: pre-wrap; overflow-wrap: anywhere; }
del { background: var(--del); }
ins { background: var(--ins); text-decoration: none; font-weight: 600; }
.diff-note { padding: 12px 16px; border-top: 1px solid var(--line); background: #fff; font-size: 14px; }
.diff-note p { margin: 6px 0 0; font-size: 13px; color: var(--mut); }

/* acções */
.actions { padding: 16px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; }
.action-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.btn-primary, .btn-secondary, .btn-danger { min-height: 46px; padding: 0 22px; border-radius: 8px; font-size: 15px; font-weight: 600; cursor: pointer; }
.btn-primary { border: 1px solid var(--ink); background: var(--ink); color: #fff; }
.btn-secondary { border: 1px solid var(--ink); background: #fff; color: var(--ink); }
.btn-danger { border: 1px solid var(--bad); background: #fff; color: var(--bad); padding: 0 18px; }
button:disabled { opacity: .4; cursor: not-allowed; }
button:focus-visible, a:focus-visible, input:focus-visible, summary:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.tamper { flex: 1 1 300px; display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; }
.tamper label { flex: 1 1 220px; display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--mut); }
.tamper input { min-height: 46px; padding: 0 12px; border: 1px solid var(--line2); border-radius: 8px; font-size: 15px; color: var(--ink); background: #fff; width: 100%; }
.hash { font-size: 12.5px; color: var(--mut); overflow-wrap: anywhere; }
.notice { margin: 0; padding: 10px 12px; border-radius: 6px; font-size: 14px; }
.notice.bad { background: var(--badbg); color: var(--badink); }
.notice.info { background: var(--panel); color: var(--ink); }
.notice.err { background: var(--badbg); color: var(--badink); }
.notice.wait { display: flex; gap: 12px; align-items: flex-start; background: #f3f3ee; border: 1px solid var(--line); font-weight: 600; }
.notice.wait .small { font-weight: 500; color: var(--mut); margin-top: 2px; }
.wait-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.spinner { width: 18px; height: 18px; border: 2.5px solid var(--line2); border-top-color: var(--ink); border-radius: 50%; animation: spin .9s linear infinite; flex: none; margin-top: 2px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }
.btn-mini { display: inline-flex; align-items: center; min-height: 36px; padding: 0 12px; border: 1px solid var(--line2); border-radius: 6px; background: #fff; color: var(--ink); font-size: 13px; font-weight: 600; cursor: pointer; text-decoration: none; }

/* lado direito */
.seg { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); border: 1px solid var(--line2); border-radius: 8px; overflow: hidden; }
.seg button { min-height: 48px; border: 0; background: #fff; color: var(--ink); font-size: 15px; font-weight: 600; cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--ink); color: #fff; }
.signer { margin: 10px 0 0; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 12px; }
.card-head h2 { margin: 0; }
.pill { font-size: 13px; font-weight: 700; padding: 4px 10px; border-radius: 99px; background: #ececea; color: var(--mut); white-space: nowrap; }
.pill.sig { background: var(--ink); color: #fff; }
.pill.ok { background: var(--okbg); color: var(--ok); }
.pill.bad { background: var(--badbg); color: var(--bad); }
.pill.wait { background: #f3eed9; color: #6b5300; }
.kv { display: grid; grid-template-columns: 112px minmax(0, 1fr); gap: 10px 12px; margin: 0; }
.kv dt { color: var(--mut); font-size: 14px; }
.kv dd { margin: 0; font-family: var(--mono, monospace), monospace; font-size: 13px; overflow-wrap: anywhere; }
.kv dd .sub { display: block; font-family: Montserrat, sans-serif; font-size: 12px; color: var(--mut); }
@media (max-width: 420px) { .kv { grid-template-columns: minmax(0, 1fr); gap: 2px; } .kv dd { margin-bottom: 8px; } }
.art-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.checks { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.checks li { display: grid; grid-template-columns: 22px minmax(0, 1fr); gap: 8px; padding: 8px 10px; background: var(--panel); border-radius: 6px; }
.checks .mark { font-weight: 700; }
.checks .mark.ok { color: var(--ok); }
.checks .mark.bad { color: var(--bad); }
.checks b { display: block; font-weight: 600; font-size: 14px; }
.checks span.d { font-size: 13px; color: var(--mut); overflow-wrap: anywhere; }
.verdict { margin-top: 10px; padding: 12px 14px; border-radius: 6px; font-weight: 600; }
.verdict.ok { background: var(--okbg); color: var(--ok); }
.verdict.bad { background: var(--badbg); color: var(--bad); }

/* registo */
.log summary { display: flex; justify-content: space-between; align-items: center; gap: 10px; cursor: pointer; list-style: none; }
.log summary::-webkit-details-marker { display: none; }
.log summary h2 { margin: 0; }
.log summary h2::before { content: "▸ "; color: var(--mut); }
.log[open] summary h2::before { content: "▾ "; }
.events { list-style: none; margin: 12px 0 0; padding: 0; max-height: 320px; overflow: auto; }
.events li { display: grid; grid-template-columns: 6.5em 7.5em minmax(0, 1fr); gap: 12px; padding: 8px 0; border-top: 1px solid var(--line); font-size: 13.5px; }
.events time, .events .t { color: var(--mut); font-variant-numeric: tabular-nums; }
@media (max-width: 600px) { .events li { grid-template-columns: 5.5em minmax(0, 1fr); } .events .t { display: none; } }
.foot { margin: 0; font-size: 13px; color: var(--mut); }

dialog { border: 1px solid var(--line); border-radius: 10px; padding: 20px; max-width: 460px; }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog p { font-size: 14px; }
.dialog-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
.diff-list { margin: 6px 0 0; padding-left: 18px; font-family: var(--mono, monospace), monospace; font-size: 13px; }

/* Controlos da integração com a API independente */
h1.title { margin: 0; }
button, input, select { font-family: inherit; }
.version { display: grid; gap: 3px; flex: 1 1 220px; color: var(--mut); font-size: 13px; }
.version select, .token-field input {
  min-height: 40px; width: 100%; padding: 0 10px;
  border: 1px solid var(--line2); border-radius: 6px; background: var(--paper); color: var(--ink);
}
.token-field { display: grid; gap: 6px; font-size: 14px; }
.empty-box { min-height: clamp(300px, 52vh, 540px); }
.empty-box p { margin: 0; }
.empty-box .btn-file { margin-top: 5px; }
.btn-file:has(input:disabled) { opacity: .55; cursor: not-allowed; }
.preview-toolbar, .pagination { display: flex; align-items: center; gap: 8px; }
.preview-toolbar { flex-wrap: wrap; justify-content: space-between; padding: 9px 14px; background: var(--panel); }
.pagination { font-size: 13px; }
.tabs button[aria-pressed="true"] { background: var(--ink); color: #fff; }
.pdf-view { min-height: 360px; max-height: 70vh; padding: 18px; }
.page-wrap { max-width: 100%; }
.page-wrap canvas { max-width: 100%; height: auto; }
.text-view { min-height: 360px; max-height: 70vh; }
#previewMessage { border-radius: 0; }
.verdict { margin: 0 0 10px; background: var(--panel); }
.verdict.ok { background: var(--okbg); }
.verdict.bad { background: var(--badbg); }
.log summary { font-weight: 600; }
.log summary span::before { content: "▸ "; color: var(--mut); }
.log[open] summary span::before { content: "▾ "; }
.events li { grid-template-columns: 8.5em 3.5em minmax(0, 1fr); overflow-wrap: anywhere; }
.events .t { display: block; }
@media (max-width: 600px) { .events li { grid-template-columns: minmax(0, 1fr); gap: 2px; } }

.login-page {
  display: grid;
  place-items: center;
  min-height: 100svh;
  padding: 24px 16px;
  background: #f5f6f9;
}
.login-card {
  width: min(100%, 410px);
  padding: clamp(24px, 6vw, 36px);
  border-color: #e0e2ea;
}
.login-brand { justify-content: space-between; color: #161744; }
.login-tablu { color: var(--mut); font-size: 14px; font-weight: 700; }
.login-card h1 { margin: 22px 0 18px; color: #161744; font-size: 23px; }
.login-card form { display: grid; gap: 13px; }
.login-card .btn-primary { background: #161744; border-color: #161744; margin-top: 4px; }
.login-switch {
  min-height: 40px;
  border: 0;
  padding: 0;
  margin: 12px auto 0;
  color: #161744;
  font-size: 13px;
}
.login-card .notice { margin-top: 12px; overflow-wrap: anywhere; }
#tokenForm { margin-top: 6px; }
#scenario {
  min-height: 40px;
  width: 100%;
  padding: 0 10px;
  border: 1px solid var(--line2);
  border-radius: 6px;
  color: var(--ink);
  background: var(--paper);
}
.otp-fields { margin: 0; padding: 0; border: 0; }
.otp-fields legend { margin-bottom: 10px; font-size: 14px; }
.otp-boxes { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 9px; }
.otp-boxes input {
  width: 100%; min-height: 48px; border: 1px solid var(--line2); border-radius: 6px;
  text-align: center; font-size: 20px; color: var(--ink);
}
#signerForm { display: grid; gap: 10px; }
#signerForm button { justify-self: start; }

/* Navegação da sandbox e auditoria. */
#workspace { --ink: #161744; --bg: #f5f6fa; background: var(--bg); }
#workspace .wrap { max-width: 1510px; }
.workspace-layout { display: grid; grid-template-columns: 225px minmax(0, 1fr); gap: 18px; align-items: start; }
.workspace-nav { position: sticky; top: 16px; }
.workspace-nav h2 { margin: 0 0 12px; }
.nav-row { display: flex; align-items: center; gap: 10px; width: 100%; margin-bottom: 7px; min-height: 42px; padding: 8px 10px; border: 1px solid #e5e6ef; border-radius: 8px; background: white; color: #161744; text-align: left; cursor: pointer; }
.nav-row:hover { background: #f1f0f8; }
.nav-row[aria-current="page"], .nav-row[aria-pressed="true"] { background: #161744; border-color: #161744; color: white; }
.nav-row.scenario-row::after { content: "›"; margin-left: auto; }
.nav-scenarios { margin-top: 16px; border-top: 1px solid #e5e6ef; padding-top: 16px; }
.scenario-generate { width: 100%; margin-top: 5px; padding-inline: 6px; min-height: 39px; }
#documentPane .grid { grid-template-columns: minmax(0, 1.5fr) minmax(260px, .85fr); }
#documentPane .log { margin-top: 18px; }
.signature-metadata { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--line); }
.signature-metadata h3, .audit-pane h3 { margin: 0 0 9px; font-size: 14px; }
.signature-metadata .btn-mini { margin-top: 10px; }
.audit-pane { display: grid; gap: 16px; }
.audit-search { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 14px; }
.audit-search label { flex-basis: 100%; font-size: 13px; }
.audit-search input { flex: 1 1 150px; min-height: 38px; border: 1px solid #dcdce8; border-radius: 7px; padding: 0 10px; }
.audit-documents, .history-list { list-style: none; padding: 0; margin: 0 0 12px; }
.audit-documents li + li { margin-top: 7px; }
.audit-record { width: 100%; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 3px 10px; text-align: left; padding: 11px 12px; border: 1px solid #e5e6ef; border-radius: 8px; background: white; color: #161744; cursor: pointer; }
.audit-record[aria-pressed="true"], .audit-record:hover { background: #f2f1f9; }
.audit-record strong, .history-list strong { overflow-wrap: anywhere; }
.audit-record .small { grid-column: 1; }
.audit-record .pill { grid-column: 2; grid-row: 1 / span 2; align-self: center; white-space: nowrap; }
.history-list li { display: grid; gap: 4px; padding: 8px 0; border-bottom: 1px solid #eeeef4; }
.history-list .btn-mini { justify-self: start; }
#historyFile { margin: 4px 0 0; overflow-wrap: anywhere; }
.timeline { padding-left: 14px; border-left: 2px solid #e8e6f2; }
#historyMore { margin-bottom: 14px; }
#signedDialog { width: min(390px, 92vw); max-width: none; padding: 26px; text-align: center; color: #161744; border-color: #e0e0ed; }
#signedDialog h2 { font-size: 22px; margin: 12px 0 20px; }
#signedDialog .btn-primary { width: 100%; background: #161744; border-color: #161744; }
.success-symbol { display: grid; place-items: center; width: 48px; height: 48px; margin: auto; border-radius: 50%; background: #e0f3d9; color: #067346; font-size: 25px; font-weight: 700; }
@media (max-width: 1120px) { #documentPane .grid { grid-template-columns: minmax(0, 1fr); } }
@media (max-width: 740px) {
  .workspace-layout { grid-template-columns: minmax(0, 1fr); }
  .workspace-nav { position: static; }
}
@media (max-width: 420px) {
  .audit-record { grid-template-columns: minmax(0, 1fr); }
  .audit-record .pill { grid-column: 1; grid-row: auto; justify-self: start; }
}

.password-field { display: grid; position: relative; }
.password-field input { padding-right: 48px; }
.password-toggle {
  display: grid; place-items: center; position: absolute; top: 1px; bottom: 1px;
  right: 1px; width: 42px; padding: 0; border: 0; border-radius: 5px;
  background: transparent; color: #161744; cursor: pointer;
}
.password-toggle .eye-slash { visibility: hidden; }
.password-toggle[aria-pressed="true"] .eye-slash { visibility: visible; }
.password-toggle:focus-visible { outline: 2px solid #161744; outline-offset: -3px; }
