:root {
  color-scheme: dark;

  --bg: #0e1116;
  --surface: #161a22;
  --surface-2: #1b2029;
  --surface-3: #212734;
  --border: #262d3a;
  --border-strong: #333c4d;

  --text: #e6e9ef;
  --text-dim: #9aa4b6;
  --text-faint: #6b7688;

  --accent: #7aa2f7;
  --accent-strong: #8fb3ff;
  --danger: #f7768e;
  --success: #9ece6a;
  --warning: #e0af68;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .25);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -.01em; }

[hidden] { display: none !important; }

.tabular { font-variant-numeric: tabular-nums; }

/* ── Layout ──────────────────────────────────────────────────────────── */

.centered {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(14, 17, 22, .85);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.topbar-inner,
.monthbar-inner,
.main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 20px;
}

.topbar-inner {
  display: flex;
  align-items: center;
  gap: 20px;
  height: 56px;
}

.brand {
  font-weight: 650;
  letter-spacing: -.02em;
  font-size: 16px;
}

.tabs {
  display: flex;
  gap: 2px;
  flex: 1;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
  appearance: none;
  border: 0;
  background: none;
  color: var(--text-dim);
  font: inherit;
  font-size: 14px;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, color .15s;
}
.tab:hover { color: var(--text); background: var(--surface-2); }
.tab[aria-selected="true"] { color: var(--text); background: var(--surface-3); }

.topbar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-dim);
  font-size: 14px;
}
.username { max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.monthbar { border-bottom: 1px solid var(--border); background: var(--bg); }
.monthbar-inner {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 52px;
}

.month-input {
  width: auto;
  flex: 0 0 auto;
  min-width: 165px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  font: inherit;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
}
.month-input::-webkit-calendar-picker-indicator { filter: invert(.7); cursor: pointer; }

.main { padding-top: 24px; padding-bottom: 64px; }

.view { display: grid; gap: 16px; }

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

/* ── Cards ───────────────────────────────────────────────────────────── */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.card-narrow { max-width: 460px; }

.card-title {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-dim);
  font-weight: 600;
  margin-bottom: 16px;
}

.auth-card { width: min(400px, 100%); box-shadow: var(--shadow); }
.auth-title { font-size: 22px; margin-bottom: 4px; }
.auth-subtitle { color: var(--text-dim); font-size: 14px; margin: 0 0 20px; }

/* ── Stats ───────────────────────────────────────────────────────────── */

.stats {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.stat-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-faint);
  margin-bottom: 6px;
}
.stat-value {
  font-size: 24px;
  font-weight: 620;
  letter-spacing: -.02em;
  font-variant-numeric: tabular-nums;
}
.stat-sub { font-size: 13px; color: var(--text-dim); margin-top: 4px; }
.stat-sub.up { color: var(--danger); }
.stat-sub.down { color: var(--success); }

/* ── Forms ───────────────────────────────────────────────────────────── */

.field { display: block; margin-bottom: 14px; }
.field > span {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.hint { color: var(--text-faint); font-size: 12px; font-weight: 400; }

input, select, .input {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  font: inherit;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, .input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(122, 162, 247, .15);
}
input::placeholder { color: var(--text-faint); }
input[type="color"] { padding: 4px; height: 40px; cursor: pointer; }
input[type="date"]::-webkit-calendar-picker-indicator { filter: invert(.7); cursor: pointer; }

select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-dim) 50%),
                    linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: right 14px center, right 9px center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 30px;
}

.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
  font-size: 14px;
  cursor: pointer;
}
.checkbox input { width: auto; }

.expense-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 12px;
}
.expense-form .field-wide,
.expense-form .form-error,
.expense-form button { grid-column: 1 / -1; }

.category-form .field-color input { max-width: 120px; }

.form-error {
  background: rgba(247, 118, 142, .1);
  border: 1px solid rgba(247, 118, 142, .3);
  color: var(--danger);
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin: 0 0 14px;
}

.row-end { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }

/* ── Buttons ─────────────────────────────────────────────────────────── */

.btn {
  appearance: none;
  border: 1px solid transparent;
  background: var(--surface-3);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: background .15s, border-color .15s, opacity .15s;
  white-space: nowrap;
}
.btn:hover { background: var(--border-strong); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #0b0f16; font-weight: 600; }
.btn-primary:hover { background: var(--accent-strong); }
.btn-danger { background: var(--danger); color: #1a0d11; font-weight: 600; }
.btn-danger:hover { background: #ff8ba3; }
.btn-ghost { background: none; border-color: var(--border-strong); color: var(--text-dim); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 11px; font-size: 13px; }
.btn-icon { padding: 6px 12px; font-size: 18px; line-height: 1; background: var(--surface-2); }

/* ── Breakdown ───────────────────────────────────────────────────────── */

.breakdown { display: grid; gap: 14px; }

.break-row { display: grid; gap: 6px; }
.break-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 14px;
}
.break-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.break-amount { font-variant-numeric: tabular-nums; font-weight: 550; }
.break-share { color: var(--text-faint); font-size: 12px; min-width: 38px; text-align: right; }

.bar {
  height: 6px;
  background: var(--surface-3);
  border-radius: 3px;
  overflow: hidden;
}
.bar-fill { height: 100%; border-radius: 3px; transition: width .3s ease; }

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex: 0 0 auto;
  display: inline-block;
}

/* ── Day chart ───────────────────────────────────────────────────────── */

.daychart {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 120px;
  padding-top: 8px;
}
.day {
  flex: 1;
  min-width: 0;
  background: var(--surface-3);
  border-radius: 2px 2px 0 0;
  min-height: 2px;
  position: relative;
  transition: background .15s;
}
.day[data-has-value="1"] { background: var(--accent); opacity: .75; }
.day:hover { opacity: 1; background: var(--accent-strong); }

/* ── Tables & lists ──────────────────────────────────────────────────── */

.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.toolbar .input { width: auto; flex: 1 1 180px; max-width: 320px; }
.toolbar select.input { flex: 0 1 210px; }
.spacer { flex: 1; }

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

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-faint);
  font-weight: 600;
  padding: 0 10px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td {
  padding: 11px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
.col-amount { text-align: right; font-variant-numeric: tabular-nums; font-weight: 550; white-space: nowrap; }
.col-actions { text-align: right; white-space: nowrap; width: 1%; }
.col-date { white-space: nowrap; color: var(--text-dim); }
.cell-category { display: flex; align-items: center; gap: 7px; }
.desc-empty { color: var(--text-faint); }

.list { display: grid; gap: 8px; }
.list-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.list-main { flex: 1; min-width: 0; }
.list-title { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-sub { font-size: 12px; color: var(--text-faint); margin-top: 2px; }
.list-actions { display: flex; gap: 6px; }

.badge {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .05em;
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
}
.badge-admin { color: var(--accent); border-color: rgba(122, 162, 247, .4); }
.badge-disabled { color: var(--danger); border-color: rgba(247, 118, 142, .4); }

.empty {
  text-align: center;
  color: var(--text-faint);
  padding: 32px 16px;
  font-size: 14px;
}

/* ── Dialog ──────────────────────────────────────────────────────────── */

.dialog {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 22px;
  width: min(420px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(0, 0, 0, .6); backdrop-filter: blur(2px); }
.dialog-text { color: var(--text-dim); font-size: 14px; margin: 0 0 16px; }

/* ── Toast ───────────────────────────────────────────────────────────── */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  z-index: 100;
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  box-shadow: var(--shadow);
  max-width: calc(100vw - 32px);
}
.toast.error { border-color: rgba(247, 118, 142, .5); color: var(--danger); }

/* ── Responsive ──────────────────────────────────────────────────────── */

@media (max-width: 720px) {
  .topbar-inner { height: auto; padding-top: 10px; padding-bottom: 10px; flex-wrap: wrap; }
  .tabs { order: 3; width: 100%; flex: none; }
  .topbar-user { margin-left: auto; }
  .expense-form { grid-template-columns: 1fr; }
  .stat-value { font-size: 20px; }
  .main { padding-left: 14px; padding-right: 14px; }
  .topbar-inner, .monthbar-inner { padding-left: 14px; padding-right: 14px; }
  .card { padding: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
