:root {
  --navy: #0f2138;
  --navy-soft: #1a3354;
  --gold: #c69b43;
  --gold-soft: #fdf7eb;
  --bg: #f4f6f9;
  --card: #ffffff;
  --border: #dfe4ec;
  --text: #162231;
  --muted: #617083;
  --danger: #b42318;
  --danger-bg: #fef3f2;
  --success: #027a48;
  --success-bg: #ecfdf3;
  --font: "IBM Plex Sans Arabic", "Tajawal", "Segoe UI", Tahoma, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 14px; line-height: 1.5; }
a { color: inherit; text-decoration: none; }
button, input, select { font-family: inherit; font-size: inherit; }
.hidden { display: none !important; }
.full-width { width: 100%; }
.flex-1 { flex: 1 1 auto; }
.muted-text { color: var(--muted); font-size: 12px; }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 7px 14px; border-radius: 8px; border: 1px solid transparent; cursor: pointer; font-weight: 600; transition: background .15s, border-color .15s; white-space: nowrap; }
.btn-sm { padding: 5px 10px; font-size: 12.5px; border-radius: 6px; }
.btn-primary { background: var(--navy); color: #fff; }
.btn-primary:hover { background: var(--navy-soft); }
.btn-gold { background: var(--gold); color: var(--navy); }
.btn-gold:hover { filter: brightness(0.95); }
.btn-outline { background: #fff; color: var(--text); border-color: var(--border); }
.btn-outline:hover { background: #f8fafc; border-color: #c7d0dd; }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: rgba(15, 33, 56, 0.06); color: var(--text); }
.btn-icon { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--border); background: #fff; color: var(--text); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; }
.btn-icon:hover { background: #f1f5f9; }
.link-btn { background: none; border: none; color: #1d4ed8; cursor: pointer; font-size: 12.5px; padding: 0; font-weight: 600; }

/* Auth Gate & Error */
.auth-gate, .sso-error-body { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; background: var(--bg); }
.auth-card, .sso-error-card { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 28px; max-width: 480px; width: 100%; text-align: center; }
.brand-mark { display: inline-flex; width: 48px; height: 48px; border-radius: 12px; align-items: center; justify-content: center; background: var(--navy); color: var(--gold); font-weight: 800; font-size: 18px; margin-bottom: 12px; }
.portal-links { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 18px; }
#auth-gate-msg:empty { display: none; }

/* Shell */
.mail-shell { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; height: 56px; padding: 0 16px; background: var(--navy); color: #fff; border-bottom: 1px solid rgba(255,255,255,0.12); flex-shrink: 0; }
.topbar-start, .topbar-end { display: flex; align-items: center; gap: 10px; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; }
.brand-badge { background: var(--gold); color: var(--navy); padding: 2px 8px; border-radius: 6px; font-weight: 800; font-size: 12px; }
.topbar-search { flex: 1 1 320px; max-width: 460px; }
.topbar-search input { width: 100%; height: 34px; border-radius: 8px; border: 1px solid rgba(255,255,255,0.22); background: rgba(255,255,255,0.12); color: #fff; padding: 0 12px; }
.topbar-search input::placeholder { color: rgba(255,255,255,0.72); }
.user-chip { background: rgba(255,255,255,0.12); border: 1px solid rgba(255,255,255,0.18); padding: 4px 10px; border-radius: 999px; font-size: 12px; white-space: nowrap; }
.topbar .btn-outline { background: transparent; color: #fff; border-color: rgba(255,255,255,0.28); }
.topbar .btn-ghost { color: rgba(255,255,255,0.85); }

.toast-bar { padding: 8px 16px; background: var(--navy-soft); color: #fff; font-size: 13px; text-align: center; flex-shrink: 0; }
.toast-bar.error { background: var(--danger); }

.workspace { display: flex; flex: 1 1 auto; min-height: 0; overflow: hidden; }

/* Sidebar */
.sidebar { width: 220px; flex-shrink: 0; background: var(--card); border-inline-end: 1px solid var(--border); display: flex; flex-direction: column; justify-content: space-between; padding: 12px 10px; overflow-y: auto; }
.folder-nav { display: flex; flex-direction: column; gap: 4px; }
.folder-item { display: flex; align-items: center; justify-content: space-between; padding: 9px 12px; border-radius: 8px; border: 1px solid transparent; background: transparent; cursor: pointer; text-align: start; font-weight: 600; color: var(--text); }
.folder-item:hover { background: #f8fafc; }
.folder-item.active { background: var(--gold-soft); border-color: #ead7aa; color: var(--navy); }
.count-pill { background: var(--navy); color: #fff; font-size: 11px; padding: 1px 7px; border-radius: 999px; font-weight: 700; }
.count-pill.muted { background: #e8edf4; color: var(--navy); }
.sidebar-filters { border-top: 1px solid var(--border); padding-top: 12px; display: flex; flex-direction: column; gap: 10px; }
.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }

/* Thread List */
.list-pane { width: 350px; flex-shrink: 0; background: var(--card); border-inline-end: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.pane-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.pane-head h2 { margin: 0; font-size: 15px; }
.thread-list { flex: 1 1 auto; overflow-y: auto; }
.thread-row { padding: 12px 14px; border-bottom: 1px solid var(--border); cursor: pointer; transition: background .12s; display: flex; flex-direction: column; gap: 4px; }
.thread-row:hover { background: #f8fafc; }
.thread-row:focus-visible { outline: 2px solid var(--navy); outline-offset: -2px; }
.thread-row.active { background: #eef3fa; border-inline-start: 3px solid var(--navy); }
.thread-row.unread { background: #fbfcfe; }
.thread-row-top { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.thread-sender { font-weight: 600; font-size: 13px; display: flex; align-items: center; gap: 6px; min-width: 0; }
.unread-dot { width: 8px; height: 8px; border-radius: 50%; background: #1d4ed8; flex-shrink: 0; }
.thread-subject { font-weight: 700; font-size: 13.5px; color: var(--navy); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-snippet { color: var(--muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-badges { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-top: 2px; }
.pill { display: inline-flex; align-items: center; gap: 4px; padding: 1px 7px; border-radius: 6px; font-size: 11px; font-weight: 600; border: 1px solid var(--border); background: #f8fafc; color: var(--muted); }
.pill-portal-transport { background: #eff6ff; border-color: #bfdbfe; color: #1e40af; }
.pill-portal-finance { background: #ecfdf3; border-color: #abefc6; color: #067647; }
.pill-portal-monitor { background: #f4f3ff; border-color: #d9d6fe; color: #5925dc; }
.pill-portal-movement { background: #fff7ed; border-color: #fed7aa; color: #c2410c; }
.pill-urgent { background: var(--danger-bg); border-color: #fecdca; color: var(--danger); }
.pill-read { background: var(--success-bg); border-color: #abefc6; color: var(--success); }

/* Reader */
.reader-pane { flex: 1 1 auto; min-width: 0; background: var(--bg); display: flex; flex-direction: column; overflow: hidden; }
.reader-empty { margin: auto; text-align: center; max-width: 400px; padding: 32px; color: var(--muted); }
.empty-icon { font-size: 38px; margin-bottom: 10px; color: var(--navy); }
.reader-content { display: flex; flex-direction: column; height: 100%; overflow-y: auto; padding: 16px 20px; gap: 14px; }
.reader-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.reader-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.thread-title-bar { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
.thread-title-bar h1 { margin: 0 0 8px; font-size: 18px; color: var(--navy); }
.participants-strip { display: flex; flex-wrap: wrap; gap: 6px; }

.messages-stream { display: flex; flex-direction: column; gap: 12px; }
.msg-card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 16px; }
.msg-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; border-bottom: 1px solid var(--border); padding-bottom: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.msg-author { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.msg-author strong { font-size: 14px; color: var(--navy); }
.msg-recipients { font-size: 12px; color: var(--muted); margin-top: 6px; display: flex; flex-wrap: wrap; gap: 6px; }
.msg-body { line-height: 1.7; font-size: 14px; overflow-wrap: anywhere; }
.msg-body table { border-collapse: collapse; width: 100%; margin: 8px 0; }
.msg-body th, .msg-body td { border: 1px solid var(--border); padding: 6px 8px; text-align: start; }
.msg-body th { background: #f8fafc; }
.msg-body blockquote { margin: 8px 0; padding: 6px 12px; border-inline-start: 3px solid var(--gold); background: #fafbfd; color: #334155; }
.msg-body img { max-width: 100%; max-height: 420px; border-radius: 8px; border: 1px solid var(--border); margin: 6px 0; cursor: pointer; }

/* Entity Cards */
.entity-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 8px; margin-top: 12px; }
.entity-card { border: 1px solid #d5deea; background: #f8fafd; border-radius: 8px; padding: 10px 12px; display: flex; flex-direction: column; gap: 4px; }
.entity-card-top { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.entity-card-title { font-weight: 700; color: var(--navy); font-size: 13.5px; }
.entity-card-sub { font-size: 12.5px; color: var(--text); }
.entity-card-meta { font-size: 11.5px; color: var(--muted); }
.entity-card-foot { display: flex; align-items: center; justify-content: space-between; margin-top: 4px; }
.entity-card-restricted { border-color: var(--border); background: #f8fafc; }
.entity-card-restricted .pill { background: #f1f5f9; border-color: var(--border); color: var(--muted); }

/* Attachments Strip */
.att-grid { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--border); }
.att-chip { display: inline-flex; align-items: center; gap: 8px; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: #fff; font-size: 12.5px; }
.att-chip-name { font-weight: 600; max-width: 210px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Reply & Rich Editor */
.reply-box { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.reply-box-head, .reply-box-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.editor-toolbar { display: flex; flex-wrap: wrap; gap: 4px; padding: 6px; background: #f8fafc; border: 1px solid var(--border); border-bottom: none; border-radius: 8px 8px 0 0; }
.editor-toolbar button { background: #fff; border: 1px solid var(--border); border-radius: 6px; padding: 4px 8px; cursor: pointer; font-size: 12px; }
.editor-toolbar button:hover { background: #eef2f7; }
.rich-editor { min-height: 110px; max-height: 320px; overflow-y: auto; border: 1px solid var(--border); border-radius: 0 0 8px 8px; padding: 10px 12px; background: #fff; outline: none; }
.rich-editor.tall { min-height: 190px; }
.rich-editor:focus { border-color: var(--navy); }
.rich-editor img { max-width: 100%; max-height: 220px; border-radius: 6px; border: 1px solid var(--border); margin: 4px 0; }

/* Picked items in composer/reply */
.picked-entities, .picked-attachments { display: flex; flex-wrap: wrap; gap: 8px; }
.picked-tag { display: inline-flex; align-items: center; gap: 6px; background: #f1f5f9; border: 1px solid var(--border); border-radius: 8px; padding: 5px 9px; font-size: 12.5px; }
.picked-tag button { background: none; border: none; color: var(--danger); cursor: pointer; font-weight: 700; padding: 0 2px; }

/* Dialogs */
dialog.mail-dialog { border: 1px solid var(--border); border-radius: 14px; padding: 0; max-width: 92vw; width: 780px; background: var(--card); color: var(--text); }
dialog.preview-dialog { width: 920px; }
dialog.entity-dialog { width: 700px; }
dialog::backdrop { background: rgba(15, 33, 56, 0.52); }
.dialog-inner { display: flex; flex-direction: column; max-height: 88vh; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.dialog-head h2 { margin: 0; font-size: 16px; color: var(--navy); }
.dialog-body { padding: 16px 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.dialog-foot { display: flex; align-items: center; gap: 10px; padding: 12px 18px; border-top: 1px solid var(--border); }
.foot-spacer { flex: 1 1 auto; }

.field-group { display: flex; flex-direction: column; gap: 5px; }
.field-group label { font-weight: 600; font-size: 13px; }
.field-label-row { display: flex; align-items: center; justify-content: space-between; }
.field-group input[type="text"], .field-group select, .entity-filter-row input, .entity-filter-row select { height: 36px; border: 1px solid var(--border); border-radius: 8px; padding: 0 10px; background: #fff; }
.subject-priority-row { display: flex; gap: 12px; flex-wrap: wrap; }
.priority-select-wrap { width: 140px; }

/* Recipient Picker */
.recipient-picker { position: relative; border: 1px solid var(--border); border-radius: 8px; padding: 6px; background: #fff; }
.chips-box { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.chips-box:empty { display: none; }
.picker-search-row { display: flex; gap: 6px; }
.picker-search-row select { height: 32px; border: 1px solid var(--border); border-radius: 6px; padding: 0 8px; background: #f8fafc; }
.picker-search-row input { flex: 1 1 auto; height: 32px; border: 1px solid var(--border); border-radius: 6px; padding: 0 10px; }
.picker-dropdown { position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 20; background: #fff; border: 1px solid var(--border); border-radius: 8px; max-height: 240px; overflow-y: auto; box-shadow: 0 10px 24px rgba(15, 33, 56, 0.14); }
.picker-option { width: 100%; text-align: start; padding: 8px 12px; border: none; border-bottom: 1px solid #f1f5f9; background: #fff; cursor: pointer; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.picker-option:hover { background: #f8fafc; }

/* Entity picker */
.entity-filter-row { display: flex; gap: 8px; flex-wrap: wrap; }
.entity-filter-row input { flex: 1 1 260px; }
.entity-results { display: flex; flex-direction: column; gap: 8px; max-height: 55vh; overflow-y: auto; }
.entity-result-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }

/* Preview Dialog Body */
.preview-head-actions { display: flex; align-items: center; gap: 8px; }
.preview-body { padding: 18px; overflow-y: auto; min-height: 360px; max-height: 74vh; background: #f8fafc; }
.preview-img { display: block; max-width: 100%; max-height: 68vh; margin: 0 auto; border-radius: 8px; border: 1px solid var(--border); background: #fff; }
.preview-pdf-frame { width: 100%; height: 68vh; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
.docx-sheet { background: #fff; border: 1px solid var(--border); border-radius: 10px; padding: 24px 28px; max-width: 780px; margin: 0 auto; line-height: 1.8; }
.docx-sheet h1, .docx-sheet h2, .docx-sheet h3 { color: var(--navy); margin: 14px 0 6px; }
.docx-sheet p { margin: 6px 0; }
.docx-sheet .docx-list { padding-inline-start: 18px; position: relative; }
.docx-sheet .docx-list::before { content: "•"; position: absolute; inset-inline-start: 4px; color: var(--gold); font-weight: 700; }
.docx-sheet table, .xlsx-table { width: 100%; border-collapse: collapse; margin: 12px 0; background: #fff; font-size: 13px; }
.docx-sheet th, .docx-sheet td, .xlsx-table td { border: 1px solid var(--border); padding: 6px 10px; vertical-align: top; }
.sheet-tabs { display: flex; gap: 6px; margin-bottom: 10px; flex-wrap: wrap; }
.preview-txt { background: #fff; border: 1px solid var(--border); border-radius: 8px; padding: 14px; white-space: pre-wrap; word-break: break-word; font-family: monospace; font-size: 13px; margin: 0; }

.mobile-only { display: none; }
.pill-portal-maintenance { background: #fef3c7; color: #92400e; }
.rich-editor:empty::before { content: attr(data-placeholder); color: var(--muted, #64748b); pointer-events: none; display: block; }
.load-more-wrap { padding: 10px 14px; text-align: center; }

@media (max-width: 880px) {
  .mobile-only { display: inline-flex; }
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 10px; gap: 8px; }
  .topbar-search { order: 3; flex: 1 1 100%; max-width: none; }
  .user-chip { display: none; }
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 40; padding-top: 16px; transform: translateX(110%); transition: transform .18s; box-shadow: -8px 0 24px rgba(0,0,0,0.16); }
  .sidebar.open { transform: translateX(0); }
  .list-pane { width: 100%; border-inline-end: none; }
  .reader-pane { width: 100%; }
  .mail-shell.reading .list-pane { display: none; }
  .mail-shell:not(.reading) .reader-pane { display: none; }
  .reader-content { padding: 12px; }
  dialog.mail-dialog { width: 96vw; max-width: 96vw; }
}

/* the hidden attribute wins over any display the classes set (the record picker button of a user with nothing to link) */
[hidden] { display: none !important; }
