:root {
  --border: #d8dee4;
  --bg: #ffffff;
  --bg-alt: #f6f8fa;
  --text: #1f2328;
  --text-dim: #57606a;
  --accent: #0969da;
  --unread: #0969da;
  --danger: #cf222e;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 14px;
}

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

/* ---- Login ---- */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: var(--bg-alt);
}
.login-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 32px;
  width: min(320px, calc(100vw - 32px));
}
.login-box h1 { font-size: 18px; margin: 0 0 20px; }
.field { margin-bottom: 14px; }
.field label { display: block; margin-bottom: 4px; color: var(--text-dim); font-size: 13px; }
.field input, .field textarea, .field select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
}
.btn {
  display: inline-block;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-alt);
  cursor: pointer;
  font-size: 14px;
}
.btn-primary { background: var(--accent); border-color: var(--accent); color: white; }
.btn-danger { color: var(--danger); }
.error-msg { color: var(--danger); font-size: 13px; margin-bottom: 12px; }
.success-msg { color: #1a7f37; font-size: 13px; margin-bottom: 12px; }

/* ---- App shell ---- */
.app {
  display: grid;
  grid-template-columns: 200px 320px 1fr;
  height: 100vh;
}

.sidebar {
  background: var(--bg-alt);
  border-right: 1px solid var(--border);
  padding: 16px 0;
  display: flex;
  flex-direction: column;
}
.sidebar-header { padding: 0 16px 16px; font-weight: 600; }
.sidebar nav { flex: 1; }
.folder-link {
  display: flex;
  justify-content: space-between;
  padding: 8px 16px;
  color: var(--text);
}
.folder-link.active { background: var(--bg); font-weight: 600; }
.folder-link .count { color: var(--text-dim); font-size: 12px; }
.sidebar-footer { padding: 12px 16px 0; border-top: 1px solid var(--border); font-size: 12px; color: var(--text-dim); }
.compose-btn { display: block; margin: 0 16px 16px; text-align: center; }

.message-list {
  border-right: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.list-toolbar { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.list-toolbar input[type="search"] {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
}
.message-row {
  display: block;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
.message-row:hover { background: var(--bg-alt); text-decoration: none; }
.message-row.selected { background: #ddf4ff; }
.message-row.unread .row-from,
.message-row.unread .row-subject { font-weight: 700; color: var(--unread); }
.row-top { display: flex; justify-content: space-between; gap: 8px; }
.row-from { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-date { color: var(--text-dim); font-size: 12px; white-space: nowrap; }
.row-subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.row-flags { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.empty-state { padding: 24px; color: var(--text-dim); text-align: center; }
.pagination { display: flex; justify-content: space-between; padding: 10px 12px; border-top: 1px solid var(--border); font-size: 13px; }

.reading-pane { padding: 20px 28px; overflow-y: auto; }
.reading-pane .placeholder { color: var(--text-dim); margin-top: 60px; text-align: center; }
.message-header { border-bottom: 1px solid var(--border); padding-bottom: 14px; margin-bottom: 16px; }
.message-header h2 { margin: 0 0 10px; font-size: 18px; }
.message-header dl { display: grid; grid-template-columns: 60px 1fr; gap: 2px 8px; font-size: 13px; color: var(--text-dim); margin: 0; }
.message-header dt { font-weight: 600; }
.message-header dd { margin: 0; word-break: break-word; }
.message-actions { margin-top: 14px; display: flex; gap: 8px; flex-wrap: wrap; }
.message-body { line-height: 1.5; white-space: pre-wrap; word-wrap: break-word; }
/* Sized to its content by the iframe's own onload handler (see
   message_view.html) — min-height is just what shows before that fires. */
.html-body { display: block; width: 100%; min-height: 150px; border: none; }
.back-link { display: none; }

.attachment-list { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-alt);
  color: var(--text);
  font-size: 13px;
  max-width: 100%;
}
.attachment-chip:hover { background: var(--border); text-decoration: none; }
.attachment-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-size { color: var(--text-dim); font-size: 11px; white-space: nowrap; }

/* ---- Compose ---- */
.compose-page { padding: 24px; max-width: 720px; }
.compose-page h1 { font-size: 18px; }
.compose-page textarea { min-height: 320px; resize: vertical; font-family: inherit; }
.compose-actions { margin-top: 16px; display: flex; gap: 10px; }

/* ---- Admin ---- */
.admin-page { padding: 24px; max-width: 640px; }
.admin-page h1 { font-size: 18px; }
.admin-page h2 { font-size: 15px; margin-top: 28px; }
.table-scroll { overflow-x: auto; }
.admin-table { width: 100%; border-collapse: collapse; margin-top: 12px; }
.admin-table th, .admin-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); font-size: 13px; }
.admin-table th { color: var(--text-dim); font-weight: 600; }
.admin-form { margin-top: 12px; }
.checkbox-field label { display: flex; align-items: center; gap: 6px; color: var(--text); font-size: 14px; }
/* Override .field input's width:100% (meant for text/password/email
   inputs) — without it, this checkbox becomes a flex item whose
   flex-basis resolves from that 100% width and stretches across the row,
   shoving the label text out to the far edge. */
.checkbox-field input[type="checkbox"] { width: auto; flex: 0 0 auto; }
.field-hint { color: var(--text-dim); font-size: 13px; margin: 6px 0 0; max-width: 60ch; }

/* ---- Account ---- */
.account-page { padding: 24px; max-width: 420px; }
.account-page h1 { font-size: 18px; }
.account-page h2 { font-size: 15px; margin-top: 24px; }
.account-email { color: var(--text-dim); font-size: 13px; }

/* ---- Mobile ---- */
/* Hidden on desktop; the media query below turns these into the folder
   drawer's open/close toggle. Declared here (not inside the query) so
   there's one definition of "what these elements are" regardless of
   viewport. */
.sidebar-toggle-checkbox { display: none; }
.sidebar-backdrop { display: none; }
.mobile-bar { display: none; }

@media (max-width: 768px) {
  .app {
    display: block;
    height: 100vh;
    overflow: hidden;
    position: relative;
  }

  .mobile-bar {
    display: flex;
    align-items: center;
    gap: 14px;
    height: 48px;
    padding: 0 14px;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
  }
  .menu-btn { font-size: 20px; line-height: 1; cursor: pointer; }
  .mobile-bar-title { font-weight: 600; }

  /* Folder drawer: a checkbox + <label for> pair toggles it without any
     JS. The checkbox has to be a sibling of .sidebar (both direct
     children of .app) for the ~ combinator below to reach it — its
     position in the markup doesn't otherwise matter, so the visible
     hamburger button can live inside .mobile-bar instead. */
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: 80%;
    max-width: 280px;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform .2s ease;
  }
  .sidebar-toggle-checkbox:checked ~ .sidebar {
    transform: translateX(0);
  }
  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 25;
    background: rgba(0, 0, 0, 0.4);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease;
  }
  .sidebar-toggle-checkbox:checked ~ .sidebar-backdrop {
    display: block;
    opacity: 1;
    pointer-events: auto;
  }

  .message-list {
    height: calc(100vh - 48px);
    border-right: none;
  }

  /* Reading pane covers the whole screen (over the message list) once it
     actually has a message loaded into it — including after an htmx swap
     of just #reading-pane's innerHTML, since :has() re-evaluates live.
     The mobile bar's hamburger isn't useful mid-message, so it's swapped
     out for the in-pane "Back" link at the same time. */
  .reading-pane {
    position: fixed;
    inset: 0;
    z-index: 20;
    padding: 16px;
    background: var(--bg);
    transform: translateX(100%);
    transition: transform .2s ease;
  }
  .app:has(#reading-pane article) .reading-pane {
    transform: translateX(0);
  }
  .app:has(#reading-pane article) .mobile-bar {
    display: none;
  }
  .back-link {
    display: inline-block;
    margin-bottom: 12px;
    font-weight: 600;
  }

  .compose-page,
  .admin-page,
  .account-page {
    padding: 16px;
  }
}
