:root {
  --bg: #f6f8fc;
  --fg: #202124;
  --muted: #5f6368;
  --line: #e8eaed;
  --line-strong: #dadce0;
  --accent: #1a73e8;
  --accent-hover: #1765cc;
  --danger: #d93025;
  --success: #137333;
  --card: #ffffff;
  --unread: #202124;
  --read: #5f6368;
  --hover: #f1f3f4;
  --row-unread: #ffffff;
  --row-read: #f6f8fc;
  --shadow-sm: 0 1px 2px 0 rgba(60,64,67,.10), 0 1px 3px 1px rgba(60,64,67,.05);
  --shadow-md: 0 1px 3px 0 rgba(60,64,67,.15), 0 4px 8px 3px rgba(60,64,67,.05);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1f2023;
    --fg: #e8eaed;
    --muted: #9aa0a6;
    --line: #303134;
    --line-strong: #3c4043;
    --accent: #8ab4f8;
    --accent-hover: #aecbfa;
    --card: #292a2d;
    --unread: #e8eaed;
    --read: #9aa0a6;
    --hover: #35363a;
    --row-unread: #292a2d;
    --row-read: #1f2023;
    --shadow-sm: 0 1px 2px 0 rgba(0,0,0,.30), 0 1px 3px 1px rgba(0,0,0,.15);
    --shadow-md: 0 1px 3px 0 rgba(0,0,0,.40), 0 4px 8px 3px rgba(0,0,0,.20);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Google Sans", "Inter", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.hidden { display: none !important; }

/* Login */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.login-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 40px 36px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-md);
}
.login-card h1 { margin: 0 0 8px; font-size: 22px; font-weight: 500; }
.login-card p { margin: 0 0 28px; color: var(--muted); }
.login-card label { display: block; margin-bottom: 16px; font-size: 13px; color: var(--muted); }
.login-card input {
  display: block; width: 100%; padding: 12px 14px; margin-top: 6px;
  font-size: 15px; border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--card); color: var(--fg);
  transition: border-color .15s, box-shadow .15s;
}
.login-card input:focus {
  outline: 0; border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(26,115,232,.15);
}
.login-card button {
  width: 100%; padding: 12px; background: var(--accent); color: white;
  border: 0; border-radius: 24px; font-size: 14px; font-weight: 500; cursor: pointer;
  transition: background .15s, box-shadow .15s;
}
.login-card button:hover { background: var(--accent-hover); box-shadow: var(--shadow-sm); }
.alert {
  background: #fce8e6; color: var(--danger);
  border: 1px solid #f5c6c2; padding: 10px 14px;
  border-radius: 8px; margin-bottom: 16px; font-size: 13px;
}
@media (prefers-color-scheme: dark) {
  .alert { background: #2d1716; border-color: #5c1f1d; color: #f6aea9; }
}

/* Topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 24px; height: 56px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.topbar .brand a {
  display: inline-flex; align-items: center; gap: 8px;
  color: var(--fg); font-weight: 500; font-size: 18px;
}
.topbar .brand a:hover { text-decoration: none; }
.logo {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--accent); color: white; font-size: 16px;
}
.topbar .meta { display: flex; align-items: center; gap: 14px; }
.badge {
  background: var(--accent); color: white; padding: 3px 10px;
  border-radius: 999px; font-size: 11px; font-weight: 500;
  letter-spacing: .2px;
}
.badge.muted { background: var(--hover); color: var(--muted); }
.user-chip {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--accent); color: white;
  font-weight: 500; font-size: 14px;
  user-select: none;
}
.inline { display: inline; margin: 0; }
.link {
  background: none; border: 0; color: var(--accent);
  cursor: pointer; font-size: 13px; padding: 0;
}
.link.danger { color: var(--danger); }
.link:hover { text-decoration: underline; }
.link-tiny {
  background: none; border: 0; color: var(--muted);
  font-size: 12px; font-weight: 500; padding: 4px 6px;
  cursor: pointer; border-radius: 4px;
  text-transform: uppercase; letter-spacing: .5px;
}
.link-tiny:hover { background: var(--hover); color: var(--fg); }

/* Compose button (in topbar) */
.btn-compose {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--card); color: var(--fg);
  border: 1px solid var(--line-strong); border-radius: 24px;
  padding: 8px 18px 8px 14px; font-weight: 500; font-size: 13px;
  box-shadow: var(--shadow-sm);
  cursor: pointer; transition: background .15s, box-shadow .15s;
}
.btn-compose:hover {
  background: var(--hover); text-decoration: none;
  box-shadow: var(--shadow-md);
}
.btn-compose .plus {
  display: inline-flex; align-items: center; justify-content: center;
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent); color: white; font-size: 14px; line-height: 1;
}

/* List */
.list-wrap { max-width: 1100px; margin: 0 auto; padding: 12px; }
.empty {
  padding: 80px 20px; text-align: center;
  background: var(--card); border-radius: var(--radius);
  border: 1px solid var(--line);
}
.empty code { background: var(--hover); padding: 2px 6px; border-radius: 4px; font-size: 13px; }
.email-list {
  list-style: none; margin: 0; padding: 0;
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.email-row {
  border-bottom: 1px solid var(--line);
  background: var(--row-read);
  transition: background .1s, box-shadow .1s;
}
.email-row.unread { background: var(--row-unread); }
.email-row:last-child { border-bottom: 0; }
.email-row:hover {
  background: var(--hover);
  box-shadow: inset 0 0 0 1px var(--line-strong);
  z-index: 1; position: relative;
}
.row-link {
  display: grid;
  grid-template-columns: 220px 1fr 120px;
  gap: 16px;
  padding: 12px 20px;
  color: var(--read);
  align-items: baseline;
}
.email-row.unread .row-link { color: var(--unread); }
.email-row.unread .row-from,
.email-row.unread .row-subject strong { font-weight: 600; }
.row-from { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 400; }
.row-subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-subject strong { font-weight: 400; }
.row-preview {
  grid-column: 1 / -1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 13px; padding-left: 236px; margin-top: -4px;
  color: var(--muted);
}
.row-date { text-align: right; white-space: nowrap; font-size: 12px; color: var(--muted); }
.email-row.unread .row-date { color: var(--fg); font-weight: 500; }
.dot {
  display: inline-block; width: 8px; height: 8px;
  background: var(--accent); border-radius: 50%; margin-right: 8px;
  vertical-align: middle;
}
.paperclip { margin-left: 8px; font-size: 12px; color: var(--muted); }
.pager {
  display: flex; justify-content: space-between; align-items: center;
  padding: 20px 4px; gap: 12px;
}

/* Email view */
.email-wrap { max-width: 920px; margin: 0 auto; padding: 24px 16px; }
.subject { font-size: 22px; font-weight: 400; margin: 0 0 24px; line-height: 1.3; }
.headers {
  width: 100%; border-collapse: collapse; margin-bottom: 20px;
  font-size: 13px; background: var(--card);
  border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.headers th, .headers td {
  text-align: left; padding: 10px 14px;
  border-bottom: 1px solid var(--line); vertical-align: top;
}
.headers tr:last-child th, .headers tr:last-child td { border-bottom: 0; }
.headers th { width: 110px; color: var(--muted); font-weight: 500; }

.attachments {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 18px; margin-bottom: 20px;
  box-shadow: var(--shadow-sm);
}
.attachments h3 { margin: 0 0 8px; font-size: 13px; color: var(--muted); font-weight: 500; }
.attachments ul { list-style: none; margin: 0; padding: 0; }
.attachments li { padding: 4px 0; }

.body {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.tabs {
  display: flex; gap: 0; border-bottom: 1px solid var(--line);
  background: var(--card);
}
.tab {
  background: none; border: 0; padding: 12px 18px;
  font: inherit; cursor: pointer; color: var(--muted);
  border-bottom: 2px solid transparent;
  font-weight: 500; font-size: 13px;
  transition: color .15s, border-color .15s;
}
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.tab:hover { color: var(--fg); }
.tab.active:hover { color: var(--accent); }
.tab-pane { display: none; padding: 20px; margin: 0; }
.tab-pane.active { display: block; }
pre.tab-pane {
  white-space: pre-wrap; word-break: break-word;
  font: 13px/1.6 ui-monospace, "SF Mono", Menlo, monospace;
}
#tab-html iframe {
  width: 100%; min-height: 500px; border: 0;
  background: white; border-radius: 4px;
}

/* Generic buttons */
.btn-primary, .btn-secondary {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 16px; border-radius: 20px;
  font-size: 13px; font-weight: 500;
  border: 1px solid transparent; cursor: pointer;
  text-decoration: none; line-height: 1.2;
  transition: background .15s, box-shadow .15s, border-color .15s;
}
.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hover); text-decoration: none; box-shadow: var(--shadow-sm); }
.btn-secondary { background: var(--card); color: var(--fg); border-color: var(--line-strong); }
.btn-secondary:hover { background: var(--hover); text-decoration: none; }
.btn-primary.small, .btn-secondary.small { padding: 5px 12px; font-size: 12px; }

/* ===== Compose page (Gmail-style) ===== */
.compose-page {
  display: flex; justify-content: center;
  padding: 32px 16px 80px;
}
.compose-card {
  width: 100%; max-width: 760px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  display: flex; flex-direction: column;
}
.compose-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px 12px 20px;
  background: #404040;
  color: #fff;
}
@media (prefers-color-scheme: dark) {
  .compose-header { background: #1f1f1f; }
}
.compose-title { font-size: 14px; font-weight: 500; letter-spacing: .2px; }
.compose-close {
  display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px; border-radius: 4px;
  color: #fff; font-size: 22px; line-height: 1;
  opacity: .85; transition: background .15s, opacity .15s;
}
.compose-close:hover {
  background: rgba(255,255,255,.12); text-decoration: none; opacity: 1;
}

.banner {
  display: flex; align-items: flex-start; gap: 12px;
  margin: 12px 16px 0;
  padding: 12px 14px; border-radius: 8px;
  font-size: 13px; line-height: 1.4;
  border: 1px solid transparent;
}
.banner.success {
  background: #e6f4ea; border-color: #ceead6; color: var(--success);
}
.banner.error {
  background: #fce8e6; border-color: #f5c6c2; color: var(--danger);
}
.banner .banner-icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; border-radius: 50%;
  background: currentColor; color: #fff;
  font-size: 12px; font-weight: 700; flex-shrink: 0;
}
.banner .muted { color: inherit; opacity: .75; margin-left: 4px; }
@media (prefers-color-scheme: dark) {
  .banner.success { background: #102a17; border-color: #1e5631; color: #81c995; }
  .banner.error { background: #2d1716; border-color: #5c1f1d; color: #f6aea9; }
}

.compose-form {
  display: flex; flex-direction: column;
  padding: 8px 4px 12px;
}
.field-row {
  display: flex; align-items: center;
  padding: 8px 20px;
  border-bottom: 1px solid var(--line);
  gap: 12px;
}
.field-row.hidden { display: none; }
.field-label {
  width: 56px; color: var(--muted); font-size: 13px;
  flex-shrink: 0;
}
.field-row input {
  flex: 1; min-width: 0;
  border: 0; outline: 0; background: transparent;
  color: var(--fg);
  font: inherit; padding: 6px 0;
}
.field-row input::placeholder { color: var(--muted); }
.field-actions {
  display: flex; gap: 4px; flex-shrink: 0;
}
.subject-input {
  font-size: 14px !important;
}

.body-row {
  padding: 12px 20px;
  flex: 1;
}
.body-row textarea {
  width: 100%;
  min-height: 320px;
  border: 0; outline: 0; background: transparent;
  color: var(--fg);
  font: 14px/1.6 -apple-system, BlinkMacSystemFont, "Inter", system-ui, sans-serif;
  resize: vertical;
  padding: 0;
}
.body-row textarea::placeholder { color: var(--muted); }

.compose-actions {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px 16px;
  border-top: 1px solid var(--line);
}
.btn-send {
  display: inline-flex; align-items: center; gap: 10px;
  background: var(--accent); color: #fff;
  border: 0; border-radius: 24px;
  padding: 10px 24px;
  font: inherit; font-weight: 500; font-size: 14px;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: background .15s, box-shadow .15s;
}
.btn-send:hover { background: var(--accent-hover); box-shadow: var(--shadow-md); }
.btn-send:disabled { opacity: .6; cursor: progress; }
.btn-send-icon {
  font-size: 12px; transform: translateY(-1px);
}
.btn-icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 50%;
  color: var(--muted); font-size: 16px;
  margin-left: auto;
  transition: background .15s, color .15s;
}
.btn-icon:hover { background: var(--hover); color: var(--fg); text-decoration: none; }

/* Responsive */
@media (max-width: 720px) {
  .row-link { grid-template-columns: 140px 1fr 80px; gap: 8px; padding: 12px 14px; }
  .row-preview { padding-left: 0; grid-column: 1 / -1; }
  .topbar { padding: 8px 14px; }
  .field-label { width: 40px; }
  .compose-page { padding: 16px 8px 60px; }
}
