/* Phase 41 §2 adaptive layout (docs/PHASE41_FOCUS_ENGAGEMENT_ATTENDANCE_PROMPT.md).
   device-profile.js keeps data-device (phone · tablet · desktop), data-input (touch · pointer) and
   data-orientation on <html>; the width media queries below are the fallback before it runs. Every
   feature stays reachable on every device - layouts change, content never disappears.
   Loaded last so it refines styles.css, reformation.css, phase39/40/41.css without fighting them. */

/* ------------------------------------------------------------------ every device */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
img, svg, video, canvas, iframe { max-width: 100%; }
.content, .rf-workspace, .rf-card, .panel { min-width: 0; }
.rf-card { overflow-wrap: anywhere; }
.resource-tabs, .rf-tabs, .p40-tabs { scrollbar-width: thin; }
:focus-visible { outline-offset: 2px; }

/* Touch screens: comfortable 44 px targets everywhere (WCAG 2.5.5), whatever the width. */
:root[data-input="touch"] .rf-button,
:root[data-input="touch"] .mini-button,
:root[data-input="touch"] .primary-button,
:root[data-input="touch"] .secondary-button,
:root[data-input="touch"] .icon-button,
:root[data-input="touch"] .nav-item,
:root[data-input="touch"] .resource-tab,
:root[data-input="touch"] .rf-tab,
:root[data-input="touch"] .doc-chip,
:root[data-input="touch"] .p41-team-btn { min-height: 44px; }
:root[data-input="touch"] .rf-link { min-height: 36px; padding: 6px 4px; }
:root[data-input="touch"] .rf-segmented span { padding: 10px 14px; }

/* ------------------------------------------------------------------ wide computers (>= 1600 px) */
@media (min-width: 1600px) {
  .content { max-width: 1680px; margin-inline: auto; }
  .p40-kpis { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
  .p41-cohort-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

/* ------------------------------------------------------------------ tablets (640 - 1099 px) */
@media (min-width: 640px) and (max-width: 1099px) {
  :root { --p41-rail: 78px; }
  .app-shell { display: grid; grid-template-columns: var(--p41-rail) minmax(0, 1fr); }
  .sidebar { position: sticky; top: 0; left: auto; z-index: 40; width: var(--p41-rail); height: 100vh; height: 100dvh; padding: 18px 10px 14px; transform: none; box-shadow: none; transition: width .22s var(--rf-ease, ease); overflow: hidden; }
  .sidebar .brand > span, .sidebar .sync-card div, .sidebar .logout-button { display: none; }
  .sidebar .brand-logo { width: 46px; height: 46px; }
  .sidebar-top { justify-content: center; padding: 0 0 16px; }
  .main-nav { overflow-y: auto; overflow-x: hidden; scrollbar-width: none; }
  .main-nav::-webkit-scrollbar { display: none; }
  .nav-group { height: 1px; margin: 10px 8px; overflow: hidden; color: transparent; background: rgba(255, 255, 255, .12); }
  .nav-item { justify-content: center; padding: 11px 0; }
  .nav-item > span:nth-child(2) { display: none; }
  .nav-item .nav-icon { width: auto; font-size: 19px; }
  .nav-item .nav-badge { position: absolute; transform: translate(14px, -12px); }
  .nav-item { position: relative; }
  .sync-card { justify-content: center; padding: 10px; }
  /* Expand the rail into the full menu on hover, keyboard focus or the menu button. */
  .sidebar:hover, .sidebar:focus-within, .sidebar.open { position: fixed; left: 0; width: 272px; box-shadow: 24px 0 50px rgba(0, 0, 0, .22); }
  .sidebar:hover .brand > span, .sidebar:focus-within .brand > span, .sidebar.open .brand > span,
  .sidebar:hover .logout-button, .sidebar:focus-within .logout-button, .sidebar.open .logout-button,
  .sidebar:hover .sync-card div, .sidebar:focus-within .sync-card div, .sidebar.open .sync-card div { display: block; }
  .sidebar:hover .nav-item > span:nth-child(2), .sidebar:focus-within .nav-item > span:nth-child(2), .sidebar.open .nav-item > span:nth-child(2) { display: inline; }
  .sidebar:hover .nav-item, .sidebar:focus-within .nav-item, .sidebar.open .nav-item { justify-content: flex-start; padding: 10px 12px; }
  .sidebar:hover .nav-group, .sidebar:focus-within .nav-group, .sidebar.open .nav-group { height: auto; margin: 16px 10px 5px; color: rgba(255, 255, 255, .4); background: none; }
  .sidebar:hover .sidebar-top, .sidebar:focus-within .sidebar-top, .sidebar.open .sidebar-top { justify-content: space-between; padding: 0 8px 20px; }
  .sidebar:hover + .scrim, .sidebar:focus-within + .scrim { display: none; }
  .mobile-only#openMenu { display: none; }
  .sidebar:not(.open) #closeMenu { display: none; }
  .topbar { position: sticky; }
  .search-box { min-width: 0; }
  .search-box input { width: 150px; }
  .p40-grid, .rf-two { grid-template-columns: 1fr; }
  .p41-mc-layout { grid-template-columns: minmax(260px, 320px) minmax(0, 1fr); }
  .p41-cohort-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .p40-kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .overview-grid { grid-template-columns: 1fr 1fr; }
  dialog.modal, .rf-dialog, .p40-dialog { max-width: 92vw; }
  .book-reader-modal { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; }
}

/* ------------------------------------------------------------------ phones (< 640 px) */
@media (max-width: 639px) {
  :root { --p41-dock-height: 64px; }
  body { padding-bottom: calc(var(--p41-dock-height) + env(safe-area-inset-bottom, 0px)); }
  input, select, textarea, .rf-input { font-size: 16px; } /* no zoom-on-focus in iOS Safari */

  /* bottom dock */
  .app-shell:not([hidden]) .p41-dock { position: fixed; left: 0; right: 0; bottom: 0; z-index: 35; display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; height: calc(var(--p41-dock-height) + env(safe-area-inset-bottom, 0px)); padding: 6px 6px env(safe-area-inset-bottom, 0px); border-top: 1px solid rgba(255, 255, 255, .08); background: rgba(3, 27, 44, .96); backdrop-filter: blur(14px); box-shadow: 0 -10px 30px rgba(3, 27, 44, .18); }
  .p41-dock-item { position: relative; display: grid; place-items: center; align-content: center; gap: 2px; min-width: 0; border: 0; border-radius: 14px; color: rgba(255, 255, 255, .66); background: transparent; font: inherit; cursor: pointer; }
  .p41-dock-item.active { color: #052b47; background: linear-gradient(135deg, #ffd978, #f0b429); }
  .p41-dock-icon { font-size: 19px; line-height: 1; }
  .p41-dock-label { max-width: 100%; overflow: hidden; font-size: 10px; font-weight: 800; text-overflow: ellipsis; white-space: nowrap; }
  .p41-dock-badge { position: absolute; top: 4px; right: 18%; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; color: #fff; background: #c81f2c; font-size: 10px; font-weight: 800; line-height: 18px; }
  .sidebar { z-index: 45; width: min(86vw, 320px); }
  .scrim { z-index: 44; }
  .nc-drawer { position: fixed; left: 8px; right: 8px; top: 64px; width: auto; max-height: 70vh; }

  /* header */
  .topbar { min-height: 60px; padding: 8px 12px; gap: 8px; align-items: center; }
  .topbar h1 { overflow: hidden; font-size: 17px; text-overflow: ellipsis; white-space: nowrap; }
  .breadcrumb { display: none; }
  .topbar-actions { gap: 4px; }
  .topbar-actions .compact-select, #refreshButton { display: none; }
  .content { padding: 12px; }

  /* layouts collapse to one column */
  .p40-grid, .rf-two, .rf-grid-2, .rf-grid-3, .overview-grid, .p41-mc-layout, .p41-cohort-grid { grid-template-columns: 1fr; }
  .p40-kpis, .rf-tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .p40-kpi, .rf-tile { padding: 12px; }
  .rf-hero, .p40-hero { flex-direction: column; align-items: flex-start; gap: 14px; padding: 22px 18px; }
  .rf-hero h2 { font-size: clamp(22px, 7vw, 28px); }
  .rf-hero-aside { align-self: center; }
  .rf-card { padding: 14px; border-radius: 16px; }
  .rf-card-head { flex-wrap: wrap; gap: 8px; }

  /* tabs and chip rows scroll sideways instead of wrapping into walls of buttons */
  .resource-tabs, .rf-tabs, .p40-tabs, .doc-chip-row, .p41-channel-chips { flex-wrap: nowrap; overflow-x: auto; scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .resource-tabs::-webkit-scrollbar, .rf-tabs::-webkit-scrollbar, .p40-tabs::-webkit-scrollbar, .doc-chip-row::-webkit-scrollbar { display: none; }
  .resource-tab, .rf-tab, .doc-chip, .p41-channel-chip { flex: none; scroll-snap-align: start; }
  .rf-segmented { display: flex; max-width: 100%; overflow-x: auto; scrollbar-width: none; }
  .rf-segmented span { white-space: nowrap; }
  .toolbar-actions { flex-wrap: wrap; justify-content: stretch; }
  .toolbar-actions > button { flex: 1 1 auto; }
  .p41-filter-row > * { flex: 1 1 100%; }
  .p41-filter-row > .rf-button, .p41-filter-row > .p41-check { flex: 1 1 auto; }

  /* Phase 41 tables become labelled cards */
  .p41-table thead { display: none; }
  .p41-table, .p41-table tbody, .p41-table tr, .p41-table td { display: block; width: 100%; }
  .p41-table tr { margin-bottom: 10px; padding: 10px 12px; border: 1px solid var(--rf-hairline); border-radius: 14px; background: var(--rf-surface); }
  .p41-table td { display: grid; grid-template-columns: minmax(96px, 38%) minmax(0, 1fr); gap: 10px; padding: 6px 0; border: 0; }
  .p41-table td[data-label]::before { content: attr(data-label); color: var(--rf-text-3); font-size: 11px; font-weight: 800; letter-spacing: .05em; text-transform: uppercase; }
  .p41-table td:not([data-label]) { display: flex; gap: 12px; }
  .p41-table .p40-bar { max-width: none; }

  /* Message Center: the list, then the conversation full width with a back button */
  .p41-mc-layout.has-thread .p41-thread-list { display: none; }
  .p41-mc-layout:not(.has-thread) .p41-conversation { display: none; }
  .p41-back { display: inline-flex; }
  .p41-thread-list { max-height: none; }
  .p41-bubbles { max-height: none; }
  .p41-bubble { max-width: 92%; }
  .p41-conv-people .p41-person small { display: none; }

  /* reading, cohorts, documents */
  .p41-book-row { grid-template-columns: 1fr; gap: 6px; }
  .p41-my-reading-main, .p41-my-cohort-head, .p41-detail-top { flex-direction: column; align-items: flex-start; }
  .p41-cal-row { grid-template-columns: 1fr 1fr; }
  .p41-cal-row input[name="calTitle"] { grid-column: 1 / -1; }
  .p41-doc-segments { grid-template-columns: repeat(3, minmax(120px, 1fr)); overflow-x: auto; }
  .p41-doc-seg b { font-size: 20px; }
  .doc-card-grid { grid-template-columns: 1fr; }
  .doc-toolbar { flex-direction: column; align-items: stretch; }

  /* dialogs become bottom sheets */
  dialog.modal:not(.book-reader-modal), .rf-dialog, .p40-dialog { width: 100vw; max-width: 100vw; max-height: 92dvh; margin: auto 0 0; border-radius: 22px 22px 0 0; padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px)); }
  .p40-form-grid { grid-template-columns: 1fr; }
  .book-reader-modal { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; }
  .book-reader-bar { flex-wrap: wrap; gap: 8px; padding: 10px 12px; }
  .book-reader-toolbar { order: 3; width: 100%; justify-content: space-between; }
  .book-reader-chip { padding-inline: 8px; font-size: 11px; }
  .p41-tracking-host { order: 2; }
  .p41-reading-indicator { font-size: 11px; }
}

/* very small phones */
@media (max-width: 360px) {
  .p40-kpis, .rf-tiles { grid-template-columns: 1fr; }
  .p41-dock-label { font-size: 9px; }
}

/* landscape phones: keep the dock slim */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
  :root { --p41-dock-height: 52px; }
  .p41-dock-label { display: none; }
}

@media print {
  .p41-dock, .sidebar, .topbar, .scrim { display: none !important; }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
}
