/* ==========================================================================
   Centre Channel OS — Shared mobile foundation
   --------------------------------------------------------------------------
   This file is the source of truth for responsive breakpoints, iOS safe-area
   handling, touch-target minimums, and shared mobile patterns (sheets,
   responsive tables, full-height layouts).

   Link this file LAST in every app page. It must cascade after per-app
   <style> blocks so its utility rules can override desktop-first defaults.

   Do not redefine --bp-*, --safe-*, or --touch-min inside per-app CSS.
   Extend this file instead.
   ========================================================================== */

:root {
  /* Unified breakpoints. Mobile-first: design for ~360px, enhance upward. */
  --bp-sm: 640px;
  --bp-md: 768px;
  --bp-lg: 1024px;

  /* iOS safe-area insets. Zero on non-notched devices. */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);

  /* Apple HIG minimum touch target. */
  --touch-min: 44px;
}

/* Prevent runaway media from causing horizontal overflow. */
img,
video,
iframe,
svg:not([width]) {
  max-width: 100%;
  height: auto;
}

/* Backstop against any accidental horizontal page scroll. Individual
   containers (like .table-responsive) can still scroll within themselves. */
html,
body {
  overflow-x: clip;
}

/* Burger / dropdown nav for app headers.
   Opt-in: an app header that includes a .header-menu-btn inside it gets
   the mobile collapse behaviour below --bp-sm. Headers without the button
   are left untouched. */
.header-menu-btn {
  display: none;
  background: none;
  border: 0;
  padding: 0;
  width: var(--touch-min);
  height: var(--touch-min);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 5px;
  flex-shrink: 0;
  color: currentColor;
}

.header-menu-btn span {
  display: block;
  width: 22px;
  height: 2px;
  background: currentColor;
  border-radius: 2px;
}

@media (max-width: 639.98px) {
  /* Keep the header's existing vertical stack (header-left row above,
     header-actions row below). Only flip .header-left to a row so the
     burger sits inline with the brand, and anchor the nav dropdown to
     the bottom of the whole header. */
  .header:has(.header-menu-btn) {
    position: relative;
  }

  .header:has(.header-menu-btn) .header-left {
    flex-direction: row;
    align-items: center;
    gap: 10px;
    min-width: 0;
  }

  .header:has(.header-menu-btn) .header-brand {
    min-width: 0;
    flex: 1;
  }

  .header:has(.header-menu-btn) .header-brand h1 {
    font-size: 1.25rem;
  }

  .header-menu-btn {
    display: inline-flex;
  }

  .header:has(.header-menu-btn) .header-nav {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    flex-direction: column;
    gap: 2px;
    background: var(--bg, #f5f2eb);
    border-bottom: 1px solid var(--border, #d8d6d0);
    padding: 8px 16px 16px;
    z-index: 100;
  }

  .header:has(.header-menu-btn) .header-nav.open {
    display: flex;
  }

  .header:has(.header-menu-btn) .header-nav a {
    padding: 12px 16px;
    min-height: var(--touch-min);
    display: flex;
    align-items: center;
    font-size: 0.8125rem;
  }
}

/* Responsive table wrapper.
   Usage: <div class="table-responsive"><table>...</table></div> */
.table-responsive {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  position: relative;
}

.table-responsive > table {
  min-width: 100%;
}

/* Full-height helpers using dvh (respects dynamic iOS address bar).
   Prefer these over 100vh for app shells, modals, and sheets. */
.h-screen-d { height: 100dvh; }
.min-h-screen-d { min-height: 100dvh; }

/* Safe-area utilities for sticky headers/footers in standalone/PWA mode. */
.safe-top { padding-top: calc(var(--safe-top) + 1rem); }
.safe-bottom { padding-bottom: calc(var(--safe-bottom) + 1rem); }
.safe-x {
  padding-left: calc(var(--safe-left) + 1rem);
  padding-right: calc(var(--safe-right) + 1rem);
}

/* Touch-target utility. Pair with inline-flex centering for icon buttons. */
.touch-target {
  min-width: var(--touch-min);
  min-height: var(--touch-min);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Mobile sheet — full-width slide-up panel on phones.
   Phase 2 will apply this to lounge and trading detail panels.
   Above --bp-md the per-app desktop layout kicks back in. */
@media (max-width: 767.98px) {
  .sheet-mobile {
    position: fixed;
    inset: auto 0 0 0;
    width: 100% !important;
    max-width: 100% !important;
    height: 100dvh;
    max-height: 100dvh;
    padding-top: calc(var(--safe-top) + 1rem);
    padding-bottom: calc(var(--safe-bottom) + 1rem);
    padding-left: calc(var(--safe-left) + 1rem);
    padding-right: calc(var(--safe-right) + 1rem);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* Touch-target minimums for known small icon buttons.
   Lounge/Backstage icon buttons below the 44px Apple HIG threshold.
   Pair with the .touch-target utility on any new icon button.
   Note: sort-indicator arrows are not targeted — the <th> itself is the
   clickable element; Phase 2 will extend these headers to 44px. */
@media (max-width: 767.98px) {
  .pill-remove,
  .file-remove,
  .file-remove-btn,
  .icon-button,
  button[data-touch-sm] {
    min-width: var(--touch-min);
    min-height: var(--touch-min);
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
}

/* Remove the grey tap-highlight flash on touch devices for a native feel. */
@media (hover: none) and (pointer: coarse) {
  a,
  button,
  [role="button"] {
    -webkit-tap-highlight-color: transparent;
  }
}

/* OS launcher — narrow-viewport grid override.
   os/apps.html uses auto-fit minmax(240px, 1fr); force single column and
   trim padding below --bp-sm so the launcher fits on small phones. */
@media (max-width: 639.98px) {
  .apps-grid {
    grid-template-columns: 1fr !important;
    padding: 32px 16px !important;
    gap: 16px !important;
  }

  .apps-grid .app-card {
    padding: 24px 20px !important;
  }

  .os-header {
    padding: calc(var(--safe-top) + 24px) 16px 0 !important;
  }

  .os-header h1 {
    font-size: 1.625rem !important;
  }
}
