/* ============================ Tokens ============================ */
:root {
  --bg: #020617;
  --surface: rgba(255, 255, 255, 0.055);
  --surface-2: rgba(255, 255, 255, 0.03);
  --border: rgba(255, 255, 255, 0.09);
  --border-strong: rgba(255, 255, 255, 0.14);
  --fg: #f8fafc;
  --muted: #94a3b8;
  --faint: #64748b;
  --accent: #22c55e;
  --accent-ink: #04140a;
  --danger: #ef4444;
  --radius: 22px;
  --radius-sm: 14px;
  --shadow: 0 10px 34px rgba(0, 0, 0, 0.42), inset 0 1px 0 rgba(255, 255, 255, 0.06);
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--fg);
  font-family: 'Fira Sans', system-ui, -apple-system, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* ===================== Ambient background ===================== */
.bg { position: fixed; inset: 0; z-index: -1; overflow: hidden; }
.blob {
  position: absolute;
  border-radius: 50%;
  filter: blur(70px);
}
.blob.b1 { width: 46vmax; height: 46vmax; top: -16vmax; left: -12vmax; background: #16a34a; opacity: 0.28; }
.blob.b2 { width: 40vmax; height: 40vmax; top: 30vmax; right: -14vmax; background: #2563eb; opacity: 0.22; }
.blob.b3 { width: 34vmax; height: 34vmax; bottom: -14vmax; left: 24vmax; background: #7c3aed; opacity: 0.18; }

/* ============================ Layout ============================ */
.app {
  max-width: 1000px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px calc(28px + env(safe-area-inset-bottom));
}

.appbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 4px 20px;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-icon {
  display: grid;
  place-items: center;
  width: 44px; height: 44px;
  border-radius: 13px;
  color: var(--accent);
  background: linear-gradient(160deg, rgba(34, 197, 94, 0.22), rgba(34, 197, 94, 0.06));
  border: 1px solid rgba(34, 197, 94, 0.28);
  flex: none;
}
.brand-icon svg { width: 22px; height: 22px; }
.brand-text h1 { margin: 0; font-size: 1.15rem; font-weight: 700; letter-spacing: -0.01em; }
.subtitle { margin: 0; font-size: 0.8rem; color: var(--muted); }

/* ============================ Glass ============================ */
.card {
  border-radius: var(--radius);
  padding: 20px;
}
.glass {
  background: linear-gradient(155deg, var(--surface), var(--surface-2));
  backdrop-filter: blur(22px) saturate(150%);
  -webkit-backdrop-filter: blur(22px) saturate(150%);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

/* ============================ Bento ============================ */
.bento {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}
.tile-hero, .tile-actions, .tile-settings, .tile-half, .tile-inbox { grid-column: 1 / -1; }
.tile-stat { grid-column: span 1; }

@media (min-width: 720px) {
  .bento { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .tile-hero { grid-column: span 4; }
  .tile-stat { grid-column: span 1; }
  .tile-actions { grid-column: span 4; }
  .tile-settings { grid-column: span 4; }
  .tile-half { grid-column: span 2; }
  .tile-inbox { grid-column: span 4; }
}

/* ============================ Settings ============================ */
.settings-help {
  margin: 0 0 8px;
  font-size: 0.86rem;
  color: var(--muted);
  line-height: 1.45;
}
.settings-help a { color: #86efac; }
.settings-meta {
  margin: 0 0 14px;
  font-size: 0.78rem;
  color: var(--faint);
}
.settings-form { display: flex; flex-direction: column; gap: 10px; }
.settings-input {
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--border-strong);
  background: rgba(0, 0, 0, 0.25);
  color: var(--fg);
  font: inherit;
  font-size: 0.95rem;
  outline: none;
}
.settings-input:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.2);
}
.settings-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.settings-textarea {
  width: 100%;
  min-height: 96px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border-strong);
  background: rgba(0, 0, 0, 0.25);
  color: var(--fg);
  font: inherit;
  font-size: 0.92rem;
  outline: none;
  resize: vertical;
}
.settings-textarea:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.2);
}

.code-create {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  margin-bottom: 18px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
}
.code-create-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 650;
}
.code-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 600;
}

.codes-toolbar {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-bottom: 10px;
}
@media (min-width: 720px) {
  .codes-toolbar {
    grid-template-columns: minmax(0, 1fr) 160px auto;
    align-items: center;
  }
}
.codes-sort { max-width: none; width: 100%; }
.codes-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}
.codes-tab {
  appearance: none;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  border-radius: 999px;
  padding: 7px 12px;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 650;
  cursor: pointer;
}
.codes-tab.active {
  color: #86efac;
  border-color: rgba(34, 197, 94, 0.35);
  background: rgba(34, 197, 94, 0.12);
}
.codes-list { display: flex; flex-direction: column; gap: 10px; }
.code-card {
  padding: 14px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.035);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.code-card-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.code-value {
  font-family: 'Fira Code', ui-monospace, monospace;
  font-weight: 650;
  font-size: 1.05rem;
  letter-spacing: 0.04em;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.code-copy {
  appearance: none;
  border: 0;
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
  width: 34px;
  height: 34px;
  border-radius: 10px;
  cursor: pointer;
}
.code-copy:hover { color: var(--fg); }
.code-badge {
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  padding: 4px 8px;
  border-radius: 999px;
  text-transform: uppercase;
}
.code-badge.active { background: rgba(34, 197, 94, 0.14); color: #86efac; }
.code-badge.expiring { background: rgba(245, 158, 11, 0.16); color: #fbbf24; }
.code-badge.expired { background: rgba(239, 68, 68, 0.14); color: #fca5a5; }
.code-badge.revoked { background: rgba(148, 163, 184, 0.16); color: #cbd5e1; }
.code-label { font-weight: 600; font-size: 0.9rem; }
.code-meta {
  font-size: 0.76rem;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
}
.code-emails {
  font-size: 0.78rem;
  color: var(--faint);
  word-break: break-all;
}
.code-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 2px;
}
.code-actions .btn,
.code-actions .chip-btn {
  min-height: 36px;
  height: auto;
  width: auto;
  padding: 0 12px;
  font-size: 0.8rem;
  font-weight: 650;
}
.master-code {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.master-code summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 10px;
}
.code-edit {
  display: none;
  flex-direction: column;
  gap: 8px;
  margin-top: 6px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
}
.code-card.editing .code-edit { display: flex; }

/* ============================ Hero ============================ */
.hero { display: flex; flex-direction: column; gap: 16px; }
.hero-top { display: flex; align-items: center; gap: 9px; }
.status-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.55);
  animation: pulse 2.4s var(--ease) infinite;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.5); }
  70% { box-shadow: 0 0 0 9px rgba(34, 197, 94, 0); }
  100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
}
.hero-status { font-weight: 600; font-size: 0.92rem; letter-spacing: 0.01em; }
.hero-route {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.route-node {
  font-weight: 600; font-size: 1.02rem;
  padding: 8px 14px; border-radius: 12px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  max-width: 100%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.route-node.dest { color: var(--accent); border-color: rgba(34, 197, 94, 0.3); background: rgba(34, 197, 94, 0.08); }
.route-arrow { color: var(--faint); display: grid; place-items: center; }
.route-arrow svg { width: 20px; height: 20px; }

/* ============================ Pills ============================ */
.pills { display: flex; flex-wrap: wrap; gap: 8px; }
.pill {
  font-size: 0.74rem; font-weight: 500;
  padding: 5px 11px; border-radius: 999px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  color: var(--muted);
  display: inline-flex; align-items: center; gap: 6px;
}
.pill::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--faint); }
.pill.ok { color: #86efac; border-color: rgba(34, 197, 94, 0.28); background: rgba(34, 197, 94, 0.08); }
.pill.ok::before { background: var(--accent); }
.pill.bad { color: #fca5a5; border-color: rgba(239, 68, 68, 0.28); background: rgba(239, 68, 68, 0.08); }
.pill.bad::before { background: var(--danger); }

/* ============================ Stat tiles ============================ */
.tile-stat { display: flex; flex-direction: column; gap: 4px; padding: 16px; min-height: 116px; }
.stat-ico {
  width: 34px; height: 34px; border-radius: 10px; margin-bottom: 6px;
  display: grid; place-items: center; color: var(--muted);
  background: rgba(255, 255, 255, 0.05); border: 1px solid var(--border);
}
.stat-ico svg { width: 18px; height: 18px; }
.stat-ico.green { color: var(--accent); background: rgba(34, 197, 94, 0.1); border-color: rgba(34, 197, 94, 0.25); }
.stat-num {
  font-size: 1.7rem; font-weight: 700; line-height: 1.1;
  font-variant-numeric: tabular-nums; letter-spacing: -0.01em;
}
.stat-num.sm { font-size: 1rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-label { font-size: 0.76rem; color: var(--muted); }

/* ============================ Actions ============================ */
.actions { display: flex; gap: 12px; flex-wrap: wrap; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px;
  border-radius: 13px; border: 1px solid var(--border-strong);
  background: rgba(255, 255, 255, 0.05); color: var(--fg);
  font: inherit; font-weight: 600; font-size: 0.92rem;
  cursor: pointer; text-decoration: none;
  transition: transform 0.15s var(--ease), background 0.2s var(--ease), border-color 0.2s var(--ease), opacity 0.2s;
  flex: 1 1 auto;
}
.btn svg { width: 18px; height: 18px; }
.btn:hover { background: rgba(255, 255, 255, 0.09); border-color: var(--border-strong); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary {
  background: linear-gradient(160deg, #34d16b, var(--accent));
  color: var(--accent-ink); border-color: transparent;
  box-shadow: 0 8px 22px rgba(34, 197, 94, 0.32);
}
.btn.primary:hover { background: linear-gradient(160deg, #3ddb74, #26cf62); }
.btn.block { width: 100%; flex: none; }

.icon-btn {
  display: grid; place-items: center;
  width: 44px; height: 44px; flex: none;
  border-radius: 13px; border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.05); color: var(--muted);
  cursor: pointer; transition: color 0.2s, background 0.2s, transform 0.15s var(--ease);
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:hover { color: #fca5a5; background: rgba(239, 68, 68, 0.1); border-color: rgba(239, 68, 68, 0.28); }
.icon-btn:active { transform: scale(0.94); }

/* ============================ Card head ============================ */
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.card-head h2 { margin: 0; font-size: 0.98rem; font-weight: 600; }
.count-badge {
  font-size: 0.78rem; font-weight: 600; font-variant-numeric: tabular-nums;
  min-width: 24px; height: 24px; padding: 0 8px; border-radius: 999px;
  display: inline-grid; place-items: center;
  background: rgba(34, 197, 94, 0.12); color: #86efac; border: 1px solid rgba(34, 197, 94, 0.25);
}

/* ============================ Accounts ============================ */
.accounts { display: flex; flex-direction: column; gap: 10px; }
.account {
  display: flex; align-items: center; gap: 12px;
  padding: 12px; border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.035); border: 1px solid var(--border);
}
.acct-avatar {
  width: 40px; height: 40px; flex: none; border-radius: 12px;
  display: grid; place-items: center; font-weight: 700; font-size: 1rem;
  color: var(--accent-ink); background: linear-gradient(160deg, #4ade80, #22c55e);
  text-transform: uppercase;
}
.acct-meta { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 2px; }
.acct-email { font-weight: 600; font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acct-sub { font-size: 0.76rem; color: var(--muted); display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.acct-sub .sdot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.acct-sub .sdot.on { background: var(--accent); }
.acct-sub .sdot.off { background: var(--faint); }
.acct-num { font-variant-numeric: tabular-nums; }
.acct-err { color: #fca5a5; width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acct-actions { display: flex; gap: 8px; flex: none; }
.chip-btn {
  width: 44px; height: 44px; flex: none; display: grid; place-items: center;
  border-radius: 12px; border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.05); color: var(--muted);
  cursor: pointer; transition: background 0.2s, color 0.2s, transform 0.15s var(--ease);
}
.chip-btn svg { width: 17px; height: 17px; }
.chip-btn:hover { background: rgba(255, 255, 255, 0.1); color: var(--fg); }
.chip-btn:active { transform: scale(0.92); }
.chip-btn.danger:hover { color: #fca5a5; background: rgba(239, 68, 68, 0.12); border-color: rgba(239, 68, 68, 0.28); }

/* ============================ Logs ============================ */
.logs { display: flex; flex-direction: column; gap: 8px; max-height: 340px; overflow-y: auto; }
.logs::-webkit-scrollbar { width: 6px; }
.logs::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.12); border-radius: 999px; }
.log { display: flex; gap: 11px; padding: 9px 4px; border-bottom: 1px solid rgba(255, 255, 255, 0.05); }
.log:last-child { border-bottom: none; }
.log-ico {
  width: 28px; height: 28px; flex: none; border-radius: 9px;
  display: grid; place-items: center;
  background: rgba(255, 255, 255, 0.05); color: var(--muted);
}
.log-ico svg { width: 15px; height: 15px; }
.log-ico.forward { color: var(--accent); background: rgba(34, 197, 94, 0.1); }
.log-ico.error { color: #fca5a5; background: rgba(239, 68, 68, 0.1); }
.log-body { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 1px; }
.log-main { font-size: 0.82rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.log-main .who { color: var(--muted); }
.log-time { font-size: 0.7rem; color: var(--faint); font-variant-numeric: tabular-nums; }

/* ============================ Received mail ============================ */
.inbox-controls { display: flex; align-items: center; gap: 8px; min-width: 0; }
.select {
  min-height: 40px; max-width: 240px; padding: 0 34px 0 12px;
  border-radius: 12px; border: 1px solid var(--border-strong);
  background: rgba(0, 0, 0, 0.25); color: var(--fg);
  font: inherit; font-size: 0.85rem; font-weight: 500;
  outline: none; cursor: pointer;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  appearance: none; -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; background-size: 16px;
}
.select:focus-visible { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.2); }
.select option { background: #0b1220; color: var(--fg); }

.messages { display: flex; flex-direction: column; gap: 8px; max-height: 420px; overflow-y: auto; }
.messages::-webkit-scrollbar { width: 6px; }
.messages::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.12); border-radius: 999px; }
.msg {
  display: flex; flex-direction: column; gap: 3px;
  padding: 12px; border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.035); border: 1px solid var(--border);
}
.msg.is-unread { border-color: rgba(34, 197, 94, 0.28); background: rgba(34, 197, 94, 0.05); }
.msg-top { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.msg-from { font-weight: 600; font-size: 0.86rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-time { font-size: 0.7rem; color: var(--faint); font-variant-numeric: tabular-nums; flex: none; }
.msg-subject { font-size: 0.85rem; color: var(--fg); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg.is-unread .msg-subject { font-weight: 600; }
.msg-preview {
  font-size: 0.78rem; color: var(--muted);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.msg-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 3px; }
.tag {
  font-size: 0.68rem; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  border: 1px solid var(--border);
}
.tag.unread { color: #86efac; background: rgba(34, 197, 94, 0.1); border-color: rgba(34, 197, 94, 0.25); }
.tag.fwd { color: #93c5fd; background: rgba(37, 99, 235, 0.12); border-color: rgba(37, 99, 235, 0.28); }

/* ============================ Login ============================ */
.login-card {
  max-width: 400px; margin: 8vh auto 0; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 30px 24px;
}
.login-badge {
  width: 60px; height: 60px; border-radius: 18px; margin-bottom: 8px;
  display: grid; place-items: center; color: var(--accent);
  background: linear-gradient(160deg, rgba(34, 197, 94, 0.2), rgba(34, 197, 94, 0.05));
  border: 1px solid rgba(34, 197, 94, 0.3);
}
.login-badge svg { width: 30px; height: 30px; }
.login-card h2 { margin: 0; font-size: 1.3rem; font-weight: 700; }
#loginHint { margin: 0 0 12px; color: var(--muted); font-size: 0.88rem; }
.login-form { width: 100%; display: flex; flex-direction: column; gap: 12px; }
input {
  width: 100%; min-height: 48px; padding: 0 16px;
  border-radius: 13px; border: 1px solid var(--border-strong);
  background: rgba(0, 0, 0, 0.25); color: var(--fg);
  font: inherit; font-size: 1rem; outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}
input::placeholder { color: var(--faint); }
input:focus-visible { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.2); }
.code-input {
  text-align: center; font-family: 'Fira Code', monospace;
  font-size: 1.7rem; letter-spacing: 0.5em; padding-left: 0.5em; font-weight: 500;
}
.error { color: #fca5a5; font-size: 0.85rem; margin: 4px 0 0; }

/* ============================ Toast ============================ */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  padding: 12px 20px; border-radius: 13px;
  background: rgba(15, 23, 42, 0.9); backdrop-filter: blur(14px);
  border: 1px solid var(--border-strong); color: var(--fg);
  font-size: 0.88rem; font-weight: 500;
  box-shadow: 0 12px 34px rgba(0, 0, 0, 0.5); z-index: 1000;
  max-width: calc(100vw - 32px);
}

/* ============================ Utilities ============================ */
.hidden { display: none !important; }
.empty { color: var(--muted); font-size: 0.85rem; text-align: center; padding: 20px 8px; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (max-width: 400px) {
  .stat-num { font-size: 1.45rem; }
  .route-node { font-size: 0.92rem; }
}

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