:root {
  --bg: #f3f6f9;
  --card: #ffffff;
  --text: #2c3e50;
  --muted: #8a9aab;
  --line: #e3e8ee;
  --input: #ffffff;
  --input-line: #dfe5ec;
  --green: #1cbf73;
  --green-dark: #17a463;
  --red: #f04b4b;
  --tag-bg: #dfe5ec;
  --tag-text: #5c6b7a;
  --hover: #f5f8fb;
  --shadow: 0 1px 3px rgba(30, 50, 70, .06);
  --overlay: rgba(40, 50, 60, .45);
  --radius: 6px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Open Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.4;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--green); }
svg.lucide { width: 1em; height: 1em; vertical-align: -0.125em; }

.boot { padding: 80px; text-align: center; color: var(--muted); }

/* ---------- Cabeçalho ---------- */
.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 24px;
  padding: 0 24px; height: 64px;
  position: sticky; top: 0; z-index: 20;
}
.logo { font-weight: 700; font-size: 20px; color: var(--text); text-decoration: none; display: flex; align-items: center; gap: 8px; }
.logo-dot { width: 28px; height: 28px; border-radius: 50%; background: var(--green); color: #fff; display: grid; place-items: center; font-size: 15px; }
.nav { display: flex; gap: 4px; height: 100%; margin: 0 auto; }
.nav a {
  display: flex; align-items: center; padding: 0 18px; height: 100%;
  color: var(--muted); text-decoration: none; font-size: 17px; border-bottom: 3px solid transparent;
}
.nav a.active { color: var(--green); border-bottom-color: var(--green); }
.nav a:hover { color: var(--text); }
.user { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: #c0392b; color: #fff; display: grid; place-items: center; font-weight: 600; text-transform: uppercase; }
.local-banner { background: #fff7e0; color: #8a6d00; text-align: center; font-size: 13px; padding: 6px 12px; }
.local-banner button { background: none; border: 0; color: inherit; text-decoration: underline; cursor: pointer; padding: 0 4px; }

main { max-width: 1240px; margin: 0 auto; padding: 28px 24px 80px; }
h1 { font-size: 26px; margin: 8px 0 20px; }
h2 { font-size: 22px; margin: 32px 0 16px; }

/* ---------- Botões e campos ---------- */
.btn {
  border: 0; border-radius: 4px; padding: 0 16px; height: 42px; cursor: pointer;
  background: var(--card); color: var(--muted); display: inline-flex; align-items: center; gap: 8px;
  box-shadow: var(--shadow); white-space: nowrap;
}
.btn:hover { color: var(--text); }
.btn-primary { background: var(--green); color: #fff; font-weight: 600; box-shadow: 0 2px 6px rgba(28, 191, 115, .3); }
.btn-primary:hover { background: var(--green-dark); color: #fff; }
.btn-primary:disabled { background: #e3e8ee; color: var(--muted); box-shadow: none; cursor: default; }
.btn-danger { background: none; box-shadow: none; color: var(--muted); text-decoration: underline; }
.btn-danger:hover { color: var(--red); }
.btn-icon { width: 42px; padding: 0; justify-content: center; }
.btn-dark { background: #2f3e4c; color: #fff; font-weight: 600; }
.btn-dark:hover { color: #fff; background: #23303c; }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label, .label { font-size: 13px; color: var(--muted); }
.input, select.input {
  height: 46px; border: 1px solid var(--input-line); border-radius: 4px; background: var(--input);
  padding: 0 12px; width: 100%; min-width: 0; outline: none;
}
.input:focus { border-color: var(--green); }
.input:disabled { background: #f7f9fb; color: var(--muted); }
.input.num { text-align: right; font-weight: 600; }
.input::placeholder { color: #a5b2bf; }
.hint { font-size: 12px; color: var(--muted); }
.error-text { color: var(--red); font-size: 13px; }

/* ---------- Cartões ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; }
.grid4 { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 18px; }
.stat .label { font-size: 15px; color: var(--text); font-weight: 600; }
.stat .value { font-size: 26px; margin-top: 2px; }
.stat .value.small { font-size: 18px; }
.pos { color: var(--green); }
.neg { color: var(--red); }
.zero { color: var(--muted); }

/* ---------- Carteiras ---------- */
.wallets { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 18px; }
.wallet {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 22px 20px; display: flex; gap: 16px; align-items: center; cursor: pointer; position: relative; text-align: left; border: 0;
}
.wallet:hover { box-shadow: 0 3px 10px rgba(30, 50, 70, .1); }
.wallet.archived { opacity: .55; }
.wallet-icon { font-size: 26px; color: #a87250; }
.wallet-name { font-size: 17px; }
.wallet-kind { font-size: 13px; color: var(--muted); }
.wallet-balance { font-size: 22px; }
.wallet-gear {
  position: absolute; top: 8px; right: 8px; border: 0; background: none; color: #b9c4cf; cursor: pointer;
  width: 30px; height: 30px; border-radius: 4px; display: grid; place-items: center;
}
.wallet-gear:hover { color: var(--text); background: var(--hover); }
.wallet-add {
  background: var(--card); border: 0; border-radius: var(--radius); box-shadow: var(--shadow);
  color: var(--green); font-weight: 600; cursor: pointer; min-height: 48px; padding: 12px;
}
.wallet-add:hover { background: #f0fbf6; }

/* ---------- Transações ---------- */
.toolbar { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 18px; }
.toolbar .spacer { flex: 1; }
.period { display: flex; gap: 8px; align-items: center; }
.period-label { background: var(--card); box-shadow: var(--shadow); height: 42px; display: flex; align-items: center; padding: 0 14px; border-radius: 4px; min-width: 250px; justify-content: space-between; gap: 10px; }
.period-label input { border: 0; width: 22px; opacity: 0; position: absolute; }
.filters { margin-bottom: 18px; }
.grid4 > .span2 { grid-column: span 2; }
.filters-head { display: flex; justify-content: space-between; margin-bottom: 12px; font-weight: 600; }
.filters-head button { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 15px; }
.filters-head button:hover { color: var(--text); }
.summary { margin-bottom: 18px; }

.txlist { padding: 8px 20px; }
.day { padding: 12px 0; border-bottom: 1px solid var(--line); }
.day:last-child { border-bottom: 0; }
.day-head { display: flex; justify-content: space-between; font-weight: 600; padding: 6px 0 10px; }
.day-head .total { color: var(--muted); font-weight: 600; font-size: 14px; }
.txrow {
  display: grid; grid-template-columns: 44px minmax(140px, 1.1fr) minmax(0, 2fr) minmax(0, 1fr) 150px;
  gap: 12px; align-items: center; padding: 8px 6px; border-radius: 4px; cursor: pointer;
}
.txrow:hover { background: var(--hover); }
.txrow .cat-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.txrow .note { min-width: 0; }
.txrow .note-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.txrow .acc { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.txrow .amount { text-align: right; font-weight: 700; }
.txrow .sub { font-size: 12px; color: var(--muted); }
.pills { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 3px; }
.pill { background: var(--tag-bg); color: var(--tag-text); border-radius: 10px; padding: 0 8px; font-size: 12px; line-height: 19px; white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 50px 20px; }
.empty .btn { margin-top: 14px; }

.cat-badge {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: #fff; flex: none; font-size: 19px;
}
.cat-badge.sm { width: 28px; height: 28px; font-size: 14px; }
.cat-badge.empty-cat { background: #c9d2db; }

/* ---------- Painel de transação ---------- */
.overlay { position: fixed; inset: 0; background: var(--overlay); z-index: 50; overflow-y: auto; padding: 14px; }
.overlay-close { margin: 0 0 12px; }
.txpanel { background: var(--card); border-radius: 8px; padding: 22px 20px 20px; position: relative; max-width: 1440px; margin: 0 auto; }
.panel-x {
  position: absolute; top: -14px; right: -10px; width: 34px; height: 34px; border-radius: 50%; border: 0;
  background: #8597a8; color: #fff; cursor: pointer; display: grid; place-items: center; font-size: 18px;
}
.txgrid { display: grid; gap: 10px; align-items: end; }
.txgrid.row1 { grid-template-columns: 222px 222px minmax(160px, 1.4fr) minmax(160px, 1fr) 210px 105px; }
.txgrid.row2 { grid-template-columns: 1fr 1fr; margin-top: 14px; }
.txgrid.row2.fx { grid-template-columns: 1fr 1fr 1fr 1fr; }
.panel-foot { display: flex; align-items: center; gap: 12px; margin-top: 18px; flex-wrap: wrap; }
.panel-foot .spacer { flex: 1; }
.panel-foot .btn-primary { min-width: 222px; justify-content: center; }
.check { display: flex; align-items: center; gap: 10px; color: var(--muted); cursor: pointer; user-select: none; }
.check input { width: 22px; height: 22px; accent-color: var(--green); }
.panel-error { margin-top: 10px; }
.fx-hint { font-size: 12px; color: var(--muted); margin-top: 6px; text-align: right; }
.rate-wrap { display: flex; align-items: center; gap: 6px; }
.rate-wrap .unit { color: var(--muted); font-size: 13px; white-space: nowrap; }

/* Seletor (botão que abre popover) */
.picker {
  height: 46px; border: 1px solid var(--input-line); border-radius: 4px; background: var(--input);
  display: flex; align-items: center; gap: 10px; padding: 0 10px; cursor: pointer; width: 100%; text-align: left; min-width: 0;
}
.picker:focus, .picker.open { border-color: var(--green); outline: none; }
.picker .ph { color: #a5b2bf; }
.picker .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picker .chev { color: var(--muted); }
.picker .pills { flex-wrap: nowrap; overflow: hidden; margin: 0; }
.plus-badge { width: 32px; height: 32px; border-radius: 50%; background: var(--green); color: #fff; display: grid; place-items: center; font-size: 18px; flex: none; }

.popover {
  position: absolute; z-index: 80; background: var(--card); border: 1px solid var(--input-line); border-radius: 4px;
  box-shadow: 0 8px 24px rgba(30, 50, 70, .15); min-width: 240px;
}
.tabs3 { display: flex; gap: 4px; padding: 6px; }
.tabs3 button { flex: 1; border: 0; border-radius: 3px; padding: 4px 6px; cursor: pointer; background: #f0f3f6; color: var(--muted); font-size: 14px; }
.tabs3 button.on-expense { background: #f86868; color: #fff; }
.tabs3 button.on-income { background: var(--green); color: #fff; }
.tabs3 button.on-transfer { background: #f04b4b; color: #fff; }
.catlist { max-height: 380px; overflow-y: auto; padding: 4px 0; }
.catitem { display: flex; align-items: center; gap: 12px; padding: 6px 12px; cursor: pointer; border: 0; background: none; width: 100%; text-align: left; font-size: 16px; }
.catitem:hover, .catitem.sel { background: var(--hover); }
.catitem.child { padding-left: 34px; font-size: 15px; }
.pop-foot { border-top: 1px solid var(--line); padding: 8px; text-align: center; }
.pop-foot a, .pop-foot button { background: #f0f3f6; border: 0; border-radius: 3px; padding: 5px 18px; font-weight: 600; color: var(--text); text-decoration: none; font-size: 14px; cursor: pointer; display: inline-block; }
.taglist { max-height: 260px; overflow-y: auto; padding: 4px 0; }
.tagitem { display: flex; align-items: center; gap: 10px; padding: 6px 12px; cursor: pointer; }
.tagitem:hover { background: var(--hover); }
.tagitem input { accent-color: var(--green); width: 17px; height: 17px; }
.tag-search { padding: 8px; border-bottom: 1px solid var(--line); }
.tag-search .input { height: 36px; }
.pop-empty { color: var(--muted); padding: 10px 12px; font-size: 14px; }

.icon-grid { display: grid; grid-template-columns: repeat(8, 36px); gap: 4px; padding: 8px; }
.icon-grid button { width: 36px; height: 36px; border: 0; border-radius: 50%; background: #f0f3f6; cursor: pointer; display: grid; place-items: center; font-size: 17px; color: var(--text); }
.icon-grid button.sel { background: var(--green); color: #fff; }
.color-grid { display: grid; grid-template-columns: repeat(7, 28px); gap: 6px; padding: 10px; }
.color-grid button { width: 28px; height: 28px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; }
.color-grid button.sel { border-color: var(--text); }

/* ---------- Modal ---------- */
.modal-wrap { position: fixed; inset: 0; background: var(--overlay); z-index: 60; display: grid; place-items: start center; overflow-y: auto; padding: 60px 14px; }
.modal { background: var(--card); border-radius: 8px; width: 100%; max-width: 520px; padding: 24px; position: relative; }
.modal h3 { margin: 0 0 18px; font-size: 19px; }
.modal .form { display: grid; gap: 14px; }
.modal .two { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; }
.modal .field { min-width: 0; }
.modal-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 22px; gap: 10px; }
.modal-foot .right { display: flex; gap: 10px; margin-left: auto; }

/* ---------- Categorias ---------- */
.subtabs { display: flex; gap: 6px; margin-bottom: 18px; }
.subtabs a { padding: 10px 18px; border-radius: 4px; text-decoration: none; color: var(--muted); font-weight: 600; }
.subtabs a.active { background: var(--card); color: var(--green); box-shadow: var(--shadow); }
.newcat { display: grid; grid-template-columns: 70px 70px minmax(160px, 1fr) 140px minmax(160px, 1fr) auto; gap: 10px; align-items: end; }
.cat-section { margin-top: 26px; }
.cat-section h3 { font-size: 16px; margin: 0 0 8px; }
.catrow { display: grid; grid-template-columns: 40px minmax(0, 1fr) minmax(0, 1fr) auto; gap: 12px; align-items: center; padding: 7px 0; border-bottom: 1px solid var(--line); }
.catrow:last-child { border-bottom: 0; }
.catrow.child { padding-left: 34px; grid-template-columns: 40px minmax(0, 1fr) minmax(0, 1fr) auto; }
.catrow.is-hidden .name, .catrow.is-hidden .cat-badge { opacity: .45; }
.catrow .meta { color: var(--muted); font-size: 13px; }
.row-actions { display: flex; gap: 6px; }
.act { width: 32px; height: 28px; border: 0; border-radius: 3px; cursor: pointer; display: grid; place-items: center; font-size: 15px; }
.act.green { background: #e3f7ee; color: var(--green); }
.act.red { background: #fde8e8; color: var(--red); }
.swatch-btn { height: 46px; border: 1px solid var(--input-line); border-radius: 4px; background: var(--input); display: flex; align-items: center; justify-content: center; gap: 6px; cursor: pointer; width: 100%; }
.swatch { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-size: 12px; }

/* ---------- Modal da conta ---------- */
.acc-head { display: flex; gap: 10px; align-items: end; }
.acc-head .field:not([style]) { width: 70px; flex: none; }
.acc-preview { display: flex; gap: 14px; align-items: center; background: var(--hover); border-radius: 6px; padding: 12px 14px; }
.acc-logo { background: #fff; border: 1px solid var(--line); overflow: hidden; }
.acc-logo img { width: 72%; height: 72%; object-fit: contain; display: block; }
.acc-logo img[src^="data:"] { width: 100%; height: 100%; }
.swatch.acc-logo { width: 26px; height: 26px; }
.cat-badge.xs { width: 28px; height: 28px; font-size: 14px; }
.acc-text { font-weight: 700; letter-spacing: -.02em; font-size: 16px; line-height: 1; }
.acc-text.len3 { font-size: 13px; }
.acc-text.len1 { font-size: 19px; }
.cat-badge.xs.acc-text { font-size: 11px; }
.cat-badge.sm.acc-text { font-size: 12px; }
.color-grid button[style*="#ffffff"] { border-color: var(--line); }
.acc-picker .tabs3 { border-bottom: 1px solid var(--line); }
.bank-list { max-height: 340px; overflow-y: auto; padding: 4px 8px 8px; }
.bank-sec { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 10px 4px 4px; }
.bank-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; }
.bank-grid.one { grid-template-columns: 1fr; }
.bank { display: flex; align-items: center; gap: 8px; border: 0; background: none; padding: 6px; border-radius: 4px; cursor: pointer; text-align: left; font-size: 14px; min-width: 0; }
.bank span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bank:hover, .bank.sel { background: var(--hover); }
.bank .cat-badge.sm { width: 30px; height: 30px; }
.bank-other { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--line); }
.bank-other .input { height: 38px; }
.bank-other .btn { height: 38px; }
.acc-upload { padding: 10px; display: grid; gap: 10px; }
.acc-upload-cur { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; }
.linkish { background: none; border: 0; padding: 0; color: var(--green); cursor: pointer; font-size: inherit; text-decoration: underline; }

/* ---------- Importar ---------- */
.btn { text-decoration: none; }
.dropzone {
  border: 2px dashed var(--input-line); border-radius: 8px; padding: 34px 20px; display: grid; gap: 6px;
  justify-items: center; text-align: center; cursor: pointer; color: var(--text);
}
.dropzone:hover, .dropzone.over { border-color: var(--green); background: #f0fbf6; }
.dz-icon { font-size: 30px; color: var(--green); }
.imp-h { margin: 0 0 12px; font-size: 16px; }
.table-wrap { overflow-x: auto; }
.imp-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.imp-table th { text-align: left; color: var(--muted); font-weight: 600; font-size: 13px; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.imp-table td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.imp-table .num { text-align: right; white-space: nowrap; }
.input.sm { height: 38px; font-size: 14px; min-width: 120px; }
.imp-stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-bottom: 8px; }
.imp-stats > div { background: var(--hover); border-radius: 6px; padding: 10px 12px; display: grid; }
.imp-stats b { font-size: 22px; }
.imp-stats span { color: var(--muted); font-size: 13px; }
.imp-stats .dup { background: #fff7e0; }
.imp-details { margin-top: 10px; }
.imp-details summary { cursor: pointer; color: var(--green); font-weight: 600; }
.imp-foot { display: flex; justify-content: flex-end; align-items: center; gap: 16px; margin-top: 18px; flex-wrap: wrap; }
.imp-foot .btn-primary { min-width: 240px; justify-content: center; }
.imp-bar { width: 260px; height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; margin-bottom: 4px; }
.imp-bar span { display: block; height: 100%; background: var(--green); transition: width .2s; }

/* ---------- Entrar ---------- */
.auth { max-width: 400px; margin: 70px auto; }
.auth .logo { justify-content: center; font-size: 28px; margin-bottom: 24px; }
.auth .logo-dot { width: 40px; height: 40px; font-size: 20px; }
.auth .card { padding: 28px; display: grid; gap: 14px; }
.auth .btn-primary { justify-content: center; }
.auth .switch { text-align: center; font-size: 14px; color: var(--muted); }
.auth .switch button { background: none; border: 0; color: var(--green); cursor: pointer; font-weight: 600; }
.ok-text { color: var(--green-dark); font-size: 14px; }

/* ---------- Avisos ---------- */
#toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 100; display: grid; gap: 8px; }
.toast { background: #2f3e4c; color: #fff; padding: 10px 18px; border-radius: 6px; box-shadow: 0 4px 14px rgba(0, 0, 0, .2); font-size: 14px; }
.toast.err { background: var(--red); }

/* ---------- Ecrãs mais estreitos ---------- */
@media (max-width: 1100px) {
  .txgrid.row1 { grid-template-columns: 1fr 1fr 1fr; }
  .wallets, .grid4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .newcat { grid-template-columns: 70px 70px 1fr; }
}
@media (max-width: 700px) {
  .topbar { padding: 0 16px; gap: 0 10px; flex-wrap: wrap; height: auto; }
  .topbar .logo { height: 52px; }
  .user { margin-left: auto; }
  .user .email { display: none; }
  .nav { order: 3; width: 100%; height: 44px; margin: 0; overflow-x: auto; }
  .nav a { padding: 0 10px; font-size: 14px; white-space: nowrap; flex: 1; justify-content: center; }
  main { padding: 18px 16px 60px; }
  .txgrid.row1, .txgrid.row2, .txgrid.row2.fx { grid-template-columns: 1fr 1fr; }
  .txgrid.row2 > .field:only-child { grid-column: 1 / -1; }
  .wallets, .grid4 { grid-template-columns: 1fr; }
  .grid4 > .span2 { grid-column: auto; }
  .summary { grid-template-columns: 1fr 1fr; gap: 10px; }
  .summary .stat { padding: 12px 14px; }
  .summary .stat .value { font-size: 18px; }
  .toolbar > * { flex: 1 1 auto; }
  .toolbar .spacer { display: none; }
  .period { width: 100%; }
  .period-label { flex: 1; }
  .txrow { grid-template-columns: 40px minmax(0, 1fr) auto; }
  .txrow .note, .txrow .acc { display: none; }
  .period-label { min-width: 0; }
  .newcat { grid-template-columns: 1fr 1fr; }
  .catrow .meta { display: none; }
  .catrow, .catrow.child { grid-template-columns: 40px minmax(0, 1fr) auto; }
  .modal { padding: 20px 16px; }
  .modal .two { grid-template-columns: minmax(0, 1fr); }
  .modal-foot { flex-wrap: wrap-reverse; }
  .modal-foot .right { flex-wrap: wrap; justify-content: flex-end; }
}
