:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --border: #dde3ea;
  --text: #1c2530;
  --muted: #64748b;
  --accent: #6d28d9;
  --accent-soft: #ede9fe;
  --danger: #b42318;
  --radius: 8px;
}

* { box-sizing: border-box; }

/* Le regole display: grid piu sotto scavalcherebbero l'attributo hidden. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font: 15px/1.5 -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

button { font: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }

/* --- Login --- */
.login { display: grid; place-items: center; min-height: 100vh; padding: 24px; }

.login-card {
  width: min(380px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  display: grid;
  gap: 8px;
}
.login-card h1 { margin: 0; font-size: 20px; }
.login-card p { margin: 0 0 12px; color: var(--muted); font-size: 14px; }
.login-card label { font-size: 13px; font-weight: 600; }
.login-card input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
}
.login-card button {
  margin-top: 8px;
  padding: 10px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
.notice { margin-top: 16px !important; font-size: 12px; }

/* --- Layout --- */
.app {
  display: grid;
  grid-template-columns: 230px 340px 1fr;
  height: 100vh;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.brand { font-weight: 700; font-size: 16px; }


.field { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.field select, .field input {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  width: 100%;
}

.folders { display: grid; gap: 2px; }
.folder {
  text-align: left;
  padding: 8px 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
}
.folder:hover { background: var(--bg); }
.folder.is-active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.primary {
  padding: 10px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
.ghost {
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
}

.sidebar-foot {
  margin-top: auto;
  display: grid;
  gap: 8px;
  font-size: 12px;
  color: var(--muted);
}
.link { border: 0; background: none; color: var(--accent); padding: 0; text-align: left; }

/* --- Lista --- */
.list-pane {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.list-head {
  display: grid;
  gap: 10px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
}
.list-search { display: flex; gap: 8px; }

/* Su una casella condivisa sapere sempre dove si sta scrivendo conta piu dell'estetica. */
.current-mailbox {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  color: var(--accent);
  overflow-wrap: anywhere;
}
.list-head input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.icon {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 6px;
  width: 36px;
  font-size: 16px;
}

.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.list li {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: grid;
  gap: 2px;
}
.list li:hover { background: var(--bg); }
.list li.is-selected { background: var(--accent-soft); }
.list li.is-unread .row-from, .list li.is-unread .row-subject { font-weight: 700; }

.row-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.row-from { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-date { font-size: 12px; color: var(--muted); white-space: nowrap; }
.row-subject { font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-preview { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.clip { color: var(--muted); }

/* --- Lettore --- */
/* Colonna: intestazione e allegati restano in alto, il corpo prende tutto lo spazio rimasto. */
.reader {
  display: flex;
  flex-direction: column;
  padding: 24px 28px;
  min-height: 0;
  overflow: hidden;
}
.reader > h2,
.reader > .meta,
.reader > .reader-actions,
.reader > .attachments,
.reader > .images-bar { flex-shrink: 0; }
.reader h2 { margin: 0 0 12px; font-size: 20px; }
.meta { display: grid; gap: 4px; font-size: 13px; color: var(--muted); margin-bottom: 16px; }
.meta strong { color: var(--text); font-weight: 600; }

.reader-actions { display: flex; gap: 8px; margin-bottom: 20px; flex-wrap: wrap; }

.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 20px; }
.attachment {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  font-size: 13px;
}
.attachment:hover { border-color: var(--accent); color: var(--accent); }
.attachment small { color: var(--muted); }

.body-frame {
  width: 100%;
  flex: 1;
  min-height: 260px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
}
.images-bar {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 8px 12px;
  margin-bottom: 8px;
  background: #fff8e6;
  border: 1px solid #f0d99b;
  border-radius: 6px;
  font-size: 13px;
}

.empty { color: var(--muted); text-align: center; padding: 40px 20px; }
.error { color: var(--danger); font-size: 13px; margin: 4px 0 0; }

/* --- Composizione --- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 32, 0.45);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 20;
}
.compose-card {
  width: min(680px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px 24px 24px;
  display: grid;
  gap: 10px;
}
.compose-card header { display: flex; justify-content: space-between; align-items: center; }
.compose-card h2 { margin: 0; font-size: 17px; }
.compose-card textarea {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  resize: vertical;
}
.file-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 4px;
  font-size: 13px;
}
.file-list li { display: flex; justify-content: space-between; gap: 12px; color: var(--muted); }
.file-list .too-big { color: var(--danger); font-weight: 600; }

.compose-card footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.from-line { margin: 0; font-size: 13px; color: var(--muted); }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 10px 18px;
  border-radius: 6px;
  font-size: 14px;
  z-index: 30;
}
.toast.is-error { background: var(--danger); }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto auto 1fr; height: auto; }
  .sidebar { flex-direction: row; flex-wrap: wrap; align-items: center; }
  .sidebar-foot { margin-left: auto; margin-top: 0; }
  .list { max-height: 45vh; }
}
