/* ============================================================
   DESIGN TOKENS
   ============================================================ */
:root {
  color-scheme: dark;

  /* Fonts */
  --font-sans: 'Geist', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'Geist Mono', ui-monospace, 'SF Mono', 'Cascadia Code', monospace;

  /* Backgrounds — neutral graphite, darkest at the edges */
  --bg-base:    #0A0B0E;
  --bg-card:    #111318;
  --bg-surface: #191C23;
  --bg-hover:   #21242E;

  /* Borders — hairlines, one gray family */
  --border:        #262A33;
  --border-subtle: #1A1D24;
  --border-strong: #333845;

  /* Text */
  --text:        #E9EBF1;
  --text-muted:  #99A0B0;
  --text-subtle: #5D6374;

  /* Accent — one accent, iris */
  --accent:       #7C89F9;
  --accent-hover: #6875F2;
  --accent-deep:  #5560E8;
  --accent-glow:  rgba(124, 137, 249, 0.16);
  --accent-text:  #A5AEFB;

  /* Status */
  --green:        #4ADE80;
  --green-bg:     rgba(74,  222, 128, 0.10);
  --green-border: rgba(74,  222, 128, 0.22);
  --red:          #F87171;
  --red-bg:       rgba(248, 113, 113, 0.10);
  --red-border:   rgba(248, 113, 113, 0.22);
  --yellow:       #FBBF24;
  --yellow-bg:    rgba(251, 191, 36,  0.10);
  --yellow-border: rgba(251, 191, 36, 0.22);
  --blue:         #60A5FA;
  --blue-bg:      rgba(96,  165, 250, 0.10);
  --blue-border:  rgba(96,  165, 250, 0.22);

  /* Sidebar */
  --sidebar-width: 236px;

  /* Radius */
  --r-sm: 6px;
  --r:    8px;
  --r-lg: 12px;
  --r-xl: 16px;

  /* Shadows — tinted to the background, single light source above */
  --shadow-sm: 0 1px 2px rgba(4, 5, 8, .5);
  --shadow:    0 4px 16px rgba(4, 5, 8, .45);
  --shadow-lg: 0 24px 64px -12px rgba(4, 5, 8, .8), 0 4px 16px rgba(4, 5, 8, .5);

  /* Transitions */
  --t: 160ms cubic-bezier(.4, 0, .2, 1);
  --t-spring: 350ms cubic-bezier(.16, 1, .3, 1);

  /* Keep legacy names for compat */
  --primary-color:   var(--accent);
  --primary-hover:   var(--accent-hover);
  --secondary-color: var(--text-subtle);
  --success-color:   var(--green);
  --danger-color:    var(--red);
  --warning-color:   var(--yellow);
  --background:      var(--bg-base);
  --surface:         var(--bg-card);
  --text-primary:    var(--text);
  --text-secondary:  var(--text-muted);
  --border-color:    var(--border);
  --sidebar-bg:      var(--bg-base);
  --sidebar-text:    var(--text-muted);
}

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

html { font-size: 14px; }

body {
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg-base);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

::selection {
  background: rgba(124, 137, 249, .32);
  color: var(--text);
}

/* ============================================================
   SCROLLBAR
   ============================================================ */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 99px;
  border: 2px solid transparent;
  background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--border-strong); background-clip: padding-box; }

/* ============================================================
   TYPOGRAPHY SCALE
   ============================================================ */
h1, h2, h3, h4, h5, h6 {
  line-height: 1.3;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
  text-wrap: balance;
}
p { color: var(--text-muted); }
a { color: var(--accent-text); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 3px; }

/* ============================================================
   FOCUS STATES (accessibility)
   ============================================================ */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--r-sm);
}

/* ============================================================
   LOGIN PAGE
   ============================================================ */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  background: var(--bg-base);
  background-image:
    radial-gradient(ellipse 65% 45% at 50% -8%, rgba(124, 137, 249, .13) 0%, transparent 70%),
    radial-gradient(ellipse 40% 30% at 85% 110%, rgba(124, 137, 249, .05) 0%, transparent 70%);
  position: relative;
}

/* Faint grain so the dark field doesn't read as flat vector */
.login-page::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: .028;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)'/%3E%3C/svg%3E");
}

.login-container {
  width: 100%;
  max-width: 400px;
  padding: 24px;
  position: relative;
}

.login-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  margin-bottom: 32px;
}

.login-logo-icon {
  width: 52px;
  height: 52px;
  background: linear-gradient(180deg, rgba(124, 137, 249, .22), rgba(124, 137, 249, .08));
  border: 1px solid rgba(124, 137, 249, .35);
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent-text);
  box-shadow: 0 0 32px rgba(124, 137, 249, .22), inset 0 1px 0 rgba(255, 255, 255, .1);
}

.login-title {
  font-size: 21px;
  font-weight: 650;
  letter-spacing: -0.02em;
  text-align: center;
}

.login-subtitle {
  font-size: 13px;
  text-align: center;
  margin-top: 2px;
  color: var(--text-muted);
}

.login-logo h1 {
  font-size: 21px;
  font-weight: 650;
  letter-spacing: -0.02em;
  color: var(--text);
}

.login-logo p {
  font-size: 13px;
  color: var(--text-muted);
}

.login-card {
  background: linear-gradient(180deg, rgba(255, 255, 255, .025), transparent 45%), var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: 32px;
  box-shadow: var(--shadow-lg), inset 0 1px 0 rgba(255, 255, 255, .04);
}

.login-header {
  margin-bottom: 28px;
}

.login-header h2 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 4px;
}

.login-header p {
  font-size: 13px;
  color: var(--text-muted);
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* ============================================================
   FORM ELEMENTS
   ============================================================ */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: 0.01em;
}

.form-group input,
.form-group select,
.form-group textarea {
  padding: 9px 12px;
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: var(--r);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  transition: border-color var(--t), box-shadow var(--t), background var(--t);
}

.form-group input:hover:not(:focus):not([disabled]),
.form-group select:hover:not(:focus),
.form-group textarea:hover:not(:focus) {
  border-color: var(--border-strong);
}

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

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--bg-card);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.form-group input[disabled],
.form-group input[readonly] {
  opacity: 0.55;
  cursor: not-allowed;
}

.form-group select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' fill='none' viewBox='0 0 24 24' stroke='%2399A0B0' stroke-width='2.5'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19 9l-7 7-7-7'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}

.form-group select option {
  background: var(--bg-card);
  color: var(--text);
}

.form-group textarea {
  resize: vertical;
  min-height: 80px;
  line-height: 1.5;
}

.form-group small {
  font-size: 12px;
  color: var(--text-subtle);
  line-height: 1.4;
}

/* Checkbox styling */
.form-group input[type="checkbox"],
input[type="checkbox"],
input[type="radio"] {
  accent-color: var(--accent);
}

.form-group input[type="checkbox"] {
  width: 16px;
  height: 16px;
  padding: 0;
  cursor: pointer;
  border-radius: 4px;
}

.form-group label:has(input[type="checkbox"]) {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: var(--text);
}

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 8px 14px;
  border: 1px solid transparent;
  border-radius: var(--r);
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: background var(--t), border-color var(--t), color var(--t),
              box-shadow var(--t), transform 80ms ease;
  white-space: nowrap;
  user-select: none;
}

.btn:active:not(:disabled) {
  transform: translateY(1px) scale(.99);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

.btn-primary {
  background: linear-gradient(180deg, var(--accent) 0%, var(--accent-deep) 100%);
  color: #fff;
  border-color: var(--accent-deep);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .18), 0 1px 2px rgba(4, 5, 8, .5);
}
.btn-primary:hover:not(:disabled) {
  background: linear-gradient(180deg, #8A96FA 0%, var(--accent-hover) 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .18),
              0 1px 2px rgba(4, 5, 8, .5),
              0 0 0 3px var(--accent-glow);
}

.btn-secondary {
  background: var(--bg-surface);
  color: var(--text-muted);
  border-color: var(--border);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .03);
}
.btn-secondary:hover:not(:disabled) {
  background: var(--bg-hover);
  color: var(--text);
  border-color: var(--border-strong);
}

.btn-danger {
  background: var(--red-bg);
  color: var(--red);
  border-color: var(--red-border);
}
.btn-danger:hover:not(:disabled) {
  background: rgba(248, 113, 113, .18);
  border-color: rgba(248, 113, 113, .4);
}

.btn-success {
  background: var(--green-bg);
  color: var(--green);
  border-color: var(--green-border);
}
.btn-success:hover:not(:disabled) {
  background: rgba(74, 222, 128, .18);
}

.btn-small {
  padding: 4px 10px;
  font-size: 12px;
  border-radius: var(--r-sm);
}

.btn-block {
  width: 100%;
  padding: 10px 16px;
  font-size: 14px;
}

.btn-icon {
  padding: 7px;
  border-radius: var(--r);
  background: transparent;
  border-color: transparent;
  color: var(--text-muted);
}
.btn-icon:hover {
  background: var(--bg-surface);
  color: var(--text);
}

/* Sibling buttons inside table cells & footers breathe */
td .btn + .btn { margin-left: 6px; }

/* Action cells keep their buttons on one line */
td:has(.btn) { white-space: nowrap; }

/* Buttons placed inside stacked form groups shouldn't stretch */
.form-group > .btn,
.condition-builder > .btn { align-self: flex-start; }

/* ============================================================
   MESSAGES
   ============================================================ */
.error-message {
  padding: 11px 14px;
  background: var(--red-bg);
  border: 1px solid var(--red-border);
  border-radius: var(--r);
  color: var(--red);
  font-size: 13px;
  line-height: 1.5;
}

.success-message {
  padding: 11px 14px;
  background: var(--green-bg);
  border: 1px solid var(--green-border);
  border-radius: var(--r);
  color: var(--green);
  font-size: 13px;
}

/* ============================================================
   DASHBOARD LAYOUT
   ============================================================ */
.dashboard {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--bg-base);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 18px 16px 16px;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  gap: 10px;
}

.sidebar-logo-icon {
  width: 32px;
  height: 32px;
  background: linear-gradient(180deg, rgba(124, 137, 249, .22), rgba(124, 137, 249, .07));
  border: 1px solid rgba(124, 137, 249, .32);
  border-radius: var(--r);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent-text);
  flex-shrink: 0;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .08);
}

.sidebar-header-text {
  min-width: 0;
}

.sidebar-header-text h2 {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-header-text span {
  font-size: 11px;
  color: var(--text-subtle);
  display: block;
}

.nav-menu {
  flex: 1;
  list-style: none;
  padding: 12px 10px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.nav-link {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--r);
  color: var(--text-muted);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  transition: background var(--t), color var(--t);
  cursor: pointer;
}

.nav-link:hover {
  background: rgba(255, 255, 255, .035);
  color: var(--text);
  text-decoration: none;
}

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

.nav-link.active::before {
  content: '';
  position: absolute;
  left: -10px;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 16px;
  border-radius: 0 3px 3px 0;
  background: var(--accent);
}

.nav-link svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  opacity: 0.75;
  transition: opacity var(--t);
}

.nav-link:hover svg,
.nav-link.active svg {
  opacity: 1;
}

.sidebar-section-label {
  font-size: 10.5px;
  font-weight: 600;
  color: var(--text-subtle);
  letter-spacing: 0.09em;
  text-transform: uppercase;
  padding: 14px 10px 5px;
  user-select: none;
}

.sidebar-section-label:first-child {
  padding-top: 4px;
}

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border-subtle);
}

/* ── Main Content ────────────────────────────────────────── */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
  background: var(--bg-base);
  background-image:
    radial-gradient(ellipse 70% 320px at 50% -80px, rgba(124, 137, 249, .06) 0%, transparent 70%);
}

.content-header {
  padding: 16px 28px;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.content-header-left-group {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

/* Hamburger — desktop hides it, the drawer breakpoint shows it */
.menu-button {
  display: none;
  border: 1px solid var(--border);
  background: var(--bg-card);
  flex-shrink: 0;
}

.sidebar-scrim {
  position: fixed;
  inset: 0;
  background: rgba(4, 5, 8, .6);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: 150;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--t);
}

body.sidebar-open .sidebar-scrim {
  opacity: 1;
  pointer-events: auto;
}

.content-header h1 {
  font-size: 19px;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.content-header-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.content-header-subtitle {
  font-size: 12px;
  color: var(--text-subtle);
}

#pageActions {
  display: flex;
  gap: 8px;
}

.content-body {
  flex: 1;
  padding: 28px;
  overflow-y: auto;
}

.content-body > * {
  max-width: 1280px;
  margin-left: auto;
  margin-right: auto;
}

/* ============================================================
   STATS CARDS
   ============================================================ */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

.stat-card {
  background: linear-gradient(180deg, rgba(255, 255, 255, .022), transparent 55%), var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 18px 20px;
  position: relative;
  overflow: hidden;
  transition: border-color var(--t), box-shadow var(--t), transform var(--t);
  box-shadow: var(--shadow-sm);
}

.stat-card:hover {
  border-color: var(--border-strong);
  transform: translateY(-1px);
  box-shadow: var(--shadow);
}

.stat-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.stat-icon {
  width: 34px;
  height: 34px;
  border-radius: var(--r);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .05);
}

.stat-icon-blue   { background: var(--blue-bg);   color: var(--blue); }
.stat-icon-green  { background: var(--green-bg);  color: var(--green); }
.stat-icon-yellow { background: var(--yellow-bg); color: var(--yellow); }
.stat-icon-red    { background: var(--red-bg);    color: var(--red); }

.stat-label {
  font-size: 11.5px;
  font-weight: 500;
  color: var(--text-subtle);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  margin-bottom: 6px;
}

.stat-value {
  font-size: 29px;
  font-weight: 650;
  color: var(--text);
  letter-spacing: -0.03em;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.stat-change {
  font-size: 12px;
  margin-top: 8px;
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--text-subtle);
}

.stat-change.positive { color: var(--green); }
.stat-change.negative { color: var(--red); }

/* ============================================================
   TABLE COMPONENTS
   ============================================================ */
.table-container {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

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

.table-scroll table {
  min-width: 700px;
}

.table-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  background: linear-gradient(180deg, rgba(255, 255, 255, .015), transparent);
}

.table-header h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}

.table-header h3 .count-badge {
  font-size: 11px;
  font-weight: 550;
  font-variant-numeric: tabular-nums;
  background: var(--bg-surface);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 1px 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
}

thead {
  background: var(--bg-base);
}

th {
  padding: 9px 16px;
  text-align: left;
  font-size: 11px;
  font-weight: 550;
  color: var(--text-subtle);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-subtle);
  font-size: 13px;
  color: var(--text);
  vertical-align: middle;
  font-variant-numeric: tabular-nums;
}

td strong { font-weight: 550; }

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

tbody tr {
  transition: background var(--t);
}

tbody tr:hover {
  background: rgba(255, 255, 255, .022);
}

/* ============================================================
   BADGES
   ============================================================ */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 550;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.badge::before {
  content: '';
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
  flex-shrink: 0;
}

.badge-success {
  background: var(--green-bg);
  color: var(--green);
  border: 1px solid var(--green-border);
}

.badge-danger {
  background: var(--red-bg);
  color: var(--red);
  border: 1px solid var(--red-border);
}

.badge-warning {
  background: var(--yellow-bg);
  color: var(--yellow);
  border: 1px solid var(--yellow-border);
}

.badge-info {
  background: var(--blue-bg);
  color: var(--blue);
  border: 1px solid var(--blue-border);
}

/* ============================================================
   MODAL
   ============================================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(4, 5, 8, .72);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
  animation: fade-in 160ms ease;
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-xl);
  width: 100%;
  max-width: 580px;
  max-height: 90dvh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg), inset 0 1px 0 rgba(255, 255, 255, .05);
  animation: modal-in var(--t-spring);
}

@keyframes modal-in {
  from { transform: translateY(14px) scale(.975); opacity: 0; }
  to   { transform: translateY(0)    scale(1);    opacity: 1; }
}

.modal-header {
  padding: 16px 22px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  background: var(--bg-card);
  z-index: 1;
}

.modal-header h2 {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 8px;
  border-radius: var(--r-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--t), color var(--t);
  line-height: 1.3;
  font-size: 20px;
}

.modal-close:hover {
  background: var(--bg-surface);
  color: var(--text);
}

.modal-body {
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-body hr {
  border: none;
  border-top: 1px solid var(--border) !important;
  margin: 6px 0 !important;
}

.modal-footer {
  padding: 14px 22px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  background: var(--bg-card);
  position: sticky;
  bottom: 0;
  z-index: 1;
}

/* Confirm dialog */
.confirm-modal {
  max-width: 420px;
}

.confirm-modal .modal-body {
  gap: 12px;
}

.confirm-description {
  font-size: 14px;
  color: var(--text-muted);
  line-height: 1.6;
}

/* ============================================================
   FILTERS
   ============================================================ */
.filters {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 14px 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow-sm);
}

.filters-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 240px));
  gap: 12px;
}

.filters-grid:not(:last-child) {
  margin-bottom: 12px;
}

.filters-actions {
  display: flex;
  gap: 8px;
}

/* ============================================================
   CONDITION BUILDER
   ============================================================ */
.condition-builder {
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: var(--r);
  padding: 12px;
}

.condition-row {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--r);
  padding: 8px;
}

.condition-row input,
.condition-row select {
  padding: 6px 10px;
  background: var(--bg-base);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
  transition: border-color var(--t), box-shadow var(--t);
}

.condition-row input:focus,
.condition-row select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

/* ============================================================
   SPINNER & LOADING
   ============================================================ */
.spinner {
  display: inline-block;
  width: 15px;
  height: 15px;
  border: 2px solid rgba(255, 255, 255, .2);
  border-radius: 50%;
  border-top-color: currentColor;
  animation: spin 0.6s linear infinite;
  flex-shrink: 0;
}

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

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 72px 20px;
  color: var(--text-subtle);
  font-size: 13px;
  animation: loading-pulse 1.6s ease-in-out infinite;
}

@keyframes loading-pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: .55; }
}

.loading::before {
  content: '';
  display: block;
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

/* ============================================================
   EMPTY STATE
   ============================================================ */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 64px 24px;
  gap: 8px;
}

.empty-state-icon {
  width: 52px;
  height: 52px;
  background: linear-gradient(180deg, var(--bg-hover), var(--bg-surface));
  border: 1px solid var(--border);
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-subtle);
  margin-bottom: 8px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .04), var(--shadow-sm);
}

.empty-state h3 {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 2px;
}

.empty-state p {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 16px;
  max-width: 340px;
  text-wrap: pretty;
}

/* ============================================================
   PAGINATION
   ============================================================ */
.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.pagination-info {
  font-size: 12px;
  color: var(--text-subtle);
  font-variant-numeric: tabular-nums;
}

.pagination-controls {
  display: flex;
  gap: 6px;
}

/* ============================================================
   TOAST NOTIFICATIONS
   ============================================================ */
#toastContainer {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 14px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg), inset 0 1px 0 rgba(255, 255, 255, .05);
  max-width: 360px;
  min-width: 260px;
  pointer-events: all;
  opacity: 0;
  transform: translateX(24px) scale(.98);
  transition: opacity var(--t-spring), transform var(--t-spring);
}

.toast.toast-visible {
  opacity: 1;
  transform: translateX(0) scale(1);
}

.toast-icon {
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  margin-top: 1px;
}

.toast-body {
  flex: 1;
  min-width: 0;
}

.toast-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 2px;
}

.toast-message {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.4;
  word-break: break-word;
}

.toast-close {
  background: none;
  border: none;
  color: var(--text-subtle);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 0;
  flex-shrink: 0;
  transition: color var(--t);
}
.toast-close:hover { color: var(--text); }

.toast-success { border-left: 3px solid var(--green); }
.toast-error   { border-left: 3px solid var(--red); }
.toast-warning { border-left: 3px solid var(--yellow); }
.toast-info    { border-left: 3px solid var(--blue); }

.toast-success .toast-icon { color: var(--green); }
.toast-error   .toast-icon { color: var(--red); }
.toast-warning .toast-icon { color: var(--yellow); }
.toast-info    .toast-icon { color: var(--blue); }

/* ============================================================
   CODE / MONOSPACE
   ============================================================ */
.code {
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--bg-surface);
  color: var(--accent-text);
  border: 1px solid var(--border);
  padding: 1px 6px;
  border-radius: var(--r-sm);
  white-space: nowrap;
}

pre.code {
  white-space: pre;
  overflow-x: auto;
  padding: 12px;
  font-size: 12px;
  line-height: 1.6;
  border-radius: var(--r);
  color: var(--text-muted);
  max-height: 240px;
}

/* ============================================================
   METADATA CHIPS (users & devices tables)
   ============================================================ */
.meta-chips {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
}

.meta-chips .code {
  font-size: 11px;
  color: var(--text-muted);
}

/* ============================================================
   SHARE BAR (failure breakdown)
   ============================================================ */
.share-bar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 18px;
  min-width: 120px;
}

.share-bar-fill {
  height: 6px;
  min-width: 2px;
  max-width: calc(100% - 48px);
  border-radius: 99px;
  background: linear-gradient(90deg, var(--red), rgba(248, 113, 113, .55));
  flex-shrink: 0;
}

.share-bar-label {
  font-size: 11.5px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ============================================================
   SECTION HEADERS (inside content)
   ============================================================ */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.section-header h2 {
  font-size: 15px;
  font-weight: 600;
}

/* ============================================================
   UTILITY CLASSES
   ============================================================ */
.mt-1 { margin-top: 8px; }
.mt-2 { margin-top: 16px; }
.mt-3 { margin-top: 24px; }
.mb-1 { margin-bottom: 8px; }
.mb-2 { margin-bottom: 16px; }
.mb-3 { margin-bottom: 24px; }

.text-center  { text-align: center; }
.text-right   { text-align: right; }
.text-muted   { color: var(--text-muted); }
.text-small   { font-size: 12px; }
.text-subtle  { color: var(--text-subtle); }

.flex           { display: flex; }
.flex-col       { flex-direction: column; }
.gap-1          { gap: 8px; }
.gap-2          { gap: 16px; }
.items-center   { align-items: center; }
.justify-between { justify-content: space-between; }

/* ============================================================
   API KEY REVEAL BOX
   ============================================================ */
.api-key-box {
  background: var(--bg-base);
  border: 1px solid var(--green-border);
  border-radius: var(--r);
  padding: 14px 16px;
  margin-top: 8px;
}

.api-key-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--green);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}

.api-key-value {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text);
  word-break: break-all;
  margin-bottom: 10px;
  line-height: 1.6;
}

.api-key-notice {
  font-size: 12px;
  color: var(--yellow);
  display: flex;
  align-items: center;
  gap: 6px;
}

/* ============================================================
   DASHBOARD SPECIFIC
   ============================================================ */
.dashboard-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  align-items: start;
}

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

/* ============================================================
   RESPONSIVE — tablet & mobile
   ============================================================ */

/* Coarse pointers get bigger tap targets regardless of width */
@media (pointer: coarse) {
  .btn { min-height: 40px; }
  .btn-small { min-height: 34px; }
  .btn-icon { min-height: 40px; min-width: 40px; }
  .nav-link { padding: 11px 10px; }
  .modal-close { padding: 8px 12px; }
  .toast-close { padding: 6px; }
}

/* ── Drawer breakpoint: sidebar becomes off-canvas ───────── */
@media (max-width: 900px) {
  .menu-button { display: inline-flex; }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(300px, 84vw);
    z-index: 200;
    transform: translateX(-100%);
    transition: transform var(--t-spring);
    border-right: 1px solid var(--border);
  }

  body.sidebar-open .sidebar {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }

  .content-header {
    padding: 12px 16px;
    flex-wrap: wrap;
  }

  .content-header h1 {
    font-size: 17px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  #pageActions {
    flex-wrap: wrap;
  }

  .content-body {
    padding: 16px;
  }

  /* Wide tables scroll inside their card; header & pagination
     stay pinned while the rows scroll underneath */
  .table-container {
    overflow-x: auto;
  }
  .table-header,
  .pagination {
    position: sticky;
    left: 0;
  }

  .filters-grid {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  }
}

/* ── Phone ───────────────────────────────────────────────── */
@media (max-width: 640px) {
  /* 16px inputs stop iOS Safari from zooming the viewport on focus */
  .form-group input,
  .form-group select,
  .form-group textarea,
  .condition-row input,
  .condition-row select {
    font-size: 16px;
  }

  .stats-grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .stat-card { padding: 14px 16px; }
  .stat-value { font-size: 23px; }
  .stat-icon { width: 30px; height: 30px; }

  .dashboard-grid { gap: 12px; }

  /* Modals become bottom sheets */
  .modal-overlay {
    padding: 0;
    align-items: flex-end;
  }

  .modal {
    max-width: 100%;
    max-height: 94dvh;
    border-radius: var(--r-xl) var(--r-xl) 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
    animation: sheet-in var(--t-spring);
  }

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

  .modal-body { padding: 18px 16px; }
  .modal-header { padding: 14px 16px; }
  .modal-footer { padding: 12px 16px; }

  /* Toasts span the viewport */
  #toastContainer {
    left: 16px;
    right: 16px;
    top: 12px;
  }
  .toast {
    max-width: none;
    width: 100%;
  }

  .login-card { padding: 24px 20px; }
  .login-container { padding: 16px; }
}

/* ============================================================
   REDUCED MOTION
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
