/* ============================================================
   ALFA Finance — дизайн-система
   Тема: светлая «бумага» + хвойно-зелёный акцент (фин-леджер)
   Шрифты: Bricolage Grotesque (дисплей) + Hanken Grotesk (UI)
   Цвет: OKLCH, нейтрали подтонированы к акценту
   ============================================================ */

:root {
  /* поверхности — тёплая бумага */
  --bg:            oklch(0.984 0.006 95);
  --surface:       oklch(0.997 0.003 95);
  --surface-2:     oklch(0.965 0.007 95);
  --surface-3:     oklch(0.945 0.009 92);

  /* границы */
  --border:        oklch(0.905 0.008 92);
  --border-strong: oklch(0.84 0.011 90);

  /* чернила (текст) — прохладный нейтрал */
  --ink:           oklch(0.275 0.014 220);
  --ink-2:         oklch(0.475 0.012 220);
  --ink-3:         oklch(0.605 0.010 220);

  /* акцент — хвойный зелёный */
  --accent:        oklch(0.475 0.082 162);
  --accent-hover:  oklch(0.415 0.088 162);
  --accent-soft:   oklch(0.945 0.030 162);
  --accent-line:   oklch(0.885 0.040 162);
  --on-accent:     oklch(0.99 0.012 162);

  /* семантика денег */
  --expense:       oklch(0.515 0.150 33);
  --expense-soft:  oklch(0.955 0.035 40);
  --income:        oklch(0.500 0.095 162);
  --income-soft:   oklch(0.950 0.035 162);
  --transfer:      oklch(0.520 0.045 235);
  --transfer-soft: oklch(0.950 0.022 235);
  --danger:        oklch(0.545 0.170 28);

  /* палитра для графиков */
  --c1: oklch(0.50 0.085 162);
  --c2: oklch(0.62 0.110 55);
  --c3: oklch(0.55 0.095 235);
  --c4: oklch(0.60 0.120 25);
  --c5: oklch(0.58 0.075 300);
  --c6: oklch(0.65 0.090 130);
  --c7: oklch(0.60 0.090 200);
  --c8: oklch(0.70 0.040 90);

  /* типографика */
  --font-display: "Bricolage Grotesque", "Hanken Grotesk", system-ui, sans-serif;
  --font-sans: "Hanken Grotesk", system-ui, -apple-system, sans-serif;

  --text-xs:   0.75rem;    /* 12 */
  --text-sm:   0.8125rem;  /* 13 */
  --text-base: 0.9375rem;  /* 15 */
  --text-md:   1.0625rem;  /* 17 */
  --text-lg:   1.375rem;   /* 22 */
  --text-xl:   1.75rem;    /* 28 */
  --text-2xl:  2.5rem;     /* 40 */
  --text-3xl:  clamp(2.5rem, 1.8rem + 3vw, 3.5rem);

  /* отступы (4pt) */
  --s-1: 4px;  --s-2: 8px;  --s-3: 12px; --s-4: 16px;
  --s-5: 20px; --s-6: 24px; --s-8: 32px; --s-10: 40px;
  --s-12: 48px; --s-16: 64px;

  /* радиусы */
  --r-sm: 8px; --r-md: 12px; --r-lg: 18px; --r-full: 999px;

  /* тени (подтонированы чернилами) */
  --sh-xs: 0 1px 2px oklch(0.4 0.03 220 / 0.05);
  --sh-sm: 0 1px 2px oklch(0.4 0.03 220 / 0.06), 0 1px 3px oklch(0.4 0.03 220 / 0.04);
  --sh-md: 0 2px 4px oklch(0.4 0.03 220 / 0.05), 0 8px 24px oklch(0.4 0.03 220 / 0.06);
  --sh-lg: 0 12px 40px oklch(0.4 0.03 220 / 0.10);

  --sidebar-w: 248px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: 1.55;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
h1, h2, h3, h4 { font-family: var(--font-display); font-weight: 600; letter-spacing: -0.015em; margin: 0; color: var(--ink); }
a { color: var(--accent); text-decoration: none; }
button { font-family: inherit; }
.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; }
.muted { color: var(--ink-3); }
.center { text-align: center; }

/* ---------- App shell ---------- */
.app { display: flex; min-height: 100vh; }

.sidebar {
  position: sticky; top: 0; align-self: flex-start;
  width: var(--sidebar-w); height: 100vh; flex-shrink: 0;
  display: flex; flex-direction: column; gap: var(--s-2);
  padding: var(--s-5) var(--s-4);
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.logo {
  font-family: var(--font-display); font-weight: 700; font-size: var(--text-md);
  letter-spacing: 0.02em; color: var(--ink); display: inline-flex; gap: 0.35em;
  padding: var(--s-2) var(--s-3) var(--s-4);
}
.logo b { color: var(--accent); }
.logo span { color: var(--ink-3); font-weight: 500; }

.add-btn {
  display: flex; align-items: center; justify-content: center; gap: var(--s-2);
  background: var(--accent); color: var(--on-accent);
  font-weight: 600; font-size: var(--text-sm);
  padding: var(--s-3) var(--s-4); border-radius: var(--r-md);
  box-shadow: var(--sh-xs); transition: background 0.18s var(--ease);
}
.add-btn:hover { background: var(--accent-hover); }
.add-btn svg { width: 18px; height: 18px; }

.side-nav { display: flex; flex-direction: column; gap: 2px; margin-top: var(--s-3); }
.side-nav a {
  display: flex; align-items: center; gap: var(--s-3);
  padding: var(--s-3) var(--s-3); border-radius: var(--r-md);
  color: var(--ink-2); font-weight: 500; font-size: var(--text-base);
  transition: background 0.15s, color 0.15s;
}
.side-nav a svg { width: 19px; height: 19px; opacity: 0.8; }
.side-nav a:hover { background: var(--surface-2); color: var(--ink); }
.side-nav a.on { background: var(--accent-soft); color: var(--accent); }
.side-nav a.on svg { opacity: 1; }

.sidebar-foot { margin-top: auto; padding-top: var(--s-4); border-top: 1px solid var(--border); }
.who { display: flex; align-items: center; gap: var(--s-3); padding: var(--s-2); }
.avatar {
  width: 34px; height: 34px; flex-shrink: 0; border-radius: var(--r-full);
  background: var(--accent-soft); color: var(--accent);
  display: grid; place-items: center; font-weight: 700; font-size: var(--text-sm);
  text-transform: uppercase;
}
.who-meta { min-width: 0; line-height: 1.25; flex: 1; }
.who-name { font-weight: 600; font-size: var(--text-sm); color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who-role { font-size: var(--text-xs); color: var(--ink-3); }
.logout { display: grid; place-items: center; width: 32px; height: 32px; border-radius: var(--r-sm); color: var(--ink-3); }
.logout:hover { background: var(--surface-2); color: var(--danger); }
.logout svg { width: 17px; height: 17px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.content { flex: 1; width: 100%; max-width: 1140px; margin: 0 auto; padding: var(--s-8) var(--s-8) var(--s-16); }

/* mobile chrome (hidden on desktop) */
.mobile-top { display: none; }
.tabbar { display: none; }

/* ---------- Page header ---------- */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--s-4); margin-bottom: var(--s-8); flex-wrap: wrap; }
.page-head h1 { font-size: var(--text-xl); }
.eyebrow { font-size: var(--text-xs); font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-3); margin: 0 0 var(--s-2); }
.page-actions { display: flex; gap: var(--s-2); align-items: center; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  font-size: var(--text-sm); font-weight: 600; line-height: 1;
  padding: var(--s-3) var(--s-4); border-radius: var(--r-md);
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--ink);
  cursor: pointer; transition: background 0.15s, border-color 0.15s, transform 0.05s;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(0.5px); }
.btn svg { width: 16px; height: 16px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); box-shadow: var(--sh-xs); }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--ink-2); }
.btn.ghost:hover { background: var(--surface-2); color: var(--ink); }
.btn.full { width: 100%; }
.btn.lg { padding: var(--s-4) var(--s-5); font-size: var(--text-base); border-radius: var(--r-md); }

.link-btn { background: none; border: 0; color: var(--accent); cursor: pointer; font-size: var(--text-sm); font-weight: 500; padding: 0; font-family: inherit; }
.link-btn:hover { color: var(--accent-hover); text-decoration: underline; }
.link-btn.danger { color: var(--ink-3); }
.link-btn.danger:hover { color: var(--danger); }

/* ---------- Cards / panels ---------- */
.panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-lg); padding: var(--s-6);
}
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--s-5); gap: var(--s-3); }
.panel-head h2 { font-size: var(--text-base); font-weight: 600; color: var(--ink-2); }
.panel-head .link-btn { font-size: var(--text-sm); }

.section-title { font-size: var(--text-xs); font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-3); margin-bottom: var(--s-4); }

/* ---------- Summary band (dashboard hero) ---------- */
.summary { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s-6); flex-wrap: wrap; margin-bottom: var(--s-8); }
.hero-figure { font-family: var(--font-display); font-weight: 700; font-size: var(--text-3xl); line-height: 0.95; letter-spacing: -0.03em; color: var(--ink); font-variant-numeric: tabular-nums; }
.hero-meta { color: var(--ink-3); font-size: var(--text-sm); margin-top: var(--s-3); }
.hero-meta b { color: var(--ink-2); font-weight: 600; }

/* ---------- Balances strip ---------- */
.balances { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: var(--s-3); margin-bottom: var(--s-8); }
.wallet {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md);
  padding: var(--s-4); display: flex; flex-direction: column; gap: var(--s-2);
}
.wallet-top { display: flex; align-items: center; justify-content: space-between; gap: var(--s-2); }
.wallet-name { font-size: var(--text-sm); font-weight: 600; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wallet-cur { font-size: var(--text-xs); font-weight: 600; color: var(--accent); background: var(--accent-soft); padding: 2px var(--s-2); border-radius: var(--r-full); }
.wallet-amount { font-family: var(--font-display); font-weight: 600; font-size: var(--text-lg); color: var(--ink); font-variant-numeric: tabular-nums; }
.wallet-amount.neg { color: var(--expense); }

/* ---------- Grid ---------- */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s-5); }
.grid-2 > * { min-width: 0; }

/* ---------- Category legend ---------- */
.legend { list-style: none; margin: var(--s-4) 0 0; padding: 0; display: flex; flex-direction: column; gap: var(--s-1); }
.legend li { display: grid; grid-template-columns: auto 1fr auto auto; align-items: center; gap: var(--s-3); padding: var(--s-2) 0; font-size: var(--text-sm); }
.legend li + li { border-top: 1px solid var(--border); }
.dot { width: 10px; height: 10px; border-radius: 3px; }
.legend .lg-name { color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend .lg-val { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; }
.legend .lg-pct { color: var(--ink-3); font-variant-numeric: tabular-nums; min-width: 42px; text-align: right; }

/* ---------- Tables ---------- */
.table { width: 100%; border-collapse: collapse; font-size: var(--text-sm); }
.table thead th { font-size: var(--text-xs); font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--ink-3); text-align: left; padding: 0 var(--s-3) var(--s-3); }
.table td { padding: var(--s-3); border-top: 1px solid var(--border); color: var(--ink); vertical-align: middle; }
.table tbody tr { transition: background 0.12s; }
.table tbody tr:hover { background: var(--surface-2); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .neg { color: var(--expense); font-weight: 600; }
.table .pos { color: var(--income); font-weight: 600; }
.table tr.off { opacity: 0.45; }
.table-flush td:first-child, .table-flush th:first-child { padding-left: 0; }
.table-flush td:last-child, .table-flush th:last-child { padding-right: 0; }

.amount-orig { color: var(--ink-3); font-size: var(--text-xs); }

/* ---------- Tags / badges ---------- */
.tag { display: inline-flex; align-items: center; gap: var(--s-1); font-size: var(--text-xs); font-weight: 600; padding: 3px var(--s-2); border-radius: var(--r-full); white-space: nowrap; }
.tag::before { content: ""; width: 6px; height: 6px; border-radius: var(--r-full); background: currentColor; opacity: 0.9; }
.tag.expense { color: var(--expense); background: var(--expense-soft); }
.tag.income { color: var(--income); background: var(--income-soft); }
.tag.transfer { color: var(--transfer); background: var(--transfer-soft); }

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: var(--s-2); margin-bottom: var(--s-4); }
.field > span, .field > label { font-size: var(--text-sm); font-weight: 500; color: var(--ink-2); }
.hint { font-size: var(--text-xs); color: var(--ink-3); font-weight: 400; }
input, select, textarea {
  width: 100%; font-family: inherit; font-size: var(--text-base); color: var(--ink);
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r-md);
  padding: var(--s-3) var(--s-3); transition: border-color 0.15s, box-shadow 0.15s;
  -webkit-appearance: none; appearance: none;
}
input::placeholder { color: var(--ink-3); }
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
select {
  background-image: url("data:image/svg+xml;charset=UTF-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right var(--s-3) center; padding-right: var(--s-8);
}
.input-amount { font-family: var(--font-display); font-weight: 600; font-size: var(--text-xl); padding: var(--s-3) var(--s-4); }
.form-row { display: flex; gap: var(--s-3); }
.form-row > * { flex: 1; margin-bottom: 0; }
.form-row .col-amount { flex: 2; }
.form-narrow { max-width: 520px; }

/* segmented control */
.seg { display: flex; gap: var(--s-1); background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--r-md); padding: var(--s-1); margin-bottom: var(--s-5); }
.seg label { flex: 1; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; text-align: center; padding: var(--s-3) var(--s-2); border-radius: var(--r-sm); font-size: var(--text-sm); font-weight: 600; color: var(--ink-3); cursor: pointer; transition: background 0.18s var(--ease), color 0.18s; }
.seg label:hover span { color: var(--ink); }
.seg input:checked + span { background: var(--surface); color: var(--accent); box-shadow: var(--sh-xs); }
.seg input:focus-visible + span { box-shadow: 0 0 0 3px var(--accent-soft); }

/* rate disclosure */
.disclose { border: 1px solid var(--border); border-radius: var(--r-md); overflow: hidden; margin-bottom: var(--s-5); }
.disclose summary { list-style: none; cursor: pointer; padding: var(--s-3) var(--s-4); font-size: var(--text-sm); color: var(--ink-2); display: flex; justify-content: space-between; align-items: center; }
.disclose summary::-webkit-details-marker { display: none; }
.disclose summary b { color: var(--ink); font-variant-numeric: tabular-nums; }
.disclose[open] summary { border-bottom: 1px solid var(--border); }
.disclose .field { margin: var(--s-4); }

/* filters */
.filters { display: flex; flex-wrap: wrap; gap: var(--s-3); align-items: end; padding: var(--s-4); margin-bottom: var(--s-5); }
.filters .field { margin: 0; min-width: 130px; flex: 1 1 130px; }
.filters .field.search { flex: 2 1 200px; }
.filters .btn { height: 42px; }

/* ---------- Alerts ---------- */
.alert { padding: var(--s-3) var(--s-4); border-radius: var(--r-md); font-size: var(--text-sm); margin-bottom: var(--s-5); border: 1px solid; display: flex; gap: var(--s-2); align-items: center; }
.alert.err { color: var(--danger); background: var(--expense-soft); border-color: oklch(0.85 0.06 35); }
.alert.ok { color: var(--income); background: var(--income-soft); border-color: var(--accent-line); }

/* ---------- Empty states ---------- */
.empty { display: flex; flex-direction: column; align-items: center; text-align: center; gap: var(--s-3); padding: var(--s-12) var(--s-4); color: var(--ink-3); }
.empty svg { width: 34px; height: 34px; opacity: 0.5; }
.empty p { margin: 0; font-size: var(--text-sm); max-width: 32ch; }
.empty .empty-title { font-family: var(--font-display); font-weight: 600; font-size: var(--text-md); color: var(--ink-2); }

/* ---------- Stat row (transactions total) ---------- */
.stat-inline { display: flex; align-items: baseline; gap: var(--s-3); padding: var(--s-4) var(--s-5); margin-bottom: var(--s-5); background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md); }
.stat-inline .lbl { font-size: var(--text-sm); color: var(--ink-3); }
.stat-inline .val { font-family: var(--font-display); font-weight: 600; font-size: var(--text-lg); color: var(--ink); font-variant-numeric: tabular-nums; }

/* row actions */
.row-actions { display: inline-flex; gap: var(--s-1); justify-content: flex-end; }
.row-actions a, .row-actions button { display: grid; place-items: center; width: 30px; height: 30px; border-radius: var(--r-sm); border: 0; background: transparent; color: var(--ink-3); cursor: pointer; }
.row-actions a:hover { background: var(--accent-soft); color: var(--accent); }
.row-actions button:hover { background: var(--expense-soft); color: var(--danger); }
.row-actions svg { width: 16px; height: 16px; }
.row-actions form { display: inline-flex; }

/* ---------- Login ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: var(--s-5); background:
  radial-gradient(120% 90% at 50% -10%, var(--accent-soft), transparent 60%), var(--bg); }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg); padding: var(--s-10) var(--s-8); box-shadow: var(--sh-md); }
.auth-brand { font-family: var(--font-display); font-weight: 700; font-size: var(--text-xl); letter-spacing: 0.02em; text-align: center; }
.auth-brand b { color: var(--accent); }
.auth-sub { text-align: center; color: var(--ink-3); font-size: var(--text-sm); margin: var(--s-2) 0 var(--s-8); }

@media (prefers-reduced-motion: no-preference) {
  .content > * { animation: rise 0.4s var(--ease) both; }
  @keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
}

/* ============================================================
   Mobile
   ============================================================ */
@media (max-width: 860px) {
  .sidebar { display: none; }
  .content { padding: var(--s-5) var(--s-4) calc(72px + var(--s-6)); max-width: 100%; }
  .grid-2 { grid-template-columns: 1fr; }

  .mobile-top {
    display: flex; align-items: center; justify-content: space-between;
    position: sticky; top: 0; z-index: 20;
    padding: var(--s-3) var(--s-4);
    background: oklch(0.997 0.003 95 / 0.85); backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
  }
  .mobile-top .logo { padding: 0; font-size: var(--text-md); }
  .mobile-top .avatar { width: 30px; height: 30px; }

  .tabbar {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
    background: oklch(0.997 0.003 95 / 0.92); backdrop-filter: blur(12px);
    border-top: 1px solid var(--border);
    padding: var(--s-2) var(--s-2) calc(var(--s-2) + env(safe-area-inset-bottom));
  }
  .tabbar a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px;
    color: var(--ink-3); font-size: 10px; font-weight: 600; padding: var(--s-1);
  }
  .tabbar a svg { width: 22px; height: 22px; }
  .tabbar a.on { color: var(--accent); }
  .tabbar a.tab-add { color: var(--accent); }
  .tabbar a.tab-add .tab-plus { width: 40px; height: 40px; border-radius: var(--r-md); background: var(--accent); color: var(--on-accent); display: grid; place-items: center; margin-top: -18px; box-shadow: var(--sh-md); }
  .tabbar a.tab-add .tab-plus svg { width: 24px; height: 24px; }

  .summary { gap: var(--s-4); }
  .hero-figure { font-size: var(--text-2xl); }
  .page-head { margin-bottom: var(--s-5); }
  .balances { grid-template-columns: 1fr 1fr; }

  /* transactions: table -> cards */
  .tx-table thead { display: none; }
  .tx-table, .tx-table tbody, .tx-table tr, .tx-table td { display: block; }
  .tx-table tr { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md); padding: var(--s-3) var(--s-4); margin-bottom: var(--s-3); }
  .tx-table tr:hover { background: var(--surface); }
  .tx-table td { border: 0; padding: var(--s-1) 0; display: flex; justify-content: space-between; align-items: center; gap: var(--s-4); text-align: right; }
  .tx-table td::before { content: attr(data-l); color: var(--ink-3); font-size: var(--text-xs); font-weight: 500; text-align: left; }
  .tx-table td.cell-main { font-weight: 600; }
  .tx-table td.cell-main::before { content: ""; }
  .tx-table td .num { text-align: right; }
  .tx-table td.cell-actions { justify-content: flex-end; padding-top: var(--s-2); }
  .tx-table td.cell-actions::before { content: ""; }
}

@media (max-width: 420px) {
  .balances { grid-template-columns: 1fr; }
  .form-row { flex-direction: column; gap: var(--s-3); }
  .form-row > * { margin-bottom: 0; }
}
