/**
 * Master Codex Financial - CSS Layer
 * 140 Micro-Interactions for Marlowe Partners
 * Mobile-First, iOS/Safari Optimized
 * All classes namespaced with .cdx-
 */

/* ==========================================================================
   PART 0: FOUNDATION & iOS/SAFARI CONSTRAINTS
   ========================================================================== */

/* Remove grey tap highlight on iOS */
* {
  -webkit-tap-highlight-color: transparent;
}

/* Prevent double-tap zoom delay */
.cdx-interactive,
button,
a,
input,
select,
textarea,
[role="button"] {
  touch-action: manipulation;
  cursor: pointer;
}

/* Safe area insets for iPhone notch/home bar */
.cdx-safe-bottom {
  padding-bottom: env(safe-area-inset-bottom, 20px);
}

.cdx-safe-top {
  padding-top: env(safe-area-inset-top, 20px);
}

/* ==========================================================================
   PART 1: EFFICIENCY LAYER CSS (1-50)
   ========================================================================== */

/* #2 Quick Actions FAB */
.cdx-fab {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  left: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  color: #fff;
  border: none;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2147483640;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.cdx-fab:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(255, 255, 255, 0.15);
}

.cdx-fab:active {
  transform: scale(0.95);
}

/* #3 Command Palette Modal */
.cdx-command-palette {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 15vh;
  z-index: 2147483647;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

.cdx-command-palette.cdx-active {
  opacity: 1;
  visibility: visible;
}

.cdx-command-palette-inner {
  width: 90%;
  max-width: 560px;
  background: #1a1a2e;
  border-radius: 12px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.5);
  overflow: hidden;
  transform: translateY(-20px);
  transition: transform 0.2s ease;
}

.cdx-command-palette.cdx-active .cdx-command-palette-inner {
  transform: translateY(0);
}

.cdx-command-input {
  width: 100%;
  padding: 16px 20px;
  background: transparent;
  border: none;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  color: #fff;
  font-size: 18px;
  outline: none;
}

.cdx-command-input::placeholder {
  color: rgba(255, 255, 255, 0.4);
}

/* #10 Toast System */
.cdx-toast-container {
  position: fixed;
  bottom: calc(80px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  z-index: 2147483647;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.cdx-toast {
  background: #1a1a2e;
  color: #fff;
  padding: 12px 24px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  font-size: 14px;
  animation: cdx-toast-pop 0.3s ease forwards;
  pointer-events: auto;
}

@keyframes cdx-toast-pop {
  0% {
    opacity: 0;
    transform: translateY(20px) scale(0.9);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.cdx-toast.cdx-toast-exit {
  animation: cdx-toast-exit 0.2s ease forwards;
}

@keyframes cdx-toast-exit {
  0% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  100% {
    opacity: 0;
    transform: translateY(-10px) scale(0.9);
  }
}

/* #12 Greeter */
.cdx-greeter {
  font-size: 1.25rem;
  font-weight: 300;
  color: rgba(255, 255, 255, 0.7);
  margin-bottom: 0.5rem;
}

/* #18 Onboarding Checklist */
.cdx-onboarding {
  position: fixed;
  bottom: calc(100px + env(safe-area-inset-bottom, 0px));
  right: 20px;
  width: 300px;
  background: #1a1a2e;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
  z-index: 2147483640;
  overflow: hidden;
}

.cdx-onboarding-header {
  padding: 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  font-weight: 600;
}

.cdx-onboarding-item {
  padding: 12px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  display: flex;
  align-items: center;
  gap: 12px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
}

.cdx-onboarding-item.cdx-done {
  color: rgba(255, 255, 255, 0.4);
  text-decoration: line-through;
}

/* #22 Zen Mode */
.cdx-zen-mode body > *:not(main):not(article):not(.cdx-zen-content) {
  opacity: 0.1 !important;
  pointer-events: none !important;
}

/* #23 Read Time */
.cdx-read-time {
  font-size: 0.875rem;
  color: rgba(255, 255, 255, 0.5);
  margin-bottom: 1rem;
}

/* #31 Night Shift */
.cdx-night-shift {
  filter: sepia(20%) saturate(90%);
}

/* #32 Pulse Loader */
.cdx-pulse {
  animation: cdx-pulse 1.5s ease-in-out infinite;
}

@keyframes cdx-pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

/* #33 Skeleton UI */
.cdx-skeleton {
  background: linear-gradient(90deg, #2a2a3e 25%, #3a3a4e 50%, #2a2a3e 75%);
  background-size: 200% 100%;
  animation: cdx-skeleton-shimmer 1.5s infinite;
  border-radius: 4px;
}

@keyframes cdx-skeleton-shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* #42 Print Styles */
@media print {
  .cdx-no-print,
  nav,
  footer,
  .cdx-fab,
  .cdx-toast-container,
  .cdx-onboarding {
    display: none !important;
  }
  
  body {
    background: #fff !important;
    color: #000 !important;
  }
}

/* #45 Confetti Canvas */
.cdx-confetti-canvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2147483647;
}

/* ==========================================================================
   PART 2: POLISH LAYER CSS (51-100)
   ========================================================================== */

/* #51 Hover Lift */
.cdx-lift {
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.cdx-lift:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* #52 Active Press */
.cdx-press:active {
  transform: scale(0.98);
}

/* #53 Focus Glow */
.cdx-focus-glow:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.4);
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.25);
}

/* #54 Skeleton Animation (see #33) */

/* #55 Menu Slide */
.cdx-menu-slide {
  opacity: 0;
  transform: translateY(-8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
}

.cdx-menu-slide.cdx-active {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

/* #56 Cursor Hand */
button,
[role="button"],
.cdx-clickable {
  cursor: pointer;
}

/* #57 Link Fade */
a {
  transition: color 0.2s ease, opacity 0.2s ease;
}

a:hover {
  opacity: 0.8;
}

/* #58 Sticky Head */
.cdx-sticky {
  position: sticky;
  top: 0;
  z-index: 100;
  background: inherit;
}

/* #59 Toast Pop (see #10) */

/* #60 Progress Bar */
.cdx-progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
  z-index: 2147483647;
  transition: width 0.1s ease;
}

/* #61 Line Height */
.cdx-readable {
  line-height: 1.6;
}

/* #62 Line Width */
.cdx-prose {
  max-width: 75ch;
}

/* #63 Hierarchy */
.cdx-h1 { font-size: 2.5rem; font-weight: 700; line-height: 1.2; }
.cdx-h2 { font-size: 2rem; font-weight: 600; line-height: 1.3; }
.cdx-h3 { font-size: 1.75rem; font-weight: 600; line-height: 1.4; }
.cdx-h4 { font-size: 1.5rem; font-weight: 500; line-height: 1.4; }

/* #64 Contrast */
.cdx-high-contrast {
  color: #222;
  background: #fff;
}

/* #65 System Fonts */
.cdx-system-font {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* #68 Link Style */
.cdx-link {
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

/* #69 Whitespace */
.cdx-mb-1 { margin-bottom: 0.25rem; }
.cdx-mb-2 { margin-bottom: 0.5rem; }
.cdx-mb-3 { margin-bottom: 1rem; }
.cdx-mb-4 { margin-bottom: 1.5rem; }
.cdx-mb-5 { margin-bottom: 2rem; }

/* #76 Error Text */
.cdx-error-text {
  color: #ef4444;
  font-size: 0.75rem;
  margin-top: 4px;
}

/* #77 Btn Primary */
.cdx-btn-primary {
  background: #1a1a2e;
  color: #fff;
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  font-weight: 500;
  transition: background 0.2s ease, transform 0.2s ease;
}

.cdx-btn-primary:hover {
  background: #16213e;
}

.cdx-btn-primary:active {
  transform: scale(0.98);
}

/* #78 Touch Target */
.cdx-touch-target {
  min-height: 44px;
  min-width: 44px;
}

/* #79 Label Float */
.cdx-float-label {
  position: relative;
}

.cdx-float-label input {
  padding-top: 20px;
}

.cdx-float-label label {
  position: absolute;
  top: 50%;
  left: 12px;
  transform: translateY(-50%);
  font-size: 14px;
  color: rgba(255, 255, 255, 0.5);
  transition: all 0.2s ease;
  pointer-events: none;
}

.cdx-float-label input:focus + label,
.cdx-float-label input:not(:placeholder-shown) + label {
  top: 8px;
  font-size: 11px;
  transform: translateY(0);
}

/* #82 Visited */
a:visited {
  opacity: 0.85;
}

/* #83 Empty State */
.cdx-empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3rem;
  text-align: center;
  color: rgba(255, 255, 255, 0.5);
}

.cdx-empty-state svg {
  width: 120px;
  height: 120px;
  margin-bottom: 1rem;
  opacity: 0.3;
}

/* #84 Custom 404 */
.cdx-404 {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #0a0a0f;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 2147483647;
}

.cdx-404-code {
  font-size: 8rem;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.1);
  line-height: 1;
}

.cdx-404-text {
  font-size: 1.25rem;
  color: rgba(255, 255, 255, 0.6);
  margin-top: 1rem;
}

/* #86 Back to Top */
.cdx-back-top {
  position: fixed;
  bottom: calc(100px + env(safe-area-inset-bottom, 0px));
  right: 20px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease, transform 0.2s ease;
  z-index: 2147483640;
}

.cdx-back-top.cdx-visible {
  opacity: 1;
  visibility: visible;
}

.cdx-back-top:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translateY(-2px);
}

/* #91 Lazy Class */
.cdx-lazy {
  filter: blur(10px);
  transition: filter 0.3s ease;
}

.cdx-lazy.cdx-loaded {
  filter: blur(0);
}

/* #92 Img Ratio */
.cdx-ratio-16-9 {
  aspect-ratio: 16 / 9;
  object-fit: cover;
}

.cdx-ratio-4-3 {
  aspect-ratio: 4 / 3;
  object-fit: cover;
}

.cdx-ratio-1-1 {
  aspect-ratio: 1 / 1;
  object-fit: cover;
}

/* #93 Alt Warn (Dev Mode) */
.cdx-dev-mode img:not([alt]),
.cdx-dev-mode img[alt=""] {
  outline: 3px solid #ef4444 !important;
}

/* #94 Tablets */
@media (min-width: 768px) {
  .cdx-tablet-hide {
    display: none;
  }
  
  .cdx-tablet-show {
    display: block;
  }
}

/* #95 Fast Tap (see foundation) */

/* #97 Target Blank Icon */
a[target="_blank"]::after {
  content: " ↗";
  font-size: 0.75em;
  opacity: 0.5;
}

/* #100 Selection */
::selection {
  background: rgba(102, 126, 234, 0.3);
  color: inherit;
}

::-moz-selection {
  background: rgba(102, 126, 234, 0.3);
  color: inherit;
}

/* ==========================================================================
   PART 3: ANTICIPATORY LAYER CSS (101-120)
   ========================================================================== */

/* #101 Velocity Type */
.cdx-velocity-fast h1,
.cdx-velocity-fast h2,
.cdx-velocity-fast h3 {
  font-weight: 800;
}

/* #102 Trajectory Glow */
.cdx-trajectory-glow {
  box-shadow: 0 0 20px rgba(102, 126, 234, 0.4);
}

/* #103 Magnet Cursor */
.cdx-magnet {
  transition: transform 0.15s ease;
}

/* #106 Circadian BG */
.cdx-circadian-morning {
  background-color: #fef3c7 !important;
}

.cdx-circadian-day {
  background-color: #fff !important;
}

.cdx-circadian-evening {
  background-color: #fef3c7 !important;
  filter: sepia(10%);
}

.cdx-circadian-night {
  background-color: #0a0a0f !important;
}

/* #107 Focus Tunnel */
.cdx-focus-tunnel *:not(:focus):not(:focus-within) {
  opacity: 0.3;
}

.cdx-focus-tunnel *:focus,
.cdx-focus-tunnel *:focus-within {
  opacity: 1;
}

/* #110 Density (Compact Mode) */
:root.cdx-compact {
  --cdx-spacer: 0.5rem;
}

:root:not(.cdx-compact) {
  --cdx-spacer: 1rem;
}

/* #111 Patina */
.cdx-patina-1 { filter: brightness(0.98); }
.cdx-patina-2 { filter: brightness(0.96); }
.cdx-patina-3 { filter: brightness(0.94); }
.cdx-patina-4 { filter: brightness(0.92); }
.cdx-patina-5 { filter: brightness(0.90); }

/* #113 Wait Cursor */
.cdx-waiting,
.cdx-waiting * {
  cursor: progress !important;
}

/* #116 Link Peek */
.cdx-link-peek {
  position: absolute;
  width: 320px;
  height: 200px;
  background: #1a1a2e;
  border-radius: 8px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  overflow: hidden;
  z-index: 2147483647;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

.cdx-link-peek.cdx-active {
  opacity: 1;
  visibility: visible;
}

.cdx-link-peek iframe {
  width: 100%;
  height: 100%;
  border: none;
  transform: scale(0.5);
  transform-origin: top left;
  width: 200%;
  height: 200%;
}

/* #117 Changelog Badge */
.cdx-new-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 2px 6px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  font-size: 10px;
  font-weight: 600;
  border-radius: 4px;
  text-transform: uppercase;
  margin-left: 8px;
}

/* ==========================================================================
   PART 4: PSYCHOLOGICAL CODEX CSS (121-140)
   ========================================================================== */

/* #121 Reverse Download Modal */
.cdx-download-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2147483647;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.cdx-download-modal.cdx-active {
  opacity: 1;
  visibility: visible;
}

.cdx-download-modal-content {
  background: #1a1a2e;
  padding: 2rem;
  border-radius: 12px;
  max-width: 400px;
  text-align: center;
}

/* #122 Welcome Prime Modal */
.cdx-welcome-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.9);
  backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2147483647;
}

.cdx-welcome-content {
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  padding: 3rem;
  border-radius: 16px;
  max-width: 480px;
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

/* #123 Exit Value Overlay */
.cdx-exit-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.95);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2147483647;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.cdx-exit-overlay.cdx-active {
  opacity: 1;
  visibility: visible;
}

/* #124 Identity Toggle */
.cdx-long-duration .cdx-noise {
  filter: blur(4px);
  opacity: 0.5;
}

/* #125 Manifesto Gate */
.cdx-gated-content {
  filter: blur(8px);
  pointer-events: none;
  user-select: none;
}

.cdx-gated-content.cdx-unlocked {
  filter: none;
  pointer-events: auto;
  user-select: auto;
}

/* #128 Silent Badge */
.cdx-silent-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 4px;
  font-size: 11px;
  color: rgba(255, 255, 255, 0.6);
  font-style: italic;
}

/* #129 Peer Notes */
.cdx-peer-notes {
  font-family: "Caveat", cursive;
  font-size: 14px;
  color: rgba(255, 200, 100, 0.8);
  position: absolute;
  right: -200px;
  width: 180px;
  transform: rotate(-2deg);
}

/* #130 Empty Chair */
.cdx-empty-chair {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.4);
  font-style: italic;
}

/* #131 Quiet Room */
.cdx-quiet-room {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: #f5f0e8;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2147483647;
  padding: 2rem;
}

.cdx-quiet-room-content {
  max-width: 600px;
  text-align: center;
  color: #2a2a2e;
  font-family: "Crimson Text", Georgia, serif;
  font-size: 1.25rem;
  line-height: 1.8;
}

/* #132 Disqualify Warning */
.cdx-disqualify {
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 1rem;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
}

/* #133 Dated Window */
.cdx-window-closed {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 2rem;
  text-align: center;
  color: rgba(255, 255, 255, 0.5);
}

/* #134 Underwriting Mode */
.cdx-underwriting table,
.cdx-underwriting .highcharts-container,
.cdx-underwriting .recharts-wrapper,
.cdx-underwriting svg {
  filter: grayscale(100%);
  font-family: "IBM Plex Mono", monospace;
}

/* #135 Legacy Font */
.cdx-legacy-font {
  font-family: "Crimson Text", Georgia, serif;
}

/* #137 Privacy Banner */
.cdx-privacy-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.9);
  padding: 12px;
  text-align: center;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.6);
  z-index: 2147483640;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
}

/* #138 Pacing Tooltip */
.cdx-pacing-tooltip {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.9);
  padding: 16px 24px;
  border-radius: 8px;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
  z-index: 2147483647;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.cdx-pacing-tooltip.cdx-active {
  opacity: 1;
  visibility: visible;
}

/* #140 Patient Tag */
.cdx-patient-tag {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.4);
  letter-spacing: 0.5px;
  text-transform: uppercase;
}

/* ==========================================================================
   UTILITY CLASSES
   ========================================================================== */

.cdx-hidden { display: none !important; }
.cdx-visible { display: block !important; }
.cdx-flex { display: flex !important; }
.cdx-center { align-items: center; justify-content: center; }
.cdx-gap-1 { gap: 0.25rem; }
.cdx-gap-2 { gap: 0.5rem; }
.cdx-gap-3 { gap: 1rem; }
.cdx-gap-4 { gap: 1.5rem; }
