:root {
  --bg: #0e1116;
  --fg: #e6e6e6;
  --muted: #8b949e;
  --card: #161b22;
  --border: #30363d;
  --accent: #58a6ff;
  --prio1: #f85149;
  --prio2: #d29922;
  --prio3: #8b949e;
  --unread: #58a6ff;
}
* { box-sizing: border-box; }
body {
  margin: 0; padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--fg);
  font-size: 14px;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  display: flex; align-items: center; gap: 1rem;
  padding: .6rem 1rem;
  background: var(--card); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: bold; font-size: 1.1rem; }
.topbar nav { flex: 1; }
.filter { display: flex; gap: .4rem; align-items: center; flex-wrap: wrap; }
.filter[hidden] { display: flex !important; }
.filter-toggle {
  display: none;
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  padding: .35rem .7rem; border-radius: 4px; cursor: pointer;
  align-items: center; gap: .3rem; font-size: 13px; margin-left: auto;
}
.filter-toggle.active { color: var(--fg); border-color: var(--fg); }
.filter input[type=search], .filter select, .filter button {
  background: var(--bg); color: var(--fg); border: 1px solid var(--border);
  padding: .3rem .5rem; border-radius: 4px; font-size: 13px;
}
.filter .chk { display: flex; align-items: center; gap: .2rem; color: var(--muted); }
.logout button {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  padding: .3rem .6rem; border-radius: 4px; cursor: pointer;
}

main { padding: 1rem; max-width: 1400px; margin: 0 auto; }

.counts { color: var(--muted); margin-bottom: .8rem; font-size: 13px; }

.tabs {
  display: flex; gap: .2rem; margin-bottom: .5rem; flex-wrap: wrap;
  border-bottom: 1px solid var(--border);
}
.tab {
  padding: .4rem .9rem; color: var(--muted);
  border: 1px solid transparent; border-bottom: none;
  border-top-left-radius: 5px; border-top-right-radius: 5px;
  text-decoration: none; font-size: 14px;
  margin-bottom: -1px;
}
.tab:hover { color: var(--fg); text-decoration: none; background: var(--card); }
.tab.active {
  color: var(--fg); background: var(--bg);
  border-color: var(--border); border-bottom: 1px solid var(--bg);
}
.tab-count {
  display: inline-block; margin-left: .3rem; font-size: 11px;
  color: var(--muted); background: var(--card); padding: 0 .35rem; border-radius: 8px;
}
.tab.active .tab-count { background: var(--border); color: var(--fg); }

table.mails { width: 100%; border-collapse: collapse; }
table.mails th {
  text-align: left; font-weight: normal; color: var(--muted);
  padding: .3rem .5rem; border-bottom: 1px solid var(--border); font-size: 12px; text-transform: uppercase;
}
a.sort-link { color: var(--muted); text-decoration: none; }
a.sort-link:hover { color: var(--fg); }
table.mails td {
  padding: .5rem; border-bottom: 1px solid var(--border); vertical-align: top;
}
table.mails tr.row { cursor: pointer; }
table.mails tr.row:hover { background: var(--card); }
tr.status-unread .subject { font-weight: bold; color: var(--fg); }
tr.status-read .subject, tr.status-replied .subject { color: var(--muted); }
tr.status-replied td.from::before { content: "↩ "; color: var(--accent); }
tr.prio-1 td.prio { color: var(--prio1); font-weight: bold; }
tr.prio-2 td.prio { color: var(--prio2); }
tr.prio-3 td.prio { color: var(--prio3); }
td.kat {
  color: var(--muted); font-size: 12px;
}
td.subject { max-width: 400px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.kurzfassung { color: var(--muted); font-size: 12px; max-width: 400px; }
td.date { color: var(--muted); font-size: 12px; white-space: nowrap; }
td.row-actions { white-space: nowrap; text-align: right; }
td.row-actions form { display: inline; margin-left: .2rem; }

/* Desktop: Toggle versteckt, Items direkt sichtbar als horizontale Icons */
.action-menu { display: inline-block; position: relative; }
.action-menu .menu-toggle { display: none; }
.action-menu .menu-items { display: inline-flex; gap: .2rem; }
.action-menu .menu-items[hidden] { display: inline-flex !important; }
.action-menu .menu-label { display: none; }
button.ico {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  width: 1.8rem; height: 1.8rem; line-height: 1; padding: 0; border-radius: 50%;
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
button.ico svg { display: block; }
button.ico:hover { color: var(--fg); border-color: var(--fg); }
button.ico.warn:hover { color: #000; background: var(--prio2); border-color: var(--prio2); }
button.ico.danger:hover { color: #000; background: var(--prio1); border-color: var(--prio1); }
.empty { text-align: center; color: var(--muted); padding: 3rem; }

.bl-form { display: flex; gap: .5rem; margin: 1rem 0; }
.bl-form input, .bl-form select {
  background: var(--bg); color: var(--fg); border: 1px solid var(--border);
  padding: .4rem; border-radius: 4px;
}
.bl-form input { flex: 1; }
.bl-form button {
  background: var(--accent); color: #000; border: none;
  padding: .4rem 1rem; border-radius: 4px; cursor: pointer;
}
.muted { color: var(--muted); font-size: 13px; }


/* ------------------- Mobile ------------------- */
@media (max-width: 720px) {
  body { font-size: 15px; }
  .topbar {
    flex-wrap: wrap; gap: .5rem; padding: .5rem .7rem;
  }
  .topbar nav { flex-basis: 100%; order: 3; }
  .filter { flex-wrap: wrap; gap: .3rem; }
  .filter[hidden] { display: none !important; }
  .filter input[type=search] { flex: 1 1 100%; }
  .filter select, .filter .chk, .filter button { font-size: 14px; }
  .filter-toggle { display: inline-flex; }
  .logout { order: 2; }
  main { padding: .5rem; }

  /* Tabelle → Karten mit Grid-Layout */
  table.mails, table.mails thead, table.mails tbody {
    display: block;
  }
  table.mails thead { display: none; }
  table.mails tr.row, table.mails tbody > tr {
    display: grid;
    grid-template-columns: auto auto 1fr auto;
    grid-template-areas:
      "prio    kat     kat       actions"
      "date    from    from      from"
      "subject subject subject   subject"
      "kurz    kurz    kurz      kurz";
    gap: .2rem .5rem;
    align-items: center;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: .6rem .8rem;
    margin-bottom: .5rem;
  }
  table.mails tr.row td, table.mails tbody > tr td {
    display: block;
    border: none; padding: 0;
    max-width: none !important; white-space: normal;
    text-overflow: clip; overflow: visible;
  }
  td.date {
    grid-area: date;
    font-size: 12px; color: var(--muted); text-align: left;
    white-space: nowrap;
  }
  td.prio {
    grid-area: prio;
    width: 1.6rem; height: 1.6rem; border-radius: 50%;
    display: inline-flex !important; align-items: center; justify-content: center;
    font-size: 12px; font-weight: bold;
    background: var(--border); color: var(--muted);
  }
  tr.prio-1 td.prio { background: var(--prio1); color: #000; }
  tr.prio-2 td.prio { background: var(--prio2); color: #000; }
  tr.prio-3 td.prio { background: transparent; color: var(--muted); border: 1px solid var(--border); }
  td.kat {
    grid-area: kat;
    font-size: 12px; color: var(--muted);
  }
  td.from { grid-area: from; font-size: 13px; color: var(--muted); }
  td.subject {
    grid-area: subject;
    font-size: 15px; font-weight: 500;
    max-width: 100% !important; white-space: normal;
  }
  td.kurzfassung { grid-area: kurz; font-size: 13px; color: var(--muted); }
  td.row-actions {
    grid-area: actions;
    display: flex !important; gap: .3rem; justify-content: flex-end;
    position: relative;
  }
  td.row-actions form { display: inline; margin: 0; }
  button.ico { width: 2rem; height: 2rem; font-size: 16px; }

  /* Mobile: Toggle sichtbar, Items als Popover */
  .action-menu .menu-toggle { display: inline-flex; }
  .action-menu .menu-items,
  .action-menu .menu-items[hidden] {
    display: none !important;
    position: absolute; top: 100%; right: 0; z-index: 20;
    background: var(--card); border: 1px solid var(--border);
    border-radius: 6px; padding: .4rem; min-width: 180px;
    box-shadow: 0 4px 12px rgba(0,0,0,.35);
    flex-direction: column; align-items: stretch;
  }
  .action-menu .menu-items:not([hidden]) { display: flex !important; }
  .action-menu .menu-items form { display: block; margin: 0; }
  .action-menu .menu-items button.ico {
    width: 100%; height: auto; border-radius: 4px;
    padding: .5rem .6rem;
    justify-content: flex-start; gap: .6rem;
  }
  .action-menu .menu-items .menu-label {
    display: inline; font-size: 14px; color: var(--fg);
  }

  /* Mail-Detail */
  .mail header h1 { font-size: 1.15rem; }
  .mail .meta { font-size: 12px; }
  .mail .actions { flex-wrap: wrap; }
  .mail .actions button, .mail .actions .btn { font-size: 12px; padding: .35rem .6rem; }
  .draft-form { flex-direction: column; }
  .draft-form input { width: 100%; }
  .send-form textarea { font-size: 15px; }

  /* Login */
  .login-form { min-width: 0; width: 90vw; }
}

/* Mail-Detail */
.mail header h1 { margin: 0 0 .5rem 0; font-size: 1.4rem; }
.mail .meta { color: var(--muted); font-size: 13px; display: flex; flex-direction: column; gap: .2rem; margin-bottom: 1rem; }
.mail .meta .kurz { color: var(--accent); margin-top: .3rem; }
.mail .actions { margin-bottom: 1rem; display: flex; gap: .5rem; }
.mail .actions button, .mail .actions .btn {
  background: transparent; border: 1px solid var(--border); color: var(--fg);
  padding: .3rem .7rem; border-radius: 4px; cursor: pointer; font-size: 13px;
  display: inline-block; text-decoration: none;
}
.mail .actions button.danger {
  border-color: var(--prio1); color: var(--prio1);
}
.mail .actions button.danger:hover {
  background: var(--prio1); color: #000;
}
.mail .body { background: var(--card); border: 1px solid var(--border); padding: 1rem; border-radius: 6px; margin-bottom: 1.5rem; }
.mail .body pre { white-space: pre-wrap; word-wrap: break-word; margin: 0; font-family: inherit; }
.mail .html-body { max-height: 600px; overflow: auto; }
.mail .html-body img { max-width: 100%; }

.reply h2 { font-size: 1rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.draft-form { display: flex; gap: .5rem; margin-bottom: .8rem; }
.draft-form input {
  flex: 1; background: var(--bg); color: var(--fg); border: 1px solid var(--border);
  padding: .5rem; border-radius: 4px;
}
.draft-form button, .send-form button {
  background: var(--accent); color: #000; border: none;
  padding: .5rem 1rem; border-radius: 4px; cursor: pointer; font-weight: 500;
}
.draft-form button { background: var(--card); color: var(--fg); border: 1px solid var(--border); font-weight: normal; }
.send-form textarea {
  width: 100%; background: var(--bg); color: var(--fg); border: 1px solid var(--border);
  padding: .7rem; border-radius: 4px; font-family: inherit; font-size: 14px; resize: vertical;
  margin-bottom: .5rem;
}
.error { background: #4a1d1d; border: 1px solid var(--prio1); padding: .5rem 1rem; border-radius: 4px; color: #ffb4b4; margin: .5rem 0; }
.back { display: inline-block; padding: .3rem .5rem; color: var(--muted); }

/* Login */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-form {
  background: var(--card); border: 1px solid var(--border); padding: 2rem; border-radius: 8px;
  display: flex; flex-direction: column; gap: 1rem; min-width: 320px;
}
.login-form h1 { margin: 0 0 .5rem 0; text-align: center; }
.login-form label { display: flex; flex-direction: column; gap: .3rem; color: var(--muted); font-size: 13px; }
.login-form input {
  background: var(--bg); color: var(--fg); border: 1px solid var(--border);
  padding: .6rem; border-radius: 4px; font-size: 15px;
}
.login-form button {
  background: var(--accent); color: #000; border: none; padding: .7rem; border-radius: 4px;
  cursor: pointer; font-weight: 500; font-size: 15px;
}
