/* Custom responsive layer for admin + owner dashboards */

.admin-mobile-nav-toggle {
  display: none;
  border: 1px solid #d9d9d9;
  border-radius: 8px;
  background: #fff;
  min-height: 44px;
  min-width: 44px;
  padding: 8px 12px;
  cursor: pointer;
  font-weight: 600;
}

/* Campaign UTM inline: keep the admin sidebar and page width intact. */
#links-group {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
}
#links-group .tabular.inline-related {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
#links-group table {
  width: max-content;
  min-width: 100%;
}
#links-group input,
#links-group select {
  max-width: 15rem;
}
#links-group .campaign-link-url {
  min-width: 0;
  max-width: 14rem;
}

/* ============================================================
   MOBILE  (≤ 767px)
   ============================================================ */
@media (max-width: 767px) {
  .admin-mobile-nav-toggle { display: inline-flex; align-items: center; gap: 6px; }

  /* --- Layout resets --- */
  #container { min-width: 0 !important; width: 100% !important; }
  #content { padding: 10px !important; }
  #content-main { overflow-x: hidden; }
  #header { padding: 8px 12px !important; }
  #branding h1 { font-size: 18px !important; }

  /* Breadcrumbs */
  div.breadcrumbs {
    padding: 6px 10px !important;
    font-size: 12px;
    overflow-x: auto;
    white-space: nowrap;
  }

  /* --- Buttons / touch targets --- */
  .submit-row,
  .submit-row input,
  .submit-row a,
  .button,
  input[type="submit"],
  button,
  a.button {
    min-height: 44px;
    min-width: 44px;
  }

  .submit-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px !important;
    position: sticky;
    bottom: 0;
    z-index: 50;
    background: #f8f8f8;
    border-top: 1px solid #ddd;
  }

  .submit-row input,
  .submit-row a,
  .submit-row .deletelink-box {
    flex: 1 1 calc(50% - 4px);
    min-width: 120px;
    text-align: center;
  }

  /* --- Change-list tables → card layout --- */
  .results table thead,
  table.metrics thead,
  table.user-list-table thead,
  table.metrics-table thead {
    display: none;
  }

  .results table,
  table.metrics,
  table.user-list-table,
  table.metrics-table {
    border: 0;
    width: 100%;
  }

  .results table tr,
  table.metrics tr,
  table.user-list-table tr,
  table.metrics-table tr {
    display: block;
    margin-bottom: 10px;
    border: 1px solid #eee;
    border-radius: 10px;
    background: #fff;
    padding: 10px;
  }

  .results table td,
  table.metrics td,
  table.user-list-table td,
  table.metrics-table td {
    display: block;
    border: none !important;
    padding: 4px 0 !important;
    text-align: left !important;
    word-break: break-word;
    overflow-wrap: break-word;
  }

  .results table td::before,
  table.metrics td::before,
  table.user-list-table td::before,
  table.metrics-table td::before {
    content: attr(data-label) ": ";
    font-weight: 600;
    color: #666;
  }

  /* Checkbox column in changelist */
  .results table td.action-checkbox,
  .results table th.action-checkbox-column {
    display: inline-block;
    width: auto;
    padding: 4px !important;
  }

  /* --- Filter sidebar → horizontal pills --- */
  #changelist-filter {
    position: relative !important;
    width: 100% !important;
    float: none !important;
    margin: 0 0 16px 0 !important;
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 10px;
    background: #f9f9f9;
  }
  #changelist-filter h2 {
    font-size: 14px;
    margin: 0 0 8px;
  }
  #changelist-filter h3 {
    font-size: 12px;
    margin: 8px 0 4px;
    color: #666;
  }
  #changelist-filter ul {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    list-style: none;
    padding: 0;
    margin: 0;
  }
  #changelist-filter li {
    margin: 0;
  }
  #changelist-filter li a {
    display: inline-block;
    padding: 4px 10px;
    font-size: 12px;
    border-radius: 12px;
    background: #eee;
    color: #333;
    text-decoration: none;
  }
  #changelist-filter li.selected a {
    background: #417690;
    color: #fff;
  }

  /* Fix changelist body not to overlap filter */
  #changelist .results,
  #changelist-form .results {
    width: 100% !important;
    margin-right: 0 !important;
  }

  /* --- Actions bar --- */
  .actions {
    padding: 8px !important;
  }
  .actions select {
    max-width: 60vw;
    font-size: 14px;
  }
  .actions label {
    display: block;
    margin-bottom: 4px;
  }

  /* --- Forms --- */
  .form-row {
    display: block !important;
    padding: 8px 0 !important;
  }
  .form-row label {
    display: block !important;
    float: none !important;
    width: 100% !important;
    padding: 0 0 4px 0 !important;
    font-weight: 600;
  }
  .form-row input[type="text"],
  .form-row input[type="number"],
  .form-row input[type="url"],
  .form-row input[type="email"],
  .form-row textarea,
  .form-row select {
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box;
    font-size: 16px;
  }
  .vTextField,
  .vURLField {
    width: 100% !important;
  }

  /* Date/time widgets side by side */
  .datetime .timezonewarning { display: block; font-size: 11px; }
  .datetimeshortcuts { display: block; margin-top: 4px; }

  /* JSONField textarea */
  .jsoneditor,
  textarea.vLargeTextField {
    width: 100% !important;
    min-height: 100px;
    font-size: 13px;
  }

  /* Fieldsets */
  fieldset {
    padding: 10px !important;
    margin: 0 0 12px !important;
  }
  fieldset .description {
    font-size: 12px;
    line-height: 1.4;
  }

  /* --- Inline related objects --- */
  .inline-group .tabular td,
  .inline-group .tabular th {
    display: block !important;
    width: 100% !important;
  }
  .inline-group .tabular fieldset.module {
    padding: 8px !important;
  }

  /* Only the Campaign UTM inline becomes stacked cards on phones. */
  #links-group,
  #links-group .tabular.inline-related {
    overflow: visible;
  }
  #links-group table,
  #links-group tbody,
  #links-group tr,
  #links-group td {
    display: block !important;
    width: 100% !important;
    box-sizing: border-box;
  }
  #links-group thead { display: none !important; }
  #links-group tr.form-row {
    margin: 0 0 14px;
    padding: 10px;
    border: 1px solid #d9e2e8;
    border-radius: 10px;
    background: #fff;
  }
  #links-group td {
    border: 0 !important;
    padding: 5px 0 !important;
  }
  #links-group td[class*="field-"]::before {
    display: block;
    margin-bottom: 3px;
    color: #667;
    font-size: 11px;
    font-weight: 700;
  }
  #links-group .field-label::before { content: "Площадка"; }
  #links-group .field-slug::before { content: "Код ссылки"; }
  #links-group .field-utm_source::before { content: "UTM source"; }
  #links-group .field-utm_medium::before { content: "Тип"; }
  #links-group .field-cost::before { content: "Бюджет, $"; }
  #links-group .field-telegram_destination::before { content: "Telegram-канал"; }
  #links-group input,
  #links-group select,
  #links-group .campaign-link-url {
    width: 100% !important;
    max-width: 100% !important;
  }

  /* --- Dashboard KPI cards --- */
  .kpi-grid { grid-template-columns: 1fr !important; gap: 10px !important; }
  .kpi-card {
    margin-bottom: 8px;
    padding: 12px !important;
  }
  .kpi-value, .metric-value {
    font-size: clamp(18px, 6vw, 28px) !important;
    word-break: break-word;
  }

  /* Period selector / report links */
  .period-selector {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin: 8px 0;
  }
  .period-selector a {
    padding: 8px 12px !important;
    font-size: 13px;
    white-space: nowrap;
  }

  /* Charts */
  .chart-container,
  canvas {
    max-width: 100%;
    overflow-x: auto;
  }
  .revenue-chart-wrap { max-height: 220px; }
  .heatmap-scroll { max-height: 250px; }

  /* --- Sidebar (off-canvas) --- */
  body.admin-sidebar-open #nav-sidebar {
    transform: translateX(0);
  }

  #nav-sidebar {
    position: fixed !important;
    left: 0;
    top: 0;
    height: 100vh;
    width: min(88vw, 320px);
    z-index: 1000;
    background: #fff;
    transform: translateX(-105%);
    transition: transform .2s ease;
    box-shadow: 0 10px 30px rgba(0,0,0,.2);
    overflow-y: auto;
  }

  body.admin-sidebar-open::before {
    content: '';
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.35);
    z-index: 999;
  }

  /* --- Search bar --- */
  #toolbar {
    padding: 8px !important;
  }
  #toolbar form {
    display: flex;
    gap: 6px;
  }
  #toolbar #searchbar {
    flex: 1;
    min-width: 0;
    font-size: 16px;
  }
  #toolbar input[type="submit"] {
    flex-shrink: 0;
  }

  /* --- Pagination --- */
  .paginator {
    font-size: 13px;
    padding: 8px !important;
    text-align: center;
  }

  /* --- Object tools (Add / History buttons) --- */
  .object-tools {
    padding: 0 !important;
    margin: 8px 0 !important;
  }
  .object-tools li {
    margin: 0 4px 4px 0;
  }
  .object-tools a {
    padding: 6px 10px !important;
    font-size: 12px;
  }

  /* --- Related-widget-wrapper --- */
  .related-widget-wrapper {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    align-items: center;
  }
  .related-widget-wrapper select {
    flex: 1;
    min-width: 0;
  }

  /* --- User card template --- */
  .user-card-grid {
    grid-template-columns: 1fr !important;
  }

  /* --- Segmentation dashboard --- */
  .segment-grid {
    grid-template-columns: 1fr !important;
  }
}

/* ============================================================
   TABLET  (768px – 1023px)
   ============================================================ */
@media (min-width: 768px) and (max-width: 1023px) {
  #content { padding: 14px !important; }
  .kpi-grid { grid-template-columns: repeat(2, minmax(180px, 1fr)) !important; }

  #changelist-filter {
    width: 180px !important;
  }
}
