/* ---------------------------------------------------------------------
   Design tokens
   Concept: an airmail envelope — a plain paper ground, ink-dark text, and
   a red/blue "airmail stripe" spent in exactly one place (the header edge
   and small state accents), not stamped across every element.
   ------------------------------------------------------------------- */
:root {
  --bg: #f6f5f0;
  --surface: #ffffff;
  --surface-2: #f0efe9;
  --ink: #171a1f;
  --muted: #5c6470;
  --border: #ddd9cd;
  --border-strong: #c7c2b2;

  --red: #b8402a;
  --blue: #1e63e9;
  --blue-bright: #4facfe;
  --red-soft: #f4e2dd;
  --blue-soft: #dfe8f3;

  --danger: #b8402a;
  --danger-surface: #f9e9e5;
  --focus: #1e63e9;

  --ok: #157a4c;
  --ok-soft: #e2f3ea;

  --font-display: "Spectral", Georgia, "Times New Roman", serif;
  --font-body: "Public Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;

  --radius-sm: 6px;
  --radius-md: 10px;
  --shadow-panel: 0 12px 32px -16px rgba(23, 26, 31, 0.28);

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151b;
    --surface: #191d25;
    --surface-2: #1f232c;
    --ink: #edeef1;
    --muted: #9aa3b2;
    --border: #2b303a;
    --border-strong: #3a4049;

    --red: #e2765e;
    --blue: #6ea1ff;
    --blue-bright: #4facfe;
    --red-soft: #3a2622;
    --blue-soft: #1f2c3d;

    --danger: #e2765e;
    --danger-surface: #3a2622;
    --focus: #6ea1ff;

    --ok: #4cc48c;
    --ok-soft: #1d3529;

    --shadow-panel: 0 12px 32px -16px rgba(0, 0, 0, 0.6);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #12151b;
  --surface: #191d25;
  --surface-2: #1f232c;
  --ink: #edeef1;
  --muted: #9aa3b2;
  --border: #2b303a;
  --border-strong: #3a4049;

  --red: #e2765e;
  --blue: #6ea1ff;
  --blue-bright: #4facfe;
  --red-soft: #3a2622;
  --blue-soft: #1f2c3d;

  --danger: #e2765e;
  --danger-surface: #3a2622;
  --focus: #6ea1ff;

  --ok: #4cc48c;
  --ok-soft: #1d3529;

  --shadow-panel: 0 12px 32px -16px rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

/* ---------------------------------------------------------------------
   Reset & base
   ------------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
html {
  -webkit-text-size-adjust: 100%;
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

main#main {
  flex: 1 0 auto;
}

h1, h2, h3 { font-family: var(--font-display); font-weight: 600; margin: 0; }
p { margin: 0; }
ul { list-style: none; margin: 0; padding: 0; }
button { font-family: inherit; }

/* The [hidden] attribute must always win over any component display value —
   without this, elements whose class sets display (e.g. .skeleton's
   inline-block) stay visible after JS sets hidden=true (the "skeleton never
   goes away" bug). */
[hidden] { display: none !important; }

a { color: var(--blue); }

.wrap {
  max-width: 640px;
  margin: 0 auto;
  padding-inline: 20px;
}

/* Desktop: the extra room goes to a wider content column with the mailbox
   card and inbox side by side (see the .layout media query further down). */
@media (min-width: 960px) {
  .wrap { max-width: 1100px; }
}

.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: 12px; top: -48px;
  background: var(--ink);
  color: var(--bg);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  transition: top 0.15s ease;
  z-index: 100;
}
.skip-link:focus { top: 12px; }

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

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------------------------------------------------------------------
   Header
   ------------------------------------------------------------------- */
.site-header { border-bottom: 1px solid var(--border); background: var(--surface); }

.site-header__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-block: 14px;
}

.wordmark {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

.wordmark__logo {
  width: 150px;
  height: auto;
  display: block;
  object-fit: contain;
}

.wordmark__logo--dark {
  display: none;
}

/* Explicit dark mode */
:root[data-theme="dark"] .wordmark__logo--light {
  display: none;
}

:root[data-theme="dark"] .wordmark__logo--dark {
  display: block;
}

/* System dark mode */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .wordmark__logo--light {
    display: none;
  }

  :root:not([data-theme="light"]) .wordmark__logo--dark {
    display: block;
  }
}

.theme-switch {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
  background: var(--surface-2);
}
.theme-switch__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px; height: 30px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.theme-switch__btn svg {
  width: 16px; height: 16px;
  fill: none; stroke: currentColor; stroke-width: 1.7;
  stroke-linecap: round; stroke-linejoin: round;
}
.theme-switch__btn[aria-pressed="true"] {
  background: var(--surface);
  color: var(--blue);
  box-shadow: 0 1px 2px rgba(0,0,0,0.12);
}
.theme-switch__btn:hover { color: var(--ink); }

.header-actions {
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.header-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 999px;
  color: var(--muted);
  transition: color 0.12s ease, background-color 0.12s ease;
}
.header-link svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.header-link:hover { color: var(--blue); background: var(--surface-2); }

.airmail-stripe {
  height: 4px;
  background: repeating-linear-gradient(
    -45deg,
    var(--red) 0 14px,
    var(--surface) 14px 20px,
    var(--blue) 20px 34px,
    var(--surface) 34px 40px
  );
}

/* ---------------------------------------------------------------------
   Intro
   ------------------------------------------------------------------- */
.intro { padding-block: 40px 24px; }
.intro h1 { font-size: clamp(1.75rem, 1.4rem + 1.5vw, 2.25rem); letter-spacing: -0.01em; }
.intro__sub { margin-top: 8px; color: var(--muted); font-size: 1.02rem; max-width: 46ch; }

/* ---------------------------------------------------------------------
   Mailbox card
   ------------------------------------------------------------------- */
.mailbox-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 18px;
  margin-bottom: 32px;
}

.address-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
}

.address-row__value {
  flex: 1;
  min-width: 0;
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 1rem;
  word-break: break-all;
}

.skeleton {
  display: inline-block;
  width: 70%;
  height: 1.1em;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--border) 25%, var(--border-strong) 37%, var(--border) 63%);
  background-size: 400% 100%;
  animation: skeleton-loading 1.4s ease infinite;
}
@keyframes skeleton-loading {
  0% { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}

/* Belt-and-suspenders with the global [hidden] rule: JS also toggles this
   class when hiding the shimmer, so the skeleton is guaranteed to vanish
   once the real address renders (and only during generation). */
.skeleton.is-hidden { display: none; }

.mailbox-actions {
  display: flex;
  gap: 10px;
  margin-top: 12px;
  flex-wrap: wrap;
}

/* ---------------------------------------------------------------------
   Buttons
   ------------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  line-height: 1.2;
  transition: background-color 0.12s ease, border-color 0.12s ease, opacity 0.12s ease;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn--primary {
  background: var(--blue);
  color: #fff;
}
.btn--primary:hover:not(:disabled) { background: color-mix(in srgb, var(--blue) 88%, black); }

.btn--ghost {
  background: transparent;
  border-color: var(--border-strong);
  color: var(--ink);
}
.btn--ghost:hover:not(:disabled) { background: var(--surface-2); }

.btn--danger { color: var(--danger); border-color: var(--danger); }
.btn--danger:hover:not(:disabled) { background: var(--danger-surface); }

.btn--icon {
  padding: 8px;
  border-color: var(--border-strong);
  background: var(--surface);
  color: var(--ink);
}
.btn--icon svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.btn--icon:hover:not(:disabled) { background: var(--surface-2); }
.btn--icon.is-success { color: var(--blue); border-color: var(--blue); }

/* ---------------------------------------------------------------------
   Custom-name form
   ------------------------------------------------------------------- */
.custom-form { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 14px; }
.custom-form__row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.custom-form__field {
  flex: 1;
  min-width: 180px;
  display: flex;
  align-items: center;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  overflow: hidden;
}
.custom-form__field input {
  flex: 1;
  min-width: 0;
  border: 0;
  padding: 9px 10px;
  font-size: 0.95rem;
  font-family: var(--font-body);
  background: transparent;
  color: var(--ink);
}
.custom-form__field input:focus { outline: none; }
.custom-form__field:focus-within { border-color: var(--focus); }
.custom-form__domain {
  padding-right: 10px;
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
}
.custom-form__status {
  margin-top: 8px;
  font-size: 0.88rem;
  color: var(--muted);
  min-height: 1.2em;
}
.custom-form__status[data-state="ok"] { color: var(--blue); }
.custom-form__status[data-state="error"] { color: var(--danger); }

/* ---------------------------------------------------------------------
   Layout: two-column on desktop, stacked on mobile
   ------------------------------------------------------------------- */
.layout { display: block; }

@media (min-width: 960px) {
  .layout {
    display: grid;
    grid-template-columns: minmax(360px, 5fr) 7fr;
    gap: 28px;
    align-items: start;
  }
  .layout__mailbox .mailbox-card {
    position: sticky;
    top: 20px;
    margin-bottom: 0;
  }
  .layout__mailbox .inbox { margin-top: 0; }
  .intro { padding-block: 48px 32px; }
}

/* ---------------------------------------------------------------------
   Inbox
   ------------------------------------------------------------------- */
.inbox__head {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}
.inbox__head h2 { font-size: 1.2rem; }

/* "Live" chip: makes background polling visible. Pulse = healthy;
   amber = a poll is in flight; red = connection lost (reconnecting). */
.live-indicator {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  background: var(--surface);
  white-space: nowrap;
}
.live-indicator__dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--ok);
  animation: live-pulse 2.2s ease-in-out infinite;
}
.live-indicator[data-state="syncing"] .live-indicator__dot {
  background: var(--blue);
  animation-duration: 0.9s;
}
.live-indicator[data-state="stalled"] .live-indicator__dot {
  background: var(--danger);
  animation: none;
}
.live-indicator[data-state="stalled"] {
  color: var(--danger);
  border-color: var(--danger);
}
@keyframes live-pulse {
  0%, 100% { opacity: 1; box-shadow: 0 0 0 0 color-mix(in srgb, var(--ok) 45%, transparent); }
  50% { opacity: 0.55; box-shadow: 0 0 0 5px color-mix(in srgb, var(--ok) 0%, transparent); }
}
@media (prefers-reduced-motion: reduce) {
  .live-indicator__dot { animation: none; }
}

.message-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }

.message-item {
  width: 100%;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  cursor: pointer;
  color: inherit;
}
.message-item:hover { border-color: var(--border-strong); }
.message-item.is-unread { border-left: 3px solid var(--blue); }
.message-item.is-new { animation: message-arrive 0.7s ease; }
@keyframes message-arrive {
  0% { background: var(--blue-soft); transform: translateY(-3px); }
  100% { background: var(--surface); transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .message-item.is-new { animation: none; }
}

.message-item__row1 {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.86rem;
  color: var(--muted);
}
.message-item__sender { font-weight: 600; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-item__time { flex-shrink: 0; }

.message-item__row2 {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.95rem;
  overflow: hidden;
}
.message-item__subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-item__attachment-icon { flex-shrink: 0; width: 15px; height: 15px; fill: none; stroke: var(--muted); stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.empty-state {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-md);
  padding: 36px 20px;
  text-align: center;
}
.empty-state__title { font-weight: 600; }
.empty-state__sub { margin-top: 4px; color: var(--muted); font-size: 0.92rem; }

/* ---------------------------------------------------------------------
   Message view (dialog)
   ------------------------------------------------------------------- */
.message-view { position: fixed; inset: 0; z-index: 50; }
.message-view__backdrop {
  position: absolute; inset: 0;
  background: rgba(15, 17, 21, 0.42);
}
.message-view__panel {
  position: absolute;
  inset: 0;
  margin: 0;
  background: var(--surface);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

@media (min-width: 640px) {
  .message-view__panel {
    inset: 5vh 50% auto 50%;
    transform: translateX(-50%);
    width: min(600px, calc(100vw - 40px));
    max-height: 90vh;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-panel);
    border: 1px solid var(--border);
  }
}

.message-view__head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}

.message-view__meta { padding: 16px; border-bottom: 1px solid var(--border); }
.message-view__meta h2 { font-size: 1.25rem; word-break: break-word; }

.message-view__fields { margin-top: 12px; display: grid; gap: 6px; }
.message-view__fields > div { display: flex; gap: 8px; font-size: 0.88rem; }
.message-view__fields dt { color: var(--muted); min-width: 66px; flex-shrink: 0; }
.message-view__fields dd { margin: 0; word-break: break-word; }

/* The body takes every pixel the header/meta/attachments don't, and the
   frame inside it grows to match — otherwise the panel (which is
   `inset: 0`, i.e. full-screen on mobile) leaves a large empty gap under a
   short email. `min-height: 0` is what actually lets a flex child shrink
   and scroll instead of overflowing its parent. */
.message-view__body {
  padding: 16px;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.message-view__body iframe {
  width: 100%;
  flex: 1 1 auto;
  min-height: 260px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
}
.message-view__body pre {
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--font-body);
  margin: 0;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}
.message-view__load-images {
  margin-bottom: 10px;
}

.message-view__attachments {
  padding: 0 16px 20px;
  flex-shrink: 0;
}
.message-view__attachments h3 { font-size: 0.95rem; margin-bottom: 8px; }
.message-view__attachments ul { display: flex; flex-direction: column; gap: 6px; }
.message-view__attachments li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 0.9rem;
}
.attachment-info { display: flex; align-items: center; gap: 8px; min-width: 0; }
.attachment-info svg { flex-shrink: 0; width: 16px; height: 16px; fill: none; stroke: var(--muted); stroke-width: 1.7; }
.attachment-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-size { color: var(--muted); flex-shrink: 0; }

/* ---------------------------------------------------------------------
   Toast
   ------------------------------------------------------------------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 600;
  z-index: 60;
  box-shadow: var(--shadow-panel);
}

/* ---------------------------------------------------------------------
   Footer
   ------------------------------------------------------------------- */
.site-footer {
  border-top: 1px solid var(--border);
  background: var(--surface);
  margin-top: 56px;
  flex-shrink: 0;
}
.site-footer__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding-block: 18px;
  font-size: 0.88rem;
}
.site-footer__brand {
  font-family: var(--font-display);
  font-weight: 600;
  color: var(--muted);
}
.site-footer__links {
  display: inline-flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
}
.site-footer__links a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  text-decoration: none;
}
.site-footer__links a svg {
  width: 15px;
  height: 15px;
  fill: currentColor;
}
.site-footer__links a:hover { color: var(--blue); }

/* ---------------------------------------------------------------------
   Legal pages (Terms of Service)
   ------------------------------------------------------------------- */
.legal { padding-block: 36px 24px; }
.legal h1 { font-size: clamp(1.75rem, 1.4rem + 1.5vw, 2.25rem); }
.legal__updated { margin-top: 6px; color: var(--muted); font-size: 0.9rem; }
.legal h2 { font-size: 1.15rem; margin-top: 28px; }
.legal p, .legal li { margin-top: 10px; color: var(--ink); max-width: 70ch; }
.legal ul { list-style: disc; padding-left: 22px; }
.legal strong { font-weight: 600; }

/* ---------------------------------------------------------------------
   Small screens: keep generous tap targets, prevent overflow
   ------------------------------------------------------------------- */
@media (max-width: 380px) {
  .wrap { padding-inline: 14px; }
  .intro { padding-block: 28px 18px; }
}
