:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e4e6ea;
  --text: #16181d;
  --text-muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #e8f0fe;
  --success: #157347;
  --success-soft: #e6f4ea;
  --danger: #b02a37;
  --danger-soft: #fbe9e9;
  --accent: #7c3aed;
  --accent-soft: #f0e9fd;
  --warning: #b45309;
  --warning-soft: #fdf1de;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0f12;
    --surface: #16181d;
    --surface-2: #1b1e24;
    --border: #2a2d34;
    --text: #e7e9ec;
    --text-muted: #9099a5;
    --primary: #5b8dfa;
    --primary-hover: #7ba1fb;
    --primary-soft: #182338;
    --success: #4ade80;
    --success-soft: #163224;
    --danger: #f87171;
    --danger-soft: #3a1e20;
    --accent: #a78bfa;
    --accent-soft: #261f3d;
    --warning: #fbbf24;
    --warning-soft: #3a2e10;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

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

.sidebar {
  width: 250px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1.5rem 1rem;
  display: flex;
  flex-direction: column;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  font-size: 1.05rem;
  margin-bottom: 1.5rem;
  padding: 0 0.5rem;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  text-align: left;
  padding: 0.6rem 0.7rem;
  margin-bottom: 0.25rem;
  border: none;
  background: none;
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: 0.92rem;
  cursor: pointer;
}

.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }

.nav-section-label {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  padding: 0 0.7rem;
  margin: 1rem 0 0.4rem;
}
.nav-section-label:first-child { margin-top: 0; }

main {
  flex: 1;
  padding: 2.25rem 3rem;
  max-width: 1280px;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 1.5rem;
}

.topbar h1 { font-size: 1.4rem; margin: 0; }
.topbar-subtitle { font-size: 0.9rem; color: var(--text-muted); margin: 0.35rem 0 0; }

#loading-bar {
  position: fixed;
  top: 0; left: 0;
  height: 3px;
  width: 100%;
  background: var(--primary);
  opacity: 0;
  transition: opacity 150ms ease;
  z-index: 100;
}
.htmx-request#loading-bar,
.htmx-request #loading-bar {
  opacity: 1;
  animation: loading-sweep 1s ease-in-out infinite;
}
@keyframes loading-sweep {
  0% { transform: scaleX(0.1); transform-origin: left; }
  50% { transform: scaleX(1); transform-origin: left; }
  51% { transform-origin: right; }
  100% { transform: scaleX(0.1); transform-origin: right; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  margin-bottom: 1.25rem;
}

.card h2 { margin: 0 0 1rem; font-size: 1.05rem; }
.card h3 { margin: 0 0 1rem; font-size: 0.95rem; }
.card h4 { margin: 1rem 0 0.75rem; font-size: 0.88rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; }

table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  padding: 0 0.6rem 0.6rem;
  border-bottom: 1px solid var(--border);
}
td { padding: 0.65rem 0.6rem; border-bottom: 1px solid var(--border); font-size: 0.92rem; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge-active { background: var(--success-soft); color: var(--success); }
.badge-suspended { background: var(--danger-soft); color: var(--danger); }
.badge-pending { background: var(--warning-soft); color: var(--warning); }
.badge-admin { background: var(--accent-soft); color: var(--accent); }
.badge-manager { background: var(--primary-soft); color: var(--primary); }
.badge-client { background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border); }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: 3px solid var(--tone, var(--border));
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.3rem;
}
.stat-tile.tone-primary { --tone: var(--primary); }
.stat-tile.tone-success { --tone: var(--success); }
.stat-tile.tone-danger { --tone: var(--danger); }
.stat-tile.tone-neutral { --tone: var(--border); }
.stat-tile .stat-label {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}
.stat-tile .stat-value {
  font-size: 2rem;
  font-weight: 600;
  font-variant-numeric: proportional-nums;
  line-height: 1;
  color: var(--text);
}

.table-scroll { overflow-x: auto; }

button {
  font-family: inherit;
  font-size: 0.88rem;
  cursor: pointer;
}

.btn {
  padding: 0.5rem 0.9rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
}
.btn:hover { background: var(--border); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--danger-soft); border-color: var(--danger-soft); color: var(--danger); }
.btn-danger:hover { filter: brightness(0.95); }
.btn-sm { padding: 0.3rem 0.65rem; font-size: 0.8rem; }

.btn-link {
  background: none;
  border: none;
  color: var(--primary);
  padding: 0;
  font-weight: 500;
}
.btn-link:hover { text-decoration: underline; }

.chip {
  padding: 0.35rem 0.8rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  margin-right: 0.4rem;
  font-size: 0.85rem;
}
.chip:hover { background: var(--border); }
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }

form.inline {
  display: flex;
  gap: 0.6rem;
  margin-top: 1.25rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
}
form.inline input {
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.88rem;
}
form.inline input[name="email"] { flex: 1; }
form.inline input[name="quota"] { width: 100px; }

section.card > h3 {
  margin-top: 1.25rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
}

.form-panel {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 1rem;
  align-items: end;
}
.form-field { display: flex; flex-direction: column; gap: 0.4rem; min-width: 0; }
.form-field.span-2 { grid-column: span 2; }
.form-field label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.form-field input,
.form-field select,
.form-field textarea {
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  width: 100%;
}
.form-field textarea { resize: vertical; }

/* Compose lives in a full-width card, not a narrow modal, so it needs a
   fixed column count instead of .form-panel's auto-fit(minmax(170px, 1fr))
   — on a wide container that produces far more columns than fields, and
   "span-2" (2 of many columns) no longer reads as "half the row". */
.compose-form { grid-template-columns: 1fr 1fr; }
.compose-form .form-field.span-2 { grid-column: 1 / -1; }
.field-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
}
.form-actions {
  grid-column: 1 / -1;
  display: flex;
  justify-content: flex-end;
  margin-top: 0.25rem;
}

.input-prefix-group {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}
.input-prefix-group input {
  border: none;
  border-radius: 0;
  flex: 1;
  min-width: 0;
  padding: 0.55rem 0.5rem;
  background: transparent;
  color: var(--text);
  font-size: 0.9rem;
}
.input-prefix-group input:focus { outline: none; }
.input-prefix,
.input-suffix {
  display: flex;
  align-items: center;
  padding: 0 0.6rem;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 0.85rem;
  white-space: nowrap;
}
.input-prefix { border-right: 1px solid var(--border); }
.input-suffix { border-left: 1px solid var(--border); }

.search-bar {
  display: flex;
  gap: 0.6rem;
  margin-bottom: 1.25rem;
}
.search-bar .search-input,
.search-bar select {
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.88rem;
}
.search-bar .search-input { flex: 1; }

td.actions { display: flex; align-items: center; gap: 0.4rem; white-space: nowrap; }
td.actions .btn { margin-right: 0; }

.action-menu { position: relative; display: inline-block; }
.action-menu > summary.action-menu-trigger {
  list-style: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1.1rem;
  line-height: 1;
}
.action-menu > summary.action-menu-trigger::-webkit-details-marker,
.action-menu > summary.action-menu-trigger::marker { display: none; content: ""; }
.action-menu > summary.action-menu-trigger:hover { background: var(--surface-2); }
.action-menu[open] > summary.action-menu-trigger { background: var(--surface-2); border-color: var(--primary); }

.action-menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 0.3rem);
  min-width: 190px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(15, 17, 21, 0.18);
  padding: 0.35rem;
  z-index: 20;
  display: flex;
  flex-direction: column;
}

.action-menu-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.45rem 0.6rem;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 0.85rem;
  text-align: left;
  border-radius: 6px;
  cursor: pointer;
}
.action-menu-item:hover { background: var(--surface-2); }
.action-menu-item.warn { color: var(--warning); }
.action-menu-item.danger { color: var(--danger); }

.bulk-toolbar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.85rem;
}
.bulk-toolbar .bulk-count {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-right: 0.4rem;
}
.bulk-toolbar .btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.edit-form {
  display: flex;
  gap: 0.5rem;
}
.edit-form input,
.edit-form select {
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.85rem;
}
.edit-form input[name="email"] { flex: 1; min-width: 160px; }

.msg-row td:first-child { font-weight: 500; }
.msg-unread { position: relative; }
.msg-unread td:first-child::before {
  content: "";
  display: inline-block;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--primary);
  margin-right: 0.5rem;
}
.empty-state { color: var(--text-muted); font-size: 0.88rem; padding: 0.5rem 0; }

.message-detail {
  margin-top: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  padding: 1.25rem 1.5rem;
}
.message-detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.5rem;
}
.message-detail-header h4 { margin: 0; font-size: 1.05rem; color: var(--text); }
.message-detail-meta {
  font-size: 0.85rem;
  color: var(--text-muted);
  padding-bottom: 0.9rem;
  margin-bottom: 0.9rem;
  border-bottom: 1px solid var(--border);
}
.message-detail-body {
  white-space: pre-wrap;
  font-size: 0.92rem;
  line-height: 1.6;
  color: var(--text);
}
.message-detail-actions {
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

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

.user-info {
  margin-bottom: 0.75rem;
  padding: 0 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.user-avatar {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--primary-soft);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
}

.user-info-text {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.user-email {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-info-text .badge { align-self: flex-start; }

.logout-btn:hover {
  background: var(--danger-soft) !important;
  color: var(--danger) !important;
}

.error-toast {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 1000;
  max-width: 420px;
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  padding: 0.75rem 0.9rem;
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: 0.88rem;
  line-height: 1.4;
}
.modal {
  border: none;
  padding: 0;
  width: min(560px, 92vw);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.modal::backdrop { background: rgba(15, 17, 21, 0.5); }
.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
}
.modal-header-title { display: flex; gap: 0.75rem; align-items: flex-start; }
.modal-header-title h3 { margin: 0; font-size: 1.05rem; }
.modal-header-title p { margin: 0.3rem 0 0; font-size: 0.85rem; color: var(--text-muted); }
.modal-icon {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary-soft);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
}
.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.1rem;
  line-height: 1;
  padding: 0.25rem;
  cursor: pointer;
}
.modal-close:hover { color: var(--text); }
.modal-body { padding: 1.5rem; }
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border);
}

.row-with-avatar { display: flex; align-items: center; gap: 0.6rem; }

.folder-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.25rem;
}
.folder-header-title { display: flex; align-items: center; gap: 0.75rem; }
.folder-header-title h3 { margin: 0; }

.tab-bar {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1rem;
}
.tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  padding: 0.7rem 0;
  color: var(--text-muted);
  font-size: 0.9rem;
  cursor: pointer;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.tab-bar-spacer { flex: 1; }

.dashboard-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1.25rem;
}

.activity-feed { list-style: none; margin: 0; padding: 0; }
.activity-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0;
  border-bottom: 1px solid var(--border);
}
.activity-item:last-child { border-bottom: none; }
.activity-icon {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.activity-icon::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.activity-icon-mailbox { background: var(--success-soft); color: var(--success); }
.activity-icon-sent { background: var(--primary-soft); color: var(--primary); }
.activity-icon-user { background: var(--accent-soft); color: var(--accent); }
.activity-body { flex: 1; min-width: 0; }
.activity-title { font-size: 0.9rem; font-weight: 600; }
.activity-detail {
  font-size: 0.82rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.activity-time {
  flex-shrink: 0;
  font-size: 0.78rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.storage-bar {
  display: flex;
  height: 10px;
  border-radius: 999px;
  overflow: hidden;
  background: var(--surface-2);
  margin-bottom: 0.85rem;
}
.storage-segment { height: 100%; }
.storage-total { font-size: 0.9rem; font-weight: 600; margin-bottom: 0.85rem; }
.storage-total-note { font-weight: 400; color: var(--text-muted); font-size: 0.8rem; }

.storage-legend { list-style: none; margin: 0; padding: 0; }
.storage-legend li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.35rem 0;
  font-size: 0.85rem;
}
.legend-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.legend-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend-value { color: var(--text-muted); flex-shrink: 0; }

.storage-segment.tone-1, .legend-dot.tone-1 { background: var(--primary); }
.storage-segment.tone-2, .legend-dot.tone-2 { background: var(--success); }
.storage-segment.tone-3, .legend-dot.tone-3 { background: var(--accent); }
.storage-segment.tone-4, .legend-dot.tone-4 { background: var(--warning); }
.storage-segment.tone-5, .legend-dot.tone-5 { background: var(--danger); }
.storage-segment.tone-other, .legend-dot.tone-other { background: var(--text-muted); }

.error-toast[hidden] { display: none; }
.error-toast-message { flex: 1; overflow-wrap: anywhere; }
.error-toast-close {
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0;
}

