/* =========================================
   Components: btn, input, card, modal, toast, badge
   ========================================= */

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  min-height: 40px;
  transition: all var(--transition);
}
.btn:hover { box-shadow: 0 0 0 2px rgba(212,175,55,0.35), 0 2px 8px rgba(212,175,55,0.2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; box-shadow: none; }

.btn-primary {
  background: linear-gradient(to bottom, var(--gold-bright), var(--gold));
  color: var(--bg);
  border: none;
  font-weight: 700;
  box-shadow: 0 1px 0 rgba(255,255,255,0.15) inset;
}
.btn-primary:hover { box-shadow: 0 1px 0 rgba(255,255,255,0.15) inset, 0 0 0 2px rgba(232,197,71,0.5), 0 2px 12px rgba(212,175,55,0.4); }

.btn-danger {
  background: transparent;
  color: var(--red-soft);
  border: 1px solid var(--red);
}
.btn-danger:hover { box-shadow: 0 0 0 2px rgba(224,96,96,0.35), 0 2px 8px rgba(224,96,96,0.25); }

.btn-ghost {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.btn-ghost:hover { box-shadow: 0 0 0 2px rgba(212,175,55,0.3), 0 2px 8px rgba(212,175,55,0.15); }

.btn-small { padding: 6px 10px; font-size: 12px; min-height: 32px; }
.btn-block { width: 100%; }

/* ---------- Inputs ---------- */
.input, .textarea, .select {
  display: block;
  width: 100%;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color var(--transition);
  min-height: 40px;
}
.input:focus, .textarea:focus, .select:focus {
  outline: none;
  border-color: var(--gold);
}
.input::placeholder { color: var(--text-faint); }
.textarea { min-height: 80px; resize: vertical; }

/* File input: button parte sinistra con stile primary dorato */
.input[type="file"] {
  padding: 0 12px 0 0;
  line-height: 40px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-dim);
}
.input[type="file"]::file-selector-button {
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  padding: 0 16px;
  height: 40px;
  margin-right: 12px;
  border: none;
  border-radius: var(--radius) 0 0 var(--radius);
  background: linear-gradient(to bottom, var(--gold-bright), var(--gold));
  color: var(--bg);
  cursor: pointer;
  box-shadow: 0 1px 0 rgba(255,255,255,0.15) inset;
  transition: all var(--transition);
}
.input[type="file"]::file-selector-button:hover {
  box-shadow: 0 1px 0 rgba(255,255,255,0.15) inset,
              0 0 0 2px rgba(232,197,71,0.5),
              0 2px 12px rgba(212,175,55,0.4);
  filter: brightness(1.04);
}

/* Rimuovi spinner su input type=number */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
}
input[type="number"] {
  -moz-appearance: textfield;
  appearance: textfield;
}

.field { margin-bottom: 14px; }
.field label {
  display: block;
  margin-bottom: 6px;
  font-size: 12px;
  color: var(--text-dim);
  font-weight: 600;
}
.field .error {
  margin-top: 4px;
  font-size: 12px;
  color: var(--red-soft);
}

/* ---------- Toggle switch ---------- */
.toggle-field {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
  font-size: 13px;
}
.toggle-switch {
  position: relative;
  width: 42px;
  height: 22px;
  flex-shrink: 0;
  display: inline-block;
}
.toggle-switch input[type="checkbox"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  margin: 0;
  cursor: pointer;
  z-index: 1;
}
.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--surface-raised);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  transition: background 0.2s;
}
.toggle-slider::before {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: var(--text-dim);
  border-radius: 50%;
  transition: all 0.2s;
}
.toggle-switch input:checked + .toggle-slider {
  background: linear-gradient(to bottom, var(--gold-bright), var(--gold));
  border-color: var(--gold-dim);
}
.toggle-switch input:checked + .toggle-slider::before {
  left: 22px;
  background: var(--bg);
}
.toggle-label { color: var(--text); font-weight: 600; }

/* ---------- Status dots ---------- */
.status-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.status-dot-on {
  background: var(--green);
  box-shadow: 0 0 0 2px rgba(74,222,128,0.2);
}
.status-dot-off {
  background: var(--red);
  box-shadow: 0 0 0 2px rgba(224,96,96,0.2);
}

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  transition: all var(--transition);
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

/* ---------- Badge ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-radius: var(--radius-sm);
  background: var(--surface-raised);
  color: var(--text-dim);
}
.badge-gold { background: rgba(212,175,55,0.15); color: var(--gold); }
.badge-purple { background: rgba(180,122,232,0.15); color: var(--purple); }
.badge-gray { background: var(--surface-raised); color: var(--gray); }
.badge-green { background: rgba(74,222,128,0.15); color: var(--green); }
.badge-orange { background: rgba(240,160,80,0.15); color: var(--orange); }
.badge-red { background: rgba(224,96,96,0.15); color: var(--red-soft); }

/* ---------- Toast ---------- */
#toast-stack {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 2000;
  display: flex;
  flex-direction: column-reverse;
  gap: 10px;
  pointer-events: none;
  max-width: 360px;
}
.toast {
  pointer-events: auto;
  padding: 12px 16px;
  background: var(--surface-raised);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: 13px;
  color: var(--text);
  display: flex;
  align-items: flex-start;
  gap: 10px;
  animation: toast-slide-in 0.25s ease-out;
}
.toast-success { border-left: 3px solid var(--green); }
.toast-error { border-left: 3px solid var(--red); }
.toast-info { border-left: 3px solid var(--gold); }
.toast-close {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 16px;
  padding: 0;
}
.toast.toast-leave { animation: toast-fade-out 0.2s ease-in forwards; }

@keyframes toast-slide-in { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: translateX(0); } }
@keyframes toast-fade-out { from { opacity: 1; } to { opacity: 0; transform: translateX(20px); } }

/* ---------- Modal ---------- */
#modal-root:empty { display: none; }
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1500;
  animation: modal-fade-in 0.2s ease-out;
  padding: 16px;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 600px;
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  animation: modal-slide-in 0.25s ease-out;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2, .modal-header h3 { margin: 0; }
.modal-close {
  background: transparent;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 20px;
  line-height: 1;
  padding: 4px;
}
.modal-body { padding: 20px; overflow-y: auto; flex: 1; }
.modal-actions {
  display: flex;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  justify-content: flex-end;
}

@keyframes modal-fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes modal-slide-in { from { opacity: 0; transform: translateY(-12px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- Spinner overlay ---------- */
.busy-overlay {
  position: absolute;
  inset: 0;
  background: rgba(14,13,16,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  z-index: 10;
  border-radius: var(--radius-lg);
  backdrop-filter: blur(2px);
}
.busy-overlay .spinner {
  width: 44px;
  height: 44px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.busy-overlay .spinner-text {
  font-size: 13px;
  color: var(--text-dim);
  font-weight: 600;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Tabs ---------- */
.tabs {
  display: flex;
  gap: 2px;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  padding: 10px 18px;
  font-size: 13px;
  color: var(--text-dim);
  cursor: pointer;
  border: none;
  background: transparent;
  white-space: nowrap;
  border-bottom: 2px solid transparent;
  transition: all var(--transition);
  font-family: inherit;
  font-weight: 600;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--gold); border-bottom-color: var(--gold); }
