:root {
  --bg: #f4f6fa; --panel: #ffffff; --panel-2: #f8fafc; --line: #e3e8ef;
  --text: #111827; --muted: #64748b; --accent: #2563eb; --accent-ink: #ffffff;
  --accent-soft: #e8efff; --danger: #dc2626; --unread: #2563eb; --sel: #eef3ff;
  --radius: 10px; --shadow: 0 10px 30px rgba(15, 23, 42, .12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117; --panel: #151b24; --panel-2: #10151d; --line: #263040;
    --text: #e5e9f0; --muted: #94a3b8; --accent: #4f8cff; --accent-ink: #ffffff;
    --accent-soft: #1b2a45; --danger: #f87171; --unread: #6ea0ff; --sel: #1a2436;
    --shadow: 0 10px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* Bottoni */
.btn { border: 1px solid transparent; border-radius: 8px; padding: 8px 14px; cursor: pointer; background: var(--panel-2); border-color: var(--line); }
.btn:hover { filter: brightness(.97); }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); font-weight: 600; }
.btn-ghost { background: transparent; }
.btn-block { display: block; width: 100%; }
.btn:disabled { opacity: .6; cursor: progress; }
.icon-btn { background: transparent; border: 0; width: 38px; height: 38px; border-radius: 8px; cursor: pointer; font-size: 18px; }
.icon-btn:hover { background: var(--panel-2); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Login */
.login-page { display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.login-brand { display: flex; align-items: center; gap: 12px; }
.login-brand h1 { margin: 0; font-size: 26px; }
.login-sub { color: var(--muted); margin: 6px 0 18px; }
.login-card label, .modal-card label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.login-card input, .modal-card input, .modal-card select, .modal-card textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2);
}
.login-card button { margin-top: 20px; padding: 12px; }
.alert { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); padding: 10px 12px; border-radius: 8px; margin: 10px 0; }
.notice { background: var(--accent-soft); padding: 10px 12px; border-radius: 8px; margin: 10px 0; }

/* App */
.app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; overflow: hidden; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; min-width: 0; }
.search { flex: 1; min-width: 0; }
.search input { width: 100%; max-width: 560px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 999px; background: var(--panel-2); }
.layout { flex: 1; display: grid; grid-template-columns: 250px minmax(300px, 420px) 1fr; min-height: 0; }
.sidebar { overflow-y: auto; border-right: 1px solid var(--line); background: var(--panel); padding: 10px 8px 24px; }
.side-title { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 14px 10px 6px; }
.side-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%; text-align: left; background: transparent; border: 0; border-radius: 8px; padding: 7px 10px; cursor: pointer; }
.side-item:hover { background: var(--panel-2); }
.side-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side-item .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-item.sub { padding-left: 24px; font-size: 14px; }
.badge { font-size: 12px; font-weight: 700; background: var(--accent); color: var(--accent-ink); border-radius: 999px; padding: 0 7px; min-width: 20px; text-align: center; }
.badge.muted { background: var(--line); color: var(--muted); }
.side-err { color: var(--danger); font-size: 12px; padding: 0 10px; }

.list-pane { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: var(--panel); }
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 14px 8px; }
.list-head h2 { margin: 0; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toggle { font-size: 13px; color: var(--muted); white-space: nowrap; display: flex; gap: 6px; align-items: center; }
.list-status { padding: 0 14px 6px; font-size: 13px; color: var(--muted); min-height: 1.2em; }
.list-status.err { color: var(--danger); }
.msg-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.msg { display: grid; grid-template-columns: 10px 1fr auto; gap: 2px 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.msg:hover { background: var(--panel-2); }
.msg.active { background: var(--sel); }
.msg .dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 7px; }
.msg.unread .dot { background: var(--unread); }
.msg .who { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg.unread .who, .msg.unread .subj { font-weight: 700; }
.msg .date { font-size: 12px; color: var(--muted); white-space: nowrap; }
.msg .subj, .msg .prev, .msg .acct { grid-column: 2 / 4; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .prev { color: var(--muted); font-size: 13px; }
.msg .acct { font-size: 12px; color: var(--accent); }
.msg .clip::before { content: "📎 "; }
#btn-more { margin: 10px 0; }

.reader { overflow-y: auto; background: var(--panel); min-width: 0; display: flex; flex-direction: column; }
.empty { margin: auto; color: var(--muted); padding: 40px; text-align: center; }
.r-actions { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 16px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); z-index: 1; }
.r-actions .btn { padding: 6px 10px; font-size: 14px; }
.r-head { padding: 14px 18px 10px; }
.r-head h1 { font-size: 20px; margin: 0 0 10px; overflow-wrap: anywhere; }
.r-meta { font-size: 14px; color: var(--muted); display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; }
.r-meta b { color: var(--text); font-weight: 600; }
.r-meta span { overflow-wrap: anywhere; }
.r-note { margin: 8px 18px 0; padding: 8px 12px; background: var(--accent-soft); border-radius: 8px; font-size: 14px; display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.r-body { padding: 8px 18px 24px; flex: 1; }
.r-body iframe { width: 100%; border: 0; min-height: 300px; background: #fff; border-radius: 8px; }
.r-text { white-space: pre-wrap; overflow-wrap: anywhere; font-family: inherit; margin: 0; }
.r-atts { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 18px 18px; }
.att { display: inline-flex; gap: 6px; align-items: center; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; text-decoration: none; color: var(--text); font-size: 14px; max-width: 100%; }
.att span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att small { color: var(--muted); }
.back { display: none; }

/* Modali */
.modal { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal-card { width: 100%; max-width: 640px; max-height: 100%; overflow-y: auto; background: var(--panel); border-radius: 14px; padding: 18px 20px; box-shadow: var(--shadow); }
.modal-card.small { max-width: 400px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-card textarea { resize: vertical; min-height: 180px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.hint { font-size: 12px; color: var(--muted); margin: 6px 0 0; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--panel); padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); z-index: 30; max-width: calc(100% - 32px); }
.toast.err { background: var(--danger); color: #fff; }

.only-mobile { display: none; }

/* Tablet: lista + lettura, sidebar a scomparsa */
@media (max-width: 1100px) {
  .layout { grid-template-columns: minmax(280px, 380px) 1fr; }
  .only-mobile { display: inline-block; }
  .sidebar { position: fixed; top: 55px; bottom: 0; left: 0; width: 280px; z-index: 10; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .layout.menu-open .sidebar { transform: none; }
}
/* Telefono: un pannello alla volta */
@media (max-width: 720px) {
  .layout { grid-template-columns: 1fr; }
  .brand span { display: none; }
  #btn-compose { padding: 8px 10px; }
  .reader { display: none; }
  .layout.reading .list-pane { display: none; }
  .layout.reading .reader { display: flex; }
  .back { display: inline-block; }
  .r-head h1 { font-size: 18px; }
  .r-body { padding: 8px 10px 20px; }
}
