:root {
  --bg: #f5f6fb;
  --panel: #ffffff;
  --panel-2: #f0f1f8;
  --text: #191c2b;
  --muted: #6b7280;
  --border: #e3e5ef;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --danger: #dc2626;
  --ok: #059669;
  --warn: #d97706;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 8px 24px -12px rgba(16, 24, 40, .18);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1017;
    --panel: #171a24;
    --panel-2: #1f2330;
    --text: #e7e9f2;
    --muted: #98a0b3;
    --border: #2a2f3f;
    --accent: #7c7bf5;
    --accent-soft: #24243f;
    --danger: #f87171;
    --ok: #34d399;
    --warn: #fbbf24;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 12px 30px -14px rgba(0, 0, 0, .7);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

body { min-height: 100vh; }

.hidden { display: none !important; }

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; }

p { margin: .4rem 0; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.muted { color: var(--muted); }
.small { font-size: .82rem; }
.grow { flex: 1; min-width: 0; }

/* ── Кнопки ─────────────────────────────────────────────────────────── */
.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: .55rem .9rem;
  border-radius: 10px;
  font-size: .9rem;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, transform .05s ease;
  white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--panel-2); }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; }
.btn.small { padding: .35rem .6rem; font-size: .8rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.icon-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
  padding: .3rem .45rem;
  border-radius: 8px;
  line-height: 1;
}
.icon-btn:hover { background: var(--panel-2); color: var(--danger); }

/* ── Авторизация ────────────────────────────────────────────────────── */
.auth {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}
.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 1.6rem;
  box-shadow: var(--shadow);
}
.brand {
  display: flex;
  gap: .8rem;
  align-items: center;
  margin-bottom: 1.3rem;
}
.brand img { width: 46px; height: 46px; border-radius: 12px; }
.brand.small { margin: 0; }
.brand.small img { width: 30px; height: 30px; border-radius: 8px; }
.brand h1 { font-size: 1.2rem; }
.brand p { margin: .2rem 0 0; color: var(--muted); font-size: .82rem; }

/* ── Формы ──────────────────────────────────────────────────────────── */
label { display: block; font-size: .82rem; color: var(--muted); font-weight: 500; }
label input, label select { margin-top: .3rem; }

input, select, textarea {
  width: 100%;
  padding: .6rem .7rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  font-size: .92rem;
  font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent-soft);
  border-color: var(--accent);
}

.stack-form { display: flex; flex-direction: column; gap: .8rem; }
.inline-form {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  align-items: center;
}
.inline-form input, .inline-form select { width: auto; flex: 1 1 140px; min-width: 0; }
.inline-form .btn { flex: 0 0 auto; }
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: .5rem;
  align-items: center;
}
.form-grid .btn { grid-column: 1 / -1; justify-self: start; }

.switch { text-align: center; margin-top: 1rem; font-size: .88rem; color: var(--muted); }

.switch-row {
  display: flex;
  align-items: center;
  gap: .6rem;
  font-size: .9rem;
  color: var(--text);
  font-weight: 500;
  cursor: pointer;
}
.switch-row input[type="checkbox"] { width: auto; margin: 0; accent-color: var(--accent); }
.admin-group { border-top: 1px solid var(--border); margin-top: .9rem; padding-top: .9rem; }
.admin-group h3 { margin-bottom: .5rem; }

/* Поля даты/времени: ввод вручную + кнопка календаря */
.dt-wrap { display: flex; align-items: center; gap: .4rem; min-width: 0; }
.dt-wrap input[type="date"], .dt-wrap input[type="month"] { flex: 1 1 auto; min-width: 0; }
.dt-wrap input[type="time"] { flex: 0 0 105px; }
.dt-wrap .dt-pick {
  flex: 0 0 auto;
  font-size: 1rem;
  border: 1px solid var(--border);
  background: var(--panel);
  padding: .45rem .55rem;
  border-radius: 10px;
}
.span-2 { grid-column: 1 / -1; }

/* Переключатель «Список / Неделя» */
.seg {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}
.seg button {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: .35rem .7rem;
  font-size: .82rem;
  cursor: pointer;
}
.seg button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* Недельный вид */
.week-nav { display: flex; align-items: center; gap: .4rem; }
.week-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(110px, 1fr));
  gap: .4rem;
  overflow-x: auto;
  padding-bottom: .3rem;
}
.week-col {
  background: var(--panel-2);
  border-radius: 10px;
  padding: .4rem;
  min-height: 90px;
}
.week-col.today { outline: 2px solid var(--accent); }
.week-col-head {
  font-size: .78rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: capitalize;
  margin-bottom: .35rem;
}
.week-event {
  display: flex;
  flex-direction: column;
  gap: .1rem;
  width: 100%;
  text-align: left;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 8px;
  padding: .3rem .4rem;
  margin-bottom: .3rem;
  cursor: pointer;
  color: var(--text);
}
.week-event:hover { border-color: var(--accent); }
.we-time { font-size: .7rem; color: var(--muted); }
.we-title { font-size: .8rem; font-weight: 500; word-break: break-word; }
.week-empty { color: var(--muted); font-size: .8rem; text-align: center; }

@media (max-width: 700px) {
  .week-grid { grid-template-columns: repeat(7, 140px); }
}

/* ── Каркас ─────────────────────────────────────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 960px;
  margin: 0 auto;
  padding: .7rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.user-box { display: flex; align-items: center; gap: .5rem; }

.tabs {
  position: sticky;
  top: 57px;
  z-index: 19;
  display: flex;
  gap: .35rem;
  overflow-x: auto;
  padding: .55rem 1rem;
  max-width: 960px;
  margin: 0 auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  padding: .42rem .8rem;
  border-radius: 999px;
  font-size: .87rem;
  cursor: pointer;
  white-space: nowrap;
  font-weight: 500;
}
.tabs button:hover { background: var(--panel-2); }
.tabs button.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 30%, transparent);
}

main {
  max-width: 960px;
  margin: 0 auto;
  padding: .6rem 1rem 4rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* ── Карточки ───────────────────────────────────────────────────────── */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  box-shadow: var(--shadow);
}
.card > h2 { margin-bottom: .2rem; }
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .8rem;
  margin-bottom: .7rem;
}

.stack { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .15rem; }
.row {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .55rem .4rem;
  border-radius: 10px;
}
.row:hover { background: var(--panel-2); }
.row .title { font-weight: 500; font-size: .93rem; word-break: break-word; }
.row.done .title { text-decoration: line-through; color: var(--muted); }
.row .time {
  min-width: 52px;
  font-variant-numeric: tabular-nums;
  font-size: .82rem;
  color: var(--muted);
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 1.6rem 1rem;
  border: 1px dashed var(--border);
  border-radius: 12px;
  font-size: .9rem;
}
.loading { text-align: center; color: var(--muted); padding: 2rem; }

.check {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
  flex: 0 0 auto;
  cursor: pointer;
}

/* ── Бюджет ─────────────────────────────────────────────────────────── */
.cards-3 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: .7rem;
}
.metric {
  background: var(--panel-2);
  border-radius: 12px;
  padding: .8rem .9rem;
}
.metric .label { font-size: .76rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.metric .value { font-size: 1.2rem; font-weight: 650; margin-top: .2rem; font-variant-numeric: tabular-nums; }
.metric.income .value { color: var(--ok); }
.metric.expense .value { color: var(--danger); }

.amount { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.amount.expense { color: var(--danger); }
.amount.income { color: var(--ok); }

/* ── Коды и подсказки ───────────────────────────────────────────────── */
.code-row {
  display: flex;
  align-items: center;
  gap: .5rem;
  background: var(--panel-2);
  border-radius: 10px;
  padding: .5rem .7rem;
  margin: .35rem 0;
  flex-wrap: wrap;
}
.code-row code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .9rem;
  word-break: break-all;
  flex: 1;
}
.hint {
  font-size: .84rem;
  color: var(--muted);
  background: var(--panel-2);
  border-left: 3px solid var(--accent);
  padding: .6rem .8rem;
  border-radius: 8px;
  margin: .6rem 0;
}
.badge {
  display: inline-block;
  font-size: .72rem;
  font-weight: 600;
  padding: .15rem .5rem;
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--muted);
}
.badge.ok { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); }
.badge.warn { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }

.day-head {
  font-size: .8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: capitalize;
  margin: 1rem 0 .2rem;
}

/* ── Тост ───────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  left: 50%;
  bottom: 1.4rem;
  transform: translate(-50%, 20px);
  background: var(--text);
  color: var(--bg);
  padding: .65rem 1rem;
  border-radius: 10px;
  font-size: .88rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  z-index: 100;
  max-width: calc(100vw - 2rem);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

@media (max-width: 520px) {
  .tabs { top: 56px; }
  main { padding: .4rem .7rem 4rem; }
  .topbar-inner { padding: .6rem .7rem; }
  .card { padding: .9rem; }
  .inline-form input, .inline-form select { flex: 1 1 100%; }
  .inline-form .btn { width: 100%; }
}
