:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface-2: #243044;
  --border: #2d3a4f;
  --text: #e8edf4;
  --muted: #8b9cb3;
  --income: #34d399;
  --expense: #f87171;
  --accent: #60a5fa;
  --pool: #a78bfa;
  --radius: 10px;
  --font: "DM Sans", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
}

.header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.header-brand {
  flex: 1;
}

.budget-switcher {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.budget-switcher select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.35rem 0.6rem;
  border-radius: 6px;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.header h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.header p {
  margin: 0.2rem 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.filters {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.filters label {
  font-size: 0.8rem;
  color: var(--muted);
}

.filters input[type="date"] {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.45rem 0.6rem;
  border-radius: 6px;
  font-family: inherit;
}

.filters button {
  background: var(--accent);
  color: #0f1419;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
}

.filters button.secondary {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}

.summary-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
  padding: 1.25rem 1.5rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
}

.card .label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.card .value {
  font-size: 1.5rem;
  font-weight: 700;
  margin-top: 0.35rem;
}

.card.income .value {
  color: var(--income);
}

.card.expense .value {
  color: var(--expense);
}

.card.net .value.positive {
  color: var(--income);
}

.card.net .value.negative {
  color: var(--expense);
}

.main {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 1rem;
  padding: 0 1.5rem 1.5rem;
  min-height: 520px;
}

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

.chart-panel,
.detail-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.chart-panel {
  padding: 0.5rem;
  min-height: 480px;
}

#sankey-chart {
  width: 100%;
  height: 460px;
}

.detail-panel {
  display: flex;
  flex-direction: column;
}

.detail-header {
  padding: 1rem 1.1rem;
  border-bottom: 1px solid var(--border);
}

.detail-header h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.detail-header .breadcrumb {
  margin-top: 0.35rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.detail-header .breadcrumb button {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  padding: 0;
  font-size: inherit;
  font-family: inherit;
}

.detail-body {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.placeholder {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 0.85rem;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.15s, border-color 0.15s;
}

.list-item:hover,
.list-item.active {
  background: var(--surface-2);
  border-color: var(--border);
}

.list-item .name {
  font-weight: 500;
  font-size: 0.92rem;
}

.list-item .meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.list-item .amount {
  font-weight: 600;
  color: var(--expense);
  white-space: nowrap;
}

.tx-row {
  padding: 0.7rem 0.85rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s;
}

.tx-row:hover {
  background: var(--surface-2);
}

.tx-row:last-child {
  border-bottom: none;
}

.tx-row .top {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
}

.tx-row .desc {
  font-size: 0.88rem;
}

.tx-row .date {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.2rem;
}

.tx-row .amount {
  font-weight: 600;
  color: var(--expense);
}

.hint {
  padding: 0.75rem 1.1rem;
  font-size: 0.78rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

.loading {
  opacity: 0.6;
  pointer-events: none;
}

.tabs {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.tab {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 0.4rem 0.85rem;
  border-radius: 6px;
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}

.tab.active {
  background: var(--accent);
  color: #0f1419;
  border-color: var(--accent);
  font-weight: 600;
}

.view-panel {
  display: none;
  padding: 0 1.5rem 1.5rem;
}

.view-panel.active {
  display: grid;
}

#view-categorize.active,
#view-categories.active,
#view-import.active {
  display: block;
}

.panel-wide {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.panel-header h2 {
  margin: 0;
  font-size: 1rem;
}

.muted {
  color: var(--muted);
  font-size: 0.9rem;
}

.inbox-list .inbox-item {
  position: relative;
  border-bottom: 1px solid var(--border);
}

.inbox-item .swipe-bg {
  position: absolute;
  inset: 0;
  background: #ef4444;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 1.25rem;
  color: white;
  font-weight: 600;
  font-size: 0.9rem;
  pointer-events: none;
}

.inbox-item .swipe-content {
  display: flex;
  align-items: stretch;
  background: var(--surface);
  position: relative;
}

.inbox-item .tx-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem 0.85rem;
  min-width: 0;
}

.inbox-item .tx-top {
  width: 100%;
}

.inbox-item .swipe-handle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  min-height: 100%;
  background: var(--surface-2);
  border-left: 1px solid var(--border);
  color: var(--muted);
  font-size: 1.1rem;
  cursor: grab;
  touch-action: none;
  user-select: none;
  flex-shrink: 0;
}

.inbox-item:last-child {
  border-bottom: none;
}

.inbox-item .desc {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.inbox-item .categorize-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
}

.btn-transfer {
  background: var(--surface-2) !important;
  color: var(--muted) !important;
  border: 1px solid var(--border) !important;
}

.hidden-section {
  margin-top: 1.5rem;
  border-top: 1px solid var(--border);
  padding-top: 1rem;
}

.hidden-section summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 0.9rem;
  padding: 0.25rem 0;
  user-select: none;
}

.hidden-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 0.85rem;
  border-bottom: 1px solid var(--border);
  gap: 1rem;
}

.inbox-item .meta {
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 0.2rem;
}

.inbox-item .categorize-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.inbox-item select,
.cat-form input,
.cat-form select,
.import-form select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.4rem 0.55rem;
  border-radius: 6px;
  font-family: inherit;
  font-size: 16px; /* must be >= 16px or iOS blocks native picker */
}

.inbox-item button,
.btn-add,
.btn-small,
.import-form button[type="submit"] {
  background: var(--accent);
  color: #0f1419;
  border: none;
  padding: 0.4rem 0.75rem;
  border-radius: 6px;
  font-weight: 600;
  font-family: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}

.btn-small.danger {
  background: transparent;
  color: var(--expense);
  border: 1px solid var(--expense);
}

.btn-small.secondary {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}

.btn-add {
  font-size: 0.8rem;
}

.categories-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 1rem;
}

.cat-column {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
}

.cat-tree {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.cat-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem 0.85rem;
}

.cat-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.cat-card-header .cat-name {
  font-weight: 600;
  font-size: 0.95rem;
}

.cat-card-actions {
  display: flex;
  gap: 0.35rem;
}

.sub-list {
  margin: 0.6rem 0 0;
  padding: 0;
  list-style: none;
}

.sub-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.35rem 0;
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
}

.sub-list li:first-child {
  border-top: none;
  padding-top: 0.15rem;
}

.cat-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px dashed var(--border);
}

.cat-form input {
  flex: 1;
  min-width: 120px;
}

.import-form {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: flex-end;
  margin-top: 1rem;
}

.import-form label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.import-result {
  margin-top: 1rem;
  padding: 0.75rem;
  background: var(--surface-2);
  border-radius: 8px;
  font-size: 0.8rem;
  overflow-x: auto;
}

.toast {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 0.65rem 1rem;
  border-radius: 8px;
  font-size: 0.85rem;
  z-index: 100;
  max-width: 320px;
}

.toast.error {
  border-color: var(--expense);
  color: var(--expense);
}

.rules-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.rules-header h3 {
  margin: 0;
  font-size: 1rem;
}

.rules-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.rules-table th {
  text-align: left;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.rules-table td {
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.pending-badge {
  display: inline-block;
  padding: 1px 6px;
  font-size: 0.65rem;
  font-weight: 600;
  background: rgba(245, 158, 11, 0.18);
  color: #f59e0b;
  border-radius: 4px;
  margin-left: 6px;
  vertical-align: middle;
  letter-spacing: 0.04em;
}

.rules-table .actions-row {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
}

.rules-table .inline-flex {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}

.rules-table tr:last-child td {
  border-bottom: none;
}

.rules-table .pattern-cell {
  font-family: monospace;
  font-size: 0.82rem;
  color: var(--accent);
}

.rules-table .actions-cell {
  white-space: nowrap;
}

.rules-table .actions-cell .btn-small {
  margin-right: 0.25rem;
}

.hit-count-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--accent);
  padding: 0.2rem 0.55rem;
  border-radius: 5px;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.hit-count-btn:hover {
  background: var(--border);
}

.modal-box-wide {
  max-width: 560px;
}

.rule-tx-list {
  max-height: 55vh;
  overflow-y: auto;
  padding: 0;
}

.rule-tx-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.65rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.rule-tx-row:last-child {
  border-bottom: none;
}

.rule-tx-desc {
  font-size: 0.88rem;
  font-weight: 500;
}

.rule-tx-meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 1rem;
}

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
  max-width: 380px;
}

.modal-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.modal-title {
  font-weight: 600;
  font-size: 1rem;
  margin-bottom: 0.4rem;
}

.modal-desc {
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.modal-amount {
  font-size: 0.82rem;
  color: var(--muted);
  margin-top: 0.2rem;
}

.modal-body {
  padding: 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.modal-label {
  font-size: 0.8rem;
  color: var(--muted);
}

.modal-text-input {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.4rem 0.55rem;
  border-radius: 6px;
  font-family: inherit;
  font-size: 16px;
  width: 100%;
}

.modal-body select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.4rem 0.55rem;
  border-radius: 6px;
  font-family: inherit;
  font-size: 16px;
  width: 100%;
}

.modal-actions {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
  justify-content: flex-end;
}

.modal-cancel {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 0.45rem 1rem;
  border-radius: 6px;
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}

.modal-confirm {
  background: var(--pool);
  color: #0f1419;
  border: none;
  padding: 0.45rem 1rem;
  border-radius: 6px;
  font-weight: 600;
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}

.db-error-banner {
  margin: 0;
  padding: 1rem 1.5rem;
  background: rgba(248, 113, 113, 0.15);
  border-bottom: 1px solid var(--expense);
  font-size: 0.9rem;
}

.db-error-banner strong {
  color: var(--expense);
}

.db-error-banner p {
  margin: 0.35rem 0 0;
}

.muted-cell {
  color: var(--muted);
  font-style: italic;
}

.db-error-banner code {
  font-size: 0.8rem;
  background: var(--surface-2);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}

/* Rule enabled/disabled toggle */
.rule-toggle-btn {
  padding: 0.2rem 0.6rem;
  border-radius: 5px;
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid transparent;
  transition: opacity 0.15s;
}

.rule-toggle-btn.enabled {
  background: rgba(52, 211, 153, 0.15);
  border-color: var(--income);
  color: var(--income);
}

.rule-toggle-btn.disabled {
  background: var(--surface-2);
  border-color: var(--border);
  color: var(--muted);
}

.rule-toggle-btn:hover {
  opacity: 0.75;
}

.rules-table tr.rule-disabled td {
  opacity: 0.45;
}

.rules-table tr.rule-disabled .rule-toggle-btn {
  opacity: 1;
}

/* Split modal */
.split-modal-body {
  gap: 0.75rem;
}

.split-rows {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.split-row {
  display: grid;
  grid-template-columns: 100px 1fr 1fr 1fr auto;
  gap: 0.4rem;
  align-items: center;
}

.split-remaining {
  font-size: 0.82rem;
  color: var(--muted);
  padding-top: 0.25rem;
}

.split-remaining span {
  font-weight: 600;
}
