/* Webmail styles - light/dark via CSS variables */

/* fix(round17): the [hidden] attribute must always win over author display rules.
   .modal { display:flex } overrode the UA's [hidden] { display:none }, so the
   compose modal could never be closed (X / Discard / Escape / after Send) and
   .addr-row (Cc/Bcc) + .reader-toolbar were stuck visible. Canonical guard: */
[hidden] { display: none !important; }

/* fix(round18): a CLOSED <dialog> must never render inline on the page.
   .modal-box { display:flex } overrode the UA's dialog:not([open]) { display:none },
   so the admin create-user form was stuck visible at the very bottom of the page.
   (The round-17 [hidden] rule does not apply here: <dialog> uses [open], not [hidden].) */
dialog.modal-box:not([open]) { display: none; }

/* round18: log detail popup + user-created popup */
.modal-body { padding: 14px 18px; overflow-y: auto; min-height: 0; }
.detail-pre {
  margin: 0; padding: 12px 14px; background: var(--panel-2);
  border: 1px solid var(--border); border-radius: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px; line-height: 1.6; white-space: pre-wrap;
  word-break: break-word; direction: ltr; text-align: left;
  max-height: 46vh; overflow-y: auto; color: var(--text);
}
.uc-note { margin: 0 0 12px; font-size: 13.5px; color: var(--text); }
.uc-rows { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; align-items: center; }
.uc-key { font-size: 12.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.uc-val { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13.5px;
          direction: ltr; text-align: left; word-break: break-all; }
.uc-val.uc-pass { font-weight: 700; color: var(--text); }

/* round18: keep the Detail button clickable - the truncated log text shrinks
   inside its own span while the button stays pinned at the cell end. */
.log-detail { display: flex; align-items: center; gap: 8px; }
.log-detail-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
                   min-width: 0; flex: 1 1 auto; }
.log-detail .btn { flex: 0 0 auto; }

:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #f0f2f5;
  --text: #1f2430;
  --muted: #6b7280;
  --border: #e3e6ea;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 4px 16px rgba(16, 24, 40, .06);
  --radius: 10px;
  --sidebar-w: 232px;
  --topbar-h: 56px;
}
[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --text: #e5e7eb;
  --muted: #9aa3af;
  --border: #2a2f3a;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --danger: #ef4444;
  --ok: #22c55e;
  --warn: #f59e0b;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 6px 20px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, "Noto Sans", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14.5px;
  line-height: 1.45;
}
button { font: inherit; color: inherit; }
a { color: var(--primary); }
.muted { color: var(--muted); }
code { background: var(--panel-2); padding: 1px 5px; border-radius: 5px; font-size: .92em; }

/* ---------- buttons / fields ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  border: 1px solid transparent; border-radius: 8px;
  padding: 8px 14px; cursor: pointer; background: var(--panel-2);
  transition: background .15s, border-color .15s, box-shadow .15s;
  white-space: nowrap;
}
.btn:hover { background: var(--border); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-ghost:hover { background: var(--panel-2); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: rgba(220, 38, 38, .08); }
.btn-block { width: 100%; justify-content: center; }
.btn-mini { padding: 4px 9px; font-size: 13px; border-radius: 7px; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 8px; border: none;
  background: transparent; cursor: pointer; display: inline-flex;
  align-items: center; justify-content: center; color: var(--muted);
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; }
.link-btn { background: none; border: none; color: var(--primary); cursor: pointer; padding: 4px 6px; }
.link-btn:hover { text-decoration: underline; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; }
.field input, .field select, .field textarea, .toolbar input, .toolbar select {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border);
  border-radius: 8px; background: var(--panel); color: var(--text);
}
.field input:focus, .field select:focus, .toolbar input:focus {
  outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent;
}
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }
.hint { font-size: 12.5px; }
.chk { display: flex; align-items: center; gap: 8px; margin: 10px 0; }
.chk input { width: 16px; height: 16px; }

.alert { padding: 10px 12px; border-radius: 8px; margin-bottom: 14px; font-size: 13.5px; }
.alert-error { background: rgba(220, 38, 38, .1); color: var(--danger); border: 1px solid rgba(220, 38, 38, .3); }

.chip {
  display: inline-block; padding: 2px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 600; background: var(--panel-2);
}
.chip-ok { background: rgba(22, 163, 74, .12); color: var(--ok); }
.chip-bad { background: rgba(220, 38, 38, .12); color: var(--danger); }

/* ---------- toasts ---------- */
.toast-wrap { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 1000; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast {
  background: #111827; color: #f9fafb; padding: 10px 16px; border-radius: 9px;
  box-shadow: var(--shadow); opacity: 0; transform: translateY(8px);
  transition: all .25s; max-width: 80vw;
}
.toast.show { opacity: 1; transform: none; }
.toast.error { background: var(--danger); }

/* ---------- auth pages ---------- */
.auth-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.auth-card {
  width: 100%; max-width: 400px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 14px; padding: 34px 30px;
  box-shadow: var(--shadow);
}
.auth-card.center { text-align: center; }
.auth-logo { text-align: center; margin-bottom: 22px; }
.auth-logo h1 { margin: 8px 0 2px; font-size: 22px; }
.auth-logo p { margin: 0; }
.auth-note { text-align: center; font-size: 12.5px; margin-top: 16px; }
/* Theme toggle on the login pages (added - operator request): fixed corner button */
.auth-theme { position: fixed; top: 14px; right: 14px; z-index: 30; }

/* ---------- app shell ---------- */
.shell { height: 100vh; display: flex; flex-direction: column; }
.topbar {
  height: var(--topbar-h); display: flex; align-items: center; gap: 14px;
  padding: 0 14px; background: var(--panel); border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.searchbar {
  flex: 1; max-width: 560px; display: flex; align-items: center; gap: 6px;
  background: var(--panel-2); border-radius: 9px; padding: 0 8px; height: 38px;
  position: relative;
}
.searchbar input {
  flex: 1; border: none; background: transparent; color: var(--text);
  height: 100%; font-size: 14px;
}
.searchbar input:focus { outline: none; }
.search-icon { width: 17px; height: 17px; color: var(--muted); }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.icon-sun { display: none; }
.icon-moon { display: block; }
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: none; }

.user-chip { position: relative; display: flex; align-items: center; gap: 8px; cursor: pointer; padding: 4px 8px; border-radius: 8px; }
.user-chip:hover { background: var(--panel-2); }
.user-chip > span:first-child {
  width: 30px; height: 30px; border-radius: 50%; background: var(--primary);
  color: #fff; display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px;
}
.user-email { font-size: 13.5px; max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
.user-menu {
  position: absolute; top: 110%; right: 0; background: var(--panel);
  border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow);
  min-width: 180px; padding: 6px; z-index: 60;
}
.menu-item {
  display: block; width: 100%; text-align: left; padding: 8px 10px;
  background: none; border: none; border-radius: 7px; cursor: pointer;
}
.menu-item:hover { background: var(--panel-2); }
.menu-danger { color: var(--danger); }

.main { flex: 1; display: flex; min-height: 0; }

/* ---------- sidebar ---------- */
.sidebar {
  width: var(--sidebar-w); background: var(--panel);
  border-right: 1px solid var(--border); padding: 14px 10px;
  display: flex; flex-direction: column; gap: 12px; flex-shrink: 0;
  overflow-y: auto;
}
.compose-btn { width: 100%; justify-content: center; padding: 11px; }
.folder-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.folder-link {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 9px 12px; background: none; border: none; border-radius: 8px;
  cursor: pointer; text-align: left; font-size: 14.5px;
}
.folder-link:hover { background: var(--panel-2); }
.folder-link.active { background: rgba(37, 99, 235, .12); color: var(--primary); font-weight: 600; }
.folder-link svg { width: 18px; height: 18px; flex-shrink: 0; }
.folder-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  background: var(--primary); color: #fff; font-size: 11.5px; font-weight: 700;
  border-radius: 999px; padding: 1px 8px;
}
.quota-box { margin-top: auto; padding: 10px; }
.quota-bar { height: 6px; background: var(--panel-2); border-radius: 999px; overflow: hidden; }
.quota-fill { height: 100%; background: var(--primary); border-radius: 999px; transition: width .3s; }
.quota-text { font-size: 12px; display: block; margin-top: 6px; }

/* ---------- list pane ---------- */
.list-pane {
  width: 400px; border-right: 1px solid var(--border); background: var(--panel);
  display: flex; flex-direction: column; min-height: 0; flex-shrink: 0;
}
.list-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.list-head h2 { margin: 0; font-size: 16px; }
.chk-unread { display: flex; gap: 6px; align-items: center; font-size: 13px; color: var(--muted); }
.list-scroll { flex: 1; overflow-y: auto; }
.msg-row {
  display: flex; gap: 10px; padding: 11px 16px; cursor: pointer;
  border-bottom: 1px solid var(--border); align-items: flex-start;
}
.msg-row:hover { background: var(--panel-2); }
.msg-row.unread { background: rgba(37, 99, 235, .05); }
[data-theme="dark"] .msg-row.unread { background: rgba(59, 130, 246, .08); }
.msg-row.unread .msg-from, .msg-row.unread .msg-subject { font-weight: 700; }
.msg-dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 7px; background: var(--primary); flex-shrink: 0; }
.msg-row:not(.unread) .msg-dot { background: transparent; }
.msg-main { flex: 1; min-width: 0; }
.msg-top { display: flex; justify-content: space-between; gap: 8px; }
.msg-from { font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-date { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.msg-subject { font-size: 13.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.msg-row.unread .msg-subject { color: var(--text); }
.clip { width: 13px; height: 13px; vertical-align: -2px; color: var(--muted); }
.list-foot {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; border-top: 1px solid var(--border); font-size: 13px;
}
.loading { padding: 30px; text-align: center; color: var(--muted); }
.truncated-note { padding: 8px 16px; font-size: 12.5px; }
.empty-state { text-align: center; padding: 60px 20px; opacity: .75; }

/* ---------- reader ---------- */
.reader-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.reader-toolbar {
  display: flex; gap: 6px; padding: 8px 12px; border-bottom: 1px solid var(--border);
  background: var(--panel); flex-wrap: wrap;
}
.reader-toolbar .spacer, .topbar .spacer { flex: 1; }
.reader-scroll { flex: 1; overflow-y: auto; padding: 18px 22px; }
.reader-subject { margin: 0 0 10px; font-size: 20px; }
.reader-meta { display: flex; flex-direction: column; gap: 3px; font-size: 13.5px; padding-bottom: 14px; border-bottom: 1px solid var(--border); }
.reader-content { margin-top: 16px; }
.mail-text { white-space: pre-wrap; word-wrap: break-word; font-family: inherit; margin: 0; }
.mail-frame {
  width: 100%; min-height: 320px; border: 1px solid var(--border);
  border-radius: 8px; background: #fff;
}
[data-theme="dark"] .mail-frame { background: #fff; color: #111; }
.reader-attachments { margin-top: 14px; }
.reader-attachments h4 { margin: 0 0 8px; }
.att-chip {
  display: inline-flex; align-items: center; gap: 7px; padding: 6px 11px;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 999px; margin: 0 8px 8px 0; font-size: 13px;
  color: var(--text); text-decoration: none; max-width: 320px;
}
.att-chip:hover { border-color: var(--primary); }
.att-remove { background: none; border: none; cursor: pointer; color: var(--muted); font-weight: 700; padding: 0 2px; }
.att-remove:hover { color: var(--danger); }

/* ---------- compose modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(10, 12, 18, .45);
  display: flex; align-items: center; justify-content: center; z-index: 200;
  padding: 16px;
}
.modal-box {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; box-shadow: var(--shadow); width: 100%;
  max-width: 680px; max-height: 92vh; display: flex; flex-direction: column;
  padding: 0;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-foot {
  display: flex; align-items: center; gap: 8px; padding: 12px 18px;
  border-top: 1px solid var(--border);
}
.compose-box { height: 82vh; }
.compose-box form { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.compose-addr { border-bottom: 1px solid var(--border); }
.addr-row { display: flex; align-items: flex-start; border-bottom: 1px solid var(--border); }
.addr-row:last-child { border-bottom: none; }
.addr-row label { width: 72px; padding: 11px 4px 0 18px; font-size: 13px; font-weight: 600; color: var(--muted); flex-shrink: 0; }
.addr-input { flex: 1; display: flex; align-items: center; padding-right: 8px; gap: 2px; }
.addr-row textarea, .addr-row input {
  flex: 1; border: none; background: transparent; color: var(--text);
  padding: 11px 8px; resize: none; font: inherit;
}
.addr-row textarea:focus, .addr-row input:focus { outline: none; }
#compose-body {
  flex: 1; border: none; resize: none; padding: 14px 18px;
  background: transparent; color: var(--text); font: inherit; min-height: 140px;
}
#compose-body:focus { outline: none; }
.compose-attachments { padding: 0 18px; }
.attach-btn svg { width: 16px; height: 16px; }

/* ---------- popover (advanced search) ---------- */
.popover {
  position: fixed; z-index: 90; background: var(--panel);
  border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  padding: 16px; width: 320px;
}
.popover-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

/* ---------- admin ---------- */
.admin-content { flex: 1; padding: 22px 26px; overflow-y: auto; min-width: 0; }
.admin-content h2 { margin-top: 0; }
.admin-nav .folder-link svg { display: none; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 20px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 16px; }
.card-value { font-size: 21px; font-weight: 700; }
.svc-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.svc {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 14px; font-size: 14px;
}
.svc-ok strong { color: var(--ok); }
.svc-bad strong { color: var(--danger); }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.toolbar { display: flex; gap: 10px; margin: 12px 0; max-width: 560px; }
.table-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th, .table td { text-align: left; padding: 10px 13px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table thead th { background: var(--panel-2); font-size: 12.5px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
.table tbody tr:hover { background: var(--panel-2); }
.table tbody tr:last-child td { border-bottom: none; }
.row-actions { display: flex; gap: 5px; flex-wrap: wrap; }
.log-detail { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: ui-monospace, monospace; font-size: 12.5px; }
.settings-form { max-width: 480px; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 18px; margin-bottom: 24px; }
dialog.modal-box { border: none; }
dialog::backdrop { background: rgba(10, 12, 18, .45); }
dialog form { padding: 18px; }

/* ---------- responsive ---------- */
.only-mobile { display: none !important; }
@media (max-width: 900px) {
  .only-mobile { display: inline-flex !important; }
  .sidebar {
    position: fixed; top: var(--topbar-h); bottom: 0; left: 0; z-index: 80;
    transform: translateX(-105%); transition: transform .2s; width: 250px;
    box-shadow: var(--shadow);
  }
  body.sidebar-open .sidebar { transform: none; }
  .list-pane { width: 100%; border-right: none; }
  .reader-pane { display: none; }
  body.show-reader .list-pane { display: none; }
  body.show-reader .reader-pane { display: flex; }
  .user-email { display: none; }
  .brand span { display: none; }
  .searchbar { max-width: none; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
