:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #cbd5e1;
  --border-soft: #e2e8f0;
  --text: #0f172a;
  --text-muted: #475569;

  /* 1. Azione Principale (Primary CTA) */
  --primary: #1e3a8a;
  --primary-mid: #2563eb;
  --primary-soft: #eff6ff;

  /* 2. Avvisi / Priorità / Eccezioni (Warning Amber) */
  --warn-bg: #fffbeb;
  --warn-border: #f59e0b;
  --warn-text: #92400e;
  --warn-icon: #d97706;

  /* 3. Neutri & Stato */
  --danger-bg: #fef2f2;
  --danger-border: #fecaca;
  --danger-text: #b91c1c;
  --success-bg: #f0fdf4;
  --success-text: #15803d;

  --radius-card: 12px;
  --radius-cta: 14px;
  --shadow-card: 0 2px 6px rgba(15, 23, 42, 0.05);
  --shadow-cta: 0 8px 20px -4px rgba(30, 58, 138, 0.35);
}

*, *::before, *::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  padding: 0;
  overflow-x: hidden;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  touch-action: manipulation;
}

#app-container {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.hidden {
  display: none !important;
}

/* Header sticky con supporto iOS Safe Area */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border-soft);
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.06);
}

.header-inner {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 16px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), var(--primary-mid));
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}

.version-badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  background: var(--surface-alt);
  border: 1px solid var(--border-soft);
  padding: 1px 8px;
  border-radius: 999px;
  color: var(--text-muted);
}

/* Navigazione Tab */
.main-tabs {
  max-width: 960px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  padding: 0 16px;
  gap: 8px;
}

.tab-btn {
  min-height: 46px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.94rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-bottom: 3px solid transparent;
  transition: color 0.15s, border-color 0.15s;
}

.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

/* Main Content */
.main-content {
  flex: 1;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 16px 14px 48px;
}

/* Pulsanti Semantici (Gerarchia a 3 Livelli) */
.btn-cta-primary,
.btn-secondary,
.btn-warning-outline,
.choice-card {
  min-height: 46px;
  padding: 10px 16px;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform 0.15s, box-shadow 0.15s, background 0.15s, border-color 0.15s;
  touch-action: manipulation;
}

.btn-cta-primary {
  border: none;
  border-radius: var(--radius-cta);
  background: linear-gradient(135deg, var(--primary), var(--primary-mid));
  color: #ffffff;
  box-shadow: var(--shadow-cta);
}

.btn-cta-primary:hover:not(:disabled) {
  transform: translateY(-1px);
}

.btn-cta-primary:active:not(:disabled) {
  transform: scale(0.98);
}

.btn-cta-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-secondary {
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  background: var(--surface-alt);
  color: var(--text);
  box-shadow: var(--shadow-card);
}

.btn-secondary:hover {
  transform: translateY(-1px);
  background: #f1f5f9;
}

.btn-secondary:active {
  transform: scale(0.98);
}

.btn-warning-outline {
  border: 1px solid var(--warn-border);
  border-radius: var(--radius-card);
  background: var(--warn-bg);
  color: var(--warn-text);
  box-shadow: var(--shadow-card);
}

.btn-warning-outline:hover {
  transform: translateY(-1px);
}

.btn-warning-outline:active {
  transform: scale(0.98);
}

.btn-sm {
  min-height: 40px;
  padding: 6px 12px;
  font-size: 0.84rem;
}

/* Choice Card (Toggle con stato selezionato + spunta) */
.choice-card {
  position: relative;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  background: var(--surface-alt);
  color: var(--text);
  box-shadow: var(--shadow-card);
}

.choice-card .check-badge {
  display: none;
  position: absolute;
  top: 6px;
  right: 8px;
  font-size: 0.75rem;
  font-weight: 800;
  color: var(--warn-icon);
}

.choice-card.selected {
  border: 2px solid var(--warn-border);
  background: var(--warn-bg);
  color: var(--warn-text);
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.15);
}

.choice-card.selected .check-badge {
  display: inline-block;
}

/* Griglia Simmetrica */
.controls-card {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-card);
  padding: 16px;
  box-shadow: var(--shadow-card);
  margin-bottom: 16px;
}

.symmetric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.config-grid {
  margin-top: 14px;
}

.span-2 {
  grid-column: span 2;
}

@media (max-width: 440px) {
  .symmetric-grid {
    grid-template-columns: 1fr;
  }
  .span-2 {
    grid-column: span 1;
  }
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.control-select,
.control-input {
  font-size: 16px; /* Previene auto-zoom iOS Safari */
  min-height: 46px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  background: var(--surface-alt);
  color: var(--text);
  width: 100%;
}

.stats-bar {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border-soft);
  font-size: 0.82rem;
  color: var(--text-muted);
}

/* States & Diagnostics */
.state-box {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-card);
  padding: 28px 18px;
  text-align: center;
  margin-bottom: 16px;
  box-shadow: var(--shadow-card);
}

.state-icon {
  margin-bottom: 8px;
  color: var(--danger-text);
}

.error-box {
  background: var(--danger-bg);
  border-color: var(--danger-border);
  color: var(--danger-text);
}

.spinner {
  width: 34px;
  height: 34px;
  margin: 0 auto 10px;
  border: 3px solid var(--border-soft);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.diagnostics-banner {
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  color: var(--warn-text);
  border-radius: var(--radius-card);
  padding: 12px 16px;
  margin-bottom: 16px;
  font-size: 0.88rem;
}

.diagnostics-banner ul {
  margin: 6px 0 0;
  padding-left: 18px;
}

/* Feed Cards */
.feed-list,
.config-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.news-card {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-card);
  padding: 16px;
  box-shadow: var(--shadow-card);
}

.news-card.is-priority {
  border: 1px solid var(--warn-border);
  border-left: 5px solid var(--warn-border);
  background: var(--warn-bg);
}

.news-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 600;
}

.badge-project {
  background: var(--primary-soft);
  color: var(--primary);
}

.badge-priority {
  background: #fef3c7;
  color: var(--warn-text);
}

.badge-cat {
  background: var(--surface-alt);
  border: 1px solid var(--border-soft);
  color: var(--text-muted);
}

.news-title {
  margin: 0 0 6px;
  font-size: 1.04rem;
  line-height: 1.35;
}

.news-title a {
  color: var(--primary);
  text-decoration: none;
}

.news-title a:hover {
  text-decoration: underline;
}

.news-excerpt {
  margin: 0 0 8px;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.extra-links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.extra-link-chip {
  font-size: 0.8rem;
  padding: 5px 10px;
  border-radius: 8px;
  background: var(--primary-soft);
  color: var(--primary);
  text-decoration: none;
  font-weight: 600;
}

/* Config Cards */
.section-title {
  margin: 0 0 6px;
  font-size: 1.15rem;
}

.project-config-card {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-card);
  padding: 16px;
  box-shadow: var(--shadow-card);
}

.project-config-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 12px;
}

.gemini-link-box {
  background: var(--surface-alt);
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin: 10px 0;
  font-size: 0.82rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  word-break: break-all;
}

.source-item-row {
  border-top: 1px solid var(--border-soft);
  padding: 12px 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.feedback-msg {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--success-bg);
  color: var(--success-text);
  font-size: 0.88rem;
  font-weight: 600;
}

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