:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --income: #16a34a;
  --income-bg: #dcfce7;
  --expense: #dc2626;
  --expense-bg: #fee2e2;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 4px 16px rgba(15, 23, 42, 0.04);
  --font: "SF Pro Text", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
}

button, input, select, textarea {
  font: inherit;
}

.app, .login-screen {
  min-height: 100vh;
}

/* Login */
.login-screen {
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 20% 20%, rgba(37, 99, 235, 0.08), transparent 40%),
    radial-gradient(circle at 80% 80%, rgba(34, 197, 94, 0.08), transparent 40%),
    var(--bg);
}

.login-card {
  width: min(380px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 4px);
  padding: 32px;
  box-shadow: var(--shadow);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.login-sub {
  margin: 8px 0 24px;
  color: var(--muted);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
}

.field span {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.field input,
.field select,
.field textarea,
.search,
.month-picker,
.inline-form input,
.inline-form select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}

.field input:focus,
.field select:focus,
.search:focus,
.month-picker:focus {
  outline: 2px solid rgba(37, 99, 235, 0.35);
  border-color: var(--accent);
}

.error {
  color: var(--expense);
  font-size: 13px;
  margin: 0 0 12px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s;
}

.btn:hover { background: #f1f5f9; }
.btn.primary {
  background: var(--accent);
  color: #fff;
}
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { color: var(--muted); border-color: var(--border); background: var(--surface); }
.btn.ghost:hover { color: var(--text); background: #f8fafc; }
.btn.sm { padding: 7px 12px; font-size: 13px; }
.btn.icon { width: 36px; height: 36px; padding: 0; font-size: 18px; }

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.topbar h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
}

.logo-mark {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--income), var(--accent));
}

.book-tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: #f1f5f9;
  border-radius: 10px;
}

.view-tabs {
  display: flex;
  gap: 4px;
  padding: 3px;
  background: #eef2ff;
  border-radius: 10px;
}

.view-tab {
  padding: 5px 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: 12px;
  cursor: pointer;
}

.view-tab.active {
  background: var(--surface);
  color: var(--accent);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}

.year-picker {
  width: 80px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.year-panel { margin-bottom: 0; }

.year-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}

.year-block h3 {
  margin: 0 0 10px;
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.year-stats {
  list-style: none;
  margin: 0;
  padding: 0;
}

.year-stats li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 0;
  border-bottom: 1px solid #f1f5f9;
  font-size: 13px;
}

.year-stats li strong {
  font-family: var(--mono);
}

.year-recurring {
  padding: 16px 18px 18px;
}

.year-recurring h3 {
  margin: 0 0 12px;
  font-size: 13px;
}

.recurring-table-wrap { overflow: auto; }

.recurring-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.recurring-table th,
.recurring-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.recurring-table th { color: var(--muted); font-size: 11px; text-transform: uppercase; }
.recurring-table td.num { text-align: right; font-family: var(--mono); }

.setup-steps {
  margin: 0 0 16px;
  padding-left: 20px;
  color: var(--muted);
  font-size: 13px;
}

.setup-steps li { margin-bottom: 6px; }

.setup-json {
  width: 100%;
  font-family: var(--mono);
  font-size: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f8fafc;
  resize: vertical;
}

.ledger-tag {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--muted);
  background: #f1f5f9;
  padding: 2px 6px;
  border-radius: 4px;
}

@media (max-width: 900px) {
  .year-grid { grid-template-columns: 1fr; }
}

.book-tab {
  padding: 6px 14px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  cursor: pointer;
}

.book-tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}

.month-picker {
  width: auto;
  min-width: 150px;
}

/* Main layout */
.main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}

.summary-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
}

.summary-card .label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.summary-card .value {
  display: block;
  margin-top: 6px;
  font-size: 1.75rem;
  font-weight: 700;
  font-family: var(--mono);
  letter-spacing: -0.02em;
}

.summary-card .sub {
  display: block;
  margin-top: 4px;
  font-size: 12px;
  color: var(--muted);
}

.summary-card.income .value { color: var(--income); }
.summary-card.expense .value { color: var(--expense); }
.summary-card.net .value { color: var(--text); }
.summary-card.budget .value { color: var(--accent); }

.panels {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 16px;
}

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

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}

.panel-head h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.panel-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.search { width: 160px; padding: 7px 10px; }

/* Category bars */
.category-bars {
  padding: 12px 18px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 360px;
  overflow: auto;
}

.cat-row {
  display: grid;
  grid-template-columns: 120px 1fr auto;
  gap: 12px;
  align-items: center;
}

.cat-row.income-row { opacity: 0.85; }

.cat-name {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 13px;
}

.cat-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.cat-track {
  height: 8px;
  background: #f1f5f9;
  border-radius: 99px;
  overflow: hidden;
}

.cat-fill {
  height: 100%;
  border-radius: 99px;
  transition: width 0.3s ease;
}

.cat-fill.over { background: var(--expense) !important; }

.cat-nums {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.cat-nums strong { color: var(--text); }

/* Daily chart */
.daily-chart {
  padding: 16px 18px 20px;
  display: flex;
  align-items: flex-end;
  gap: 3px;
  min-height: 180px;
  overflow-x: auto;
}

.day-col {
  flex: 1 0 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 12px;
}

.day-bars {
  display: flex;
  gap: 1px;
  align-items: flex-end;
  height: 120px;
}

.day-bar {
  width: 5px;
  border-radius: 2px 2px 0 0;
  min-height: 2px;
}

.day-bar.income { background: var(--income); opacity: 0.85; }
.day-bar.expense { background: var(--expense); opacity: 0.75; }

.day-label {
  font-size: 9px;
  color: var(--muted);
}

/* Transactions */
.table-wrap { overflow: auto; }

.tx-table {
  width: 100%;
  border-collapse: collapse;
}

.tx-table th,
.tx-table td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.tx-table th {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  background: #fafbfc;
}

.tx-table td.num,
.tx-table th.num { text-align: right; font-family: var(--mono); }

.tx-table tr:hover td { background: #fafbfc; }

.cat-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 8px;
  border-radius: 99px;
  font-size: 12px;
  font-weight: 600;
  background: #f1f5f9;
}

.tx-actions { display: flex; gap: 4px; justify-content: flex-end; }
.tx-actions button {
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
}
.tx-actions button:hover { background: #f1f5f9; color: var(--text); }

.source-tag {
  font-size: 10px;
  color: var(--muted);
  margin-left: 4px;
}

.empty-state {
  padding: 32px;
  text-align: center;
  color: var(--muted);
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  z-index: 50;
}

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 2px);
  background: var(--surface);
  box-shadow: 0 24px 48px rgba(15, 23, 42, 0.18);
  width: min(440px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  overflow: auto;
  z-index: 51;
}

.modal-wide { width: min(640px, calc(100vw - 32px)); }

.modal::backdrop { background: transparent; }

.modal-head,
.modal-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}

.modal-foot {
  border-bottom: 0;
  border-top: 1px solid var(--border);
  justify-content: flex-end;
}

.modal-head h3 { margin: 0; font-size: 1rem; }
.modal-body { padding: 18px; }

.hint {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 13px;
}

.import-result {
  margin-top: 12px;
  padding: 12px;
  background: #f8fafc;
  border-radius: 8px;
  font-size: 13px;
  white-space: pre-wrap;
}

.inline-form {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.inline-form input,
.inline-form select {
  flex: 1 1 120px;
  width: auto;
}

.recurring-list,
.category-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.recurring-list li,
.category-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.recurring-list li:last-child,
.category-list li:last-child { border-bottom: 0; }

.item-meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.item-meta strong { font-weight: 600; }
.item-meta span { font-size: 12px; color: var(--muted); }

.item-actions { display: flex; gap: 6px; }

@media (max-width: 900px) {
  .summary-grid { grid-template-columns: repeat(2, 1fr); }
  .panels { grid-template-columns: 1fr; }
}

@media (max-width: 600px) {
  .summary-grid { grid-template-columns: 1fr; }
  .topbar { flex-direction: column; align-items: stretch; }
  .cat-row { grid-template-columns: 1fr; gap: 6px; }
  .panel-actions { width: 100%; }
  .search { flex: 1; width: auto; }
}
