:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --line: #e5e9f0;
  --line-soft: #eef1f6;
  --text: #131a24;
  --text-soft: #5b6675;
  --text-mute: #808b9a;
  --accent: #1f6feb;
  --accent-soft: #eaf1fe;
  --accent-dark: #1859c4;
  --green: #0f7a52;
  --green-soft: #e6f5ee;
  --danger: #b4392f;
  --danger-soft: #fdecea;
  --radius: 13px;
  --shadow: 0 1px 2px rgba(19, 26, 36, 0.04), 0 8px 24px rgba(19, 26, 36, 0.05);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0; letter-spacing: -0.01em; }
p { margin: 0; }
button, input, textarea { font: inherit; color: inherit; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute; left: 16px; top: -48px; z-index: 20;
  background: var(--card); color: var(--accent); padding: 10px 14px;
  border-radius: 10px; text-decoration: none; box-shadow: var(--shadow);
  transition: top 0.15s ease;
}
.skip-link:focus { top: 12px; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 8px;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px clamp(14px, 3vw, 28px);
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  display: grid; place-items: center; width: 38px; height: 38px;
  border-radius: 11px; background: var(--accent-soft); color: var(--accent);
}
.brand-text h1 { font-size: 17px; font-weight: 650; }
.brand-text p { font-size: 12.5px; color: var(--text-mute); }
.mailbox { font-size: 13px; color: var(--text-mute); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-right { display: flex; align-items: center; gap: 10px; min-width: 0; }
/* Shown only when the API reports demo mode, so anyone looking at a public
   deployment can see at a glance that these are sample emails, not real mail. */
.demo-badge {
  flex: none; font-size: 11.5px; font-weight: 600; letter-spacing: 0.01em;
  padding: 3px 9px; border-radius: 999px; white-space: nowrap;
  background: var(--accent-soft); color: var(--accent); border: 1px solid #cfe0fd;
}

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(300px, 380px) 1fr;
  gap: 18px;
  padding: 18px clamp(14px, 3vw, 28px) 28px;
  align-items: start;
  max-width: 1440px; margin: 0 auto;
}
.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.inbox { display: flex; flex-direction: column; padding: 16px; gap: 12px; }
.reader { padding: clamp(16px, 2.2vw, 26px); min-height: 60vh; }

.panel-head { display: flex; align-items: center; justify-content: space-between; }
.panel-head h2 { font-size: 16px; font-weight: 650; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  border-radius: 10px; border: 1px solid transparent; padding: 9px 14px;
  font-size: 14px; font-weight: 550; cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.12s ease;
}
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not([disabled]) { background: var(--accent-dark); }
.btn-secondary { background: #fff; border-color: var(--line); color: var(--text); }
.btn-secondary:hover:not([disabled]) { background: #f7f9fc; border-color: #d7dde8; }
.btn-ghost { background: transparent; color: var(--text-soft); padding: 7px 10px; }
.btn-ghost:hover:not([disabled]) { background: #f4f6fa; color: var(--text); }
.btn-quiet-danger { background: transparent; color: var(--danger); }
.btn-quiet-danger:hover:not([disabled]) { background: var(--danger-soft); }

/* ---------- search ---------- */
.search { position: relative; }
.search-icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--text-mute); }
.search input {
  width: 100%; padding: 10px 12px 10px 34px;
  border: 1px solid var(--line); border-radius: 10px; background: #fbfcfe;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.search input:hover { border-color: #d7dde8; }
.search input:focus { background: #fff; }

/* ---------- mail list ---------- */
.mail-list {
  list-style: none; margin: 0; padding: 2px; display: flex; flex-direction: column; gap: 2px;
  max-height: min(62vh, 620px); overflow-y: auto;
}
.mail-list:focus-visible { outline-offset: 0; }
.mail-item {
  display: grid; grid-template-columns: 10px 1fr; gap: 9px;
  padding: 11px 12px; border-radius: 11px; cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.14s ease, border-color 0.14s ease;
}
.mail-item + .mail-item { box-shadow: inset 0 1px 0 var(--line-soft); }
.mail-item:hover { background: #f7f9fc; }
.mail-item.selected { background: var(--accent-soft); border-color: #cfe0fd; box-shadow: none; }
.mail-item .dot { width: 8px; height: 8px; border-radius: 50%; margin-top: 7px; background: transparent; }
.mail-item.unread .dot { background: var(--accent); }
.mail-item > span:last-child { display: block; min-width: 0; }
.mail-subject, .mail-preview { display: block; }
.mail-subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mail-row { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.mail-sender { font-weight: 650; font-size: 14px; }
.mail-item.unread .mail-sender { color: var(--text); }
.mail-time { font-size: 12px; color: var(--text-mute); white-space: nowrap; }
.mail-subject { font-size: 13.5px; color: var(--text); margin-top: 1px; }
.mail-preview {
  font-size: 12.5px; color: var(--text-mute); margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.list-foot { font-size: 12px; color: var(--text-mute); padding: 0 4px; }

/* ---------- states ---------- */
.state { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; padding: 12px 4px; }
.state-title { font-weight: 600; }
.state-line { font-size: 13.5px; color: var(--text-soft); }
.skeletons { width: 100%; display: flex; flex-direction: column; gap: 10px; margin-top: 4px; }
.sk-row {
  height: 54px; border-radius: 11px;
  background: linear-gradient(90deg, #f1f4f8 25%, #e8edf4 37%, #f1f4f8 63%);
  background-size: 400% 100%; animation: shimmer 1.4s ease-in-out infinite;
}
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }

.reader-empty { align-items: center; text-align: center; justify-content: center; min-height: 46vh; gap: 10px; }
.empty-mark { display: grid; place-items: center; width: 54px; height: 54px; border-radius: 16px; background: var(--accent-soft); color: var(--accent); }

/* ---------- reader ---------- */
.mail-head { padding-bottom: 12px; border-bottom: 1px solid var(--line-soft); }
.mail-head h3 { font-size: clamp(18px, 2.2vw, 22px); font-weight: 650; }
.mail-meta { font-size: 13px; color: var(--text-mute); margin-top: 4px; }
.mail-text {
  margin-top: 16px; padding: 16px; border: 1px solid var(--line); border-radius: 12px;
  background: #fbfcfe; white-space: pre-wrap; font-size: 14.5px; color: var(--text-soft);
  max-height: 32vh; overflow-y: auto;
}
.reply { margin-top: 22px; }
.reply-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.reply-head h4 { font-size: 15px; font-weight: 650; }
.pill {
  font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 999px;
  background: var(--green-soft); color: var(--green);
}
.writing { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--text-soft); margin-top: 10px; }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: pulse 1.1s ease-in-out infinite; }
.dots i:nth-child(2) { animation-delay: 0.15s; }
.dots i:nth-child(3) { animation-delay: 0.3s; }
@keyframes pulse { 0%, 100% { opacity: 0.25; } 50% { opacity: 1; } }

#replyText {
  width: 100%; margin-top: 12px; padding: 14px; border-radius: 12px;
  border: 1px solid var(--line); background: #fff; resize: vertical; min-height: 190px;
  font-size: 14.5px; line-height: 1.6; transition: border-color 0.15s ease;
}
#replyText:hover { border-color: #d7dde8; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }

/* A failed draft leaves the box empty, and the toast is gone in under three
   seconds. Say what happened, and keep saying it, next to the empty box. */
.reply-error {
  display: flex; gap: 8px; margin-top: 12px; padding: 10px 12px;
  border: 1px solid #f3cdc8; border-radius: 10px;
  background: var(--danger-soft); color: var(--danger); font-size: 13.5px;
}

/* Only meaningful once the panels stack; see the media queries below. */
.back-to-inbox { display: none; margin-bottom: 14px; }

/* ---------- dialog ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(19, 26, 36, 0.4);
  display: grid; place-items: center; padding: 20px; z-index: 30;
}
.dialog {
  background: #fff; border-radius: 14px; padding: 22px; max-width: 400px; width: 100%;
  box-shadow: 0 24px 60px rgba(19, 26, 36, 0.2);
}
.dialog h3 { font-size: 17px; font-weight: 650; }
.dialog p { font-size: 14px; color: var(--text-soft); margin-top: 8px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

/* ---------- toast ---------- */
.toast-wrap {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; z-index: 40; pointer-events: none;
}
.toast {
  background: #131a24; color: #fff; padding: 10px 16px; border-radius: 999px;
  font-size: 13.5px; box-shadow: 0 10px 30px rgba(19, 26, 36, 0.25);
  animation: rise 0.22s ease;
}
.toast.warn { background: var(--danger); }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- tablet landscape / small laptop ---------- */
@media (max-width: 1100px) {
  .layout { grid-template-columns: minmax(260px, 320px) 1fr; gap: 14px; }
}

/* ---------- stacked: phones and tablet portrait ---------- */
@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    padding-bottom: calc(24px + env(safe-area-inset-bottom));
  }
  /* Tall enough to browse, short enough that the reader below stays discoverable. */
  .mail-list { max-height: 52dvh; }
  .reader { min-height: auto; }
  .reader-empty { min-height: 30vh; }
  .mail-text { max-height: 40dvh; }
  .back-to-inbox { display: inline-flex; }

  /* iOS Safari zooms the page when a focused field is under 16px. */
  .search input,
  #replyText { font-size: 16px; }

  /* Comfortable thumb targets. */
  .btn { padding: 11px 16px; min-height: 44px; }
  .btn-ghost { padding: 9px 12px; }
  .mail-item { padding: 13px 12px; }
}

/* ---------- phones ---------- */
@media (max-width: 600px) {
  body { font-size: 14.5px; }
  .topbar { padding: 11px 14px; gap: 10px; }
  .brand-mark { width: 34px; height: 34px; }
  .brand-text h1 { font-size: 16px; white-space: nowrap; }
  /* The tagline is decorative and wraps to two lines once the badge is there.
     Which inbox this is matters more than the strapline on a small screen. */
  .brand-text p { display: none; }
  /* The address is a nicety; the brand must not be crushed by it. */
  .mailbox { font-size: 12px; max-width: 42vw; }
  .layout { padding: 14px 12px calc(20px + env(safe-area-inset-bottom)); gap: 12px; }
  .inbox { padding: 13px; }
  .reader { padding: 15px; }
  #replyText { min-height: 200px; }
  /* Full-width buttons read better than a ragged wrap on a narrow screen. */
  .actions { display: grid; grid-template-columns: 1fr 1fr; }
  .actions .btn { width: 100%; }
  .actions .btn:last-child { grid-column: 1 / -1; }
  .toast-wrap { left: 12px; right: 12px; bottom: calc(16px + env(safe-area-inset-bottom)); transform: none; align-items: center; }
  .toast { max-width: 100%; }
  .dialog-actions { flex-direction: column-reverse; }
  .dialog-actions .btn { width: 100%; }
}

/* ---------- very narrow (360px and below) ---------- */
@media (max-width: 380px) {
  /* The badge outranks the address here: knowing the inbox is sample data
     matters more than knowing which address it is. */
  .mailbox { display: none; }
  .demo-badge { font-size: 11px; padding: 3px 8px; }
  .actions { grid-template-columns: 1fr; }
  .actions .btn:last-child { grid-column: auto; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

[hidden] { display: none !important; }
