:root {
  color-scheme: light dark;
  --bg: #fafafa;
  --card-bg: #ffffff;
  --text: #1a1a1a;
  --muted: #6b6b6b;
  --border: #e2e2e2;
  --income: #1a7f37;
  --expense: #b42318;
  --accent: #2563eb;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --card-bg: #1c1c1e;
    --text: #f0f0f0;
    --muted: #9a9a9a;
    --border: #333333;
    --income: #4ade80;
    --expense: #f87171;
    --accent: #60a5fa;
  }
}

* { box-sizing: border-box; }

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

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.brand { font-weight: 700; font-size: 1.2rem; color: var(--text); text-decoration: none; }

nav { display: flex; align-items: center; gap: 1.2rem; }
nav a { color: var(--text); text-decoration: none; }
nav a:hover { color: var(--accent); }

.link-button {
  background: none; border: none; padding: 0; font: inherit; color: var(--muted); cursor: pointer;
}
.link-button:hover { color: var(--accent); }

main {
  max-width: 900px;
  margin: 0 auto;
  padding: 1.5rem;
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.2rem 1.5rem;
  margin-bottom: 1.2rem;
}

.balance-card {
  text-align: center;
  padding: 2rem 1rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 1.5rem;
}
.balance-label { color: var(--muted); font-size: 0.9rem; text-transform: uppercase; letter-spacing: 0.05em; }
.balance-amount { font-size: 3rem; font-weight: 700; margin: 0.2rem 0; }
.balance-amount.negative { color: var(--expense); }
.balance-sub { color: var(--muted); font-size: 0.85rem; }

.callout {
  border-radius: 10px;
  padding: 1rem 1.5rem;
  margin-bottom: 1.2rem;
  border: 1px solid var(--border);
}
.callout h2 { margin-top: 0; font-size: 1rem; }
.callout ul { margin: 0; padding-left: 1.2rem; }
.callout-highlight { background: rgba(96, 165, 250, 0.08); border-color: var(--accent); }
.callout-annual { background: rgba(250, 204, 21, 0.08); border-color: #eab308; }
.callout-ending { background: rgba(248, 113, 113, 0.08); border-color: var(--expense); }

.periods {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.2rem;
  margin-bottom: 1.2rem;
}
@media (max-width: 640px) {
  .periods { grid-template-columns: 1fr; }
}

.period-col {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.2rem;
}
.period-col h2 { margin-top: 0; margin-bottom: 0.2rem; }
.period-subtotal { color: var(--muted); font-size: 0.85rem; margin-bottom: 0.6rem; }
.period-col h3 { font-size: 0.8rem; text-transform: uppercase; color: var(--muted); margin: 0.8rem 0 0.3rem; }

.txn-list { list-style: none; margin: 0; padding: 0; }
.txn-list li {
  display: flex;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.3rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.92rem;
}
.txn-list.muted li { color: var(--muted); }
.txn-list li.empty { color: var(--muted); font-style: italic; border-bottom: none; }
.txn-list .date { color: var(--muted); margin-right: 0.4rem; min-width: 1.5rem; display: inline-block; }
.amount.income { color: var(--income); margin-left: auto; }
.amount.expense { color: var(--expense); margin-left: auto; }

.form-row { display: flex; gap: 1rem; flex-wrap: wrap; margin-bottom: 0.8rem; }
.form-row label { display: flex; flex-direction: column; font-size: 0.85rem; color: var(--muted); gap: 0.2rem; flex: 1; min-width: 140px; }
.checkbox-row { display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.8rem; }
.checkbox-row input { width: auto; padding: 0; }
.muted-note { color: var(--muted); font-size: 0.85rem; margin-top: -0.4rem; }
.edit-link { color: var(--accent); text-decoration: none; font-size: 0.9rem; margin-right: 0.3rem; }
.cancel-link { color: var(--muted); text-decoration: none; margin-left: 0.8rem; font-size: 0.9rem; }
input, select, button {
  font: inherit;
  padding: 0.5rem 0.6rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
button {
  background: var(--accent);
  color: white;
  border: none;
  cursor: pointer;
  font-weight: 600;
}
button:hover { opacity: 0.9; }
button.danger { background: var(--expense); }

.rules-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.rules-table th, .rules-table td { text-align: left; padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--border); }
.rules-table tr.ended { opacity: 0.5; }
.rule-actions { display: flex; gap: 0.8rem; align-items: center; }
.rule-actions form.inline { display: inline; }

@media (max-width: 640px) {
  .rules-table thead { display: none; }
  .rules-table, .rules-table tbody, .rules-table tr, .rules-table td { display: block; width: 100%; }
  .rules-table tr {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.6rem 0.8rem;
    margin-bottom: 0.8rem;
  }
  .rules-table td {
    border-bottom: none;
    padding: 0.25rem 0;
    display: flex;
    justify-content: space-between;
    gap: 1rem;
  }
  .rules-table td[data-label]:not([data-label=""])::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 0.8rem;
  }
  .rule-actions { justify-content: flex-end; padding-top: 0.4rem; }
}

.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}
.login-form { width: 100%; max-width: 320px; display: flex; flex-direction: column; gap: 0.8rem; }
.login-form h1 { text-align: center; margin-bottom: 0.5rem; }
.login-form label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.85rem; color: var(--muted); }
.error { color: var(--expense); font-size: 0.9rem; }
