/* ================================================================
   components.css — shared primitives
   ================================================================
   Every page composes these. No page invents its own button, card,
   field, badge, modal, or toast.

   Rules locked from prompt.md:
     - No borders on surfaces. Separation by background shade only.
     - 1px focus outline on interactive elements (via base.css).
     - Colored text only on: profit, loss, parity even/odd.
     - Tighter radii (4/6/8/10/12/14). Soft via smooth antialiasing.
     - Phone-first sizing. Larger at 768px.

   Sections:
     1. Card
     2. Button
     3. Badge
     4. Status dot
     5. Field + input + select + textarea
     6. Password field
     7. Segmented control
     8. Toggle switch
     9. Tabs
     10. Modal
     11. Toast
     12. Empty state
     13. Metric
     14. Divider
     15. Skeleton loader
     16. Dropdown menu
     17. Utility text tones
     18. Reduced motion
   ================================================================ */

/* ---------------------------------------------------------------- 1. CARD */

.card {
  background: var(--bg-surface);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  min-width: 0;
  box-shadow: var(--shadow-sm);
}

@media (min-width: 768px) {
  .card {
    border-radius: var(--radius-lg);
    padding: var(--space-4);
    gap: var(--space-3);
  }
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

.card-header-title {
  font-family: var(--font-display);
  font-size: var(--text-sm);
  font-weight: var(--weight-bold);
  letter-spacing: -0.01em;
  text-transform: none;
  color: var(--text-primary);
}

.card-header-meta {
  font-size: var(--text-2xs);
  font-weight: var(--weight-medium);
  letter-spacing: 0.02em;
  text-transform: none;
  color: var(--text-muted);
}

.card-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  min-width: 0;
}

.card-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
  padding-top: var(--space-2);
  border-top: 1px solid var(--divider);
}

/* ---------------------------------------------------------------- 2. BUTTON */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 34px;
  padding: 0 var(--space-3);
  border: none;
  border-radius: var(--radius-sm);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  letter-spacing: -0.005em;
  white-space: nowrap;
  text-decoration: none;
  cursor: pointer;
  transition:
    background-color var(--duration-fast) var(--ease),
    color var(--duration-fast) var(--ease),
    opacity var(--duration-fast) var(--ease);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.btn svg,
.btn i {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

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

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

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-primary:active:not(:disabled) {
  background: var(--accent-active);
}

.btn-secondary {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--bg-subtle);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.btn-danger {
  background: var(--loss-subtle);
  color: var(--loss);
}

.btn-danger:hover:not(:disabled) {
  background: var(--loss);
  color: var(--accent-fg);
}

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

.btn-success:hover:not(:disabled) {
  background: var(--profit);
  color: var(--accent-fg);
}

.btn-sm {
  min-height: 28px;
  padding: 0 var(--space-2);
  font-size: var(--text-xs);
  gap: 4px;
}

.btn-sm svg,
.btn-sm i {
  width: 12px;
  height: 12px;
}

.btn-lg {
  min-height: 42px;
  padding: 0 var(--space-4);
  font-size: var(--text-md);
  border-radius: var(--radius-md);
}

.btn-icon {
  min-width: 34px;
  padding: 0;
}

.btn-icon.btn-sm {
  min-width: 28px;
}

.btn-block {
  width: 100%;
}

.btn.loading {
  pointer-events: none;
  position: relative;
  color: transparent;
}

.btn.loading::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  width: 14px;
  height: 14px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  color: var(--accent-fg);
  animation: spin 0.7s linear infinite;
}

.btn-secondary.loading::after,
.btn-ghost.loading::after {
  color: var(--text-primary);
}

.btn.loading::after {
  color: var(--accent-fg);
}

.btn-secondary.loading::after,
.btn-ghost.loading::after {
  color: var(--text-primary);
}

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

@media (min-width: 768px) {
  .btn {
    min-height: 36px;
    padding: 0 var(--space-4);
    font-size: var(--text-md);
  }
  .btn-sm { min-height: 30px; padding: 0 var(--space-2); }
  .btn-lg { min-height: 44px; padding: 0 var(--space-5); }
}

/* ---------------------------------------------------------------- 3. BADGE */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 7px;
  border-radius: var(--radius-full);
  font-family: var(--font-sans);
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.02em;
  line-height: 1.4;
  white-space: nowrap;
}

.badge-neutral {
  background: var(--bg-elevated);
  color: var(--text-secondary);
}

.badge-accent {
  background: var(--accent-subtle);
  color: var(--accent);
}

.badge-profit {
  background: var(--profit-subtle);
  color: var(--profit);
}

.badge-loss {
  background: var(--loss-subtle);
  color: var(--loss);
}

.badge-warning {
  background: var(--warning-subtle);
  color: var(--warning);
}

.badge-muted {
  background: var(--bg-elevated);
  color: var(--text-muted);
}

/* Square badge for status tags (real/demo/etc) */
.badge-square {
  border-radius: var(--radius-xs);
  padding: 2px 6px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

/* ---------------------------------------------------------------- 4. STATUS DOT */

.status-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}

.status-dot.online,
.status-dot.connected {
  background: var(--profit);
}

.status-dot.offline,
.status-dot.disconnected {
  background: var(--loss);
}

.status-dot.connecting {
  background: var(--warning);
  animation: pulse 1.4s ease-in-out infinite;
}

.status-dot.idle {
  background: var(--text-subtle);
}

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

/* ---------------------------------------------------------------- 5. FIELD + INPUT */

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.field-label {
  font-family: var(--font-sans);
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.field-hint {
  font-size: var(--text-xs);
  color: var(--text-muted);
  line-height: var(--leading-snug);
}

.field-error {
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
  color: var(--loss);
  line-height: var(--leading-snug);
}

.field-error[hidden] {
  display: none;
}

.field-input {
  box-sizing: border-box;
  width: 100%;
  min-height: 34px;
  padding: 7px 10px;
  background: var(--bg-input);
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: var(--text-md);
  font-weight: var(--weight-medium);
  line-height: 1.4;
  appearance: none;
  -webkit-appearance: none;
  transition:
    background-color var(--duration-fast) var(--ease),
    box-shadow var(--duration-fast) var(--ease);
  font-variant-numeric: tabular-nums;
  font-feature-settings: 'tnum' 1, 'zero' 1;
}

.field-input:hover:not(:disabled):not(:focus) {
  background: var(--bg-elevated);
}

.field-input:focus {
  outline: none;
  background: var(--bg-elevated);
}

.field-input:focus-visible {
  outline: var(--focus-ring-width) solid var(--accent);
  outline-offset: var(--focus-ring-offset);
}

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

.field-input::placeholder {
  color: var(--text-muted);
  opacity: 1;
  font-weight: var(--weight-normal);
}

.field-input[aria-invalid='true'] {
  background: var(--loss-subtle);
}

/* Numeric variant — tabular figures, right-align option */
.field-input[type='number'] {
  -moz-appearance: textfield;
  appearance: textfield;
  font-variant-numeric: tabular-nums;
}

.field-input[type='number']::-webkit-outer-spin-button,
.field-input[type='number']::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Select */
select.field-input {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238a97a8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
  background-repeat: no-repeat;
  background-position: right 9px center;
  background-size: 12px 12px;
  padding-right: 28px;
  cursor: pointer;
}

select.field-input option {
  background: var(--bg-surface);
  color: var(--text-primary);
  padding: 8px;
}

/* Textarea */
textarea.field-input {
  min-height: 88px;
  resize: vertical;
  line-height: var(--leading-base);
  font-family: var(--font-sans);
}

@media (min-width: 768px) {
  .field-input {
    min-height: 36px;
    padding: 8px 12px;
    font-size: var(--text-md);
  }
  textarea.field-input {
    min-height: 100px;
  }
}

/* ---------------------------------------------------------------- 6. PASSWORD FIELD */

.password-field {
  position: relative;
  display: flex;
  align-items: center;
}

.password-field .field-input {
  padding-right: 38px;
}

.password-toggle {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  transition: color var(--duration-fast) var(--ease);
}

.password-toggle:hover {
  color: var(--text-primary);
}

.password-toggle svg,
.password-toggle i {
  width: 14px;
  height: 14px;
}

/* ---------------------------------------------------------------- 7. SEGMENTED CONTROL */

.segmented {
  display: inline-flex;
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  padding: 2px;
  gap: 2px;
  align-self: flex-start;
  max-width: 100%;
}

.segmented-option {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-xs);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--text-secondary);
  cursor: pointer;
  transition:
    background-color var(--duration-fast) var(--ease),
    color var(--duration-fast) var(--ease);
  position: relative;
  white-space: nowrap;
}

.segmented-option:hover {
  color: var(--text-primary);
}

.segmented-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 0;
  height: 0;
}

.segmented-option:has(input:checked) {
  background: var(--bg-surface);
  color: var(--text-primary);
  font-weight: var(--weight-semibold);
  box-shadow: var(--shadow-sm);
}

.segmented-option:has(input:focus-visible) {
  outline: var(--focus-ring-width) solid var(--accent);
  outline-offset: var(--focus-ring-offset);
}

/* ---------------------------------------------------------------- 8. TOGGLE SWITCH */

.toggle {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  cursor: pointer;
  user-select: none;
  min-height: 28px;
}

.toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 0;
  height: 0;
}

.toggle-track {
  position: relative;
  width: 32px;
  height: 18px;
  background: var(--bg-elevated);
  border-radius: var(--radius-full);
  flex-shrink: 0;
  transition: background-color var(--duration-fast) var(--ease);
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--text-muted);
  transition:
    transform var(--duration-fast) var(--ease),
    background-color var(--duration-fast) var(--ease);
}

.toggle input:checked ~ .toggle-track {
  background: var(--accent-subtle);
}

.toggle input:checked ~ .toggle-track .toggle-thumb {
  transform: translateX(14px);
  background: var(--accent);
}

.toggle input:focus-visible ~ .toggle-track {
  outline: var(--focus-ring-width) solid var(--accent);
  outline-offset: var(--focus-ring-offset);
}

.toggle-text {
  font-size: var(--text-sm);
  color: var(--text-primary);
  font-weight: var(--weight-medium);
}

/* ---------------------------------------------------------------- 9. TABS */

.tabs {
  display: flex;
  gap: 2px;
  padding: 2px;
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  overflow-x: auto;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 28px;
  padding: 0 var(--space-3);
  background: transparent;
  border-radius: var(--radius-xs);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--text-secondary);
  white-space: nowrap;
  cursor: pointer;
  transition:
    background-color var(--duration-fast) var(--ease),
    color var(--duration-fast) var(--ease);
}

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

.tab.active {
  background: var(--bg-surface);
  color: var(--text-primary);
  font-weight: var(--weight-semibold);
  box-shadow: var(--shadow-sm);
}

/* ---------------------------------------------------------------- 10. MODAL */

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 9000;
  display: none;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.modal-overlay.active,
.modal-overlay.is-open {
  display: flex;
}

.modal {
  background: var(--bg-surface);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  max-width: 480px;
  width: 100%;
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  box-shadow: var(--shadow-float);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

@media (min-width: 768px) {
  .modal {
    border-radius: var(--radius-xl);
    padding: var(--space-5);
    gap: var(--space-4);
  }
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
}

.modal-title {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: var(--weight-bold);
  color: var(--text-primary);
  letter-spacing: -0.02em;
  margin: 0;
  line-height: var(--leading-snug);
}

.modal-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  background: transparent;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  transition:
    background-color var(--duration-fast) var(--ease),
    color var(--duration-fast) var(--ease);
}

.modal-close:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.modal-close svg,
.modal-close i {
  width: 14px;
  height: 14px;
}

.modal-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  font-size: var(--text-md);
  color: var(--text-secondary);
  line-height: var(--leading-relaxed);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  flex-wrap: wrap;
}

@media (max-width: 480px) {
  .modal-overlay {
    padding: var(--space-3);
  }
  .modal-footer {
    flex-direction: column-reverse;
  }
  .modal-footer .btn {
    width: 100%;
  }
}

/* ---------------------------------------------------------------- 11. TOAST */

.toast {
  position: fixed;
  bottom: var(--space-4);
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  z-index: 9500;
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  background: var(--bg-surface);
  color: var(--text-primary);
  border-radius: var(--radius-full);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  box-shadow: var(--shadow-float);
  opacity: 0;
  pointer-events: none;
  max-width: calc(100vw - 32px);
  transition:
    opacity var(--duration-base) var(--ease),
    transform var(--duration-base) var(--ease-out);
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
  pointer-events: auto;
}

.toast svg,
.toast i {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

.toast.success svg,
.toast.success i { color: var(--profit); }
.toast.error   svg,
.toast.error   i { color: var(--loss); }
.toast.warning svg,
.toast.warning i { color: var(--warning); }
.toast.info    svg,
.toast.info    i { color: var(--accent); }

/* ---------------------------------------------------------------- 12. EMPTY STATE */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-6) var(--space-3);
  text-align: center;
  color: var(--text-muted);
}

.empty-state svg,
.empty-state i {
  width: 24px;
  height: 24px;
  opacity: 0.6;
}

.empty-state p {
  margin: 0;
  font-size: var(--text-sm);
  color: var(--text-muted);
}

/* ---------------------------------------------------------------- 13. METRIC */

.metric {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.metric-label {
  font-size: var(--text-2xs);
  font-weight: var(--weight-semibold);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.metric-value {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-weight: var(--weight-bold);
  color: var(--text-primary);
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  font-feature-settings: 'tnum' 1, 'zero' 1;
  line-height: var(--leading-tight);
}

.metric-sub {
  font-size: var(--text-xs);
  color: var(--text-secondary);
}

/* ---------------------------------------------------------------- 14. DIVIDER */

.divider {
  height: 1px;
  background: var(--divider);
  margin: var(--space-2) 0;
  border: none;
}

/* ---------------------------------------------------------------- 15. SKELETON */

.skeleton {
  display: inline-block;
  background: linear-gradient(
    90deg,
    var(--bg-elevated) 0%,
    var(--bg-subtle) 50%,
    var(--bg-elevated) 100%
  );
  background-size: 400px 100%;
  border-radius: var(--radius-xs);
  animation: shimmer 1.4s linear infinite;
  color: transparent;
  pointer-events: none;
  min-height: 1em;
}

@keyframes shimmer {
  0%   { background-position: -400px 0; }
  100% { background-position: 400px 0; }
}

/* ---------------------------------------------------------------- 16. DROPDOWN MENU */

.menu {
  min-width: 200px;
  max-width: calc(100vw - 32px);
  background: var(--bg-surface);
  border-radius: var(--radius-md);
  padding: var(--space-1);
  box-shadow: var(--shadow-float);
  display: flex;
  flex-direction: column;
  gap: 1px;
  z-index: 100;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  min-height: 34px;
  border-radius: var(--radius-xs);
  color: var(--text-primary);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  cursor: pointer;
  text-decoration: none;
  transition: background-color var(--duration-fast) var(--ease);
  white-space: nowrap;
}

.menu-item:hover {
  background: var(--bg-elevated);
}

.menu-item svg,
.menu-item i {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  color: var(--text-secondary);
}

.menu-item.danger {
  color: var(--loss);
}

.menu-item.danger svg,
.menu-item.danger i {
  color: var(--loss);
}

.menu-item.is-active {
  background: var(--accent-subtle);
  color: var(--accent);
}

.menu-item.is-active svg,
.menu-item.is-active i {
  color: var(--accent);
}

.menu-divider {
  height: 1px;
  background: var(--divider);
  margin: 2px 0;
}

/* ---------------------------------------------------------------- 17. UTILITY TEXT TONES */

.text-profit    { color: var(--profit); }
.text-loss      { color: var(--loss); }
.text-warning   { color: var(--warning); }
.text-muted     { color: var(--text-muted); }
.text-secondary { color: var(--text-secondary); }
.text-accent    { color: var(--accent); }

/* Even/odd — the only other place color is allowed besides P/L */
.text-even { color: var(--profit); }
.text-odd  { color: var(--loss); }

/* ---------------------------------------------------------------- 18. REDUCED MOTION */

@media (prefers-reduced-motion: reduce) {
  .btn,
  .btn.loading::after,
  .status-dot.connecting,
  .field-input,
  .segmented-option,
  .toggle-track,
  .toggle-thumb,
  .tab,
  .modal-close,
  .toast,
  .menu-item,
  .skeleton {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}