:root {
  --bg: #1c2128;
  --bg-elevated: #22272e;
  --bg-hover: #2d333b;
  --border: #444c56;
  --text: #adbac7;
  --text-strong: #e6edf3;
  --muted: #768390;
  --primary: #539bf5;
  --primary-hover: #4184e4;
  --danger: #e5534b;
  --flash: #347d39;
  --flash-error: #c93c37;
  --unread: #e6edf3;
  --sidebar-w: 220px;
  --radius: 6px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.app {
  display: flex;
  min-height: 100vh;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem 0.75rem;
  gap: 0.75rem;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand a {
  color: var(--text-strong);
  font-weight: 700;
  font-size: 1.05rem;
  text-decoration: none;
}

.compose-btn { display: block; text-align: center; margin: 0.25rem 0 0.5rem; }

.folder-nav, .secondary-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.folder-nav a, .secondary-nav a {
  color: var(--text);
  padding: 0.45rem 0.65rem;
  border-radius: var(--radius);
  text-decoration: none;
}

.folder-nav a:hover, .secondary-nav a:hover { background: var(--bg-hover); text-decoration: none; }
.folder-nav a.active, .secondary-nav a.active {
  background: var(--bg-hover);
  color: var(--text-strong);
  font-weight: 600;
}

.sidebar-footer {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.user-email {
  font-size: 0.8rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 0.5rem;
}

.main {
  flex: 1;
  padding: 1.25rem 1.5rem 2rem;
  min-width: 0;
  min-height: 0;
}

.main-split {
  padding: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
  max-height: 100vh;
  overflow: hidden;
  min-height: 0;
}

.main-split > .flash,
.main-split > .flash-error {
  margin: 0.75rem 1rem 0;
}

/* Split pane: list | reader */
.mail-split {
  display: grid;
  grid-template-columns: minmax(280px, 38%) 1fr;
  flex: 1;
  min-height: 0;
  height: 100%;
}

.mail-list-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  border-right: 1px solid var(--border);
  background: var(--bg);
  height: 100%;
  overflow: hidden;
}

.mail-read-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden; /* header stays put; body scrolls */
  padding: 1rem 1.25rem 1.5rem;
  background: var(--bg);
}

.pane-header {
  padding: 0.85rem 1rem 0.5rem;
  margin-bottom: 0;
  flex-shrink: 0;
}

.pane-header h1 {
  font-size: 1.15rem;
}

.msg-list-scroll {
  flex: 1;
  overflow: auto;
  min-height: 0;
}

.msg-list.compact {
  border: none;
  border-radius: 0;
  background: transparent;
}

.msg-list.compact td {
  padding: 0.5rem 0.65rem;
  vertical-align: top;
}

.msg-list tr.selected td {
  background: rgba(83, 155, 245, 0.15);
  box-shadow: inset 3px 0 0 var(--primary);
}

.msg-row-link {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  color: inherit;
  text-decoration: none;
}

.msg-row-link:hover { text-decoration: none; color: inherit; }
.msg-from {
  font-size: 0.88rem;
  color: var(--text-strong);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.msg-subject {
  font-size: 0.82rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.msg-list tr.unread .msg-subject { color: var(--text); font-weight: 600; }

.mail-list-pane .pager {
  padding: 0.5rem 0.75rem 0.75rem;
  margin-top: 0;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.reader-placeholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 280px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--muted);
}

.reader-placeholder-inner { text-align: center; }
.reader-placeholder-icon { font-size: 2.5rem; margin-bottom: 0.5rem; opacity: 0.6; }

.reader-header { align-items: flex-start; }
.reader-title { min-width: 0; flex: 1; }
.reader-title h1 {
  font-size: 1.2rem;
  word-break: break-word;
}

.asset-banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  background: rgba(218, 170, 63, 0.12);
  border: 1px solid #9e6a03;
  color: var(--text-strong);
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius);
  margin-bottom: 0.85rem;
  font-size: 0.9rem;
}

.asset-banner-ok {
  background: rgba(52, 125, 57, 0.15);
  border-color: var(--flash);
}

.asset-banner-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.mail-read-pane .reader-header,
.mail-read-pane .message-meta,
.mail-read-pane .asset-banner,
.mail-read-pane .attachments {
  flex-shrink: 0;
}

.mail-read-pane .message-body {
  flex: 1 1 auto;
  min-height: 0; /* critical for flex child scrolling */
  max-height: 100%;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 960px) {
  .mail-split {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(200px, 40%) 1fr;
  }
  .mail-list-pane {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 40vh;
  }
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.page-header h1 {
  margin: 0.25rem 0 0;
  color: var(--text-strong);
  font-size: 1.4rem;
}

.actions { display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center; }
form.inline { display: inline; margin: 0; }

.btn {
  display: inline-block;
  background: var(--bg-hover);
  color: var(--text-strong);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.4rem 0.75rem;
  font-size: 0.9rem;
  cursor: pointer;
  text-decoration: none;
}

.btn:hover { background: #373e47; text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { color: #fff; background: var(--danger); border-color: var(--danger); }
.btn-ghost { background: transparent; width: 100%; }
.btn-block { width: 100%; }

.flash {
  background: rgba(52, 125, 57, 0.2);
  border: 1px solid var(--flash);
  color: var(--text-strong);
  padding: 0.6rem 0.85rem;
  border-radius: var(--radius);
  margin-bottom: 1rem;
}

.flash-error {
  background: rgba(201, 60, 55, 0.15);
  border-color: var(--flash-error);
}

.empty {
  color: var(--muted);
  padding: 2rem;
  text-align: center;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.msg-list {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.msg-list th, .msg-list td {
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  font-size: 0.92rem;
}

.msg-list th {
  color: var(--muted);
  font-weight: 600;
  background: #2a3038;
}

.msg-list tr:hover td { background: var(--bg-hover); }
.msg-list tr.unread td { color: var(--unread); font-weight: 600; }
.msg-list a { color: inherit; text-decoration: none; }
.msg-list a:hover { color: var(--primary); }

.col-flag { width: 2rem; color: #daaa3f; }
.col-from { width: 20%; max-width: 14rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.col-date { width: 6.5rem; color: var(--muted); white-space: nowrap; }

.pager { margin-top: 1rem; display: flex; gap: 1rem; }

.message-meta {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  margin-bottom: 1rem;
  line-height: 1.5;
  font-size: 0.92rem;
}

.attachments {
  margin-bottom: 1rem;
  font-size: 0.92rem;
}

.attachments ul { margin: 0.35rem 0 0; padding-left: 1.2rem; }

.att-list {
  list-style: none;
  padding-left: 0 !important;
  margin: 0.5rem 0 0;
}

.att-item {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 0.75rem;
  padding: 0.45rem 0.65rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 0.4rem;
}

.att-name {
  color: var(--text-strong);
  font-weight: 500;
  word-break: break-all;
}

.att-size { font-size: 0.85rem; }

.att-actions {
  margin-left: auto;
  display: flex;
  gap: 0.35rem;
}

.btn-sm {
  padding: 0.25rem 0.55rem;
  font-size: 0.8rem;
}

.att-hint {
  margin: 0.4rem 0 0;
  font-size: 0.8rem;
}

.message-body {
  background: #fff;
  color: #222;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  min-height: 12rem;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.plain-body {
  margin: 0;
  padding: 1rem;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9rem;
  background: var(--bg-elevated);
  color: var(--text);
}

.html-body {
  padding: 1rem;
  color: #222;
  background: #fff;
  overflow-x: auto;
  line-height: 1.45;
  /* height comes from content; parent .message-body scrolls */
}

.html-body img { max-width: 100%; height: auto; }

.compose-form label {
  display: block;
  margin-bottom: 0.75rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.compose-form input[type="text"],
.compose-form input[type="email"],
.compose-form input[type="password"],
.compose-form input[type="number"],
.compose-form textarea {
  display: block;
  width: 100%;
  margin-top: 0.3rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-strong);
  padding: 0.5rem 0.65rem;
  font: inherit;
}

.compose-form textarea { resize: vertical; }

.recipient-field { margin-bottom: 0.85rem; }

.recipient-label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.3rem;
}

.recipient-label-row label {
  margin-bottom: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.contact-pick-btn { flex-shrink: 0; }

.autocomplete-wrap {
  position: relative;
}

.autocomplete-wrap .recipient-input {
  display: block;
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-strong);
  padding: 0.5rem 0.65rem;
  font: inherit;
}

.autocomplete-list {
  position: absolute;
  z-index: 40;
  left: 0;
  right: 0;
  top: calc(100% + 2px);
  margin: 0;
  padding: 0.25rem 0;
  list-style: none;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  max-height: 240px;
  overflow: auto;
}

.autocomplete-list li {
  padding: 0.45rem 0.75rem;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.autocomplete-list li:hover,
.autocomplete-list li.active {
  background: var(--bg-hover);
}

.ac-name {
  color: var(--text-strong);
  font-size: 0.9rem;
}

.ac-email {
  color: var(--muted);
  font-size: 0.8rem;
}

/* Contact picker modal */
.modal[hidden] { display: none !important; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}

.modal-card {
  position: relative;
  width: 100%;
  max-width: 420px;
  max-height: min(70vh, 520px);
  display: flex;
  flex-direction: column;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.1rem;
  color: var(--text-strong);
}

.picker-search {
  width: 100%;
  margin-bottom: 0.65rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-strong);
  padding: 0.5rem 0.65rem;
  font: inherit;
}

.picker-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow: auto;
  flex: 1;
  min-height: 0;
}

.picker-item {
  padding: 0.55rem 0.65rem;
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.picker-item:hover {
  background: var(--bg-hover);
}

.picker-empty {
  margin: 0.5rem 0 0;
  text-align: center;
}

.muted { color: var(--muted); }

/* Login */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.75rem;
}

.login-card h1 {
  margin: 0 0 0.35rem;
  color: var(--text-strong);
  font-size: 1.5rem;
}

.login-form label {
  display: block;
  margin-bottom: 0.85rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.login-form input[type="email"],
.login-form input[type="password"],
.login-form input[type="text"],
.login-form input[type="number"] {
  display: block;
  width: 100%;
  margin-top: 0.3rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-strong);
  padding: 0.55rem 0.65rem;
  font: inherit;
}

.advanced {
  margin: 0.5rem 0 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.5rem 0.75rem;
}

.advanced summary {
  cursor: pointer;
  color: var(--text);
  margin-bottom: 0.5rem;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
}

.checkbox {
  display: flex !important;
  align-items: center;
  gap: 0.4rem;
  margin-top: 1.4rem !important;
}

@media (max-width: 800px) {
  .app { flex-direction: column; }
  .sidebar {
    width: 100%;
    height: auto;
    position: relative;
  }
  .folder-nav { flex-direction: row; flex-wrap: wrap; }
  .grid-2 { grid-template-columns: 1fr; }
}
