/* CYRIF Mail Admin — blue theme */
:root {
  --bg: #0a1528;
  --bg-card: #0f1e3d;
  --bg-elev: #152b52;
  --border: #1e3763;
  --text: #e6edf7;
  --text-dim: #8ea5c9;
  --accent: #3b82f6;
  --accent-hover: #60a5fa;
  --danger: #ef4444;
  --success: #22c55e;
  --warn: #f59e0b;
  --shadow: 0 4px 16px rgba(0,0,0,0.25);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; background: var(--bg); color: var(--text); font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }

/* ---------- Login ---------- */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(180deg, #0a1528, #061023); }
.login-card { width: 360px; background: var(--bg-card); border: 1px solid var(--border); border-radius: 14px; padding: 32px; box-shadow: var(--shadow); }
.brand { text-align: center; margin-bottom: 24px; }
.brand-mark { font-size: 28px; font-weight: 700; letter-spacing: 2px; color: var(--accent); }
.brand-sub { color: var(--text-dim); font-size: 13px; letter-spacing: 1.5px; text-transform: uppercase; margin-top: 2px; }
.login-card label { display: block; margin-bottom: 14px; }
.login-card label span { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.8px; }
.login-card input, .login-card button { width: 100%; padding: 10px 12px; border-radius: 8px; font-size: 14px; }
.login-card input { background: var(--bg-elev); border: 1px solid var(--border); color: var(--text); }
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-card button { background: var(--accent); color: white; border: none; font-weight: 600; cursor: pointer; margin-top: 8px; }
.login-card button:hover { background: var(--accent-hover); }
.msg { color: var(--danger); font-size: 13px; margin-top: 12px; text-align: center; min-height: 18px; }

/* ---------- App shell ---------- */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: #081224; border-right: 1px solid var(--border); padding: 20px 0; }
.sidebar .brand { text-align: left; padding: 0 20px; margin-bottom: 24px; }
.sidebar .brand-mark { font-size: 20px; }
.sidebar .brand-sub { font-size: 11px; }
.sidebar nav a { display: flex; align-items: center; gap: 10px; padding: 10px 20px; color: var(--text-dim); text-decoration: none; font-size: 14px; border-left: 3px solid transparent; }
.sidebar nav a:hover { background: rgba(59, 130, 246, 0.08); color: var(--text); }
.sidebar nav a.active { background: rgba(59, 130, 246, 0.12); color: var(--accent); border-left-color: var(--accent); }
.sidebar nav a .dot { width: 6px; height: 6px; background: currentColor; border-radius: 50%; }

.main { padding: 28px 36px; overflow-y: auto; }
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; }
.page-header h1 { font-size: 24px; margin: 0; font-weight: 600; }
.user-chip { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--text-dim); }
.user-chip a { color: var(--text-dim); text-decoration: none; padding: 4px 10px; border: 1px solid var(--border); border-radius: 6px; }
.user-chip a:hover { color: var(--text); border-color: var(--accent); }

/* ---------- Cards ---------- */
.card { background: var(--bg-card); border: 1px solid var(--border); border-radius: 12px; padding: 20px; margin-bottom: 20px; }
.card h2 { font-size: 16px; margin: 0 0 16px 0; font-weight: 600; color: var(--text); }
.card h2 small { font-weight: 400; color: var(--text-dim); font-size: 13px; margin-left: 8px; }

/* ---------- Buttons ---------- */
.btn { padding: 8px 14px; border-radius: 6px; font-size: 13px; font-weight: 500; cursor: pointer; border: 1px solid var(--border); background: var(--bg-elev); color: var(--text); }
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: white; }
.btn-sm { padding: 4px 10px; font-size: 12px; }

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th { text-align: left; padding: 10px 8px; color: var(--text-dim); font-weight: 500; font-size: 11px; text-transform: uppercase; letter-spacing: 0.8px; border-bottom: 1px solid var(--border); }
tbody td { padding: 10px 8px; border-bottom: 1px solid rgba(30, 55, 99, 0.3); }
tbody tr:hover { background: rgba(59, 130, 246, 0.04); }
.td-actions { text-align: right; white-space: nowrap; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 500; }
.badge.passwdfile { background: rgba(59, 130, 246, 0.15); color: var(--accent); }
.badge.system { background: rgba(245, 158, 11, 0.15); color: var(--warn); }
.badge.unknown { background: rgba(239, 68, 68, 0.15); color: var(--danger); }

/* ---------- Forms ---------- */
.form-row { display: flex; gap: 12px; margin-bottom: 12px; }
.form-row label { flex: 1; display: block; }
.form-row label span { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 4px; text-transform: uppercase; letter-spacing: 0.5px; }
.form-row input, .form-row select, .form-row textarea { width: 100%; padding: 8px 10px; background: var(--bg-elev); border: 1px solid var(--border); color: var(--text); border-radius: 6px; font-size: 13px; font-family: inherit; }
.form-row input:focus, .form-row select:focus, .form-row textarea:focus { outline: none; border-color: var(--accent); }

.toolbar { display: flex; gap: 12px; align-items: center; margin-bottom: 14px; }
.toolbar .search { flex: 1; padding: 8px 12px; background: var(--bg-elev); border: 1px solid var(--border); color: var(--text); border-radius: 6px; font-size: 13px; }

/* ---------- Modal ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal { background: var(--bg-card); border: 1px solid var(--border); border-radius: 12px; padding: 24px; width: 420px; max-width: 90vw; max-height: 85vh; overflow-y: auto; }
.modal h3 { margin: 0 0 16px 0; font-size: 16px; }
.modal .actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }

/* ---------- Health tiles ---------- */
.health-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.health-tile { background: var(--bg-elev); border-radius: 8px; padding: 14px; }
.health-tile .k { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.8px; margin-bottom: 6px; }
.health-tile .v { font-size: 15px; font-weight: 500; }
.dot-ok { color: var(--success); }
.dot-bad { color: var(--danger); }

/* ---------- Toasts ---------- */
#toast-root { position: fixed; top: 20px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast { padding: 10px 16px; border-radius: 8px; font-size: 13px; box-shadow: var(--shadow); animation: slide-in 0.2s ease; }
.toast.ok { background: var(--success); color: white; }
.toast.err { background: var(--danger); color: white; }
@keyframes slide-in { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

.empty { text-align: center; padding: 40px; color: var(--text-dim); font-size: 13px; }
code { background: var(--bg-elev); padding: 2px 6px; border-radius: 4px; font-size: 12px; }
.hidden { display: none !important; }

/* ---------- Email row preview/click (Phases 1-2) ---------- */
.email-row { cursor: pointer; }
.email-row:hover { background: rgba(59, 130, 246, 0.08); }
.email-row:active { background: rgba(59, 130, 246, 0.14); }
#email-snippet-tip {
  position: fixed;
  z-index: 300;
  max-width: 520px;
  padding: 10px 14px;
  background: #0a1528;
  border: 1px solid var(--accent);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  font-size: 12px;
  line-height: 1.45;
  color: var(--text);
  pointer-events: none;
  white-space: pre-wrap;
  word-break: break-word;
  opacity: 0;
  transition: opacity 0.15s ease;
}
#email-snippet-tip.visible { opacity: 1; }
#email-snippet-tip .tip-from { color: var(--text-dim); font-size: 11px; margin-bottom: 6px; font-family: ui-monospace, monospace; }
#email-snippet-tip .tip-subject { font-weight: 600; margin-bottom: 6px; color: var(--accent); }
#email-snippet-tip .tip-body { color: var(--text); }
#email-snippet-tip .tip-empty { color: var(--text-dim); font-style: italic; }

/* =============================================================
   CEO Mobile View — only applies when body[data-role="mailflow_only"]
   Targets phones (<768px). Admin panel for issa is unaffected.
   ============================================================= */

/* Disable hover snippet tooltip on touch devices (no hover) */
@media (hover: none) {
  #email-snippet-tip { display: none !important; }
}

@media (max-width: 768px) {
  /* Tighter padding on small screens (general improvement, no role check) */
  .main { padding: 14px 10px; }

  /* Modal goes full-screen-ish on mobile */
  .modal {
    width: 96vw !important;
    max-width: 96vw !important;
    max-height: 92vh !important;
    padding: 16px !important;
  }
}

/* CEO-specific mobile: hide sidebar entirely, big top header, card layout for emails */
@media (max-width: 768px) {
  body[data-role="mailflow_only"] .app {
    grid-template-columns: 1fr; /* single column, no sidebar */
  }
  body[data-role="mailflow_only"] .sidebar { display: none; }
  body[data-role="mailflow_only"] .main { padding: 10px 8px 30px; }

  /* Big mobile header bar */
  body[data-role="mailflow_only"] .page-header {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 12px 12px 10px;
    background: var(--bg-card);
    border-radius: 12px;
    margin-bottom: 12px;
  }
  body[data-role="mailflow_only"] .page-header h1 {
    font-size: 22px;
    text-align: center;
  }
  body[data-role="mailflow_only"] .page-header h1::before {
    content: "📧 ";
  }
  body[data-role="mailflow_only"] .user-chip {
    justify-content: center;
    flex-wrap: wrap;
    font-size: 12px;
  }
  body[data-role="mailflow_only"] .user-chip a {
    padding: 8px 14px;
    min-height: 36px;
  }

  /* Tighter cards */
  body[data-role="mailflow_only"] .card {
    padding: 12px 10px;
    margin-bottom: 12px;
    border-radius: 10px;
  }
  body[data-role="mailflow_only"] .card h2 {
    font-size: 14px;
    margin-bottom: 12px;
  }
  body[data-role="mailflow_only"] .card h2 small {
    display: block;
    margin-left: 0;
    margin-top: 4px;
  }

  /* Mail flow summary tiles: 2 columns on mobile */
  body[data-role="mailflow_only"] .health-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }
  body[data-role="mailflow_only"] .health-tile {
    padding: 10px;
  }
  body[data-role="mailflow_only"] .health-tile .v {
    font-size: 17px;
  }

  /* Email tables → CARD layout. Each row becomes a stacked card. */
  body[data-role="mailflow_only"] table thead { display: none; }
  body[data-role="mailflow_only"] table,
  body[data-role="mailflow_only"] table tbody,
  body[data-role="mailflow_only"] table tr,
  body[data-role="mailflow_only"] table td {
    display: block;
    width: 100%;
    box-sizing: border-box;
  }
  body[data-role="mailflow_only"] table tr.email-row {
    background: var(--bg-elev);
    border-radius: 10px;
    padding: 12px 14px;
    margin-bottom: 10px;
    border: 1px solid var(--border);
  }
  body[data-role="mailflow_only"] table tr.email-row:hover {
    background: var(--bg-elev);
    border-color: var(--accent);
  }
  body[data-role="mailflow_only"] table tr.email-row td {
    padding: 2px 0;
    border: none;
    text-align: left !important;
    white-space: normal;
  }
  /* Time row — top, dim */
  body[data-role="mailflow_only"] table tr.email-row td:first-child {
    color: var(--text-dim);
    font-size: 11px;
    margin-bottom: 4px;
  }
  /* Mailbox row — strong, accent */
  body[data-role="mailflow_only"] table tr.email-row td:nth-child(2) {
    font-size: 13px;
  }
  body[data-role="mailflow_only"] table tr.email-row td:nth-child(2) strong {
    color: var(--accent);
  }
  /* From / To row */
  body[data-role="mailflow_only"] table tr.email-row td:nth-child(3) {
    color: var(--text-dim);
    font-size: 12px;
    margin-top: 4px;
  }
  /* Subject row — main visual */
  body[data-role="mailflow_only"] table tr.email-row td:nth-child(4) {
    font-size: 14px;
    color: var(--text);
    margin-top: 4px;
    font-weight: 500;
  }
  /* Last column (size or delivery+size) */
  body[data-role="mailflow_only"] table tr.email-row td:last-child,
  body[data-role="mailflow_only"] table tr.email-row td:nth-last-child(2) {
    margin-top: 6px;
    font-size: 11px;
  }

  /* Recent events table (mailflow log) — keep readable, allow horizontal scroll */
  body[data-role="mailflow_only"] table:not(:has(tr.email-row)) {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    font-size: 11px;
  }

  /* Touch targets — bigger buttons */
  body[data-role="mailflow_only"] .btn {
    padding: 10px 16px;
    min-height: 40px;
    font-size: 13px;
  }
  body[data-role="mailflow_only"] .btn-sm {
    padding: 6px 12px;
    min-height: 32px;
  }

  /* Form inputs / selects bigger for fingers */
  body[data-role="mailflow_only"] select,
  body[data-role="mailflow_only"] input[type=text],
  body[data-role="mailflow_only"] input[type=password],
  body[data-role="mailflow_only"] input[type=number] {
    min-height: 36px;
    font-size: 14px;
  }

  /* Login page also gets mobile love */
  body[data-role="mailflow_only"] .login-card,
  .login-page .login-card {
    width: 92vw;
    padding: 22px;
  }
}

/* Login page mobile (always, regardless of role since user not yet logged in) */
@media (max-width: 768px) {
  .login-card { width: 92vw !important; padding: 22px !important; }
  .login-card input, .login-card button { min-height: 42px; font-size: 15px; }
}

/* =============================================================
   Mobile (CEO view) — STACK everything. Show full text, multi-line.
   No truncation, no ellipsis. Each cell wraps onto as many lines as
   it needs. Long unbreakable strings (emails, URLs) break-word so
   they fit the screen.
   ============================================================= */
@media (max-width: 768px) {
  body[data-role="mailflow_only"] {
    overflow-x: hidden;
  }
  body[data-role="mailflow_only"] .main {
    max-width: 100vw;
    overflow-x: hidden;
    box-sizing: border-box;
  }

  /* Email row card — full-width, all cells wrap onto as many lines as needed */
  body[data-role="mailflow_only"] table tr.email-row {
    max-width: 100%;
    box-sizing: border-box;
  }
  body[data-role="mailflow_only"] table tr.email-row td {
    box-sizing: border-box;
    max-width: 100%;
    /* No truncation — text stacks onto as many lines as needed */
    white-space: normal !important;
    overflow: visible !important;
    text-overflow: clip !important;
    /* Wrap at word boundaries first; if a single token (email address, URL)
       is wider than the card, break it mid-string so it fits */
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
    line-height: 1.4;
    padding: 3px 0;
  }

  /* Subject (4th td) — slightly bigger / bolder */
  body[data-role="mailflow_only"] table tr.email-row td:nth-child(4) {
    font-size: 14px;
    font-weight: 500;
    line-height: 1.4;
    margin-top: 4px;
  }

  /* Delivery badge — keep as one chip */
  body[data-role="mailflow_only"] table tr.email-row .badge {
    white-space: nowrap;
  }

  /* Modal: full content, wrap naturally */
  body[data-role="mailflow_only"] .modal {
    overflow-x: hidden;
  }
  body[data-role="mailflow_only"] .modal pre {
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
    max-width: 100%;
    box-sizing: border-box;
  }
  body[data-role="mailflow_only"] .modal table {
    table-layout: fixed;
    width: 100%;
  }
  body[data-role="mailflow_only"] .modal table td {
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
    white-space: normal;
  }
  body[data-role="mailflow_only"] .modal code {
    word-break: break-all;
    overflow-wrap: anywhere;
    white-space: normal;
  }

  /* Health tile values — wrap if long, no truncation */
  body[data-role="mailflow_only"] .health-tile .v {
    word-wrap: break-word;
    overflow-wrap: break-word;
    word-break: break-word;
    white-space: normal;
  }

  /* Recent events log table — keep horizontal scroll inside, stays inside page */
  body[data-role="mailflow_only"] table:not(:has(tr.email-row)) {
    max-width: 100%;
    box-sizing: border-box;
  }
}
