/* =====================================================================
   Orvix Webmail — production-grade enterprise stylesheet.
   Layout: 3-pane (sidebar | list | reading pane), responsive,
   dark theme with accessible contrast. RTL-ready.
   ===================================================================== */

:root {
  --bg-0: #0e1116;
  --bg-1: #141821;
  --bg-2: #1b212c;
  --bg-3: #232a36;
  --bg-4: #2d3441;
  --bg-hover: #243044;
  --bg-selected: #1f3a5f;
  --bg-selected-soft: #182a45;
  --border-1: #2a3140;
  --border-2: #38415a;
  --text-0: #f4f6fb;
  --text-1: #d6dbe6;
  --text-2: #a3acbf;
  --text-3: #7a8294;
  --accent: #4c8dff;
  --accent-hover: #6ba1ff;
  --accent-soft: #1c2f55;
  --danger: #ff6b6b;
  --danger-hover: #ff8787;
  --danger-soft: #3a1e26;
  --warn: #f5b14b;
  --success: #5dd39e;
  --info: #6bd0e8;
  --shadow-1: 0 1px 0 rgba(0, 0, 0, 0.4);
  --shadow-2: 0 6px 18px rgba(0, 0, 0, 0.45);
  --shadow-3: 0 12px 36px rgba(0, 0, 0, 0.55);
  --radius-1: 4px;
  --radius-2: 8px;
  --radius-3: 12px;
  --sidebar-w: 240px;
  --list-w: 380px;
  --topbar-h: 56px;
  --font-stack: "Segoe UI", "SF Pro Text", "Helvetica Neue", "Noto Sans",
    "Noto Sans Arabic", "Noto Sans Hebrew", system-ui, -apple-system,
    BlinkMacSystemFont, Roboto, Arial, sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg-0);
  color: var(--text-0);
  font-family: var(--font-stack);
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
}

button,
input,
textarea,
select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

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

/* ── Top bar ─────────────────────────────────────────────── */
.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  background: var(--bg-1);
  border-bottom: 1px solid var(--border-1);
  flex-shrink: 0;
  position: relative;
  z-index: 5;
}

.topbar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.2px;
  color: var(--text-0);
  white-space: nowrap;
}

.topbar .brand .logo {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: linear-gradient(135deg, #4c8dff 0%, #2d6cdf 100%);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  box-shadow: var(--shadow-1);
}

.topbar .menu-btn {
  display: none;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-1);
  color: var(--text-1);
  padding: 6px 8px;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}
.topbar .menu-btn:hover {
  background: var(--bg-hover);
  border-color: var(--border-1);
}

.topbar .search {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 640px;
  position: relative;
}
.topbar .search input {
  flex: 1;
  background: var(--bg-2);
  border: 1px solid var(--border-1);
  border-radius: var(--radius-2);
  padding: 8px 12px 8px 36px;
  color: var(--text-0);
  outline: none;
  transition: border-color 0.15s, background 0.15s;
  width: 100%;
}
.topbar .search input::placeholder {
  color: var(--text-3);
}
.topbar .search input:focus {
  border-color: var(--accent);
  background: var(--bg-3);
}
.topbar .search .icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-3);
  font-size: 14px;
  pointer-events: none;
}
.topbar .search .clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: 0;
  color: var(--text-3);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
  border-radius: var(--radius-1);
}
.topbar .search .clear:hover {
  color: var(--text-0);
  background: var(--bg-hover);
}

.topbar .actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar .user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px 4px 4px;
  background: var(--bg-2);
  border: 1px solid var(--border-1);
  border-radius: 999px;
  color: var(--text-1);
  font-size: 13px;
  cursor: default;
}
.topbar .user-chip .avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 11px;
}

/* ── Buttons ─────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--radius-2);
  border: 1px solid var(--border-2);
  background: var(--bg-2);
  color: var(--text-0);
  cursor: pointer;
  font-weight: 500;
  font-size: 13px;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s, transform 0.05s;
}
.btn:hover {
  background: var(--bg-3);
  border-color: var(--border-2);
}
.btn:active {
  transform: translateY(1px);
}
.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.btn.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
.btn.danger:hover {
  background: var(--danger-hover);
  border-color: var(--danger-hover);
}
.btn.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-1);
}
.btn.ghost:hover {
  background: var(--bg-hover);
  color: var(--text-0);
}
.btn.sm {
  padding: 4px 10px;
  font-size: 12px;
}
.btn.icon-only {
  padding: 6px 8px;
  min-width: 30px;
}
.btn[disabled],
.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-2);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-1);
  font-size: 14px;
  line-height: 1;
}
.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text-0);
}
.icon-btn.active {
  color: var(--accent);
  background: var(--accent-soft);
}

/* ── 3-pane layout ───────────────────────────────────────── */
.app {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-1);
  border-right: 1px solid var(--border-1);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  min-height: 0;
}

.sidebar .compose-btn-wrap {
  padding: 14px 14px 8px;
}
.sidebar .compose-btn {
  width: 100%;
  padding: 10px 14px;
  font-size: 14px;
  font-weight: 600;
  border-radius: var(--radius-2);
}

.sidebar nav {
  flex: 1;
  overflow-y: auto;
  padding: 4px 6px 12px;
}

.sidebar nav .group-label {
  padding: 12px 12px 6px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-3);
}

.folder {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-2);
  cursor: pointer;
  color: var(--text-1);
  user-select: none;
  position: relative;
  font-size: 13.5px;
}
.folder:hover {
  background: var(--bg-hover);
  color: var(--text-0);
}
.folder.active {
  background: var(--bg-selected);
  color: #fff;
}
.folder .icon {
  width: 18px;
  text-align: center;
  font-size: 14px;
  color: inherit;
  opacity: 0.9;
}
.folder .label {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.folder .count {
  background: var(--bg-3);
  color: var(--text-1);
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 999px;
  min-width: 22px;
  text-align: center;
}
.folder.active .count {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
}
.folder .unread-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
}

.sidebar .footer {
  padding: 10px 14px;
  border-top: 1px solid var(--border-1);
  font-size: 11px;
  color: var(--text-3);
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ── Message list ────────────────────────────────────────── */
.list-pane {
  width: var(--list-w);
  background: var(--bg-1);
  border-right: 1px solid var(--border-1);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  min-height: 0;
}

.list-header {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  gap: 8px;
  border-bottom: 1px solid var(--border-1);
  background: var(--bg-1);
  min-height: 44px;
}
.list-header .title {
  flex: 1;
  font-weight: 600;
  font-size: 14px;
  color: var(--text-0);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.list-header .count {
  color: var(--text-3);
  font-size: 12px;
  font-weight: normal;
  margin-inline-start: 6px;
}

.list-toolbar {
  display: flex;
  align-items: center;
  padding: 6px 8px;
  gap: 4px;
  border-bottom: 1px solid var(--border-1);
  background: var(--bg-1);
}
.list-toolbar .spacer {
  flex: 1;
}

.messages {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}

.message {
  display: grid;
  grid-template-columns: 32px 1fr auto;
  column-gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-1);
  cursor: pointer;
  align-items: start;
  user-select: none;
}
.message:hover {
  background: var(--bg-2);
}
.message.selected {
  background: var(--bg-selected-soft);
  border-left: 3px solid var(--accent);
  padding-left: 9px;
}
.message.unread {
  background: var(--bg-2);
}
.message.unread.selected {
  background: var(--bg-selected);
}
.message.unread .from,
.message.unread .subject,
.message.unread .preview {
  font-weight: 600;
  color: var(--text-0);
}

.message .star {
  margin-top: 2px;
  color: var(--text-3);
  cursor: pointer;
  font-size: 14px;
  background: transparent;
  border: 0;
  padding: 0;
}
.message .star.on {
  color: var(--warn);
}
.message .star:hover {
  color: var(--warn);
}

.message .body {
  min-width: 0;
}
.message .from {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-1);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.message .from .name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.message .from .badge {
  font-size: 10px;
  text-transform: uppercase;
  padding: 1px 5px;
  border-radius: 3px;
  background: var(--bg-3);
  color: var(--text-2);
  font-weight: 600;
}
.message .subject {
  font-size: 13px;
  color: var(--text-1);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}
.message .preview {
  font-size: 12.5px;
  color: var(--text-2);
  margin-top: 2px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}
.message .meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  font-size: 11.5px;
  color: var(--text-3);
  white-space: nowrap;
}
.message .meta .icons {
  display: flex;
  gap: 6px;
  color: var(--text-2);
}
.message .meta .icons .att {
  color: var(--text-2);
}

.empty-state,
.error-state,
.loading-state {
  padding: 40px 24px;
  text-align: center;
  color: var(--text-3);
  font-size: 13px;
}
.error-state {
  color: var(--danger);
}
.loading-state .spinner {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2px solid var(--border-2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
  margin-inline-end: 8px;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ── Reading pane ────────────────────────────────────────── */
.reading-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-0);
  min-width: 0;
  min-height: 0;
}

.reading-pane .toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-1);
  background: var(--bg-1);
  min-height: 44px;
}
.reading-pane .toolbar .spacer {
  flex: 1;
}

.message-view {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}

.message-view .header {
  padding: 18px 24px 12px;
  border-bottom: 1px solid var(--border-1);
  background: var(--bg-1);
}
.message-view .header h1 {
  margin: 0 0 10px;
  font-size: 20px;
  font-weight: 600;
  color: var(--text-0);
  word-break: break-word;
}
.message-view .header .meta-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.message-view .header .avatar-lg {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  flex-shrink: 0;
}
.message-view .header .from-block {
  flex: 1;
  min-width: 0;
}
.message-view .header .from-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-0);
}
.message-view .header .from-email {
  font-size: 12px;
  color: var(--text-2);
}
.message-view .header .date {
  font-size: 12px;
  color: var(--text-3);
  white-space: nowrap;
}

.message-view .details {
  padding: 8px 24px 4px;
  background: var(--bg-1);
  border-bottom: 1px solid var(--border-1);
  font-size: 12.5px;
  color: var(--text-2);
}
.message-view .details .row {
  display: flex;
  gap: 8px;
  padding: 2px 0;
}
.message-view .details .row .label {
  color: var(--text-3);
  width: 60px;
  flex-shrink: 0;
}
.message-view .details .row .value {
  flex: 1;
  word-break: break-word;
}

.message-view .body {
  padding: 24px;
  font-size: 14px;
  line-height: 1.7;
  color: var(--text-1);
}
.message-view .body pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  font-family: inherit;
  margin: 0;
}
.message-view .body a {
  color: var(--accent);
}
.message-view .body img {
  max-width: 100%;
  height: auto;
}
.message-view .body blockquote {
  border-inline-start: 3px solid var(--border-2);
  margin: 12px 0;
  padding: 6px 14px;
  color: var(--text-2);
  background: var(--bg-2);
  border-radius: var(--radius-1);
}
.message-view .body .quote-meta {
  color: var(--text-3);
  font-size: 12px;
  margin-bottom: 6px;
}

.attachments {
  margin: 16px 0;
  padding: 12px;
  background: var(--bg-1);
  border: 1px solid var(--border-1);
  border-radius: var(--radius-2);
}
.attachments h3 {
  margin: 0 0 8px;
  font-size: 12px;
  color: var(--text-3);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
}
.attachments ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.attachments li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border-radius: var(--radius-1);
}
.attachments li:hover {
  background: var(--bg-2);
}
.attachments .att-icon {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-1);
  background: var(--bg-3);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-2);
  font-size: 16px;
}
.attachments .att-name {
  flex: 1;
  font-size: 13px;
  color: var(--text-1);
}
.attachments .att-size {
  font-size: 11px;
  color: var(--text-3);
}

/* ── Compose modal ───────────────────────────────────────── */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 24px;
}
.modal {
  width: min(820px, 100%);
  max-height: calc(100vh - 48px);
  background: var(--bg-1);
  border: 1px solid var(--border-2);
  border-radius: var(--radius-3);
  box-shadow: var(--shadow-3);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-1);
  background: var(--bg-2);
}
.modal-header .title {
  flex: 1;
  font-weight: 600;
  font-size: 14px;
}
.modal-body {
  padding: 0;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}
.modal-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border-1);
  background: var(--bg-2);
}

.field {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border-1);
}
.field .label {
  font-size: 12px;
  color: var(--text-3);
  text-transform: uppercase;
  width: 60px;
  flex-shrink: 0;
  letter-spacing: 0.4px;
}
.field input,
.field textarea {
  flex: 1;
  background: transparent;
  border: 0;
  outline: none;
  color: var(--text-0);
  font-size: 14px;
  min-width: 0;
}
.field.cc-bcc {
  padding: 0 14px;
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.2s ease, padding 0.2s ease;
  border-bottom: 0;
}
.field.cc-bcc.open {
  max-height: 60px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border-1);
}
.field input::placeholder,
.field textarea::placeholder {
  color: var(--text-3);
}

.compose-body {
  width: 100%;
  min-height: 280px;
  background: transparent;
  border: 0;
  outline: none;
  color: var(--text-1);
  font-family: var(--font-stack);
  font-size: 14px;
  line-height: 1.6;
  padding: 14px;
  resize: vertical;
}

/* ── Toasts ──────────────────────────────────────────────── */
.toasts {
  position: fixed;
  bottom: 16px;
  inset-inline-end: 16px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 360px;
}
.toast {
  padding: 10px 14px;
  border-radius: var(--radius-2);
  background: var(--bg-2);
  border: 1px solid var(--border-2);
  color: var(--text-0);
  box-shadow: var(--shadow-2);
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
  animation: toast-in 0.18s ease-out;
}
.toast.success {
  border-color: var(--success);
}
.toast.error {
  border-color: var(--danger);
  background: var(--danger-soft);
  color: #ffd5d5;
}
.toast .close {
  margin-inline-start: auto;
  background: transparent;
  border: 0;
  color: inherit;
  cursor: pointer;
  opacity: 0.7;
}
.toast .close:hover {
  opacity: 1;
}
@keyframes toast-in {
  from {
    transform: translateY(8px);
    opacity: 0;
  }
}

/* ── Misc ────────────────────────────────────────────────── */
[hidden] {
  display: none !important;
}

.kbd {
  display: inline-block;
  padding: 0 5px;
  border-radius: 3px;
  background: var(--bg-3);
  border: 1px solid var(--border-2);
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-2);
}

.spinner-inline {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid var(--border-2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -2px;
  margin-inline-end: 4px;
}

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 1100px) {
  :root {
    --list-w: 340px;
  }
}

@media (max-width: 860px) {
  .topbar .menu-btn {
    display: inline-flex;
  }
  .topbar .brand span:not(.logo) {
    display: none;
  }
  .sidebar {
    position: absolute;
    inset-inline-start: 0;
    top: 0;
    bottom: 0;
    z-index: 10;
    transform: translateX(-100%);
    transition: transform 0.18s ease-out;
    box-shadow: var(--shadow-3);
  }
  html[dir="rtl"] .sidebar {
    transform: translateX(100%);
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .list-pane {
    width: 100%;
    border-right: 0;
  }
  .reading-pane {
    position: absolute;
    inset: 0;
    z-index: 8;
    background: var(--bg-0);
  }
  .reading-pane:not(.open) {
    display: none;
  }
  .back-btn {
    display: inline-flex !important;
  }
}

.back-btn {
  display: none;
}

/* ── RTL support ─────────────────────────────────────────── */
html[dir="rtl"] body {
  /* font stack already covers Arabic via Noto Sans Arabic */
}
html[dir="rtl"] .message-view .details .row,
html[dir="rtl"] .message-view .header .meta-row,
html[dir="rtl"] .field {
  direction: rtl;
}
html[dir="rtl"] .topbar .search .icon {
  left: auto;
  right: 12px;
}
html[dir="rtl"] .topbar .search input {
  padding: 8px 36px 8px 12px;
}
html[dir="rtl"] .topbar .search .clear {
  right: auto;
  left: 8px;
}
html[dir="rtl"] .modal {
  direction: rtl;
}

/* ── High-contrast focus rings (accessibility) ───────────── */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
