/* =========================================================
   Mobile & responsive overrides for the admin panel
   Breakpoints:
     - ≤1024px : compact desktop / tablet landscape
     - ≤860px  : tablet / mobile-major (sidebar becomes drawer)
     - ≤640px  : phone
     - ≤420px  : tiny phone
   ========================================================= */

/* ---------- Defaults (shared) ---------- */
.tb-burger { display: none; }
.mobile-backdrop { display: none; }
.tb-title { display: flex; flex-direction: column; min-width: 0; }
.tb-title h1 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tb-user-avatar {
  width: 22px; height: 22px; border-radius: 999px;
  background: linear-gradient(135deg, #7C3AED, #EC4899);
  color: #fff; display: grid; place-items: center;
  font-size: 10px; font-weight: 600; flex-shrink: 0;
}

/* ===================================================================
   ≤1024px — tablet landscape: tighten paddings, slightly smaller text
   =================================================================== */
@media (max-width: 1024px) {
  .content { padding: 24px 20px; }
  .topbar { padding: 0 20px; }
  .page-title { font-size: 24px; }

  /* KPI: from 3 cols to 2 cols (already in admin.css at 1100px) */
  .kpi-grid { grid-template-columns: repeat(2, 1fr) !important; }

  /* Two-column dashboard grids -> stack */
  .stagger,
  .grid-2 { gap: 12px; }
}

/* ===================================================================
   ≤860px — sidebar becomes overlay drawer, topbar compact
   =================================================================== */
@media (max-width: 860px) {
  /* ---- App: drop sidebar from grid ---- */
  .app {
    grid-template-columns: 1fr !important;
    min-height: 100vh;
    min-height: 100svh;
  }

  /* ---- Sidebar: slide-in drawer ---- */
  .sidebar {
    position: fixed;
    top: 0; left: 0;
    width: 280px;
    max-width: 86vw;
    height: 100vh;
    height: 100svh;
    z-index: 1100;
    transform: translateX(-100%);
    transition: transform 240ms cubic-bezier(.3, 0, 0, 1), box-shadow 240ms;
    border-right: 1px solid var(--border);
    background: var(--surface);
    box-shadow: none;
  }
  .app.mobile-nav-open .sidebar {
    transform: translateX(0);
    box-shadow: 0 20px 50px rgba(13, 13, 13, .25);
  }

  /* drawer mode forces brand visible even if .collapsed is set */
  .app.collapsed .sb-brand,
  .app.collapsed .sb-section,
  .app.collapsed .sb-item .label,
  .app.collapsed .sb-item .badge-num,
  .app.collapsed .sb-user-meta,
  .app.collapsed .sb-foot button.iconbtn.collapse { opacity: 1; pointer-events: auto; display: revert; height: auto; padding: revert; }
  .sb-foot button.iconbtn.collapse { display: none !important; }

  /* ---- Backdrop ---- */
  .mobile-backdrop {
    display: block;
    position: fixed; inset: 0;
    background: rgba(13, 13, 13, .35);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 200ms ease;
    z-index: 1050;
  }
  .app.mobile-nav-open .mobile-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  /* Lock body scroll while drawer open */
  body.nav-lock { overflow: hidden; }

  /* ---- Topbar: show burger, compact spacing ---- */
  .topbar {
    padding: 0 14px;
    gap: 10px;
    height: 60px;
  }
  .tb-burger { display: inline-grid; }
  .topbar .crumbs { display: none; }
  .topbar h1 { font-size: 16px; font-weight: 600; }

  .tb-right { gap: 4px; }
  /* hide bulky bits, keep user dropdown + logout */
  .status-pill { display: none; }
  .tb-divider { display: none; }
  .topbar .iconbtn[title="Уведомления"] { display: none; }

  .tb-user-btn { height: 34px; padding: 0 6px; gap: 0; }
  .tb-user-btn .tb-user-text,
  .tb-user-btn > svg:last-child { display: none; }

  /* ---- Content: tighten ---- */
  .content { padding: 20px 16px; }
  .page-head { gap: 12px; margin-bottom: 16px; }
  .page-title { font-size: 22px; }
  .page-sub { font-size: 12px; }

  /* Page-head action button rows wrap and grow */
  .page-head > div:last-child {
    flex-wrap: wrap;
    gap: 8px;
  }
  .page-head .pill-group { width: 100%; overflow-x: auto; }

  /* ---- All multi-column dashboard layouts collapse to 1 col ---- */
  .kpi-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 10px; }
  .grid-2,
  .grid-3 { grid-template-columns: 1fr !important; }

  /* Tighten card padding on small screens */
  .card-head { padding: 14px 16px; gap: 10px; }
  .card-body { padding: 16px; }
  .card-head .card-title { font-size: 13px; }

  /* KPI tweaks for stacking */
  .kpi { padding: 14px 16px; }
  .kpi-value { font-size: 24px; }
  .kpi-spark { width: 64px; height: 24px; }

  /* ---- Filters row ---- */
  .filter-row {
    flex-wrap: wrap;
    padding: 12px;
    gap: 8px;
  }
  .filter-row .search { flex: 1 1 100%; min-width: 0; }
  .filter-row > select,
  .filter-row > .btn { flex-shrink: 0; }
  .filter-row .v-div { display: none; }
  .filter-row > div[style*="margin-left:auto"] { width: 100%; }

  /* Chips row scrollable */
  .filter-row .chip { flex-shrink: 0; }

  /* ---- Tables: horizontal scroll with fade hint on right ---- */
  .tbl-wrap {
    position: relative;
    -webkit-overflow-scrolling: touch;
  }
  .tbl-wrap::after {
    content: '';
    position: absolute;
    top: 0; right: 0; bottom: 0;
    width: 24px;
    background: linear-gradient(90deg, transparent, var(--surface));
    pointer-events: none;
    opacity: .7;
  }
  .tbl thead th,
  .tbl tbody td { padding: 12px 12px; }
  .tbl tbody td { white-space: nowrap; }

  /* ---- Tabs scroll if too many ---- */
  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { flex-shrink: 0; }

  /* ---- Wizard progress: compact ---- */
  .wizard-progress {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px 14px;
    gap: 6px;
  }
  .wizard-progress::-webkit-scrollbar { display: none; }
  .wiz-step .label { display: none; }
  .wiz-step .num { width: 26px; height: 26px; }

  /* ---- Pagination ---- */
  .pagination {
    padding: 12px;
    flex-wrap: wrap;
    gap: 6px;
  }
  .pagination .meta { width: 100%; margin-right: 0; order: -1; }

  /* ---- Modal ---- */
  .modal-bd { padding: 12px; }
  .modal {
    max-width: 100% !important;
    max-height: calc(100vh - 24px);
    border-radius: 14px;
  }
  .modal-head { padding: 18px 18px 4px; }
  .modal-title { font-size: 15px; }
  .modal-desc { font-size: 12px; }
  .modal-body { padding: 10px 18px 4px; }
  .modal-foot {
    padding: 14px 18px 18px;
    flex-direction: column-reverse;
    gap: 8px;
  }
  .modal-foot .btn { width: 100%; }

  /* ---- Charts: ensure they fit, lower height ---- */
  .chart-host { height: 200px !important; }
  .chart-host svg { height: 200px !important; }

  /* ---- Buttons wrap nicely ---- */
  .btn { white-space: nowrap; }
  .page-head .btn.lg,
  .page-head .btn { flex-shrink: 0; }

  /* ---- Toast: full width with margin ---- */
  .toast-host {
    bottom: 16px;
    right: 16px;
    left: 16px;
  }
  .toast {
    max-width: none;
    min-width: 0;
  }

  /* ---- Push preview: prevent overflow ---- */
  .push-preview { width: 100% !important; max-width: 320px; }

  /* ---- User detail header: stack ---- */
  .user-cell .meta { white-space: normal; }

  /* JSON viewer: limit width */
  .json { font-size: 11px; }
}

/* ===================================================================
   ≤640px — phone: 1-col KPI, tighter typography
   =================================================================== */
@media (max-width: 640px) {
  .content { padding: 16px 12px; }
  .topbar { padding: 0 12px; }
  .page-head { margin-bottom: 14px; }
  .page-title { font-size: 20px; }

  /* KPI to single column */
  .kpi-grid { grid-template-columns: 1fr !important; }
  .kpi { padding: 14px 16px; }
  .kpi-value { font-size: 26px; }
  .kpi-spark { width: 72px; height: 26px; }

  /* Page head actions wrap and span full width */
  .page-head > div:last-child > .btn { flex: 1 1 auto; }
  .page-head .pill-group { padding: 3px; }
  .page-head .pill-group .chip { height: 28px; padding: 0 10px; font-size: 12px; }

  /* Filter row */
  .filter-row { padding: 10px; gap: 6px; }
  .filter-row .chip { font-size: 11px; height: 28px; padding: 0 10px; }

  /* Cards */
  .card-head { padding: 12px 14px; }
  .card-body { padding: 14px; }

  /* Stat cards in campaign detail */
  .card-body[style*="grid-template-columns:repeat(3,1fr)"],
  .card-body[style*="grid-template-columns:repeat(3, 1fr)"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Wizard choice cards 4-col -> 2-col */
  [data-choice-group][style*="grid-template-columns:repeat(4,1fr)"],
  [data-choice-group][style*="grid-template-columns:repeat(4, 1fr)"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Wizard step content split (1.4fr 1fr) -> 1 col */
  [data-step][style*="grid-template-columns"],
  [data-step] > div[style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
  }

  /* Notification wizard content / preview split: ensure stacked */
  [data-step="0"] > div[style*="grid-template-columns:1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }
  [data-step="0"] > div[style*="grid-template-columns:1fr 1fr"] > div:last-child {
    border-left: none !important;
    border-top: 1px solid var(--border);
  }

  /* User detail header inside card */
  .card > div[style*="display:flex"][style*="gap:18px"][style*="padding:20px"] {
    flex-wrap: wrap;
    gap: 12px !important;
    padding: 16px !important;
  }
  .card > div[style*="display:flex"][style*="gap:18px"] > div:last-child {
    width: 100%;
  }

  /* Force tight padding on inline grid layouts */
  div[style*="grid-template-columns:1fr 1fr"][style*="gap:14px"],
  div[style*="grid-template-columns:1fr 1fr"][style*="gap:16px"],
  div[style*="grid-template-columns:1.4fr 1fr"] {
    grid-template-columns: 1fr !important;
  }

  /* Topbar tighter */
  .topbar { height: 56px; gap: 8px; }
  .topbar h1 { font-size: 15px; }
  .tb-burger { width: 34px; height: 34px; }

  /* Login: stack and hide art */
  .login-form-side { padding: 32px 20px !important; }
  .login-form-side h1 { font-size: 26px !important; }

  /* QR block: stack and center */
  .qr-block {
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 14px;
  }
  .qr-block > div[style*="flex:1"] { width: 100%; }

  /* Push preview: smaller */
  .push-preview { width: 100% !important; max-width: 290px; }

  /* Modal foot button gap */
  .modal-foot { gap: 6px; }

  /* Tables: a touch smaller */
  .tbl { font-size: 12px; }
  .tbl thead th { font-size: 10px; padding: 10px 12px; }
  .tbl tbody td { padding: 12px; }

  /* Wizard nav footer stacks vertically */
  [data-wizard] > div[style*="justify-content:space-between"][style*="padding:18px 24px"] {
    flex-direction: column-reverse;
    gap: 8px;
    padding: 14px 16px !important;
  }
  [data-wizard] > div[style*="justify-content:space-between"] .btn { width: 100%; }

  /* Toggle slightly larger for touch */
  .toggle { width: 40px; height: 22px; }
  .toggle::after { width: 18px; height: 18px; }
  .toggle.on::after { left: 20px; }

  /* Increase tap target for icon-only buttons */
  .iconbtn { width: 36px; height: 36px; }

  /* Page-sub line height */
  .page-sub { line-height: 1.4; }
}

/* ===================================================================
   ≤420px — very small phones: edge-to-edge cards
   =================================================================== */
@media (max-width: 420px) {
  .content { padding: 14px 10px; }
  .card { border-radius: 10px; }
  .page-title { font-size: 19px; }

  /* Hide quick-period button labels except active state in some places */
  .pill-group { width: 100%; }

  /* Page-head action buttons: full-width column */
  .page-head > div:last-child {
    width: 100%;
    flex-direction: column;
  }
  .page-head > div:last-child > .btn,
  .page-head > div:last-child > a.btn {
    width: 100%;
    justify-content: center;
  }

  /* Filter chips wrap multi-row */
  .filter-row > .chip { padding: 0 8px; height: 26px; font-size: 11px; }

  /* Push preview compact */
  .push-preview { padding: 12px; max-width: 280px; }

  /* User detail header avatar size */
  .card > div[style*="padding:20px"] > div.avatar[style*="width:56px"] {
    width: 48px !important;
    height: 48px !important;
    font-size: 18px !important;
  }
  .card h1[style*="font-size:22px"] { font-size: 18px !important; }

  /* Login art is already hidden at 900px in admin.css */
  .login-shell { grid-template-columns: 1fr; }
  .login-form-side { padding: 28px 16px !important; }
  .login-form-side h1 { font-size: 24px !important; line-height: 1.15 !important; }
}

/* ===================================================================
   Touch-device polish: remove hover lift on phones, use :active feedback
   =================================================================== */
@media (hover: none) and (pointer: coarse) {
  .kpi:hover { transform: none; box-shadow: none; }
  .card.hoverable:hover { box-shadow: none; }
  .sb-item:active { background: var(--bg-muted); }
  .chip:active { transform: scale(.98); }
  .btn:active { transform: scale(.98); }
}

/* ===================================================================
   Landscape phone (low height): keep usable
   =================================================================== */
@media (max-height: 480px) and (orientation: landscape) {
  .topbar { height: 52px; }
  .content { padding: 14px 16px; }
}

/* ===================================================================
   Notch / safe area
   =================================================================== */
@supports (padding: max(0px)) {
  .sidebar { padding-top: env(safe-area-inset-top, 0); }
  .topbar { padding-left: max(env(safe-area-inset-left, 0), 14px); padding-right: max(env(safe-area-inset-right, 0), 14px); }
  .content { padding-bottom: max(env(safe-area-inset-bottom, 0), 16px); }
  .toast-host {
    bottom: max(env(safe-area-inset-bottom, 0), 16px);
    left: max(env(safe-area-inset-left, 0), 16px);
    right: max(env(safe-area-inset-right, 0), 16px);
  }
}
