/* Email-specific reader, compose, attachment and folder widgets inside crud_shell. */
/* Webmail — Gmail-inspired stylesheet */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --blue: var(--accent-background);
  --blue-dark: var(--accent-strong);
  --bg: var(--page-background);
  --surface: var(--surface-raised);
  --border: color-mix(in srgb, var(--main-font-color) 18%, transparent);
  --text: var(--main-font-color);
  --text2: color-mix(in srgb, var(--main-font-color) 75%, var(--main-background));
  --text3: var(--banner-subtext);
  --unread-bg: var(--main-background);
  --read-bg: var(--surface-subtle);
  --hover-bg: var(--surface-subtle);
  --sel-bg: var(--accent-soft-background);
  --err: var(--status-failed-background);
  --ok: var(--status-success-background);
  --warn: var(--status-warning-font-color);
  --shadow: var(--shadow-soft);
  --radius:    8px;
}

html, body { height: 100%; font-family: var(--page-font-family); font-size: 14px; color: var(--text); background: var(--bg); }

/* ── Top bar ─────────────────────────────────────────────────────────────── */

.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 100;
  height: 64px; background: var(--surface);
  display: flex; align-items: center; padding: 0 8px;
  border-bottom: 1px solid var(--border);
}

.topbar-left  { display: flex; align-items: center; width: 256px; min-width: 0; }
.topbar-right { display: flex; align-items: center; margin-left: auto; }

.menu-btn {
  width: 40px; height: 40px; border: none; background: none; cursor: pointer;
  display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 5px;
  border-radius: 50%;
}
.menu-btn:hover { background: var(--hover-bg); }
.menu-btn span  { display: block; width: 20px; height: 2px; background: var(--text2); border-radius: 1px; }

.app-logo { display: flex; align-items: center; gap: 6px; padding-left: 4px; font-size: 20px; color: var(--text2); font-weight: 400; }
.app-logo svg { flex-shrink: 0; }

.topbar-search { flex: 1; max-width: 720px; padding: 0 16px; }
.search-wrap {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface-subtle); border-radius: 24px; padding: 0 16px; height: 46px;
  transition: background .15s;
}
.search-wrap:focus-within { background: var(--surface); box-shadow: var(--shadow); }
.search-icon { flex-shrink: 0; }
.search-wrap input { flex: 1; border: none; outline: none; background: none; font-size: 16px; color: var(--text); }
#searchScope { max-width: 100px; color: var(--text2); border: none; background: transparent; font-size: 12px; }
.shortcut-toggle { display: flex; align-items: center; gap: 8px; }
.msg-folder-tag { flex-shrink: 0; max-width: 100px; overflow: hidden; text-overflow: ellipsis; font-size: 11px; color: var(--text2); background: var(--bg); border-radius: 4px; padding: 3px 6px; }
.upload-chip { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.upload-status { font-size: 11px; color: var(--text2); }
.upload-error { color: var(--err); font-size: 12px; margin-top: 4px; }
.upload-error:empty { display: none; }
.upload-chip.failed { border-color: var(--err); }
.upload-chip progress { width: 90px; height: 8px; accent-color: var(--blue); }
.compose-modal.drop-active { outline: 3px dashed var(--blue); outline-offset: -3px; }
.compose-modal.drop-active .compose-attachments { background: var(--sel-bg); }
.undo-toast { position: fixed; bottom: 24px; left: 24px; z-index: 400; padding: 14px 20px; background: var(--main-font-color); color: var(--accent-contrast); border-radius: 8px; box-shadow: var(--shadow); display: flex; align-items: center; gap: 24px; max-width: calc(100vw - 32px); }
.undo-toast[hidden] { display: none; }
.undo-toast button { background: none; color: var(--accent-background); border: none; cursor: pointer; font-weight: 700; }
.modal-overlay.preview-overlay.open { display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,.45); }
.preview-modal, .shortcut-modal { width: 900px; max-width: calc(100vw - 32px); background: var(--surface); border-radius: 8px; overflow: hidden; box-shadow: var(--shadow); }
.shortcut-modal { width: 480px; }
#previewContent { height: 70vh; padding: 12px; display: flex; align-items: center; justify-content: center; }
#previewContent img { max-width: 100%; max-height: 100%; object-fit: contain; }
.pdf-preview { width:100%; height:100%; overflow:auto; text-align:center; }
.pdf-preview canvas { max-width:100%; height:auto; background:var(--main-background); }
.pdf-controls { display:flex; justify-content:center; align-items:center; gap:12px; margin-bottom:12px; flex-wrap:wrap; }
.shortcut-content { padding: 20px; }
.shortcut-content dl { display: grid; grid-template-columns: 120px 1fr; gap: 10px; margin-top: 16px; }
.shortcut-content dt { font-family: monospace; font-weight: 700; }

.user-menu { position: relative; cursor: pointer; padding: 4px 8px; border-radius: 4px; }
.user-menu:hover .user-dropdown { display: block; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; background: var(--blue);
  color: var(--accent-contrast); font-size: 15px; font-weight: 500;
  display: flex; align-items: center; justify-content: center;
}
.user-dropdown {
  display: none; position: absolute; right: 0; top: 100%; min-width: 240px;
  background: var(--surface); border-radius: var(--radius); box-shadow: 0 4px 16px rgba(0,0,0,.18);
  overflow: hidden; z-index: 200;
}
.user-info { padding: 16px; border-bottom: 1px solid var(--border); }
.user-name  { font-weight: 500; font-size: 15px; }
.user-email { color: var(--text2); font-size: 13px; margin-top: 2px; }
.dropdown-item { display: block; padding: 12px 16px; color: var(--text); text-decoration: none; }
.dropdown-item:hover { background: var(--hover-bg); }
.dropdown-button { width: 100%; text-align: left; border: 0; background: var(--surface); font: inherit; cursor: pointer; }

.password-modal {
  width: 400px; max-width: calc(100vw - 32px); margin: 10vh auto 0;
  background: var(--surface); border-radius: var(--radius); box-shadow: 0 8px 32px rgba(0,0,0,.28);
  overflow: hidden;
}
.password-form { padding: 24px; }
.password-status { min-height: 20px; margin-bottom: 12px; color: var(--text2); }
.password-status.error { color: var(--err); }
.password-status.success { color: var(--ok); }

/* ── Layout ──────────────────────────────────────────────────────────────── */

.app-layout {
  display: flex; padding-top: 64px; height: 100vh;
}

.sidebar {
  width: 256px; min-width: 256px; height: calc(100vh - 64px);
  overflow-y: auto; overflow-x: hidden; padding: 8px 0;
  background: var(--bg); flex-shrink: 0;
  transition: width .2s, min-width .2s;
}
.sidebar.collapsed { width: 72px; min-width: 72px; }

.content {
  flex: 1; overflow: hidden; height: calc(100vh - 64px);
  background: var(--surface); border-radius: 16px 0 0 0;
  position: relative;
}

/* ── Compose button ──────────────────────────────────────────────────────── */

.compose-btn {
  display: flex; align-items: center; gap: 12px;
  margin: 8px 8px 8px 16px; padding: 18px 24px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; cursor: pointer; font-size: 14px; font-weight: 500;
  color: var(--text); box-shadow: var(--shadow);
  transition: box-shadow .15s;
}
.compose-btn:hover { box-shadow: 0 2px 6px rgba(0,0,0,.18); }

/* ── Folder list ─────────────────────────────────────────────────────────── */

.folder-list { list-style: none; padding: 4px 0; }
.folder-list li a {
  display: flex; align-items: center; gap: 16px;
  padding: 8px 16px 8px 26px; border-radius: 0 20px 20px 0; margin-right: 8px;
  text-decoration: none; color: var(--text); font-size: 14px; font-weight: 400;
  transition: background .1s;
}
.folder-list li a:hover    { background: var(--hover-bg); }
.folder-list li a.active   { background: var(--sel-bg); font-weight: 700; }
.folder-icon { width: 20px; height: 20px; flex-shrink: 0; color: var(--text2); }
.folder-name { flex: 1; }
.folder-badge {
  font-size: 12px; font-weight: 700; color: var(--text);
  min-width: 18px; text-align: right;
}

/* ── List view ───────────────────────────────────────────────────────────── */

.list-view, .read-view { position: absolute; inset: 0; overflow-y: auto; }
.list-view, .read-view { display: none; }
.list-view.active, .read-view.active { display: block; }

.list-toolbar {
  display: flex; align-items: center; gap: 4px; padding: 8px 16px;
  border-bottom: 1px solid var(--border); position: sticky; top: 0;
  background: var(--surface); z-index: 10;
}
.list-toolbar, .read-toolbar { flex-wrap: wrap; }
.list-filters { display: flex; align-items: center; gap: 8px; padding: 8px 16px; border-bottom: 1px solid var(--border); }
.filter-btn { border: 1px solid var(--border); background: var(--surface); color: var(--text2); border-radius: 16px; padding: 5px 12px; cursor: pointer; }
.filter-btn.active { background: var(--sel-bg); color: var(--blue); border-color: var(--blue); }
.selected-action:disabled { display: none; }
select.tb-btn { max-width: 160px; border: 1px solid var(--border); }

.check-all { display: flex; align-items: center; padding: 4px 8px; }
.check-all input { width: 16px; height: 16px; cursor: pointer; accent-color: var(--blue); }

.tb-btn {
  display: flex; align-items: center; gap: 4px; border: none;
  background: none; cursor: pointer; padding: 6px 8px; border-radius: 4px;
  color: var(--text2); font-size: 13px;
}
.tb-btn:hover:not(:disabled) { background: var(--hover-bg); }
.tb-btn:disabled { opacity: .4; cursor: default; }
.back-btn { font-size: 14px; color: var(--text); }

.list-status { color: var(--text2); font-size: 13px; margin-left: 4px; }
.list-pager  { margin-left: auto; display: flex; align-items: center; gap: 4px; }
#pagerInfo   { font-size: 13px; color: var(--text2); min-width: 80px; text-align: center; }

/* ── Message row ─────────────────────────────────────────────────────────── */

.msg-row {
  display: flex; align-items: center; gap: 4px;
  padding: 0 16px; border-bottom: 1px solid var(--border);
  height: 52px; cursor: pointer; transition: background .1s;
  background: var(--unread-bg);
}
.msg-row.read   { background: var(--read-bg); }
.msg-row:hover  { box-shadow: inset 1px 0 0 var(--border), inset -1px 0 0 var(--border), 0 2px 4px rgba(0,0,0,.1); z-index: 1; position: relative; }
.msg-row.selected { background: var(--sel-bg); }

.msg-check  { padding: 4px 8px; flex-shrink: 0; }
.msg-check input { width: 16px; height: 16px; cursor: pointer; accent-color: var(--blue); }

.msg-star {
  padding: 4px 4px; flex-shrink: 0; color: var(--border); cursor: pointer; font-size: 16px;
  background: none; border: none;
}
.msg-star.starred { color: var(--main-font-color); }
.msg-star:hover   { color: var(--main-font-color); }

.msg-from    { width: 200px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex-shrink: 0; }
.msg-row.read .msg-from { font-weight: 400; }
.msg-middle  { flex: 1; overflow: hidden; display: flex; align-items: center; gap: 8px; min-width: 0; }
.msg-subject { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex-shrink: 0; max-width: 300px; }
.msg-row.read .msg-subject { font-weight: 400; }
.msg-snippet { color: var(--text2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
.msg-snippet::before { content: ' — '; }
.msg-date    { width: 80px; text-align: right; font-size: 12px; color: var(--text2); flex-shrink: 0; }
.msg-row:not(.read) .msg-date { font-weight: 700; color: var(--text); }

/* empty state */
.empty-state { padding: 64px 32px; text-align: center; color: var(--text2); }
.empty-state svg { opacity: .3; margin-bottom: 16px; }

/* loading spinner */
.loading { padding: 32px; text-align: center; color: var(--text2); }
.loading::after { content: ''; display: inline-block; width: 20px; height: 20px; border: 2px solid var(--border); border-top-color: var(--blue); border-radius: 50%; animation: spin .6s linear infinite; vertical-align: middle; margin-left: 8px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Read view ───────────────────────────────────────────────────────────── */

.read-toolbar {
  display: flex; align-items: center; gap: 4px; padding: 8px 16px;
  border-bottom: 1px solid var(--border); position: sticky; top: 0;
  background: var(--surface); z-index: 10;
}

#messageContent { padding: 32px 48px; max-width: 900px; }

.msg-header { margin-bottom: 24px; }
.msg-subject-big { font-size: 22px; font-weight: 400; color: var(--text); margin-bottom: 16px; word-break: break-word; }
.msg-meta { display: flex; align-items: flex-start; gap: 12px; }
.msg-avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--blue); color: var(--accent-contrast); font-size: 17px; font-weight: 500; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.msg-meta-details { flex: 1; }
.msg-from-full { font-weight: 500; }
.msg-to-line   { font-size: 13px; color: var(--text2); margin-top: 2px; }
.msg-date-full { font-size: 12px; color: var(--text2); margin-top: 2px; }

.msg-body { margin-top: 24px; line-height: 1.6; word-wrap: break-word; }
.msg-body iframe { width: 100%; border: none; }
.msg-body a { color: var(--blue); }
.msg-body pre { white-space: pre-wrap; font-family: monospace; }

.attachments { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }
.attachments h4 { font-size: 13px; color: var(--text2); margin-bottom: 8px; }
.attachment-chip { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); border-radius: 4px; padding: 6px 12px; font-size: 13px; color: var(--text); }

/* ── Compose modal ───────────────────────────────────────────────────────── */

.modal-overlay {
  display: none; position: fixed; inset: 0; z-index: 300;
  pointer-events: none;
}
.modal-overlay.open { display: block; pointer-events: auto; }

.compose-modal {
  position: absolute; bottom: 0; right: 24px;
  width: 560px; max-width: calc(100vw - 32px);
  background: var(--surface); border-radius: 8px 8px 0 0;
  box-shadow: 0 8px 32px rgba(0,0,0,.28);
  display: flex; flex-direction: column; max-height: 80vh;
}

.compose-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; background: var(--main-font-color); border-radius: 8px 8px 0 0; color: var(--accent-contrast);
  font-size: 14px; font-weight: 500;
}
.modal-close { background: none; border: none; color: var(--accent-contrast); font-size: 20px; cursor: pointer; line-height: 1; }
.modal-close:hover { opacity: .75; }

.compose-fields { border-bottom: 1px solid var(--border); }
.compose-field  { display: flex; align-items: center; border-bottom: 1px solid var(--border); padding: 4px 16px; }
.compose-field label { width: 52px; flex-shrink: 0; color: var(--text2); font-size: 13px; }
.compose-field input { flex: 1; border: none; outline: none; font-size: 14px; padding: 6px 0; }
.compose-field--between { align-items: stretch; }
.compose-subject-wrap   { display: flex; align-items: center; flex: 1; gap: 8px; }
.compose-subject-wrap label { width: 52px; flex-shrink: 0; color: var(--text2); font-size: 13px; }
.compose-subject-wrap input { flex: 1; border: none; outline: none; font-size: 14px; padding: 6px 0; }
.compose-cc-links { display: flex; align-items: center; gap: 4px; padding-right: 4px; }

#composeBody {
  flex: 1; border: none; outline: none; resize: none; padding: 12px 16px;
  font-size: 14px; font-family: inherit; min-height: 200px; line-height: 1.5;
}

.compose-footer {
  display: flex; align-items: center; gap: 8px; padding: 12px 16px;
  border-top: 1px solid var(--border);
}
.compose-status  { font-size: 13px; color: var(--text2); flex: 1; }
.compose-discard { margin-left: auto; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */

.btn-primary {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 10px 24px; background: var(--blue); color: var(--accent-contrast);
  border: none; border-radius: 4px; font-size: 14px; font-weight: 500;
  cursor: pointer; transition: background .15s;
}
.btn-primary:hover { background: var(--blue-dark); }
.btn-block { width: 100%; justify-content: center; }

.btn-ghost {
  display: inline-flex; align-items: center;
  padding: 8px 12px; background: none; border: none; border-radius: 4px;
  color: var(--text2); font-size: 14px; cursor: pointer;
}
.btn-ghost:hover { background: var(--hover-bg); }

.link-btn { background: none; border: none; color: var(--text2); font-size: 13px; cursor: pointer; text-decoration: underline; }

/* ── Login page ──────────────────────────────────────────────────────────── */

.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: var(--bg); }
.login-card {
  width: 400px; max-width: calc(100vw - 32px);
  background: var(--surface); border-radius: 8px;
  padding: 48px 40px 36px; box-shadow: var(--shadow);
}
.login-logo { display: flex; align-items: center; gap: 10px; margin-bottom: 32px; font-size: 22px; font-weight: 400; }
.login-error { background: var(--status-failed-background); color: var(--status-failed-font-color); border-radius: 4px; padding: 10px 14px; margin-bottom: 16px; font-size: 14px; }
.form-group { margin-bottom: 20px; }
.form-group label { display: block; font-size: 13px; color: var(--text2); margin-bottom: 6px; }
.form-group input {
  width: 100%; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: 4px; font-size: 15px; outline: none;
  transition: border-color .15s;
}
.form-group input:focus { border-color: var(--blue); }

/* File selection and download controls */
.compose-attachments { padding: 8px 16px; border-top: 1px solid var(--border); max-height: 120px; overflow-y: auto; }
.attachment-picker { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.attachment-help { color: var(--text2); font-size: 12px; }
.compose-file-list { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; list-style: none; }
.compose-file-list .attachment-chip { max-width: 100%; }
.attachment-name { overflow-wrap: anywhere; }
.attachment-remove { border: none; background: none; color: var(--text2); cursor: pointer; font-size: 18px; padding: 0 4px; }
.attachments a.attachment-chip { text-decoration: none; margin: 0 6px 6px 0; }
.attachments a.attachment-chip:hover { background: var(--hover-bg); }
.msg-row:focus-visible { outline: 2px solid var(--blue); outline-offset: -2px; }
@media (max-width: 700px) {
  .sidebar { position: fixed; left: 0; top: 64px; width: 220px; min-width: 220px; z-index: 30; box-shadow: var(--shadow); }
  .sidebar.collapsed { display: none; }
  .compose-btn { padding: 12px; margin: 8px; }
  .topbar-left { width: auto; flex-shrink: 0; }
  .topbar-search { min-width: 0; padding: 0 8px; }
  .search-wrap { padding: 0 10px; gap: 6px; }
  .search-wrap input { min-width: 0; width: 100%; font-size: 14px; }
  .app-logo span { display: none; }
  .msg-from { width: 90px; min-width: 90px; }
  .msg-row { padding: 0 8px; }
  .msg-date { width: 56px; }
  .msg-middle { display: block; }
  .msg-subject { display: block; max-width: 100%; }
  .msg-snippet { display: none; }
  .msg-folder-tag { max-width: 52px; font-size: 10px; }
  #searchScope { max-width: 74px; font-size: 11px; }
  .list-filters { flex-wrap: wrap; }
  .compose-modal { right: 8px; max-width: calc(100vw - 16px); }
}

@media (max-width:480px) { .topbar-left .app-logo { font-size:16px; } #searchScope { display:none; } }
