/* ============================================
   FIDEM Components
   Buttons, inputs, modals, toasts
   ============================================ */

/* --- Button base --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  min-height: var(--btn-height);
  min-width: var(--touch-min);
  padding: var(--space-sm) var(--space-lg);
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: var(--font-size-base);
  transition: background var(--transition-fast),
              opacity var(--transition-fast);
  user-select: none;
  -webkit-user-select: none;
  white-space: nowrap;
}

.btn:hover {
  opacity: 0.85;
}

.btn:active {
  opacity: 0.7;
  transition-duration: 80ms;
}

.btn:disabled {
  cursor: not-allowed;
  pointer-events: none;
}

.btn svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

/* --- Primary --- */
.btn-primary {
  background: var(--accent);
  color: var(--bg);
}

[data-theme="light"] .btn-primary {
  background: var(--accent);
  color: #FEFCF8;
}

.btn-primary:hover {
  background: #DFCA94;
}

[data-theme="light"] .btn-primary:hover {
  background: var(--accent-hover);
}

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

.btn-primary:disabled {
  background: var(--surface-alt);
  color: var(--text-dim);
}

/* --- Danger --- */
.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-danger:hover {
  background: var(--danger-hover);
}

.btn-danger:disabled {
  background: var(--surface-alt);
  color: var(--text-dim);
}

/* --- Ghost --- */
.btn-ghost {
  background: transparent;
  color: var(--text-sub);
  border: 1px solid var(--border);
}

.btn-ghost:hover {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
}

[data-theme="light"] .btn-ghost:hover {
  background: rgba(0, 0, 0, 0.04);
}

[data-theme="light"] .btn-primary:disabled {
  background: var(--surface-alt);
  color: var(--text-dim);
}

[data-theme="light"] .btn-danger:disabled {
  background: var(--surface-alt);
  color: var(--text-dim);
}

/* --- Icon button --- */
.btn-icon {
  min-height: var(--touch-min);
  min-width: var(--touch-min);
  padding: var(--space-sm);
  border-radius: var(--radius-full);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-icon:hover {
  background: var(--surface-alt);
}

.btn-icon svg {
  width: 24px;
  height: 24px;
}

/* --- Large button (CTA) --- */
.btn-lg {
  display: flex;
  width: 100%;
  min-height: 52px;
  font-size: var(--font-size-base);
  border-radius: var(--radius-md);
}

@media (min-width: 481px) {
  .btn-lg {
    max-width: 280px;
    margin-left: auto;
    margin-right: auto;
  }

  .screen-footer .btn-lg {
    max-width: none;
  }
}

/* --- Button group --- */
.btn-group {
  display: flex;
  gap: var(--space-sm);
  width: 100%;
}

.btn-group .btn {
  flex: 1;
}

/* --- Input base --- */
.input {
  width: 100%;
  min-height: var(--btn-height);
  padding: var(--space-sm) var(--space-md);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text);
  font-size: var(--font-size-base);
  transition: border-color var(--transition-fast),
              box-shadow var(--transition-fast);
}

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

.input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
  outline: 2px solid transparent;
}

.input.error {
  border-color: var(--danger);
}

/* --- Input group (label + input + error) --- */
.input-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
  width: 100%;
}

.input-label {
  font-size: var(--font-size-sm);
  color: var(--text-sub);
  font-weight: 500;
}

.input-error {
  font-size: var(--font-size-xs);
  color: var(--danger);
  min-height: 16px;
}

/* --- Select --- */
.select {
  width: 100%;
  min-height: var(--btn-height);
  padding: var(--space-sm) var(--space-md);
  padding-right: var(--space-xl);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text);
  font-size: var(--font-size-base);
  background-image: var(--select-arrow);
  background-repeat: no-repeat;
  background-position: right 12px center;
  cursor: pointer;
}

.select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
  outline: 2px solid transparent;
}

/* --- Checkbox --- */
.checkbox {
  display: flex;
  align-items: flex-start;
  gap: var(--space-sm);
  cursor: pointer;
  min-height: var(--touch-min);
  padding: var(--space-xs) 0;
}

.checkbox input[type="checkbox"] {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  margin-top: 1px;
  border: 2px solid var(--text-sub);
  border-radius: 4px;
  background: transparent;
  cursor: pointer;
  position: relative;
  transition: border-color 0.15s, background 0.15s;
}

.checkbox input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}

.checkbox input[type="checkbox"]:checked::after {
  content: '';
  position: absolute;
  left: 6px;
  top: 2px;
  width: 6px;
  height: 11px;
  border: solid var(--bg);
  border-width: 0 2.5px 2.5px 0;
  transform: rotate(45deg);
}

[data-theme="light"] .checkbox input[type="checkbox"]:checked::after {
  border-color: #FEFCF8;
}

.checkbox-label {
  font-size: var(--font-size-sm);
  color: var(--text-sub);
  line-height: 1.5;
}

.checkbox-label a {
  color: var(--text);
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* --- Modal (bottom sheet on mobile) --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay);
  z-index: var(--z-overlay);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--transition-normal),
              visibility var(--transition-normal);
}

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

.modal {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: var(--space-lg);
  padding-bottom: calc(var(--space-lg) + var(--safe-bottom));
  z-index: var(--z-modal);
  transform: translateY(100%);
  transition: transform var(--transition-normal);
  max-height: 80dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.modal-overlay.active .modal {
  transform: translateY(0);
}

.modal-handle {
  width: 36px;
  height: 4px;
  background: var(--border);
  border-radius: var(--radius-full);
  margin: 0 auto var(--space-md);
}

.modal-title {
  font-size: var(--font-size-lg);
  font-weight: 700;
  margin-bottom: var(--space-md);
}

@media (min-width: 1024px) {
  .modal {
    bottom: auto;
    top: 50%;
    left: 50%;
    right: auto;
    transform: translate(-50%, -50%) scale(0.95);
    width: min(var(--max-width), 90vw);
    border-radius: var(--radius-lg);
    max-height: 80vh;
  }

  .modal-overlay.active .modal {
    transform: translate(-50%, -50%) scale(1);
  }

  .modal-handle {
    display: none;
  }
}

/* --- Toast (bottom, minimal) --- */
.toast-container {
  position: fixed;
  bottom: calc(var(--space-lg) + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  width: min(calc(100% - var(--space-lg) * 2), var(--max-width));
  pointer-events: none;
}

.toast {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: var(--space-sm) var(--space-md);
  pointer-events: auto;
  animation: toast-in 200ms ease-out forwards;
  font-size: var(--font-size-sm);
  color: var(--text);
  text-align: center;
}

.toast.toast-out {
  animation: toast-out 200ms ease-in forwards;
}

.toast-success { color: var(--success); }
.toast-error { color: var(--danger); }
.toast-info { color: var(--text-sub); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes toast-out {
  from { opacity: 1; transform: translateY(0); }
  to { opacity: 0; transform: translateY(8px); }
}

/* --- Progress bar --- */
.progress-bar {
  width: 100%;
  height: 4px;
  background: var(--border);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: var(--radius-full);
  transition: width var(--transition-normal);
}

/* --- Badge --- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: 2px var(--space-sm);
  border-radius: var(--radius-full);
  font-size: var(--font-size-xs);
  font-weight: 600;
  background: var(--accent-glow);
  color: var(--accent);
}

.badge-danger {
  background: rgba(255, 107, 107, 0.15);
  color: var(--danger);
}

/* --- Divider --- */
.divider {
  width: 100%;
  height: 1px;
  background: var(--border);
  margin: var(--space-md) 0;
}

/* --- Avatar placeholder --- */
.avatar {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-full);
  background: var(--surface-alt);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-sub);
  font-weight: 600;
  flex-shrink: 0;
}

/* --- Card --- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-md);
}

/* --- Countdown / Timer --- */
.timer {
  font-size: var(--font-size-xl);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

/* --- Status dot --- */
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  background: var(--success);
  display: inline-block;
}

.status-dot.offline {
  background: var(--text-dim);
}

/* --- Call action buttons --- */
.btn-call-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-sm);
}

.btn-call-label {
  font-size: var(--font-size-xs);
  color: var(--text-sub);
}

.btn-call-action {
  width: 64px;
  height: 64px;
  border-radius: var(--radius-full);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: opacity var(--transition-fast), border-color var(--transition-fast), color var(--transition-fast);
}

.btn-call-action:active {
  opacity: 0.7;
  transition-duration: 80ms;
}

.btn-call-action:disabled {
  opacity: 0.5;
  pointer-events: none;
}

.btn-call-mic {
  background: var(--surface);
  border: 2px solid #3A3A42;
  color: var(--text);
}

.btn-call-mic.muted {
  border-color: var(--danger);
  color: var(--danger);
  background: rgba(232, 97, 77, 0.12);
}

.btn-call-speaker {
  background: var(--surface);
  border: 2px solid #3A3A42;
  color: var(--text);
}

.btn-call-speaker.active {
  border-color: var(--accent);
  color: var(--accent);
}

.btn-call-hangup {
  background: var(--danger);
  color: #fff;
}

.btn-call-hangup:hover {
  background: var(--danger-hover);
}

/* --- Chat button --- */
.btn-call-chat {
  position: relative;
  background: var(--surface);
  border: 2px solid var(--border);
  color: var(--text);
}

.btn-call-chat.active {
  border-color: var(--accent);
  color: var(--accent);
}

.chat-badge {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--danger);
  display: none;
}

.chat-badge.visible {
  display: block;
}

/* --- Chat panel --- */
.chat-panel {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: var(--bg);
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-sm) var(--space-md);
  padding-bottom: var(--space-md);
  display: flex;
  flex-direction: column;
  gap: 6px;
  justify-content: flex-end;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.chat-messages:has(.chat-empty-state) {
  justify-content: center;
}

.chat-messages::-webkit-scrollbar {
  width: 3px;
}

.chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: var(--radius-full);
}

.chat-msg {
  max-width: 78%;
  padding: 10px 14px;
  display: inline-flex;
  align-items: flex-end;
  gap: 6px;
  word-break: break-word;
  animation: msg-in 200ms ease-out;
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.chat-msg--mine {
  align-self: flex-end;
  background: var(--accent);
  color: #1A1A1F;
  border-radius: 18px 18px 4px 18px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

.chat-msg--peer {
  align-self: flex-start;
  background: var(--surface-alt);
  color: var(--text);
  border-radius: 18px 18px 18px 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

[data-theme="light"] .chat-msg--mine {
  color: #FEFCF8;
}

.chat-msg__text {
  font-size: var(--font-size-sm);
  line-height: 1.5;
}

.chat-msg__time {
  font-size: 0.625rem;
  opacity: 0.5;
  white-space: nowrap;
  flex-shrink: 0;
  margin-bottom: 1px;
}

.chat-input-bar {
  display: flex;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md) calc(var(--space-sm) + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
  align-items: center;
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  padding: 10px 16px;
  font-size: var(--font-size-sm);
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color 150ms ease-out;
}

.chat-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
}

.chat-input:disabled {
  opacity: 0.4;
}

.chat-send {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  background: var(--accent);
  color: #1A1A1F;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 100ms ease-out, opacity 150ms ease-out;
}

[data-theme="light"] .chat-send {
  color: #FEFCF8;
}

.chat-send:active {
  opacity: 0.7;
  transform: scale(0.92);
}

/* --- Call bar (compact top control) --- */
.call-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-sm) var(--space-md);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: 48px;
  gap: var(--space-sm);
}

.call-bar-left {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.call-bar-left .call-timer {
  font-size: var(--font-size-base);
  font-weight: 600;
}

/* --- Audio waveform (between call-bar and chat) --- */
.call-waveform {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-secondary, rgba(0,0,0,0.03));
  border-bottom: 1px solid var(--border, rgba(0,0,0,0.06));
}

.call-audio-canvas {
  width: 100%;
  height: 48px;
  opacity: 0.2;
  transition: opacity 300ms ease-out;
  display: block;
}

.call-status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: live-dot-pulse 2s ease-in-out infinite;
}

.call-status-text {
  font-size: var(--font-size-sm);
  color: var(--text-sub);
}

.call-bar-right {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
}

.call-bar-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  border: none;
  background: transparent;
  color: var(--text-sub);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background var(--transition-fast), color var(--transition-fast);
}

.call-bar-btn:active {
  background: var(--surface-alt);
}

.call-bar-btn:disabled {
  opacity: 0.4;
  pointer-events: none;
}

.call-bar-btn.active {
  color: var(--accent);
  background: rgba(196, 164, 110, 0.12);
}

.call-bar-btn.muted {
  color: var(--danger);
  background: rgba(232, 97, 77, 0.12);
}

.call-bar-btn.call-hangup {
  color: #fff;
  background: var(--danger);
}

.call-bar-btn.call-hangup:active {
  opacity: 0.8;
}

.call-bar-btn svg {
  width: 18px;
  height: 18px;
}

.call-bar-btn.call-report {
  color: var(--text-dim);
}

.call-bar-btn.call-report svg {
  width: 16px;
  height: 16px;
}

/* --- Chat empty state --- */
.chat-empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: var(--font-size-sm);
  gap: var(--space-sm);
  opacity: 0.6;
  padding: var(--space-xl) 0;
}

.chat-empty-state::before {
  content: '';
  display: block;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  border: 2px solid var(--text-dim);
  opacity: 0.3;
  background: transparent;
}

/* --- Route guidance overlay --- */
.call-route-guidance {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  justify-content: center;
  padding: var(--space-md);
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 10;
}

.route-guidance-content {
  text-align: center;
}

.route-guidance-text {
  font-size: var(--font-size-sm);
  color: var(--text);
  margin-bottom: var(--space-xs);
}

.route-guidance-gesture {
  font-size: var(--font-size-xs);
  color: var(--text-sub);
  margin-bottom: var(--space-sm);
}

/* --- Segment control (2-option toggle) --- */
.segment-control {
  display: flex;
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-xs);
  gap: 0;
}

.segment-control .segment-option {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-sm) var(--space-md);
  border-radius: calc(var(--radius-md) - 3px);
  font-size: var(--font-size-sm);
  font-weight: 500;
  color: var(--text-sub);
  cursor: pointer;
  transition: background 150ms ease-out, color 150ms ease-out;
  border: none;
  background: none;
  min-height: var(--touch-min);
  white-space: nowrap;
}

@media (max-width: 360px) {
  .segment-control .segment-option {
    padding: var(--space-sm) var(--space-sm);
    font-size: var(--font-size-xs);
  }
}

.segment-control .segment-option:hover {
  color: var(--text);
  opacity: 1;
}

.segment-control .segment-option.selected {
  background: rgba(196, 164, 110, 0.15);
  border: 1px solid var(--accent);
  color: var(--accent);
  font-weight: 600;
}

[data-theme="light"] .segment-control .segment-option.selected {
  background: rgba(122, 101, 53, 0.12);
  color: var(--accent);
}

/* --- Range Slider (single & dual) --- */
.range-slider {
  position: relative;
  width: 100%;
  height: 44px;
  display: flex;
  align-items: center;
}

.range-slider__track {
  position: absolute;
  left: 0;
  right: 0;
  height: 4px;
  background: var(--border);
  border-radius: var(--radius-full);
  top: 50%;
  transform: translateY(-50%);
}

.range-slider__fill {
  position: absolute;
  height: 4px;
  background: var(--accent);
  border-radius: var(--radius-full);
  top: 50%;
  transform: translateY(-50%);
}

.range-slider__thumb {
  position: absolute;
  width: 24px;
  height: 24px;
  background: var(--accent);
  border: 2px solid var(--surface);
  border-radius: var(--radius-full);
  top: 50%;
  transform: translate(-50%, -50%);
  cursor: grab;
  z-index: 2;
  touch-action: none;
  box-shadow: 0 0 0 0 var(--accent-glow);
  transition: box-shadow 150ms ease-out, transform 150ms ease-out;
}

.range-slider__thumb:active,
.range-slider__thumb.active {
  cursor: grabbing;
  box-shadow: 0 0 0 6px var(--accent-glow);
  transform: translate(-50%, -50%) scale(1.15);
}

.range-slider__value {
  position: absolute;
  top: -2px;
  font-size: var(--font-size-xs);
  font-weight: 600;
  color: var(--accent);
  transform: translateX(-50%);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.range-slider__labels {
  display: flex;
  justify-content: space-between;
  width: 100%;
  font-size: var(--font-size-xs);
  color: var(--text-dim);
  margin-top: var(--space-xs);
  font-variant-numeric: tabular-nums;
}

/* --- Skeleton loader --- */
.skeleton {
  background: linear-gradient(
    90deg,
    var(--surface) 25%,
    var(--surface-alt) 50%,
    var(--surface) 75%
  );
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.5s infinite;
  border-radius: var(--radius-sm);
}

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

/* --- Live counter (online users indicator) --- */
.live-counter {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: color 1s ease-out, opacity 1s ease-out, text-shadow 1s ease-out;
}

.live-counter:empty {
  opacity: 0;
}

.live-counter .live-dot {
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  background: var(--success);
  flex-shrink: 0;
  animation: live-dot-pulse 2s ease-in-out infinite;
}

.live-counter .live-count {
  color: var(--accent);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.live-counter.moderate {
  opacity: 0.85;
}

.live-counter.active {
  color: var(--accent);
  opacity: 0.75;
  text-shadow: 0 0 12px var(--accent-glow);
}

@keyframes live-dot-pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .live-counter .live-dot {
    animation: none;
    opacity: 0.7;
  }
}

/* --- Connect progress bar --- */
.connect-progress {
  width: 100%;
  max-width: 280px;
  margin: 0 auto;
  text-align: center;
}

.connect-progress-bar {
  height: 4px;
  background: var(--border);
  border-radius: var(--radius-full);
  overflow: hidden;
  margin-bottom: var(--space-sm);
}

.connect-progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: var(--radius-full);
  width: 0%;
  transition: width 0.4s ease;
}

.connect-progress-fill.pulse {
  animation: progress-pulse 1.5s ease-in-out infinite;
}

@keyframes progress-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.connect-progress-text {
  font-size: var(--font-size-sm);
  color: var(--text-sub);
}
