:root {
  --bg: #e3e4e6;
  --card: #ffffff;
  --line: #ececee;
  --line-strong: #e0e0e3;
  --text: #1b1b1d;
  --text-2: #3d3d42;
  --muted: #8a8a91;
  --soft: #f6f6f7;
  --dark: #232326;
  --mid: #a4a4aa;
  --light: #e8e8eb;
  --blue: #2f7cf6;
  --green: #1f9d48;
  --green-bg: #e8f8ed;
  --red: #e5402b;
  --red-bg: #fdecea;
  --amber-bg: #fbf6ea;
  --amber-line: #efe3c4;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(20, 20, 30, .04), 0 8px 24px -12px rgba(20, 20, 30, .10);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { margin: 0; }

body {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "tnum" 1, "cv11" 1;
}

button, input { font: inherit; color: inherit; }

svg { display: block; }

.icon-btn svg, .btn svg, .card-title svg {
  width: 18px; height: 18px;
  fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}

.app {
  max-width: 1320px;
  margin: 0 auto;
  padding: 28px 32px 48px;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { margin: 0; font-size: 18px; font-weight: 600; letter-spacing: -.01em; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

.logo {
  width: 40px; height: 40px;
  border-radius: 12px;
  background: var(--dark);
  color: #fff;
  display: grid; place-items: center;
  font-weight: 700;
}

.month-nav {
  display: flex; align-items: center; gap: 6px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 4px;
  box-shadow: var(--shadow);
}
.month-nav .icon-btn { border: 0; width: 34px; height: 34px; }
.month-label {
  border: 0; background: none; cursor: pointer;
  min-width: 150px; text-align: center;
  font-weight: 600; font-size: 14px;
  padding: 6px 8px; border-radius: 8px;
}
.month-label:hover { background: var(--soft); }

.actions { display: flex; gap: 10px; }

.icon-btn {
  width: 38px; height: 38px;
  display: inline-grid; place-items: center;
  border-radius: 10px;
  border: 1px solid var(--line-strong);
  background: var(--card);
  color: var(--text-2);
  cursor: pointer;
  transition: background .15s, border-color .15s;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--soft); }

.btn {
  display: inline-flex; align-items: center; gap: 8px;
  height: 38px; padding: 0 16px;
  border-radius: 10px;
  border: 1px solid var(--line-strong);
  background: var(--card);
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, transform .05s;
}
.btn:hover { background: var(--soft); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--dark); border-color: var(--dark); color: #fff; }
.btn.primary:hover { background: #000; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--red); }
.btn.danger:hover { background: var(--red-bg); }


/* ---------- Grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 18px;
}
.c-limit, .c-planned, .c-entries { grid-column: span 4; }
.c-flow { grid-column: span 12; }
.c-balance, .c-goal { grid-column: span 6; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px 22px 22px;
  box-shadow: var(--shadow);
  min-width: 0;
  display: flex; flex-direction: column;
}

.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  min-height: 38px;
  margin-bottom: 14px;
}
.card-title {
  display: flex; align-items: center; gap: 10px;
  font-size: 13px; font-weight: 600;
  letter-spacing: .03em;
  text-transform: uppercase;
  color: var(--text-2);
}

.chip {
  font-size: 12px; font-weight: 500;
  color: var(--text-2);
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  padding: 5px 10px;
  white-space: nowrap;
}

.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.divider { height: 1px; background: var(--line); margin: 16px 0; }

.big-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.big {
  font-size: 34px; font-weight: 600;
  letter-spacing: -.03em;
  line-height: 1.1;
}
.c-balance p { margin: 8px 0 0; }


.pos { color: var(--green); }
.neg { color: var(--red); }

/* ---------- Rows ---------- */
.rows { display: flex; flex-direction: column; gap: 14px; }
.row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.row-label { display: flex; align-items: flex-start; gap: 10px; min-width: 0; }
.row-label .dot { margin-top: 6px; }
.row-name { font-weight: 500; color: var(--text); }
.row-sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.row-value { font-weight: 600; white-space: nowrap; }
.row.soft .row-name, .row.soft .row-value { color: var(--muted); font-weight: 500; }

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.dot.dark { background: var(--dark); }
.dot.mid { background: var(--mid); }
.dot.light { background: var(--light); box-shadow: inset 0 0 0 1px #d6d6da; }
.dot.blue { background: var(--blue); }
.dot.grey { background: #c9c9ce; }
.dot.green { background: var(--green); }
.dot.red { background: var(--red); }

/* ---------- Gauge ---------- */
.gauge-wrap {
  position: relative; margin: 2px auto 20px; width: 100%; max-width: 330px;
  container-type: inline-size;
}
#gauge { width: 100%; height: auto; overflow: visible; }
#gauge line { transition: stroke .4s ease; }
.gauge-center {
  position: absolute; left: 0; right: 0; bottom: -4px;
  text-align: center;
}
.gauge-value { font-size: clamp(18px, 7.4cqw, 25px); font-weight: 600; letter-spacing: -.03em; }
.gauge-value.neg { color: var(--red); }
.gauge-value.warn { color: #c2410c; }
.gauge-sub { color: var(--muted); font-size: clamp(11px, 3.9cqw, 13px); margin-top: 2px; }

/* ---------- Goal ---------- */
.progress {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 4px;
  margin: 14px 0 18px;
}
.progress i {
  height: 22px; border-radius: 6px;
  background: var(--soft);
  box-shadow: inset 0 0 0 1px var(--line);
  position: relative; overflow: hidden;
}
.progress i.full { background: var(--dark); box-shadow: none; }
.progress i.part::after {
  content: ""; position: absolute; inset: 0 auto 0 0;
  width: var(--w); background: var(--mid);
}

.notice {
  margin-top: 16px;
  background: var(--amber-bg);
  border: 1px solid var(--amber-line);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 13px;
  line-height: 1.5;
  color: #5b4a1d;
}
.notice.ok { background: var(--green-bg); border-color: #cdeed8; color: #17542d; }
.notice strong { font-weight: 600; }
.notice p { margin: 0; }
.notice p + p { margin-top: 6px; }

/* ---------- Legend / chart ---------- */
.legend { display: flex; gap: 16px; color: var(--text-2); font-size: 13px; }
.legend span { display: inline-flex; align-items: center; gap: 7px; }

.chart { position: relative; height: 290px; margin-top: auto; overflow: hidden; }
.chart svg { position: absolute; inset: 0; }
.chart .axis { fill: var(--muted); font-size: 12px; }
.chart .grid-line { stroke: #efeff1; stroke-dasharray: 3 4; }
.chart .hit { fill: transparent; cursor: default; }

.tooltip {
  position: absolute;
  pointer-events: none;
  background: #262629;
  color: #fff;
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 13px;
  min-width: 190px;
  box-shadow: 0 10px 30px -10px rgba(0,0,0,.4);
  opacity: 0;
  transform: translateY(4px);
  transition: opacity .15s, transform .15s;
  z-index: 2;
}
.tooltip.show { opacity: 1; transform: none; }
.tooltip .t-title { font-weight: 500; margin-bottom: 8px; }
.tooltip .t-row { display: flex; align-items: center; gap: 8px; margin-top: 5px; }
.tooltip .t-row span:first-of-type { color: rgba(255,255,255,.6); flex: 1; }
.tooltip .t-row b { font-weight: 500; }

/* ---------- Lists ---------- */
.line-between {
  display: flex; justify-content: space-between; align-items: baseline;
  margin-bottom: 12px;
}
.line-between strong { font-weight: 600; }

.list {
  display: flex; flex-direction: column; gap: 8px;
  max-height: 360px; overflow-y: auto;
  margin: 0 -4px; padding: 0 4px;
}

.item {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center; gap: 14px;
  padding: 13px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fcfcfc;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.item:hover { border-color: #d9d9de; background: #fff; }
.item.planned { grid-template-columns: 1fr auto auto auto; }

.item-name {
  font-weight: 500; font-size: 14.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: flex; align-items: center; gap: 8px;
}
.item-name .tag {
  font-size: 11px; padding: 1px 6px; border-radius: 5px;
  background: #efeff1; color: var(--text-2); font-weight: 500;
}
.item-date {
  color: var(--muted); font-size: 13px;
  white-space: nowrap; text-align: right;
}
.item-date.warn { color: var(--red); }
.item-amount {
  font-weight: 600; font-size: 14.5px;
  white-space: nowrap; text-align: right; min-width: 92px;
}
.item-amount.inc { color: var(--green); }

.pay-btn {
  width: 32px; height: 32px;
  border-radius: 50%;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--muted);
  cursor: pointer;
  display: inline-grid; place-items: center;
  flex-shrink: 0;
  transition: background .15s, color .15s, border-color .15s;
}
.pay-btn:hover { background: var(--dark); color: #fff; border-color: var(--dark); }
.pay-btn svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }

.empty {
  text-align: center;
  padding: 28px 12px;
  color: var(--muted);
  border: 1px dashed var(--line-strong);
  border-radius: 12px;
  font-size: 13px;
}
.empty button {
  margin-top: 10px;
  background: none; border: 0; color: var(--text); font-weight: 600;
  cursor: pointer; text-decoration: underline; text-underline-offset: 3px;
}

/* ---------- Segmented ---------- */
.segmented {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  background: var(--soft);
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 3px;
  gap: 3px;
}
.segmented button {
  border: 0; background: none; cursor: pointer;
  border-radius: 8px;
  padding: 8px 12px;
  font-weight: 500; color: var(--muted);
  transition: background .15s, color .15s;
}
.segmented button.active { background: #fff; color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.segmented.small button { padding: 5px 12px; font-size: 13px; }

/* ---------- Modal ---------- */
.modal {
  border: 0; padding: 0;
  border-radius: 20px;
  width: min(480px, calc(100vw - 32px));
  box-shadow: 0 30px 80px -20px rgba(0,0,0,.35);
  color: var(--text);
}
.modal::backdrop { background: rgba(30, 30, 35, .35); backdrop-filter: blur(3px); }
.modal[open] { animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }

.modal form { padding: 22px; display: flex; flex-direction: column; gap: 14px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { margin: 0; font-size: 17px; font-weight: 600; }
.modal-head .icon-btn { border: 0; }
.modal-foot { display: flex; gap: 8px; margin-top: 6px; }
.spacer { flex: 1; }
.type-hint { margin: -4px 0 0; min-height: 16px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; font-weight: 500; color: var(--text-2); }
.field input {
  height: 42px; padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: #fff;
  outline: none;
  width: 100%;
  transition: border-color .15s, box-shadow .15s;
}
.field input:focus { border-color: #b9b9bf; box-shadow: 0 0 0 3px #f0f0f2; }
.money { position: relative; }
.money em { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); font-style: normal; color: var(--muted); }
.money input { padding-left: 38px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.check { display: flex; align-items: center; gap: 10px; cursor: pointer; font-size: 14px; }
.check input { width: 17px; height: 17px; accent-color: var(--dark); }
.check em { font-style: normal; }

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

.data-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px;
  transform: translate(-50%, 20px);
  background: var(--dark); color: #fff;
  padding: 10px 10px 10px 16px;
  border-radius: 12px;
  display: flex; align-items: center; gap: 14px;
  font-size: 13.5px;
  opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s;
  box-shadow: 0 10px 30px -8px rgba(0,0,0,.4);
  z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast button {
  background: rgba(255,255,255,.14); border: 0; color: #fff;
  border-radius: 8px; padding: 6px 10px; cursor: pointer; font-weight: 500;
}
.toast button:hover { background: rgba(255,255,255,.24); }

/* ---------- Responsive ---------- */
@media (max-width: 1120px) {
  .c-limit, .c-planned { grid-column: span 6; }
  .c-entries, .c-flow { grid-column: span 12; }
  .c-balance, .c-goal { grid-column: span 6; }
}

/* ---------- Login ---------- */
.auth {
  min-height: 100vh; min-height: 100dvh;
  display: grid; place-items: center;
  padding: 24px;
}
.auth-card {
  width: min(380px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 28px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px;
}
.auth-card h1 { margin: 6px 0 0; font-size: 20px; font-weight: 600; letter-spacing: -.01em; }
.auth-card > p.muted { margin: -8px 0 4px; }
.btn.block { width: 100%; justify-content: center; height: 44px; }
.btn:disabled { opacity: .6; cursor: default; }

/* ---------- Status de sincronização ---------- */
.sync {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 38px; padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--line-strong);
  background: var(--card);
  font-size: 12.5px; color: var(--text-2);
  cursor: pointer; white-space: nowrap;
}
.sync i { width: 7px; height: 7px; border-radius: 50%; background: var(--green); flex-shrink: 0; }
.sync.saving i, .sync.loading i { background: var(--mid); animation: pulse 1s infinite; }
.sync.error { color: var(--red); border-color: #f5c9c3; background: var(--red-bg); }
.sync.error i { background: var(--red); }
.sync.local i { background: var(--mid); }
@keyframes pulse { 50% { opacity: .3; } }

.fab { display: none; }

@media (max-width: 760px) {
  .app { padding: 14px 14px calc(96px + env(safe-area-inset-bottom)); }
  .topbar { flex-wrap: wrap; gap: 12px; margin-bottom: 14px; }
  .brand p { display: none; }
  .month-nav { order: 3; width: 100%; justify-content: space-between; }
  #openAdd { display: none; }
  .sync { width: 38px; padding: 0; }
  .sync span { display: none; }

  .fab {
    display: grid; place-items: center;
    position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom));
    width: 58px; height: 58px; border-radius: 18px;
    background: var(--dark); color: #fff; border: 0;
    box-shadow: 0 12px 30px -8px rgba(0, 0, 0, .45);
    z-index: 20; cursor: pointer;
  }
  .fab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }

  .grid { gap: 14px; }
  .grid > .card { grid-column: span 12; }
  .card { padding: 18px; }
  .big { font-size: 30px; }
  .row2 { grid-template-columns: 1fr; }
  .card-head { flex-wrap: wrap; }
  .list { max-height: none; }

  /* listas: mais espaço para o título em telas estreitas */
  .item { gap: 10px; padding: 12px; }
  .item-date { font-size: 12px; }
  .item-amount { min-width: 0; font-size: 14px; }

  /* 16px evita o zoom automático do iPhone ao focar um campo */
  .field input { font-size: 16px; }

  /* Modais viram painel inferior */
  .modal {
    width: 100%; max-width: 100%;
    margin: auto 0 0;
    border-radius: 22px 22px 0 0;
    max-height: 92dvh;
  }
  .modal form { padding-bottom: calc(22px + env(safe-area-inset-bottom)); }
  .modal[open] { animation: sheet .22s ease-out; }
  .toast { bottom: calc(92px + env(safe-area-inset-bottom)); max-width: calc(100vw - 28px); }
}
@keyframes sheet { from { transform: translateY(40px); opacity: .6; } }
