:root {
  --bg: #f5f5f4;
  --surface: #ffffff;
  --border: #e7e5e4;
  --text: #1c1917;
  --muted: #78716c;
  --accent: #1d4ed8;
  --good: #15803d;
  --bad: #b91c1c;
  --new: #ca8a04;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
  padding:
    env(safe-area-inset-top, 0)
    env(safe-area-inset-right, 0)
    env(safe-area-inset-bottom, 0)
    env(safe-area-inset-left, 0);
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
}

.btn-help {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  text-decoration: none;
  color: var(--accent);
  background: #eff6ff;
  border-color: #93c5fd;
  font-weight: 500;
  font-size: 0.82rem;
  padding: 0.35rem 0.65rem;
}

.btn-help:hover {
  background: #dbeafe;
}

.btn-help .icon {
  width: 1rem;
  height: 1rem;
  flex-shrink: 0;
}

.modal-hint {
  margin: 0.15rem 0 0.5rem;
  font-size: 0.8rem;
}

.header-title-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  flex-shrink: 0;
  color: var(--muted);
  text-decoration: none;
}

.btn-icon:hover {
  color: var(--accent);
  background: var(--bg);
}

.btn-icon .icon {
  width: 1.2rem;
  height: 1.2rem;
}

.header-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

.header-email {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#search-tabs {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: calc(100vh - 65px);
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 0.75rem;
}

.tab {
  display: block;
  width: 100%;
  text-align: left;
  padding: 0.65rem 0.75rem;
  margin-bottom: 0.25rem;
  border: none;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
  font-size: 0.9rem;
}

.tab:hover { background: var(--bg); }

.tab.active {
  background: #dbeafe;
  color: var(--accent);
  font-weight: 600;
}

.tab small {
  display: block;
  color: var(--muted);
  font-weight: 400;
  font-size: 0.75rem;
}

.content {
  padding: 1.25rem 1.5rem 2rem;
  overflow-x: auto;
}

.search-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 1.25rem;
  gap: 1rem;
}

.search-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  flex-shrink: 0;
}

.search-header h2 {
  margin: 0 0 0.25rem;
  font-size: 1.1rem;
}

.muted { color: var(--muted); font-size: 0.85rem; }

.band {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 1rem;
  overflow: hidden;
}

.band-header {
  padding: 0.75rem 1rem;
  background: #fafaf9;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: center;
}

.band-header h3 {
  margin: 0;
  font-size: 0.95rem;
  min-width: 100px;
}

.band-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  font-size: 0.85rem;
}

.band-stats strong { font-weight: 600; }

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.listings-table { min-width: 900px; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

th, td {
  padding: 0.5rem 0.65rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

th {
  background: #fafaf9;
  font-weight: 600;
  white-space: nowrap;
}

tr:last-child td { border-bottom: none; }

.num { text-align: right; font-variant-numeric: tabular-nums; }

.dev-good { color: var(--good); font-weight: 600; }
.dev-bad { color: var(--bad); font-weight: 600; }

.media-col {
  width: 3rem;
  text-align: center;
}

.media-cell {
  text-align: center;
  vertical-align: middle;
}

.media-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.75rem;
  height: 1.75rem;
  padding: 0;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 700;
  line-height: 1;
  color: transparent;
}

.media-toggle.on {
  background: #dcfce7;
  border-color: #86efac;
  color: #15803d;
}

.media-toggle.off {
  background: #f5f5f4;
  border-color: #d6d3d1;
  color: #d6d3d1;
}

.listings-table tbody tr.excluded-from-media td:not(.media-cell) {
  opacity: 0.55;
}

.badge {
  display: inline-block;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 600;
  margin-right: 0.25rem;
}

.badge-new { background: #fef3c7; color: #92400e; }
.badge-price { background: #fee2e2; color: #991b1b; }

.btn {
  padding: 0.45rem 0.9rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-size: 0.85rem;
}

.btn:hover { background: var(--bg); }
.btn.primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}
.btn.primary:hover { background: #1e40af; }
.btn:disabled { opacity: 0.6; cursor: wait; }

.btn-sm {
  padding: 0.25rem 0.5rem;
  font-size: 0.75rem;
}

.btn-fetch-one {
  flex-shrink: 0;
}

.btn-danger {
  color: var(--bad);
  border-color: #fecaca;
  background: #fef2f2;
}

.btn-danger:hover {
  background: #fee2e2;
}

input[type="text"], select {
  width: 100%;
  padding: 0.3rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.82rem;
}

a { color: var(--accent); }

.empty-band {
  padding: 0.75rem 1rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: #1c1917;
  color: white;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  font-size: 0.85rem;
  z-index: 100;
}

.toast.hidden { display: none; }

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 2rem;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

.login-card h1 {
  margin: 0 0 0.25rem;
  font-size: 1.25rem;
}

.login-card label {
  display: block;
  margin-top: 1rem;
  font-size: 0.85rem;
  font-weight: 500;
}

.login-card input {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.95rem;
}

.login-btn {
  width: 100%;
  margin-top: 1.25rem;
  padding: 0.6rem;
}

.login-error {
  margin: 0.75rem 0 0;
  color: var(--bad);
  font-size: 0.85rem;
  white-space: pre-wrap;
}

.login-error.hidden { display: none; }

.add-search-btn {
  width: 100%;
  margin-bottom: 0.75rem;
}

.modal.hidden { display: none; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

.modal-card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.5rem;
  width: min(520px, calc(100vw - 2rem));
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

.modal-card h2 {
  margin: 0 0 0.35rem;
  font-size: 1.1rem;
}

.modal-card label {
  display: block;
  margin-top: 1rem;
  font-size: 0.85rem;
  font-weight: 500;
}

.modal-card input {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.95rem;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1.25rem;
}

.preview-box {
  margin-top: 1rem;
  padding: 0.75rem 1rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.85rem;
}

.preview-box.hidden { display: none; }

.preview-box .warning {
  color: #a16207;
  margin-top: 0.5rem;
}

.preview-box ul {
  margin: 0.5rem 0 0;
  padding-left: 1.2rem;
}

.remove-search-message {
  margin: 0.75rem 0 0.35rem;
  font-size: 0.95rem;
  font-weight: 500;
}

.help-page {
  max-width: 42rem;
  margin: 0 auto;
  padding: 1.25rem 1.5rem 2rem;
}

.help-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
}

.help-section h2 {
  margin: 0 0 0.5rem;
  font-size: 0.95rem;
}

.help-section h3 {
  margin: 0 0 0.5rem;
  font-size: 0.95rem;
}

.help-steps {
  margin: 0;
  padding-left: 1.25rem;
}

.help-steps li {
  margin-bottom: 0.45rem;
}

.help-cards {
  display: grid;
  gap: 0.75rem;
}

@media (min-width: 640px) {
  .help-cards {
    grid-template-columns: 1fr 1fr;
  }
}

.help-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  background: var(--bg);
}

.help-card-good {
  border-color: #93c5fd;
  background: #eff6ff;
}

.help-badge {
  display: inline-block;
  margin: 0 0 0.5rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent);
}

.help-example {
  display: block;
  margin: 0.5rem 0;
  padding: 0.65rem 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.72rem;
  line-height: 1.45;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

.help-tip {
  margin: 0.5rem 0 0;
  padding: 0.5rem 0.6rem;
  background: #ecfdf5;
  border-radius: 6px;
  color: #166534;
  font-size: 0.8rem;
}

.help-warn {
  margin: 0.5rem 0 0;
  padding: 0.5rem 0.6rem;
  background: #fffbeb;
  border-radius: 6px;
  color: #92400e;
  font-size: 0.8rem;
}

.help-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.help-table th,
.help-table td {
  padding: 0.5rem 0.65rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.help-table th {
  background: #fafaf9;
  font-weight: 600;
}

.help-faq {
  margin: 0;
}

.help-faq dt {
  font-weight: 600;
  margin-top: 0.75rem;
  font-size: 0.9rem;
}

.help-faq dt:first-child {
  margin-top: 0;
}

.help-faq dd {
  margin: 0.25rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

code {
  font-family: ui-monospace, monospace;
  font-size: 0.85em;
  background: #f5f5f4;
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    min-height: auto;
  }

  .sidebar {
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 0.75rem;
    position: sticky;
    top: 0;
    z-index: 20;
    background: var(--surface);
  }

  #search-tabs {
    flex-direction: row;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.1rem;
  }

  .tab {
    flex: 0 0 auto;
    min-width: 8rem;
    max-width: 12rem;
    margin-bottom: 0;
    padding: 0.65rem 0.75rem;
  }

  .header {
    flex-wrap: wrap;
    gap: 0.75rem;
    padding: 0.85rem 1rem;
  }

  .header h1 {
    font-size: 1.15rem;
  }

  .header-actions {
    flex: 1;
    justify-content: flex-end;
  }

  .header-updated {
    display: none;
  }

  .content {
    padding: 1rem 1rem 1.5rem;
  }

  .search-actions {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .btn-fetch-one,
  .btn-danger {
    width: auto;
    flex: 1;
    min-width: 8rem;
  }

  .band-stats {
    gap: 0.5rem 0.85rem;
  }

  .search-header {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }

  .listings-table {
    min-width: 0;
  }

  .table-wrap {
    overflow: visible;
  }

  .listings-table thead {
    display: none;
  }

  .header-email {
    display: none;
  }

  .listings-table tbody tr {
    display: block;
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid var(--border);
  }

  .listings-table tbody tr:last-child {
    border-bottom: none;
  }

  .listings-table tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    padding: 0.4rem 0;
    border-bottom: none;
    text-align: right;
  }

  .listings-table tbody td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--muted);
    text-align: left;
    flex: 0 0 auto;
    max-width: 42%;
  }

  .listings-table tbody td[data-label="Via"] {
    display: block;
    padding: 0 0 0.65rem;
    margin-bottom: 0.35rem;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    text-align: left;
  }

  .listings-table tbody td[data-label="Via"]::before {
    display: none;
  }

  .listings-table tbody td[data-label="STATO"],
  .listings-table tbody td[data-label="Note"] {
    align-items: stretch;
  }

  .listings-table tbody td[data-label="STATO"] select,
  .listings-table tbody td[data-label="Note"] input {
    flex: 1;
    min-height: 2rem;
    font-size: 0.9rem;
  }

  .toast {
    left: 1rem;
    right: 1rem;
    bottom: calc(1rem + env(safe-area-inset-bottom, 0));
  }

  .modal {
    align-items: flex-end;
  }

  .modal-card {
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    border-radius: 12px 12px 0 0;
    padding: 1.25rem 1rem calc(1rem + env(safe-area-inset-bottom, 0));
  }

  .modal-actions .btn {
    min-height: unset;
    font-size: 0.9rem;
  }

  .login-card {
    margin: 1rem;
    padding: 1.5rem;
  }

  .login-card input,
  .login-btn {
    min-height: 2.75rem;
    font-size: 1rem;
  }
}
