:root {
  --bg: #0b1220;
  --top: #0f172a;
  --surface: #111827;
  --surface-2: #172033;
  --surface-3: #1e293b;
  --text: #f8fafc;
  --muted: #94a3b8;
  --line: #263449;
  --line-soft: #1e2a3a;
  --accent: #3b82f6;
  --accent-hover: #60a5fa;
  --accent-soft: rgba(59, 130, 246, .14);
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, .12);
  --reader-bg: #eef2f7;
  --reader-text: #1f2937;
  --shadow: 0 18px 48px rgba(2, 6, 23, .30);
  --radius: 14px;
  --topbar-h: 66px;
  color-scheme: dark;
}

html[data-theme="light"] {
  --bg: #edf2f7;
  --top: #fff;
  --surface: #fff;
  --surface-2: #f7f9fc;
  --surface-3: #e8eef5;
  --text: #0f172a;
  --muted: #64748b;
  --line: #cbd5e1;
  --line-soft: #e2e8f0;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: rgba(37, 99, 235, .10);
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --danger-soft: rgba(220, 38, 38, .10);
  --reader-bg: #edf2f7;
  --reader-text: #1f2937;
  --shadow: 0 18px 48px rgba(15, 23, 42, .13);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, Arial, "Noto Sans Hebrew", "Noto Sans Arabic", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hidden { display: none !important; }

.auth-screen, .modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  place-items: center;
  padding: 18px;
  background: radial-gradient(circle at 75% 10%, rgba(59,130,246,.13), transparent 38%), #070d18;
}
.auth-screen.visible, .modal-backdrop:not(.hidden) { display: grid; }
.auth-card, .modal-card {
  width: min(460px, 100%);
  padding: 26px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.auth-brand { display: flex; align-items: center; gap: 14px; margin-bottom: 24px; }
.auth-brand img { width: 56px; height: 56px; object-fit: contain; border-radius: 14px; background: var(--surface-2); }
.auth-brand h1 { margin: 0; font-size: 26px; }
.auth-brand p { margin: 5px 0 0; color: var(--muted); }
.form-error { margin: 12px 0 0; color: #fca5a5; font-size: 13px; }

.app { height: 100vh; display: grid; grid-template-rows: var(--topbar-h) minmax(0, 1fr); }
.topbar {
  display: grid;
  grid-template-columns: minmax(max-content, 1fr) auto minmax(max-content, 1fr);
  grid-template-areas: "left center right";
  align-items: center;
  gap: 14px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--top);
  direction: ltr;
  z-index: 20;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: nowrap; white-space: nowrap; }
.topbar-left { grid-area: left; justify-self: start; direction: ltr; }
.topbar-right { grid-area: right; justify-self: end; direction: rtl; }
.topbar-center { grid-area: center; justify-self: center; }
.brand { display: flex; align-items: center; gap: 10px; direction: rtl; }
.brand img { width: 42px; height: 42px; object-fit: contain; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); padding: 3px; }
.brand-copy { display: grid; text-align: right; }
.brand-copy strong { font-size: 17px; }
.brand-copy small { color: var(--muted); font-size: 10px; }

.btn {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0 13px;
  color: var(--text);
  background: var(--surface-2);
  font-weight: 720;
  transition: .15s ease;
}
.btn:hover { background: var(--surface-3); }
.btn.primary { border-color: transparent; color: #fff; background: var(--accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.secondary { background: var(--surface-3); }
.btn.ghost { background: transparent; }
.btn.danger { color: #fecaca; border-color: rgba(239,68,68,.35); background: var(--danger-soft); }
html[data-theme="light"] .btn.danger { color: var(--danger); }
.btn.icon { width: 40px; min-width: 40px; padding: 0; display: grid; place-items: center; font-size: 18px; }
.btn.compact { min-height: 34px; padding: 0 11px; font-size: 12px; }
.btn.tiny { min-height: 30px; padding: 0 9px; font-size: 10px; }
.btn.wide { width: 100%; }
.btn.master { border-color: rgba(59,130,246,.75); color: #fff; background: linear-gradient(145deg, var(--accent), #1d4ed8); }
.btn.admin { position: relative; border-color: rgba(245,158,11,.55); color: #fde68a; background: rgba(245,158,11,.12); }
.badge { min-width: 19px; height: 19px; display: inline-grid; place-items: center; margin-inline-start: 5px; padding: 0 5px; border-radius: 999px; color: #fff; background: var(--danger); font-size: 10px; }
.status-pill { min-height: 34px; display: inline-flex; align-items: center; gap: 7px; padding: 0 10px; border: 1px solid var(--line); border-radius: 10px; color: var(--muted); background: var(--surface-2); font-size: 11px; direction: rtl; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warning); }
.status-dot.online { background: var(--success); }

.input, .select, .search input, .address-input input {
  min-width: 0;
  border: 1px solid var(--line);
  outline: 0;
  color: var(--text);
  background: var(--bg);
}
.input, .select { width: 100%; min-height: 43px; border-radius: 10px; padding: 0 12px; }
.input:focus, .select:focus, .search:focus-within, .address-input:focus-within { border-color: var(--accent); }
.select.compact-select { width: auto; min-height: 38px; padding: 0 9px; }
.field-label { display: block; margin: 0 0 7px; color: var(--muted); font-size: 11px; font-weight: 720; }

.workspace {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(440px, 1fr) minmax(360px, 430px) 330px;
  grid-template-areas: "reader messages mailboxes";
  gap: 1px;
  background: var(--line);
  direction: ltr;
}
.pane { min-width: 0; min-height: 0; overflow: hidden; color: var(--text); background: var(--surface); direction: var(--ui-dir, rtl); }
.mailbox-pane { grid-area: mailboxes; display: grid; grid-template-rows: auto auto auto minmax(0, 1fr); border-top: 3px solid var(--accent); }
.message-pane { grid-area: messages; display: grid; grid-template-rows: auto auto auto minmax(0, 1fr); border-top: 3px solid #64748b; }
.reader-pane { grid-area: reader; border-top: 3px solid #60a5fa; }

.pane-header, .message-pane-header, .reader-header { border-bottom: 1px solid var(--line); background: var(--surface-2); }
.pane-header { padding: 15px; }
.pane-header h2, .message-pane-header h2 { margin: 3px 0 0; font-size: 17px; }
.eyebrow { color: var(--muted); font-size: 10px; font-weight: 760; }
.create-card { margin: 13px; padding: 14px; border: 1px solid color-mix(in srgb, var(--accent) 38%, var(--line)); border-radius: var(--radius); background: color-mix(in srgb, var(--accent) 5%, var(--surface-2)); }
.address-input { display: grid; grid-template-columns: minmax(0, 1fr) auto; margin-bottom: 11px; overflow: hidden; border: 1px solid var(--line); border-radius: 10px; direction: ltr; }
.address-input input { height: 43px; border: 0; padding: 0 11px; }
.address-input span { display: flex; align-items: center; padding: 0 10px; border-left: 1px solid var(--line); color: var(--muted); background: var(--surface); font-size: 11px; }
.create-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 11px; }
.create-card small { display: block; margin-top: 9px; color: var(--muted); font-size: 10px; }
.mailbox-tools, .message-toolbar { display: grid; gap: 9px; padding: 11px 12px; border-bottom: 1px solid var(--line); }
.segmented { display: flex; gap: 7px; }
.segment { min-height: 34px; border: 1px solid var(--line); border-radius: 9px; padding: 0 12px; color: var(--muted); background: var(--surface-2); font-weight: 700; font-size: 11px; cursor: pointer; }
.segment.active { color: #fff; border-color: var(--accent); background: var(--accent); }
.search { height: 41px; display: flex; align-items: center; gap: 8px; padding: 0 11px; border: 1px solid var(--line); border-radius: 10px; color: var(--muted); background: var(--bg); }
.search input { flex: 1; width: 100%; height: 100%; border: 0; background: transparent; }
.mailbox-list, .message-list { min-height: 0; overflow: auto; padding: 8px; }
.mailbox-item {
  width: 100%; display: grid; grid-template-columns: 32px minmax(0,1fr) auto; gap: 8px; align-items: center;
  margin-bottom: 5px; padding: 9px; border: 1px solid transparent; border-radius: 11px; color: var(--text); background: transparent; text-align: start;
}
.mailbox-item:hover { background: var(--surface-2); }
.mailbox-item.active { border-color: var(--accent); background: var(--accent-soft); }
.mailbox-icon { width: 32px; height: 32px; display: grid; place-items: center; border: 1px solid var(--line); border-radius: 9px; background: var(--surface-2); }
.mailbox-info { min-width: 0; }
.mailbox-address { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: ltr; font-size: 12px; font-weight: 760; }
.mailbox-meta { margin-top: 3px; color: var(--muted); font-size: 9px; }
.mailbox-actions { display: flex; align-items: center; gap: 5px; }
.favorite { color: #fbbf24 !important; }
.count-pill { min-width: 21px; height: 21px; display: grid; place-items: center; border-radius: 999px; color: #fff; background: var(--accent); font-size: 9px; }

.message-pane-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 15px; }
.message-pane-header h2 { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: ltr; }
.header-actions { display: flex; gap: 6px; }
.batch-toolbar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; padding: 9px 11px; border-bottom: 1px solid var(--line); background: var(--accent-soft); }
.batch-toolbar > span { margin-inline-end: auto; font-size: 11px; font-weight: 760; }
.message-item {
  width: 100%; display: grid; grid-template-columns: 27px minmax(0,1fr) auto; gap: 8px;
  margin-bottom: 5px; padding: 11px; border: 1px solid transparent; border-radius: 10px; color: var(--text); background: transparent; text-align: start;
}
.message-item:hover { background: var(--surface-2); }
.message-item.active { border-color: var(--accent); background: var(--accent-soft); }
.message-item.unread .message-from, .message-item.unread .message-subject { font-weight: 820; }
.message-select { width: 18px; height: 18px; accent-color: var(--accent); }
.message-content { min-width: 0; }
.message-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.message-from, .message-subject, .message-preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-from { font-size: 11px; font-weight: 720; }
.message-time { flex: 0 0 auto; color: var(--muted); font-size: 9px; }
.message-subject { margin-top: 4px; font-size: 12px; }
.message-preview { margin-top: 3px; color: var(--muted); font-size: 10px; }
.message-recipient { margin-top: 4px; color: var(--warning); font-size: 9px; direction: ltr; }
.message-side { display: grid; place-items: center; gap: 4px; }
.empty-list, .empty-state { color: var(--muted); text-align: center; }
.empty-list { padding: 32px 18px; font-size: 12px; }

.reader-pane { overflow: hidden; }
.empty-state { height: 100%; display: grid; place-content: center; gap: 8px; padding: 30px; }
.empty-state span { font-size: 30px; }
.reader { height: 100%; display: grid; grid-template-rows: auto auto minmax(0,1fr); }
.reader-header { padding: 17px 19px; }
.reader-title-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 14px; }
.reader-title-row h1 { min-width: 0; margin: 0; font-size: clamp(19px, 2vw, 25px); line-height: 1.3; overflow-wrap: anywhere; }
.reader-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.sender-card { display: grid; grid-template-columns: 42px minmax(0,1fr); gap: 12px; margin-top: 14px; padding: 12px 13px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
.avatar { width: 42px; height: 42px; display: grid; place-items: center; border-radius: 50%; color: #fff; background: linear-gradient(145deg, var(--accent), #1d4ed8); font-weight: 800; }
.sender-details { min-width: 0; }
.sender-details > strong { display: block; margin-bottom: 7px; overflow-wrap: anywhere; }
.meta-grid { display: grid; grid-template-columns: max-content minmax(0,1fr); gap: 4px 9px; color: var(--muted); font-size: 10px; }
.meta-grid b { color: var(--muted); font-weight: 500; overflow-wrap: anywhere; }
.view-controls { display: flex; gap: 7px; margin-top: 12px; direction: ltr; }
.smart-panels { display: grid; gap: 9px; padding: 12px; border-bottom: 1px solid var(--line); background: var(--reader-bg); }
.smart-panel { padding: 10px; border: 1px solid #d4dde7; border-radius: 11px; background: #fff; color: #1f2937; }
.smart-panel h3 { margin: 0 0 8px; font-size: 12px; }
.chip-list, .attachment-list { display: flex; gap: 7px; flex-wrap: wrap; }
.code-chip, .attachment-item { display: inline-flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid #bfdbfe; border-radius: 9px; color: #1d4ed8; background: #eff6ff; font-weight: 760; }
.code-chip button, .attachment-item button { border: 0; color: inherit; background: transparent; font-weight: 800; cursor: pointer; }
.link-list { display: grid; gap: 7px; }
.link-item { display: grid; grid-template-columns: minmax(0,1fr) auto auto; gap: 7px; align-items: center; padding: 8px; border: 1px solid #dbe2ea; border-radius: 9px; background: #f8fafc; }
.link-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; direction: ltr; font-size: 10px; }
.reader-body { min-height: 0; overflow: auto; padding: 20px; background: var(--reader-bg); color: var(--reader-text); }
.html-renderer, .text-renderer { width: min(920px, 100%); min-height: 300px; margin: 0 auto; border: 1px solid #dbe2ea; border-radius: 14px; background: #fff; box-shadow: 0 8px 24px rgba(15,23,42,.08); }
.text-renderer { padding: 26px; color: #1f2937; white-space: pre-wrap; line-height: 1.75; direction: auto; unicode-bidi: plaintext; overflow-wrap: anywhere; }
.text-renderer a { color: #1d4ed8; text-decoration: underline; }

.modal-card header { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 18px; }
.modal-card h2 { margin: 0; }
.settings-card { display: grid; gap: 13px; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 11px 0; border-top: 1px solid var(--line-soft); }
.setting-row small { display: block; margin-top: 3px; color: var(--muted); }
.password-row { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 8px; }
.turnstile-box { margin-top: 10px; min-height: 65px; }
.toast { position: fixed; z-index: 300; inset-inline-start: 50%; bottom: 24px; transform: translateX(-50%); max-width: min(420px, calc(100vw - 32px)); padding: 11px 15px; border: 1px solid var(--line); border-radius: 11px; color: var(--text); background: var(--surface-3); box-shadow: var(--shadow); text-align: center; }

.admin-overlay { position: fixed; inset: 0; z-index: 150; padding: 16px; background: rgba(2,6,23,.82); backdrop-filter: blur(7px); }
.admin-shell { width: min(1500px, 100%); height: 100%; margin: 0 auto; display: grid; grid-template-rows: auto auto minmax(0,1fr); overflow: hidden; border: 1px solid var(--line); border-radius: 18px; background: var(--bg); box-shadow: var(--shadow); }
.admin-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 15px 18px; border-bottom: 1px solid var(--line); background: var(--top); }
.admin-header h1 { margin: 3px 0 0; font-size: 20px; }
.admin-tabs { display: flex; gap: 6px; padding: 9px 12px; border-bottom: 1px solid var(--line); background: var(--surface); }
.admin-tab { min-height: 37px; border: 1px solid var(--line); border-radius: 9px; padding: 0 13px; color: var(--muted); background: var(--surface-2); font-weight: 760; cursor: pointer; }
.admin-tab.active { color: #fff; border-color: var(--accent); background: var(--accent); }
.admin-view { min-height: 0; overflow: hidden; }
#adminUsersView { display: grid; grid-template-columns: 390px minmax(0,1fr); gap: 1px; background: var(--line); direction: ltr; }
.admin-list-column, .admin-detail { min-width: 0; min-height: 0; overflow: auto; background: var(--surface); direction: var(--ui-dir, rtl); }
.admin-list-toolbar { position: sticky; top: 0; z-index: 2; display: grid; gap: 8px; padding: 11px; border-bottom: 1px solid var(--line); background: var(--surface); }
.admin-user-list { padding: 8px; }
.admin-user-card { margin-bottom: 7px; padding: 12px; border: 1px solid var(--line); border-radius: 11px; background: var(--surface-2); cursor: pointer; }
.admin-user-card.active { border-color: var(--accent); background: var(--accent-soft); }
.admin-user-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.admin-user-code { margin-top: 7px; padding: 7px 9px; border: 1px dashed var(--line); border-radius: 8px; color: #bfdbfe; background: var(--bg); direction: ltr; font-family: ui-monospace, Consolas, monospace; }
.admin-user-stats { display: grid; grid-template-columns: repeat(3,1fr); gap: 6px; margin-top: 8px; color: var(--muted); font-size: 9px; }
.admin-detail { padding: 15px; }
.admin-detail-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; margin-bottom: 13px; }
.admin-detail-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.admin-section { margin-bottom: 14px; padding: 13px; border: 1px solid var(--line); border-radius: 13px; background: var(--surface-2); }
.admin-section h3 { margin: 0 0 10px; }
.admin-mailbox-grid { display: grid; grid-template-columns: repeat(auto-fill,minmax(210px,1fr)); gap: 8px; }
.admin-mailbox { padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); cursor: pointer; }
.admin-mailbox.deleted { opacity: .72; border-color: rgba(239,68,68,.35); }
.admin-mailbox strong { display: block; direction: ltr; overflow: hidden; text-overflow: ellipsis; }
.admin-message-layout { display: grid; grid-template-columns: 360px minmax(0,1fr); gap: 1px; min-height: 520px; margin-top: 12px; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--line); }
.admin-message-list, .admin-message-reader { min-width: 0; overflow: auto; background: var(--surface); }
.admin-message-list { padding: 7px; }
.admin-message-reader { padding: 14px; }
.admin-table-list { height: 100%; overflow: auto; padding: 12px; }
.device-row, .notification-row { display: grid; grid-template-columns: minmax(180px,1fr) repeat(4,minmax(110px,auto)); gap: 10px; align-items: center; margin-bottom: 7px; padding: 11px; border: 1px solid var(--line); border-radius: 11px; background: var(--surface); }
.notification-row { grid-template-columns: minmax(200px,1fr) minmax(140px,auto) auto; }
.notification-row.unread { border-color: var(--accent); background: var(--accent-soft); }
.admin-notification-toolbar { padding: 10px 12px; border-bottom: 1px solid var(--line); }

.mobile-nav { display: none; }

@media (max-width: 1100px) {
  .workspace { grid-template-columns: minmax(380px,1fr) 350px 290px; }
  .brand-copy small { display: none; }
}

@media (max-width: 850px) {
  :root { --topbar-h: 72px; }
  body { overflow: hidden; }
  .app { grid-template-rows: var(--topbar-h) minmax(0,1fr) 66px; }
  .topbar {
    grid-template-columns: 92px minmax(44px,1fr) 152px;
    grid-template-areas: "left center right";
    gap: 7px;
    padding: 8px 9px;
    overflow: hidden;
  }
  .topbar-left { gap: 5px; }
  .topbar-left .status-pill, .topbar-left #logoutBtn { display: none; }
  .topbar-left .btn.icon { width: 42px; min-width: 42px; height: 45px; }
  .brand-copy { display: none; }
  .brand img { width: 44px; height: 44px; }
  .topbar-right { width: 152px; min-width: 152px; }
  .topbar-right .compact-select { display: none; }
  .btn.master { width: 152px; min-width: 152px; height: 48px; padding: 0 10px; font-size: 14px; }
  .btn.admin { position: fixed; top: 78px; inset-inline-end: 10px; z-index: 30; min-height: 38px; box-shadow: var(--shadow); }
  .workspace { display: block; min-height: 0; overflow: hidden; }
  .pane { display: none; width: 100%; height: 100%; border-top-width: 2px; }
  .workspace.mobile-pane-mailboxes .mailbox-pane,
  .workspace.mobile-pane-messages .message-pane,
  .workspace.mobile-pane-reader .reader-pane { display: grid; }
  .workspace.mobile-pane-reader .reader-pane { display: block; }
  .create-card, .mailbox-tools, .message-toolbar, .batch-toolbar, .message-pane-header { width: calc(100% - 20px); max-width: 650px; margin-inline: auto; }
  .create-card { margin-top: 10px; border-radius: 16px; }
  .mailbox-list, .message-list { width: 100%; max-width: 650px; margin: 0 auto; padding: 10px; }
  .mailbox-item, .message-item { margin-bottom: 7px; border-color: var(--line-soft); background: var(--surface-2); }
  .reader-header { padding: 14px 12px; }
  .reader-title-row { display: grid; gap: 10px; }
  .reader-actions { justify-content: flex-start; }
  .smart-panels { padding: 10px; }
  .reader-body { padding: 0; }
  .html-renderer, .text-renderer { width: 100%; min-height: 100%; border: 0; border-radius: 0; box-shadow: none; }
  .link-item { grid-template-columns: minmax(0,1fr) auto; }
  .link-item .copy-link { grid-column: 1 / -1; width: 100%; }
  .mobile-nav { display: grid; grid-template-columns: repeat(3,1fr); border-top: 1px solid var(--line); background: var(--top); box-shadow: 0 -7px 20px rgba(2,6,23,.20); padding-bottom: env(safe-area-inset-bottom); }
  .mobile-nav button { border: 0; color: var(--muted); background: transparent; display: grid; place-content: center; gap: 3px; font-weight: 720; }
  .mobile-nav button.active { color: #dbeafe; background: var(--accent-soft); box-shadow: inset 0 3px 0 var(--accent); }
  .mobile-nav span { font-size: 20px; }
  .mobile-nav small { font-size: 10px; }
  .modal-backdrop { align-items: center; overflow: auto; }
  .modal-card { max-height: calc(100dvh - 28px); overflow: auto; }
  .admin-overlay { padding: 0; }
  .admin-shell { border: 0; border-radius: 0; }
  #adminUsersView { display: block; overflow: auto; }
  .admin-list-column, .admin-detail { height: auto; min-height: 100%; }
  .admin-detail { border-top: 1px solid var(--line); }
  .admin-message-layout { grid-template-columns: 1fr; }
  .admin-message-list { max-height: 330px; }
  .device-row, .notification-row { grid-template-columns: 1fr; }
}

@media (max-width: 430px) {
  .topbar { grid-template-columns: 88px minmax(40px,1fr) 145px; padding-inline: 7px; }
  .topbar-left { gap: 4px; }
  .topbar-left .btn.icon { width: 40px; min-width: 40px; }
  .topbar-right, .btn.master { width: 145px; min-width: 145px; }
  .btn.master { font-size: 13px; }
  .create-actions { grid-template-columns: 1fr; }
  .reader-actions .btn { min-height: 35px; }
  .sender-card { grid-template-columns: 38px minmax(0,1fr); }
  .avatar { width: 38px; height: 38px; }
  .batch-toolbar { overflow-x: auto; flex-wrap: nowrap; }
  .batch-toolbar > * { flex: 0 0 auto; }
  .admin-tabs { overflow-x: auto; }
  .admin-tab { flex: 0 0 auto; }
}

/* Dynamic component styling used by app.js */
.mailbox-item { display:grid; grid-template-columns:minmax(0,1fr) auto; align-items:center; gap:8px; }
.mailbox-main,.message-open { min-width:0; border:0; color:inherit; background:transparent; text-align:start; cursor:pointer; }
.mailbox-main { display:grid; gap:4px; padding:0; }
.mailbox-address { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; font-weight:780; }
.mailbox-meta { color:var(--muted); font-size:10px; }
.mailbox-actions { display:flex; gap:5px; }
.message-item { display:grid; grid-template-columns:24px minmax(0,1fr) 36px; align-items:start; gap:8px; }
.message-open { display:grid; gap:4px; padding:0; }
.message-check { width:17px; height:17px; margin-top:2px; accent-color:var(--accent); }
.message-star { width:34px; height:34px; border:0; border-radius:9px; color:var(--warning); background:transparent; font-size:20px; cursor:pointer; }
.message-star:hover { background:var(--surface-3); }
.message-top { display:flex; align-items:center; justify-content:space-between; gap:8px; }
.message-top strong,.message-subject,.message-preview { min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.message-top time { flex:0 0 auto; color:var(--muted); font-size:9px; }
.message-subject { font-size:12px; }
.message-preview { color:var(--muted); font-size:10px; }
.message-open small { color:var(--warning); font-size:9px; }
.message-item.unread { border-color:color-mix(in srgb,var(--accent) 35%,var(--line)); background:color-mix(in srgb,var(--accent) 7%,var(--surface-2)); }
.message-item.unread .message-top strong,.message-item.unread .message-subject { font-weight:820; }
.code-chip { min-height:38px; padding:0 13px; border:1px solid #bfdbfe; border-radius:10px; color:#1d4ed8; background:#eff6ff; font-weight:800; cursor:pointer; }
.link-item { min-width:0; display:grid; grid-template-columns:minmax(0,1fr) auto auto; align-items:center; gap:8px; padding:10px; border:1px solid #d7e0ea; border-radius:11px; background:#f8fafc; color:#17202b; }
.link-item div { min-width:0; display:grid; gap:3px; }
.link-item strong,.link-item small { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.link-item small { color:#64748b; font-family:ui-monospace,monospace; }
.attachment-item { min-width:220px; text-align:start; }
.text-renderer { white-space:pre-wrap; overflow-wrap:anywhere; line-height:1.75; }
.text-renderer a { color:#2563eb; text-decoration:underline; }
.admin-user-row { width:100%; display:grid; grid-template-columns:minmax(0,1fr) auto; gap:7px 10px; padding:12px; border:1px solid transparent; border-radius:11px; color:var(--text); background:transparent; text-align:start; cursor:pointer; }
.admin-user-row:hover,.admin-user-row.active { border-color:var(--accent); background:var(--accent-soft); }
.admin-user-row div { min-width:0; display:grid; gap:4px; }
.admin-user-row code { color:var(--warning); font-size:11px; user-select:all; }
.admin-user-row small { grid-column:1/-1; color:var(--muted); }
.status-tag { align-self:start; padding:4px 8px; border-radius:999px; font-size:10px; font-weight:800; }
.status-tag.active { color:#b9f6d5; background:rgba(34,197,94,.13); }
.status-tag.blocked { color:#fecaca; background:rgba(239,68,68,.13); }
.admin-detail-header { display:flex; align-items:flex-start; justify-content:space-between; gap:12px; padding-bottom:14px; border-bottom:1px solid var(--line); }
.admin-detail-header h2 { margin:0 0 6px; }
.admin-detail-header p { margin:0; color:var(--muted); }
.admin-detail-header code { color:var(--warning); font-size:14px; user-select:all; }
.admin-detail-actions { display:flex; gap:7px; flex-wrap:wrap; }
.admin-stats { display:flex; flex-wrap:wrap; gap:8px; margin:14px 0; }
.admin-stats span { padding:7px 10px; border:1px solid var(--line); border-radius:9px; background:var(--surface-2); color:var(--muted); font-size:11px; }
.admin-user-tabs { margin-bottom:12px; }
.admin-mailbox-grid { display:grid; grid-template-columns:1fr 1fr; gap:12px; }
.admin-mailbox-grid section,.admin-message-section { min-width:0; padding:12px; border:1px solid var(--line); border-radius:12px; background:var(--surface-2); }
.admin-mailbox-grid h3,.admin-message-section h3 { margin:0 0 9px; font-size:13px; }
.admin-mailbox-row,.admin-message-row { width:100%; min-width:0; display:grid; gap:4px; margin-bottom:6px; padding:9px; border:1px solid var(--line-soft); border-radius:9px; color:var(--text); background:var(--surface); text-align:start; cursor:pointer; }
.admin-mailbox-row:hover,.admin-message-row:hover { border-color:var(--accent); }
.admin-mailbox-row span,.admin-message-row span,.admin-message-row small { color:var(--muted); font-size:10px; }
.admin-message-section { margin-top:12px; }
.admin-preview-body { white-space:pre-wrap; overflow-wrap:anywhere; line-height:1.65; color:var(--text); }
.device-row,.notification-row { display:grid; grid-template-columns:180px minmax(0,1fr) 170px auto; gap:10px; align-items:center; padding:11px; border-bottom:1px solid var(--line); }
.device-row span,.notification-row span,.device-row time,.notification-row time { color:var(--muted); font-size:11px; }
.notification-row.unread { background:var(--accent-soft); }
.password-row { display:grid; grid-template-columns:minmax(0,1fr) auto; gap:8px; }
.form-error { color:var(--danger); }

@media (max-width:850px) {
  .admin-detail-header { display:grid; }
  .admin-mailbox-grid { grid-template-columns:1fr; }
  .message-item { grid-template-columns:24px minmax(0,1fr) 34px; }
  .link-item { grid-template-columns:minmax(0,1fr) auto; }
  .link-item .btn:last-child { grid-column:1/-1; width:100%; }
  .device-row,.notification-row { grid-template-columns:1fr; }
}


/* Personal access-code change */
.personal-code-section {
  display: grid;
  gap: 9px;
  margin: 6px 0 18px;
}

.personal-code-section h3 {
  margin: 0;
  font-size: 16px;
}

.personal-code-section > p:not(.form-error) {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.55;
}

.settings-divider {
  height: 1px;
  margin: 5px 0 9px;
  background: var(--line);
}

@media (max-width: 850px) {
  .personal-code-section {
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--surface-2);
  }
}


/* ===== Reliable vertical scrolling ===== */
.mailbox-list,
.message-list,
.reader-body,
.modal-card,
.settings-card {
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  touch-action: pan-y;
  scrollbar-gutter: stable;
}

.reader,
.reader-pane,
.reader-body {
  min-height: 0;
}

.reader-body {
  overflow-x: hidden;
  overflow-y: auto !important;
}

.html-renderer,
.text-renderer {
  max-width: 100%;
  overflow-wrap: anywhere;
}

@media (max-width: 850px) {
  .workspace.mobile-pane-reader .reader-pane {
    height: 100%;
    min-height: 0;
    overflow: hidden !important;
  }

  .workspace.mobile-pane-reader .reader {
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }

  .workspace.mobile-pane-reader .reader-body {
    height: auto;
    min-height: 0;
    max-height: 100%;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
  }

  .modal-backdrop {
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    align-items: start;
  }

  .modal-card {
    margin-block: auto;
    overflow-y: auto !important;
  }
}


/* ===== Continuous reader scrolling =====
   The detected codes, links, attachments and the email body now share
   one vertical scroll area. The fixed message header stays visible. */
.reader {
  grid-template-rows: auto minmax(0, 1fr) !important;
}

.reader-scroll {
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow-x: hidden;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  touch-action: pan-y;
  scrollbar-gutter: stable;
  background: var(--reader-bg);
}

.reader-scroll .smart-panels {
  overflow: visible;
}

.reader-scroll .reader-body {
  height: auto !important;
  min-height: 0;
  max-height: none !important;
  overflow: visible !important;
}

.reader-scroll .html-renderer,
.reader-scroll .text-renderer {
  height: auto;
  max-height: none;
}

@media (max-width: 850px) {
  .workspace.mobile-pane-reader .reader-scroll {
    height: 100%;
    min-height: 0;
    overflow-y: auto !important;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }

  .workspace.mobile-pane-reader .reader-body {
    height: auto !important;
    min-height: 0;
    max-height: none !important;
    overflow: visible !important;
    padding-bottom: 0;
  }

  .workspace.mobile-pane-reader .html-renderer,
  .workspace.mobile-pane-reader .text-renderer {
    min-height: 320px;
  }
}


/* ===== Mobile settings and personal-code fix ===== */
.settings-backdrop {
  overflow: hidden !important;
  padding: 12px;
}

.settings-card {
  width: min(520px, 100%) !important;
  height: min(760px, calc(100dvh - 24px));
  max-height: calc(100dvh - 24px) !important;
  display: grid !important;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 0 !important;
  padding: 0 !important;
  overflow: hidden !important;
}

.settings-header {
  position: relative;
  z-index: 2;
  margin: 0 !important;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.settings-scroll-content {
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  touch-action: pan-y;
  scrollbar-gutter: stable;
  padding: 16px 18px calc(22px + env(safe-area-inset-bottom));
}

.personal-code-section {
  display: grid;
  gap: 10px;
  margin: 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface-2);
}

.personal-code-section h3 {
  margin: 0;
}

.personal-code-section > p:not(.form-error) {
  margin: 0 0 3px;
}

.settings-general-section {
  display: grid;
  gap: 12px;
}

.settings-general-section .settings-divider {
  margin: 17px 0 2px;
}

@media (max-width: 850px) {
  .settings-backdrop {
    align-items: center !important;
    place-items: center !important;
    overflow: hidden !important;
    padding:
      max(8px, env(safe-area-inset-top))
      8px
      max(8px, env(safe-area-inset-bottom));
  }

  .settings-card {
    width: 100% !important;
    height: calc(100dvh - max(16px, env(safe-area-inset-top)) - max(16px, env(safe-area-inset-bottom)));
    max-height: none !important;
    margin: 0 !important;
    border-radius: 16px;
  }

  .settings-header {
    padding: 13px 14px;
  }

  .settings-scroll-content {
    padding: 13px 13px calc(24px + env(safe-area-inset-bottom));
  }

  .personal-code-section {
    padding: 13px;
  }
}
