/*
 * Shared interaction and presentation layer inspired by Apple's web-facing
 * design principles: direct feedback, spatial continuity, material hierarchy,
 * adaptive typography, and accessible motion.
 */

:root {
  --apple-font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Segoe UI", system-ui, sans-serif;
  --apple-mono-family: ui-monospace, "SFMono-Regular", "SF Mono", Menlo, Monaco, Consolas, monospace;
  --apple-motion-press: 100ms;
  --apple-motion-fast: 160ms;
  --apple-motion-response: 320ms;
  --apple-motion-theme: 240ms;
  --apple-ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --apple-ease-standard: cubic-bezier(0.4, 0, 0.2, 1);
  --apple-material-blur: 24px;
  --apple-material-saturation: 150%;
  --apple-material-dark: rgba(11, 18, 32, 0.78);
  --apple-material-light: rgba(255, 255, 255, 0.78);
  --apple-material-edge-dark: rgba(255, 255, 255, 0.1);
  --apple-material-edge-light: rgba(15, 23, 42, 0.1);
  --apple-material-shadow: 0 22px 64px rgba(1, 5, 14, 0.24);
  --apple-origin-x: 50%;
  --apple-origin-y: 22%;
}

html {
  font-family: var(--apple-font-family);
  font-optical-sizing: auto;
  scroll-behavior: smooth;
}

body,
body * {
  font-family: var(--apple-font-family) !important;
  font-optical-sizing: auto;
}

body :is(code, pre, kbd, samp, .code, .monospace) {
  font-family: var(--apple-mono-family) !important;
}

body {
  letter-spacing: 0;
  text-rendering: optimizeLegibility;
}

body :is(h1, h2, h3, h4, h5, h6) {
  font-family: var(--apple-font-family) !important;
  text-wrap: balance;
}

body h1 {
  line-height: 1.06;
  letter-spacing: -0.032em;
}

body h2 {
  line-height: 1.1;
  letter-spacing: -0.026em;
}

body h3 {
  line-height: 1.16;
  letter-spacing: -0.02em;
}

body h4 {
  line-height: 1.22;
  letter-spacing: -0.014em;
}

body :is(h5, h6) {
  line-height: 1.28;
  letter-spacing: -0.008em;
}

body :is(p, li, dd, dt) {
  letter-spacing: 0;
}

body :is(.eyebrow, .dashboard-live-label, .sidebar-nav-group > summary, .admin-page-header .eyebrow) {
  letter-spacing: 0.085em;
}

body :is(
  button,
  .button,
  [role="button"],
  summary,
  .nav-pill,
  .tab,
  .profile-tab,
  .category-row,
  .search-result,
  .settings-quick-link,
  .dashboard-kpi-card,
  .job-card
) {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transform-origin: center;
}

body :is(
  button,
  .button,
  [role="button"],
  summary,
  .nav-pill,
  .tab,
  .profile-tab,
  .category-row,
  .search-result,
  .settings-quick-link,
  .dashboard-kpi-card,
  .job-card
):not(tr):active:not(:disabled):not([aria-disabled="true"]) {
  transform: scale(0.97) !important;
  transition-duration: var(--apple-motion-press) !important;
  transition-timing-function: ease-out !important;
  will-change: transform;
}

body :is(input, select, textarea, [contenteditable="true"]) {
  touch-action: manipulation;
}

body :is(.pwa-wrapper, .card, .panel, .shell, .builder-header, .builder-panel) {
  transition:
    color var(--apple-motion-fast) var(--apple-ease-standard),
    background-color var(--apple-motion-theme) var(--apple-ease-standard),
    border-color var(--apple-motion-theme) var(--apple-ease-standard),
    box-shadow var(--apple-motion-theme) var(--apple-ease-standard);
}

body[data-auth="in"] .panel-tools,
body.form-builder-page .builder-header,
.policy-page > header,
.portal-header {
  backdrop-filter: blur(var(--apple-material-blur)) saturate(var(--apple-material-saturation));
  -webkit-backdrop-filter: blur(var(--apple-material-blur)) saturate(var(--apple-material-saturation));
}

body[data-auth="in"] .panel-tools {
  position: sticky;
  top: 0;
  z-index: 24;
  padding: 0.58rem;
  border: 1px solid color-mix(in srgb, var(--border) 84%, transparent);
  border-top-color: color-mix(in srgb, var(--text) 10%, var(--border));
  border-radius: 16px;
  background: color-mix(in srgb, var(--panel) 78%, transparent);
  box-shadow: 0 8px 26px rgba(1, 5, 14, 0.08);
  transition:
    background-color var(--apple-motion-theme) var(--apple-ease-standard),
    border-color var(--apple-motion-theme) var(--apple-ease-standard),
    box-shadow var(--apple-motion-fast) var(--apple-ease-standard),
    transform var(--apple-motion-response) var(--apple-ease-out);
}

body[data-apple-scrolled="true"][data-auth="in"] .panel-tools {
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  box-shadow: 0 16px 42px rgba(1, 5, 14, 0.2);
}

.sidebar {
  background: color-mix(in srgb, var(--panel-elevated, var(--panel)) 84%, transparent) !important;
  backdrop-filter: blur(28px) saturate(145%);
  -webkit-backdrop-filter: blur(28px) saturate(145%);
  border-top-color: color-mix(in srgb, var(--text) 9%, var(--border)) !important;
}

:root[data-theme="light"] .sidebar,
:root[data-theme="light"] body[data-auth="in"] .panel-tools {
  background: rgba(255, 255, 255, 0.8) !important;
}

.card,
.panel,
.shell,
.builder-panel,
.policy-page > section {
  box-shadow:
    inset 0 1px 0 color-mix(in srgb, white 7%, transparent),
    var(--shadow-sm, var(--portal-shadow, var(--shadow, 0 16px 44px rgba(1, 5, 14, 0.12))));
}

:root[data-theme="light"] :is(.card, .panel, .shell, .builder-panel),
.policy-page > section,
.tracker-page .shell {
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.9),
    0 18px 52px rgba(15, 23, 42, 0.1);
}

.workspace-toolbar,
.profile-tabs,
.tab-row,
.table-pagination,
.view-tabs {
  background: color-mix(in srgb, var(--control-bg, var(--panel-alt, rgba(255, 255, 255, 0.08))) 80%, transparent);
  backdrop-filter: blur(14px) saturate(130%);
  -webkit-backdrop-filter: blur(14px) saturate(130%);
}

.modal,
.customer-profile {
  opacity: 1;
  transition: opacity var(--apple-motion-fast) var(--apple-ease-standard);
}

.modal[hidden],
.customer-profile[hidden],
body.form-builder-page .modal[aria-hidden="true"]:not([data-apple-closing="true"]) {
  opacity: 1;
  pointer-events: none;
}

.modal[data-apple-closing="true"],
.customer-profile[data-apple-closing="true"] {
  display: flex !important;
  opacity: 1;
  pointer-events: none;
}

.modal[data-apple-opening="true"],
.customer-profile[data-apple-opening="true"] {
  opacity: 0;
}

.modal[data-apple-opening-active="true"],
.customer-profile[data-apple-opening-active="true"] {
  opacity: 1;
}

.modal[data-apple-closing-active="true"],
.customer-profile[data-apple-closing-active="true"] {
  opacity: 0;
}

.modal-backdrop,
.customer-profile-backdrop {
  transition:
    opacity var(--apple-motion-fast) var(--apple-ease-standard),
    backdrop-filter var(--apple-motion-response) var(--apple-ease-out);
}

.modal-panel,
.customer-profile-panel,
.modal-dialog,
body.form-builder-page .modal .box {
  --apple-overlay-shift: 0.75rem;
  opacity: 1;
  filter: blur(0);
  transform: translateY(0) scale(1);
  transform-origin: var(--apple-origin-x) var(--apple-origin-y);
  transition:
    opacity var(--apple-motion-fast) var(--apple-ease-standard),
    filter var(--apple-motion-fast) var(--apple-ease-standard),
    transform var(--apple-motion-response) var(--apple-ease-out),
    background-color var(--apple-motion-theme) var(--apple-ease-standard),
    border-color var(--apple-motion-theme) var(--apple-ease-standard);
}

.modal-panel,
.customer-profile-panel {
  background: color-mix(in srgb, var(--panel) 92%, transparent) !important;
  backdrop-filter: blur(28px) saturate(145%);
  -webkit-backdrop-filter: blur(28px) saturate(145%);
}

.modal[hidden] :is(.modal-panel, .modal-dialog),
.customer-profile[hidden] .customer-profile-panel,
body.form-builder-page .modal[aria-hidden="true"]:not([data-apple-closing="true"]) .box {
  opacity: 1;
  filter: none;
  transform: translateY(0) scale(1);
  transition: none;
}

.modal[data-apple-closing="true"] :is(.modal-panel, .modal-dialog),
body.form-builder-page .modal[data-apple-closing="true"] .box,
.customer-profile[data-apple-closing="true"] .customer-profile-panel {
  opacity: 1;
  filter: none;
  transform: translateY(0) scale(1);
  transition:
    opacity var(--apple-motion-fast) var(--apple-ease-standard),
    filter var(--apple-motion-fast) var(--apple-ease-standard),
    transform 280ms var(--apple-ease-out);
}

.modal[data-apple-opening="true"] :is(.modal-panel, .modal-dialog),
body.form-builder-page .modal[data-apple-opening="true"] .box,
.customer-profile[data-apple-opening="true"] .customer-profile-panel {
  opacity: 0;
  filter: blur(5px);
  transform: translateY(var(--apple-overlay-shift)) scale(0.982);
}

.modal[data-apple-opening-active="true"] :is(.modal-panel, .modal-dialog),
body.form-builder-page .modal[data-apple-opening-active="true"] .box,
.customer-profile[data-apple-opening-active="true"] .customer-profile-panel {
  opacity: 1;
  filter: blur(0);
  transform: translateY(0) scale(1);
}

.modal[data-apple-closing-active="true"] :is(.modal-panel, .modal-dialog),
body.form-builder-page .modal[data-apple-closing-active="true"] .box,
.customer-profile[data-apple-closing-active="true"] .customer-profile-panel {
  opacity: 0;
  filter: blur(5px);
  transform: translateY(var(--apple-overlay-shift)) scale(0.982);
}

.sidebar,
.sidebar-overlay,
.content-area,
.sidebar-collapse,
.sidebar-collapse svg {
  transition-timing-function: var(--apple-ease-out) !important;
}

.sidebar {
  transition-duration: var(--apple-motion-response) !important;
}

.sidebar-overlay {
  transition-duration: var(--apple-motion-fast) !important;
}

.status-line:not(:empty),
.form-status:not(:empty) {
  display: inline-flex;
  align-items: flex-start;
  gap: 0.45rem;
  width: fit-content;
  max-width: 100%;
  min-height: 0;
  padding: 0.42rem 0.62rem;
  border: 1px solid color-mix(in srgb, currentColor 22%, transparent);
  border-radius: 10px;
  background: color-mix(in srgb, currentColor 7%, transparent);
  line-height: 1.42;
}

.status-line.success,
.form-status.is-success {
  color: #47cf94;
}

:root[data-theme="light"] .status-line.success,
:root[data-theme="light"] .form-status.is-success,
.policy-page .status-line.success {
  color: #147a52;
}

.status-line.error,
.form-status.is-error {
  color: var(--danger, var(--portal-danger, #e14c45));
}

.status-line.warning {
  color: #d99a16;
}

button[aria-busy="true"],
.button[aria-busy="true"] {
  cursor: progress;
}

button[aria-busy="true"]::after,
.button[aria-busy="true"]::after {
  content: "";
  width: 0.82rem;
  height: 0.82rem;
  flex: 0 0 auto;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  opacity: 0.72;
  animation: apple-progress-spin 700ms linear infinite;
}

.app-action-icon {
  width: 1.15rem;
  height: 1.15rem;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

body button.app-icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 44px !important;
  width: 44px;
  min-width: 44px;
  max-width: 44px;
  height: 44px;
  min-height: 44px;
  padding: 0 !important;
  gap: 0;
}

body button.app-icon-leading {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
}

body button.app-icon-button[aria-busy="true"] .app-action-icon {
  display: none;
}

@keyframes apple-progress-spin {
  to { transform: rotate(1turn); }
}

@media (max-width: 700px) {
  body[data-auth="in"] .panel-tools {
    position: relative;
    top: auto;
    z-index: auto;
  }

  .modal-panel,
  .customer-profile-panel,
  .modal-dialog,
  body.form-builder-page .modal .box {
    --apple-overlay-shift: 1rem;
    --apple-origin-x: 50%;
    --apple-origin-y: 100%;
  }
}

@media (pointer: coarse) {
  body :is(
    button,
    .button,
    [role="button"],
    summary,
    .nav-pill,
    .tab,
    .profile-tab,
    .view-tab,
    .category-row,
    .icon-button
  ) {
    min-height: 44px;
  }

  body :is(.icon-button, .sidebar-close, .sidebar-collapse) {
    min-width: 44px;
  }
}

@media (hover: none) {
  body :is(.button, button, .nav-pill, .dashboard-kpi-card):hover {
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
  }

  body :is(
    button,
    .button,
    [role="button"],
    summary,
    .nav-pill,
    .tab,
    .profile-tab,
    .category-row,
    .search-result,
    .settings-quick-link,
    .dashboard-kpi-card,
    .job-card
  ):active:not(:disabled):not([aria-disabled="true"]) {
    transform: none !important;
  }

  .modal,
  .customer-profile,
  .modal-backdrop,
  .customer-profile-backdrop,
  .modal-panel,
  .customer-profile-panel,
  .modal-dialog,
  body.form-builder-page .modal .box,
  .sidebar,
  .sidebar-overlay,
  .content-area {
    filter: none !important;
    transform: none !important;
    transition-duration: 120ms !important;
    transition-property: opacity, color, background-color, border-color, box-shadow !important;
  }

  .modal[hidden],
  .customer-profile[hidden],
  body.form-builder-page .modal[aria-hidden="true"] {
    display: none !important;
    transition: none !important;
  }
}

@media (prefers-reduced-transparency: reduce) {
  body[data-auth="in"] .panel-tools,
  .sidebar,
  .modal-panel,
  .customer-profile-panel,
  .modal-backdrop,
  .customer-profile-backdrop,
  .workspace-toolbar,
  .profile-tabs,
  .tab-row,
  .table-pagination,
  .view-tabs,
  body.form-builder-page .builder-header,
  .policy-page > header,
  .portal-header {
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }

  body[data-auth="in"] .panel-tools,
  .sidebar,
  .modal-panel,
  .customer-profile-panel {
    background: var(--panel, #0b1220) !important;
  }

  :root[data-theme="light"] body[data-auth="in"] .panel-tools,
  :root[data-theme="light"] .sidebar,
  :root[data-theme="light"] .modal-panel,
  :root[data-theme="light"] .customer-profile-panel,
  .policy-page > header {
    background: #ffffff !important;
  }
}

@media (prefers-contrast: more) {
  :root {
    --border: rgba(255, 255, 255, 0.3);
    --border-strong: rgba(255, 255, 255, 0.52);
    --muted: #c8d5e8;
  }

  :root[data-theme="light"] {
    --border: rgba(15, 23, 42, 0.28);
    --border-strong: rgba(15, 23, 42, 0.5);
    --muted: #334155;
  }

  body :is(button, .button, input, select, textarea, [tabindex]):focus-visible {
    outline-width: 3px !important;
    outline-offset: 3px !important;
  }

  body[data-auth="in"] .panel-tools,
  .sidebar,
  .modal-panel,
  .customer-profile-panel,
  .card,
  .panel,
  .shell {
    border-color: var(--border-strong) !important;
  }
}

@media (forced-colors: active) {
  body :is(button, .button, input, select, textarea, [tabindex]):focus-visible {
    outline: 2px solid Highlight !important;
  }

  .modal-backdrop,
  .customer-profile-backdrop {
    forced-color-adjust: none;
    background: rgba(0, 0, 0, 0.72) !important;
  }
}
