/* ══════════════════════════════════════════════════════════════════
   rules.css — Rules tab presentation layer
   ══════════════════════════════════════════════════════════════════
   Loaded AFTER css/database.css inside templates/pages/database.html.
   database.css sets nearly every property with !important (it was
   mechanically extracted out of an inline <style> block), so the
   overrides here have to match that weight to land. New selectors —
   anything prefixed .rules-* or .rule-* — need no !important.

   The brief was "soft". Concretely that meant four changes, all in
   terms of the existing design tokens so both themes follow:

     1. borders recede      hairline --border, not --border-strong,
                            and never a saturated accent outline on a
                            resting control
     2. shadows diffuse     wide + low alpha instead of tight + dark
     3. radii open up       18-20px on controls, 16px on containers
     4. colour means state  the toolbar goes neutral so the only
                            saturated things left on screen are the
                            ones that carry information — an active
                            filter, a variable you filtered on, a rule
                            that is paused or expiring
   ══════════════════════════════════════════════════════════════════ */

/* ── soft tokens, scoped to this tab ──────────────────────────── */
#panel-database {
  --rules-radius: 16px;
  --rules-radius-pill: 20px;
  --rules-radius-sm: 9px;
  --rules-shadow-rest: 0 1px 2px rgba(35, 31, 32, 0.03), 0 10px 30px -12px rgba(35, 31, 32, 0.10);
  --rules-shadow-lift: 0 2px 4px rgba(35, 31, 32, 0.04), 0 18px 44px -16px rgba(35, 31, 32, 0.16);
  --rules-hairline: var(--border);
  --rules-row-hover: color-mix(in srgb, var(--accent) 4%, transparent);
  --rules-focus-ring: 0 0 0 3px color-mix(in srgb, var(--accent) 16%, transparent);
}

[data-theme='dark'] #panel-database {
  --rules-shadow-rest: 0 1px 2px rgba(0, 0, 0, 0.30), 0 10px 30px -12px rgba(0, 0, 0, 0.55);
  --rules-shadow-lift: 0 2px 6px rgba(0, 0, 0, 0.36), 0 20px 48px -16px rgba(0, 0, 0, 0.65);
  --rules-row-hover: color-mix(in srgb, var(--accent) 10%, transparent);
}

/* color-mix has been in every evergreen browser since 2023, but a
   fallback costs two lines and keeps the tab usable in an older
   embedded webview. */
@supports not (color: color-mix(in srgb, red 50%, blue)) {
  #panel-database {
    --rules-row-hover: var(--surface-1);
    --rules-focus-ring: 0 0 0 3px var(--accent-soft);
  }
}

#panel-database .db-layout {
  padding: 26px 28px 40px;
}

/* ── toolbar ──────────────────────────────────────────────────── */
#panel-database .db-toolbar {
  gap: 14px 18px;
  margin-bottom: 18px;
}

#panel-database .db-filters {
  gap: 10px;
  align-items: center;
}

#panel-database .db-search {
  border-radius: var(--rules-radius-pill);
  border: 1px solid var(--rules-hairline);
  background: var(--surface);
  box-shadow: none;
  padding: 10px 18px 10px 40px;
  min-width: 260px;
  /* Magnifier drawn in, so the field reads as search without an extra
     wrapper element around it. */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%236a686a' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='M20 20l-3.5-3.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: 15px center;
}

#panel-database .db-search:focus {
  border-color: var(--accent-border);
  box-shadow: var(--rules-focus-ring);
}

#panel-database .db-select {
  border-radius: var(--rules-radius-pill);
  border: 1px solid var(--rules-hairline);
  background: var(--surface);
  padding: 9px 14px;
  font-size: 0.83rem;
  font-weight: 500;
  color: var(--text-sec);
}

#panel-database .db-select:hover {
  border-color: var(--border-strong);
}

#panel-database .db-select:focus {
  border-color: var(--accent-border);
  box-shadow: var(--rules-focus-ring);
}

/* The two multiselect triggers sat behind a saturated accent border
   and a glow on hover — two resting controls shouting as loudly as an
   applied filter. Neutral at rest; the count badge is what says
   "something is on". */
#panel-database .db-multiselect-btn {
  border: 1px solid var(--rules-hairline) !important;
  background: var(--surface) !important;
  border-radius: var(--rules-radius-pill) !important;
  box-shadow: none !important;
  padding: 8px 15px !important;
  font-weight: 500 !important;
  color: var(--text-sec) !important;
  min-width: 0 !important;
  gap: 8px !important;
}

#panel-database .db-multiselect-btn:hover {
  background: var(--surface-1) !important;
  border-color: var(--border-strong) !important;
  box-shadow: none !important;
}

#panel-database .db-multiselect-btn:focus-visible {
  outline: none !important;
  border-color: var(--accent-border) !important;
  box-shadow: var(--rules-focus-ring) !important;
}

#panel-database .db-multiselect-badge {
  background: var(--accent-soft) !important;
  color: var(--accent) !important;
  border: 1px solid var(--accent-border) !important;
  border-radius: 999px !important;
  padding: 1px 7px !important;
  font-weight: 600 !important;
}

#panel-database .db-multiselect-panel {
  border-radius: var(--rules-radius) !important;
  border: 1px solid var(--rules-hairline) !important;
  box-shadow: var(--rules-shadow-lift) !important;
  padding: 10px !important;
}

#panel-database .db-multiselect-option {
  border-radius: var(--rules-radius-sm) !important;
  padding: 8px 10px !important;
  gap: 10px !important;
}

#panel-database .db-multiselect-option-label {
  flex: 1;
  min-width: 0;
}

/* Count of rules using each variable — the picker is much easier to
   plan with when you can see that "Hazmat" covers 2 rules and
   "Miles" covers 61. */
#panel-database .db-multiselect-option-count {
  flex: none;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 1px 7px;
}

#panel-database .db-multiselect-action-btn {
  border-radius: var(--rules-radius-pill) !important;
  border-color: var(--rules-hairline) !important;
  font-weight: 500 !important;
}

#panel-database .db-multiselect-action-btn:hover {
  background: var(--accent-soft) !important;
  color: var(--accent) !important;
  border-color: var(--accent-border) !important;
}

#panel-database .add-filter-btn {
  border-radius: var(--rules-radius-pill) !important;
  border: 1px dashed var(--border-strong) !important;
  background: transparent !important;
  color: var(--text-sec) !important;
  padding: 8px 15px !important;
  font-weight: 500 !important;
}

#panel-database .add-filter-btn:hover {
  background: var(--accent-soft) !important;
  border-color: var(--accent-border) !important;
  color: var(--accent) !important;
  box-shadow: none !important;
}

/* Dark theme had accent-coloured text on an accent-coloured fill —
   the label was invisible. */
[data-theme='dark'] #panel-database .add-filter-btn {
  background: transparent !important;
  color: var(--text-sec) !important;
  border-color: var(--border-strong) !important;
}

[data-theme='dark'] #panel-database .add-filter-btn:hover {
  background: var(--accent-soft) !important;
  color: var(--accent) !important;
}

#panel-database .filter-builder-popover {
  border-radius: var(--rules-radius) !important;
  border: 1px solid var(--rules-hairline) !important;
  box-shadow: var(--rules-shadow-lift) !important;
}

#panel-database .fb-select {
  border-width: 1px !important;
  border-color: var(--rules-hairline) !important;
  border-radius: var(--rules-radius-sm) !important;
}

#panel-database .fb-select:focus {
  border-color: var(--accent-border) !important;
  box-shadow: var(--rules-focus-ring) !important;
}

#panel-database .fb-submit-btn {
  border-radius: var(--rules-radius-sm) !important;
  background: var(--accent) !important;
  box-shadow: none !important;
  font-weight: 600 !important;
}

#panel-database .fb-submit-btn:hover {
  background: var(--accent-hover) !important;
  box-shadow: 0 6px 18px -8px var(--accent) !important;
}

#panel-database .fb-cancel-btn {
  border-radius: var(--rules-radius-sm) !important;
  border-width: 1px !important;
  border-color: var(--rules-hairline) !important;
}

#panel-database .fb-combobox-list {
  border-radius: var(--rules-radius-sm) !important;
  border-width: 1px !important;
  border-color: var(--rules-hairline) !important;
  box-shadow: var(--rules-shadow-lift) !important;
}

/* Dark-mode combobox hover was accent text on an accent fill — an
   invisible option label. */
[data-theme='dark'] #panel-database .fb-combobox-item:hover {
  background: var(--accent-soft) !important;
  color: var(--accent) !important;
}

/* ── result count ─────────────────────────────────────────────── */
#panel-database .rules-result-count {
  font-family: var(--font-mono);
  font-size: 0.76rem;
  font-weight: 500;
  color: var(--text-muted);
  white-space: nowrap;
  padding-left: 2px;
}

/* ── scope chip row ───────────────────────────────────────────── */
#panel-database #scope-tabs-container {
  gap: 6px;
  margin-bottom: 14px;
  padding-bottom: 2px;
}

#panel-database .scope-chip {
  border-radius: var(--rules-radius-pill);
  border: 1px solid var(--rules-hairline);
  background: var(--surface);
  box-shadow: none;
  transition: background 0.18s ease, border-color 0.18s ease, color 0.18s ease;
}

#panel-database .scope-chip:hover {
  background: var(--surface-1);
  border-color: var(--border-strong);
}

#panel-database .scope-chip.active {
  background: var(--accent-soft);
  border-color: var(--accent-border);
  color: var(--accent);
}

/* ── active filter pills ──────────────────────────────────────── */
#panel-database .stacked-filter-pill {
  background: var(--accent-soft) !important;
  border: 1px solid var(--accent-border) !important;
  color: var(--accent) !important;
  border-radius: var(--rules-radius-pill) !important;
  box-shadow: none !important;
  font-weight: 500 !important;
}

#panel-database .stacked-filter-pill .remove-btn {
  color: var(--accent) !important;
  opacity: 0.6;
}

#panel-database .stacked-filter-pill .remove-btn:hover {
  opacity: 1;
  color: var(--neg) !important;
}

/* ── the table shell ──────────────────────────────────────────── */
#panel-database .table-wrapper {
  border-radius: var(--rules-radius);
  border: 1px solid var(--rules-hairline);
  box-shadow: var(--rules-shadow-rest);
  overflow: hidden;
}

#panel-database .table-wrapper:hover {
  border-color: var(--rules-hairline);
  box-shadow: var(--rules-shadow-rest);
}

#panel-database .table-container {
  max-height: min(64vh, 720px);
  overflow: auto;
  /* Keeps the sticky header from tearing against the rounded corner
     while a scroll is in flight. */
  border-radius: inherit;
}

#panel-database .rules-table {
  font-size: 0.86rem;
  table-layout: fixed;
  width: 100%;
}

/* Fixed layout so the Variables column can't be squeezed to nothing by
   one rule with a very long name. Percentages, so it still adapts. */
#panel-database .rules-table col.col-name { width: 33%; }
#panel-database .rules-table col.col-scope { width: 12%; }
#panel-database .rules-table col.col-vars { width: 24%; }
#panel-database .rules-table col.col-period { width: 19%; }
#panel-database .rules-table col.col-actions { width: 12%; }

#panel-database .rules-table thead th {
  background: var(--surface-1);
  border-bottom: 1px solid var(--rules-hairline);
  padding: 13px 18px;
  font-size: 0.68rem;
  letter-spacing: 0.07em;
  color: var(--text-muted);
  font-weight: 600;
  user-select: none;
}

#panel-database .rules-table thead th.is-sortable {
  cursor: pointer;
  transition: color 0.15s ease, background 0.15s ease;
}

#panel-database .rules-table thead th.is-sortable:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

#panel-database .rules-table thead th .sort-ind {
  display: inline-block;
  min-width: 10px;
  margin-left: 4px;
  color: var(--accent);
  font-weight: 700;
}

#panel-database .rules-table td {
  padding: 13px 18px;
  border-bottom: 1px solid var(--rules-hairline);
  vertical-align: middle;
}

#panel-database .rules-table tbody tr:last-child td {
  border-bottom: none;
}

#panel-database .rules-row {
  cursor: pointer;
  transition: background 0.16s ease;
}

#panel-database .rules-row:hover {
  background: var(--rules-row-hover);
}

#panel-database .rules-row:focus-visible {
  outline: 2px solid var(--accent-border);
  outline-offset: -2px;
}

/* Border colour must not change on hover — it made every row nudge by
   a hairline as the cursor travelled down the table. */
#panel-database .rules-table tbody tr:hover td {
  border-bottom-color: var(--rules-hairline);
}

/* Paused and expired rules stay legible but visibly recede, so a long
   list reads as "these are the ones doing something". */
#panel-database .rules-row--dim .rule-name,
#panel-database .rules-row--dim .rule-summary {
  opacity: 0.62;
}

/* ── name cell ────────────────────────────────────────────────── */
#panel-database .rule-name-wrap {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  min-width: 0;
}

#panel-database .rule-name-text {
  min-width: 0;
  flex: 1;
}

#panel-database .rule-name-line {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

#panel-database .rule-name {
  font-size: 0.87rem;
  font-weight: 650;
  color: var(--text-primary);
  line-height: 1.35;
}

#panel-database .rule-summary {
  font-size: 0.76rem;
  color: var(--text-muted);
  line-height: 1.45;
  margin-top: 2px;
  text-transform: none;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}

#panel-database .rule-group-arrow {
  font-size: 0.65rem;
  color: var(--text-muted);
  margin-top: 4px;
  flex: none;
  transition: color 0.15s ease;
}

#panel-database .rules-row--group:hover .rule-group-arrow {
  color: var(--accent);
}

#panel-database .rule-subcount {
  font-size: 0.68rem;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 2px 8px;
  white-space: nowrap;
}

/* ── Variables column ─────────────────────────────────────────── */
#panel-database .rule-vars {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
}

/* The badge was warn-amber — the same colour the app uses for "this
   is expiring". A rule testing Weight is not a warning. Neutral at
   rest; accent only when you have filtered on it. */
#panel-database .var-badge {
  display: inline-flex;
  align-items: center;
  font-family: var(--font-sans);
  background: var(--surface-2);
  color: var(--text-sec);
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 3px 9px;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: none;
  white-space: nowrap;
  margin: 0;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

#panel-database button.var-badge {
  font: inherit;
  font-size: 0.68rem;
  font-weight: 600;
  line-height: 1.45;
}

#panel-database .var-badge:hover {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent-border);
}

#panel-database .var-badge--active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent-border);
}

#panel-database .var-badge--more {
  background: transparent;
  border: 1px dashed var(--border-strong);
  color: var(--text-muted);
  cursor: default;
}

#panel-database .var-badge--more:hover {
  background: transparent;
  color: var(--text-muted);
  border-color: var(--border-strong);
}

#panel-database .rule-vars-none {
  font-size: 0.74rem;
  color: var(--text-muted);
  font-style: italic;
}

/* ── Active period column ─────────────────────────────────────── */
#panel-database .rule-period {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

#panel-database .rule-period-range {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 0.74rem;
  color: var(--text-sec);
  white-space: nowrap;
}

#panel-database .rule-period-note {
  font-size: 0.7rem;
  font-weight: 500;
  color: var(--text-muted);
  line-height: 1.35;
}

/* The note is where the lifecycle colour lives now that the Status
   column is gone — but only for the states worth noticing. */
#panel-database .rule-period-note--expiring { color: var(--warn); font-weight: 600; }
#panel-database .rule-period-note--scheduled { color: var(--note); font-weight: 600; }
#panel-database .rule-period-note--expired,
#panel-database .rule-period-note--paused { color: var(--text-muted); }

/* Softer lifecycle chip to match — it now sits inline next to the
   rule name rather than filling its own column. */
#panel-database .rule-status {
  font-family: var(--font-sans);
  font-size: 0.62rem;
  letter-spacing: 0.04em;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 700;
}

/* Compact Table Row Badges (KMA / BA) */
#panel-database .kma-badge,
#panel-database .ba-badge {
  display: inline-flex;
  align-items: center;
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.4px;
  line-height: 1.1;
  margin-left: 6px;
  text-transform: uppercase;
  vertical-align: middle;
}

#panel-database .kma-badge {
  background: var(--warn-soft, #fef3c7);
  color: var(--warn, #d97706);
  border: 1px solid var(--warn-border, #fcd34d);
}

#panel-database .ba-badge {
  background: var(--accent-soft, #e0f2fe);
  color: var(--accent, #0284c7);
  border: 1px solid var(--accent-border, #7dd3fc);
}

/* ── actions column ───────────────────────────────────────────── */
#panel-database .rule-actions {
  display: flex;
  gap: 2px;
  justify-content: flex-end;
  align-items: center;
}

#panel-database .rule-actions .btn-icon {
  border-radius: 9px;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid transparent;
  padding: 6px;
  transition: background 0.15s ease, color 0.15s ease;
}

#panel-database .rule-actions .btn-icon:hover {
  background: var(--surface-2);
  color: var(--text-primary);
}

#panel-database .rule-actions .btn-icon.danger:hover {
  background: var(--neg-soft);
  color: var(--neg);
}

#panel-database .rule-actions .toggle {
  margin-right: 6px;
}

/* Icon buttons only reveal their affordance on row hover, so a long
   table reads as content rather than as a wall of controls. They stay
   fully visible on keyboard focus. */
#panel-database .rule-actions .btn-icon {
  opacity: 0.55;
}

#panel-database .rules-row:hover .rule-actions .btn-icon,
#panel-database .rule-actions .btn-icon:focus-visible {
  opacity: 1;
}

@media (hover: none) {
  #panel-database .rule-actions .btn-icon { opacity: 1; }
}

/* ── expanded sub-rule panel ──────────────────────────────────── */
#panel-database .rules-subrow > td {
  padding: 0;
  background: var(--surface-1);
}

#panel-database .rule-branches {
  padding: 12px 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#panel-database .rule-branch {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--rules-hairline);
  border-radius: var(--rules-radius-sm);
}

#panel-database .rule-branch-main {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}

#panel-database .rule-branch-idx {
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--accent);
  white-space: nowrap;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#panel-database .rule-branch-summary {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-primary);
  text-transform: none;
}

#panel-database .rule-branch-action {
  font-family: var(--font-mono);
  font-size: 0.74rem;
  color: var(--text-muted);
  white-space: nowrap;
  flex: none;
}

/* ── empty / loading / error states ───────────────────────────── */
#panel-database .rules-empty {
  padding: 56px 24px !important;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.88rem;
}

#panel-database .rules-empty-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-sec);
  margin-bottom: 4px;
}

#panel-database .rules-empty-sub {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 14px;
}

#panel-database .rules-empty--error {
  color: var(--neg);
}

#panel-database .rules-inline-retry {
  margin-top: 4px;
  padding: 7px 16px;
  border-radius: var(--rules-radius-pill);
  border: 1px solid var(--accent-border);
  background: var(--accent-soft);
  color: var(--accent);
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
}

#panel-database .rules-inline-retry:hover {
  background: var(--accent-tint);
}

/* ── narrow viewports ─────────────────────────────────────────── */
@media (max-width: 1180px) {
  /* Scope is already implied by the client badge inside the name cell
     on smaller screens; Variables is the column this change exists to
     make room for, so it is the one that stays. */
  #panel-database .rules-table col.col-name { width: 40%; }
  #panel-database .rules-table col.col-scope { width: 0; }
  #panel-database .rules-table col.col-vars { width: 30%; }
  #panel-database .rules-table col.col-period { width: 18%; }
  #panel-database .rules-table col.col-actions { width: 12%; }
  #panel-database .rules-cell-scope,
  #panel-database .rules-table thead th.rules-th-scope { display: none; }
}

@media (max-width: 860px) {
  #panel-database .db-layout { padding: 18px 14px 32px; }
  #panel-database .db-search { min-width: 0; width: 100%; }
  #panel-database .rules-table col.col-period { width: 0; }
  #panel-database .rules-cell-period,
  #panel-database .rules-table thead th.rules-th-period { display: none; }
  #panel-database .rules-table col.col-name { width: 52%; }
  #panel-database .rules-table col.col-vars { width: 30%; }
  #panel-database .rules-table col.col-actions { width: 18%; }
}

@media (prefers-reduced-motion: reduce) {
  #panel-database * {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}