/* Finance Tracker — design system
   Numbers-first ledger aesthetic: tabular figures, hierarchy through weight
   and spacing rather than colour noise, one deliberate debit/credit and
   positive/negative convention used everywhere. */

:root {
  --bg: #10131c;
  --surface: #171c2a;
  --surface-alt: #1c2233;
  --border: #262d40;
  --border-strong: #3a4864;
  --text: #e8eaf1;
  --text-muted: #9aa3b8;
  --text-faint: #6d7690;
  --accent: #6f93ff;
  --accent-soft: #1e2740;
  --positive: #3fbd7a;
  --negative: #e2665f;
  --credit-tint: #1a2036;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
  --radius: 10px;
  --radius-sm: 6px;

  --font-ui: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-num: ui-monospace, "SF Mono", "Roboto Mono", "Menlo", "Consolas", monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body {
  padding-bottom: env(safe-area-inset-bottom);
}

.num {
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

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

a { color: var(--accent); }

/* ---- App shell ---------------------------------------------------- */

#app {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 16px 48px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg);
  padding: 12px 0 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid transparent;
}
.topbar.scrolled { border-bottom-color: var(--border); }

.brand {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.01em;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand-mark {
  width: 20px; height: 20px; border-radius: 5px;
  background: linear-gradient(160deg, var(--accent), var(--positive));
  flex: none;
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  width: 34px; height: 34px;
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer;
  font-size: 15px;
}
.icon-btn:hover { color: var(--text); border-color: var(--accent); }

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

/* ---- Month selector -------------------------------------------------- */

.month-selector {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 14px 0 18px;
}
.month-selector .nav-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 16px;
}
.month-selector .nav-btn:hover { color: var(--accent); border-color: var(--accent); }
.month-selector .nav-btn:active { transform: scale(0.96); }

.month-picker {
  display: flex;
  align-items: baseline;
  gap: 8px;
  cursor: pointer;
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
}
.month-picker:hover { border-color: var(--border); background: var(--surface); }
.month-picker .m { font-size: 20px; font-weight: 700; }
.month-picker .y { font-size: 20px; font-weight: 400; color: var(--text-muted); }

.month-picker-inputs {
  display: none;
  gap: 8px;
  padding: 0 0 12px;
  justify-content: center;
}
.month-picker-inputs.open { display: flex; }
.month-picker-inputs select {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 14px;
  color: var(--text);
}

/* ---- Top-line computed figures ---------------------------------------- */

.headline-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}
.headline-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 14px 12px;
  box-shadow: var(--shadow);
}
.headline-card .label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 6px;
}
.headline-card .value {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.headline-card.emphasis {
  background: linear-gradient(165deg, var(--accent-soft), var(--surface) 70%);
  border-color: var(--accent);
}

.value.positive { color: var(--positive); }
.value.negative { color: var(--negative); }

/* ---- Sections ------------------------------------------------------- */

.section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 16px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.section-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface-alt);
  border-bottom: 1px solid var(--border);
}
.section-header .title {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text);
}
.section-header .total {
  font-size: 16px;
  font-weight: 700;
}

.computed-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-alt);
}
.computed-row:last-child { border-bottom: none; }
.computed-row .label { font-size: 13px; color: var(--text-muted); font-weight: 600; }
.computed-row .value { font-size: 15px; font-weight: 700; }

/* leaf field row */
.field-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  min-height: 40px;
}
.field-row:last-child { border-bottom: none; }
.field-row:hover { background: var(--surface-alt); }
.field-row:active { background: var(--accent-soft); }

.field-row .label {
  font-size: 13.5px;
  color: var(--text);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cols {
  display: flex;
  gap: 18px;
  flex: none;
}
.field-col {
  min-width: 84px;
  text-align: right;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}
.field-col.placeholder { visibility: hidden; }
.field-col .col-tag {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
  font-weight: 600;
}
.field-col.credit .col-tag { color: var(--accent); }
.field-col .col-val { font-size: 13.5px; font-weight: 600; }
.field-col.credit .col-val:not(.positive):not(.negative) { color: var(--accent); }

/* Debit / Credit column headers, printed once at the top of the Expense
   block rather than repeated per row. */
.expense-col-headers {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 16px 5px;
  background: var(--surface-alt);
  border-bottom: 1px solid var(--border);
}
.expense-col-headers .col-tag { font-size: 10px; }

.section-header .cols .col-val { font-size: 16px; font-weight: 700; }

/* category group (Accommodation / Bills / Car) */
.category-group {
  border-bottom: 1px solid var(--border-strong);
}
.category-group:last-child { border-bottom: none; }
.category-group-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 8px 16px 4px;
  background: var(--surface-alt);
}
.category-group-header .label {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.category-group-header .value { font-size: 13.5px; font-weight: 700; }
.category-group .field-row { padding-left: 26px; }

/* Empty variant: same bar styling, no label/total — purely a visual break
   between grouped categories and the plain single-field ones. */
.category-group-header.blank { padding: 12px 16px; }

/* ---- Modal / sheet ---------------------------------------------------- */

.overlay {
  position: fixed; inset: 0;
  background: rgba(10, 12, 20, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 100;
  animation: fade-in 0.15s ease;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.sheet {
  background: var(--surface);
  width: 100%;
  max-width: 520px;
  border-radius: 16px 16px 0 0;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.25);
}
@media (min-width: 640px) {
  .overlay { align-items: center; }
  .sheet { border-radius: 16px; max-height: 80vh; }
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 12px;
  border-bottom: 1px solid var(--border);
}
.sheet-header h2 { font-size: 16px; margin: 0; font-weight: 700; }
.sheet-header .sum { font-size: 13px; color: var(--text-muted); margin-top: 2px; }
.sheet-close {
  border: none; background: var(--surface-alt); color: var(--text-muted);
  width: 30px; height: 30px; border-radius: 999px; cursor: pointer; font-size: 15px;
}

.sheet-body { overflow-y: auto; padding: 12px 18px 18px; flex: 1; }

.tx-entry-row {
  display: flex; gap: 8px; margin-bottom: 14px;
  position: sticky; top: 0; background: var(--surface); padding-top: 2px; z-index: 5;
}
.tx-sign-btn {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  border-radius: var(--radius-sm);
  width: 44px; font-size: 16px; font-weight: 700; cursor: pointer; color: var(--text-muted);
}
.tx-sign-btn.negative-active { color: var(--negative); border-color: var(--negative); background: color-mix(in srgb, var(--negative) 10%, var(--surface)); }
.tx-amount-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 17px;
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  background: var(--surface-alt);
  color: var(--text);
}
.tx-amount-input:focus {
  outline: none; border-color: var(--accent);
  background: var(--surface);
}

.tx-list { display: flex; flex-direction: column; gap: 1px; }
.tx-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 9px 4px; border-bottom: 1px solid var(--border);
}
.tx-row:last-child { border-bottom: none; }
.tx-amount {
  font-size: 14.5px; font-weight: 600;
}
.tx-meta { font-size: 11px; color: var(--text-faint); }
.tx-row-actions { display: flex; gap: 6px; align-items: center; }
.tx-edit-input {
  width: 100px; text-align: right;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 5px 8px; font-size: 13.5px; font-family: var(--font-num);
  background: var(--surface-alt); color: var(--text);
}
.tx-del-btn {
  border: none; background: transparent; color: var(--text-faint);
  cursor: pointer; font-size: 15px; padding: 4px 6px;
}
.tx-del-btn:hover { color: var(--negative); }
.tx-empty { color: var(--text-faint); font-size: 13px; padding: 18px 4px; text-align: center; }
.tx-source-tag {
  font-size: 9px; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--accent); background: var(--accent-soft);
  padding: 1px 5px; border-radius: 4px; margin-left: 6px;
}

.column-tabs {
  display: flex; gap: 6px; margin-bottom: 12px;
}
.column-tab {
  flex: 1; text-align: center; padding: 8px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-alt);
  font-size: 12.5px; font-weight: 600; color: var(--text-muted); cursor: pointer;
}
.column-tab.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

/* ---- Forms / settings -------------------------------------------------- */

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.panel h2 { font-size: 14px; margin: 0 0 4px; text-transform: uppercase; letter-spacing: 0.05em; }
.panel .desc { font-size: 12.5px; color: var(--text-muted); margin: 0 0 14px; line-height: 1.5; }

.field {
  margin-bottom: 12px;
}
.field label {
  display: block; font-size: 12px; font-weight: 600; color: var(--text-muted);
  margin-bottom: 5px;
}
.field input, .field select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 14px;
  background: var(--surface-alt);
  color: var(--text);
}
.field input:focus, .field select:focus { outline: none; border-color: var(--accent); background: var(--surface); }

.btn {
  border: none;
  background: var(--accent);
  color: #fff;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { filter: brightness(1.08); }
.btn.secondary {
  background: var(--surface-alt); color: var(--text); border: 1px solid var(--border);
}
.btn:disabled { opacity: 0.5; cursor: default; }

.sync-status-row { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.sync-status { font-size: 12.5px; font-weight: 600; }
.sync-status.idle { color: var(--text-faint); }
.sync-status.syncing { color: var(--accent); }
.sync-status.ok { color: var(--positive); }
.sync-status.error { color: var(--negative); }

.back-link {
  display: inline-flex; align-items: center; gap: 6px;
  color: var(--text-muted); font-size: 13px; font-weight: 600;
  cursor: pointer; margin: 14px 0 6px; text-decoration: none;
}
.back-link:hover { color: var(--accent); }

/* ---- Import screen ----------------------------------------------------- */

.import-group {
  margin-bottom: 18px;
}
.import-group h3 {
  font-size: 12.5px; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--text-muted); margin: 0 0 8px;
}
.paste-box {
  width: 100%;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-alt);
  color: var(--text);
  font-family: var(--font-num);
  font-size: 12.5px;
  padding: 10px;
  min-height: 56px;
  resize: vertical;
  margin-bottom: 10px;
}
.import-field-row {
  display: grid;
  grid-template-columns: 1fr 130px;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}
.import-field-row:last-child { border-bottom: none; }
.import-field-row label { font-size: 13px; color: var(--text); }
.import-field-row input {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 9px;
  font-size: 13.5px;
  font-family: var(--font-num);
  text-align: right;
  background: var(--surface-alt);
  color: var(--text);
}
.import-field-row input:focus { outline: none; border-color: var(--accent); background: var(--surface); }

.import-actions {
  display: flex; gap: 10px; justify-content: flex-end;
  margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border);
}

.banner {
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-size: 13px;
  margin-bottom: 16px;
  color: var(--text);
}
.banner.warn {
  background: color-mix(in srgb, var(--negative) 10%, var(--surface));
  border-color: var(--negative);
}

/* ---- Responsive: iPad / Mac widen to a grid ---------------------------- */

@media (min-width: 760px) {
  #app { padding: 0 24px 64px; }
  .headline-grid { gap: 14px; }
  .field-row .label { font-size: 14px; }
}

@media (min-width: 1000px) {
  #app { max-width: 1080px; }
}

.empty-state {
  text-align: center;
  color: var(--text-faint);
  font-size: 13px;
  padding: 40px 20px;
}
