/* SleepSimSocial - Global Styles */
/* Design System: Sleep lab at 2 AM energy */

/* CSS Variables */
:root {
  /* Base viewport unit - everything derives from this */
  --vw: 1vw;

  /* Colors */
  --color-bg: #0f0f1a;
  --color-surface: #1a1a2e;
  --color-surface-elevated: #252540;
  --color-primary: #4a9eff;
  --color-primary-hover: #3a8eef;
  --color-success: #22c55e;
  --color-error: #ef4444;
  --color-text: #e2e8f0;
  --color-text-muted: #94a3b8;
  --color-border: #2a2a4a;

  /* Typography - Viewport-based with minimum sizes for readability */
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-size-xs: max(calc(var(--vw) * 0.625), 10px);   /* ~12px at 1920 */
  --font-size-sm: max(calc(var(--vw) * 0.73), 11px);    /* ~14px at 1920 */
  --font-size-base: max(calc(var(--vw) * 0.83), 13px);  /* ~16px at 1920 */
  --font-size-lg: max(calc(var(--vw) * 0.94), 14px);    /* ~18px at 1920 */
  --font-size-xl: max(calc(var(--vw) * 1.04), 15px);    /* ~20px at 1920 */
  --font-size-2xl: max(calc(var(--vw) * 1.25), 17px);   /* ~24px at 1920 */
  --font-size-3xl: max(calc(var(--vw) * 1.67), 20px);   /* ~32px at 1920 */

  /* Spacing - Viewport-based */
  --space-1: calc(var(--vw) * 0.21);   /* ~4px at 1920 */
  --space-2: calc(var(--vw) * 0.42);   /* ~8px at 1920 */
  --space-3: calc(var(--vw) * 0.625);  /* ~12px at 1920 */
  --space-4: calc(var(--vw) * 0.83);   /* ~16px at 1920 */
  --space-5: calc(var(--vw) * 1.04);   /* ~20px at 1920 */
  --space-6: calc(var(--vw) * 1.25);   /* ~24px at 1920 */
  --space-8: calc(var(--vw) * 1.67);   /* ~32px at 1920 */
  --space-10: calc(var(--vw) * 2.08);  /* ~40px at 1920 */
  --space-12: calc(var(--vw) * 2.5);   /* ~48px at 1920 */

  /* Size presets for common UI elements */
  --size-icon: calc(var(--vw) * 1.25);      /* ~24px at 1920 */
  --size-icon-lg: calc(var(--vw) * 1.67);   /* ~32px at 1920 */
  --size-icon-xl: calc(var(--vw) * 2.5);    /* ~48px at 1920 */
  --size-card: calc(var(--vw) * 14);        /* ~269px at 1920 */

  /* Border Radius - Viewport-based */
  --radius-sm: calc(var(--vw) * 0.21);  /* ~4px at 1920 */
  --radius-md: calc(var(--vw) * 0.42);  /* ~8px at 1920 */
  --radius-lg: calc(var(--vw) * 0.625); /* ~12px at 1920 */
  --radius-xl: calc(var(--vw) * 0.83);  /* ~16px at 1920 */
  --radius-2xl: calc(var(--vw) * 1.25); /* ~24px at 1920 */

  /* Shadows - Keep px values for crisp visual effects */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.3);
  --shadow-glow: 0 0 20px rgba(74, 158, 255, 0.15);

  /* Transitions */
  --transition-fast: 150ms ease;
  --transition-normal: 250ms ease;

  /* Safe areas for notched phones */
  --safe-area-top: env(safe-area-inset-top, 0px);
  --safe-area-bottom: env(safe-area-inset-bottom, 0px);
  --safe-area-left: env(safe-area-inset-left, 0px);
  --safe-area-right: env(safe-area-inset-right, 0px);

  /* Touch target minimum - viewport-based with minimum */
  --touch-target: max(calc(var(--vw) * 2.3), 44px);
}

/* Reset */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-family);
  font-size: var(--font-size-base);
  line-height: 1.5;
  color: var(--color-text);
  background-color: var(--color-bg);
  min-height: 100vh;
  min-height: 100dvh; /* Dynamic viewport height for mobile */
  overflow-x: hidden;
  padding-top: var(--safe-area-top);
  padding-bottom: var(--safe-area-bottom);
  padding-left: var(--safe-area-left);
  padding-right: var(--safe-area-right);
}

/* Typography */
h1, h2, h3, h4, h5, h6 {
  font-weight: 600;
  line-height: 1.2;
  word-wrap: break-word;
}

h1 { font-size: var(--font-size-3xl); }
h2 { font-size: var(--font-size-2xl); }
h3 { font-size: var(--font-size-xl); }

p {
  color: var(--color-text-muted);
  word-wrap: break-word;
}

a {
  color: var(--color-primary);
  text-decoration: none;
  transition: color var(--transition-fast);
}

a:hover {
  color: var(--color-primary-hover);
}

/* Improve touch targets */
a, button, input, select, textarea {
  touch-action: manipulation;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-3) var(--space-6);
  font-family: var(--font-family);
  font-size: var(--font-size-base);
  font-weight: 500;
  line-height: 1;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  text-decoration: none;
  min-height: var(--touch-target);
  min-width: var(--touch-target);
  user-select: none;
  -webkit-user-select: none;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background-color: var(--color-primary);
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background-color: var(--color-primary-hover);
  box-shadow: var(--shadow-glow);
}

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

.btn-secondary {
  background-color: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
}

.btn-secondary:hover:not(:disabled) {
  background-color: var(--color-surface-elevated);
  border-color: var(--color-primary);
}

.btn-secondary:active:not(:disabled) {
  transform: scale(0.98);
}

.btn-success {
  background-color: var(--color-success);
  color: white;
}

.btn-error {
  background-color: var(--color-error);
  color: white;
}

.btn-sm {
  padding: var(--space-2) var(--space-4);
  font-size: var(--font-size-sm);
  min-height: calc(var(--vw) * 1.88); /* ~36px at 1920 */
}

.btn-lg {
  padding: var(--space-4) var(--space-8);
  font-size: var(--font-size-lg);
}

/* Forms */
.form-group {
  margin-bottom: var(--space-4);
}

.form-label {
  display: block;
  margin-bottom: var(--space-2);
  font-size: var(--font-size-sm);
  font-weight: 500;
  color: var(--color-text);
}

.form-input {
  width: 100%;
  padding: var(--space-3) var(--space-4);
  font-family: var(--font-family);
  font-size: var(--font-size-base);
  color: var(--color-text);
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
  min-height: var(--touch-target);
}

.form-input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(74, 158, 255, 0.1);
}

.form-input::placeholder {
  color: var(--color-text-muted);
}

/* Cards */
.card {
  background-color: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: var(--space-6);
  box-shadow: var(--shadow-md);
}

/* Utility Classes */
.text-center { text-align: center; }
.text-muted { color: var(--color-text-muted); }
.text-success { color: var(--color-success); }
.text-error { color: var(--color-error); }

.mt-2 { margin-top: var(--space-2); }
.mt-4 { margin-top: var(--space-4); }
.mt-6 { margin-top: var(--space-6); }
.mt-8 { margin-top: var(--space-8); }

.mb-2 { margin-bottom: var(--space-2); }
.mb-4 { margin-bottom: var(--space-4); }
.mb-6 { margin-bottom: var(--space-6); }
.mb-8 { margin-bottom: var(--space-8); }

.hidden { display: none !important; }

/* Flex utilities */
.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.items-center { align-items: center; }
.justify-center { justify-content: center; }
.justify-between { justify-content: space-between; }
.gap-2 { gap: var(--space-2); }
.gap-4 { gap: var(--space-4); }

/* Animations */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
  20%, 40%, 60%, 80% { transform: translateX(5px); }
}

.shake {
  animation: shake 0.5s ease-in-out;
}

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

.fade-in {
  animation: fadeIn 0.3s ease-out;
}

/* Loading Spinner */
.spinner {
  width: calc(var(--vw) * 1.04);  /* ~20px at 1920 */
  height: calc(var(--vw) * 1.04);
  border: 2px solid var(--color-surface);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Toast Notifications */
.toast {
  position: fixed;
  bottom: calc(var(--space-6) + var(--safe-area-bottom));
  left: 50%;
  transform: translateX(-50%);
  padding: var(--space-3) var(--space-6);
  background-color: var(--color-surface-elevated);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  z-index: 1000;
  animation: fadeIn 0.3s ease-out;
  max-width: calc(100vw - var(--space-8));
}

.toast-success {
  border-left: 3px solid var(--color-success);
}

.toast-error {
  border-left: 3px solid var(--color-error);
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: var(--space-4);
  padding-top: calc(var(--space-4) + var(--safe-area-top));
  padding-bottom: calc(var(--space-4) + var(--safe-area-bottom));
}

.modal {
  background-color: var(--color-surface);
  border-radius: var(--radius-xl);
  padding: var(--space-6);
  max-width: calc(var(--vw) * 20.8); /* ~400px at 1920 */
  width: 100%;
  max-height: calc(100vh - var(--space-8));
  max-height: calc(100dvh - var(--space-8));
  overflow-y: auto;
  animation: fadeIn 0.3s ease-out;
}

.modal-title {
  margin-bottom: var(--space-6);
}

/* Responsive Container */
.container {
  width: 100%;
  max-width: calc(var(--vw) * 62.5); /* ~1200px at 1920 */
  margin: 0 auto;
  padding: 0 var(--space-4);
}

/* Note: Viewport-based scaling handles most responsive cases automatically.
   Media queries below are for special cases only (reduced motion, high contrast, print). */

/* Reduce motion for accessibility */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  html {
    scroll-behavior: auto;
  }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  :root {
    --color-border: #4a4a6a;
    --color-text-muted: #b0b8c8;
  }
}

/* Dark mode is default, but support light mode preference */
@media (prefers-color-scheme: light) {
  /* Keep dark mode - it's our brand */
}

/* Print styles */
@media print {
  body {
    background-color: white;
    color: black;
  }

  .btn, .modal-overlay, .toast {
    display: none;
  }
}

/* ===== MOBILE LANDSCAPE-ONLY MODE ===== */
/* Force landscape orientation on mobile devices */

/* Rotate device overlay - shown in portrait mode on touch devices */
.rotate-device-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: linear-gradient(135deg, var(--color-bg) 0%, var(--color-surface) 100%);
  z-index: 99999;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--space-6);
}

.rotate-device-overlay .rotate-icon {
  width: calc(var(--vw) * 6.25);   /* ~120px at 1920 */
  height: calc(var(--vw) * 6.25);  /* ~120px at 1920 */
  margin-bottom: var(--space-6);
  animation: rotatePhone 2s ease-in-out infinite;
}

.rotate-device-overlay .rotate-icon svg {
  width: 100%;
  height: 100%;
  fill: var(--color-primary);
}

.rotate-device-overlay .rotate-title {
  font-size: var(--font-size-2xl);
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: var(--space-4);
}

.rotate-device-overlay .rotate-message {
  font-size: var(--font-size-base);
  color: var(--color-text-muted);
  max-width: calc(var(--vw) * 14.6);  /* ~280px at 1920 */
  line-height: 1.5;
}

@keyframes rotatePhone {
  0%, 100% {
    transform: rotate(0deg);
  }
  25% {
    transform: rotate(-15deg);
  }
  75% {
    transform: rotate(90deg);
  }
}

/* Show rotate overlay in portrait mode on mobile/tablet devices */
@media screen and (orientation: portrait) and (max-width: 1024px) and (pointer: coarse) {
  .rotate-device-overlay {
    display: flex !important;
  }

  /* Hide main content when rotate overlay is shown */
  body > *:not(.rotate-device-overlay) {
    visibility: hidden;
  }
}

/* Also catch touch devices without pointer: coarse support */
@media screen and (orientation: portrait) and (max-width: 1024px) and (hover: none) {
  .rotate-device-overlay {
    display: flex !important;
  }

  body > *:not(.rotate-device-overlay) {
    visibility: hidden;
  }
}

/* Fallback for portrait mode on any device under 768px width */
@media screen and (orientation: portrait) and (max-width: 768px) {
  .rotate-device-overlay {
    display: flex !important;
  }

  body > *:not(.rotate-device-overlay) {
    visibility: hidden;
  }
}
