* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --grad: linear-gradient(45deg, #f09433, #e6683c, #dc2743, #cc2366, #bc1888);
  --primary: #dc2743;
  --bg: #faf7f5;
  --card: #ffffff;
  --text: #26201d;
  --muted: #8a7f78;
  --border: #eee4de;
  --ok: #1d9e55;
  --err: #d43f3f;
  --radius: 14px;
}

html, body {
  /* consente solo lo scorrimento: niente pinch-zoom né doppio tocco (vale per il touch) */
  touch-action: pan-x pan-y;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.9em; font-weight: normal; }
.error { color: var(--err); font-size: 0.9em; min-height: 1.2em; margin-top: 8px; }

/* ---------- schermate di accesso ---------- */
.auth-screen {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  padding: 20px;
}
.auth-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 36px;
  max-width: 420px; width: 100%;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0,0,0,0.06);
}
.auth-card h1 { font-size: 1.4rem; margin-bottom: 8px; }
.auth-card p { color: var(--muted); margin-bottom: 20px; font-size: 0.95rem; }
.auth-card input { margin-bottom: 12px; }
.logo { font-size: 3rem; margin-bottom: 12px; }

/* ---------- header e tab ---------- */
header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 14px 24px;
  display: flex; align-items: center; justify-content: space-between;
}
.header-title { display: flex; align-items: center; gap: 12px; }
.logo-small { font-size: 1.6rem; }
.account-badge {
  display: block; font-size: 0.8rem; color: var(--muted);
}
.account-badge.connected { color: var(--ok); }

.tabs {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  display: flex; gap: 4px;
}
.tab .tab-icon { margin-right: 6px; }

/* ---- modalità iPhone: barra fissa in basso, stile app nativa ---- */
@media (max-width: 480px) {
  html, body { -webkit-text-size-adjust: 100%; overscroll-behavior-y: none; }

  .tabs {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    z-index: 50;
    border-bottom: none;
    border-top: 1px solid var(--border);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    gap: 0;
    justify-content: space-around;
    box-shadow: 0 -4px 16px rgba(0,0,0,0.05);
  }
  .tab {
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    flex: 1 1 0; padding: 4px 2px;
    border-bottom: none;
    color: var(--muted);
  }
  .tab .tab-icon { margin-right: 0; font-size: 1.35rem; line-height: 1.2; }
  .tab .tab-label { font-size: 0.66rem; font-weight: 600; }
  .tab.active { color: var(--primary); border-bottom: none; }

  /* il contenuto scorre sotto la barra senza restarne coperto */
  main { padding-bottom: calc(84px + env(safe-area-inset-bottom)); }

  /* iOS ingrandisce da solo i campi con testo < 16px: evitiamolo */
  input, select, textarea { font-size: 16px !important; }
}
.tab {
  background: none; border: none; cursor: pointer;
  padding: 12px 18px; font-size: 0.95rem; color: var(--muted);
  border-bottom: 3px solid transparent;
}
.tab.active { color: var(--text); border-bottom-color: var(--primary); font-weight: 600; }

main { max-width: 860px; margin: 0 auto; padding: 28px 20px 60px; }

.section-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 16px; margin-bottom: 20px;
}
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; margin-bottom: 6px; }

/* ---------- bottoni e campi ---------- */
.btn {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 18px;
  font-size: 0.95rem;
  cursor: pointer;
  color: var(--text);
  white-space: nowrap;
  transition: filter 0.15s;
}
.btn:hover { filter: brightness(0.96); }
.btn.primary { background: var(--grad); color: #fff; border: none; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 12px; font-size: 0.85rem; }
.btn.danger { color: var(--err); }

input[type="text"], input[type="password"], input[type="search"], input[type="date"], select, textarea {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 0.95rem;
  font-family: inherit;
  background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #f0a4b4; border-color: transparent; }

label { display: block; font-weight: 600; font-size: 0.9rem; margin: 16px 0 6px; }
.checkbox-row { display: flex; align-items: center; gap: 10px; font-weight: normal; cursor: pointer; }
.checkbox-row input { width: auto; }

.field-row { display: flex; gap: 10px; align-items: center; margin: 8px 0; }
.field-row code {
  flex: 1; background: var(--bg); padding: 10px 14px; border-radius: 10px;
  font-size: 0.85rem; word-break: break-all; border: 1px solid var(--border);
}

/* ---------- card ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 22px;
  margin-bottom: 16px;
}
.copy-field { margin-top: 14px; }
.copy-field label { margin: 0 0 4px; }

/* ---------- lista regole ---------- */
#rule-search {
  margin-bottom: 14px;
  padding: 12px 16px;
  border-radius: 12px;
}
.rule-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 12px;
  display: flex; justify-content: space-between; gap: 16px;
}
.rule-card.inactive { opacity: 0.55; }
.rule-info { flex: 1; min-width: 0; }
.rule-name { font-weight: 700; margin-bottom: 4px; }
.rule-meta { font-size: 0.85rem; color: var(--muted); margin-bottom: 6px; }
.rule-stats { display: flex; gap: 8px; margin: 4px 0 6px; }
.rule-stats span {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 999px; padding: 2px 12px;
  font-size: 0.8rem; font-weight: 600; color: var(--text);
}
.rule-message {
  font-size: 0.9rem; background: var(--bg); border-radius: 10px;
  padding: 10px 12px; white-space: pre-wrap; word-break: break-word;
}
.rule-actions { display: flex; flex-direction: column; gap: 8px; align-items: flex-end; }
.keyword-pill {
  display: inline-block; background: #fdeef1; color: var(--primary);
  border-radius: 999px; padding: 2px 12px; font-size: 0.85rem; font-weight: 600;
  margin: 2px 4px 2px 0;
}

.empty-state {
  text-align: center; padding: 48px 20px; color: var(--muted);
  background: var(--card); border: 1px dashed var(--border); border-radius: var(--radius);
}

/* ---------- test box ---------- */
.test-box {
  margin-top: 32px; background: var(--card);
  border: 1px solid var(--border); border-radius: var(--radius); padding: 20px 22px;
}
.test-row { display: flex; gap: 10px; margin-top: 10px; }
#test-result { margin-top: 14px; }
.test-match {
  background: #ecf9f1; border: 1px solid #bfe8cf; border-radius: 10px;
  padding: 14px 16px; font-size: 0.92rem; white-space: pre-wrap;
}
.test-nomatch {
  background: #fdf3f3; border: 1px solid #f2cccc; border-radius: 10px;
  padding: 14px 16px; font-size: 0.92rem;
}

/* ---------- inbox ---------- */
.inbox-row {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 16px; margin-bottom: 8px;
  cursor: pointer;
}
.inbox-row:hover { filter: brightness(0.98); }
.chat-avatar {
  width: 44px; height: 44px; border-radius: 50%; overflow: hidden;
  background: var(--grad); color: #fff; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 1.1rem;
}
.chat-avatar img { width: 100%; height: 100%; object-fit: cover; }
.inbox-info { flex: 1; min-width: 0; }
.inbox-name { font-weight: 700; }
.inbox-snippet {
  font-size: 0.85rem; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.inbox-time { font-size: 0.78rem; color: var(--muted); flex: 0 0 auto; }

.chat-header {
  display: flex; align-items: center; gap: 12px;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 14px; margin-bottom: 10px;
}
.chat-title { display: flex; flex-direction: column; line-height: 1.3; min-width: 0; }
.chat-messages {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
  height: 50vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 8px;
}
.chat-bubble {
  max-width: 78%; padding: 9px 14px; border-radius: 16px;
  font-size: 0.93rem; white-space: pre-wrap; word-break: break-word;
  position: relative;
}
.chat-bubble.theirs { background: var(--bg); align-self: flex-start; border-bottom-left-radius: 4px; }
.chat-bubble.mine { background: var(--grad); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.chat-time { display: block; font-size: 0.68rem; opacity: 0.7; margin-top: 3px; }
.chat-compose { display: flex; gap: 8px; margin-top: 10px; align-items: flex-end; }
.chat-compose textarea { resize: none; }

/* ---------- statistiche ---------- */
.period-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 18px; }
.period.active { background: var(--grad); color: #fff; border: none; font-weight: 600; }
.period-custom { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.period-custom input[type="date"] { width: auto; padding: 8px 10px; }

.stat-tiles { display: flex; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.stat-tile {
  flex: 1; min-width: 140px; background: var(--card);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 20px; text-align: center;
}
.stat-num { font-size: 2rem; font-weight: 800; }
.stat-label { color: var(--muted); font-size: 0.85rem; }

.chart {
  display: flex; align-items: flex-end; gap: 4px;
  height: 180px; padding: 10px 0 0;
}
.chart-col {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: flex-end; height: 100%; min-width: 0; position: relative;
  border-radius: 6px;
}
.chart-col:hover { background: #f6efe9; }
.chart-col:hover::after {
  content: attr(data-tip);
  position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%);
  background: #26201d; color: #fff; font-size: 0.75rem;
  padding: 6px 10px; border-radius: 8px; white-space: nowrap; z-index: 10;
}
.chart-val { font-size: 0.72rem; color: var(--muted); }
.chart-bar {
  width: 70%; max-width: 34px; background: var(--primary);
  border-radius: 4px 4px 0 0; min-height: 0;
}
.chart-day { font-size: 0.7rem; color: var(--muted); margin-top: 4px; white-space: nowrap; }
.chart-table { margin-top: 10px; }
.chart-table summary { cursor: pointer; }
.data-table { border-collapse: collapse; margin-top: 8px; width: 100%; max-width: 420px; }
.data-table th, .data-table td { text-align: left; padding: 4px 12px 4px 0; font-size: 0.85rem; border-bottom: 1px solid var(--border); }
.data-table th { color: var(--muted); font-weight: 600; }

.rank-group { border-bottom: 1px solid var(--border); padding: 6px 0; }
.rank-group:last-child { border-bottom: none; }
.rank-row { display: flex; align-items: center; gap: 12px; padding: 8px 0; }
.rank-row.rank-sub { padding: 2px 0 2px 40px; }
.rank-pos { font-weight: 800; width: 28px; text-align: right; flex: 0 0 auto; }
.rank-info { flex: 1; min-width: 0; }
.rank-name { font-size: 0.9rem; margin-bottom: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-name a { color: var(--primary); font-size: 0.8rem; }
.rank-bar { background: var(--bg); border-radius: 999px; height: 10px; overflow: hidden; }
.rank-fill { background: var(--primary); height: 100%; border-radius: 999px; }
.rank-sub .rank-fill { background: #f09433; }
.rank-value { font-size: 0.85rem; color: var(--muted); white-space: nowrap; flex: 0 0 auto; }

/* ---------- log ---------- */
.log-entry {
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 16px; margin-bottom: 8px; font-size: 0.9rem;
  display: flex; gap: 12px; align-items: baseline;
}
.log-time { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }
.log-badge {
  font-size: 0.75rem; font-weight: 700; border-radius: 999px; padding: 2px 10px; white-space: nowrap;
}
.log-badge.dm_inviato { background: #ecf9f1; color: var(--ok); }
.log-badge.ignorato { background: #f4f1ef; color: var(--muted); }
.log-badge.errore { background: #fdf3f3; color: var(--err); }
.log-badge.follow_richiesto { background: #fdf6e9; color: #b07d1a; }
.log-badge.sistema { background: #eef4fd; color: #2b6cb0; }
.log-detail { flex: 1; word-break: break-word; }

/* ---------- modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(30,20,15,0.45);
  display: flex; align-items: center; justify-content: center; padding: 20px;
  z-index: 100;
}
.modal-card {
  background: var(--card); border-radius: var(--radius);
  padding: 28px 30px; max-width: 560px; width: 100%;
  max-height: 90vh; overflow-y: auto;
}
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* ---------- selettore post a miniature ---------- */
.media-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin: 8px 0;
}
.media-thumb {
  position: relative;
  aspect-ratio: 1;
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  border: 3px solid transparent;
  background: var(--bg);
  transition: border-color 0.15s, transform 0.1s;
}
.media-thumb:hover { transform: scale(1.03); }
.media-thumb.selected { border-color: var(--primary); }
.media-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.media-noimg {
  display: flex; align-items: center; justify-content: center;
  width: 100%; height: 100%; font-size: 1.6rem;
}
.media-check {
  position: absolute; top: 4px; right: 4px;
  background: var(--primary); color: #fff;
  width: 22px; height: 22px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.8rem; font-weight: 700;
}
#media-showall { margin-bottom: 6px; }
@media (max-width: 480px) {
  .media-grid { grid-template-columns: repeat(3, 1fr); }
}

/* ---------- editor link multipli ---------- */
.link-row { display: flex; gap: 8px; margin-bottom: 8px; }
.link-row .link-label { flex: 2; }
.link-row .link-url { flex: 3; }
.link-row button { flex: 0 0 auto; }
.rule-link-line { font-size: 0.88rem; margin-top: 6px; }
.rule-link-line .lbl { font-weight: 600; }
.rule-link-line a { color: var(--primary); word-break: break-all; }

#connection-result { font-size: 0.9rem; margin-left: 10px; }

@media (max-width: 600px) {
  .section-head { flex-direction: column; }
  .rule-card { flex-direction: column; }
  .rule-actions {
    flex-direction: row;
    flex-wrap: wrap;           /* i bottoni vanno a capo invece di uscire dalla scheda */
    align-items: stretch;
  }
  .rule-actions .btn {
    flex: 1 1 40%;             /* due bottoni per riga, larghi uguali */
    text-align: center;
  }
}
