:root {
  --bg: #f7f7f9;
  --card: #ffffff;
  --text: #1f2330;
  --muted: #6b7384;
  --border: #e3e6ed;
  --primary: #4f46e5;
  --primary-text: #ffffff;
  --success-bg: #d1fae5;
  --success-text: #065f46;
  --warning-bg: #fef3c7;
  --warning-text: #92400e;
  --muted-bg: #e5e7eb;
  --error-bg: #fee2e2;
  --error-text: #991b1b;
  --radius: 8px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; color: var(--text); background: var(--bg); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Top bar ---------- */
.topbar { background: #fff; border-bottom: 1px solid var(--border); position: fixed; top: 0; left: 240px; right: 0; height: 56px; z-index: 10; }
.topbar-inner { height: 100%; padding: 0 24px 0 20px; display: flex; align-items: center; gap: 24px; }
.brand { font-weight: 700; color: var(--text); display: flex; align-items: center; gap: 10px; }
.brand-logo { height: 18px; width: 18px; object-fit: contain; }
/* "admin" tag next to the wordmark — dark blue so the admin area reads at a glance. */
.brand-admin { color: #1e3a8a; font-weight: 700; }
.topbar-user { margin-left: auto; display: flex; align-items: center; gap: 12px; color: var(--muted); }
.topbar-user .user-name { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.user-avatar { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; background: var(--muted-bg); border: 1px solid var(--border); flex-shrink: 0; }
.user-avatar--placeholder { display: inline-flex; align-items: center; justify-content: center; color: var(--muted); }
.user-avatar--placeholder i { font-size: 18px; }
.topbar-user form { display: inline; margin: 0; }
.btn-sm { padding: 6px 10px; font-size: 13px; }

/* ---------- Left sidebar ---------- */
/* Runs full-height to the very top (like the /frontend tenant sidebar); the
   brand header sits at its top and the topbar starts to its right. */
.sidebar { position: fixed; top: 0; left: 0; bottom: 0; width: 240px; background: #fff; border-right: 1px solid var(--border); padding: 0 0 16px; overflow-y: auto; z-index: 5; }
.sidebar-brand { display: flex; align-items: center; gap: 10px; height: 56px; padding: 0 20px; font-weight: 700; color: var(--text); }
.sidebar-brand:hover { text-decoration: none; }
.sidebar-heading { padding: 16px 20px 4px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 0; }
.sidebar-nav { list-style: none; padding: 0; margin: 0; }
.sidebar-nav .nav-item { margin: 0; }
.sidebar-nav .nav-link { display: flex; align-items: center; gap: 10px; padding: 10px 20px; color: var(--muted); text-decoration: none; border-left: 3px solid transparent; font-size: 14px; }
.sidebar-nav .nav-link:hover { color: var(--text); background: #f5f6fa; text-decoration: none; }
.sidebar-nav .link-active .nav-link { color: var(--primary); background: #eef0ff; border-left-color: var(--primary); font-weight: 600; }
.sidebar-nav .nav-link i { width: 18px; text-align: center; color: inherit; }
.sidebar-nav .sidebar-label { flex: 1; }
.sidebar-nav .badge { background: var(--muted-bg); color: var(--muted); margin-left: auto; }

/* ---------- Main content ---------- */
/* Default: centered (used by the auth pages — login, forgot password, reset). */
.container { max-width: 1100px; margin: 24px auto; padding: 0 24px; }
/* Signed-in: center the content within the region to the right of the fixed
   sidebar (below the fixed topbar). The asymmetric padding (sidebar + gutter on
   the left, gutter on the right) combined with auto side-margins keeps the
   block horizontally centered in that region instead of hugging the sidebar. */
body.has-sidebar .container { max-width: calc(1100px + 288px); margin: 80px auto 24px; padding: 0 24px 0 264px; }
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; gap: 12px; }
.page-header form { margin: 0; }

.breadcrumbs { margin-bottom: 4px; font-size: 13px; }
.breadcrumbs ol { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.breadcrumbs li { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.breadcrumbs li + li::before { content: "/"; color: var(--border); }
.breadcrumbs a { color: var(--muted); text-decoration: none; }
.breadcrumbs a:hover { color: var(--primary); text-decoration: underline; }
.breadcrumbs [aria-current="page"] { color: var(--text); }

.muted { color: var(--muted); }
.empty-state { color: var(--muted); padding: 32px; background: var(--card); border: 1px dashed var(--border); border-radius: var(--radius); text-align: center; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.card { display: flex; flex-direction: column; gap: 8px; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; color: var(--text); }
.card:hover { text-decoration: none; border-color: var(--primary); }
.card-label { color: var(--muted); font-size: 14px; }
.card-value { font-size: 28px; font-weight: 600; }

.data-table { width: 100%; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); border-collapse: collapse; overflow: hidden; }
.data-table th, .data-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.data-table th { background: #fafbfd; color: var(--muted); font-weight: 600; font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; }
.data-table tr:last-child td { border-bottom: none; }
/* Actions column always pinned to the right edge of the table. */
.data-table th:last-child,
.data-table td:last-child,
.data-table .row-actions { text-align: right; white-space: nowrap; }
.row-action { display: inline-flex; align-items: center; gap: 6px; color: var(--primary); }
.row-action i { font-size: 12px; }
.data-table code { background: var(--muted-bg); padding: 2px 6px; border-radius: 4px; font-size: 13px; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 500; background: var(--muted-bg); color: var(--muted); }
.badge-success { background: var(--success-bg); color: var(--success-text); }
.badge-warning { background: var(--warning-bg); color: var(--warning-text); }
.badge-muted { background: var(--muted-bg); color: var(--muted); }

.form {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px 24px;
}

/* Rows that should span both columns. */
.form > .errors,
.form > .fieldset,
.form > fieldset,
.form > .form-actions { grid-column: 1 / -1; }

/* SimpleForm row: <div class="input string required ..."><label>…</label><input>…</div> */
.form .input { display: flex; flex-direction: column; gap: 4px; margin: 0; min-width: 0; }
.form .input > label { font-weight: 500; font-size: 14px; color: var(--text); }
.form .input > label abbr { color: var(--error-text); text-decoration: none; margin-left: 2px; }
.form .input input[type="text"],
.form .input input[type="email"],
.form .input input[type="url"],
.form .input input[type="password"],
.form .input input[type="number"],
.form .input input[type="search"],
.form .input input[type="tel"],
.form .input input[type="date"],
.form .input input[type="datetime-local"],
.form .input input[type="time"],
.form .input select,
.form .input textarea {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  background: var(--card);
  color: var(--text);
  width: 100%;
  box-sizing: border-box;
  font-family: inherit;
}
.form .input input:focus,
.form .input select:focus,
.form .input textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: 0;
  border-color: var(--primary);
}
.form .input .hint { color: var(--muted); font-size: 13px; }
.form .input .error { color: var(--error-text); font-size: 13px; }

/* Invalid state */
.form .input.field_with_errors input,
.form .input.field_with_errors select,
.form .input.field_with_errors textarea { border-color: var(--error-text); }

/* Boolean checkbox row: label wraps the checkbox + text */
/* Boolean row: checkbox + label inline; hint sits on its own line below. */
.form .input.boolean { flex-direction: column; align-items: flex-start; gap: 4px; }
.form .input.boolean > label { display: flex; align-items: center; gap: 8px; font-weight: normal; cursor: pointer; margin: 0; }
.form .input.boolean input[type="checkbox"] { width: 16px; height: 16px; margin: 0; accent-color: var(--primary); }
.form .input.boolean .hint { padding-left: 24px; }  /* align hint with label text, not checkbox */

/* Fieldset (grouped section) — also a 2-column grid for its inputs. */
.form .fieldset, .form fieldset.fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px 24px;
}
.form .fieldset > legend, .form fieldset.fieldset > legend {
  padding: 0 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.form .fieldset > .muted, .form fieldset.fieldset > .muted {
  margin: 0;
  grid-column: 1 / -1;
}

/* 12-column page grid for side-by-side panels (e.g. tenant edit: settings |
   users). Compose with .col-N children (col-4 + col-8, col-5 + col-7, ...).
   Collapses to a single stacked column on screens narrower than lg. */
.split-grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 24px; align-items: start; }
.split-grid > [class*="col-"] { min-width: 0; }
.split-grid > .col-4 { grid-column: span 4; }
.split-grid > .col-5 { grid-column: span 5; }
.split-grid > .col-6 { grid-column: span 6; }
.split-grid > .col-7 { grid-column: span 7; }
.split-grid > .col-8 { grid-column: span 8; }
@media (max-width: 1024px) { .split-grid > [class*="col-"] { grid-column: 1 / -1; } }

/* Wider content area for two-panel pages (opt-in via content_for in the view). */
.container--wide { max-width: 1500px; }
/* Sidebar-aware override (the signed-in .container rule is more specific). */
body.has-sidebar .container--wide { max-width: calc(1500px + 288px); }

/* Card panel — same chrome as .form, for non-form content (e.g. the tenant
   users list rendered beside the tenant settings form). */
.panel { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; }
.panel-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.panel-header h2 { margin: 0; font-size: 18px; font-weight: 600; }
.panel .empty-state { color: var(--muted); margin: 0; }

/* Checkbox rows for the roles / permissions assignment panel. */
.check-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.check-row { display: flex; align-items: flex-start; gap: 8px; font-size: 14px; cursor: pointer; margin: 0; font-weight: normal; }
.check-row input[type="checkbox"] { width: 16px; height: 16px; margin: 2px 0 0; accent-color: var(--primary); flex-shrink: 0; }
.check-row small { color: var(--muted); display: block; }
.perm-group { border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; margin: 0 0 12px; }
.perm-group > legend { padding: 0 6px; color: var(--muted); font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.perm-actions { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 6px 16px; }
.panel-section { margin-bottom: 20px; }
.panel-section > h3 { margin: 0 0 4px; font-size: 15px; font-weight: 600; }
.panel-section > .muted { margin: 0 0 12px; font-size: 13px; }
.panel-note { background: var(--muted-bg); color: var(--muted); padding: 10px 14px; border-radius: 6px; font-size: 13px; margin: 0 0 16px; }

.form-actions { display: flex; gap: 8px; margin-top: 0; }
.form-actions .btn { padding: 10px 18px; font-size: 14px; }
/* Edit/new footer bar: Save pinned right, Delete (button_to form) pushed left. */
.form-actions--split { justify-content: flex-end; align-items: center; margin-top: 16px; }
.form-actions--split form { margin: 0; }
.form-actions--split .form-actions__delete { margin-right: auto; }

.btn { display: inline-block; padding: 8px 14px; border-radius: 6px; border: 1px solid var(--border); background: var(--card); color: var(--text); cursor: pointer; font-size: 14px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-ghost { background: transparent; }
.btn-danger { background: var(--error-text); border-color: var(--error-text); color: var(--primary-text); }

.flash { padding: 10px 14px; border-radius: 6px; margin-bottom: 16px; }
.flash-success { background: var(--success-bg); color: var(--success-text); }
.flash-error { background: var(--error-bg); color: var(--error-text); }

.errors { background: var(--error-bg); color: var(--error-text); padding: 12px 16px; border-radius: 6px; margin-bottom: 16px; }
.errors ul { margin: 4px 0 0 18px; }

.auth-card { max-width: 380px; margin: 80px auto; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; }
.auth-card h1 { margin-top: 0; }
.auth-card hr { border: 0; border-top: 1px solid var(--border); }

/* Bootstrap utility shim — only the classes the engine login partial uses. */
.d-none { display: none !important; }
.text-center { text-align: center; }
.text-left { text-align: left; }
.w-100 { width: 100%; }
.mt-3 { margin-top: 12px; }
.my-3 { margin-top: 12px; margin-bottom: 12px; }
.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: 4px; }
.mb-2 { margin-bottom: 8px; }
.mb-4 { margin-bottom: 16px; }
.ml-1 { margin-left: 4px; }
.alert { padding: 10px 14px; border-radius: 6px; margin-bottom: 12px; }
.alert-danger { background: var(--error-bg); color: var(--error-text); }

/* Form-signin + form-password layout inside the auth-card. */
.auth-card .form-signin,
.auth-card .form-password { max-width: 100%; padding: 0; text-align: left; }

/* Headings and intro text in the password reset partials. */
.auth-card h1,
.auth-card h2 { margin: 0 0 8px; font-size: 20px; font-weight: 600; }
.auth-card .intro { color: var(--muted); margin: 0 0 16px; font-size: 14px; }

/* Engine submit button uses `module-gradient-button-inverted-system` instead
   of btn-primary. Style it (and the `#login-button` ID used on every form) to
   match the primary button. */
.auth-card #login-button,
.auth-card .module-gradient-button-inverted-system {
  background: var(--primary) !important;
  border: 1px solid var(--primary) !important;
  color: var(--primary-text) !important;
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  width: 100%;
  margin-top: 12px;
}
.auth-card #login-button:hover,
.auth-card .module-gradient-button-inverted-system:hover { filter: brightness(0.95); }

/* devise/shared/_links override renders inside auth-card; constrain text size. */
.auth-card a { color: var(--primary); }
.auth-card p { margin: 12px 0; font-size: 14px; }
.auth-card .alert { font-size: 13px; }

/* All text-like inputs in the login form share the same look. */
.auth-card .simple_form .input { width: 100%; margin: 0 0 10px; }
.auth-card .simple_form .input input,
.auth-card input[type="email"],
.auth-card input[type="password"],
.auth-card input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  background: var(--card);
  color: var(--text);
  box-sizing: border-box;
}
.auth-card input[type="email"]:focus,
.auth-card input[type="password"]:focus,
.auth-card input[type="text"]:focus {
  outline: 2px solid var(--primary);
  outline-offset: 0;
  border-color: var(--primary);
}

/* Remember-me row: checkbox + inline label, no extra wrapping. */
.auth-card .login-fields.text-left {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 8px 0 14px;
}
.auth-card .login-fields.text-left input[type="checkbox"] { margin: 0; width: auto; }
.auth-card .login-fields.text-left label { margin: 0; font-weight: normal; cursor: pointer; }

/* SimpleForm validation message styling (in case of error). */
.auth-card .simple_form .error { color: var(--error-text); font-size: 13px; margin-top: 4px; }

/* Utility: shown only on small screens (see the mobile media query). */
.show-mobile { display: none; }

/* Secondary table line (e.g. email) stacked under the main cell text. */
.cell-subtext { display: block; color: var(--muted); font-size: 12px; }

/* Row of badges stacked under a table cell's main text (e.g. tenant languages). */
.cell-badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }

/* ---------- Mobile (≤768px) ----------
   The left sidebar becomes a fixed, icon-only bottom bar (mobile-app style),
   and the topbar sign-out collapses to its icon. */
@media (max-width: 768px) {
  /* Utility: hide secondary table columns / labels on small screens. */
  .hide-mobile { display: none; }
  .show-mobile { display: block; }

  /* Sidebar drops to a bottom bar on mobile, so the topbar spans full width. */
  .topbar { left: 0; }
  .topbar-inner { padding: 0 16px; gap: 12px; }

  /* Sign-out: icon only, hide the label. */
  .topbar-user .btn-label { display: none; }

  /* Sidebar → fixed bottom nav bar with evenly spaced icons. */
  .sidebar {
    top: auto;
    bottom: 0;
    width: 100%;
    height: 56px;
    padding: 0;
    border-right: none;
    border-top: 1px solid var(--border);
    overflow: visible;
    display: flex;
    z-index: 20;
  }
  .sidebar .sidebar-heading { display: none; }
  /* Flatten both <ul>s so all items distribute equally across the bar. */
  .sidebar .sidebar-nav { display: contents; }
  .sidebar .nav-item { flex: 1; }
  .sidebar .nav-link {
    flex-direction: column;
    justify-content: center;
    gap: 0;
    height: 100%;
    padding: 0;
    border-left: none;
  }
  .sidebar .nav-link i { font-size: 20px; width: auto; }
  /* Icons only — drop every text label in the bar. */
  .sidebar .nav-link .ml-1,
  .sidebar .nav-link .sidebar-label { display: none; }
  .sidebar .link-active .nav-link {
    border-left: none;
    box-shadow: inset 0 2px 0 var(--primary);
  }

  /* Content: clear the fixed topbar above and the bottom nav bar below.
     Reset the desktop sidebar padding (no fixed sidebar on mobile). */
  body.has-sidebar .container { margin: 72px 16px 72px; padding: 0 16px; }
}
