/* Every colour is a token, so the dark palette at the foot of this file needs
   only to re-point them — no rule is written twice. */
/* One declaration per token: light-dark() picks a side from the resolved
   color-scheme, so the theme toggle only has to change `color-scheme` and no
   rule below ever needs a dark twin. */
:root {
  color-scheme: light dark;

  --bg:          light-dark(#f6f8fc, #16181c);
  --surface:     light-dark(#ffffff, #202124);
  --fg:          light-dark(#202124, #e8eaed);
  --muted:       light-dark(#5f6368, #9aa0a6);
  --border:      light-dark(#dadce0, #3c4043);
  --divider:     light-dark(#ebedf0, #2c2e33);
  /* light #1a73e8 fell to 3.93:1 against --accent-soft, which is the compose
     pill and the active folder; both sides here clear AA on every surface. */
  --accent:      light-dark(#0b57d0, #a8c7fa);
  --accent-soft: light-dark(#e8f0fe, #22364f);
  --on-accent:   light-dark(#ffffff, #062e6f);
  --hover:       light-dark(#f1f3f4, #2a2c31);
  --warn:        light-dark(#c5221f, #f28b82);
  --on-warn:     light-dark(#ffffff, #3c0d0a);
  --ok-bg:       light-dark(#e6f4ea, #1d3826);
  --ok-fg:       light-dark(#137333, #81c995);
  --alert-bg:    light-dark(#feefc3, #3d3520);
  --alert-fg:    light-dark(#7a4f01, #fdd663);
  --toast-bg:    light-dark(#202124, #e8eaed);
  --toast-fg:    light-dark(#e8eaed, #202124);
  --backdrop:    light-dark(rgba(32, 33, 36, 0.3), rgba(0, 0, 0, 0.6));
  --shadow-near: light-dark(rgba(60, 64, 67, 0.2), rgba(0, 0, 0, 0.55));
  --shadow-far:  light-dark(rgba(60, 64, 67, 0.08), rgba(0, 0, 0, 0.35));

  --ui: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --radius: 8px;
  --shadow: 0 1px 3px var(--shadow-near), 0 4px 8px 3px var(--shadow-far);
}

/* The toggle pins a side; without the attribute the system decides. */
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;  /* mobile browser chrome eats the last row of 100vh */
  font: 14px/1.5 var(--ui);
  color: var(--fg);
  background: var(--surface);
}

button {
  font: inherit;
  border: 0;
  border-radius: var(--radius);
  background: none;
  color: var(--accent);
  padding: 6px 14px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { background: var(--hover); }
button:disabled { color: var(--muted); cursor: default; }
button:disabled:hover { background: none; }

/* Line icons from the sprite in index.html; they take their colour from the
   button, so no icon needs a palette of its own. */
.sprite { display: none; }
.icon {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-only { padding: 8px; display: inline-flex; }

input, textarea {
  font: inherit;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--fg);
}

/* Keyboard focus was invisible: buttons carry no border and the input ring used
   --accent-soft, which is near-white on white. */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input:focus, textarea:focus { outline: 2px solid var(--accent); border-color: var(--accent); }

/* --- top bar --------------------------------------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}
.brand { font-size: 18px; font-weight: 500; letter-spacing: -0.02em; }
.search {
  flex: 1;
  min-width: 0;  /* an input's intrinsic size is its flex floor otherwise, and it overflows narrow bars */
  max-width: 640px;
  margin: 0 auto;
  background: var(--hover);
  border: 0;
  border-radius: var(--radius);
  padding: 8px 16px;
}
.search:focus { background: var(--surface); outline: 2px solid var(--accent); }
.top-actions { display: flex; align-items: center; gap: 4px; }
.top-actions .icon-only { color: var(--muted); }
.top-actions .icon-only:hover { color: var(--fg); }
.account { font-size: 13px; color: var(--muted); margin-right: 4px; }
.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 15px;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
}

/* --- layout ------------------------------------------------------------------ */

main { flex: 1; min-height: 0; display: flex; background: var(--bg); }

.rail {
  width: 200px;
  padding: 12px 8px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
/* The one primary action here, so it carries a solid fill rather than the soft
   one it shares with selected rows — and its box edge lines up with the folder
   buttons below, so the rail reads as a single column. */
.compose {
  align-self: stretch;
  display: flex;
  align-items: center;
  gap: 14px;
  margin: 2px 0 18px;
  padding: 12px 16px;
  border-radius: 20px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 500;
}
.compose:hover { background: var(--accent); box-shadow: var(--shadow); }

#folders button {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  text-align: left;
  padding: 9px 16px;
  color: var(--fg);
  font-weight: 500;
}
#folders .label { flex: 1; }
#folders button:hover { background: var(--hover); }
#folders button.active { background: var(--accent-soft); color: var(--accent); }
#folders .count { color: var(--accent); font-weight: 500; margin-left: 8px; }

.listpane {
  display: flex;
  flex-direction: column;
  min-width: 360px;
  max-width: 460px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.list { flex: 1; min-height: 0; overflow-y: auto; }

/* Fetch acts on the mailbox, so it sits over the mailbox rather than in the
   account cluster — and it can stand down in the contacts folder. */
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px 6px 12px;
  border-bottom: 1px solid var(--border);
}
.toolbar .tools { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.toolbar .primary { display: flex; align-items: center; gap: 8px; color: var(--accent); font-weight: 500; }
#import-form input {
  flex: 1;
  min-width: 0;  /* an input's intrinsic width is its flex floor otherwise */
  font-family: var(--mono);
  font-size: 12px;
  padding: 5px 10px;
}
/* The fetch icon turns while a session is open. */
.toolbar .primary:disabled .icon { animation: spin 1.1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .toolbar .primary:disabled .icon { animation: none; } }
.list-status { margin-left: auto; padding-right: 6px; font-size: 12px; color: var(--muted); }

/* Message rows are <button>, so they are reachable and openable from the
   keyboard; the generic button styling above is undone here. */
.item {
  display: flex;
  gap: 12px;
  align-items: baseline;
  width: 100%;
  padding: 9px 16px;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--divider);
  border-radius: 0;
  background: var(--surface);
  cursor: pointer;
  color: var(--muted);
}
.item:hover { background: var(--hover); }
.item.selected { background: var(--accent-soft); }
.item.unread { color: var(--fg); }
.item .who { width: 150px; flex: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .mid { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .subject { font-weight: 400; }
.item.unread .who { font-weight: 700; }
.item.unread .subject { font-weight: 700; }
.item .snippet { color: var(--muted); }
.item.unread .snippet { color: var(--fg); }
.item .when { flex: none; font-size: 12px; color: var(--muted); }
.item button { padding: 2px 12px; font-size: 12px; }
.item.contact .mid { white-space: normal; line-height: 1.45; font-family: var(--mono); font-size: 12px; color: var(--muted); }
.item.contact .badge + .badge { margin-left: 6px; }
/* A sender whose key is bound to no address, visible before opening the row. */
.item .dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-left: 6px;
  border-radius: 50%;
  background: var(--alert-fg);
}
.badge {
  display: inline-block;
  margin-top: 3px;
  font-family: var(--ui);
  font-size: 11px;
  padding: 1px 9px;
  border-radius: 10px;
  background: var(--hover);
  color: var(--muted);
}
.badge.ok { background: var(--ok-bg); color: var(--ok-fg); }
.badge.alert { background: var(--alert-bg); color: var(--alert-fg); }

.view { flex: 1; min-width: 0; overflow-y: auto; padding: 24px 28px; }
.view h2 { margin: 0 0 10px; font-size: 20px; font-weight: 400; }
.view .meta { font-size: 13px; color: var(--muted); margin-bottom: 14px; white-space: pre-line; word-break: break-word; }
.view .fields { font-family: var(--mono); font-size: 12px; color: var(--muted); margin-bottom: 10px; white-space: pre-line; }
.view pre {
  white-space: pre-wrap;
  overflow-wrap: anywhere;  /* an unbroken URL must not widen the reader */
  font-family: inherit;
  margin: 14px 0 0;
  padding: 14px 0 0;
  border-top: 1px solid var(--border);
}
.view .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }
.view .actions button { display: flex; align-items: center; gap: 8px; border: 1px solid var(--border); box-shadow: var(--shadow); color: var(--fg); }
.view .actions button:hover { background: var(--hover); color: var(--fg); }
/* Only meaningful once the reader covers the list, below the breakpoint. */
.back { display: none; align-items: center; gap: 6px; margin: 0 0 10px -8px; }

/* What is actually known about the signer: a signature always verifies here, so
   the chip reports the key-to-address binding, which is the part that can be weak. */
.trust { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.trust .keyblock { font-family: var(--mono); font-size: 12px; line-height: 1.55; color: var(--muted); }
.trust .keyblock .full { word-break: break-all; }

/* Superseded contact keys: the local record that a rotation happened. */
.history { margin-top: 22px; border-top: 1px solid var(--border); padding-top: 14px; }
.history h3 {
  margin: 0 0 4px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.history .keyblock { margin-top: 12px; font-family: var(--mono); font-size: 12px; color: var(--muted); line-height: 1.55; }
.history .keyblock .full { word-break: break-all; }
.history .replaced { font-family: var(--ui); font-style: italic; }

.warn { color: var(--warn); }
.placeholder { color: var(--muted); font-style: italic; padding: 12px 16px; }

.name-sender { display: flex; gap: 8px; margin: 12px 0 0; }
.name-sender input { flex: 1; }

/* --- setup -------------------------------------------------------------------- */

.setup {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  text-align: center;
  background: var(--bg);
  padding: 16px;
}
.setup h2 { margin: 0; font-weight: 400; }
.setup p { color: var(--muted); max-width: 480px; }
.setup .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: center; }
.setup .row.restore-row { align-items: flex-end; }
.setup label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 150px;
  font-size: 12px;
  color: var(--muted);
  text-align: left;
}
.setup label input { font-family: var(--mono); font-size: 13px; }

/* --- dialogs --------------------------------------------------------------------- */

dialog {
  border: 0;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 24px;
  color: var(--fg);
  background: var(--surface);
  min-width: min(460px, calc(100vw - 32px));
  max-width: min(640px, calc(100vw - 32px));
  max-height: calc(100dvh - 48px);
  overflow-y: auto;
}
dialog::backdrop { background: var(--backdrop); }
#compose form { display: flex; flex-direction: column; gap: 10px; }
#compose input, #compose textarea { width: 100%; }
#compose textarea { font-family: inherit; resize: vertical; }
#compose footer { display: flex; gap: 8px; align-items: center; justify-content: flex-end; padding-top: 4px; flex-wrap: wrap; }
#compose .anon { font-size: 13px; color: var(--muted); margin-right: auto; display: flex; align-items: center; gap: 6px; white-space: nowrap; }
/* Without this every action read exactly like "cancel" and "close". */
.filled { background: var(--accent); color: var(--on-accent); font-weight: 500; padding: 8px 20px; }
.filled:hover { background: var(--accent); box-shadow: var(--shadow); }
.filled:disabled { background: var(--hover); color: var(--muted); box-shadow: none; }
/* Irreversible, not just consequential — rotate stays plain-text, this doesn't. */
.danger { background: var(--warn); color: var(--on-warn); font-weight: 500; padding: 8px 20px; }
.danger:hover { background: var(--warn); box-shadow: var(--shadow); }
.error { color: var(--warn); font-size: 13px; }

#settings section { margin-bottom: 26px; }
#settings h3 {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 6px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
/* The numbers say what order these have to happen in — registering against an
   unpinned server is refused, so the server section cannot come second. */
#settings .step {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  flex: none;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
}
#settings .step.done { background: var(--ok-bg); color: var(--ok-fg); }
#settings form { display: flex; gap: 10px; margin: 10px 0 6px; flex-wrap: wrap; align-items: flex-end; }
#settings label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
  min-width: 170px;
  font-size: 12px;
  color: var(--muted);
}
#settings label input { font-family: var(--mono); font-size: 13px; }
#settings footer { display: flex; justify-content: flex-end; }
.hint { font-size: 12px; color: var(--muted); margin: 4px 0; }

/* label / value rows, in place of the monospace `key: value` dump */
.field { display: flex; align-items: baseline; gap: 12px; padding: 3px 0; }
.field-label { flex: none; width: 96px; font-size: 12px; color: var(--muted); }
.field-value { flex: 1; min-width: 0; font-family: var(--mono); font-size: 12px; word-break: break-all; }
.field-value.empty { font-family: inherit; font-style: italic; color: var(--muted); }

.seed-card { border: 1px solid var(--warn); border-radius: var(--radius); padding: 12px 14px; margin: 14px 0 10px; }
.seed-card h4 {
  margin: 0 0 8px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--warn);
}
.seed-card .row { display: flex; align-items: center; gap: 8px; }
.backup-row { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.seed-card .seed { flex: 1; min-width: 0; font-family: var(--mono); font-size: 12px; word-break: break-all; }
.seed-card p { margin: 10px 0 0; font-size: 12px; color: var(--muted); }

#settings details { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 10px; }
#settings summary { font-size: 12px; color: var(--muted); cursor: pointer; }
#settings details .hint { margin: 8px 0; }

.kv { list-style: none; padding: 0; margin: 6px 0; font-size: 13px; }
.kv li { display: flex; gap: 10px; padding: 3px 0; align-items: baseline; }
.kv .key { font-family: var(--mono); word-break: break-all; color: var(--muted); flex: 1; }

.copy { font-size: 12px; padding: 4px 10px; }

/* --- toast ------------------------------------------------------------------------ */

.toast {
  position: fixed;
  left: 24px;
  bottom: 24px;
  max-width: 520px;
  margin: 0;
  padding: 12px 18px;
  border-radius: var(--radius);
  background: var(--toast-bg);
  color: var(--toast-fg);
  font-size: 13px;
  white-space: pre-wrap;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.toast.warn { background: var(--warn); color: var(--on-warn); }
.toast[hidden] { display: none; }

/* --- narrow layouts ----------------------------------------------------------------- */

/* The reader takes the whole pane and the list steps aside, rather than both
   being squeezed below the width either one needs. */
@media (max-width: 1000px) {
  .listpane { flex: 1; min-width: 0; max-width: none; border-right: 0; }
  .view { display: none; }
  #app.reading .listpane { display: none; }
  #app.reading .view { display: block; }
  .back { display: inline-flex; }
}

@media (max-width: 640px) {
  main { flex-direction: column; }
  .rail {
    width: auto;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    padding: 8px;
    overflow-x: auto;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
  .compose { margin: 0; padding: 8px 20px; }
  .compose { align-self: center; }
  #folders { display: flex; gap: 4px; }
  #folders button { width: auto; gap: 8px; padding: 9px 12px; }
  #folders .label { display: none; }  /* aria-label keeps the accessible name */
  .topbar { gap: 8px; padding: 8px 12px; }
  .search { max-width: none; padding: 6px 12px; }
  .account { display: none; }
  .item .who { width: auto; flex: 0 1 110px; }
  .view { padding: 16px; }
  .toast { left: 12px; right: 12px; bottom: 12px; max-width: none; }
}
