:root {
  --bg: #f4f5f7; --panel: #ffffff; --panel-2: #f8f9fb; --border: #e4e6eb;
  --text: #14161a; --muted: #6b7280; --accent: #fe2c55; --accent-2: #25f4ee;
  --good: #12a150; --good-bg: #e7f7ee; --bad: #dc2626; --bad-bg: #fdecec;
  --warn: #c2410c; --warn-bg: #fff2e6; --info: #2563eb; --info-bg: #eaf1ff;
  --bar: #c7cbd4; --line: #fe2c55; --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115; --panel: #171a21; --panel-2: #1d2129; --border: #2a2f3a;
    --text: #eef0f4; --muted: #9aa3b2; --good: #34d399; --good-bg: #0f2a1f; --bad: #f87171; --bad-bg: #2d1414;
    --warn: #fb923c; --warn-bg: #2d1c0e; --info: #60a5fa; --info-bg: #13203a; --bar: #3a4150;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--info); }
.loading-screen { min-height: 100vh; display: grid; place-items: center; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 28px; box-shadow: var(--shadow); }
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login p { margin: 0 0 20px; color: var(--muted); }
.login .err { color: var(--bad); margin-top: 10px; min-height: 20px; }

/* Header */
.topbar { position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--border); }
.topbar-in { max-width: 1320px; margin: 0 auto; padding: 10px 16px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; margin-right: 6px; white-space: nowrap; }
.brand .logo { width: 26px; height: 26px; border-radius: 7px; background: var(--accent); display: grid; place-items: center; }
.badge { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--warn-bg); color: var(--warn); white-space: nowrap; }
.badge.ro { background: var(--info-bg); color: var(--info); }
.spacer { flex: 1; }
select, input[type=date], input[type=text], input[type=password] {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; min-height: 38px; max-width: 100%; }
input[type=text], input[type=password] { width: 100%; }
.seg { display: inline-flex; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: none; padding: 5px 10px; border-radius: 7px; cursor: pointer; color: var(--muted); white-space: nowrap; }
.seg button.on { background: var(--text); color: var(--bg); }
.btn { border: 1px solid var(--border); background: var(--panel); border-radius: 10px; padding: 8px 14px; cursor: pointer; min-height: 38px; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.custom-dates { display: none; gap: 6px; align-items: center; }
.custom-dates.on { display: inline-flex; }

main { max-width: 1320px; margin: 0 auto; padding: 16px; }
.share-head { margin: 4px 0 14px; }
.share-head h1 { margin: 0; font-size: 22px; }
.share-head p { margin: 2px 0 0; color: var(--muted); }

/* KPI */
.kpis { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 10px; margin-bottom: 14px; }
.kpi { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); min-width: 0; }
.kpi .l { color: var(--muted); font-size: 12px; }
.kpi .v { font-size: 20px; font-weight: 700; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.kpi.hl .v { color: var(--accent); }

.grid2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: 14px; margin-bottom: 14px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); min-width: 0; }
.card h3 { margin: 0 0 10px; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-left: auto; font-weight: 400; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }
.chart svg { width: 100%; height: 220px; display: block; }
.chart text { fill: var(--muted); font-size: 10px; }
.tip { position: fixed; pointer-events: none; background: var(--text); color: var(--bg); font-size: 12px; padding: 6px 8px; border-radius: 8px; z-index: 50; display: none; white-space: nowrap; }

.insights { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; max-height: 260px; overflow: auto; }
.insights li { padding: 9px 11px; border-radius: 10px; font-size: 13px; border-left: 3px solid; }
.insights .good { background: var(--good-bg); border-color: var(--good); }
.insights .bad { background: var(--bad-bg); border-color: var(--bad); }
.insights .warn { background: var(--warn-bg); border-color: var(--warn); }
.insights .info { background: var(--info-bg); border-color: var(--info); }

/* Tabs */
.tabs { display: flex; gap: 6px; margin: 6px 0 12px; align-items: center; flex-wrap: wrap; }
.tabs .tab { border: 0; background: none; padding: 8px 14px; border-radius: 10px; cursor: pointer; font-weight: 600; color: var(--muted); }
.tabs .tab.on { background: var(--panel); color: var(--text); box-shadow: var(--shadow); border: 1px solid var(--border); }

/* Tree table */
.tbl { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.row { display: grid; grid-template-columns: minmax(260px, 2.6fr) repeat(7, minmax(70px, 1fr)); gap: 8px; align-items: center; padding: 10px 14px; border-top: 1px solid var(--border); }
.row.head { border-top: 0; background: var(--panel-2); color: var(--muted); font-size: 12px; font-weight: 600; position: sticky; top: 0; }
.row.head .num { cursor: pointer; user-select: none; }
.row .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.row.l1 { background: var(--panel-2); }
.row.l1:hover, .row.l2:hover, .row.l0:hover { background: color-mix(in srgb, var(--panel-2) 60%, var(--border)); }
.row.l0 { cursor: pointer; }
.row.l1 { cursor: pointer; }
.namecell { display: flex; align-items: center; gap: 10px; min-width: 0; }
.namecell .nm { min-width: 0; }
.namecell .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.namecell .s { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.caret { width: 18px; flex: none; color: var(--muted); transition: transform .15s; text-align: center; }
.caret.open { transform: rotate(90deg); }
.ind1 { padding-left: 22px; }
.ind2 { padding-left: 44px; }
.thumb { width: 38px; height: 52px; border-radius: 6px; flex: none; object-fit: cover; background: var(--border); position: relative; overflow: hidden; display: grid; place-items: center; color: #fff; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.mlabel { display: none; }

/* Toggle */
.sw { position: relative; width: 38px; height: 22px; flex: none; }
.sw input { opacity: 0; width: 0; height: 0; position: absolute; }
.sw span { position: absolute; inset: 0; background: var(--bar); border-radius: 99px; cursor: pointer; transition: .15s; }
.sw span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: .15s; box-shadow: 0 1px 2px rgba(0,0,0,.25); }
.sw input:checked + span { background: var(--good); }
.sw input:checked + span::after { transform: translateX(16px); }
.sw input:disabled + span { opacity: .5; cursor: wait; }
.pill { font-size: 11px; padding: 2px 8px; border-radius: 99px; font-weight: 600; white-space: nowrap; flex: none; }
.pill.on { background: var(--good-bg); color: var(--good); }
.pill.off { background: var(--panel-2); color: var(--muted); border: 1px solid var(--border); }

.v-best { background: var(--good-bg); color: var(--good); }
.v-good { background: var(--info-bg); color: var(--info); }
.v-expensive { background: var(--warn-bg); color: var(--warn); }
.v-noleads { background: var(--bad-bg); color: var(--bad); }
.v-learning, .v-nodata { background: var(--panel-2); color: var(--muted); border: 1px solid var(--border); }

/* Creatives grid */
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.cgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; }
.cr { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); display: flex; flex-direction: column; }
.cr .media { aspect-ratio: 9/12; position: relative; background: var(--border); display: grid; place-items: center; color: #fff; overflow: hidden; }
.cr .media img { width: 100%; height: 100%; object-fit: cover; position: absolute; inset: 0; }
.cr .media .vb { position: absolute; top: 8px; left: 8px; }
.cr .media .sw { position: absolute; top: 8px; right: 8px; }
.cr .media .play { position: relative; width: 46px; height: 46px; border-radius: 50%; background: rgba(0,0,0,.35); display: grid; place-items: center; }
.cr .media .cap { position: absolute; left: 10px; right: 10px; bottom: 10px; font-size: 12px; text-shadow: 0 1px 3px rgba(0,0,0,.6); }
.cr .body { padding: 10px 12px 12px; }
.cr .body .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cr .body .s { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-bottom: 8px; }
.mets { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.mets div { background: var(--panel-2); border-radius: 8px; padding: 5px 7px; min-width: 0; }
.mets b { display: block; font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mets span { font-size: 11px; color: var(--muted); }
.tagline { font-size: 11px; color: var(--warn); margin-top: 6px; }

/* Modal */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40; display: grid; place-items: center; padding: 16px; }
.modal { background: var(--panel); border-radius: var(--radius); border: 1px solid var(--border); width: 100%; max-width: 640px; max-height: 90vh; overflow: auto; padding: 20px; }
.modal h2 { margin: 0 0 14px; font-size: 18px; display: flex; align-items: center; }
.modal h2 .x { margin-left: auto; border: 0; background: none; font-size: 22px; cursor: pointer; color: var(--muted); }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.field select { width: 100%; }
.linkbox { display: flex; gap: 8px; }
.linkbox input { flex: 1; min-width: 0; }
.share-list { list-style: none; padding: 0; margin: 8px 0 0; }
.share-list li { display: flex; gap: 8px; align-items: center; padding: 8px 0; border-top: 1px solid var(--border); }
.share-list li .nm { flex: 1; min-width: 0; }
.share-list li .nm div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.share-list li .nm small { color: var(--muted); }
.md h2 { font-size: 16px; margin: 16px 0 6px; }
.md ul { margin: 4px 0; padding-left: 20px; }
.md p { margin: 6px 0; }
.empty { padding: 30px; text-align: center; color: var(--muted); }
.err-box { background: var(--bad-bg); color: var(--bad); padding: 12px; border-radius: 10px; margin-bottom: 12px; }

#toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 60; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: calc(100% - 32px); }
#toast.on { opacity: 1; }
.loadbar { height: 2px; background: linear-gradient(90deg, transparent, var(--accent), transparent); background-size: 200% 100%; animation: lb 1s linear infinite; position: fixed; top: 0; left: 0; right: 0; z-index: 30; display: none; }
.loadbar.on { display: block; }
@keyframes lb { from { background-position: 200% 0; } to { background-position: -200% 0; } }

/* Responsive */
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .grid2 { grid-template-columns: 1fr; }
  .row { grid-template-columns: minmax(220px, 2.6fr) repeat(4, minmax(70px, 1fr)); }
  .row .num.hide-m { display: none; }
}
@media (max-width: 760px) {
  .topbar-in { padding: 8px 16px; gap: 8px; }
  .brand { width: 100%; margin: 0; }
  .brand .logout { margin-left: auto; }
  #acc { flex: 1 1 100%; }
  .seg { width: 100%; }
  .seg button { flex: 1; padding: 6px 4px; }
  .custom-dates.on { display: flex; width: 100%; }
  .custom-dates input { flex: 1; min-width: 0; }
  .topbar { position: static; }
  .seg button { font-size: 12px; min-width: 0; }
  .actions { display: flex; gap: 6px; width: 100%; }
  .actions .btn { flex: 1 1 0; min-width: 0; justify-content: center; padding: 8px 6px; font-size: 13px; overflow: hidden; text-overflow: ellipsis; }
  .mets { gap: 4px; }
  .mets div { padding: 4px 5px; }
  .mets b { font-size: 12px; }
  .spacer { display: none; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .kpi .v { font-size: 17px; }
  .chart svg { height: 180px; }
  .tbl { background: none; border: 0; box-shadow: none; overflow: visible; }
  .row.head { display: none; }
  .row { grid-template-columns: repeat(3, minmax(0, 1fr)); border: 1px solid var(--border); border-radius: 12px; margin-bottom: 8px; background: var(--panel); row-gap: 6px; }
  .row.l1 { margin-left: 10px; }
  .row.l2 { margin-left: 20px; }
  .row .namecell { grid-column: 1 / -1; }
  .ind1, .ind2 { padding-left: 0; }
  .row .num { text-align: left; background: var(--panel-2); border-radius: 8px; padding: 4px 7px; }
  .row .num.hide-m { display: none; }
  .mlabel { display: block; font-size: 11px; color: var(--muted); }
  .cgrid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .mets { grid-template-columns: repeat(2, 1fr); }
  .cr .body { padding: 8px; }
  main { padding: 12px 16px; }
}

/* Кабинеттер тізімі */
.kpis.k4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.back { display: inline-block; margin-bottom: 4px; text-decoration: none; font-size: 13px; }
.linkbtn { flex: none; margin-left: auto; }
.row .namecell .nm { flex: 1; }
@media (max-width: 760px) { .kpis.k4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

/* ===== Телефон нұсқасы ===== */
.m-only { display: none !important; }
.bnav { display: none; }
.brand-r { margin-left: auto; display: inline-flex; gap: 6px; }
.btn.icon { width: 34px; padding: 0; justify-content: center; font-size: 17px; }

@media (max-width: 760px) {
  .m-only { display: inline-flex !important; }
  body { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  input, select, textarea { font-size: 16px !important; } /* iOS-та зум болмауы үшін */

  /* Жоғарғы панель — шағын, жабысқақ */
  .topbar { position: sticky; top: 0; }
  .topbar-in { padding: 8px 12px; gap: 6px; padding-top: calc(8px + env(safe-area-inset-top)); }
  .brand { font-size: 15px; }
  .brand .logout { margin-left: 0; }
  .actions { display: none !important; }
  #acc { min-height: 40px; }
  .seg { overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .seg::-webkit-scrollbar { display: none; }
  .seg button { flex: 1 0 auto; padding: 7px 10px; font-size: 13px; }

  main { padding: 12px; }
  .share-head h1 { font-size: 19px; }
  .share-head p { font-size: 12px; }

  /* KPI: негізгі 3 үлкен, қалғаны шағын */
  .kpis.kmain { grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 6px; }
  .kpis.kmain .kpi { grid-column: span 3; padding: 8px; border-radius: 10px; }
  .kpis.kmain .kpi:nth-child(-n+3) { grid-column: span 4; padding: 10px; }
  .kpis.kmain .kpi .l { font-size: 10.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .kpis.kmain .kpi .v { font-size: 12px; }
  .kpis.kmain .kpi:nth-child(-n+3) .v { font-size: 14px; }
  .kpis.k4 .kpi .v { font-size: 16px; }

  .card { padding: 12px; }
  .chart svg { height: 150px; }
  .insights { max-height: none; }
  .insights.collapsed li:nth-child(n+3) { display: none; }
  .insights li { font-size: 13px; padding: 8px 10px; }
  .more { margin-top: 8px; width: 100%; justify-content: center; }

  .tabs { position: sticky; top: var(--hdr, 0); z-index: 15; background: var(--bg); margin: 0 -12px 10px; padding: 6px 12px; }
  .tabs .tab { flex: 1; text-align: center; padding: 9px 6px; }

  /* Кесте → ықшам карточкалар, 4 көрсеткіш бір қатарда */
  .row { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; padding: 10px; column-gap: 4px; }
  .row.l1 { margin-left: 8px; }
  .row.l2 { margin-left: 16px; }
  .row .num { font-size: 12.5px; padding: 4px 6px; overflow: hidden; text-overflow: ellipsis; }
  .mlabel { font-size: 10px; }
  .namecell { gap: 8px; }
  .namecell .t { white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-height: 1.3; }
  .linkbtn { padding: 4px 8px; }

  /* Тетіктер саусаққа ыңғайлы */
  .sw { width: 46px; height: 28px; }
  .sw span::after { width: 22px; height: 22px; }
  .sw input:checked + span::after { transform: translateX(18px); }
  .btn.small { min-height: 34px; }

  .filters input, .filters select { flex: 1 1 140px; }
  .cgrid { gap: 8px; }
  .cr .media { aspect-ratio: 3/4; }

  /* Төменгі мәзір */
  .bnav { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 25; background: var(--panel); border-top: 1px solid var(--border);
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); }
  .bnav button { flex: 1; border: 0; background: none; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 2px; color: var(--muted); font-size: 11px; border-radius: 10px; cursor: pointer; min-width: 0; }
  .bnav button.on { color: var(--accent); }
  .bnav button span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

  /* Модаль — төменнен шығатын парақ */
  .modal-bg { place-items: end stretch; padding: 0; }
  .modal { max-width: none; border-radius: 18px 18px 0 0; max-height: 88vh; padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); animation: sheet .2s ease-out; }
  @keyframes sheet { from { transform: translateY(40px); opacity: .6; } to { transform: none; opacity: 1; } }
  .linkbox { flex-wrap: wrap; }
  .linkbox input { flex: 1 1 100%; }
  .linkbox .btn { flex: 1; justify-content: center; }
  #toast { bottom: calc(76px + env(safe-area-inset-bottom)); }
}

/* TikTok қосу */
.ov-head { display: flex; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.ov-head > div { flex: 1; min-width: 200px; }
.tt-banner { display: flex; align-items: center; gap: 12px; background: var(--warn-bg); border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent); color: var(--text); padding: 12px 14px; border-radius: var(--radius); margin-bottom: 14px; flex-wrap: wrap; }
.tt-banner > div { flex: 1; min-width: 200px; font-size: 13px; }
.steps { padding-left: 20px; margin: 6px 0; }
.steps li { margin-bottom: 8px; }
@media (max-width: 760px) { .tt-banner .btn { width: 100%; justify-content: center; } }
