/* AR Ruler - Mobile-First Styles */

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --primary: #007AFF;
  --primary-dark: #0056CC;
  --bg: #000;
  --text: #fff;
  --text-secondary: rgba(255, 255, 255, 0.7);
  --overlay: rgba(0, 0, 0, 0.5);
  --success: #34C759;
  --warning: #FF9500;
  --danger: #FF3B30;
  --card-bg: rgba(255, 255, 255, 0.1);
  --blur: saturate(180%) blur(20px);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --measure-color: #00ff88;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* Screens */
.screen {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
}

.screen.hidden {
  display: none;
}

/* Loading Overlay */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  transition: opacity 0.3s ease;
}

.loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 48px;
  height: 48px;
  border: 3px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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

.loading-overlay p {
  margin-top: 16px;
  color: var(--text-secondary);
}

/* Landing Screen */
.landing-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  background: linear-gradient(180deg, #1a1a2e 0%, #16213e 50%, #0f0f23 100%);
}

.landing-icon {
  font-size: 80px;
  margin-bottom: 24px;
  animation: bounce 2s ease-in-out infinite;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

.landing-content h1 {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 12px;
}

.landing-content p {
  font-size: 16px;
  color: var(--text-secondary);
  margin-bottom: 24px;
  max-width: 300px;
}

.support-info {
  margin-bottom: 24px;
  padding: 16px;
  background: var(--card-bg);
  border-radius: 12px;
  max-width: 300px;
}

.support-info.supported {
  border: 1px solid var(--success);
}

.support-info.unsupported {
  border: 1px solid var(--warning);
}

.support-info p {
  margin-bottom: 8px;
  font-size: 14px;
}

.btn-primary {
  background: var(--primary);
  color: white;
  border: none;
  padding: 16px 48px;
  font-size: 18px;
  font-weight: 600;
  border-radius: 30px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-primary:hover {
  background: var(--primary-dark);
  transform: scale(1.02);
}

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

.btn-primary:disabled {
  background: #444;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--card-bg);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.2);
  padding: 12px 24px;
  font-size: 16px;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.2);
}

.btn-text {
  background: transparent;
  color: var(--text-secondary);
  border: none;
  padding: 12px 24px;
  font-size: 14px;
  cursor: pointer;
  transition: color 0.2s ease;
}

.btn-text:hover {
  color: var(--text);
}

.hint {
  margin-top: 16px;
  font-size: 14px;
  color: var(--text-secondary);
}

/* AR Screen */
#ar-canvas {
  width: 100%;
  height: 100%;
}

/* Reticle */
.reticle {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 60px;
  height: 60px;
  pointer-events: none;
  z-index: 10;
}

.reticle-inner {
  width: 100%;
  height: 100%;
  border: 2px solid var(--measure-color);
  border-radius: 50%;
  animation: reticlePulse 2s ease-in-out infinite;
}

.reticle-inner::before,
.reticle-inner::after {
  content: '';
  position: absolute;
  background: var(--measure-color);
}

.reticle-inner::before {
  width: 2px;
  height: 12px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.reticle-inner::after {
  width: 12px;
  height: 2px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

@keyframes reticlePulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.7; transform: scale(0.95); }
}

.reticle.surface-detected .reticle-inner {
  border-color: var(--success);
  animation: none;
}

/* Measurement Display */
.measurement {
  position: absolute;
  top: calc(var(--safe-top) + 80px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--measure-color);
  color: black;
  padding: 12px 24px;
  border-radius: 30px;
  font-size: 24px;
  font-weight: 700;
  z-index: 20;
  display: flex;
  align-items: baseline;
  gap: 4px;
  box-shadow: 0 4px 20px rgba(0, 255, 136, 0.3);
}

.measurement.hidden {
  display: none;
}

#measure-value {
  font-size: 32px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

#measure-unit {
  font-size: 18px;
  font-weight: 600;
  opacity: 0.8;
}

/* Top Bar */
.top-bar {
  position: absolute;
  top: var(--safe-top);
  left: 0;
  right: 0;
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 30;
}

.spacer {
  flex: 1;
}

.icon-btn {
  width: 44px;
  height: 44px;
  border: none;
  background: var(--overlay);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.icon-btn:active {
  transform: scale(0.9);
}

.icon-btn svg {
  width: 24px;
  height: 24px;
  color: var(--text);
}

/* Bottom Bar */
.bottom-bar {
  position: absolute;
  bottom: calc(var(--safe-bottom) + 24px);
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 0 24px;
  z-index: 30;
}

/* Unit Toggle */
.unit-toggle {
  display: flex;
  background: var(--overlay);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  border-radius: 12px;
  overflow: hidden;
}

.unit-btn {
  padding: 10px 14px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

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

/* Place Button */
.place-btn {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--measure-color);
  border: 4px solid rgba(255, 255, 255, 0.3);
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.place-btn:hover {
  transform: scale(1.05);
}

.place-btn:active {
  transform: scale(0.95);
}

.place-btn-inner {
  font-size: 32px;
  font-weight: 700;
  color: black;
}

/* Instructions */
.instructions {
  position: absolute;
  bottom: calc(var(--safe-bottom) + 140px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--overlay);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  padding: 16px 24px;
  border-radius: 16px;
  text-align: left;
  z-index: 20;
  transition: opacity 0.3s ease;
}

.instructions.hidden {
  opacity: 0;
  pointer-events: none;
}

.instructions p {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.instructions p:last-child {
  margin-bottom: 0;
}

/* Fallback Screen */
.fallback-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
}

.fallback-icon {
  font-size: 64px;
  margin-bottom: 24px;
}

.fallback-content h2 {
  font-size: 24px;
  margin-bottom: 12px;
}

.fallback-content > p {
  color: var(--text-secondary);
  margin-bottom: 32px;
}

.fallback-options {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 100%;
  max-width: 320px;
  margin-bottom: 24px;
}

.option-card {
  background: var(--card-bg);
  padding: 20px;
  border-radius: 16px;
  text-align: left;
}

.option-card h3 {
  font-size: 16px;
  margin-bottom: 8px;
}

.option-card p {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

.option-card button {
  width: 100%;
}

/* Calibration Modal */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: #1a1a2e;
  border-radius: 20px;
  padding: 24px;
  max-width: 400px;
  width: 100%;
}

.modal-content h3 {
  font-size: 20px;
  margin-bottom: 12px;
  text-align: center;
}

.modal-content > p {
  font-size: 14px;
  color: var(--text-secondary);
  text-align: center;
  margin-bottom: 20px;
}

.calibration-area {
  position: relative;
  width: 100%;
  aspect-ratio: 4/3;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  margin-bottom: 16px;
}

#calibration-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.calibration-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 200px;
  height: 126px; /* Credit card ratio 85.6 x 53.98 mm */
  border: 2px dashed var(--measure-color);
  border-radius: 8px;
}

.calibration-slider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.calibration-slider input {
  flex: 1;
  height: 4px;
  -webkit-appearance: none;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
  outline: none;
}

.calibration-slider input::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  background: var(--primary);
  border-radius: 50%;
  cursor: pointer;
}

.calibration-slider span {
  min-width: 50px;
  text-align: right;
  font-size: 14px;
  color: var(--text-secondary);
}

.modal-actions {
  display: flex;
  gap: 12px;
}

.modal-actions button {
  flex: 1;
}

/* Language Selector */
.lang-selector {
  position: fixed;
  top: calc(var(--safe-top) + 16px);
  right: 70px;
  z-index: 50;
}

#ar-screen .lang-selector,
#fallback-screen .lang-selector {
  display: none;
}

.lang-selector select {
  background: var(--overlay);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  border: none;
  padding: 8px 12px;
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}

/* Toast */
.toast {
  position: fixed;
  bottom: calc(var(--safe-bottom) + 200px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--overlay);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  padding: 12px 24px;
  border-radius: 12px;
  font-size: 14px;
  z-index: 200;
  animation: toastIn 0.3s ease;
}

.toast.hidden {
  display: none;
}

.toast.success {
  background: rgba(52, 199, 89, 0.9);
}

.toast.error {
  background: rgba(255, 59, 48, 0.9);
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* Responsive */
@media (min-width: 768px) {
  .landing-content {
    max-width: 500px;
    margin: 0 auto;
  }

  .fallback-options {
    flex-direction: row;
    max-width: 600px;
  }

  .option-card {
    flex: 1;
  }
}
