/* ===========================================================================
   ENS Platform — Design System
   Shared tokens, reset, and base components used across all pages.
   Page-specific styles (sidebar layouts, chat bubbles, etc.) remain inline.
   =========================================================================== */

/* ---------------------------------------------------------------------------
   DESIGN TOKENS
   --------------------------------------------------------------------------- */
:root {
  /* Spacing scale (4px base) */
  --sp-1: 4px;  --sp-2: 8px;   --sp-3: 12px;  --sp-4: 16px;
  --sp-5: 20px; --sp-6: 24px;  --sp-8: 32px;  --sp-10: 40px;
  --sp-12: 48px; --sp-16: 64px;

  /* Brand & semantic colors */
  --color-primary: #2b6cb0;
  --color-primary-hover: #2c5282;
  --color-primary-weak: #ebf8ff;
  --color-primary-text: #2a4365;

  --color-danger: #c53030;
  --color-danger-hover: #9b2c2c;
  --color-danger-weak: #fff5f5;
  --color-danger-text: #742a2a;

  --color-success: #22543d;
  --color-success-weak: #f0fff4;
  --color-success-border: #c6f6d5;

  --color-warning: #c05621;
  --color-warning-weak: #fffaf0;

  /* Surface & text */
  --color-bg: #f0f4f8;
  --color-surface: #ffffff;
  --color-surface-2: #f7fafc;
  --color-text: #2d3748;
  --color-text-muted: #718096;
  --color-text-subtle: #a0aec0;

  /* Border */
  --color-border: #e2e8f0;
  --color-border-strong: #cbd5e0;
  --color-border-focus: #4299e1;

  /* Topbar */
  --color-topbar-bg: #1a365d;
  --color-topbar-text: #bee3f8;
  --color-topbar-hover-bg: rgba(255, 255, 255, 0.12);
  --color-topbar-hover-text: #ffffff;

  /* Typography */
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                 "Helvetica Neue", Arial, sans-serif;
  --font-size-xs: 0.75rem;     /* 12px */
  --font-size-sm: 0.875rem;    /* 14px */
  --font-size-base: 0.9375rem; /* 15px */
  --font-size-lg: 1.0625rem;   /* 17px */
  --font-size-xl: 1.25rem;     /* 20px */
  --font-size-2xl: 1.5rem;     /* 24px */
  --font-weight-normal: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
  --font-weight-bold: 700;

  /* Elevation */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 30px rgba(0, 0, 0, 0.15);
  --shadow-topbar: 0 2px 8px rgba(0, 0, 0, 0.25);

  /* Radius */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-full: 9999px;

  /* Transitions */
  --trans-fast: 120ms ease;
  --trans-med: 200ms ease;

  /* Layout */
  --topbar-height: 56px;
  --sidebar-width: 280px;
}

/* Dark mode overrides */
[data-theme="dark"] {
  --color-bg: #0d1a2d;
  --color-surface: #1a2b42;
  --color-surface-2: #233951;
  --color-text: #e2e8f0;
  --color-text-muted: #a0aec0;
  --color-text-subtle: #718096;
  --color-border: #2d3748;
  --color-border-strong: #4a5568;
  --color-primary-weak: #1a365d;
  --color-primary-text: #bee3f8;
  --color-danger-weak: #3a1a1a;
  --color-danger-text: #feb2b2;
  --color-success-weak: #1a3a2a;
  --color-success-border: #2f4f3f;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 10px 30px rgba(0, 0, 0, 0.6);
}

/* ---------------------------------------------------------------------------
   RESET
   --------------------------------------------------------------------------- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: var(--font-family);
  font-size: 93.75%;
  color: var(--color-text);
  background: var(--color-bg);
}

/* SECURITY: hide body until JS auth gate verifies the session cookie.
   Prevents flash of protected content before redirect to /login.
   Individual pages can override this if they're public (e.g. login.html). */
body {
  visibility: hidden;
}
body.is-public {
  visibility: visible;
}

/* ---------------------------------------------------------------------------
   TOPBAR
   --------------------------------------------------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--topbar-height);
  padding: 0 var(--sp-5);
  background: var(--color-topbar-bg);
  color: var(--color-topbar-text);
  box-shadow: var(--shadow-topbar);
  position: relative;
  z-index: 100;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  font-weight: var(--font-weight-bold);
  font-size: var(--font-size-lg);
  color: var(--color-topbar-hover-text);
  text-decoration: none;
}

.topbar-brand svg {
  flex-shrink: 0;
}

.topbar-nav {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}

.topbar-link {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  color: var(--color-topbar-text);
  text-decoration: none;
  border-radius: var(--radius-sm);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  transition: background var(--trans-fast), color var(--trans-fast);
}

.topbar-link:hover {
  background: var(--color-topbar-hover-bg);
  color: var(--color-topbar-hover-text);
}

.topbar-link.is-active {
  background: var(--color-topbar-hover-bg);
  color: var(--color-topbar-hover-text);
}

/* ---------------------------------------------------------------------------
   BUTTONS
   --------------------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-5);
  font-family: inherit;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  line-height: 1.3;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-text);
  cursor: pointer;
  transition: background var(--trans-fast), color var(--trans-fast),
              border-color var(--trans-fast), box-shadow var(--trans-fast);
  white-space: nowrap;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
}
.btn-primary:hover:not(:disabled) {
  background: var(--color-primary-hover);
}

.btn-danger {
  background: var(--color-danger);
  color: #fff;
}
.btn-danger:hover:not(:disabled) {
  background: var(--color-danger-hover);
}

.btn-secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border-strong);
}
.btn-secondary:hover:not(:disabled) {
  background: var(--color-surface-2);
  border-color: var(--color-border-focus);
}

.btn-ghost {
  background: transparent;
  color: var(--color-text-muted);
}
.btn-ghost:hover:not(:disabled) {
  background: var(--color-surface-2);
  color: var(--color-text);
}

.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: var(--radius-sm);
}

/* Logout button — ghost style that goes slightly red on hover */
.btn-logout {
  background: transparent;
  color: var(--color-topbar-text);
  border: 1px solid rgba(255, 255, 255, 0.2);
  padding: var(--sp-1) var(--sp-3);
  font-size: var(--font-size-sm);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--trans-fast), color var(--trans-fast);
}
.btn-logout:hover {
  background: rgba(245, 101, 101, 0.15);
  color: #feb2b2;
  border-color: rgba(245, 101, 101, 0.35);
}

/* ---------------------------------------------------------------------------
   CARDS
   --------------------------------------------------------------------------- */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: var(--sp-6);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--sp-4);
}

.card-title {
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text);
}

.card-subtitle {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
}

/* ---------------------------------------------------------------------------
   FORMS
   --------------------------------------------------------------------------- */
.form-field {
  display: block;
  margin-bottom: var(--sp-4);
}

.form-field label {
  display: block;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  color: var(--color-text);
  margin-bottom: var(--sp-2);
}

.input,
.select,
.textarea {
  display: block;
  width: 100%;
  padding: var(--sp-2) var(--sp-3);
  font-family: inherit;
  font-size: var(--font-size-base);
  line-height: 1.4;
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  transition: border-color var(--trans-fast), box-shadow var(--trans-fast);
}

.input:focus,
.select:focus,
.textarea:focus {
  outline: none;
  border-color: var(--color-border-focus);
  box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.2);
}

.input::placeholder,
.textarea::placeholder {
  color: var(--color-text-subtle);
}

.textarea {
  min-height: 80px;
  resize: vertical;
}

.field-hint {
  font-size: var(--font-size-xs);
  color: var(--color-text-muted);
  margin-top: var(--sp-1);
}

.field-error {
  font-size: var(--font-size-xs);
  color: var(--color-danger);
  margin-top: var(--sp-1);
  display: none;
}

.field-error.is-visible {
  display: block;
}

/* ---------------------------------------------------------------------------
   MODAL
   --------------------------------------------------------------------------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-4);
  z-index: 1000;
}

.modal-overlay.hidden {
  display: none;
}

.modal-dialog {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--sp-6);
  max-width: 480px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-dialog h3 {
  font-size: var(--font-size-xl);
  font-weight: var(--font-weight-semibold);
  margin-bottom: var(--sp-2);
}

.modal-subtitle {
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  margin-bottom: var(--sp-5);
}

.modal-actions {
  display: flex;
  gap: var(--sp-2);
  justify-content: flex-end;
  margin-top: var(--sp-6);
}

/* ---------------------------------------------------------------------------
   TOASTS
   --------------------------------------------------------------------------- */
.toast-container {
  position: fixed;
  bottom: var(--sp-6);
  right: var(--sp-6);
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  z-index: 2000;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  padding: var(--sp-3) var(--sp-4);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  min-width: 200px;
  max-width: 400px;
  animation: toast-in 200ms ease;
}

.toast-success {
  background: var(--color-success-weak);
  color: var(--color-success);
  border: 1px solid var(--color-success-border);
}

.toast-error {
  background: var(--color-danger-weak);
  color: var(--color-danger);
  border: 1px solid #fed7d7;
}

.toast-info {
  background: var(--color-primary-weak);
  color: var(--color-primary-text);
  border: 1px solid #bee3f8;
}

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

/* ---------------------------------------------------------------------------
   UTILITIES
   --------------------------------------------------------------------------- */
.hidden { display: none !important; }
.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0);
  white-space: nowrap; border: 0;
}

.admin-only { display: none; }

/* ---------------------------------------------------------------------------
   RESPONSIVE
   --------------------------------------------------------------------------- */
@media (max-width: 768px) {
  .topbar {
    padding: 0 var(--sp-3);
  }
  .topbar-link {
    padding: var(--sp-2);
  }
  .modal-dialog {
    padding: var(--sp-4);
  }
  .toast-container {
    bottom: var(--sp-3);
    right: var(--sp-3);
    left: var(--sp-3);
  }
  .toast {
    max-width: 100%;
  }
}
