:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #16181c;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --danger: #dc2626;
  --income: #16a34a;
  --chip-bg: #eef2ff;
  --chip-text: #3730a3;
  --nav-bg: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121417;
    --surface: #1b1e23;
    --text: #f2f3f5;
    --text-muted: #9aa0a8;
    --border: #2b2f36;
    --accent: #5b8def;
    --danger: #f87171;
    --income: #4ade80;
    --chip-bg: #26304a;
    --chip-text: #b7c4ff;
    --nav-bg: #1b1e23;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  padding-bottom: calc(64px + env(safe-area-inset-bottom));
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px;
}

.app-header h1 {
  margin: 0;
  font-size: 1.25rem;
}

.view { padding: 12px 16px 24px; }
.hidden { display: none !important; }

.month-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 8px 0;
  font-size: 1.05rem;
  font-weight: 600;
}

.icon-btn {
  border: none;
  background: var(--surface);
  color: var(--text);
  font-size: 1.4rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.total-bar {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  margin: 8px 0 16px;
}

.total-label { color: var(--text-muted); font-size: 0.9rem; }
.total-value { font-size: 1.3rem; font-weight: 700; }

.list-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  min-height: 32px;
}

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.95rem;
  font-weight: 600;
  padding: 8px 4px;
}

.txn-list, .rule-list, .category-list, .picker-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.txn-row, .rule-row, .category-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.txn-row.excluded { opacity: 0.5; }

.txn-checkbox {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  display: none;
}

.select-mode .txn-checkbox { display: block; }

.txn-main {
  flex: 1;
  min-width: 0;
}

.txn-desc {
  font-weight: 600;
  font-size: 0.98rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.txn-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  flex-wrap: wrap;
}

.chip {
  background: var(--chip-bg);
  color: var(--chip-text);
  border: none;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.8rem;
  font-weight: 600;
}

.chip.uncategorized {
  background: transparent;
  border: 1px dashed var(--text-muted);
  color: var(--text-muted);
}

.exclude-btn {
  border: 1px solid var(--border);
  background: none;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  padding: 4px 10px;
  border-radius: 999px;
}

.exclude-btn.excluded {
  color: var(--danger);
  border-color: var(--danger);
}

.confirm-btn {
  color: var(--accent);
  border-color: var(--accent);
}

.note-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
}

/* Double class to out-specificity the generic input[type="text"] rule below,
   which otherwise wins on equal specificity by appearing later in the file. */
.note-input.note-input {
  flex: 1;
  min-width: 0;
  margin-top: 0;
  border: none;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  background: transparent;
  padding: 4px 2px;
  font-size: 0.85rem;
  color: var(--text);
}

.note-input.note-input::placeholder {
  color: var(--text-muted);
}

.note-input.note-input:focus {
  outline: none;
  border-bottom-color: var(--accent);
}

.note-clear {
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 4px 2px;
  flex-shrink: 0;
}

.txn-amount-col {
  text-align: right;
  flex-shrink: 0;
}

.txn-amount {
  font-weight: 700;
  font-size: 1rem;
}

.txn-amount.income { color: var(--income); }
.txn-amount.expense { color: var(--text); }

.txn-date {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.mono-comment {
  font-size: 0.82rem;
  font-style: italic;
  color: var(--text-muted);
  margin-top: 4px;
}

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

.rule-row, .category-row { justify-content: space-between; }
.rule-pattern { font-weight: 600; }
.rule-amount-note { font-size: 0.75rem; color: var(--text-muted); margin-top: 2px; }
.rule-arrow { color: var(--text-muted); margin: 0 8px; }
.category-name { font-weight: 600; }
.category-desc { font-size: 0.8rem; color: var(--text-muted); margin-top: 2px; }

.category-row-main {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.category-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 1px solid rgba(0, 0, 0, 0.08);
}

.category-row-actions { display: flex; gap: 4px; }
.reorder-btn {
  border: none;
  background: var(--bg);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  font-size: 1rem;
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--nav-bg);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10;
}

.nav-btn {
  flex: 1;
  border: none;
  background: none;
  color: var(--text-muted);
  padding: 10px 0 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 0.72rem;
}

.nav-btn.active { color: var(--accent); }
.nav-icon { font-size: 1.3rem; }

.bulk-bar {
  position: fixed;
  bottom: calc(64px + env(safe-area-inset-bottom));
  left: 0;
  right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  gap: 10px;
  z-index: 9;
}

.primary-btn, .secondary-btn, .danger-btn {
  flex: 1;
  border: none;
  border-radius: 12px;
  padding: 14px;
  font-size: 1rem;
  font-weight: 600;
}

.primary-btn { background: var(--accent); color: #fff; }
.secondary-btn { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.danger-btn { background: transparent; color: var(--danger); border: 1px solid var(--danger); }

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 19;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 12px 20px calc(20px + env(safe-area-inset-bottom));
  z-index: 20;
  max-height: 80vh;
  overflow-y: auto;
}

/* These two can be opened while another sheet (excluded/uncategorized/review)
   is already showing underneath, so they need to win the stacking order
   regardless of DOM position. */
#category-picker, #confirm-rule-sheet, #confirm-dialog {
  z-index: 21;
}

.sheet-handle {
  width: 40px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 4px auto 12px;
}

.sheet h2 { margin: 0 0 12px; font-size: 1.1rem; }

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

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

.picker-item-btn {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 14px 10px;
  background: none;
  border: none;
  font: inherit;
  font-weight: 600;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.picker-item-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.picker-item-clear {
  color: var(--text-muted);
  font-style: italic;
}

.category-summary-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.category-summary-label {
  flex: 1;
  min-width: 0;
}

.category-summary-row strong {
  flex-shrink: 0;
}

.category-summary {
  margin-bottom: 16px;
}

.analytics-category-item {
  border-bottom: 1px solid var(--border);
}

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

.analytics-category-header {
  width: 100%;
  padding: 14px 10px;
  background: none;
  border: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.analytics-category-header:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.analytics-chevron {
  display: inline-block;
  color: var(--text-muted);
  font-size: 1.1rem;
  flex-shrink: 0;
  transition: transform 0.15s ease;
}

.analytics-category-item.expanded .analytics-chevron {
  transform: rotate(90deg);
}

.category-edit-icon {
  background: none;
  border: none;
  font-size: 0.95rem;
  padding: 4px 6px;
  flex-shrink: 0;
  opacity: 0.55;
  cursor: pointer;
}

.category-edit-icon:hover,
.category-edit-icon:focus-visible {
  opacity: 1;
}

.analytics-category-txns {
  padding: 0 0 10px 4px;
}

.analytics-category-txns.hidden {
  display: none;
}

.analytics-category-txns .empty-state {
  padding: 8px 10px 14px;
  font-size: 0.85rem;
}

.field-label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin: 12px 0 6px;
}

input[type="text"], select, textarea {
  width: 100%;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
}

.sheet-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 16px;
}

.toast {
  position: fixed;
  bottom: calc(80px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 0.9rem;
  z-index: 30;
}

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 28px 24px;
}

.login-title {
  margin: 0 0 4px;
  font-size: 1.3rem;
  text-align: center;
}

.login-subtitle {
  margin: 0 0 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.google-btn {
  width: 100%;
  margin-top: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-decoration: none;
  background: #4285f4;
}

.header-actions {
  position: absolute;
  right: 16px;
  top: calc(env(safe-area-inset-top) + 12px);
}

.logout-btn {
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 0.85rem;
  padding: 6px;
}

.period-toggle {
  display: flex;
  gap: 6px;
  justify-content: center;
  padding-top: 8px;
}

.period-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.82rem;
  font-weight: 600;
  padding: 5px 14px;
  border-radius: 999px;
}

.period-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.delta-line {
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: -8px;
  margin-bottom: 12px;
}

.delta-line.up { color: var(--danger); }
.delta-line.down { color: var(--income); }

.search-bar { margin-bottom: 10px; }
.search-bar input {
  width: 100%;
}

.list-toolbar.chips {
  flex-wrap: wrap;
  row-gap: 6px;
  justify-content: flex-start;
}

.list-toolbar.chips .link-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.82rem;
}

.list-toolbar.chips .link-btn span:not(:empty)::before {
  content: " ";
}

.list-toolbar.chips .link-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

#select-toggle,
#select-all-toggle {
  background: none !important;
  border: none !important;
  padding: 8px 4px !important;
}

.limits-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.limit-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
}

.limit-row-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.limit-row-name {
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}

.limit-input {
  width: 90px;
  text-align: right;
  padding: 6px 8px;
  font-size: 0.85rem;
}

.limit-bar-track {
  height: 8px;
  border-radius: 4px;
  background: var(--bg);
  overflow: hidden;
}

.limit-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 4px;
}

.limit-bar-fill.warn { background: #d97706; }
.limit-bar-fill.over { background: var(--danger); }

.limit-row-spent {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.icon-color-row {
  display: flex;
  gap: 16px;
  margin-top: 12px;
}

.icon-color-row > div { flex: 1; }

.icon-color-row input[type="color"] {
  padding: 2px;
  height: 42px;
}

.sheet-hint {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin: 4px 0 12px;
}
