/**
 * AD213 Financial Accounting PWA - Responsive Design & Mobile Touch-First CSS
 * Handles Desktop editorial reader vs Mobile touch sheet, safe area insets,
 * and ensures document.documentElement.scrollWidth <= window.innerWidth.
 */

/* ==========================================================================
   Desktop / Landscape Tablet Layout (>= 768px)
   ========================================================================== */

@media (min-width: 768px) {
  .mobile-only {
    display: none !important;
  }

  .main-viewport {
    flex-direction: row;
  }
}

/* ==========================================================================
   Mobile Portrait Touch-First Layout (< 768px)
   ========================================================================== */

@media (max-width: 767px) {
  :root {
    --header-height: 48px;
    --footer-height: 56px;
  }

  .desktop-only {
    display: none !important;
  }

  /* Root & Layout Structure */
  #app-root {
    padding-top: env(safe-area-inset-top, 0px);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }

  .app-header {
    padding: 0 10px;
    gap: 6px;
  }

  .header-left {
    gap: 6px;
  }

  .app-brand {
    gap: 6px;
  }

  .brand-logo-mark {
    width: 26px;
    height: 26px;
    border-radius: 6px;
  }

  .app-brand span.brand-text {
    display: none;
  }

  .lecture-selector {
    margin-left: 0;
  }

  .lecture-btn {
    padding: 0 9px;
    white-space: nowrap;
  }

  .search-btn-label {
    display: none;
  }

  .brand-text-group {
    display: none;
  }

  .header-right {
    gap: 2px;
  }

  .header-btn {
    padding: 0 7px;
    font-size: 0.75rem;
  }

  #compass-btn span:not(.kbd-hint),
  #reset-progress-btn span:not(.kbd-hint) {
    display: none;
  }

  .kbd-hint {
    display: none;
  }

  /* Main Viewport becomes vertical column */
  .main-viewport {
    flex-direction: column;
    width: 100vw;
    max-width: 100vw;
    overflow: hidden;
  }

  /* Full-screen Single-Column Study Reader on Mobile */
  .study-pane {
    flex: 1;
    width: 100vw;
    max-width: 100vw;
    height: 100%;
    background-color: var(--bg-primary);
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
  }

  .study-pane-inner {
    padding: 24px 16px 48px 16px;
    gap: 24px;
  }

  .study-main-card {
    padding: 0;
    gap: 20px;
  }

  .slide-title {
    font-size: 1.45rem;
    line-height: 1.25;
  }

  .slide-kicker {
    font-size: 0.7rem;
    row-gap: 4px;
  }

  /* Lecture/slide number on its own line, section on the next */
  .kicker-lecture {
    flex-basis: 100%;
  }

  .kicker-sep {
    display: none;
  }

  /* Diagrams: keep text readable and let the figure scroll sideways */
  .editorial-diagram-wrap {
    overflow-x: auto;
    align-items: stretch;
    -webkit-overflow-scrolling: touch;
  }

  .editorial-diagram-wrap svg {
    width: 600px;
    min-width: 600px;
    max-width: none;
  }

  /* Caption stays put while the figure scrolls, with a swipe hint */
  .editorial-diagram-wrap .diagram-caption {
    position: sticky;
    left: 0;
  }

  .editorial-diagram-wrap .diagram-caption::before {
    content: 'Swipe sideways to see the full diagram \2192';
    display: block;
    margin-bottom: 4px;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--accent-text);
  }

  /* Bottom Thumb Bar (56px with >= 44px touch targets) */
  .app-footer {
    height: calc(var(--mobile-thumb-height) + env(safe-area-inset-bottom, 0px));
    min-height: calc(var(--mobile-thumb-height) + env(safe-area-inset-bottom, 0px));
    max-height: calc(var(--mobile-thumb-height) + env(safe-area-inset-bottom, 0px));
    padding: 0 8px env(safe-area-inset-bottom, 0px) 8px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    background-color: var(--bg-surface);
    border-top: 1px solid var(--border-color);
  }

  .footer-center {
    display: none;
  }

  .mobile-thumb-bar {
    display: flex;
    align-items: center;
    justify-content: space-around;
    width: 100%;
    height: 100%;
  }

  .mobile-touch-target {
    min-width: 44px;
    min-height: 44px;
    padding: 6px 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: var(--text-secondary);
    font-size: 0.68rem;
    font-weight: 500;
    cursor: pointer;
    touch-action: manipulation;
  }

  .mobile-touch-target:active {
    background-color: var(--bg-hover);
    color: var(--text-primary);
  }

  .mobile-touch-target.active {
    color: var(--accent-text);
  }

  .mobile-touch-target:disabled {
    opacity: 0.35;
    pointer-events: none;
  }

  .mobile-touch-target svg,
  .mobile-touch-target .icon {
    width: 18px;
    height: 18px;
  }

  /* Modals as Bottom Sheets on Mobile */
  .modal-dialog {
    width: 100vw;
    max-width: 100vw;
    max-height: 85dvh;
    border-radius: 14px 14px 0 0;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    animation: sheetSlideUp 0.22s ease-out;
  }

  .modal-body,
  .study-pane,
  .search-results-list {
    overscroll-behavior-y: contain;
  }

  /* Prevent iOS Safari auto-zoom on input focus (must be >= 16px) */
  input,
  textarea,
  select,
  .header-search-input,
  .compass-search-input {
    font-size: 16px !important;
  }

  /* Mobile Header Search Bar & Dropdown */
  /* While searching, the search box takes the whole header row */
  .app-header:has(.header-search-wrapper.open) .lecture-selector,
  .header-right:has(.header-search-wrapper.open) > :not(.header-search-wrapper) {
    display: none;
  }

  .header-right:has(.header-search-wrapper.open) {
    flex: 1;
    justify-content: flex-end;
  }

  .header-search-input {
    transition: opacity 0.18s ease, border-color 0.18s ease;
  }

  .header-search-wrapper.open .header-search-input {
    width: calc(100vw - 104px);
    max-width: calc(100vw - 104px);
  }

  .header-search-dropdown {
    position: fixed;
    top: calc(var(--header-height) + env(safe-area-inset-top, 0px) + 6px);
    left: 8px;
    right: 8px;
    width: auto;
    max-width: none;
  }

  /* Mobile Compass Filter Bar & Cards */
  .compass-filter-bar {
    gap: 2px;
  }

  .compass-pill {
    padding: 6px 3px;
    font-size: 0.72rem;
  }

  .compass-accounts-list {
    grid-template-columns: 1fr;
  }

  .dealer-columns {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  @keyframes sheetSlideUp {
    from {
      transform: translateY(100%);
    }
    to {
      transform: translateY(0);
    }
  }
}

/* Touch devices (including iPads/tablets): prevent iOS Safari auto-zoom on input focus */
@media (hover: none) and (pointer: coarse) {
  input,
  textarea,
  select,
  .header-search-input,
  .compass-search-input {
    font-size: 16px !important;
  }
}

/* ==========================================================================
   Header labels never wrap onto two lines
   ========================================================================== */

.lecture-btn,
.brand-text,
.header-btn {
  white-space: nowrap;
}

/* Tablets / narrow laptops: drop secondary header text so it fits */
@media (min-width: 768px) and (max-width: 1023px) {
  .header-right .kbd-hint,
  .brand-text {
    display: none;
  }

  .app-header {
    padding: 0 14px;
  }
}

/* Phone in landscape: give the reading area more height */
@media (min-width: 768px) and (max-height: 500px) {
  :root {
    --header-height: 44px;
    --footer-height: 44px;
  }

  .app-footer {
    margin-bottom: 6px;
  }
}

/* Very small phones (iPhone SE 1st gen, 320px) */
@media (max-width: 374px) {
  .app-header {
    padding: 0 6px;
  }

  .header-right {
    gap: 0;
  }

  .header-btn {
    padding: 0 5px;
  }

  .lecture-btn {
    padding: 0 7px;
    font-size: 0.72rem;
  }
}

