/* ====== Base, mobile-first (iPhone Safari, 375-430px) ====== */
:root {
  --bg: #f7f7f9;
  --card: #ffffff;
  --text: #1c1c1e;
  --text-secondary: #6e6e73;
  --accent: #0a7d3b;
  --accent-dark: #086a32;
  --border: #e3e3e8;
  --danger: #d92d20;
  --summary-bg: #eef8f1;
  --summary-border: #bfe5cc;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

.hidden { display: none !important; }

.screen { min-height: 100dvh; display: flex; flex-direction: column; }

/* ====== Bottoni (target tap min 44px) ====== */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  min-height: 44px;
  padding: 0 16px;
  cursor: pointer;
}
.btn:active { opacity: 0.7; }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:disabled { background: #a7c9b4; border-color: #a7c9b4; cursor: not-allowed; }
.btn-block { width: 100%; }
.btn-small { min-height: 36px; font-size: 14px; padding: 0 12px; font-weight: 500; }

/* ====== Login ====== */
#screen-login { justify-content: center; align-items: center; padding: 24px; }
.login-box { width: 100%; max-width: 360px; text-align: center; }
.login-logo { width: min(260px, 80vw); height: auto; margin-bottom: 20px; }
.login-box h1 { font-size: 24px; margin: 0 0 24px; }
#login-form { display: flex; flex-direction: column; gap: 12px; }
#login-password {
  border: 1px solid var(--border);
  border-radius: 12px;
  min-height: 48px;
  padding: 0 14px;
  font-size: 16px;
  background: var(--card);
}
.error-msg { color: var(--danger); font-size: 14px; margin-top: 12px; }

/* ====== Header logo ====== */
.brand-header {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 68px;
  padding: 14px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.brand-logo {
  display: block;
  width: min(220px, 70vw);
  height: auto;
}

/* ====== Topbar / barra di ricerca sticky ====== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(247, 247, 249, 0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  margin-top: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
}
.search-icon { color: var(--text-secondary); flex-shrink: 0; margin-left: 4px; }
#search-input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 12px;
  min-height: 44px;
  padding: 0 12px;
  font-size: 16px;
}
.search-btn { flex-shrink: 0; }
.reset-btn {
  flex-shrink: 0;
  font-weight: 500;
  color: var(--text-secondary);
  padding: 0 12px;
}

/* ====== Contenuto ====== */
.content { flex: 1; padding: 12px 12px calc(170px + var(--safe-bottom)); }

/* ====== Tab bar (stile app mobile) ====== */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 40;
  display: flex;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}
.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 6px;
  min-height: 52px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
}
.tab-btn svg { width: 24px; height: 24px; }
.tab-btn.active { color: var(--accent); }
.tab-logout { color: var(--danger); }

/* ====== Home: elenco caselle ====== */
.home-topbar { display: flex; flex-direction: column; gap: 10px; text-align: center}
.page-title { font-size: 20px; font-weight: 700; margin: 8px 0; }
.home-search {
  display: flex;
  align-items: center;
  gap: 4px;
}
#home-search-input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 12px;
  min-height: 44px;
  padding: 0 12px;
  font-size: 16px;
}
.home-list { list-style: none; margin: 0; padding: 0; }
.home-list li { margin-bottom: 8px; }
.home-link {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  min-height: 56px;
  font-size: 15px;
  text-align: left;
  cursor: pointer;
}
.home-link:active { opacity: 0.7; }
.home-link .mb-name { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.home-link .chevron { font-size: 22px; }

/* ====== Posta in arrivo ====== */
.inbox-title { flex: 1; min-width: 0; display: flex; flex-direction: column; margin-left: 6px; }
.inbox-title span:first-child {
  font-weight: 700;
  font-size: 16px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.inbox-mail {
  font-size: 12px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 14px 0;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 600;
}
.pagination .btn { min-width: 52px; font-size: 20px; }
.pagination .btn:disabled { opacity: 0.35; cursor: not-allowed; }

/* ====== Pannello caselle ====== */
.mailbox-panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  margin-bottom: 12px;
  overflow: hidden;
}
.mailbox-panel summary {
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 48px;
  padding: 0 14px;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
.mailbox-panel summary::-webkit-details-marker { display: none; }
.chevron { color: var(--text-secondary); transition: transform 0.2s; font-size: 20px; }
.mailbox-panel[open] .chevron { transform: rotate(90deg); }
.mailbox-actions {
  display: flex;
  gap: 8px;
  padding: 0 14px 10px;
}
.mailbox-list { list-style: none; margin: 0; padding: 0 6px 8px; max-height: 40dvh; overflow-y: auto; }
.mailbox-list li { border-top: 1px solid var(--border); }
.mailbox-list label {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 4px 8px;
  cursor: pointer;
}
.mailbox-list .mb-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mailbox-list .mb-mail { color: var(--text-secondary); font-size: 13px; display: block; }
input[type="checkbox"] {
  width: 22px;
  height: 22px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

/* ====== Risultati ====== */
.results-info { color: var(--text-secondary); font-size: 14px; padding: 2px 4px 8px; }
.results-list { list-style: none; margin: 0; padding: 0; }
.result-item {
  display: flex;
  gap: 10px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 8px;
}
.result-check {
  display: flex;
  align-items: flex-start;
  padding-top: 2px;
  min-width: 32px;
  min-height: 44px;
}
.result-main { flex: 1; min-width: 0; cursor: pointer; }
.result-row1 { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.result-from { font-weight: 700; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-date { color: var(--text-secondary); font-size: 12px; flex-shrink: 0; }
.result-subject { font-size: 15px; margin: 2px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-preview {
  color: var(--text-secondary);
  font-size: 13px;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.result-mailbox {
  display: inline-block;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 11px;
  padding: 1px 6px;
  margin-top: 6px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-state { text-align: center; color: var(--text-secondary); padding: 48px 24px; }
.empty-logo { width: 64px; opacity: 0.5; margin-bottom: 12px; }
.empty-hint { font-size: 14px; }

/* ====== Toolbar selezione (sticky bottom) ====== */
.selection-toolbar {
  position: fixed;
  bottom: calc(58px + var(--safe-bottom)); /* sopra la tab bar */
  left: 0;
  right: 0;
  z-index: 30;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.selection-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  font-weight: 600;
}
.selection-limit { color: var(--danger); font-size: 13px; margin: 0; text-align: center; }

/* ====== Dettaglio ====== */
.detail-topbar { display: flex; align-items: center; }
.back-btn { border: none; background: transparent; color: var(--accent); font-size: 17px; padding: 0 4px; }
.detail-content { padding-bottom: calc(24px + var(--safe-bottom)); }
#detail-subject { font-size: 20px; margin: 4px 0 12px; line-height: 1.3; }
.detail-meta {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  font-size: 14px;
  margin-bottom: 12px;
}
.detail-meta p { margin: 4px 0; word-break: break-word; }
.detail-mailbox span { color: var(--text-secondary); }
.detail-body {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  font-size: 15px;
  line-height: 1.5;
  overflow-x: auto;
  word-break: break-word;
}
.detail-body img { max-width: 100%; height: auto; }
.detail-body table { max-width: 100%; }
.detail-body pre { white-space: pre-wrap; font-family: inherit; margin: 0; }
.summarize-btn { margin-top: 16px; }

/* ====== Card riassunto AI ====== */
.summary-card {
  background: var(--summary-bg);
  border: 1px solid var(--summary-border);
  border-radius: 14px;
  padding: 14px;
  margin-top: 16px;
  font-size: 15px;
  line-height: 1.5;
}
.summary-card h4 { margin: 0 0 8px; font-size: 15px; color: var(--accent-dark); }
.summary-card ul { margin: 6px 0; padding-left: 20px; }
.summary-card p { margin: 6px 0; }

/* ====== Modale ====== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.modal-card {
  background: var(--card);
  border-radius: 18px 18px 0 0;
  width: 100%;
  max-width: 560px;
  max-height: 88dvh;
  display: flex;
  flex-direction: column;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 17px; }
.modal-close {
  border: none;
  background: var(--bg);
  border-radius: 50%;
  width: 32px;
  height: 32px;
  font-size: 15px;
  cursor: pointer;
  color: var(--text-secondary);
}
.modal-body {
  padding: 16px;
  padding-bottom: calc(24px + var(--safe-bottom));
  overflow-y: auto;
  font-size: 15px;
  line-height: 1.55;
}
.modal-body ul { padding-left: 20px; }

/* ====== Loading / toast ====== */
.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(247, 247, 249, 0.75);
  backdrop-filter: blur(4px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: var(--text-secondary);
  font-weight: 600;
}
.spinner {
  width: 36px;
  height: 36px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.spinner-inline {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 3px solid rgba(255, 255, 255, 0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: -3px;
}

.toast {
  position: fixed;
  bottom: calc(160px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 70;
  background: #2c2c2e;
  color: #fff;
  font-size: 14px;
  padding: 10px 18px;
  border-radius: 12px;
  max-width: 90vw;
  text-align: center;
}

/* ====== Schermi più larghi (tablet/desktop) ====== */
@media (min-width: 640px) {
  .content, .topbar { max-width: 640px; margin-left: auto; margin-right: auto; width: 100%; }
  .topbar { left: 0; right: 0; }
  .modal { align-items: center; }
  .modal-card { border-radius: 18px; }
}
