:root {
  --bg: #ffffff;
  --bg-sub: #f7f8f9;
  --bg-rail: #f2f3f5;
  --bg-hover: #ebedf0;
  --bg-sel: #e3ecfb;
  --border: #e2e5e9;
  --text: #16191d;
  --text-dim: #6b7280;
  --text-faint: #9aa1ab;
  --accent: #1f6feb;
  --accent-soft: #dbe8fd;
  --danger: #c0392b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 4px 16px rgba(0, 0, 0, .06);
  --radius: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --rail-w: 264px;
  --list-w: 400px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181c;
    --bg-sub: #1b1e23;
    --bg-rail: #101215;
    --bg-hover: #24282e;
    --bg-sel: #1d2f4d;
    --border: #2a2f36;
    --text: #e6e8eb;
    --text-dim: #9aa1ab;
    --text-faint: #6b7280;
    --accent: #4b96ff;
    --accent-soft: #1e3358;
    --danger: #ef6a5c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font: 14px/1.5 var(--font);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
}

/* ------------------------------------------------------------------ login */

#login {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--bg-rail);
  z-index: 50;
}

#login form {
  width: min(360px, calc(100vw - 40px));
  padding: 32px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

#login h1 {
  margin: 0 0 4px;
  font-size: 18px;
  letter-spacing: -.01em;
}

#login p {
  margin: 0 0 24px;
  color: var(--text-dim);
  font-size: 13px;
}

#login input {
  width: 100%;
  padding: 10px 12px;
  margin-bottom: 10px;
  font: inherit;
  color: var(--text);
  background: var(--bg-sub);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

#login input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

#login button {
  width: 100%;
  padding: 10px;
  margin-top: 6px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border-radius: var(--radius);
}

#login button:disabled { opacity: .6; cursor: default; }

#login-error {
  min-height: 18px;
  margin-top: 10px;
  color: var(--danger);
  font-size: 13px;
  text-align: center;
}

/* ----------------------------------------------------------------- shell */

#shell {
  display: grid;
  grid-template-rows: 52px minmax(0, 1fr);
  height: 100%;
}

#shell[hidden] { display: none; }

#topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  background: var(--bg-rail);
  border-bottom: 1px solid var(--border);
}

#topbar .brand strong { font-size: 14px; letter-spacing: -.01em; }

.tabs {
  display: flex;
  gap: 2px;
  margin-left: 18px;
  padding: 3px;
  background: var(--bg-hover);
  border-radius: 8px;
}

.tab {
  padding: 5px 14px;
  color: var(--text-dim);
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
}

.tab:hover { color: var(--text); }

.tab[aria-current="true"] {
  color: var(--text);
  background: var(--bg);
  box-shadow: var(--shadow);
}

#sync-label { margin-left: auto; color: var(--text-faint); font-size: 11px; }
#whoami { color: var(--text-faint); font-size: 12px; }

.link-btn { color: var(--text-dim); font-size: 12px; text-decoration: underline; }
.link-btn:hover { color: var(--text); }
.link-btn.danger { color: var(--danger); }
.link-btn:disabled { opacity: .5; }

/* ------------------------------------------------------------------- app */

#app {
  display: grid;
  grid-template-columns: var(--rail-w) var(--list-w) minmax(0, 1fr);
  min-height: 0;
}

#app[hidden], #config[hidden] { display: none; }

.pane-title {
  color: var(--text-faint);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
}

.pane {
  display: flex;
  min-width: 0;
  flex-direction: column;
  border-right: 1px solid var(--border);
  overflow: hidden;
}

.pane:last-child { border-right: 0; }

.pane-head {
  display: flex;
  flex: none;
  align-items: center;
  gap: 8px;
  height: 52px;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
}

.pane-body { flex: 1; overflow-y: auto; overscroll-behavior: contain; }

/* ------------------------------------------------------------------ rail */

#rail { background: var(--bg-rail); }

.brand { min-width: 0; }

.brand strong { display: block; font-size: 14px; letter-spacing: -.01em; }

.brand span {
  display: block;
  color: var(--text-faint);
  font-size: 11px;
}

.icon-btn {
  display: grid;
  flex: none;
  place-items: center;
  width: 30px;
  height: 30px;
  color: var(--text-dim);
  border-radius: 6px;
}

.icon-btn:hover { color: var(--text); background: var(--bg-hover); }
.icon-btn svg { width: 16px; height: 16px; }

.spinning { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.group {
  padding: 14px 14px 4px;
  color: var(--text-faint);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
}

.folder {
  display: flex;
  align-items: center;
  gap: 10px;
  width: calc(100% - 16px);
  margin: 1px 8px;
  padding: 7px 10px;
  border-radius: 6px;
  text-align: left;
}

.folder:hover { background: var(--bg-hover); }

.folder[aria-current="true"] {
  background: var(--bg-sel);
  color: var(--accent);
}

.folder .dot {
  flex: none;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-faint);
  opacity: .45;
}

.folder[data-kind="list"] .dot { background: var(--accent); opacity: 1; }

.folder .name {
  flex: 1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.folder .count {
  flex: none;
  padding: 1px 6px;
  color: #fff;
  background: var(--accent);
  border-radius: 9px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.folder .count[data-zero="1"] {
  color: var(--text-faint);
  background: none;
}

.folder .warn { flex: none; color: var(--danger); font-size: 12px; }

.rail-foot {
  flex: none;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  color: var(--text-faint);
  font-size: 11px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.rail-foot button { color: var(--text-dim); text-decoration: underline; font-size: 11px; }
.rail-foot button:hover { color: var(--text); }

/* ------------------------------------------------------------------ list */

#search {
  width: 100%;
  padding: 7px 10px 7px 30px;
  font: inherit;
  color: var(--text);
  background: var(--bg-sub) no-repeat 9px center;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='13' height='13' viewBox='0 0 24 24' fill='none' stroke='%239aa1ab' stroke-width='2.4' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E");
  border: 1px solid var(--border);
  border-radius: 7px;
}

#search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.row {
  display: block;
  width: 100%;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.row:hover { background: var(--bg-hover); }

.row[aria-selected="true"] {
  background: var(--bg-sel);
  box-shadow: inset 3px 0 0 var(--accent);
}

.row-top {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 2px;
}

.row .from {
  flex: 1;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 13px;
  font-weight: 600;
}

.row .date {
  flex: none;
  color: var(--text-faint);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.row .subject {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 13px;
  margin-bottom: 1px;
}

.row .snippet {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: var(--text-faint);
  font-size: 12px;
}

.row[data-unread="1"] .from::before {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--accent);
  vertical-align: 1px;
}

.row[data-unread="1"] .subject { font-weight: 600; }

.row .tag {
  padding: 0 5px;
  margin-right: 6px;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 4px;
  font-size: 10px;
  font-weight: 600;
  vertical-align: 1px;
}

.clip { color: var(--text-faint); }

.empty {
  padding: 48px 20px;
  color: var(--text-faint);
  text-align: center;
  font-size: 13px;
}

/* ------------------------------------------------------------------ read */

#reader { background: var(--bg-sub); }

.msg-head {
  flex: none;
  padding: 18px 24px 14px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.msg-head h2 {
  margin: 0 0 10px;
  font-size: 17px;
  line-height: 1.35;
  letter-spacing: -.01em;
}

.meta {
  display: flex;
  align-items: baseline;
  gap: 8px;
  color: var(--text-dim);
  font-size: 12.5px;
}

.meta .who { color: var(--text); font-weight: 600; }
.meta .addr { color: var(--text-faint); }
.meta .when { margin-left: auto; flex: none; color: var(--text-faint); }

.meta-sub {
  margin-top: 3px;
  color: var(--text-faint);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 9px;
  color: var(--text);
  background: var(--bg-sub);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
  text-decoration: none;
}

.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip .sz { color: var(--text-faint); }

.banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 24px;
  background: var(--accent-soft);
  color: var(--text);
  font-size: 12.5px;
  border-bottom: 1px solid var(--border);
}

.banner button {
  margin-left: auto;
  padding: 3px 10px;
  color: #fff;
  background: var(--accent);
  border-radius: 5px;
  font-size: 12px;
  font-weight: 600;
}

#reader-inner {
  display: flex;
  flex: 1;
  min-height: 0;
  flex-direction: column;
}

/* Le contenu est positionné en absolu dans ce cadre : une iframe en height:100%
   dont le parent tire sa hauteur de flex:1 retombe sur ses 150px intrinsèques. */
#body-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  background: var(--bg);
}

#body-frame {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background: #fff;
}

#body-text {
  position: absolute;
  inset: 0;
  margin: 0;
  padding: 22px 24px 60px;
  overflow: auto;
  font: 13.5px/1.65 ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  white-space: pre-wrap;
  word-wrap: break-word;
}

#body-text a { color: var(--accent); }

.placeholder {
  display: grid;
  place-items: center;
  flex: 1;
  padding: 20px;
  color: var(--text-faint);
  text-align: center;
  font-size: 13px;
}

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
}

/* ---------------------------------------------------------- configuration */

#config { overflow-y: auto; background: var(--bg-sub); }

.config-page {
  max-width: 980px;
  margin: 0 auto;
  padding: 28px 24px 80px;
}

.card {
  margin-bottom: 24px;
  padding: 22px 24px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.card-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 6px;
}

.card h2 { margin: 0; font-size: 16px; letter-spacing: -.01em; }
.card-head .link-btn { margin-left: auto; }

.hint {
  margin: 0 0 18px;
  color: var(--text-dim);
  font-size: 12.5px;
  line-height: 1.55;
}

.loading, .error-box { padding: 18px 0; color: var(--text-faint); font-size: 13px; }
.error-box { color: var(--danger); }

.muted { color: var(--text-faint); }
.small { font-size: 12px; }

/* -- lignes de redirection */

.row-cfg {
  display: grid;
  grid-template-columns: 180px minmax(0, 1fr);
  gap: 16px;
  padding: 14px 0;
  border-top: 1px solid var(--border);
}

.rows .row-cfg:first-child { border-top: 0; }
.row-key { padding-top: 6px; font-size: 13px; overflow-wrap: anywhere; }
.row-key strong { display: block; }
.row-key .muted { font-size: 11px; }

.row-val { min-width: 0; }
.row-val .chips { margin: 0 0 8px; }

.chip.removable { padding-right: 4px; gap: 4px; }

.chip.removable button {
  display: grid;
  place-items: center;
  width: 17px;
  height: 17px;
  color: var(--text-faint);
  border-radius: 50%;
  font-size: 14px;
  line-height: 1;
}

.chip.removable button:hover { color: #fff; background: var(--danger); }

.adder { display: flex; gap: 8px; margin-bottom: 8px; }

.add-input {
  flex: 1;
  min-width: 0;
  padding: 7px 10px;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--bg-sub);
  border: 1px solid var(--border);
  border-radius: 7px;
}

.add-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.btn {
  flex: none;
  padding: 7px 14px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 600;
}

.btn.primary { color: #fff; background: var(--accent); }
.btn.ghost { color: var(--text-dim); border: 1px solid var(--border); }
.btn.ghost:hover { color: var(--text); background: var(--bg-hover); }
.btn:disabled { opacity: .55; cursor: default; }

.row-actions { display: flex; gap: 8px; justify-content: flex-end; }
.row-actions[hidden] { display: none; }

/* -- listes de diffusion */

.list-block {
  padding: 18px 0;
  border-top: 1px solid var(--border);
}

.list-block:first-child { border-top: 0; padding-top: 4px; }

.list-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 14px;
}

.list-head h3 { margin: 0; font-size: 14px; }
.list-head .muted { font-size: 12px; }

.badge {
  margin-left: auto;
  padding: 2px 9px;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.settings {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 12px 20px;
  margin-bottom: 14px;
}

.field { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; }
.field > span { color: var(--text-dim); }

.field select {
  padding: 7px 9px;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--bg-sub);
  border: 1px solid var(--border);
  border-radius: 7px;
}

.field.toggle {
  flex-direction: row;
  align-items: center;
  gap: 9px;
  align-self: end;
  padding-bottom: 7px;
  cursor: pointer;
}

.field.toggle input { accent-color: var(--accent); width: 15px; height: 15px; }
.field.toggle span { color: var(--text); }

.members { margin-top: 12px; }

.member-list {
  max-height: 320px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.member {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.member:last-child { border-bottom: 0; }
.member:hover { background: var(--bg-hover); }
.member span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

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

#toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  z-index: 60;
  padding: 10px 18px;
  color: #fff;
  background: #22262c;
  border-radius: 8px;
  box-shadow: var(--shadow);
  font-size: 13px;
  max-width: min(560px, calc(100vw - 40px));
  opacity: 0;
  transform: translate(-50%, 12px);
  transition: opacity .18s, transform .18s;
  pointer-events: none;
}

#toast[data-show="1"] { opacity: 1; transform: translate(-50%, 0); }
#toast[data-kind="error"] { background: var(--danger); }

/* ------------------------------------------------------------- responsive */

@media (max-width: 1100px) {
  :root { --rail-w: 220px; --list-w: 320px; }
}

@media (max-width: 820px) {
  #topbar { gap: 6px; padding: 0 10px; }
  .tabs { margin-left: 8px; }
  #sync-label, #whoami { display: none; }
  .row-cfg { grid-template-columns: 1fr; gap: 8px; }
  #app { grid-template-columns: 1fr; }
  .pane { display: none; }
  #app[data-mobile="rail"] #rail,
  #app[data-mobile="list"] #list,
  #app[data-mobile="reader"] #reader { display: flex; }
  .back { display: grid !important; }
}

.back { display: none; }
