/*
 * app-shell.css — cross-page polish + mobile responsive layer (Sprint 31).
 *
 * Loaded AFTER each page's inline <style> block so its rules win via cascade
 * order. Nothing here is !important unless we're deliberately overriding a
 * page-specific desktop rule on mobile.
 *
 * Contents:
 *   1. Extra design tokens (transitions, shadows, elevation, easing)
 *   2. Universal polish: focus rings, hover elevation, skeleton, transitions
 *   3. Mobile responsive rules (≤640px + ≤380px) touching common class names
 *   4. Bottom nav bar (fixed, visible on mobile only)
 *   5. Hamburger menu affordance
 */

/* ── 1. Extra tokens (page :root wins for base palette) ─────────────────── */
:root {
  --transition-fast: 120ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition:      180ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-slow: 240ms cubic-bezier(0.4, 0, 0.2, 1);
  --easing-standard: cubic-bezier(0.4, 0, 0.2, 1);
  --easing-out:      cubic-bezier(0, 0, 0.2, 1);
  --shadow-sm:  0 1px 2px rgba(0,0,0,0.10);
  --shadow-md:  0 4px 12px rgba(0,0,0,0.14);
  --shadow-lg:  0 12px 32px rgba(0,0,0,0.24);
  --shadow-glow: 0 4px 20px rgba(99, 102, 241, 0.22);
  --bottom-nav-h: 64px;
}

/* ── 2. Universal polish ─────────────────────────────────────────────────── */

/* Smoother font rendering on Mac/iOS */
html { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }

/* Buttons + links + inputs have consistent motion */
a, button, select, input, textarea,
.btn, .kpi, .kpi-card, .ltv-card, .card, .chip, .pill,
[class*="range-pill"], [class*="menu-item"] {
  transition:
    background-color var(--transition),
    border-color     var(--transition),
    color            var(--transition),
    box-shadow       var(--transition),
    transform        var(--transition-fast),
    opacity          var(--transition);
}

/* Focus rings — visible for keyboard users, invisible for mouse */
a:focus-visible, button:focus-visible,
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--bg, #0f1117), 0 0 0 4px var(--accent, #6366f1);
  border-radius: 6px;
}

/* Card hover: subtle lift + border glow */
.card:hover, .kpi:hover, .kpi-card:hover, .ltv-card:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
  border-color: color-mix(in srgb, var(--accent, #6366f1) 30%, var(--border, #2d3148));
}

/* Buttons: elevation on hover, tactile press */
.btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: var(--shadow-glow);
}
.btn:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: var(--shadow-sm);
}
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Ghost buttons + toolbar selects: border glow instead */
.btn-ghost:hover, .toolbar button:hover, .toolbar select:hover,
select:hover, .range-pill:hover:not(.active) {
  border-color: var(--accent, #6366f1);
  color: var(--text, #e8eaf0);
}

/* Table rows: subtle hover tint */
table tbody tr { transition: background-color var(--transition-fast); }
table tbody tr:hover td { background: color-mix(in srgb, var(--accent, #6366f1) 4%, transparent); }

/* Skeleton shimmer for loading states */
@keyframes skel-shimmer {
  0%   { background-position: -400px 0; }
  100% { background-position:  400px 0; }
}
.skeleton, [data-skeleton] {
  background: linear-gradient(
    90deg,
    color-mix(in srgb, var(--surface, #1a1d27) 100%, transparent) 0%,
    color-mix(in srgb, var(--surface2, #22263a) 100%, transparent) 50%,
    color-mix(in srgb, var(--surface, #1a1d27) 100%, transparent) 100%
  );
  background-size: 800px 100%;
  animation: skel-shimmer 1.4s infinite linear;
  color: transparent !important;
  border-radius: 6px;
  user-select: none;
}

/* Empty states: consistent visual weight */
.empty {
  padding: 40px 20px;
  color: var(--muted, #7b84a3);
  text-align: center;
  font-size: 13px;
  line-height: 1.55;
}

/* Fade-in for page main content — feels snappier */
@keyframes shell-fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}
main, .page, .app {
  animation: shell-fade-in 260ms var(--easing-out) both;
}

/* Scrollbar polish (WebKit + Firefox) */
* { scrollbar-width: thin; scrollbar-color: var(--border, #2d3148) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb {
  background: var(--border, #2d3148);
  border-radius: 4px;
}
*::-webkit-scrollbar-thumb:hover { background: var(--muted, #7b84a3); }
*::-webkit-scrollbar-track { background: transparent; }

/* Tap targets ≥ 44px (iOS HIG) — auto-applied on mobile via media query */

/* ── 3. Mobile responsive ────────────────────────────────────────────────── */
@media (max-width: 640px) {
  body { font-size: 14px; }

  /* Header: nav becomes hamburger — hide horizontal nav.
     The mobile bottom nav (see section 4) takes over primary navigation. */
  header nav { display: none !important; }
  header {
    padding: 0 14px !important;
    height: 52px;
    gap: 10px !important;
  }
  header .logo { font-size: 15px !important; }

  /* Store selector: don't overflow header */
  #store-select {
    max-width: 46vw !important;
    min-width: 0 !important;
    margin-left: auto !important;
    font-size: 12px !important;
  }

  /* Page container: reduced padding + room for bottom nav */
  .page, main.page {
    padding: 16px 12px calc(var(--bottom-nav-h) + 24px) !important;
  }
  main.app {
    padding-bottom: calc(var(--bottom-nav-h) + 24px) !important;
  }
  h1 { font-size: 20px !important; margin-bottom: 4px !important; }
  .subtitle, .sub { font-size: 12px !important; margin-bottom: 16px !important; }

  /* KPI grids: 2 columns (down to ≤380 = 1 column, see below) */
  .ltv-grid, .grid, .kpi-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }
  .ltv-card, .kpi, .kpi-card {
    padding: 12px !important;
  }
  .ltv-card .v, .kpi .v, .kpi-value {
    font-size: 16px !important;
    letter-spacing: -0.3px;
  }
  .ltv-card .l, .kpi .l, .kpi-label {
    font-size: 10px !important;
  }
  .ltv-card .s, .kpi .s, .kpi-sub {
    font-size: 10px !important;
  }

  /* Toolbar (view selector, filter chips): stack vertically */
  .toolbar {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
  }
  .toolbar > div[style*="flex:1"] { display: none; }
  .toolbar select { width: 100%; padding: 8px 10px !important; font-size: 13px !important; min-height: 36px; }
  .toolbar label { width: 100%; display: flex; flex-direction: column; gap: 4px; }

  /* Charts: shorter on phone */
  .chart-wrap, .chart-container {
    height: 240px !important;
  }

  /* Cash Flow slider row: readable on phone */
  .slider-row {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
    font-size: 11px !important;
  }
  .slider-row > span { text-align: center; }
  input[type=range] { width: 100% !important; min-width: 0 !important; }

  /* Tables: enable horizontal scroll on their wrappers */
  .cohort-table-wrap, .table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
  }
  .cohort-table, table {
    font-size: 11px;
  }
  .cohort-table th, .cohort-table td {
    padding: 5px 7px !important;
  }
  table th, table td { padding: 8px 6px !important; }

  /* Section titles: tighter */
  .section-title {
    font-size: 11px !important;
    margin: 18px 0 8px !important;
  }

  /* Modals: full-width, bottom-sheet-ish */
  .modal, .overlay .modal {
    width: 94vw !important;
    max-height: 88vh;
    overflow-y: auto;
    border-radius: 14px 14px 8px 8px !important;
  }
  .field input, .field select {
    min-height: 40px;
    font-size: 14px !important;
  }
  .modal-actions { flex-direction: column-reverse; gap: 8px !important; }
  .modal-actions .btn { width: 100%; }

  /* All buttons: min tap target 40px on phone */
  .btn, button.btn, a.btn { min-height: 40px; }
  .btn-sm { min-height: 32px; }

  /* Bottom nav appears */
  .mobile-bottom-nav { display: flex !important; }

  /* Safe-area bottom padding for iOS notches */
  body { padding-bottom: env(safe-area-inset-bottom); }
}

/* Even narrower — collapse to single-column KPIs */
@media (max-width: 380px) {
  .ltv-grid, .grid, .kpi-grid {
    grid-template-columns: 1fr !important;
  }
  .kpi-grid > .kpi-card:last-child:nth-child(odd) { grid-column: auto !important; }
  h1 { font-size: 18px !important; }
}

/* ── 4. Bottom nav (mobile only) ─────────────────────────────────────────── */
.mobile-bottom-nav {
  display: none; /* toggled via @media above */
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: calc(var(--bottom-nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface, #1a1d27) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--border, #2d3148);
  z-index: 40;
  align-items: stretch;
}
.mobile-bottom-nav a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  color: var(--muted, #7b84a3);
  text-decoration: none;
  font-size: 10px;
  font-weight: 500;
  min-height: 44px;
  padding: 6px 4px;
}
.mobile-bottom-nav a svg {
  width: 22px; height: 22px;
  stroke: currentColor;
  stroke-width: 2;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.mobile-bottom-nav a[data-active="true"] {
  color: var(--accent, #6366f1);
}
.mobile-bottom-nav a:active {
  background: var(--surface2, #22263a);
}

/* ── 5. Reduced motion respect ─────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
