/* Авто-адаптация панелей под мобильные (data-viewport на <html>) */
html {
  -webkit-text-size-adjust: 100%;
}
body {
  -webkit-tap-highlight-color: transparent;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.mobile-hint {
  display: none;
  margin-top: 14px;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 0.85rem;
}
.mobile-hint a { text-decoration: underline; }

/* Sites */
[data-viewport="mobile"] .mobile-hint,
.panel-mobile .mobile-hint {
  display: block;
  background: rgba(59, 130, 246, 0.12);
  border: 1px solid rgba(59, 130, 246, 0.25);
  color: #93c5fd;
}
/* Bots */
.panel-bots [data-viewport="mobile"] .mobile-hint,
.panel-bots.panel-mobile .mobile-hint {
  background: rgba(31, 107, 235, 0.12);
  border: 1px solid rgba(31, 107, 235, 0.25);
  color: #79c0ff;
}

[data-viewport="mobile"] .wrap,
.panel-mobile .wrap {
  padding: 12px 12px calc(28px + env(safe-area-inset-bottom));
}
[data-viewport="mobile"] .card,
.panel-mobile .card {
  padding: 16px;
}
[data-viewport="mobile"] h1,
.panel-mobile h1 {
  font-size: 1.3rem;
}
[data-viewport="mobile"] h2,
.panel-mobile h2 {
  font-size: 1rem;
}
[data-viewport="mobile"] .login-box,
.panel-mobile .login-box {
  margin: 4vh auto 0;
  max-width: 100%;
}
[data-viewport="mobile"] input,
[data-viewport="mobile"] select,
[data-viewport="mobile"] textarea,
[data-viewport="mobile"] .btn,
.panel-mobile input,
.panel-mobile select,
.panel-mobile textarea,
.panel-mobile .btn {
  font-size: 16px;
}
[data-viewport="mobile"] .btn,
.panel-mobile .btn {
  min-height: 44px;
  padding: 11px 14px;
}
[data-viewport="mobile"] .btn-sm,
.panel-mobile .btn-sm {
  min-height: 40px;
  font-size: 0.84rem;
}

[data-viewport="mobile"] .header,
.panel-mobile .header {
  flex-direction: column;
  align-items: stretch;
  gap: 12px;
}
[data-viewport="mobile"] .header-actions,
[data-viewport="mobile"] .toolbar,
[data-viewport="mobile"] .row-actions,
[data-viewport="mobile"] .detail-actions,
[data-viewport="mobile"] .section-toolbar .actions,
[data-viewport="mobile"] .modal-header .actions,
.panel-mobile .header-actions,
.panel-mobile .toolbar,
.panel-mobile .row-actions,
.panel-mobile .detail-actions,
.panel-mobile .section-toolbar .actions,
.panel-mobile .modal-header .actions,
[data-auto-stack="1"] {
  display: grid !important;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  width: 100%;
}
[data-viewport="mobile"] .header-actions .btn,
[data-viewport="mobile"] .toolbar .btn,
[data-viewport="mobile"] .row-actions .btn,
[data-viewport="mobile"] .detail-actions .btn,
[data-viewport="mobile"] .section-toolbar .actions .btn,
[data-viewport="mobile"] .modal-header .actions .btn,
.panel-mobile .header-actions .btn,
.panel-mobile .toolbar .btn,
.panel-mobile .row-actions .btn,
.panel-mobile .detail-actions .btn,
.panel-mobile .section-toolbar .actions .btn,
.panel-mobile .modal-header .actions .btn {
  width: 100%;
}
[data-viewport="mobile"] .toolbar,
.panel-mobile .toolbar {
  padding: 10px;
}
[data-viewport="mobile"] .toolbar .btn,
.panel-mobile .toolbar .btn {
  font-size: 0.82rem;
  padding: 10px 8px;
  white-space: normal;
  line-height: 1.25;
}

[data-viewport="mobile"] .stats,
.panel-mobile .stats {
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
[data-viewport="mobile"] .stat,
.panel-mobile .stat {
  padding: 12px;
}
[data-viewport="mobile"] .stat-value,
.panel-mobile .stat-value {
  font-size: 0.92rem;
}
[data-viewport="mobile"] .grid-2,
.panel-mobile .grid-2 {
  grid-template-columns: 1fr;
  gap: 14px;
}
[data-viewport="mobile"] .layout,
.panel-mobile .layout {
  grid-template-columns: 1fr;
}
[data-viewport="mobile"] .top-nav,
.panel-mobile .top-nav {
  width: 100%;
}
[data-viewport="mobile"] .top-nav a,
.panel-mobile .top-nav a {
  flex: 1;
  text-align: center;
  padding: 12px 10px;
  min-height: 44px;
}
[data-viewport="mobile"] table,
.panel-mobile table {
  font-size: 0.82rem;
  min-width: 520px;
}
[data-viewport="mobile"] th,
[data-viewport="mobile"] td,
.panel-mobile th,
.panel-mobile td {
  padding: 10px 8px;
}
[data-viewport="mobile"] .bot-list,
.panel-mobile .bot-list {
  max-height: 40vh;
}
[data-viewport="mobile"] .bot-item,
.panel-mobile .bot-item {
  min-height: 44px;
  padding: 12px;
}
[data-viewport="mobile"] .log-main,
.panel-mobile .log-main {
  height: 200px;
}
[data-viewport="mobile"] .log-global,
.panel-mobile .log-global {
  height: 80px;
}

[data-viewport="mobile"] .modal,
.panel-mobile .modal {
  padding: 0;
  align-items: stretch;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
[data-viewport="mobile"] .modal-card,
.panel-mobile .modal-card {
  width: 100%;
  max-height: none;
  min-height: 100%;
  height: auto;
  border-radius: 0;
  overflow-y: visible;
}
[data-viewport="mobile"] .modal-header,
.panel-mobile .modal-header {
  padding: 14px 16px;
  flex-direction: column;
  align-items: stretch;
  position: static !important;
  top: auto !important;
}
[data-viewport="mobile"] .modal-body,
.panel-mobile .modal-body {
  padding: 14px 16px calc(20px + env(safe-area-inset-bottom));
  overflow: visible;
}
[data-viewport="mobile"] .scroll-top-btn,
.panel-mobile .scroll-top-btn {
  right: 14px;
  bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  width: 46px;
  height: 46px;
}
[data-viewport="mobile"] .toast,
.panel-mobile .toast {
  left: 12px;
  right: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  max-width: none;
}
[data-viewport="mobile"] .section-toolbar,
.panel-mobile .section-toolbar {
  flex-direction: column;
  align-items: stretch;
}
[data-viewport="mobile"] .toggle-row,
.panel-mobile .toggle-row {
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
}
[data-viewport="mobile"] .overlay-card,
.panel-mobile .overlay-card {
  margin: 16px;
  width: calc(100% - 32px);
}

[data-viewport="mobile"][data-touch="1"] .stats,
.panel-mobile.panel-touch .stats {
  grid-template-columns: 1fr 1fr;
}
html[data-viewport="mobile"] .toolbar,
html.panel-mobile .toolbar {
  grid-template-columns: 1fr 1fr;
}

@media (max-width: 420px) {
  [data-viewport="mobile"] .stats,
  .panel-mobile .stats {
    grid-template-columns: 1fr;
  }
  [data-viewport="mobile"] .toolbar,
  [data-viewport="mobile"] .header-actions,
  [data-viewport="mobile"] .detail-actions,
  .panel-mobile .toolbar,
  .panel-mobile .header-actions,
  .panel-mobile .detail-actions,
  [data-auto-stack="1"] {
    grid-template-columns: 1fr !important;
  }
}

/* Tablet */
[data-viewport="tablet"] .wrap,
.panel-tablet .wrap {
  padding: 16px 16px 32px;
}
[data-viewport="tablet"] .grid-2,
.panel-tablet .grid-2 {
  grid-template-columns: 1fr;
}
[data-viewport="tablet"] .layout,
.panel-tablet .layout {
  grid-template-columns: 1fr;
}

/* Fallback без JS (чистый CSS для телефонов) */
@media (max-width: 768px) {
  .wrap {
    padding: 12px 12px 32px;
    max-width: 100%;
  }
  .card {
    padding: 16px;
    border-radius: 10px;
  }
  h1 { font-size: 1.25rem; }
  h2 { font-size: 1rem; }
  .login-box {
    margin: 4vh auto 0;
    max-width: 100%;
  }
  input, select, textarea, .btn {
    font-size: 16px;
    min-height: 44px;
  }
  .btn-sm {
    min-height: 40px;
    padding: 8px 12px;
  }
  .header {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }
  .header-actions,
  .toolbar,
  .row-actions,
  .detail-actions,
  .section-toolbar .actions,
  .modal-header .actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    width: 100%;
  }
  .header-actions .btn,
  .toolbar .btn,
  .row-actions .btn,
  .detail-actions .btn {
    width: 100%;
    justify-content: center;
  }
  .toolbar {
    grid-template-columns: 1fr 1fr;
  }
  .stats {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }
  .grid-2,
  .layout {
    grid-template-columns: 1fr !important;
  }
  .top-nav {
    flex-wrap: wrap;
    gap: 8px;
  }
  .top-nav a {
    flex: 1 1 auto;
    text-align: center;
    padding: 10px 12px;
  }
  table { font-size: 0.82rem; }
  th, td { padding: 10px 8px; }
  .toast {
    left: 12px;
    right: 12px;
    bottom: 12px;
    max-width: none;
  }
  .modal-card {
    width: calc(100vw - 24px);
    max-height: 90vh;
    margin: 12px;
  }
  .form-row {
    grid-template-columns: 1fr !important;
  }
  body {
    overflow-x: hidden;
    touch-action: manipulation;
  }
}

@media (max-width: 420px) {
  .stats,
  .toolbar,
  .header-actions,
  .detail-actions,
  [data-auto-stack="1"] {
    grid-template-columns: 1fr !important;
  }
}

.hidden-mobile { display: none !important; }

.api-chip {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  padding: 2px 8px;
  border-radius: 999px;
  vertical-align: middle;
  border: 1px solid transparent;
}
.api-chip.checking {
  color: #cbd5e1;
  background: rgba(148, 163, 184, 0.15);
  border-color: rgba(148, 163, 184, 0.3);
}
.api-chip.online {
  color: #86efac;
  background: rgba(34, 197, 94, 0.14);
  border-color: rgba(34, 197, 94, 0.35);
}
.api-chip.offline {
  color: #fca5a5;
  background: rgba(239, 68, 68, 0.14);
  border-color: rgba(239, 68, 68, 0.35);
}

.emp-cards {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.emp-card {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  background: var(--surface2, #243044);
  border: 1px solid var(--border, #2d3a4f);
  border-radius: 12px;
  padding: 12px 14px;
}
.emp-card-check {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding-top: 2px;
  cursor: pointer;
}
.emp-card-body {
  flex: 1;
  min-width: 0;
}
.emp-card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
  font-size: 0.85rem;
}
.emp-card-online {
  margin-top: 6px;
  font-size: 0.86rem;
  color: var(--muted, #94a3b8);
}

.links-cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.link-card {
  background: var(--surface2, #243044);
  border: 1px solid var(--border, #2d3a4f);
  border-radius: 12px;
  padding: 14px;
  cursor: pointer;
}
.link-card.selected {
  border-color: rgba(59, 130, 246, 0.55);
  box-shadow: 0 0 0 1px rgba(59, 130, 246, 0.25);
}
.link-card-head {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 8px;
}
.link-card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}
.link-card-url {
  display: block;
  font-size: 0.82rem;
  word-break: break-all;
  margin-bottom: 10px;
}
.link-card .row-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

@media (max-width: 768px) {
  /* Меню, toolbar, side-nav, шапки модалок — без sticky/fixed */
  .side-nav,
  .toolbar,
  .top-nav,
  .header,
  .section-toolbar,
  .modal-header {
    position: static !important;
    top: auto !important;
  }
  .toolbar {
    padding: 10px 0;
    margin: -8px 0 12px;
  }
  .top-nav {
    padding-bottom: 8px;
  }
  .side-nav {
    margin-bottom: 12px;
  }
  #loginError:not(.hidden) {
    padding: 10px;
    border-radius: 8px;
    background: rgba(239, 68, 68, 0.12);
  }
}