/* ==========================================================================
   Family Medicine Exam Prep: Mock UI Application Stylesheet
   Follows design-system principles: Figtree interface, Source Serif reading,
   petrol blue brand, semantic colors, and 44px minimum tap targets.
   ========================================================================== */

/* Layout Shell */
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  overflow-x: hidden;
  max-width: 100vw;
  width: 100%;
}

*, *::before, *::after {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}

/* Touch device guards: prevent sticky hover and tap highlights on options */
.samp-option-card {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

@media (hover: none) and (pointer: coarse) {
  /* On touchscreens, force non-selected option cards to maintain standard neutral background */
  .samp-option-card:not(.is-selected):not(.is-evaluated) {
    background-color: rgba(255, 255, 255, 0.03) !important;
    border-color: rgba(255, 255, 255, 0.09) !important;
  }
  /* Prevent sticky hover border/background on cards on touch devices */
  .ios-glass-card:not(:active),
  .ios-card:not(:active),
  .ds-panel:not(:active) {
    border-color: var(--ds-line) !important;
  }
}

body {
  display: flex;
  flex-direction: column;
  background: var(--ds-bg);
  color: var(--ds-ink);
  font-family: var(--ds-font-sans);
  font-size: var(--ds-text-md);
  line-height: var(--ds-leading-ui);
  -webkit-font-smoothing: antialiased;
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 40;
  height: 60px;
  background: color-mix(in srgb, var(--ds-bg) 92%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--ds-line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--ds-space-5);
  gap: var(--ds-space-4);
}

.app-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
  flex-shrink: 0;
}

.app-brand__logo {
  width: 34px;
  height: 34px;
  border-radius: var(--ds-r-control);
  background: var(--ds-brand);
  color: var(--ds-on-brand);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 1rem;
  letter-spacing: -0.02em;
}

.app-brand__title {
  font-size: 1rem;
  font-weight: 700;
  margin: 0;
  line-height: 1.1;
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--ds-ink);
}

.app-brand__text h1 {
  font-size: 1rem;
  font-weight: 700;
  margin: 0;
  line-height: 1.1;
  display: flex;
  align-items: center;
  gap: 6px;
}

.app-brand__text p {
  font-size: var(--ds-text-xs);
  color: var(--ds-ink-2);
  margin: 2px 0 0;
  line-height: 1;
}

/* Quick Search Trigger */
.app-search-trigger {
  flex: 1;
  max-width: 440px;
  min-height: 38px;
  border-radius: var(--ds-r-control);
  border: 1px solid var(--ds-line);
  background: var(--ds-surface);
  color: var(--ds-ink-2);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  font-size: var(--ds-text-sm);
  cursor: pointer;
  transition: border-color var(--ds-fast), background var(--ds-fast);
}

.app-search-trigger:hover {
  border-color: var(--ds-brand);
  color: var(--ds-ink);
}

.app-search-trigger kbd {
  background: var(--ds-surface-2);
  color: var(--ds-ink-2);
  font-family: var(--ds-font-sans);
  font-size: 0.72rem;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid var(--ds-line);
}

/* Shell & Workspace */
.app-shell {
  flex: 1;
  display: flex;
  overflow: hidden;
  height: calc(100dvh - 60px);
  min-height: calc(100vh - 60px);
  width: 100%;
  max-width: 100vw;
}

/* Sidebar */
.app-sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--ds-surface);
  border-right: 1px solid var(--ds-line);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: var(--ds-space-4) var(--ds-space-3);
  transition: width var(--ds-base), transform var(--ds-base);
}

.app-sidebar.is-collapsed {
  width: 0;
  padding: 0;
  overflow: hidden;
  border-right: 0;
}

.app-sidebar__group {
  margin-bottom: var(--ds-space-4);
}

.app-sidebar__title {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ds-ink-2);
  padding: 4px 10px;
  margin-bottom: 2px;
}

.app-nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: var(--ds-tap);
  padding: 0 12px;
  border-radius: var(--ds-r-control);
  color: var(--ds-ink-2);
  font-size: 0.92rem;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: background var(--ds-fast), color var(--ds-fast);
  border: 0;
  background: transparent;
  width: 100%;
  text-align: left;
}

.app-nav-item:hover {
  background: var(--ds-surface-2);
  color: var(--ds-ink);
}

.app-nav-item.is-active {
  background: var(--ds-brand-tint);
  color: var(--ds-brand);
  font-weight: 600;
}

.app-nav-item .ds-icon {
  font-size: 1.15rem;
}

.app-sidebar__footer {
  margin-top: auto;
  padding-top: var(--ds-space-3);
  border-top: 1px solid var(--ds-line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: var(--ds-text-xs);
  color: var(--ds-ink-2);
  padding-left: 8px;
  padding-right: 8px;
}

/* Main Content Area */
.app-main {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: var(--ds-space-6) var(--ds-space-6) var(--ds-space-11);
  background: var(--ds-bg);
  width: 100%;
  max-width: 100vw;
}

.app-container {
  max-width: var(--ds-page-max);
  margin: 0 auto;
  width: 100%;
}

/* Section Header */
.app-view-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--ds-space-3);
  margin-bottom: var(--ds-space-5);
  border-bottom: 1px solid var(--ds-line);
  padding-bottom: var(--ds-space-4);
}

.app-view-header h2 {
  font-size: var(--ds-text-xl);
  font-weight: 700;
  letter-spacing: -0.015em;
  margin: 0;
  line-height: 1.2;
}

.app-view-header p {
  color: var(--ds-ink-2);
  font-size: 0.92rem;
  margin: 4px 0 0;
}

/* Metric Widgets Grid */
.app-metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--ds-space-3);
  margin-bottom: var(--ds-space-5);
}

.app-metric-card {
  background: var(--ds-surface);
  border: 1px solid var(--ds-line);
  border-radius: var(--ds-r-card);
  padding: var(--ds-space-4);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.app-metric-card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--ds-ink-2);
  font-size: var(--ds-text-xs);
  font-weight: 600;
  margin-bottom: 8px;
}

.app-metric-card__value {
  font-size: 1.8rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  margin-bottom: 4px;
}

.app-metric-card__sub {
  font-size: var(--ds-text-xs);
  color: var(--ds-ink-2);
}

/* Modals */
.app-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(16, 38, 43, 0.45);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 60px 16px 20px;
  overflow-y: auto;
}

.app-modal {
  width: 100%;
  max-width: 600px;
  background: var(--ds-surface);
  border: 1px solid var(--ds-line);
  border-radius: var(--ds-r-panel);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
  overflow: hidden;
  animation: app-fade-in var(--ds-base) ease-out;
}

.app-modal--lg {
  max-width: 860px;
}

@keyframes app-fade-in {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* 3D Flashcard Component */
.flashcard-wrap {
  perspective: 1200px;
  width: 100%;
  max-width: 680px;
  margin: 0 auto;
}

.flashcard {
  position: relative;
  width: 100%;
  min-height: 380px;
  border-radius: var(--ds-r-panel);
  border: 1px solid var(--ds-line);
  background: var(--ds-surface);
  cursor: pointer;
  transform-style: preserve-3d;
  transition: transform 0.4s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.flashcard.is-flipped {
  transform: rotateY(180deg);
}

.flashcard__face {
  position: absolute;
  inset: 0;
  padding: var(--ds-space-6);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  border-radius: inherit;
  background: inherit;
}

.flashcard__face--back {
  transform: rotateY(180deg);
  background: var(--ds-surface);
  border-top: 3px solid var(--ds-brand);
}

/* Spotters & ECG Calipers */
.ecg-canvas-container {
  background: var(--ds-surface);
  border: 1px solid var(--ds-line);
  border-radius: var(--ds-r-card);
  position: relative;
  overflow: hidden;
  min-height: 220px;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
}

.ecg-grid {
  background-image:
    linear-gradient(to right, rgba(255, 120, 120, 0.2) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(255, 120, 120, 0.2) 1px, transparent 1px),
    linear-gradient(to right, rgba(255, 80, 80, 0.4) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(255, 80, 80, 0.4) 1px, transparent 1px);
  background-size: 5px 5px, 5px 5px, 25px 25px, 25px 25px;
}

.caliper-line {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--ds-brand);
  cursor: ew-resize;
  z-index: 10;
}

.caliper-badge {
  position: absolute;
  top: 8px;
  padding: 4px 8px;
  background: var(--ds-brand);
  color: #fff;
  font-size: var(--ds-text-xs);
  font-weight: 600;
  border-radius: 4px;
  pointer-events: none;
}

/* Audio Simulation for SOO Live */
.audio-visualizer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 48px;
  margin: 12px 0;
}

.audio-bar, .audio-visualizer span {
  width: 4px;
  height: 8px;
  border-radius: var(--ds-r-pill);
  background: var(--ds-brand);
  transition: height 0.15s ease;
}

.audio-visualizer.is-active .audio-bar,
.audio-visualizer.is-active span {
  animation: audio-bounce 0.8s ease-in-out infinite alternate;
}

.audio-visualizer .audio-bar:nth-child(2), .audio-visualizer span:nth-child(2) { animation-delay: 0.15s; }
.audio-visualizer .audio-bar:nth-child(3), .audio-visualizer span:nth-child(3) { animation-delay: 0.3s; }
.audio-visualizer .audio-bar:nth-child(4), .audio-visualizer span:nth-child(4) { animation-delay: 0.45s; }
.audio-visualizer .audio-bar:nth-child(5), .audio-visualizer span:nth-child(5) { animation-delay: 0.2s; }
.audio-visualizer .audio-bar:nth-child(6), .audio-visualizer span:nth-child(6) { animation-delay: 0.35s; }

@keyframes audio-bounce {
  from { height: 8px; }
  to { height: 38px; }
}

/* Live dialogue bubble */
.dialogue-stream {
  display: grid;
  gap: 12px;
  max-height: min(380px, 40dvh);
  overflow-y: auto;
  padding: 12px 0;
}

.dialogue-bubble {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: var(--ds-r-card);
  font-size: 0.94rem;
  line-height: 1.5;
}

.dialogue-bubble--patient {
  align-self: flex-start;
  background: var(--ds-surface-2);
  border: 1px solid var(--ds-line);
  color: var(--ds-ink);
  border-bottom-left-radius: 4px;
}

.dialogue-bubble--doctor {
  align-self: flex-end;
  background: var(--ds-brand-tint);
  border: 1px solid var(--ds-brand);
  color: var(--ds-brand);
  border-bottom-right-radius: 4px;
  margin-left: auto;
}

/* Responsive & Mobile Drawer */
.mobile-drawer {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(16, 38, 43, 0.5);
  display: none;
  opacity: 0;
  transition: opacity var(--ds-base);
}

.mobile-drawer.is-open {
  display: block;
  opacity: 1;
}

.mobile-drawer__panel {
  width: 280px;
  height: 100%;
  background: var(--ds-surface);
  border-right: 1px solid var(--ds-line);
  padding: var(--ds-space-4);
  overflow-y: auto;
  animation: drawer-slide var(--ds-base) ease-out;
}

@keyframes drawer-slide {
  from { transform: translateX(-100%); }
  to { transform: translateX(0); }
}

/* ==========================================================================
   Responsive & Adaptive Breakpoints
   ========================================================================== */

/* Tablet & Mobile Breakpoint (<= 900px) */
@media (max-width: 900px) {
  .app-sidebar {
    display: none !important;
  }
  #mobile-menu-btn {
    display: inline-grid !important;
  }
  .header-desktop-only {
    display: none !important;
  }
  .header-badge-sitting {
    display: none !important;
  }
  .dashboard-grid, .soo-grid, .mock-setup-grid, .reports-grid {
    grid-template-columns: 1fr !important;
  }
  .mock-circuit-grid, .error-reflection-grid, .foundations-grid {
    grid-template-columns: 1fr !important;
  }
  /* Raise header buttons to 44px touch target on tablet/touch */
  .ds-btn--sm {
    min-height: var(--ds-tap);
    padding: 0 14px;
  }
  /* Segment / filter buttons: meet 44px touch target */
  .ds-seg button {
    min-height: var(--ds-tap);
    padding: 0 14px;
  }
  .ds-chip {
    min-height: var(--ds-tap);
  }
  /* Hide search bar at sidebar-break point, not 768px */
  .search-text-full {
    display: none !important;
  }
  .search-text-short {
    display: inline !important;
  }
  .header-kbd {
    display: none !important;
  }
  .app-search-trigger {
    max-width: 200px;
    padding: 0 10px;
  }
}

/* Intermediate Tablet / Phablet (<= 768px) */
@media (max-width: 768px) {
  .app-search-trigger {
    max-width: 180px;
    padding: 0 10px;
  }
}

/* Mobile Handheld Devices (<= 640px) */
@media (max-width: 640px) {
  .app-header {
    height: 54px;
    padding: 0 12px;
  }
  .app-brand__sub {
    display: none !important;
  }
  .app-search-trigger {
    display: none !important;
  }
  #mobile-search-btn {
    display: inline-grid !important;
  }
  .app-shell {
    height: calc(100dvh - 54px);
    min-height: calc(100vh - 54px);
  }
  .app-main {
    padding: 12px 10px 40px !important;
  }
  .app-container {
    padding: 0;
  }
  .ds-panel {
    padding: 16px 14px !important;
    border-radius: var(--ds-r-card) !important;
  }
  .ds-panel--tinted {
    padding: 14px 12px !important;
  }
  .app-view-header {
    margin-bottom: 14px;
    padding-bottom: 10px;
    gap: 8px;
  }
  .app-view-header h2 {
    font-size: 1.25rem !important;
  }
  .app-metrics-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
    margin-bottom: 14px !important;
  }
  .app-metric-card {
    padding: 10px 12px !important;
  }
  .app-metric-card__value {
    font-size: 1.35rem !important;
  }
  .ds-mosaic {
    gap: 2.5px !important;
  }
  .ds-mosaic button {
    min-width: 0 !important;
    width: 100% !important;
  }
  .samp-cases-grid {
    grid-template-columns: 1fr !important;
  }
  .reports-metrics-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
  }
  .fsrs-buttons-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
  }
  /* Horizontal scrolling segmented tabs on mobile */
  .ds-seg-scrollable {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
    scrollbar-width: none;
    padding: 3px !important;
  }
  .ds-seg-scrollable::-webkit-scrollbar {
    display: none;
  }
  .ds-seg-scrollable button {
    flex-shrink: 0;
    white-space: nowrap;
    padding: 6px 12px !important;
    font-size: 0.84rem !important;
  }
  /* Modals on mobile */
  .app-modal-backdrop {
    padding: 12px !important;
    align-items: center !important;
  }
  .app-modal {
    max-height: 88dvh;
    overflow-y: auto;
  }
  /* Flashcard min-height */
  .flashcard {
    min-height: 400px !important;
  }
  .flashcard-face {
    padding: 16px 14px !important;
  }
  /* Confidence Bar */
  .confidence-bar {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 8px !important;
  }
}

/* Ultra-compact Mobile (<= 400px) */
@media (max-width: 400px) {
  .app-metrics-grid {
    grid-template-columns: 1fr !important;
  }
  .reports-metrics-grid {
    grid-template-columns: 1fr !important;
  }
}

/* Print Styles */
@media print {
  .app-header, .app-sidebar, .app-nav-item, .ds-btn, .no-print {
    display: none !important;
  }
  .app-main {
    padding: 0 !important;
    background: #fff !important;
    color: #000 !important;
  }
  .ds-panel {
    border: 1px solid #ccc !important;
  }
}
