/* Responsive — tablet & smartphone
   Key invariant: the app is a CSS grid with template-rows `44px 1fr` (topbar, content).
   On mobile we switch topbar grid-area and overlay the tabbar as fixed bottom nav. */

/* ───────── TABLET ≤ 1024px ───────── */
@media (max-width: 1024px) {
  .app { grid-template-columns: 60px 1fr; }
  .sidebar { width: 60px; }
  .sidebar .scope .meta,
  .sidebar .nav-item .label,
  .sidebar .nav-label,
  .sidebar .compliance { display: none; }
  .sidebar .nav-item {
    justify-content: center;
    padding: 9px 8px;
    position: relative;
  }
  .sidebar .nav-item .badge {
    position: absolute; top: 2px; right: 6px;
    font-size: 9px; min-width: 14px; height: 14px;
    padding: 0 3px;
  }
  .sidebar .scope { padding: 10px 8px; }
  .sidebar .scope-chip { justify-content: center; padding: 6px; }
  .sidebar .scope-chip > div:not(.scope-mark) { display: none; }

  .topbar .crumb { display: none; }
  .topbar .search { width: 160px; }

  .split { grid-template-columns: 1fr minmax(380px, 0.9fr); }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ───────── MOBILE ≤ 720px ───────── */
@media (max-width: 720px) {
  html, body { height: 100%; overscroll-behavior: none; }

  /* App grid: collapse sidebar column; reserve space for bottom tabbar via padding */
  .app {
    grid-template-columns: 1fr !important;
    grid-template-rows: 52px 1fr;
    height: 100dvh;
  }

  /* ── Topbar ── */
  .topbar {
    height: 52px;
    padding: 0 8px;
    gap: 2px;
  }
  .topbar .brand { font-size: 15px; padding: 4px 4px; flex-shrink: 0; }
  .topbar .brand > span:not(.brand-mark) { display: none; }
  .topbar .crumb,
  .topbar .sep,
  .topbar > .pill,
  .topbar .spacer { display: none !important; }
  .topbar > .tb-item:first-of-type {
    /* Portal switcher — icon only */
    padding: 0 6px;
  }
  .topbar > .tb-item:first-of-type > :not(svg):not(.icon) { display: none; }
  .topbar .search {
    flex: 0 0 36px;
    width: 36px;
    height: 36px;
    padding: 0;
    display: grid; place-items: center;
    font-size: 0;
    margin-left: auto;
  }
  .topbar .search kbd,
  .admin-top .search kbd,
  .search kbd { display: none; }
  .topbar .search svg { font-size: 14px; }
  .topbar .tb-item.icon { width: 36px; height: 36px; }
  /* User chip — avatar only on mobile */
  .topbar > div[style*="position: relative"] > button {
    padding: 4px !important;
    gap: 0 !important;
  }
  .topbar > div[style*="position: relative"] > button > div,
  .topbar > div[style*="position: relative"] > button > svg:last-child { display: none !important; }

  /* ── Sidebar → off-canvas drawer ── */
  .sidebar {
    position: fixed !important;
    top: 52px; bottom: 64px; left: 0;
    width: min(86vw, 320px) !important;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform .24s cubic-bezier(.2,.8,.2,1);
    box-shadow: 6px 0 24px oklch(0% 0 0 / 0.25);
    border-right: 1px solid var(--border);
    background: var(--bg-raised);
    overflow-y: auto;
  }
  body.drawer-open .sidebar { transform: translateX(0); }
  body.drawer-open::after {
    content: "";
    position: fixed; inset: 52px 0 64px 0;
    background: oklch(0% 0 0 / 0.4);
    z-index: 35;
  }

  /* Restore labels in drawer */
  .sidebar .scope .meta,
  .sidebar .nav-item .label,
  .sidebar .nav-label,
  .sidebar .compliance { display: initial !important; }
  .sidebar .nav-item {
    justify-content: flex-start !important;
    padding: 11px 14px !important;
    font-size: 14px;
  }
  .sidebar .nav-item .badge {
    position: static !important;
    font-size: 10px;
    height: 16px; min-width: 16px;
  }
  .sidebar .scope { padding: 14px 16px !important; }
  .sidebar .scope-chip {
    justify-content: flex-start !important;
    padding: 10px !important;
  }
  .sidebar .scope-chip > div:not(.scope-mark) { display: block !important; }

  /* ── Content ── */
  .content {
    padding-bottom: 72px; /* clear bottom tabbar */
    -webkit-overflow-scrolling: touch;
    min-width: 0;
  }
  .content-head {
    padding: 14px 16px 10px;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .content-head h1 { font-size: 22px; line-height: 1.2; }
  .content-head .sub { font-size: 12px; }
  .content-head .right {
    display: flex; flex-wrap: wrap; gap: 6px;
    width: 100%;
  }
  .content-head .right .btn {
    padding: 0 12px; height: 36px; font-size: 13px;
    flex: 1 1 auto;
    min-width: 0;
    white-space: nowrap;
  }
  .content-head .right .btn.primary { flex: 1 1 100%; }

  /* ── KPI cards ── */
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px;
    padding: 10px 12px !important;
  }
  .kpi-card { padding: 12px !important; }
  .kpi-card .value { font-size: 22px !important; }
  .kpi-card .spark { display: none !important; }
  .kpi-card .sub { font-size: 11px; }

  /* ── Tables → horizontal scroll, min-width ── */
  .split { grid-template-columns: 1fr !important; overflow: visible; }
  .split > * + * {
    border-left: 0 !important;
    border-top: 1px solid var(--border);
  }
  .master, .detail {
    max-height: none !important;
    overflow: visible !important;
  }
  .master .table,
  .table {
    min-width: 560px;
    font-size: 12.5px;
  }
  .master {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }
  table.table th, table.table td {
    padding: 10px 10px !important;
    white-space: nowrap;
  }

  /* Tabs strip: horizontal scroll */
  .tabs {
    overflow-x: auto;
    flex-wrap: nowrap !important;
    padding: 0 12px !important;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs .tab { flex-shrink: 0; }

  /* Toolbar */
  .toolbar {
    padding: 8px 12px !important;
    gap: 6px !important;
    flex-wrap: wrap;
  }
  .toolbar .search-inp,
  .toolbar input[type="search"] {
    min-width: 120px; flex: 1;
  }

  /* Cards & panels */
  .card { border-radius: 10px; }
  .card-body { padding: 12px !important; }
  .card-head {
    padding: 10px 12px !important;
    font-size: 12px !important;
  }

  /* Detail pane */
  .detail-head { padding: 14px 16px !important; }
  .detail-head h2 { font-size: 18px !important; }
  .detail-section { padding: 14px 16px !important; }
  .kv {
    grid-template-columns: 100px 1fr !important;
    font-size: 13px !important;
  }

  /* ── Bottom tab bar ── */
  .mobile-tabbar {
    position: fixed; bottom: 0; left: 0; right: 0;
    height: 64px;
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--bg-raised);
    border-top: 1px solid var(--border);
    display: flex;
    z-index: 30;
    box-shadow: 0 -4px 14px oklch(0% 0 0 / 0.06);
  }
  .mobile-tabbar .tab-btn {
    flex: 1;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 3px;
    background: transparent; border: 0;
    color: var(--fg-muted);
    font-size: 10.5px; font-weight: 500;
    cursor: pointer; position: relative;
    padding: 8px 2px 6px;
  }
  .mobile-tabbar .tab-btn.active { color: var(--accent); }
  .mobile-tabbar .tab-btn.active::before {
    content: "";
    position: absolute; top: 0;
    left: 30%; right: 30%;
    height: 2px;
    background: var(--accent);
    border-radius: 0 0 3px 3px;
  }
  .mobile-tabbar .tab-btn .badge {
    position: absolute;
    top: 4px; left: 54%;
    background: var(--danger); color: white;
    font-size: 9px; font-weight: 600;
    min-width: 15px; height: 15px;
    border-radius: 8px; padding: 0 4px;
    display: grid; place-items: center;
    line-height: 1;
  }
  .mobile-tabbar .mic-tab {
    width: 54px; height: 54px;
    margin: 4px 6px 0;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--accent), var(--accent-hover));
    color: var(--accent-fg);
    box-shadow: 0 6px 16px oklch(60% 0.15 45 / 0.35);
    display: grid; place-items: center;
    border: 0; cursor: pointer;
    align-self: center;
    flex-shrink: 0;
  }
  .mobile-tabbar .mic-tab:active { transform: scale(0.96); }

  /* Desktop-only chrome to hide */
  .voice-fab { display: none !important; }
  .hide-mobile { display: none !important; }

  /* ── Voice sheet → fullscreen ── */
  .voice-backdrop { padding: 0 !important; }
  .voice-sheet {
    width: 100% !important;
    height: 100dvh !important;
    max-height: none !important;
    border-radius: 0 !important;
  }
  .voice-meta { grid-template-columns: 1fr !important; }
  .mic-big { width: 96px !important; height: 96px !important; }
  .wave { height: 72px !important; }
  .rec-time { font-size: 22px !important; }

  /* Modals → fullscreen */
  .modal, .sheet, .na-sheet, .na-backdrop > .na-sheet {
    width: 100% !important;
    max-width: 100% !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    border-radius: 0 !important;
    margin: 0 !important;
  }

  /* Login */
  .auth { grid-template-columns: 1fr !important; }
  .auth-art { display: none !important; }
  .auth-form {
    padding: 28px 20px !important;
    max-width: 100% !important;
  }
  .role-grid { grid-template-columns: 1fr !important; }

  /* Tweaks panel anchors to bottom */
  .tweaks-panel {
    left: 10px !important; right: 10px !important;
    bottom: 78px !important;
    top: auto !important;
    width: auto !important;
    max-width: none !important;
  }

  /* Menus → bottom sheets */
  .menu {
    position: fixed !important;
    left: 10px !important; right: 10px !important;
    bottom: 78px !important;
    top: auto !important;
    max-width: none !important;
    min-width: 0 !important;
    border-radius: 12px !important;
  }

  /* Admin console */
  .admin-app { grid-template-rows: 48px 1fr; }
  .admin-top { height: 48px; padding: 0 10px; }
  .admin-top .sep, .admin-top .badge { display: none; }
  .admin-body { grid-template-columns: 1fr !important; }
  .admin-nav { display: none; }
  .danger-banner { font-size: 11px; padding: 8px 12px; }

  /* Charts: constrain min height */
  svg.chart, .chart svg { max-width: 100%; height: auto; }
}

/* ───────── Very narrow (< 400) ───────── */
@media (max-width: 400px) {
  .kpi-grid { grid-template-columns: 1fr !important; }
  .topbar .brand > span:not(.brand-mark) { display: none; }
  .mobile-tabbar .tab-btn .lbl { font-size: 9.5px; }
  .content-head h1 { font-size: 20px; }
}
