:root {
  --page: #f4f7f6;
  --bg: #ffffff;
  --bg2: #ffffff;
  --bg3: #f0f4f8;
  --input-bg: #ffffff;
  --hover: #f8fafc;
  --active: #eef2ff;
  --nav-hover: #f0f4f8;
  --nav-active: #eef2ff;
  --nav-active-ink: #4f46e5;
  --line: #e2e8f0;

  --ink: #0f172a;
  --dim: #475569;
  --faint: #94a3b8;

  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-border: #4f46e5;
  --accent-text: #4f46e5;
  --ring: rgba(79, 70, 229, 0.2);
  --on-accent: #ffffff;
  
  --err: #ef4444;
  --enc: #7c3aed;
  --enc-bg: rgba(124, 58, 237, 0.05); 
  --iframe-bg: #ffffff;
  --scrim: rgba(15, 23, 42, 0.6);
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --radius: 6px;

  --mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: light;
}

html.theme-dark {
  --page: #0f172a;
  --bg: #1e293b;
  --bg2: #1e293b;
  --bg3: #334155;
  --input-bg: #0f172a;
  --hover: #2dd4bf0a;
  --active: #334155;
  --nav-hover: #334155;
  --nav-active: #475569;
  --nav-active-ink: #f8fafc;
  --line: #334155;

  --ink: #f8fafc;
  --dim: #cbd5e1;
  --faint: #64748b;

  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --accent-border: #6366f1;
  --accent-text: #818cf8;
  --on-accent: #ffffff;
  
  --err: #f87171;
  --enc: #a78bfa;
  --enc-bg: rgba(167, 139, 250, 0.1);
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  margin: 0;
  padding: 0;
  /* Ensures layout stretches perfectly to edges but protects text from corners */
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  height: 100dvh; /* Dynamic viewport height adjusts instantly for the mobile keyboard */
  overflow: hidden;
}

/* Scrollbars */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: var(--dim); }

/* Forms & Buttons */
button { font-family: inherit; font-size: 13px; font-weight: 500; color: var(--ink); background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 16px; cursor: pointer; transition: all 0.15s; }
button:hover { background: var(--bg3); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
button.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

/* Ensure all standard buttons and dropdowns are tall enough to tap */
button, 
select {
  min-height: 44px;
  -webkit-tap-highlight-color: transparent; /* Removes the ugly blue flash on iOS when tapped */
}

/* Ensure your folder list and message rows are easy to tap */
.folder, 
.msg {
  min-height: 44px;
  user-select: none;
}

.btn-danger { color: var(--err); border-color: var(--line); }
.btn-danger:hover { background: var(--err); color: white; border-color: var(--err); }
input, textarea, select { font-family: inherit; font-size: 14px; color: var(--ink); background: var(--input-bg); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; width: 100%; transition: all 0.2s; }
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent-border); box-shadow: 0 0 0 3px var(--ring); }
.hidden { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }
.spacer { flex: 1; }
.err { color: var(--err); font-size: 13px; margin-top: 8px; }

/* Login */
.login-wrap { height: 100vh; display: grid; place-items: center; background: var(--page); }
.login-card { width: 400px; padding: 32px; background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 16px; }
.login-card h1 { font-size: 22px; font-weight: 600; margin-bottom: 4px; }
.login-card .sub { color: var(--dim); font-size: 13px; line-height: 1.5; margin-bottom: 8px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 500; }
#loginBtn { margin-top: 12px; padding: 12px; font-size: 14px; }

/* Layout Shell */
/* Using 100% forces it to respect the safe-area bounds we put on the body */
.app { height: 100%; width: 100%; display: flex; flex-direction: column; overflow: hidden; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 12px 24px; background: var(--bg2); border-bottom: 1px solid var(--line); }
.brand { font-size: 18px; font-weight: 600; letter-spacing: -0.5px; }
.topbar select { width: 260px; padding: 6px 10px; }
.main { flex: 1; display: grid; grid-template-columns: 200px 30% 1fr; min-height: 0; width: 100%; }
/* Sidebar & Folders */
.sidebar { padding: 16px; border-right: 1px solid var(--line); background: var(--page); display: flex; flex-direction: column; gap: 16px; }
.compose-btn { width: 100%; padding: 12px; font-size: 14px; border-radius: 8px; }
.folders { display: flex; flex-direction: column; gap: 4px; overflow-y: auto; }
.folder { padding: 10px 12px; border-radius: 6px; cursor: pointer; color: var(--dim); font-size: 14px; font-weight: 500; display: flex; justify-content: space-between; }
.folder:hover { background: var(--nav-hover); color: var(--ink); }
.folder.active { background: var(--nav-active); color: var(--nav-active-ink); }

/* Message List */
.list { border-right: 1px solid var(--line); overflow-y: auto; background: var(--bg); }
/* Ensure the grid column for the checkbox is fixed */
.msg {
  display: grid;
  grid-template-columns: 20px 1fr;
  align-items: center;
  gap: 12px;
  padding: 11px 13px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}

/* Ensure the content wrapper fills the remaining space */
.msg-content {
  display: flex;
  flex-direction: column;
  min-width: 0; /* CRITICAL: Allows ellipsis to work on long subjects */
  overflow: hidden;
}
.msg-select { cursor: pointer; }
.msg:hover { background: var(--hover); box-shadow: inset 2px 0 0 var(--line); }
.msg.active { background: var(--active); box-shadow: inset 3px 0 0 var(--accent); }
.msg-top { display: flex; justify-content: space-between; }
.msg-from { font-weight: 500; font-size: 14px; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-date { font-size: 12px; color: var(--faint); }
.msg-subj { font-size: 13px; color: var(--dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-prev { font-size: 13px; color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* Force children to respect the width limit */
.msg-subj, .msg-prev, .msg-from {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.batch-toolbar {
  padding: 10px 16px;
  background: var(--bg2);
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 8px;
  align-items: center;
}
/* ====================================================================
   Reader Pane
   ==================================================================== */

/* 1. The Container: Anchors natively to the left, no centering */
.reader {
  overflow-y: auto;
  background: var(--bg);
  display: block; 
}

/* 2. The Wrapper: The 900px Safe Zone */
#reader-content-wrapper {
  width: 100%;
  max-width: 900px;
  margin: 0; /* Keeps the entire column pinned to the left edge */
  padding: 24px;
  display: flex;
  flex-direction: column;
  /* 'align-items: flex-start' has been REMOVED so the iframe can stretch full width */
}

.reader-empty { margin: 100px auto; text-align: center; color: var(--faint); font-size: 15px; }

/* 3. The Header (Subject, From, Date, Reply Buttons) */
.reader-head {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  width: 100%;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--line);
}

.reader-subj {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 16px;
  width: 100%;
}

.reader-meta { font-size: 13px; color: var(--dim); line-height: 1.6; }

.reader-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  justify-content: flex-start;
}

/* 4. The Body (The actual email content) */
.reader-body iframe { width: 100%; border: none; min-height: 500px; background: var(--iframe-bg); }
.reader-text { padding: 24px 0; white-space: pre-wrap; font-family: var(--sans); font-size: 14px; line-height: 1.6; }
.atts { padding: 16px 0; background: var(--page); border-top: 1px solid var(--line); }

/* Modals */
.modal { position: fixed; inset: 0; background: var(--scrim); display: grid; place-items: center; z-index: 100; padding: 16px; backdrop-filter: blur(2px); }
.modal-card { width: 600px; max-height: 90vh; background: var(--bg2); border-radius: 12px; box-shadow: var(--shadow); display: flex; flex-direction: column; overflow: hidden; }
.modal-card.wide { width: 700px; }
.modal-head { padding: 16px 24px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; font-size: 16px; }
.modal-head .x { background: transparent; border: none; font-size: 24px; color: var(--dim); padding: 0; }
.modal-body { padding: 24px; overflow-y: auto; display: flex; flex-direction: column; gap: 16px; }
.modal-foot { padding: 16px 24px; border-top: 1px solid var(--line); display: flex; gap: 12px; background: var(--page); }
.cfield { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 500; color: var(--dim); }
.krow { display: flex; gap: 16px; }
.krow .cfield { flex: 1; }
.ksec { margin-top: 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--faint); border-bottom: 1px solid var(--line); padding-bottom: 4px; }

/* Theme Toggle Utility */
.theme-toggle { background: transparent; border: none; padding: 4px; display: flex; align-items: center; justify-content: center; border-radius: 50%; width: 32px; height: 32px; }
.theme-toggle svg { width: 18px; height: 18px; }
.theme-toggle .icon-sun { display: none; }
html.theme-dark .theme-toggle .icon-sun { display: block; }
html.theme-dark .theme-toggle .icon-moon { display: none; }

/* Progress & Attachments */
.upload-progress-track { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; margin-bottom: 4px; }
.upload-progress-track span { display: block; height: 100%; background: var(--enc); transition: width 0.2s; }
.compose-atts { border: 1px solid var(--line); border-radius: var(--radius); padding: 8px; }
.compose-att { display: flex; justify-content: space-between; align-items: center; padding: 8px; background: var(--page); border-radius: 4px; margin-bottom: 4px; }

/* ====================================================================
   PGP Encryption Visuals
   ==================================================================== */

/* The tag injected by app.js into the reader's subject line */
.enc-tag {
  display: inline-flex;
  align-items: center;
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 500;
  color: var(--enc);
  border: 1px solid var(--enc);
  background: var(--enc-bg);
  border-radius: 4px;
  padding: 3px 8px;
  margin-left: 12px;
  vertical-align: middle;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* Force the lock icon to have a fixed width so it doesn't push text */
.msg .lock {
  display: inline-block;
  width: 16px; 
  color: var(--enc);
  font-size: 11px;
}

/* Applies a solid purple left border to any message row in the inbox 
  that contains the lock icon, instantly highlighting at-rest encrypted mail.
*/
.msg:has(.lock) {
  border-left: 3px solid var(--enc);
  /* Adjust padding from 16px to 13px to account for the 3px border */
  padding-left: 13px; 
}

/* Emphasize the locked vault button in the sidebar */
#btn-pgp-vault.status-locked {
  color: var(--dim);
}

/* Emphasize the unlocked vault button in the sidebar */
#btn-pgp-vault.status-unlocked {
  color: var(--enc);
  background: var(--enc-bg);
  border-right: 3px solid var(--enc);
  border-radius: 0;
}
/* Reduce the harshness of the white HTML email background in night mode */
html.theme-dark .reader-body iframe {
  background: #e2e8f0; /* Sets a softer grayish-white base */
  filter: brightness(0.85) contrast(1.05); /* Dims the glare while preserving text legibility */
  opacity: 0.95; 
}
/* Acenet, Inc. Branding Updates */
.brand {
  display: flex;
  align-items: center;
  /* Ensure the container has a minimum width based on the logo's aspect ratio */
  min-width: 106px; 
}

.brand-logo {
  height: 28px;         /* Use explicit height rather than max-height */
  width: auto;
  object-fit: contain;
  flex-shrink: 0;       /* Strictly forbids Flexbox from squashing the image to 0px */
  display: block;
}

/* Brighten the logo specifically for dark mode if it contains dark text */
html.theme-dark .brand-logo {
  filter: brightness(0) invert(1); 
}
/* ====================================================================
   Login Screen Branding
   ==================================================================== */

.login-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 4px;
}

.login-brand {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Clear the default margin since the gap property handles spacing now */
.login-card h1 { 
  margin-bottom: 0; 
}

.login-logo {
  height: 36px; /* Slightly larger presence for the login screen */
  width: auto;
  object-fit: contain;
  flex-shrink: 0;
  display: block;
}

/* Invert the logo in dark mode just like the topbar */
html.theme-dark .login-logo {
  filter: brightness(0) invert(1); 
}
/* =========================================================
   DESKTOP / WIDESCREEN LAYOUT
   ========================================================= */
@media (min-width: 769px) {
  .main {
    display: grid;
    grid-template-columns: 240px 360px 1fr; 
  }
  
  .mobile-back-btn { display: none !important; }
  
  /* Ensure sliding classes don't break desktop if resized */
  .sidebar, .list, .reader {
    transform: none !important;
    z-index: auto;
  }
}

/* =========================================================
   MOBILE / SMARTPHONE LAYOUT
   ========================================================= */
@media (max-width: 768px) {
  .main {
    position: relative;
    display: block; /* Kills the CSS grid */
    width: 100%;
    height: 100%;
    overflow: hidden; 
  }

  /* Target the actual layout columns */
  .sidebar,
  .list,
  .reader {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--bg, #ffffff);
    transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    z-index: 10;
  }

  .panel-hidden {
    transform: translateX(100%);
    pointer-events: none; 
  }

  .panel-active {
    transform: translateX(0);
    z-index: 20; 
  }
/* -------------------------------------------
     Mobile Top Bar Restructuring
     ------------------------------------------- */
  .topbar {
    flex-wrap: wrap; /* Allows items to drop to a second row */
    padding: 8px 12px; 
    gap: 8px;
  }
  
  /* Force the dropdown to its own full-width row at the bottom of the topbar */
  #accountSelect {
    order: 10; 
    width: 100%; 
    margin-top: 4px;
  }

  /* Hide the "X keys loaded" text to save precious horizontal space */
  #keyState {
    display: none;
  }
}
