/*
 * SUPERAPP responsive rules (navy + maroon refresh)
 *
 *   >= 1200px  desktop   sidebar fixed, 4-column KPI strips, 2-column panels
 *   992-1199   laptop    same layout, tighter spacing
 *   768-991    tablet    sidebar becomes a drawer, KPI 2 columns
 *   < 768      phone     navy header, bottom tab bar, cards, tables as lists
 */

@media (max-width: 1199.98px) {
  .app-main { padding-left: 1.1rem; padding-right: 1.1rem; }
  .app-topbar { padding: 0 1.1rem; }
}

/* tablet and below: sidebar becomes an off-canvas drawer */
@media (max-width: 991.98px) {
  .app-sidebar {
    transform: translateX(-100%);
    transition: transform 0.14s ease-out;
    width: 248px;
  }
  body.sidebar-open .app-sidebar { transform: translateX(0); }
  body.sidebar-open .sidebar-scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(11, 21, 48, 0.55);
    z-index: 1035;
  }
  .app-topbar { left: 0; }
  .app-main { margin-left: 0; }
  body.is-loading .app-main::before, body.is-loading .app-main::after { left: 50%; }
  .menu-btn { display: inline-flex; }

  .kpi-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.75rem; }
  .kpi-value { font-size: 1.35rem; }
  .kpi-value.is-primary { font-size: 1.45rem; }
  .page-tools { width: 100%; }
}

/* phone */
@media (max-width: 575.98px) {
  body { font-size: 0.875rem; }
  .app-main { padding: calc(var(--topbar-h) + 1rem) 0.85rem 1.5rem; }
  .app-topbar { padding: 0 0.6rem; gap: 0.4rem; }
  .user-meta { display: none; }
  .user-btn { padding-right: 0.25rem; }
  .topbar-title { font-size: 0.9rem; }

  .page-title { font-size: 1.3rem; }
  .page-head { margin-bottom: 1rem; }

  .kpi-strip { gap: 0.6rem; }
  .kpi { padding: 0.85rem 0.9rem; }
  .kpi-value { font-size: 1.1rem; }
  .kpi-value.is-primary { font-size: 1.5rem; }
  .kpi-label { font-size: 0.7rem; }

  .panel-head { flex-wrap: wrap; padding: 0.8rem 0.95rem; }
  .panel-body { padding: 0.95rem; }
  .data-table th, .data-table td { padding: 0.55rem 0.75rem; }

  .auth-wrap { padding: 0.75rem; align-items: flex-start; padding-top: 12vh; }
}

@media print {
  .app-sidebar, .app-topbar, .sidebar-scrim { display: none !important; }
  .app-main { margin: 0; padding: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .app-sidebar { transition: none; }
  body.is-loading .app-main::before { animation: none; }
}

/* ------------------------------------------------------------------
 * Phone refinements (Phase 3)
 * Goals: thumb-sized targets, no iOS zoom on focus, tables that read as
 * lists, dialogs that open from the bottom, target figure on its own row.
 * ------------------------------------------------------------------ */
@media (max-width: 575.98px) {
  /* touch targets */
  .btn { min-height: 42px; }
  .btn-sm { min-height: 36px; }
  .icon-btn { width: 42px; height: 42px; }
  .icon-btn .icon { width: 20px; height: 20px; }
  .side-link { padding: 0.72rem 1.1rem; font-size: 0.9375rem; }
  .app-sidebar { width: min(84vw, 300px); }
  .dropdown-item { padding: 0.65rem 1rem; }

  /* 16px inputs: iOS Safari zooms the page on focus below this size */
  .form-control, .form-select, .form-control-sm, .form-select-sm {
    font-size: 16px;
    min-height: 44px;
  }
  .page-tools { gap: 0.5rem; }
  .page-tools { flex-wrap: nowrap; }
  .page-tools .form-control { flex: 1 1 0; min-width: 0; }
  .page-tools .form-select { flex: 1 1 0; min-width: 0; }
  .page-tools .btn { flex: 0 0 auto; }

  .app-main { padding-bottom: calc(1.5rem + env(safe-area-inset-bottom)); }
  .panel { border-radius: 14px; }
  .kpi { border-radius: 14px; }

  /* realisation card on a phone */
  .real-card { padding: 1.2rem 1.1rem 1rem; border-radius: 18px; }
  .real-card::after { right: -80px; top: -80px; width: 190px; height: 190px; }
  .real-main { min-width: 0; }
  .real-amount { font-size: clamp(1.6rem, 8vw, 2rem); }
  .real-percent { font-size: 1.05rem; padding: 0.2rem 0.65rem; flex: none; }
  .real-stats { grid-template-columns: 1fr 1fr; }
  .real-stats > :last-child { grid-column: 1 / -1; }
  .section-title span { display: block; margin: 0.15rem 0 0; }
  .kpi-value.kpi-text { font-size: 1rem; }

  /* tables become lists: one block per row, "label: value" lines */
  .table-stack thead { display: none; }
  .table-stack, .table-stack tbody, .table-stack tr, .table-stack td { display: block; width: 100%; }
  .table-stack tbody tr { padding: 0.85rem 0.95rem; border-bottom: 1px solid var(--c-line-soft); }
  .table-stack tbody tr:last-child { border-bottom: 0; }
  .table-stack tbody tr:hover td { background: transparent; }
  .table-stack td {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.2rem 0;
    border: 0;
    text-align: right;
    white-space: normal;
  }
  .table-stack td::before {
    content: attr(data-label);
    flex: none;
    color: var(--c-muted);
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.03em;
    text-transform: uppercase;
    text-align: left;
  }
  .table-stack td:first-child {
    justify-content: flex-start;
    text-align: left;
    font-weight: 650;
    font-size: 0.9375rem;
    padding-bottom: 0.3rem;
  }
  .table-stack td:first-child::before { display: none; }
  .table-stack td.num { text-align: right; }
  .table-stack td .share-bar { margin-left: auto; }
  .table-stack td.actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 0.4rem;
    padding-top: 0.6rem;
  }
  .table-stack td.actions::before { display: none; }
  .table-stack td.no-actions { display: none; }
  .table-stack td.actions .btn { margin: 0; flex: 1 1 auto; }
  .table-stack .empty-row { padding: 1.4rem 1rem; }
  .table-stack .empty-row td { display: block; text-align: center; padding: 0; }
  .table-stack .empty-row td::before { display: none; }
  .table-stack .cell-name { min-width: 0; }

  /* dialogs open from the bottom edge */
  .modal-dialog {
    margin: 0;
    max-width: none;
    min-height: 100%;
    display: flex;
    align-items: flex-end;
  }
  .modal-content {
    width: 100%;
    border-radius: 20px 20px 0 0;
    max-height: 92vh;
    overflow-y: auto;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .modal-header { position: relative; padding-top: 1.35rem; }
  .modal-header::before {
    content: "";
    position: absolute;
    top: 8px;
    left: 50%;
    width: 40px;
    height: 4px;
    margin-left: -20px;
    border-radius: 999px;
    background: var(--c-line);
  }
  .modal-footer { flex-wrap: nowrap; }
  .modal-footer .btn { flex: 1 1 0; }

  /* login */
  .auth-wrap { padding: 1rem; padding-top: 9vh; }
  .auth-head { padding: 1.6rem 1.4rem 0; }
  .auth-body { padding: 1.25rem 1.4rem 1.5rem; }

  /* chart note under the title on its own line */
  .panel-head .panel-note { width: 100%; }
}

/* ------------------------------------------------------------------
 * Phone (< 768 px): no sidebar. Navigation = bottom tab bar + icon grid.
 * Tablet and desktop keep the sidebar.
 * ------------------------------------------------------------------ */
@media (max-width: 767.98px) {
  .app-sidebar, .menu-btn { display: none !important; }
  body.sidebar-open .sidebar-scrim, .sidebar-scrim { display: none !important; }
  .app-topbar .topbar-title { display: none; }
  .topbar-brand { display: inline-flex; margin-left: 0.3rem; color: #fff; }
  .topbar-brand .brand-mark { width: 24px; height: 24px; border-radius: 7px; }
  .topbar-brand .brand-mark::after { inset: 7px; }

  /* navy app header, like a phone app */
  .app-topbar {
    background: var(--c-navy);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    border-bottom: 0;
    box-shadow: 0 2px 10px rgba(11, 21, 48, 0.25);
  }
  .app-topbar .icon-btn { color: #fff; }
  .app-topbar .icon-btn:hover, .app-topbar .icon-btn:active { background: rgba(255, 255, 255, 0.1); }
  .app-topbar .user-btn:hover { background: rgba(255, 255, 255, 0.1); }
  .app-topbar .avatar { box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.25); }
  .app-topbar .user-btn { color: #fff; }
  .app-topbar .user-role { color: var(--c-navy-text); }

  .tabbar {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(58px + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: rgba(255, 255, 255, 0.96);
    -webkit-backdrop-filter: saturate(180%) blur(10px);
    backdrop-filter: saturate(180%) blur(10px);
    border-top: 1px solid var(--c-line);
    box-shadow: 0 -4px 16px rgba(11, 21, 48, 0.06);
    z-index: 1030;
  }
  .tab-item {
    position: relative;
    flex: 1 1 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: var(--c-muted);
    text-decoration: none;
    font-size: 0.68rem;
    font-weight: 600;
  }
  .tab-item .icon { width: 23px; height: 23px; }
  .tab-item.is-active { color: var(--c-accent); font-weight: 700; }
  .tab-item.is-active::before {
    content: "";
    position: absolute;
    top: 0;
    left: 50%;
    width: 28px;
    height: 3px;
    margin-left: -14px;
    border-radius: 0 0 3px 3px;
    background: var(--c-accent);
  }
  .tab-item:active { background: var(--c-surface-2); }

  .app-main { padding-bottom: calc(58px + 1.5rem + env(safe-area-inset-bottom)); }
  .phone-only { display: block; }

  /* the tile grid sits flush inside its panel */
  .menu-grid { gap: 1.1rem 0.25rem; }
  body.is-loading .app-main::before, body.is-loading .app-main::after { left: 50%; }
}

/* tablet and laptop: secondary columns move into the edit dialog */
@media (min-width: 768px) and (max-width: 1199.98px) {
  .col-secondary { display: none; }
}

/* activity log filters */
@media (min-width: 576px) {
  .filter-grid > :first-child { grid-column: span 2; }
}
@media (max-width: 575.98px) {
  .filter-toggle { display: block; padding: 0.6rem 0.8rem; border-bottom: 1px solid var(--c-line-soft); }
  .filter-grid:not(.is-open) { display: none; }
  .filter-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filter-reset { grid-column: 1 / -1; }
  .filter-reset .btn { width: 100%; }
  .pager { flex-direction: column; align-items: stretch; }
  .pager-buttons { justify-content: space-between; }
  .table-stack td { word-break: break-word; }
}
