/* ============================================================
   AppCalcoloTari – Custom Styles
   ============================================================ */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* ── Design tokens ────────────────────────────────────────── */
:root {
  --iam-color-primary:      #0d6930;
  --iam-color-primary-dark: #04401b;
  --iam-color-primary-light:#bfd9c9;
  --iam-color-success:      #1a7a3f;
  --iam-color-success-bg:   #e6f4ec;
  --iam-color-warn:         #8a5a00;
  --iam-color-warn-bg:      #fff3cd;
  --iam-color-error:        #c0392b;
  --iam-color-error-bg:     #fdecea;
  --iam-color-text:         #1a1a2e;
  --iam-color-text-muted:   #6b7280;
  --iam-color-border:       #d1d5db;
  --iam-color-border-focus: #003F18;
  --iam-color-bg:           #f3f6fb;
  --iam-color-card:         #ffffff;
  --iam-color-highlight:    #fef08a;
  --iam-color-option-hover: #f0f4ff;
  --iam-color-option-active:#dbeafe;

  --iam-radius-sm: 6px;
  --iam-radius-md: 10px;
  --iam-radius-lg: 16px;

  --iam-shadow-sm: 0 1px 3px rgba(0,0,0,.08);
  --iam-shadow-md: 0 4px 16px rgba(0,0,0,.10);
  --iam-shadow-lg: 0 8px 32px rgba(0,0,0,.12);

  --iam-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --iam-transition: 180ms ease;
}

/* ── Layout ────────────────────────────────────────────────── */
body {
  font-family: var(--iam-font);
  background: var(--iam-color-bg);
  color: var(--iam-color-text);
  min-height: 100vh;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px 80px;
}

.iam-container {
  width: 100%;
  max-width: 620px;
}

/* ── Header ────────────────────────────────────────────────── */
.iam-header {
  text-align: center;
  margin-bottom: 32px;
}

.iam-title {
  font-size: 1.875rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--iam-color-primary);
}

.iam-subtitle {
  margin-top: 8px;
  color: var(--iam-color-text-muted);
  font-size: 0.9375rem;
  line-height: 1.5;
}

/* ── Card ──────────────────────────────────────────────────── */
.iam-card {
  background: var(--iam-color-card);
  border-radius: var(--iam-radius-lg);
  box-shadow: var(--iam-shadow-md);
  padding: 32px;
}

/* ── Form ──────────────────────────────────────────────────── */
.iam-form {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* ── Field label ───────────────────────────────────────────── */
.iam-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.iam-label {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--iam-color-text);
  letter-spacing: 0.01em;
}

/* ── Autocomplete wrapper ──────────────────────────────────── */
.iam-autocomplete {
  position: relative;
}

.iam-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

/* ── Text input ────────────────────────────────────────────── */
.iam-input {
  width: 100%;
  height: 48px;
  padding: 0 80px 0 44px;
  font-size: 1rem;
  font-family: var(--iam-font);
  color: var(--iam-color-text);
  background: var(--iam-color-card);
  border: 2px solid var(--iam-color-border);
  border-radius: var(--iam-radius-md);
  transition: border-color var(--iam-transition), box-shadow var(--iam-transition);
  outline: none;
  appearance: none;
}

.iam-input::placeholder {
  color: var(--iam-color-text-muted);
}

.iam-input:focus {
  border-color: var(--iam-color-border-focus);
  box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.15);
}

.iam-input:disabled {
  background: #f9fafb;
  cursor: not-allowed;
  opacity: 0.7;
}

/* ── Search icon ───────────────────────────────────────────── */
.iam-input-icon {
  position: absolute;
  left: 14px;
  width: 18px;
  height: 18px;
  color: var(--iam-color-text-muted);
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.iam-input-icon svg {
  width: 18px;
  height: 18px;
}

/* ── Clear button ──────────────────────────────────────────── */
.iam-clear-btn {
  position: absolute;
  right: 12px;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #e5e7eb;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  color: var(--iam-color-text-muted);
  transition: background var(--iam-transition), color var(--iam-transition);
}

.iam-clear-btn svg {
  width: 14px;
  height: 14px;
}

.iam-clear-btn:hover {
  background: #d1d5db;
  color: var(--iam-color-text);
}

.iam-clear-btn:focus-visible {
  outline: 2px solid var(--iam-color-border-focus);
  outline-offset: 2px;
}

/* ── Dropdown listbox ──────────────────────────────────────── */
.iam-listbox {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  z-index: 100;
  background: var(--iam-color-card);
  border: 1.5px solid var(--iam-color-border);
  border-radius: var(--iam-radius-md);
  box-shadow: var(--iam-shadow-lg);
  list-style: none;
  max-height: 300px;
  overflow-y: auto;
  overscroll-behavior: contain;
  animation: iam-fade-in 140ms ease;
}

@keyframes iam-fade-in {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Option items ──────────────────────────────────────────── */
.iam-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  cursor: pointer;
  font-size: 0.9375rem;
  transition: background var(--iam-transition);
  border-bottom: 1px solid #f3f4f6;
}

.iam-option:last-child {
  border-bottom: none;
}

.iam-option:hover {
  background: var(--iam-color-option-hover);
}

.iam-option--active {
  background: var(--iam-color-option-active);
}

.iam-option-comune {
  font-weight: 500;
  color: var(--iam-color-text);
  flex: 1;
}

.iam-option-istat {
  font-size: 0.75rem;
  color: var(--iam-color-text-muted);
  font-family: 'Courier New', monospace;
  white-space: nowrap;
}

/* ── Highlight match ───────────────────────────────────────── */
.iam-highlight {
  background: var(--iam-color-highlight);
  color: inherit;
  border-radius: 2px;
  padding: 0 1px;
}

/* ── No results message ────────────────────────────────────── */
.iam-no-results {
  font-size: 0.875rem;
  color: var(--iam-color-text-muted);
  padding: 6px 2px 0;
}

/* ── Info grid (dopo selezione) ────────────────────────────── */
.iam-field-info {
  background: var(--iam-color-primary-light);
  border: 1.5px solid #c3d9f7;
  border-radius: var(--iam-radius-md);
  padding: 16px 20px;
  animation: iam-fade-in 200ms ease;
}

.iam-info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px 24px;
}

.iam-info-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.iam-info-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--iam-color-text-muted);
}

.iam-info-value {
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--iam-color-text);
}

/* ── Badge tipologia tributo ───────────────────────────────── */
.iam-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 20px;
  font-size: 0.8125rem;
  font-weight: 600;
  text-transform: capitalize;
  width: fit-content;
}

.iam-badge--tributo {
  background: var(--iam-color-warn-bg);
  color: var(--iam-color-warn);
}

.iam-badge--corrispettivo {
  background: var(--iam-color-success-bg);
  color: var(--iam-color-success);
}

.iam-badge--puntuale {
  background: #ede9fe;
  color: #5b21b6;
}

/* ── Selects row ───────────────────────────────────────────── */
.iam-fields-row {
  display: flex;
  gap: 16px;
  margin-bottom: 1rem;
}

.iam-fields-row .iam-field {
  flex: 1;
  min-width: 0;
}

/* ── Anno / Utenza select ──────────────────────────────────── */
.iam-select-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.iam-select {
  width: 100%;
  height: 48px;
  padding: 0 44px 0 16px;
  font-size: 1rem;
  font-family: var(--iam-font);
  color: var(--iam-color-text);
  background: var(--iam-color-card);
  border: 2px solid var(--iam-color-border);
  border-radius: var(--iam-radius-md);
  transition: border-color var(--iam-transition), box-shadow var(--iam-transition);
  outline: none;
  appearance: none;
  cursor: pointer;
}

.iam-select:focus {
  border-color: var(--iam-color-border-focus);
  box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.15);
}

.iam-select:disabled {
  background: #f9fafb;
  cursor: not-allowed;
  opacity: 0.7;
}

.iam-select-icon {
  position: absolute;
  right: 14px;
  width: 18px;
  height: 18px;
  color: var(--iam-color-text-muted);
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.iam-select-icon svg {
  width: 18px;
  height: 18px;
}

/* ── Readonly value display ────────────────────────────────── */
.iam-value-display {
  height: 48px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  font-size: 1rem;
  font-weight: 600;
  color: var(--iam-color-text);
  background: #f9fafb;
  border: 2px solid var(--iam-color-border);
  border-radius: var(--iam-radius-md);
  user-select: none;
}

/* ── Form actions ──────────────────────────────────────────── */
.iam-form-actions {
  display: flex;
  justify-content: flex-end;
}

.iam-btn {
  height: 46px;
  padding: 0 28px;
  font-size: 1rem;
  font-family: var(--iam-font);
  font-weight: 600;
  border: none;
  border-radius: var(--iam-radius-md);
  cursor: pointer;
  transition: background var(--iam-transition), transform 80ms ease, box-shadow var(--iam-transition);
  outline: none;
}

.iam-btn-primary {
  background: var(--iam-color-primary);
  color: #ffffff;
  box-shadow: var(--iam-shadow-sm);
}

.iam-btn-primary:hover:not(:disabled) {
  background: var(--iam-color-primary-dark);
  box-shadow: var(--iam-shadow-md);
}

.iam-btn-primary:active:not(:disabled) {
  transform: scale(0.98);
}

.iam-btn-primary:focus-visible {
  outline: 2px solid var(--iam-color-border-focus);
  outline-offset: 3px;
}

.iam-btn-primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ── Status / errori globali ───────────────────────────────── */
.iam-status {
  margin-top: 16px;
  font-size: 0.875rem;
  color: var(--iam-color-text-muted);
  text-align: center;
}

.iam-status--error {
  color: var(--iam-color-error);
}

.iam-error-message {
  padding: 8px 12px;
  font-size: 0.875rem;
  color: var(--iam-color-error);
  background: var(--iam-color-error-bg);
  border: 1px solid var(--iam-color-error);
  border-radius: var(--iam-radius-sm);
}

.iam-margin-right-2 {
  margin-right: 16px;
}

/* ── Scrollbar personalizzata nel dropdown ─────────────────── */
.iam-listbox::-webkit-scrollbar {
  width: 6px;
}

.iam-listbox::-webkit-scrollbar-track {
  background: transparent;
}

.iam-listbox::-webkit-scrollbar-thumb {
  background: #d1d5db;
  border-radius: 3px;
}

.iam-listbox::-webkit-scrollbar-thumb:hover {
  background: #9ca3af;
}

/* ── Responsive ────────────────────────────────────────────── */
@media (max-width: 480px) {
  .iam-card {
    padding: 20px 16px;
  }

  .iam-title {
    font-size: 1.5rem;
  }

  .iam-info-grid {
    grid-template-columns: 1fr;
  }

  .iam-form-actions {
    justify-content: stretch;
  }

  .iam-btn {
    width: 100%;
  }
}

/* ── Table ──────────────────────────────────────────────────────────────────── */
.iam-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
  color: var(--iam-color-text);
  background: var(--iam-color-card);
  border-radius: var(--iam-radius-sm);
  overflow: hidden;
  margin-top: 12px;
}

.iam-table caption {
  font-size: 0.95rem;
  font-weight: 600;
  text-align: left;
  padding: 8px 12px;
  color: var(--iam-color-primary);
  background: var(--iam-color-primary-light);
  border-bottom: 1px solid var(--iam-color-border);
}

.iam-table thead tr {
  background: var(--iam-color-primary);
  color: #ffffff;
}

.iam-table th {
  padding: 10px 12px;
  text-align: left;
  font-weight: 600;
  white-space: nowrap;
}

.iam-table td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--iam-color-border);
  vertical-align: middle;
}

.iam-table tbody tr:last-child td {
  border-bottom: none;
}

.iam-table tbody tr:nth-child(even) {
  background: var(--iam-color-bg);
}

.iam-table tbody tr:hover {
  background: var(--iam-color-option-hover);
}

/* ── ND Contratti / Tab system ─────────────────────────────── */
#iam-nd-contracts {
  margin-bottom: 1rem;
}

#iam-nd-tab-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  border-bottom: 2px solid var(--iam-color-border);
  margin-bottom: 16px;
}

.iam-tab {
  height: 36px;
  padding: 0 14px;
  font-size: 0.875rem;
  font-family: var(--iam-font);
  font-weight: 500;
  border: 2px solid var(--iam-color-border);
  border-bottom: none;
  border-radius: var(--iam-radius-sm) var(--iam-radius-sm) 0 0;
  background: #f3f6fb;
  color: var(--iam-color-text-muted);
  cursor: pointer;
  transition: background var(--iam-transition), color var(--iam-transition);
  position: relative;
  bottom: -2px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.iam-tab:hover {
  background: #e8f0ec;
  color: var(--iam-color-text);
}

.iam-tab--active {
  background: var(--iam-color-card);
  color: var(--iam-color-primary);
  border-color: var(--iam-color-border);
  border-bottom-color: var(--iam-color-card);
  font-weight: 600;
}

.iam-tab--add {
  background: transparent;
  border: 2px dashed var(--iam-color-border);
  color: var(--iam-color-primary);
  font-weight: 600;
}

.iam-tab--add:hover {
  background: var(--iam-color-success-bg);
  border-color: var(--iam-color-primary);
}

.iam-tab-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  font-size: 1rem;
  line-height: 1;
  color: var(--iam-color-text-muted);
  transition: background var(--iam-transition), color var(--iam-transition);
}

.iam-tab-close:hover {
  background: var(--iam-color-error-bg);
  color: var(--iam-color-error);
}

.iam-nd-panel {
  display: block;
}

.iam-nd-label-ripartito {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  cursor: pointer;
  font-weight: 600;
}

.iam-nd-check-ripartito {
  width: 1rem;
  height: 1rem;
  cursor: pointer;
  accent-color: var(--iam-color-primary);
}

.iam-nd-panel[hidden] {
  display: none;
}

#iam-row-preventivo-nd {
  flex-direction: column;
}

.iam-nd-row-quota-fissa {
  margin-bottom: 1rem;
}

/* ── ND Multi-dotazione ────────────────────────────────────── */
.iam-nd-dotazioni-wrap {
  margin-bottom: 0.75rem;
}

.iam-nd-dotazioni-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 8px;
}

.iam-nd-dotazione-row {
  border: 1px solid var(--iam-color-border);
  border-radius: var(--iam-radius-md);
  padding: 12px 14px 8px;
  background: var(--iam-color-card);
}

.iam-nd-dotazione-row-fields {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 12px;
  align-items: start;
}

.iam-nd-dotazione-row-actions {
  display: flex;
  align-items: flex-end;
  padding-bottom: 4px;
}

.iam-nd-btn-remove-dotazione,
.iam-nd-btn-remove-ripartito {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: 1px solid var(--iam-color-border);
  border-radius: var(--iam-radius-sm);
  background: var(--iam-color-card);
  color: var(--iam-color-text-muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  transition: background var(--iam-transition), color var(--iam-transition), border-color var(--iam-transition);
  flex-shrink: 0;
}

.iam-nd-btn-remove-dotazione:hover,
.iam-nd-btn-remove-ripartito:hover {
  background: var(--iam-color-error-bg);
  color: var(--iam-color-error);
  border-color: var(--iam-color-error);
}

.iam-nd-btn-add-dotazione,
.iam-nd-btn-add-ripartito {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 32px;
  padding: 0 12px;
  font-size: 0.8125rem;
  font-family: var(--iam-font);
  font-weight: 600;
  border: 2px dashed var(--iam-color-border);
  border-radius: var(--iam-radius-sm);
  background: transparent;
  color: var(--iam-color-primary);
  cursor: pointer;
  transition: background var(--iam-transition), border-color var(--iam-transition);
}

.iam-nd-btn-add-dotazione:hover,
.iam-nd-btn-add-ripartito:hover {
  background: var(--iam-color-success-bg);
  border-color: var(--iam-color-primary);
}

/* ── ND Contenitori ripartiti ──────────────────────────────── */
.iam-nd-ripartiti-section {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px dashed var(--iam-color-border);
}

.iam-nd-ripartiti-header {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--iam-color-primary);
  margin-bottom: 10px;
}

.iam-nd-ripartiti-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 8px;
}

.iam-nd-ripartito-row {
  border: 1px solid var(--iam-color-border);
  border-radius: var(--iam-radius-md);
  padding: 12px 14px 8px;
  background: var(--iam-color-bg);
}

.iam-nd-ripartito-row-fields {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: start;
}

.iam-nd-ripartito-row-actions {
  display: flex;
  align-items: flex-end;
  padding-bottom: 4px;
}

/* Responsive: impila le colonne dotazione su schermi stretti */
@media (max-width: 520px) {
  .iam-nd-dotazione-row-fields {
    grid-template-columns: 1fr;
  }
  .iam-nd-dotazione-row-actions {
    justify-content: flex-end;
    padding-bottom: 0;
  }
  .iam-nd-ripartito-row-fields {
    grid-template-columns: 1fr;
  }
  .iam-nd-ripartito-row-actions {
    justify-content: flex-end;
    padding-bottom: 0;
  }
}
