:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --text: #17212b;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #1f5eff;
  --primary-hover: #1748c8;
  --success: #16803d;
  --success-bg: #eaf7ef;
  --danger: #c9362b;
  --danger-bg: #fff0ef;
  --warning: #9a6700;
  --warning-bg: #fff7df;
  --shadow: 0 12px 32px rgba(25, 33, 45, .07);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.45;
}
a { color: inherit; }
.topbar {
  height: 68px;
  padding: 0 28px;
  display: flex;
  align-items: center;
  gap: 14px;
  background: rgba(255,255,255,.92);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
  backdrop-filter: blur(12px);
}
.brand { text-decoration: none; font-weight: 750; letter-spacing: -.02em; }
.subtitle { color: var(--muted); font-size: 14px; }
.container { max-width: 1240px; margin: 0 auto; padding: 38px 24px 64px; }
.page-head { display: flex; justify-content: space-between; align-items: end; gap: 24px; margin-bottom: 24px; }
.page-head.compact { align-items: start; }
h1 { margin: 6px 0 6px; font-size: clamp(28px, 4vw, 42px); line-height: 1.1; letter-spacing: -.035em; }
h2 { margin: 0; font-size: 19px; letter-spacing: -.015em; }
h3 { margin-top: 0; }
p { color: var(--muted); }
.back-link { color: var(--muted); text-decoration: none; font-size: 14px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 24px; box-shadow: var(--shadow); margin-bottom: 20px; }
.section-title-row { display: flex; align-items: center; gap: 10px; margin-bottom: 20px; }
.counter { min-width: 26px; height: 26px; padding: 0 8px; border-radius: 13px; background: var(--surface-2); color: var(--muted); display: inline-flex; align-items: center; justify-content: center; font-size: 13px; }
.btn { min-height: 42px; border: 1px solid transparent; border-radius: 11px; padding: 10px 15px; font: inherit; font-weight: 650; text-decoration: none; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; transition: .15s ease; white-space: nowrap; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: #fff; color: var(--text); border-color: var(--line); }
.btn-secondary:hover { background: var(--surface-2); }
.btn-danger { color: var(--danger); border-color: #efc6c2; background: #fff; }
.btn-danger:hover { background: var(--danger-bg); }
.btn-small { min-height: 36px; padding: 7px 11px; font-size: 13px; }
.form-card { max-width: 760px; }
.form-grid { display: grid; gap: 18px; }
.compact-form { max-width: 760px; }
label { display: grid; gap: 7px; font-size: 14px; font-weight: 650; }
label span b { color: var(--danger); }
input, textarea, select {
  width: 100%; border: 1px solid #d7dce2; background: #fff; color: var(--text); border-radius: 11px; padding: 11px 12px; font: inherit; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: #7ea0ff; box-shadow: 0 0 0 3px rgba(31,94,255,.1); }
textarea { resize: vertical; }
.actions, .row-actions, .button-grid { display: flex; gap: 10px; flex-wrap: wrap; }
.button-grid { margin-top: 22px; }
.info-grid { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 18px 24px; }
.info-grid > div { min-width: 0; }
.key { display: block; color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 5px; }
.status { display: inline-flex; align-items: center; border-radius: 999px; padding: 5px 9px; font-size: 12px; font-weight: 750; }
.status.success { color: var(--success); background: var(--success-bg); }
.status.error { color: var(--danger); background: var(--danger-bg); }
.status.pending { color: var(--warning); background: var(--warning-bg); }
.info-button { border: 0; background: transparent; cursor: pointer; color: var(--muted); font-size: 16px; vertical-align: middle; }
.source-add-grid { display: grid; grid-template-columns: 1fr 2fr auto; gap: 14px; align-items: end; }
.align-end { align-self: end; }
.sources-list { display: grid; gap: 12px; }
.source-row { border: 1px solid var(--line); border-radius: 14px; padding: 16px; background: var(--surface-2); }
.source-display { display: flex; align-items: center; justify-content: space-between; gap: 18px; }
.source-main { min-width: 0; display: grid; gap: 5px; }
.source-main a { color: var(--primary); overflow-wrap: anywhere; font-size: 13px; }
.source-edit { display: grid; grid-template-columns: 1fr 2fr auto; align-items: end; gap: 12px; }
.empty-state, .empty-inline { padding: 26px; text-align: center; color: var(--muted); }
.empty-state h2 { color: var(--text); margin-bottom: 8px; }
.table-card { padding: 0; overflow: hidden; }
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td { padding: 15px 18px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.data-table th { font-size: 12px; text-transform: uppercase; letter-spacing: .045em; color: var(--muted); background: var(--surface-2); cursor: pointer; user-select: none; }
.data-table tr:last-child td { border-bottom: 0; }
.group-link { color: var(--primary); font-weight: 700; text-decoration: none; }
.muted { color: var(--muted); }
.toast { position: fixed; top: 82px; right: 24px; max-width: min(520px, calc(100vw - 48px)); z-index: 50; padding: 13px 16px; border-radius: 12px; box-shadow: var(--shadow); border: 1px solid; background: white; }
.toast-success { color: var(--success); border-color: #b9e0c7; }
.toast-error { color: var(--danger); border-color: #efc6c2; }
.modal-backdrop { position: fixed; inset: 0; background: rgba(15,23,42,.42); z-index: 60; display: grid; place-items: center; padding: 24px; }
.modal-backdrop[hidden] { display: none; }
.modal { width: min(680px, 100%); max-height: 82vh; overflow: auto; background: #fff; border-radius: 18px; padding: 24px; box-shadow: 0 30px 80px rgba(15,23,42,.24); position: relative; }
.modal.warning-modal { border-top: 5px solid #d89a13; }
.modal-close { position: absolute; top: 13px; right: 14px; width: 34px; height: 34px; border: 0; border-radius: 10px; background: var(--surface-2); cursor: pointer; font-size: 24px; line-height: 1; }
pre { white-space: pre-wrap; overflow-wrap: anywhere; background: #f6f8fa; border: 1px solid var(--line); border-radius: 12px; padding: 14px; color: #394150; font-size: 12px; }

@media (max-width: 900px) {
  .info-grid { grid-template-columns: 1fr 1fr; }
  .source-add-grid, .source-edit { grid-template-columns: 1fr; }
  .source-display { align-items: start; flex-direction: column; }
}
@media (max-width: 640px) {
  .container { padding: 26px 14px 48px; }
  .topbar { padding: 0 14px; }
  .subtitle { display: none; }
  .page-head { align-items: stretch; flex-direction: column; }
  .info-grid { grid-template-columns: 1fr; }
  .card { border-radius: 14px; padding: 18px; }
}

/* Диагностика отдельных XML-источников */
.source-title-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.source-health { display: inline-flex; align-items: center; gap: 5px; }
.source-meta { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 8px; color: var(--muted); font-size: 12px; }
.source-meta b { color: var(--text); font-weight: 600; }

/* UI refinements: group/source actions and explicit edit-mode visibility */
.section-title-row-between { justify-content: space-between; }
.group-header-actions { margin-left: auto; }
.group-header-actions form,
.source-header-actions form { display: inline-flex; margin: 0; }

.source-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}
.source-header-actions { flex-shrink: 0; }
.source-meta {
  margin-top: 10px;
  margin-bottom: 18px;
}
.source-display { display: block; }
.source-display[hidden],
.source-edit[hidden] { display: none !important; }
.source-url-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}
.source-url-text {
  min-width: 0;
  max-width: 720px;
  color: var(--muted);
  font-size: 13px;
  overflow-wrap: anywhere;
}
.source-edit {
  display: grid;
  grid-template-columns: minmax(220px, .8fr) minmax(320px, 1.35fr) auto;
  align-items: end;
  gap: 12px;
  margin-top: 24px;
}
.source-edit-url { max-width: 620px; }
.source-edit input { min-height: 43px; }
.source-edit-actions { align-items: center; flex-wrap: nowrap; }
.source-edit-actions .btn { min-height: 43px; padding-top: 9px; padding-bottom: 9px; }

@media (max-width: 900px) {
  .section-title-row-between,
  .source-card-header { align-items: flex-start; }
  .source-card-header { flex-direction: column; }
  .source-edit { grid-template-columns: 1fr; }
  .source-edit-url { max-width: none; }
}
@media (max-width: 640px) {
  .section-title-row-between { align-items: flex-start; flex-direction: column; }
  .group-header-actions { margin-left: 0; }
  .source-url-row { align-items: flex-start; flex-direction: column; }
  .source-edit-actions { flex-wrap: wrap; }
}

/* Session login / logout */
.topbar-actions { margin-left: auto; display: flex; align-items: center; }
.logout-form { margin: 0; }
.login-shell { min-height: calc(100vh - 170px); display: grid; place-items: center; }
.login-card { width: min(440px, 100%); margin: 0; }
.login-heading { margin-bottom: 24px; }
.login-heading h1 { font-size: 30px; margin: 0 0 8px; }
.login-heading p { margin: 0; }
.login-form .btn { width: 100%; margin-top: 4px; }
.inline-message { border: 1px solid var(--line); border-radius: 11px; padding: 11px 13px; margin-bottom: 18px; font-size: 14px; }
.inline-message.success { color: var(--success); border-color: #b9e0c7; background: var(--success-bg); }
.inline-message.error { color: var(--danger); border-color: #efc6c2; background: var(--danger-bg); }


/* Source sorting and advertisement counters */
.sources-section-header { justify-content: space-between; gap: 16px; }
.sources-title-wrap { display: flex; align-items: center; gap: 8px; }
.source-sort-button { margin-left: auto; }
.source-item-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  height: 26px;
  padding: 0 8px;
  border-radius: 999px;
  background: #eef2f6;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}
@media (max-width: 640px) {
  .sources-section-header { align-items: flex-start; flex-direction: column; }
  .source-sort-button { margin-left: 0; }
}

/* Group info card: pin management actions to the top-right corner */
.group-info-card { position: relative; }
.group-info-title-row { padding-right: 470px; }
.group-info-card .group-header-actions {
  position: absolute;
  top: 24px;
  right: 24px;
  margin-left: 0;
  z-index: 1;
}
@media (max-width: 900px) {
  .group-info-title-row { padding-right: 0; }
  .group-info-card .group-header-actions {
    position: static;
    margin-top: 12px;
  }
  .group-info-title-row {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
  }
}
