/* MediaDFlow Finance — тёмная тема */
:root {
  color-scheme: dark;
  --bg: #0e0f11;
  --surface-1: #17181b;
  --surface-2: #1f2024;
  --surface-3: #2a2b30;
  --border: #2a2b30;
  --text-primary: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #8d8c85;
  --accent: #3987e5;
  --series-1: #3987e5;  /* blue   */
  --series-2: #d95926;  /* orange */
  --series-3: #199e70;  /* aqua   */
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --radius: 14px;
  --gap: 14px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", Roboto, sans-serif;
  --mono: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text-primary); font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased; -webkit-text-size-adjust: 100%; }
[hidden] { display: none !important; }
a { color: var(--accent); }
h1 { font-size: 24px; font-weight: 650; margin: 0 0 4px; letter-spacing: -.01em; }
h2 { font-size: 17px; font-weight: 620; margin: 0 0 12px; }
h3 { font-size: 14px; font-weight: 600; margin: 0 0 8px; color: var(--text-secondary); text-transform: uppercase; letter-spacing: .04em; }
p { margin: 0 0 8px; }
.muted { color: var(--text-muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.err { color: #ff8a8a; min-height: 1.2em; }

/* ---------- каркас */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.side { position: sticky; top: 0; height: 100vh; padding: 20px 14px; border-right: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 18px; background: #111215; }
.side nav { display: flex; flex-direction: column; gap: 2px; }
.side nav a, .sheet a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 10px;
  color: var(--text-secondary); text-decoration: none; font-weight: 500; }
.side nav a:hover { background: var(--surface-2); color: var(--text-primary); }
.side nav a.on { background: var(--surface-2); color: var(--text-primary); box-shadow: inset 3px 0 0 var(--accent); }
.side nav a .ic, .tabbar .ic { width: 20px; text-align: center; opacity: .9; }
.logout { margin-top: auto; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; letter-spacing: -.01em; }
.brand.big { font-size: 22px; }
.logo { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; font-weight: 800;
  background: linear-gradient(135deg, #3987e5, #9085e9); color: #fff; font-size: 15px; }
.top { display: none; }
main { padding: 26px 30px 60px; max-width: 1280px; width: 100%; outline: none; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.tabbar { display: none; }
section.block { margin-top: 26px; }

/* ---------- карточки */
.grid { display: grid; gap: var(--gap); grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; min-width: 0; }
.kpi { display: flex; flex-direction: column; gap: 4px; position: relative; }
.kpi .label { color: var(--text-secondary); font-size: 13px; font-weight: 500; display: flex; justify-content: space-between; gap: 6px; }
.kpi .value { font-size: 28px; font-weight: 650; letter-spacing: -.02em; font-variant-numeric: tabular-nums; line-height: 1.15; }
.kpi .value.na { color: var(--text-muted); font-size: 20px; }
.kpi .sub { color: var(--text-muted); font-size: 12.5px; }
.kpi.neg .value { color: #ff8a8a; }
.kpi.pos .value { color: #6fd36f; }
.tag { display: inline-flex; align-items: center; gap: 4px; font-size: 11.5px; font-weight: 600; padding: 2px 8px;
  border-radius: 99px; background: var(--surface-3); color: var(--text-secondary); white-space: nowrap; }
.tag.plan { background: rgba(144,133,233,.16); color: #b9b2f2; }
.tag.est { background: rgba(250,178,25,.14); color: var(--warning); }
.tag.fact { background: rgba(57,135,229,.16); color: #8dbaf2; }
.tag.good { background: rgba(12,163,12,.16); color: #6fd36f; }
.tag.warning { background: rgba(250,178,25,.16); color: var(--warning); }
.tag.critical { background: rgba(208,59,59,.2); color: #ff8a8a; }

details.why { font-size: 12.5px; color: var(--text-muted); }
details.why summary { cursor: pointer; list-style: none; display: inline-flex; width: 20px; height: 20px; border-radius: 50%;
  border: 1px solid var(--surface-3); align-items: center; justify-content: center; font-size: 11px; color: var(--text-muted); }
details.why summary::-webkit-details-marker { display: none; }
details.why[open] summary { color: var(--text-primary); border-color: var(--text-muted); }
.why-text { margin-top: 6px; padding: 8px 10px; background: var(--surface-2); border-radius: 8px; color: var(--text-secondary); line-height: 1.4; }
.kpi details.why { position: static; }

/* ---------- индикатор бюджета */
.hero { display: grid; grid-template-columns: 1.3fr 1fr; gap: 18px; align-items: center; }
.hero .big { font-size: 38px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.meter { position: relative; height: 12px; border-radius: 99px; background: var(--surface-3); overflow: visible; margin: 14px 0 22px; }
.meter .fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 99px; min-width: 4px; }
.meter .tick { position: absolute; top: -4px; bottom: -4px; width: 2px; background: var(--bg); }
.meter .tick:last-child span { transform: translateX(-100%); }
.meter .tick span { position: absolute; top: 18px; transform: translateX(-50%); font-size: 11px; color: var(--text-muted); }
.lvl-green { background: var(--good); } .lvl-yellow { background: var(--warning); } .lvl-red { background: var(--critical); } .lvl-none { background: var(--surface-3); }
.status-line { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }

/* ---------- уведомления */
.alerts { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.alert { display: flex; gap: 10px; align-items: flex-start; padding: 10px 14px; border-radius: 12px; border: 1px solid; font-size: 14px; }
.alert.warning { border-color: rgba(250,178,25,.35); background: rgba(250,178,25,.07); }
.alert.critical { border-color: rgba(208,59,59,.45); background: rgba(208,59,59,.08); }
.alert.info { border-color: rgba(57,135,229,.35); background: rgba(57,135,229,.07); }
.alert .ic { flex: none; }

/* ---------- таблицы */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-weight: 550; font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em;
  padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 10px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
td.r, th.r { text-align: right; }
tr.warn td { background: rgba(250,178,25,.05); }
tr.total td { font-weight: 650; border-top: 1px solid var(--surface-3); }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: nowrap; }
.note { color: var(--text-muted); font-size: 12.5px; margin-top: 2px; white-space: normal; }

/* ---------- кнопки и формы */
.btn { appearance: none; border: 1px solid var(--surface-3); background: var(--surface-2); color: var(--text-primary);
  padding: 9px 14px; border-radius: 10px; font: 600 14px var(--font); cursor: pointer; white-space: nowrap; touch-action: manipulation; }
.btn:hover { border-color: var(--text-muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: #ff8a8a; }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 10px; font-size: 13px; border-radius: 8px; }
.btn.icon { padding: 6px 9px; min-width: 34px; }
input, select, textarea { width: 100%; font: 16px var(--font); color: var(--text-primary); background: var(--surface-2);
  border: 1px solid var(--surface-3); border-radius: 10px; padding: 9px 11px; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
input[type=checkbox] { width: auto; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--text-secondary); font-weight: 500; }
label.inline { flex-direction: row; align-items: center; gap: 8px; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
.form-grid .wide { grid-column: 1 / -1; }
.hint { font-size: 12px; color: var(--text-muted); font-weight: 400; }
.assume { border-left: 3px solid var(--warning); padding-left: 10px; }
.segmented { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.segmented button { border: 0; background: transparent; color: var(--text-secondary); padding: 6px 12px; border-radius: 8px; font: 600 13px var(--font); cursor: pointer; }
.segmented button.on { background: var(--surface-3); color: var(--text-primary); }

/* ---------- модальное окно */
dialog { border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary); border-radius: 18px;
  padding: 0; width: min(620px, calc(100vw - 24px)); max-height: calc(100vh - 40px); }
dialog::backdrop { background: rgba(0,0,0,.6); backdrop-filter: blur(3px); }
dialog form { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
dialog .foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.secret { font-family: var(--mono); font-size: 13px; word-break: break-all; background: var(--surface-2); padding: 10px; border-radius: 8px; user-select: all; }
pre.code { font-family: var(--mono); font-size: 12.5px; background: var(--surface-2); padding: 12px; border-radius: 10px; overflow-x: auto; color: var(--text-secondary); white-space: pre; }

/* ---------- графики */
.chart-box { position: relative; height: 280px; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; color: var(--text-secondary); margin-bottom: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }

.toast { position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--surface-3); color: var(--text-primary); padding: 10px 16px; border-radius: 12px; transition: .2s; pointer-events: none; z-index: 50; font-weight: 500; max-width: 90vw; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.bad { background: #5a1f1f; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: min(380px, 100%); display: flex; flex-direction: column; gap: 14px; background: var(--surface-1);
  border: 1px solid var(--border); border-radius: 20px; padding: 28px; }

.empty { color: var(--text-muted); padding: 18px 0; text-align: center; }
.split { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.list-row { display: flex; justify-content: space-between; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--border); }
.list-row:last-child { border-bottom: 0; }
.skeleton { height: 120px; border-radius: var(--radius); background: linear-gradient(90deg, var(--surface-1), var(--surface-2), var(--surface-1)); background-size: 200% 100%; animation: sk 1.2s infinite; }
@keyframes sk { to { background-position: -200% 0; } }

/* ---------- телефон */
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .side { display: none; }
  .top { display: flex; position: sticky; top: 0; z-index: 10; justify-content: space-between; align-items: center;
    padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px; background: rgba(14,15,17,.88);
    backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); border-bottom: 1px solid var(--border); }
  main { padding: 16px 14px calc(96px + var(--safe-b)); }
  h1 { font-size: 21px; }
  .page-head { margin-bottom: 12px; }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .grid.two { grid-template-columns: 1fr; }
  .card { padding: 13px 14px; border-radius: 13px; }
  .kpi .value { font-size: 21px; }
  .kpi .label { font-size: 12px; }
  .hero { grid-template-columns: 1fr; }
  .hero .big { font-size: 30px; }
  .chart-box { height: 230px; }
  .tabbar { display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    padding: 6px 6px calc(6px + var(--safe-b)); background: rgba(17,18,21,.94); backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px); border-top: 1px solid var(--border); }
  .tabbar a, .tabbar button { display: flex; flex-direction: column; align-items: center; gap: 2px; font: 500 10.5px var(--font);
    color: var(--text-muted); text-decoration: none; padding: 6px 0; background: none; border: 0; cursor: pointer; }
  .tabbar .ic { font-size: 19px; }
  .tabbar .on { color: var(--text-primary); }
  .sheet { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.55); display: flex; align-items: flex-end; }
  .sheet .panel { width: 100%; background: var(--surface-1); border-radius: 20px 20px 0 0; padding: 14px 12px calc(18px + var(--safe-b));
    display: grid; grid-template-columns: 1fr 1fr; gap: 6px; border-top: 1px solid var(--border); }
  .sheet a { background: var(--surface-2); padding: 14px; }
  /* таблицы превращаются в карточки */
  table.cards thead { display: none; }
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tr { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; }
  table.cards td { border: 0; padding: 3px 0; display: flex; justify-content: space-between; gap: 12px; text-align: right; }
  table.cards td::before { content: attr(data-l); color: var(--text-muted); font-size: 12.5px; text-align: left; flex: none; }
  table.cards td.title { font-weight: 650; font-size: 15px; text-align: left; display: block; }
  table.cards td.title::before { content: none; }
  table.cards td.row-actions-cell::before { content: none; }
  table.cards .row-actions { justify-content: flex-start; margin-top: 6px; }
  table.cards tr.total { background: var(--surface-3); }
  .form-grid { grid-template-columns: 1fr 1fr; }
  dialog { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 20px 20px 0 0; max-height: 92vh; }
  dialog form { padding-bottom: calc(20px + var(--safe-b)); }
}
@media (max-width: 380px) { .grid { grid-template-columns: 1fr; } }
