:root {
  --bg: #0f1117;
  --bg2: #1a1d27;
  --bg3: #22263a;
  --border: #2d3148;
  --text: #e2e8f0;
  --text2: #8892a4;
  --accent: #6366f1;
  --accent2: #818cf8;
  --green: #22c55e;
  --yellow: #f59e0b;
  --red: #ef4444;
  --radius: 12px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
}

/* ---- Light theme ---- */
body.theme-light {
  --bg: #f4f6fb;
  --bg2: #ffffff;
  --bg3: #eef1f8;
  --border: #d1d8e8;
  --text: #1a1d2e;
  --text2: #5a6380;
  --accent: #5558e8;
  --accent2: #6366f1;
  --shadow: 0 4px 24px rgba(0,0,0,0.08);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 15px; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

/* ---- Layout ---- */
#app { display: flex; flex-direction: column; min-height: 100vh; }

nav {
  position: sticky; top: 0; z-index: 100;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px;
  padding: 0 20px; height: 56px;
}
nav .logo { display: flex; align-items: center; gap: 8px; margin-right: auto; flex-shrink: 0; }
nav .logo img { height: 34px; width: auto; display: block; }
nav .logo span { color: var(--text2); font-weight: 400; font-size: 0.85rem; }
nav a {
  padding: 6px 14px; border-radius: 8px; text-decoration: none;
  color: var(--text2); font-size: 0.875rem; font-weight: 500;
  transition: background 0.15s, color 0.15s;
}
nav a:hover, nav a.active { background: var(--bg3); color: var(--text); }
nav .user-info { font-size: 0.8rem; color: var(--text2); }
nav .btn-logout, nav .btn-demo {
  border: 1px solid var(--border); background: transparent; color: var(--text2);
  border-radius: 8px; padding: 5px 12px; cursor: pointer; font-size: 0.8rem;
  transition: all 0.15s;
}
nav .btn-demo.active { background: var(--yellow); color: #000; border-color: var(--yellow); }
nav .btn-logout:hover { border-color: var(--red); color: var(--red); }

main { flex: 1; padding: 24px 20px; max-width: 1400px; margin: 0 auto; width: 100%; }

/* ---- Page header ---- */
.page-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 20px; flex-wrap: wrap; gap: 12px;
}
.page-header h1 { font-size: 1.4rem; font-weight: 700; }
.period-nav { display: flex; align-items: center; gap: 8px; }
.period-nav button {
  background: var(--bg3); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; width: 32px; height: 32px; cursor: pointer; font-size: 1rem;
}
.period-nav .period-label { font-weight: 600; font-size: 1rem; min-width: 120px; text-align: center; }

/* ---- Cards ---- */
.card {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px; margin-bottom: 20px;
  box-shadow: var(--shadow);
}
.card h2 {
  font-size: 0.95rem; font-weight: 600; color: var(--text2);
  text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 16px;
  display: flex; align-items: center; gap: 8px;
}
.card h2 .icon { font-size: 1.1rem; }

/* ---- Grid ---- */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; }
@media (max-width: 900px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }

/* ---- Tables ---- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
th {
  text-align: left; padding: 8px 12px; color: var(--text2);
  font-weight: 600; font-size: 0.78rem; text-transform: uppercase;
  border-bottom: 1px solid var(--border); white-space: nowrap;
}
td { padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255,255,255,0.02); }
.row-highlight td { background: rgba(99,102,241,0.08); font-weight: 600; }
.row-total td { background: rgba(99,102,241,0.15); font-weight: 700; }

/* ---- Traffic light ---- */
.tl { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; }
.tl-green  { background: var(--green); box-shadow: 0 0 6px var(--green); }
.tl-yellow { background: var(--yellow); box-shadow: 0 0 6px var(--yellow); }
.tl-red    { background: var(--red); box-shadow: 0 0 6px var(--red); }
.tl-null   { background: var(--text2); }

.badge { display: inline-flex; align-items: center; gap: 4px; }
.badge-green  { color: var(--green); }
.badge-yellow { color: var(--yellow); }
.badge-red    { color: var(--red); }
.badge-null   { color: var(--text2); }

/* ---- Numbers ---- */
.num-positive { color: var(--green); }
.num-negative { color: var(--red); }
.num-null     { color: var(--text2); font-style: italic; }

/* ---- Pct bar ---- */
.pct-bar { display: flex; align-items: center; gap: 8px; }
.pct-bar-bg { flex: 1; height: 6px; background: var(--bg3); border-radius: 3px; overflow: hidden; min-width: 60px; }
.pct-bar-fill { height: 100%; border-radius: 3px; transition: width 0.4s; }
.pct-bar-fill.green  { background: var(--green); }
.pct-bar-fill.yellow { background: var(--yellow); }
.pct-bar-fill.red    { background: var(--red); }

/* ---- Alerts ---- */
.alerts { display: flex; flex-direction: column; gap: 8px; margin-bottom: 20px; }
.alert {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 12px 16px; border-radius: 10px; font-size: 0.875rem;
}
.alert-red    { background: rgba(239,68,68,0.12);  border: 1px solid rgba(239,68,68,0.3); }
.alert-yellow { background: rgba(245,158,11,0.12); border: 1px solid rgba(245,158,11,0.3); }
.alert-green  { background: rgba(34,197,94,0.1);   border: 1px solid rgba(34,197,94,0.25); }
.alert-icon   { font-size: 1rem; flex-shrink: 0; margin-top: 1px; }

/* ---- Sync status ---- */
.sync-strip {
  display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 20px;
}
.sync-chip {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 12px; border-radius: 20px; font-size: 0.78rem;
  background: var(--bg3); border: 1px solid var(--border);
}
.sync-chip .dot { width: 7px; height: 7px; border-radius: 50%; }
.sync-chip .dot.ok    { background: var(--green); }
.sync-chip .dot.fail  { background: var(--red); }
.sync-chip .dot.warn  { background: var(--yellow); }
.sync-chip .time      { color: var(--text2); }

/* ---- Charts ---- */
.chart-wrap { position: relative; height: 260px; }
.chart-wrap-tall { position: relative; height: 340px; }
.chart-wrap-sm   { position: relative; height: 100px; }

/* ---- Waterfall labels ---- */
.wf-labels { display: flex; gap: 0; flex-wrap: nowrap; overflow-x: auto; }

/* ---- Sparkline ---- */
.sparkline-row { display: flex; align-items: center; gap: 8px; }
.sparkline-canvas { width: 80px !important; height: 30px !important; }

/* ---- KPI grid ---- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
}
.kpi-card {
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px;
}
.kpi-card .label { font-size: 0.75rem; color: var(--text2); margin-bottom: 4px; }
.kpi-card .value { font-size: 1.25rem; font-weight: 700; }
.kpi-card .sub   { font-size: 0.75rem; color: var(--text2); margin-top: 2px; }
.kpi-card .trend { font-size: 0.78rem; margin-top: 4px; }

/* ---- Funnel (new) ---- */
.funnel-step { padding: 10px 0; border-bottom: 1px solid var(--border); }
.funnel-step:last-child { border-bottom: none; }
.funnel-step-header { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.funnel-step-icon { font-size: 1rem; flex-shrink: 0; }
.funnel-step-label { font-size: 0.85rem; font-weight: 600; width: 70px; flex-shrink: 0; }
.funnel-step-vals { display: flex; align-items: center; gap: 8px; flex: 1; flex-wrap: wrap; font-size: 0.85rem; }
.funnel-step-plan { color: var(--text2); font-size: 0.78rem; }
.funnel-step-pct { font-size: 0.75rem; font-weight: 700; padding: 1px 6px; border-radius: 4px; background: var(--bg3); }
.funnel-nodata { font-size: 0.75rem; color: var(--text2); font-style: italic; }
.funnel-bars { display: flex; flex-direction: column; gap: 3px; margin-bottom: 4px; }
.funnel-bar-bg { height: 8px; background: var(--bg3); border-radius: 4px; overflow: hidden; }
.funnel-bar-plan { height: 100%; background: var(--border); border-radius: 4px; opacity: 0.8; }
.funnel-bar-fact { height: 100%; border-radius: 4px; transition: width 0.5s; }
.funnel-cr-row { display: flex; align-items: center; gap: 6px; font-size: 0.75rem; color: var(--text2); padding-top: 2px; }
.funnel-cr-arrow { color: var(--accent2); font-size: 0.9rem; }
.funnel-cr-label { flex-shrink: 0; }
.funnel-cr-fact { font-weight: 700; color: var(--text); }
.funnel-cr-plan { color: var(--text2); }

/* ---- P&L waterfall (HTML) ---- */
.pnl-waterfall { display: flex; flex-direction: column; gap: 2px; }
.pnl-row { display: grid; grid-template-columns: 180px 1fr 90px 42px; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; }
.pnl-row:hover { background: var(--bg3); }
.pnl-subtotal { background: rgba(99,102,241,0.07); font-weight: 700; border-radius: 6px; }
.pnl-label { font-size: 0.82rem; }
.pnl-bar-wrap { height: 10px; background: var(--bg3); border-radius: 5px; overflow: hidden; }
.pnl-bar { height: 100%; border-radius: 5px; transition: width 0.5s; min-width: 2px; }
.pnl-value { font-size: 0.82rem; font-weight: 600; text-align: right; white-space: nowrap; }
.pnl-pct { font-size: 0.72rem; color: var(--text2); text-align: right; }

/* ---- Theme toggle ---- */
.btn-theme { border: 1px solid var(--border); background: transparent; color: var(--text2);
  border-radius: 8px; padding: 5px 12px; cursor: pointer; font-size: 0.8rem; transition: all 0.15s; }

/* ---- Expenses ---- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 16px; border-radius: 8px; border: none;
  cursor: pointer; font-size: 0.875rem; font-weight: 500;
  transition: all 0.15s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent2); }
.btn-danger  { background: rgba(239,68,68,0.15); color: var(--red); }
.btn-danger:hover  { background: rgba(239,68,68,0.25); }
.btn-ghost   { background: transparent; border: 1px solid var(--border); color: var(--text2); }
.btn-ghost:hover { border-color: var(--accent); color: var(--accent2); }
.btn-sm { padding: 4px 10px; font-size: 0.78rem; }

/* ---- Modal ---- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center; z-index: 200;
}
.modal {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 28px; width: 500px; max-width: 95vw; box-shadow: var(--shadow);
}
.modal h3 { font-size: 1.1rem; margin-bottom: 20px; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 0.8rem; color: var(--text2); margin-bottom: 6px; font-weight: 500; }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; background: var(--bg3); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; padding: 9px 12px;
  font-size: 0.875rem; outline: none; transition: border-color 0.15s;
}
.form-group input:focus, .form-group select:focus { border-color: var(--accent); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }

/* ---- Login ---- */
#page-login {
  display: flex; align-items: center; justify-content: center;
  min-height: calc(100vh - 56px);
}
.login-box {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 40px; width: 400px; max-width: 95vw;
}
.login-logo { margin-bottom: 20px; }
.login-logo img { height: 44px; width: auto; display: none; }
/* Тёмная тема — показываем чёрный логотип (светлый для тёмного фона) */
body:not(.theme-light) .login-logo-dark  { display: block; }
body:not(.theme-light) .login-logo-light { display: none; }
/* Светлая тема — показываем белый логотип (тёмный для светлого фона) */
body.theme-light .login-logo-dark  { display: none; }
body.theme-light .login-logo-light { display: block; }
.login-box .sub { color: var(--text2); font-size: 0.875rem; margin-bottom: 28px; }
.login-error { color: var(--red); font-size: 0.85rem; margin-top: 8px; min-height: 20px; }

/* ---- Settings ---- */
.settings-table td:nth-child(3), .settings-table td:nth-child(4) { width: 140px; }
.settings-table input[type=number] {
  background: var(--bg3); border: 1px solid var(--border); color: var(--text);
  border-radius: 6px; padding: 5px 8px; width: 110px; font-size: 0.85rem;
}

/* ---- History ---- */
.history-meta { color: var(--text2); font-size: 0.85rem; margin-bottom: 16px; }
.status-badge {
  font-size: 0.75rem; padding: 2px 8px; border-radius: 10px; font-weight: 600;
}
.status-green  { background: rgba(34,197,94,0.15);  color: var(--green); }
.status-yellow { background: rgba(245,158,11,0.15); color: var(--yellow); }
.status-red    { background: rgba(239,68,68,0.15);  color: var(--red); }
.status-null   { background: rgba(136,146,164,0.15);color: var(--text2); }

/* ---- Plans table inputs ---- */
.plan-input {
  background: var(--bg3); border: 1px solid var(--border); color: var(--text);
  border-radius: 6px; padding: 4px 7px; width: 100%; min-width: 80px; font-size: 0.82rem;
  outline: none; transition: border-color 0.15s;
}
.plan-input:focus { border-color: var(--accent); }
.plan-input:disabled { opacity: 0.5; cursor: not-allowed; }
.plan-input[type=text] { min-width: 100px; }
.plan-calc {
  display: block; font-size: 0.82rem; color: var(--accent2);
  font-weight: 600; white-space: nowrap; padding: 4px 2px;
}

/* ---- KPI Top Row ---- */
.kpi-top-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
  margin-bottom: 20px;
}
@media (max-width: 1100px) { .kpi-top-row { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 700px)  { .kpi-top-row { grid-template-columns: repeat(2, 1fr); } }

.kpi-top-card {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
  display: flex; gap: 12px; align-items: flex-start;
  border-left: 3px solid var(--border);
  transition: border-color 0.2s;
}
.kpi-top-card.kpi-green  { border-left-color: var(--green); }
.kpi-top-card.kpi-yellow { border-left-color: var(--yellow); }
.kpi-top-card.kpi-red    { border-left-color: var(--red); }
.kpi-top-card.kpi-null   { border-left-color: var(--text2); }

.kpi-top-icon { font-size: 1.5rem; flex-shrink: 0; margin-top: 2px; }
.kpi-top-body { flex: 1; min-width: 0; }
.kpi-top-label { font-size: 0.72rem; color: var(--text2); text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 4px; }
.kpi-top-value { font-size: 1.3rem; font-weight: 700; line-height: 1.2; }
.kpi-top-sub   { font-size: 0.72rem; color: var(--text2); margin-top: 4px; line-height: 1.4; }
.kpi-top-hint  { font-size: 0.72rem; margin-top: 6px; font-weight: 600; }
.kpi-top-card.kpi-green  .kpi-top-hint { color: var(--green); }
.kpi-top-card.kpi-yellow .kpi-top-hint { color: var(--yellow); }
.kpi-top-card.kpi-red    .kpi-top-hint { color: var(--red); }

/* ---- grid-2 spacing ---- */
.grid-2 .card { margin-bottom: 0; }
.grid-2 { margin-bottom: 20px; }

/* ---- Export ---- */
.export-row { display: flex; gap: 10px; flex-wrap: wrap; }

/* ---- Scrollbar ---- */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ---- Loading ---- */
.loading {
  display: flex; align-items: center; justify-content: center;
  height: 200px; color: var(--text2); gap: 12px; font-size: 0.95rem;
}
.spinner {
  width: 20px; height: 20px; border: 2px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- No data ---- */
.no-data {
  text-align: center; padding: 40px; color: var(--text2); font-size: 0.9rem;
}
.no-data .icon { font-size: 2rem; display: block; margin-bottom: 8px; }
