/**
 * Responsive shell, display modes, mobile navigation, shared mobile patterns.
 * Effective layout uses html[data-layout="mobile|desktop"] (set by displayMode.js).
 * Preference is html[data-display-mode="auto|mobile|desktop"].
 */

:root {
  --mobile-header-h: 56px;
  --mobile-bottom-h: 64px;
  --touch-min: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

body.vq-locked .mobile-bottom-nav,
body.vq-locked .mobile-topbar,
body.vq-locked .nav-drawer-backdrop,
body.vq-locked #main-sidebar,
body.vq-locked .flow-assistant-fab,
body.vq-locked .offline-banner {
  display: none !important;
}

.clients-search-bar {
  margin: 0 0 14px;
}

.clients-search-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.clients-search-input {
  flex: 1 1 220px;
  min-height: var(--touch-min, 44px);
  font-size: 16px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--veriqal-border);
  background: var(--veriqal-surface, transparent);
  color: inherit;
}

.clients-search-input:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring, 0 0 0 3px rgba(18, 200, 232, 0.45));
  border-color: var(--veriqal-cyan, #12C8E8);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.cal-event-meta {
  display: block;
  font-size: 11px;
  opacity: 0.85;
  margin-top: 2px;
}

.offline-banner {
  display: none;
  position: sticky;
  top: 0;
  z-index: 1200;
  background: #7a1f1f;
  color: #fff;
  text-align: center;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 600;
}
html.is-offline .offline-banner,
body.is-offline .offline-banner {
  display: block;
}

/* —— Mobile top bar (hidden on desktop layout) —— */
.mobile-topbar {
  display: none;
  position: sticky;
  top: 0;
  z-index: 80;
  align-items: center;
  gap: 10px;
  min-height: var(--mobile-header-h);
  padding: calc(8px + var(--safe-top)) 12px 8px;
  padding-left: calc(12px + var(--safe-left));
  padding-right: calc(12px + var(--safe-right));
  background: var(--sidebar-bg, var(--veriqal-sidebar-bg, #061B3A));
  color: var(--sidebar-text, #fff);
  border-bottom: 1px solid var(--sidebar-border, rgba(18, 200, 232, 0.35));
}

.mobile-menu-btn {
  flex: 0 0 auto;
  min-width: var(--touch-min);
  min-height: var(--touch-min);
  border-radius: 12px;
  border: 1px solid var(--sidebar-nav-border, rgba(255, 255, 255, 0.2));
  background: transparent;
  color: inherit;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}

.mobile-menu-btn:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring, 0 0 0 3px rgba(18, 200, 232, 0.45));
}

.mobile-topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.mobile-topbar-logo {
  width: 36px;
  height: 36px;
  object-fit: contain;
  border-radius: 8px;
  background: var(--logo-bg, #fff);
  border: 1px solid var(--logo-border, rgba(255, 255, 255, 0.2));
}

.mobile-topbar-titles {
  min-width: 0;
}

.mobile-topbar-titles strong {
  display: block;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mobile-topbar-titles span {
  display: block;
  font-size: 11px;
  opacity: 0.8;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mobile-topbar-assistant {
  min-width: var(--touch-min);
  min-height: var(--touch-min);
  border-radius: 12px;
  border: 1px solid var(--sidebar-nav-border, rgba(255, 255, 255, 0.2));
  background: var(--sidebar-nav-hover, rgba(18, 200, 232, 0.16));
  color: inherit;
  font-size: 12px;
  font-weight: 700;
  padding: 0 10px;
  cursor: pointer;
}

/* —— Drawer overlay —— */
.nav-drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(0, 0, 0, 0.45);
  border: 0;
  padding: 0;
  cursor: pointer;
}

html[data-layout="mobile"] .app-shell {
  grid-template-columns: 1fr;
  padding-bottom: calc(var(--mobile-bottom-h) + var(--safe-bottom));
}

html[data-layout="mobile"] .mobile-topbar {
  display: flex;
}

html[data-layout="mobile"] .sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(320px, 88vw);
  height: 100vh;
  height: 100dvh;
  z-index: 100;
  transform: translateX(-105%);
  transition: transform 0.22s ease;
  padding-top: calc(22px + var(--safe-top));
  padding-bottom: calc(22px + var(--safe-bottom));
  box-shadow: 8px 0 28px rgba(0, 0, 0, 0.35);
}

html[data-layout="mobile"].nav-drawer-open .sidebar,
html[data-layout="mobile"] body.nav-drawer-open .sidebar {
  transform: translateX(0);
}

html.nav-drawer-open .nav-drawer-backdrop,
body.nav-drawer-open .nav-drawer-backdrop {
  display: block;
}

html.nav-drawer-open,
html.nav-drawer-open body,
body.nav-drawer-open {
  overflow: hidden;
}

html[data-layout="mobile"] .main-content {
  padding: 16px 14px calc(24px + var(--mobile-bottom-h));
}

html[data-layout="mobile"] .page-header {
  flex-direction: column;
  align-items: stretch;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 14px;
}

html[data-layout="mobile"] .page-header-logo {
  display: none;
}

html[data-layout="mobile"] .page-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

html[data-layout="mobile"] .page-actions .btn {
  min-height: var(--touch-min);
  flex: 1 1 auto;
}

/* Desktop layout keeps classic sticky sidebar */
html[data-layout="desktop"] .app-shell {
  grid-template-columns: 280px minmax(0, 1fr);
}

html[data-layout="desktop"] .sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
  transform: none !important;
}

html[data-layout="desktop"] .mobile-topbar,
html[data-layout="desktop"] .mobile-bottom-nav,
html[data-layout="desktop"] .nav-drawer-backdrop {
  display: none !important;
}

html[data-display-mode="desktop"][data-viewport="phone"] .app-shell,
html[data-display-mode="desktop"][data-viewport="tablet"] .app-shell {
  overflow-x: auto;
}

html[data-display-mode="desktop"][data-viewport="phone"] .main-content {
  min-width: min(100%, 720px);
}

/* —— Bottom quick actions —— */
.mobile-bottom-nav {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 70;
  grid-template-columns: repeat(5, 1fr);
  gap: 2px;
  min-height: var(--mobile-bottom-h);
  padding: 6px 4px calc(6px + var(--safe-bottom));
  background: var(--veriqal-surface, #101827);
  border-top: 1px solid var(--veriqal-border, rgba(255, 255, 255, 0.12));
  box-shadow: 0 -6px 20px rgba(0, 0, 0, 0.2);
}

html[data-layout="mobile"] .mobile-bottom-nav {
  display: grid;
}

html.hide-mobile-quick-actions .mobile-bottom-nav {
  display: none !important;
}

html.hide-mobile-quick-actions[data-layout="mobile"] .app-shell,
html.hide-mobile-quick-actions[data-layout="mobile"] .main-content {
  padding-bottom: 16px;
}

.mobile-bottom-nav button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-primary, inherit);
  min-height: var(--touch-min);
  border-radius: 10px;
  font-size: 10px;
  font-weight: 700;
  line-height: 1.15;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 4px 2px;
}

.mobile-bottom-nav button .mbn-icon {
  font-size: 16px;
  line-height: 1;
}

.mobile-bottom-nav button.active {
  background: var(--veriqal-accent-16, rgba(18, 200, 232, 0.16));
  color: var(--veriqal-cyan, #12C8E8);
}

.mobile-bottom-nav button:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring, 0 0 0 3px rgba(18, 200, 232, 0.45));
}

/* FAB sits above bottom nav on mobile */
html[data-layout="mobile"] .flow-assistant-fab {
  bottom: calc(var(--mobile-bottom-h) + var(--safe-bottom) + 12px);
  right: max(12px, var(--safe-right));
  z-index: 65;
}

html.hide-mobile-quick-actions[data-layout="mobile"] .flow-assistant-fab {
  bottom: calc(12px + var(--safe-bottom));
}

/* —— Display mode control (menus / settings) —— */
.display-mode-control {
  display: grid;
  gap: 8px;
}

.display-mode-control legend,
.display-mode-label {
  font-size: 12px;
  font-weight: 700;
  color: var(--veriqal-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.display-mode-options {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.display-mode-options label {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--touch-min);
  padding: 8px 10px;
  border: 1px solid var(--veriqal-border);
  border-radius: 10px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
}

.display-mode-options input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.display-mode-options label:has(input:checked),
.display-mode-options label.is-selected {
  border-color: var(--veriqal-cyan, #12C8E8);
  background: var(--veriqal-accent-16, rgba(18, 200, 232, 0.16));
}

.sidebar-display-mode {
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--sidebar-border);
}

.sidebar-display-mode .display-mode-options label {
  color: var(--sidebar-text);
  border-color: var(--sidebar-nav-border);
  font-size: 11px;
  padding: 6px 4px;
}

/* —— Shared responsive list / card patterns —— */
.responsive-cards {
  display: none;
  gap: 12px;
}

.vq-mobile-card {
  border: 1px solid var(--veriqal-border);
  border-radius: 12px;
  padding: 14px;
  background: var(--veriqal-surface);
}

.vq-mobile-card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 10px;
}

.vq-mobile-card h4 {
  margin: 0;
  font-size: 15px;
  line-height: 1.3;
}

.vq-mobile-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 12px;
  margin: 0 0 12px;
}

html.compact-mobile-cards .vq-mobile-meta {
  grid-template-columns: 1fr;
}

.vq-mobile-meta dt {
  margin: 0;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--veriqal-muted);
}

.vq-mobile-meta dd {
  margin: 2px 0 0;
  font-size: 13px;
}

.vq-mobile-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.vq-mobile-actions .btn {
  min-height: var(--touch-min);
}

html[data-layout="mobile"] .responsive-table-desktop {
  display: none;
}

html[data-layout="mobile"] .responsive-cards {
  display: grid;
}

html[data-layout="desktop"] .responsive-cards {
  display: none !important;
}

html[data-layout="desktop"] .responsive-table-desktop {
  display: block;
}

/* Touch-friendly controls */
html[data-layout="mobile"] .btn,
html[data-layout="mobile"] .nav-button,
html[data-layout="mobile"] .settings-tab {
  min-height: var(--touch-min);
}

html[data-layout="mobile"] .form-grid {
  grid-template-columns: 1fr;
}

html[data-layout="mobile"] input,
html[data-layout="mobile"] select,
html[data-layout="mobile"] textarea {
  font-size: 16px; /* reduce iOS zoom-on-focus */
  min-height: var(--touch-min);
}

html[data-layout="mobile"] .modal-overlay {
  padding: 0;
  align-items: stretch;
}

html[data-layout="mobile"] .modal-panel,
html[data-layout="mobile"] .modal-panel-md,
html[data-layout="mobile"] .modal-panel-lg,
html[data-layout="mobile"] .cal-event-modal,
html[data-layout="mobile"] .handoff-modal,
html[data-layout="mobile"] .status-update-modal,
html[data-layout="mobile"] .flow-assistant-panel {
  width: 100%;
  max-width: none;
  min-height: 100vh;
  min-height: 100dvh;
  max-height: 100vh;
  max-height: 100dvh;
  border-radius: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

/* Calendar mobile helpers */
html[data-layout="mobile"] .cal-month-grid,
html[data-layout="mobile"] .cal-week-grid {
  display: none;
}

html[data-layout="mobile"] .cal-view-panel[data-force-grid="1"] .cal-month-grid,
html[data-layout="mobile"] .cal-view-panel[data-force-grid="1"] .cal-week-grid {
  display: grid;
}

.cal-threeday-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

html[data-layout="mobile"] .cal-threeday-grid {
  grid-template-columns: 1fr;
}

html[data-layout="mobile"] .cal-toolbar {
  flex-wrap: wrap;
  gap: 8px;
}

html[data-layout="mobile"] .cal-view-btns {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  width: 100%;
}

html[data-layout="mobile"] .cal-view-btns .btn {
  flex: 1 1 calc(33.33% - 6px);
  min-height: var(--touch-min);
}

/* Assignment workspace mobile accordions */
html[data-layout="mobile"] .aw-section {
  border: 1px solid var(--veriqal-border);
  border-radius: 12px;
  padding: 0;
  overflow: hidden;
}

html[data-layout="mobile"] .aw-section > .aw-section-title {
  margin: 0;
  padding: 14px 16px;
  background: var(--veriqal-panel-tint, rgba(18, 200, 232, 0.06));
}

html[data-layout="mobile"] details.aw-accordion {
  border: 1px solid var(--veriqal-border);
  border-radius: 12px;
  background: var(--veriqal-surface);
  margin-bottom: 10px;
}

html[data-layout="mobile"] details.aw-accordion > summary {
  list-style: none;
  cursor: pointer;
  min-height: var(--touch-min);
  padding: 12px 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

html[data-layout="mobile"] details.aw-accordion > summary::-webkit-details-marker {
  display: none;
}

html[data-layout="mobile"] details.aw-accordion > summary::after {
  content: '+';
  font-size: 18px;
  opacity: 0.7;
}

html[data-layout="mobile"] details.aw-accordion[open] > summary::after {
  content: '−';
}

html[data-layout="mobile"] .aw-accordion-body {
  padding: 0 14px 14px;
}

.aw-mobile-summary {
  display: none;
}

html[data-layout="mobile"] .aw-mobile-summary {
  display: block;
  margin-bottom: 12px;
}

.aw-mobile-action-bar {
  display: none;
  position: sticky;
  bottom: calc(var(--mobile-bottom-h) + var(--safe-bottom) + 8px);
  z-index: 40;
  gap: 8px;
  padding: 10px;
  background: var(--veriqal-surface);
  border: 1px solid var(--veriqal-border);
  border-radius: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}

html[data-layout="mobile"] .aw-mobile-action-bar {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

html[data-layout="mobile"] .aw-header-actions {
  display: none;
}

/* Flow Assistant mobile */
html[data-layout="mobile"] .flow-assistant-layout {
  grid-template-columns: 1fr;
  gap: 12px;
}

html[data-layout="mobile"] .flow-chat-history {
  max-height: min(42vh, 360px);
}

html[data-layout="mobile"] .flow-preview-pane {
  max-height: none;
}

html[data-layout="mobile"] .voice-mic-btn,
html[data-layout="mobile"] #flow-mic {
  min-height: 64px;
  width: 100%;
  font-size: 16px;
  font-weight: 700;
  border-radius: 16px;
}

html[data-layout="mobile"] .flow-compose {
  position: sticky;
  bottom: 0;
  background: var(--veriqal-surface, #101827);
  padding-bottom: var(--safe-bottom);
  z-index: 2;
}

html[data-layout="mobile"] .flow-compose textarea {
  min-height: 52px;
}

.flow-mobile-upload-row {
  display: none;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}

html[data-layout="mobile"] .flow-mobile-upload-row {
  display: grid;
}

html[data-layout="mobile"] .flow-mobile-upload-row .btn {
  min-height: var(--touch-min);
  font-size: 12px;
}

/* Ops dashboard priority stack */
html[data-layout="mobile"] .ops-mobile-stack > .panel {
  margin-bottom: 12px;
}

/* Install guidance */
.pwa-install-banner {
  display: none;
  margin: 0 0 12px;
  padding: 12px 14px;
  border: 1px solid var(--veriqal-border);
  border-radius: 12px;
  background: var(--veriqal-panel-tint, rgba(18, 200, 232, 0.06));
  font-size: 13px;
}

html[data-layout="mobile"] .pwa-install-banner.is-visible {
  display: block;
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  html[data-layout="mobile"] .sidebar,
  .nav-drawer-backdrop {
    transition: none !important;
  }
}

html[data-reduce-motion="1"] *,
html.reduce-motion-pref * {
  animation: none !important;
  transition: none !important;
  scroll-behavior: auto !important;
}

/* Auto fallback when JS has not set data-layout yet */
@media (max-width: 900px) {
  html:not([data-layout]) .app-shell {
    grid-template-columns: 1fr;
  }
}
