/* === Design tokens =================================================== */
:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --fg: #1c1f24;
  --muted: #6c7280;
  --border: #e6e8eb;
  --accent: #0a66c2;
  --accent-fg: #ffffff;
  --accent-soft: #e8f0fa;
  --ok-bg: #e8f6ef;
  --ok-fg: #1f7c4d;
  --err-bg: #fdecea;
  --err-fg: #c0392b;
  --shadow-sm: 0 1px 2px rgba(20, 24, 32, .04);
  --shadow-md: 0 2px 6px rgba(20, 24, 32, .06);
  --r-sm: 4px;
  --r-md: 6px;
  --r-lg: 10px;
  --space-1: .25rem;
  --space-2: .5rem;
  --space-3: .75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181c22;
    --fg: #e7e9ec;
    --muted: #97a0aa;
    --border: #262b33;
    --accent: #4ea1ff;
    --accent-fg: #0f1115;
    --accent-soft: #1a2a40;
    --ok-bg: #112d20;
    --ok-fg: #69d6a1;
    --err-bg: #341615;
    --err-fg: #f47c70;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .25);
    --shadow-md: 0 2px 6px rgba(0, 0, 0, .35);
  }
}

/* === Base ============================================================ */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.4;
}
h1, h2, h3 { margin: 0 0 var(--space-3); line-height: 1.2; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.15rem; margin-top: var(--space-5); }
h3 { font-size: 1rem; margin-top: var(--space-4); color: var(--muted); }
code { font-family: var(--font-mono); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.err   { color: var(--err-fg); }
.inline { display: inline; }

/* === Topbar ========================================================== */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--space-3) var(--space-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: var(--space-3);
}
.topbar .brand {
  font-weight: 600;
  color: var(--fg);
  font-size: 1.05rem;
}
.topbar nav {
  display: flex;
  gap: var(--space-4);
  align-items: center;
}
.topbar nav a {
  padding: var(--space-1) var(--space-2);
  border-radius: var(--r-sm);
}
.topbar nav a[aria-current="page"] {
  background: var(--accent-soft);
  color: var(--accent);
}
.topbar .user {
  color: var(--muted);
  font-size: .9rem;
}
.topbar details summary { list-style: none; }
.topbar details summary::-webkit-details-marker { display: none; }
.topbar details > nav { display: flex; gap: var(--space-4); align-items: center; }
.topbar form.inline { margin: 0; }
.topbar button.link {
  background: none;
  border: 0;
  color: var(--accent);
  cursor: pointer;
  padding: var(--space-1) var(--space-2);
  font: inherit;
}
.topbar button.link:hover { text-decoration: underline; }
.menu-toggle { display: none; }

/* === Layout ========================================================== */
main {
  padding: var(--space-5) var(--space-4);
  max-width: 1100px;
  margin: 0 auto;
}
section { margin-bottom: var(--space-5); }

/* === Cards (login, placeholder, alias-detail, manage) =============== */
.login, .placeholder, .alias-detail, .manage, .settings, .admin, .dashboard {
  background: var(--surface);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}
.login    { max-width: 380px; margin: 4rem auto; padding: var(--space-6); }
.placeholder, .alias-detail, .manage, .settings, .admin {
  padding: var(--space-5);
}
.dashboard { padding: 0; background: transparent; box-shadow: none; }

/* === Forms =========================================================== */
input, select, textarea, button {
  font: inherit;
  color: inherit;
}
input[type="text"], input[type="email"], input[type="password"], input[type="search"], select, textarea {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--fg);
  min-width: 0;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}
button {
  background: var(--accent);
  color: var(--accent-fg);
  border: 0;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--r-sm);
  cursor: pointer;
  font-weight: 500;
}
button:hover { filter: brightness(1.05); }
button.alt {
  background: var(--accent-soft);
  color: var(--accent);
}
button.danger {
  background: var(--err-fg);
  color: #fff;
}

.login label { display: block; margin: var(--space-4) 0; }
.login input { display: block; width: 100%; margin-top: var(--space-1); }
.login button { width: 100%; padding: var(--space-3); }

/* === Dashboard header + filters ====================================== */
.dash-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-top: var(--space-3);
}
.filters {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  flex-wrap: wrap;
}
.filters input[type=search] { min-width: 14rem; }
.chip {
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--fg);
  font-size: .85rem;
  white-space: nowrap;
}
.chip.on {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}
.chip:hover { text-decoration: none; border-color: var(--accent); }

/* === Quick-create ==================================================== */
.quick-create, .mailbox-add, .admin-form {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
  align-items: center;
  padding: var(--space-4);
  background: var(--surface);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  margin-bottom: var(--space-4);
}
.quick-create input[type=text], .quick-create select { min-width: 8rem; }

/* === Tables ========================================================== */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin-top: var(--space-3); }
table.aliases, table.contacts, table.logs, table.mailboxes, table.apikeys, table.admin-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: var(--r-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  font-size: .9rem;
}
table thead th {
  text-align: left;
  padding: var(--space-3);
  background: var(--bg);
  color: var(--muted);
  font-weight: 500;
  font-size: .8rem;
  text-transform: uppercase;
  letter-spacing: .03em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table td {
  padding: var(--space-3);
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
table tr:last-child td { border-bottom: 0; }
.aliases a { color: inherit; }
.aliases a:hover { color: var(--accent); }

.toggle {
  background: none;
  border: 0;
  cursor: pointer;
  font-size: 1.4rem;
  padding: 0;
  line-height: 1;
  color: var(--border);
}
.toggle.on { color: #2ecc71; }
.toggle.off { color: var(--border); }

.badge {
  display: inline-block;
  font-size: .75rem;
  padding: var(--space-1) var(--space-2);
  border-radius: 999px;
  vertical-align: middle;
  margin-left: var(--space-2);
}
.badge.on { background: var(--ok-bg); color: var(--ok-fg); }
.badge.off { background: var(--err-bg); color: var(--err-fg); }

/* === Flash banners ==================================================== */
.flash {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--r-md);
  margin-bottom: var(--space-3);
  border: 1px solid transparent;
}
.flash.ok  { background: var(--ok-bg);  color: var(--ok-fg); border-color: var(--ok-fg); }
.flash.err { background: var(--err-bg); color: var(--err-fg); border-color: var(--err-fg); }

/* === Alias detail / contacts / logs ================================== */
.alias-edit label { display: block; margin: var(--space-3) 0; }
.alias-edit input, .alias-edit select { min-width: 280px; max-width: 100%; }
.contact-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: var(--space-1) var(--space-3);
  cursor: pointer;
  font-size: .85rem;
  color: var(--fg);
}
.contact-btn.on { color: var(--ok-fg); border-color: var(--ok-fg); }
.contact-btn.off { color: var(--err-fg); border-color: var(--err-fg); }

/* === Manage page (token-auth) ======================================== */
.manage .stats { display: grid; grid-template-columns: max-content 1fr; gap: var(--space-2) var(--space-4); margin: var(--space-4) 0; }
.manage .stats dt { color: var(--muted); }
.manage .stats dd { margin: 0; font-weight: 500; }

/* === Responsive ====================================================== */
@media (max-width: 768px) {
  main { padding: var(--space-4) var(--space-3); }
  .menu-toggle {
    display: inline-block;
    background: none;
    color: var(--fg);
    border: 1px solid var(--border);
    border-radius: var(--r-sm);
    padding: var(--space-1) var(--space-3);
  }
  details[open] .menu-toggle { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
  .topbar { flex-wrap: wrap; }
  .topbar details { width: 100%; }
  .topbar details summary { cursor: pointer; width: max-content; margin-left: auto; }
  .topbar details:not([open]) > nav { display: none; }
  .topbar details[open] > nav {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-1);
    padding-top: var(--space-3);
  }
  .topbar nav a, .topbar nav .user { padding: var(--space-2) var(--space-3); }
  .topbar .user { border-top: 1px solid var(--border); padding-top: var(--space-3); }
  /* Dashboard: hide low-priority columns */
  .aliases th:nth-child(3), .aliases td:nth-child(3),  /* last contact */
  .aliases th:nth-child(4), .aliases td:nth-child(4),  /* last received */
  .aliases th:nth-child(5), .aliases td:nth-child(5) { /* forwards */
    display: none;
  }
  /* Contacts/logs: hide subject + last-seen on narrow */
  .logs th:nth-child(4), .logs td:nth-child(4) { display: none; }
  .contacts th:nth-child(3), .contacts td:nth-child(3) { display: none; }
}
@media (max-width: 480px) {
  .quick-create, .mailbox-add, .admin-form { flex-direction: column; align-items: stretch; }
  .quick-create input, .quick-create select, .quick-create button,
  .mailbox-add input, .mailbox-add button,
  .admin-form input, .admin-form button { width: 100%; }
  .quick-create span { display: none; }
  main { padding: var(--space-3) var(--space-2); }
  .alias-edit input, .alias-edit select { min-width: 0; width: 100%; }
}
