:root {
  --bg: #0b0f14;
  --surface: #131a24;
  --surface-2: #1a2332;
  --border: #243044;
  --text: #e8edf4;
  --muted: #7d8da6;
  --accent: #7c5cfc;
  --accent-hover: #6b4ae8;
  --accent-glow: rgba(124, 92, 252, 0.15);
  --success: #22c55e;
  --error: #ef4444;
  --warning: #f59e0b;
  --sidebar-w: 260px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

.view { min-height: 100vh; }
.hidden { display: none !important; }

/* Login */
.login-card {
  max-width: 400px;
  margin: 12vh auto;
  padding: 2rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
}

.login-brand { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.25rem; }
.brand-icon {
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent-glow);
  border: 1px solid var(--accent);
  border-radius: 10px;
  font-size: 1.2rem;
}

h1 { font-size: 1.35rem; font-weight: 600; }
h2 { font-size: 1.05rem; font-weight: 600; }

.subtitle { color: var(--muted); font-size: 0.88rem; margin-bottom: 1.5rem; }

/* App layout */
#app-view {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
  transition: transform 0.25s ease;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1.25rem 1rem;
  border-bottom: 1px solid var(--border);
}

.brand-sub { display: block; font-size: 0.72rem; color: var(--muted); font-weight: 400; }

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem 0.5rem;
}

.nav-group { margin-bottom: 1rem; }

.nav-label {
  display: block;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--muted);
  padding: 0.35rem 0.75rem;
  margin-bottom: 0.25rem;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 0.88rem;
  cursor: pointer;
  text-align: left;
  transition: all 0.15s;
}

.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }

.nav-item:hover {
  background: var(--surface-2);
  color: var(--text);
}

.nav-item.active {
  background: var(--accent-glow);
  border-color: var(--accent);
  color: var(--text);
}

.nav-badge {
  margin-left: auto;
  font-size: 0.7rem;
  font-weight: 600;
  background: var(--border);
  color: var(--text);
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  min-width: 1.4rem;
  text-align: center;
}

.sidebar-footer {
  padding: 0.75rem 0.5rem 1rem;
  border-top: 1px solid var(--border);
}

.sidebar-status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.status-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--warning);
  flex-shrink: 0;
}

.status-dot.ok { background: var(--success); box-shadow: 0 0 6px var(--success); }
.status-dot.err { background: var(--error); box-shadow: 0 0 6px var(--error); }
.status-dot.warn { background: var(--warning); }

.logout-btn { color: var(--muted); }

.app-shell {
  flex: 1;
  margin-left: var(--sidebar-w);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 50;
}

.topbar h1 { font-size: 1.15rem; }
.topbar-sub { font-size: 0.8rem; color: var(--muted); }

.menu-toggle {
  display: none;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1.1rem;
}

.content {
  flex: 1;
  padding: 1.5rem;
  max-width: 1100px;
}

.page { display: none; }
.page.active { display: block; }

/* Cards & forms */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.25rem;
}

.page .card + .card { margin-top: 1rem; }

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.card-header h2 { margin-bottom: 0; }

.card-header-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.live-pill {
  display: inline-block;
  font-size: 0.62rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #22c55e;
  background: rgba(34, 197, 94, 0.12);
  border: 1px solid rgba(34, 197, 94, 0.35);
  border-radius: 999px;
  padding: 0.15rem 0.45rem;
  vertical-align: middle;
  margin-left: 0.35rem;
}

.dash-logs-scroll {
  max-height: 520px;
  overflow-y: auto;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 0.85rem;
}

input, textarea, select {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  color: var(--text);
  font-size: 0.92rem;
}

input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
}

button {
  border: none;
  border-radius: 8px;
  padding: 0.6rem 1.1rem;
  font-size: 0.88rem;
  cursor: pointer;
  font-weight: 500;
}

.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-secondary:hover { background: var(--border); }
.btn-sm { padding: 0.35rem 0.75rem; font-size: 0.8rem; }

.actions { display: flex; gap: 0.75rem; flex-wrap: wrap; align-items: center; margin-bottom: 0.5rem; }

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.toggles { display: flex; flex-wrap: wrap; gap: 1rem; margin-bottom: 0.75rem; }

.toggle {
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
  color: var(--text);
  margin-bottom: 0;
}

.toggle input { width: auto; accent-color: var(--accent); }

.hint { font-size: 0.8rem; color: var(--muted); margin-bottom: 0.75rem; }
.hint code { background: var(--bg); padding: 0.1rem 0.35rem; border-radius: 4px; }

.status-text { font-size: 0.85rem; color: var(--success); margin-top: 0.5rem; }
.error { color: var(--error); font-size: 0.85rem; margin-top: 0.5rem; }

.badge {
  margin-left: auto;
  font-size: 0.75rem;
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  background: var(--border);
  white-space: nowrap;
}

.badge.ok { background: rgba(34, 197, 94, 0.15); color: var(--success); }
.badge.warn { background: rgba(245, 158, 11, 0.15); color: var(--warning); }
.badge.err { background: rgba(239, 68, 68, 0.15); color: var(--error); }

/* Stats */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem 1.15rem;
}

.stat-label { display: block; font-size: 0.75rem; color: var(--muted); margin-bottom: 0.35rem; }
.stat-value { display: block; font-size: 1.35rem; font-weight: 600; }
.stat-sub { display: block; font-size: 0.75rem; color: var(--muted); margin-top: 0.15rem; }

.stat-value.ok { color: var(--success); }
.stat-value.warn { color: var(--warning); }
.stat-value.err { color: var(--error); }

.alert {
  padding: 0.75rem 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.88rem;
}

.alert-error {
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: #fca5a5;
}

/* Tables */
.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }

th, td {
  text-align: left;
  padding: 0.55rem 0.65rem;
  border-bottom: 1px solid var(--border);
}

th { color: var(--muted); font-weight: 500; font-size: 0.78rem; }

.event-received { color: #60a5fa; }
.event-scheduled { color: var(--warning); }
.event-sent { color: var(--success); }
.event-error { color: var(--error); }

#attachments-list { list-style: none; margin-top: 0.75rem; }

#attachments-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.65rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.88rem;
}

#attachments-list .att-actions { display: flex; gap: 0.5rem; }
#attachments-list button { padding: 0.3rem 0.6rem; font-size: 0.78rem; }

.card-sub {
  margin: 1rem 0;
  padding: 0.85rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.02);
}

.card-sub h3 { margin: 0 0 0.5rem; font-size: 0.95rem; }

.card-sub h4.qr-subheading {
  margin: 1rem 0 0.35rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
}

.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
}

@media (max-width: 768px) {
  .grid-3 { grid-template-columns: 1fr; }
}

.links-config-card label + label { margin-top: 0.85rem; }

.placeholder-reference-card .card-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.placeholder-search-wrap {
  display: block;
  margin: 0.75rem 0 1rem;
  max-width: 360px;
}

.placeholder-search-wrap input {
  width: 100%;
  margin-top: 0.35rem;
}

.placeholder-catalog {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.placeholder-group-title {
  margin: 0 0 0.25rem;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.placeholder-group-desc {
  margin: 0 0 0.65rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.placeholder-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.placeholder-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
}

.placeholder-table th,
.placeholder-table td {
  text-align: left;
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.placeholder-table th {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.03);
}

.placeholder-table tr:last-child td { border-bottom: none; }

.placeholder-tag {
  display: inline-block;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.82rem;
  color: #a5b4fc;
  background: rgba(79, 70, 229, 0.12);
  border: 1px solid rgba(79, 70, 229, 0.25);
  border-radius: 5px;
  padding: 0.2rem 0.45rem;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}

.placeholder-tag:hover {
  background: rgba(79, 70, 229, 0.22);
  border-color: rgba(79, 70, 229, 0.45);
}

.placeholder-tag.copied {
  background: rgba(34, 197, 94, 0.15);
  border-color: rgba(34, 197, 94, 0.4);
  color: #86efac;
}

.placeholder-use { color: var(--text); line-height: 1.45; }

.placeholder-example {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.78rem;
  color: var(--muted);
  word-break: break-all;
}

.placeholder-empty {
  padding: 1rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.88rem;
}

.live-console-card .card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.console-toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.console-status {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.75rem;
  color: #86efac;
  padding: 0.25rem 0.5rem;
  border: 1px solid rgba(34, 197, 94, 0.25);
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.25);
}

.console-status.warn { color: #fbbf24; border-color: rgba(251, 191, 36, 0.3); }
.console-status.error { color: #f87171; border-color: rgba(248, 113, 113, 0.3); }

.live-console {
  margin-top: 0.75rem;
  min-height: 420px;
  max-height: 65vh;
  overflow-y: auto;
  background: #1a1a1a;
  border: 1px solid #2d2d2d;
  border-radius: 8px;
  padding: 0.85rem 1rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Courier New", monospace;
  font-size: 0.82rem;
  line-height: 1.55;
}

.console-line {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0 0 0.15rem;
}

.console-system { color: #4ade80; }
.console-info { color: #86efac; }
.console-warn { color: #facc15; }
.console-error { color: #f87171; }
.console-received { color: #67e8f9; }
.console-scheduled { color: #fde047; }
.console-sent { color: #4ade80; }

.server-details {
  margin-bottom: 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem 0.85rem;
}

.server-details summary {
  cursor: pointer;
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 500;
}

.server-details[open] summary { margin-bottom: 0.75rem; }

/* Responsive */
@media (max-width: 900px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); }
  .app-shell { margin-left: 0; }
  .menu-toggle { display: block; }
  .grid-2 { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .mailbox-item { flex-direction: column; align-items: flex-start; }
}

.mailbox-list { display: flex; flex-direction: column; gap: 0.65rem; margin-bottom: 0.5rem; }

.mailbox-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.mailbox-item.disabled { opacity: 0.55; }

.mailbox-info { flex: 1; min-width: 0; }
.mailbox-info strong { display: block; font-size: 0.92rem; }
.mailbox-info span { font-size: 0.78rem; color: var(--muted); }
.mailbox-info .mb-err { color: var(--error); font-size: 0.75rem; margin-top: 0.2rem; }
.mailbox-info .mb-display-name { display: block; margin-top: 0.45rem; font-size: 0.78rem; color: var(--muted); }
.mailbox-info .mb-display-name input { margin-top: 0.2rem; width: 100%; max-width: 280px; }

.mailbox-actions { display: flex; gap: 0.5rem; flex-shrink: 0; }
.mailbox-actions button { padding: 0.35rem 0.65rem; font-size: 0.78rem; }

.btn-danger {
  background: transparent;
  border: 1px solid var(--error);
  color: #fca5a5;
}
.btn-danger:hover { background: rgba(239, 68, 68, 0.15); }

.status-pill {
  display: inline-block;
  font-size: 0.7rem;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  margin-left: 0.35rem;
  vertical-align: middle;
}
.status-pill.ok { background: rgba(34, 197, 94, 0.15); color: var(--success); }
.status-pill.off { background: rgba(245, 158, 11, 0.15); color: var(--warning); }

.placeholders-hint code { margin-right: 0.35rem; }

.builder-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 0.85rem;
}

.builder-editor textarea {
  font-family: "Cascadia Code", "Fira Code", Consolas, monospace;
  font-size: 0.82rem;
  line-height: 1.45;
  min-height: 380px;
  resize: vertical;
}

.builder-preview {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  min-height: 380px;
}

.preview-header {
  padding: 0.5rem 0.75rem;
  font-size: 0.78rem;
  font-weight: 600;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  color: var(--muted);
}

.preview-note { font-weight: 400; color: var(--muted); font-size: 0.75rem; }

#doc-preview {
  flex: 1;
  width: 100%;
  min-height: 340px;
  border: none;
  background: #fff;
}

.att-kind {
  font-size: 0.68rem;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  background: var(--accent-glow);
  color: var(--accent);
  margin-left: 0.35rem;
}

@media (max-width: 900px) {
  .builder-grid { grid-template-columns: 1fr; }
}

/* Toast notifications */
.toast-stack {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  pointer-events: none;
  width: min(92vw, 420px);
}

.toast {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.85rem 1.1rem;
  background: #1e1e1e;
  border: 1px solid #333;
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  color: #fff;
  font-size: 0.95rem;
  font-weight: 600;
  pointer-events: auto;
  animation: toast-in 0.28s ease-out;
}

.toast.leaving {
  animation: toast-out 0.22s ease-in forwards;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(12px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toast-out {
  from { opacity: 1; transform: translateY(0) scale(1); }
  to { opacity: 0; transform: translateY(8px) scale(0.96); }
}

.toast-icon {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
  font-style: normal;
}

.toast-info .toast-icon {
  background: transparent;
  border: 2px solid #38bdf8;
  color: #38bdf8;
}

.toast-success .toast-icon {
  background: #22c55e;
  border: none;
  color: #fff;
}

.toast-error .toast-icon {
  background: #ef4444;
  border: none;
  color: #fff;
}

.toast-spinner {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(56, 189, 248, 0.25);
  border-top-color: #38bdf8;
  border-radius: 50%;
  animation: toast-spin 0.7s linear infinite;
}

@keyframes toast-spin {
  to { transform: rotate(360deg); }
}

.toast-message { flex: 1; line-height: 1.3; }

/* Confirm modal */
.confirm-modal {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
}

.confirm-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(4, 8, 14, 0.72);
  backdrop-filter: blur(4px);
  opacity: 0;
  transition: opacity 0.22s ease;
}

.confirm-modal-card {
  position: relative;
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.75rem 1.75rem 1.5rem;
  text-align: center;
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.45);
  transform: scale(0.94) translateY(8px);
  opacity: 0;
  transition: transform 0.22s ease, opacity 0.22s ease;
}

.confirm-modal.open .confirm-modal-backdrop { opacity: 1; }
.confirm-modal.open .confirm-modal-card {
  transform: scale(1) translateY(0);
  opacity: 1;
}

.confirm-modal-icon {
  width: 52px;
  height: 52px;
  margin: 0 auto 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.35);
  color: #fbbf24;
}

.confirm-modal-icon svg {
  width: 26px;
  height: 26px;
}

.confirm-modal-icon.danger {
  background: rgba(239, 68, 68, 0.12);
  border-color: rgba(239, 68, 68, 0.35);
  color: #f87171;
}

.confirm-modal-title {
  font-size: 1.15rem;
  font-weight: 600;
  margin-bottom: 0.55rem;
  color: var(--text);
}

.confirm-modal-message {
  font-size: 0.9rem;
  color: var(--muted);
  line-height: 1.55;
  margin-bottom: 1.35rem;
}

.confirm-modal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
}

.confirm-modal-btn {
  width: 100%;
  justify-content: center;
}

.confirm-modal-btn.btn-danger {
  background: var(--error);
  border-color: var(--error);
  color: #fff;
}

.confirm-modal-btn.btn-danger:hover {
  background: #dc2626;
  border-color: #dc2626;
}

#login-mfa {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.security-setup {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.security-qr {
  display: block;
  width: 200px;
  height: 200px;
  margin: 0.75rem 0;
  border-radius: 8px;
  background: #fff;
  padding: 8px;
}

.security-secret code {
  font-size: 0.85rem;
  word-break: break-all;
}

.security-setup-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
}

.security-note {
  color: var(--success);
  margin-bottom: 0.75rem;
}

#page-security .card + .card {
  margin-top: 1rem;
}
