:root {
  --bg: #eef1f7;
  --surface: #ffffff;
  --border: #e4e8f0;
  --text: #1b2230;
  --muted: #6b7686;
  --primary: #d6332e;        /* фирменный красный Яндекса */
  --primary-dark: #b3231f;
  --up: #18a957;             /* рост позиций */
  --down: #e0483d;           /* падение позиций */
  --sidebar-bg: #171d2b;
  --sidebar-soft: #222a3b;
  --sidebar-text: #aab2c4;
  --shadow: 0 1px 2px rgba(20, 30, 50, 0.06), 0 8px 24px rgba(20, 30, 50, 0.06);
  --radius: 14px;
}

* { box-sizing: border-box; }

/* Атрибут hidden должен скрывать элемент даже если у класса задан display
   (иначе .login-screen/.btn с display перебивают браузерное [hidden]). */
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ============ Каркас ============ */
.app { display: flex; align-items: flex-start; min-height: 100vh; }

.sidebar {
  width: 250px;
  flex-shrink: 0;
  align-self: stretch;
  position: sticky;
  top: 0;
  height: 100vh;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  font-weight: 700; font-size: 19px; color: #fff;
  padding: 6px 10px 18px;
}
.brand .logo { font-size: 22px; }

.site-switch { padding: 0 6px 14px; }
.site-switch select {
  width: 100%;
  background: var(--sidebar-soft);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, .12);
  border-radius: 10px;
  padding: 9px 11px;
  font-size: 13px;
  cursor: pointer;
}
.site-switch select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(214, 51, 46, .25); }
.site-switch select option { color: #1b2230; background: #fff; }

.nav { display: flex; flex-direction: column; gap: 4px; }
.nav a {
  display: flex; align-items: center; gap: 12px;
  text-decoration: none;
  color: var(--sidebar-text);
  padding: 10px 12px;
  border-radius: 10px;
  font-weight: 500;
  transition: background .15s, color .15s;
}
.nav a svg { width: 19px; height: 19px; flex-shrink: 0; opacity: .85; }
.nav a:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav a.active { background: var(--sidebar-soft); color: #fff; }
.nav a.active svg { color: var(--primary); opacity: 1; }

.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 12px; padding-top: 16px; }
.site-card {
  background: rgba(255,255,255,.05);
  border: 1px solid rgba(255,255,255,.07);
  border-radius: 12px;
  padding: 12px 14px;
}
.site-card-label { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: #7a839a; }
.site-card-name { color: #fff; font-weight: 600; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.site-card-url { font-size: 12px; color: #8b93a7; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.site-card-status { font-size: 11px; color: #757e94; margin-top: 8px; line-height: 1.6; }

.main { flex: 1; min-width: 0; padding: 30px 34px 64px; }
.loader { color: var(--muted); padding: 60px; text-align: center; }

/* ============ Заголовок страницы ============ */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; margin-bottom: 22px; gap: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 24px; margin: 0 0 4px; letter-spacing: -.01em; }
.page-head .sub { color: var(--muted); font-size: 13px; }

/* ============ Карточки ============ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  margin-bottom: 20px;
}
.card h2 { font-size: 16px; margin: 0 0 16px; }
.card.pad-0 { padding: 0; overflow: hidden; }

/* ============ Сводка (плитки) ============ */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 22px; }
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px;
}
.stat-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; margin-bottom: 8px; }
.stat-value { font-size: 27px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.stat-value .delta { font-size: 20px; }

/* ============ Формы ============ */
.field { margin-bottom: 16px; }
.field label { display: block; font-weight: 600; margin-bottom: 6px; font-size: 13px; }
.field .hint { color: var(--muted); font-size: 12px; margin-top: 5px; }
input[type="text"], input[type="password"], select {
  width: 100%;
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  background: #fff;
  color: var(--text);
  font-family: inherit;
}
input:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(214,51,46,.13); }
.row { display: flex; gap: 14px; flex-wrap: wrap; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s, transform .05s;
}
.btn svg { width: 17px; height: 17px; }
.btn:hover { background: var(--bg); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: #fbeceb; color: var(--down); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 11px; font-size: 12px; }

/* ============ Таблица ============ */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 13px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
thead th { font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; white-space: nowrap; background: #fafbfd; }
thead th.num { text-align: right; }
thead th.sortable { cursor: pointer; user-select: none; transition: color .12s; }
thead th.sortable:hover { color: var(--text); }
thead th.sorted { color: var(--text); }
.th-inner { display: inline-flex; align-items: center; gap: 5px; }
.sort-arrow { font-size: 9px; color: var(--primary); line-height: 1; }
tbody tr { transition: background .12s; }
tbody tr:hover { background: #fafbfe; }
tbody tr:last-child td { border-bottom: none; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td a { color: var(--primary); text-decoration: none; font-weight: 500; }
td a:hover { text-decoration: underline; }
.page-cell .pname { font-weight: 600; }
.page-cell .purl { color: var(--muted); font-size: 12px; max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.pos { font-weight: 700; font-variant-numeric: tabular-nums; }
.delta { display: inline-flex; align-items: center; gap: 3px; font-weight: 700; font-variant-numeric: tabular-nums; }
.delta.up { color: var(--up); }
.delta.down { color: var(--down); }
.delta.flat { color: var(--muted); }

/* ============ Дашборд: группы по страницам ============ */
.list-toolbar { display: flex; justify-content: flex-end; margin-bottom: 12px; }
.grp-list { display: flex; flex-direction: column; gap: 12px; }
.grp { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.grp-head { display: flex; align-items: center; gap: 14px; padding: 15px 20px; cursor: pointer; user-select: none; transition: background .12s; }
.grp-head:hover { background: #fafbfe; }
.grp.open .grp-head { border-bottom: 1px solid var(--border); }
.grp-caret { color: var(--muted); font-size: 11px; width: 12px; flex-shrink: 0; }
.grp-title { flex: 1; min-width: 0; }
.grp-title .pname { font-weight: 600; }
.grp-title .purl { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.grp-stats { display: flex; align-items: center; gap: 18px; color: var(--muted); font-size: 13px; white-space: nowrap; flex-wrap: wrap; justify-content: flex-end; }
.grp-stats b { color: var(--text); font-variant-numeric: tabular-nums; }

/* ============ Прочее ============ */
.empty { text-align: center; color: var(--muted); padding: 52px 16px; }
.empty svg { width: 56px; height: 56px; color: #c4ccdb; margin-bottom: 14px; }
.empty .big { font-size: 17px; color: var(--text); font-weight: 600; margin-bottom: 4px; }
.meta { color: var(--muted); font-size: 13px; margin-bottom: 4px; }
.meta b { color: var(--text); }
.link-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 0; border-bottom: 1px solid var(--border); }
.link-row:last-child { border-bottom: none; }
.link-row .info .name { font-weight: 600; }
.link-row .info .url { color: var(--muted); font-size: 12px; word-break: break-all; }
.banner { background: #fff7e6; border: 1px solid #ffe0a3; color: #8a5a00; padding: 14px 16px; border-radius: 10px; margin-bottom: 20px; }
.spinner { display: inline-block; width: 15px; height: 15px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ============ График ============ */
.chart svg { display: block; width: 100%; height: auto; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis-label { fill: var(--muted); font-size: 11px; }
.chart .line { fill: none; stroke: var(--primary); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: rgba(214, 51, 46, 0.08); stroke: none; }
.chart .dot { fill: #fff; stroke: var(--primary); stroke-width: 2; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 18px; margin-top: 14px; color: var(--muted); font-size: 13px; }
.chart-legend b { color: var(--text); }

/* ============ Видимость по ТОПам ============ */
.vis-chips { display: flex; gap: 12px; flex-wrap: wrap; margin: 4px 0 18px; }
.vis-chip { display: flex; align-items: center; gap: 10px; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; min-width: 96px; }
.vis-dot { width: 11px; height: 11px; border-radius: 3px; display: inline-block; flex-shrink: 0; }
.vis-chip-val { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1; }
.vis-chip-label { font-size: 12px; color: var(--muted); margin-top: 3px; }
.vis-leg { display: inline-flex; align-items: center; gap: 6px; }

/* ============ Импорт из sitemap ============ */
.sm-bar { display: flex; gap: 8px; align-items: center; margin: 16px 0 10px; flex-wrap: wrap; }
.sm-bar input { flex: 1; min-width: 200px; }
.sm-meta { color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.sm-meta b { color: var(--text); }
.sm-list { max-height: 340px; overflow-y: auto; border: 1px solid var(--border); border-radius: 10px; }
.sm-row { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-bottom: 1px solid var(--border); cursor: pointer; font-size: 13px; }
.sm-row:last-child { border-bottom: none; }
.sm-row:hover { background: #fafbfe; }
.sm-row.sm-muted { color: var(--muted); cursor: default; }
.sm-u { word-break: break-all; }
.sm-exist { margin-left: auto; font-size: 11px; color: var(--muted); background: var(--bg); border: 1px solid var(--border); padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.sm-cb { width: 16px; height: 16px; flex-shrink: 0; }

/* ============ Тосты ============ */
.toasts { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 100; }
.toast { background: #2b3447; color: #fff; padding: 13px 17px; border-radius: 10px; box-shadow: var(--shadow); max-width: 380px; animation: slidein .2s ease; }
.toast.error { background: var(--down); }
.toast.success { background: var(--up); }
@keyframes slidein { from { transform: translateY(12px); opacity: 0; } }

/* ============ Экран входа ============ */
.login-screen { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px; width: 100%; max-width: 360px; }
.login-brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 22px; }
.login-brand .logo { font-size: 26px; }
.login-sub { color: var(--muted); margin: 4px 0 20px; }
.login-error { background: #fdecec; color: var(--down); border: 1px solid #f5c6c4; padding: 9px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 14px; }
.sidebar-logout { background: transparent; color: var(--sidebar-text); border-color: rgba(255, 255, 255, .14); }
.sidebar-logout:hover { background: rgba(255, 255, 255, .06); color: #fff; }

/* ============ Адаптив ============ */
@media (max-width: 860px) {
  .app { flex-direction: column; }
  .sidebar {
    width: 100%; height: auto; position: sticky; top: 0; z-index: 20;
    flex-direction: row; align-items: center; flex-wrap: wrap; gap: 8px; padding: 10px 14px;
  }
  .brand { padding: 0 8px 0 4px; }
  .nav { flex-direction: row; flex: 1; overflow-x: auto; }
  .nav a span { display: none; }
  .nav a { padding: 9px 11px; }
  .site-switch { width: 100%; padding: 0; order: 5; }
  .sidebar-foot { margin: 0; flex-direction: row; align-items: center; gap: 10px; padding: 0; }
  .site-card { display: none; }
  .btn-block { width: auto; }
  .main { padding: 20px 16px 48px; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .grp-head { flex-wrap: wrap; }
  .grp-stats { width: 100%; padding-left: 26px; justify-content: flex-start; gap: 12px; font-size: 12px; }
}
