:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e3e6ea;
  --text: #16191d;
  --muted: #6b7280;
  --accent: #2563eb;
  --ok: #128a54;
  --warn: #b45309;
  --danger: #b42318;
  --ok-bg: #e7f6ee;
  --warn-bg: #fdf3e3;
  --danger-bg: #fdecea;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 4px 16px rgba(16, 24, 40, .04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1f242c;
    --border: #2a3039;
    --text: #e8eaed;
    --muted: #98a1ad;
    --accent: #6ea8fe;
    --ok: #4ade80;
    --warn: #fbbf24;
    --danger: #f87171;
    --ok-bg: #12291e;
    --warn-bg: #2b2213;
    --danger-bg: #2d1717;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

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

/* --- Шапка ---------------------------------------------------------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 20px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(160%) blur(12px);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 650;
  color: var(--text);
  letter-spacing: -.01em;
}

.brand-mark {
  width: 11px;
  height: 11px;
  border-radius: 3px;
  background: linear-gradient(135deg, var(--accent), var(--ok));
}

.topbar nav {
  display: flex;
  gap: 4px;
  margin-left: auto;
  overflow-x: auto;
}

.topbar nav a {
  padding: 7px 13px;
  border-radius: 9px;
  color: var(--muted);
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
}

.topbar nav a:hover { background: var(--surface-2); color: var(--text); }
.topbar nav a.active { background: var(--surface-2); color: var(--text); }

main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 22px 20px 48px;
}

/* --- Общие блоки --------------------------------------------------------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}

section { margin-bottom: 26px; }

h1 { font-size: 23px; margin: 0 0 4px; letter-spacing: -.02em; }
h2 { font-size: 17px; margin: 0 0 14px; letter-spacing: -.01em; }
h3 { font-size: 15px; margin: 0 0 8px; }

.subtitle { color: var(--muted); font-size: 14px; margin: 0 0 20px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }

/* --- Итог ---------------------------------------------------------------- */
.total-card {
  display: flex;
  flex-wrap: wrap;
  gap: 20px 40px;
  align-items: flex-end;
}

.total-value {
  font-size: 38px;
  font-weight: 640;
  letter-spacing: -.03em;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.total-label { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .06em; }

/* --- Уровни ликвидности -------------------------------------------------- */
.tiers { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(255px, 1fr)); }

.tier-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.tier-name { font-weight: 600; }
.tier-share { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.tier-hint { color: var(--muted); font-size: 12.5px; margin: 2px 0 12px; }

.tier-amount {
  font-size: 25px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.02em;
}

.bar {
  height: 7px;
  border-radius: 99px;
  background: var(--surface-2);
  overflow: hidden;
  margin: 12px 0 8px;
}

.bar > i { display: block; height: 100%; border-radius: 99px; background: var(--accent); }
.bar > i.ok { background: var(--ok); }
.bar > i.low { background: var(--warn); }
.bar > i.high { background: var(--accent); }

.tier-target { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* --- Значки -------------------------------------------------------------- */
.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 9px;
  border-radius: 99px;
  font-size: 12px;
  font-weight: 550;
  white-space: nowrap;
}

.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.low, .pill.warning { background: var(--warn-bg); color: var(--warn); }
.pill.high, .pill.info { background: var(--surface-2); color: var(--muted); }
.pill.critical { background: var(--danger-bg); color: var(--danger); }

/* --- Таблицы ------------------------------------------------------------- */
table { width: 100%; border-collapse: collapse; }

th {
  text-align: left;
  font-size: 12px;
  font-weight: 550;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  padding: 0 10px 9px;
  border-bottom: 1px solid var(--border);
}

td { padding: 12px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
tr:last-child td { border-bottom: none; }
th.right, td.right { text-align: right; }
td.right { font-variant-numeric: tabular-nums; }

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

/* --- Формы --------------------------------------------------------------- */
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }

input[type=text], input[type=number], input[type=date], select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 16px; /* 16px, иначе Safari на iOS зумит страницу при фокусе */
  font-variant-numeric: tabular-nums;
}

input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.field { margin-bottom: 14px; }
.field-row { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }

.check { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.check input { width: auto; }
.check label { margin: 0; color: var(--text); font-size: 14px; }

button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 550;
  cursor: pointer;
}

button:hover { filter: brightness(1.06); }
button.ghost { background: transparent; color: var(--muted); border-color: var(--border); }
button.ghost:hover { background: var(--surface-2); color: var(--text); }
button.small { padding: 7px 11px; font-size: 13px; }

.inline-form { display: flex; gap: 8px; align-items: flex-end; }
.inline-form input { min-width: 0; }

details.adder { margin-top: 16px; }
details.adder > summary { cursor: pointer; color: var(--accent); font-size: 14px; font-weight: 550; }
details.adder > div { margin-top: 16px; }

/* --- Алерты -------------------------------------------------------------- */
.alert {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 13px 15px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface);
  margin-bottom: 9px;
}

.alert.critical { background: var(--danger-bg); border-color: color-mix(in srgb, var(--danger) 28%, transparent); }
.alert.warning { background: var(--warn-bg); border-color: color-mix(in srgb, var(--warn) 28%, transparent); }
.alert-body { flex: 1; min-width: 0; }
.alert-title { font-weight: 570; font-size: 14.5px; }
.alert-detail { color: var(--muted); font-size: 13px; margin-top: 3px; }

/* --- Подсказки по пополнению -------------------------------------------- */
.suggestion {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.suggestion:last-child { border-bottom: none; }
.suggestion-amount { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* --- Календарь ----------------------------------------------------------- */
.month-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .05em;
  margin: 22px 0 10px;
}

.event-date {
  min-width: 52px;
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  color: var(--muted);
  padding-top: 1px;
}

.empty { text-align: center; padding: 42px 20px; color: var(--muted); }

.logout-form { display: flex; align-items: center; margin: 0; }
.logout-form button { color: var(--muted); }

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}
.login-card { width: min(420px, 100%); }
.login-error {
  margin: 0 0 14px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 14px;
}

.footer {
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 20px 34px;
  color: var(--muted);
  font-size: 12.5px;
}

.chart-wrap { position: relative; height: 260px; }

@media (max-width: 560px) {
  main { padding: 16px 14px 40px; }
  .card { padding: 15px; border-radius: 12px; }
  .total-value { font-size: 31px; }
  .topbar { padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top)); }
  .topbar nav { width: 100%; margin-left: 0; }
  .inline-form { flex-direction: column; align-items: stretch; }
}
