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

/* Neutral greys with a charcoal accent; --signal (cobalt) marks the current
   selection only and is never used as a fill for large areas. */
:root,
[data-theme="light"] {
  --bg: #f2f2f2;
  --surface: #ffffff;
  --surface2: #f7f7f7;
  --border: #e0e0e0;
  --accent: #3a3a3a;
  --accent-hover: #1f1f1f;
  --on-accent: #ffffff;
  --signal: #1f5fbf;
  --btn: #1f5fbf;
  --btn-hover: #194f9f;
  --on-btn: #ffffff;
  --accent-danger: #c62828;
  --accent-success: #2e7d32;
  --accent-warning: #b26a00;
  --mark-trigger: #2f6fb3;
  --mark-cond: #e07a1f;
  --mark-action: #3c8c4a;
  --text: #1f1f1f;
  --text-muted: #5f5f5f;
  --text-dim: #8a8a8a;
  --radius: 4px;
  --shadow: 0 2px 10px rgba(0,0,0,.10);
  --transition: 0.18s ease;
  --header-h: 52px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

[data-theme="dark"] {
  --bg: #161616;
  --surface: #1f1f1f;
  --surface2: #262626;
  --border: #333333;
  --accent: #f2f2f2;
  --accent-hover: #ffffff;
  --on-accent: #1f1f1f;
  --accent-danger: #ef5350;
  --accent-success: #4caf50;
  --accent-warning: #f0a030;
  --signal: #4a86e0;
  --btn: #2f6fd0;
  --btn-hover: #3d7fe0;
  --mark-trigger: #5a9be0;
  --mark-cond: #f0953f;
  --mark-action: #5cb46a;
  --text: #ececec;
  --text-muted: #a8a8a8;
  --text-dim: #7a7a7a;
  --shadow: 0 2px 12px rgba(0,0,0,.45);
}

body {
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  line-height: 1.5;
  min-height: 100vh;
}

/* ===== App Shell: header on top, sidebar navigation on the left ===== */
#app {
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr);
  grid-template-rows: var(--header-h) 1fr;
  min-height: 100vh;
}

header {
  grid-column: 1 / -1;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  display: flex;
  align-items: center;
  gap: 14px;
  height: var(--header-h);
  position: sticky;
  top: 0;
  z-index: 100;
}

header h1 {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

header .subtitle {
  font-size: 11.5px;
  color: var(--text-muted);
}

/* ===== Sidebar navigation ===== */
.tabs {
  grid-column: 1;
  grid-row: 2;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 12px 0;
  position: sticky;
  top: var(--header-h);
  height: calc(100vh - var(--header-h));
}
.tabs-spacer { flex: 1; }

.tab-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  background: none;
  border: none;
  border-left: 3px solid transparent;
  color: var(--text);
  padding: 10px 20px;
  cursor: pointer;
  font-size: 13.5px;
  font-weight: 500;
  text-align: left;
  text-decoration: none;
  white-space: nowrap;
  transition: background var(--transition), border-color var(--transition);
}
.tab-btn::before {
  content: "";
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  background: currentColor;
  opacity: .7;
  -webkit-mask: var(--ico) center / contain no-repeat;
  mask: var(--ico) center / contain no-repeat;
}
.tab-btn:hover { background: var(--surface2); }
.tab-btn.active {
  font-weight: 600;
  border-left-color: var(--signal);
  background: color-mix(in srgb, var(--signal) 10%, var(--surface));
}
.tab-btn.active::before { opacity: 1; }
a.tab-btn { font-size: 12.5px; color: var(--text-muted); padding-top: 8px; padding-bottom: 8px; }

.tab-btn[data-tab="rules"]     { --ico: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M4 5h3v3H4zM10 5.5h10v2H10zM4 10.5h3v3H4zM10 11h10v2H10zM4 16h3v3H4zM10 16.5h10v2H10z'/%3E%3C/svg%3E"); }
.tab-btn[data-tab="log"]       { --ico: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20zm0 2a8 8 0 1 1 0 16 8 8 0 0 1 0-16zm-1 3v6l5 3 1-1.6-4-2.4V7z'/%3E%3C/svg%3E"); }
.tab-btn[data-tab="variables"] { --ico: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M8 3C5.8 3 5 4.2 5 6v3c0 1-.6 2-2 2v2c1.4 0 2 1 2 2v3c0 1.8.8 3 3 3h1v-2H8c-.8 0-1-.4-1-1.2V15c0-1.4-.6-2.4-1.6-3 1-.6 1.6-1.6 1.6-3V6.2C7 5.4 7.2 5 8 5h1V3zm8 0h-1v2h1c.8 0 1 .4 1 1.2V9c0 1.4.6 2.4 1.6 3-1 .6-1.6 1.6-1.6 3v2.8c0 .8-.2 1.2-1 1.2h-1v2h1c2.2 0 3-1.2 3-3v-3c0-1 .6-2 2-2v-2c-1.4 0-2-1-2-2V6c0-1.8-.8-3-3-3z'/%3E%3C/svg%3E"); }
.tab-btn[data-tab="settings"]  { --ico: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M3 6h9v2H3zM16 6h5v2h-5zM3 16h5v2H3zM12 16h9v2h-9zM12 4h3v6h-3zM8 14h3v6H8z'/%3E%3C/svg%3E"); }
a.tab-btn[href^="swagger"]     { --ico: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M8.6 6.6 3.2 12l5.4 5.4L10 16l-4-4 4-4zM15.4 6.6 14 8l4 4-4 4 1.4 1.4 5.4-5.4z'/%3E%3C/svg%3E"); }
a.tab-btn[href^="help"]        { --ico: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20zm0 2a8 8 0 1 1 0 16 8 8 0 0 1 0-16zm-1 11h2v2h-2zm1-9a4 4 0 0 0-4 4h2a2 2 0 1 1 3 1.7c-1.2.7-2 1.5-2 3.3h2c0-1 .3-1.4 1.1-1.9A4 4 0 0 0 12 6z'/%3E%3C/svg%3E"); }

.tab-content { grid-column: 2; grid-row: 2; display: none; padding: 28px 36px; min-width: 0; }
.tab-content.active { display: block; }

/* ===== Containers: centered in the area right of the sidebar ===== */
.container { max-width: 900px; margin: 0 auto; }

/* ===== Cards ===== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
.card h3 { font-size: 15px !important; color: var(--text) !important; font-weight: 600; }

/* ===== Settings: device backup row and optional sections ===== */
.device-backup {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}
.device-backup-title { font-size: 13px; font-weight: 600; margin-bottom: 2px; }
.device-backup-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.settings-group-title { font-size: 15px; font-weight: 600; margin: 28px 0 2px; }
.settings-group-hint { font-size: 12px; color: var(--text-muted); margin-bottom: 12px; }

.opt-section { padding: 0; margin-bottom: 8px; }
.section-head {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  cursor: pointer;
  user-select: none;
  border-radius: var(--radius);
}
.section-head:hover { background: var(--surface2); }
.opt-section.open .section-head { border-radius: var(--radius) var(--radius) 0 0; }
.section-heading { flex: 1; min-width: 0; }
.section-title { margin: 0; }
.section-desc { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.section-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  max-width: 40%;
  overflow: hidden;
}
.section-status > span:last-child { overflow: hidden; text-overflow: ellipsis; }
.section-head .toggle { cursor: pointer; }
.section-chevron {
  flex-shrink: 0;
  width: 28px; height: 28px;
  display: inline-flex; align-items: center; justify-content: center;
  background: none; border: 0; border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
}
.section-chevron:hover { color: var(--text); }
.section-chevron svg { transition: transform .15s ease; }
.opt-section.open .section-chevron svg { transform: rotate(180deg); }
.section-body { display: none; padding: 16px; border-top: 1px solid var(--border); }
.opt-section.open .section-body { display: block; }
@media (max-width: 640px) {
  .section-head { flex-wrap: wrap; gap: 8px 12px; }
  .section-heading { flex-basis: calc(100% - 100px); }
  .section-status { order: 3; max-width: 100%; flex-basis: 100%; }
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition), color var(--transition), opacity var(--transition);
  white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary   { background: var(--btn); color: var(--on-btn); font-weight: 600; }
.btn-primary:hover { background: var(--btn-hover); }
.btn-danger    { background: var(--accent-danger); color: #fff; }
.btn-danger:hover { opacity: .85; }
.btn-ghost     { background: transparent; color: var(--text); border: 1px solid transparent; }
.btn-ghost:hover { background: color-mix(in srgb, var(--text) 7%, transparent); }
.btn-success   { background: var(--accent-success); color: #fff; }
.btn-template  { background: transparent; color: var(--text); border: 1px solid var(--text); }
.btn-template:hover { background: var(--text); color: var(--surface); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-icon { padding: 6px; color: var(--text-muted); }
.btn-icon svg { width: 18px; height: 18px; }

/* ===== Rule List: one flat table of rows ===== */
.rule-list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
.rule-list-header h2, .event-log-header h2 { font-size: 20px !important; font-weight: 600; letter-spacing: -.01em; }

#rules-list:has(.rule-card) {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.rule-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: background var(--transition);
}
.rule-card:hover { background: var(--surface2); }
.rule-card.disabled { opacity: .55; }
.reorder-arrows { opacity: 0; transition: opacity var(--transition); }
.rule-card:hover .reorder-arrows, .reorder-arrows:focus-within { opacity: 1; }
.rule-actions .btn-icon { opacity: .55; }
.rule-card:hover .rule-actions .btn-icon, .rule-actions .btn-icon:focus-visible { opacity: 1; }

.rule-meta { flex: 1; min-width: 0; }
.rule-name-row { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.rule-uuid { font-size: 10px; color: var(--text-muted); opacity: 0.55; cursor: pointer; user-select: all; white-space: nowrap; }
.rule-uuid:hover { opacity: 1; color: var(--accent); }
.rule-name {
  font-weight: 600;
  font-size: 13.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rule-badges { display: flex; gap: 6px; margin-top: 4px; flex-wrap: wrap; }
/* Neutral chips; trigger/condition/action are told apart by a small marker */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  padding: 1px 9px;
  border-radius: 999px;
  font-weight: 500;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.badge-trigger::before, .badge-cond::before, .badge-action::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
}
.badge-trigger::before { background: var(--mark-trigger); }
.badge-cond::before    { background: var(--mark-cond); }
.badge-action::before  { background: var(--mark-action); }

.rule-actions { display: flex; gap: 6px; flex-shrink: 0; }

/* Toggle */
.toggle {
  position: relative;
  width: 34px;
  height: 18px;
  flex-shrink: 0;
}
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute;
  inset: 0;
  background: #c4c4c4;
  border-radius: 20px;
  cursor: pointer;
  transition: background var(--transition);
}
[data-theme="dark"] .toggle-slider { background: #4a4a4a; }
.toggle-slider::before {
  content: '';
  position: absolute;
  width: 14px; height: 14px;
  left: 2px; top: 2px;
  background: #fff;
  border-radius: 50%;
  transition: transform var(--transition);
  box-shadow: 0 1px 2px rgba(0,0,0,.25);
}
.toggle input:checked + .toggle-slider { background: var(--accent-success); }
.toggle input:checked + .toggle-slider::before { transform: translateX(14px); background: #fff; }

/* Last fired */
.rule-fired-time { font-size: 11px; color: var(--text-dim); flex-shrink: 0; }

/* ===== Event Log ===== */
#tab-log .event-log-header { flex-wrap: wrap; gap: 8px; }
#tab-rules .container, #tab-settings .container { max-width: 1100px; }
#tab-log .container { max-width: 1400px; }
.event-log-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.event-table { width: 100%; border-collapse: collapse; }
.event-table th, .event-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
}
.event-table th, .var-table th { color: var(--text-muted); font-size: 11.5px; font-weight: 600; background: var(--surface); }
.event-table td { font-size: 12.5px; padding: 9px 12px; }
.event-table tr:hover td { background: var(--surface2); }

.event-fired   { color: var(--accent-success); font-weight: 600; }
.event-blocked { color: var(--text-muted); }
.event-badge-fired, [data-theme] .event-badge-fired { background: transparent; color: var(--accent-success); border: 0; padding-left: 0; }
.event-badge-blocked, [data-theme] .event-badge-blocked { background: transparent; color: var(--text-dim); border: 0; padding-left: 0; }
.event-badge-fired::before   { content: "\25CF"; font-size: 9px; }
.event-badge-blocked::before { content: "\25CB"; font-size: 9px; }
.event-badge-info    { background: transparent; color: var(--text-muted); border-color: var(--border); }
.var-persist, .var-initial {
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  padding: 4px 8px; border-radius: 6px; font-size: 12px;
}
.event-badge-warning { background: #3a2f12; color: #f5b942; border-color: #5a4a1c; }
.event-badge-error   { background: #3a1616; color: #f87171; border-color: #5c2222; }
[data-theme="light"] .event-badge-warning { background: #fef3c7; color: #92400e; border-color: #fde68a; }
[data-theme="light"] .event-badge-error   { background: #fee2e2; color: #991b1b; border-color: #fecaca; }

/* ===== Variables ===== */
.var-table { width: 100%; border-collapse: collapse; }
.var-table th, .var-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.var-value-cell { font-family: var(--mono); color: var(--text); }

/* ===== Status ===== */
.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.stat-value { font-size: 26px; font-weight: 600; color: var(--text); }
.stat-label { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* ===== Modal Overlay ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .28);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
  overflow: hidden;
}
.modal-overlay.hidden { display: none; }

/* Centred dialog: header and footer stay put, only the body scrolls */
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 100%;
  max-width: 900px;
  height: 100%;
  max-height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .22);
  animation: dialogIn .18s ease;
}

@keyframes dialogIn {
  from { opacity: 0; transform: translateY(8px) scale(.985); }
  to   { opacity: 1; transform: none; }
}

.modal-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-title { font-size: 16px; font-weight: 700; }
.modal-close {
  background: none; border: none;
  color: var(--text-muted); font-size: 20px;
  cursor: pointer; padding: 4px 8px; border-radius: 4px;
}
.modal-close:hover { color: var(--text); background: var(--surface2); }

.modal-body { padding: 20px; flex: 1; min-height: 0; overflow-y: auto; }

.modal-footer {
  flex-shrink: 0;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
}

/* ===== Form Elements ===== */
.form-row {
  display: flex;
  gap: 12px;
  margin-bottom: 14px;
}
.form-group {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
  /* let fields in a row shrink below their input's default width instead of
     pushing the row past the editor's border */
  min-width: 0;
}
.form-group label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: none;
  letter-spacing: 0;
}
.form-group input,
.form-group select,
.form-group textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  transition: border-color var(--transition), box-shadow var(--transition);
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}
.form-group textarea { resize: vertical; min-height: 70px; }
.form-group select option { background: var(--surface); }

/* Modbus register map / values table: fixed columns that fit the editor; the
   wrapper scrolls only on screens narrower than the table's minimum. */
.mb-scroll { overflow-x: auto; max-width: 100%; }
.mb-table { width: 100%; min-width: 520px; table-layout: fixed; border-collapse: collapse; font-size: 12px; }
.mb-table th { text-align: left; font-weight: 500; padding: 2px 3px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mb-table td { padding: 2px 3px 2px 0; }
.mb-table td:last-child { padding-right: 0; text-align: center; }
.form-group .mb-table input,
.form-group .mb-table select { width: 100%; min-width: 0; box-sizing: border-box; padding: 5px 6px; font-size: 12px; }
.mb-table .tca-remove { margin: 0; padding: 2px 4px; }
.form-group .mb-table input:disabled { opacity: .35; cursor: not-allowed; }

.form-hint { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.form-hint code { overflow-wrap: anywhere; }
.form-hint .btn { max-width: 100%; white-space: normal; }
.form-warning {
  font-size: 12px;
  color: var(--accent-warning);
  background: color-mix(in srgb, var(--accent-warning) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent-warning) 40%, transparent);
  border-left: 3px solid var(--accent-warning);
  border-radius: 4px;
  padding: 8px 10px;
  margin-top: 6px;
  line-height: 1.4;
}

/* ===== Section headers ===== */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 16px 0 8px;
}
.section-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .03em;
}
.logic-toggle { display: flex; gap: 4px; }
.logic-btn {
  padding: 4px 10px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
}
.logic-btn.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

/* ===== Trigger / Condition / Action rows ===== */
.tca-row {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
  margin-bottom: 8px;
  position: relative;
}
.tca-row-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.tca-row-header select { min-width: 0; flex: 0 1 auto; }
.tca-type-badge {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: .4px;
}
.tca-trigger { background: #e7f2fb; color: #0d4f7a; }
.tca-condition { background: #fbf5e7; color: #836000; }
.tca-action { background: #eaf6ef; color: #1f6a3f; }
[data-theme="light"] .form-group input,
[data-theme="light"] .form-group select,
[data-theme="light"] .form-group textarea { background: #ffffff; border-color: #c4c4c4; }

[data-theme="light"] .tca-trigger   { background: #dbeafe; color: #1d4ed8; }
[data-theme="light"] .tca-condition { background: #fef9c3; color: #a16207; }
[data-theme="light"] .tca-action    { background: #dcfce7; color: #15803d; }

.tca-remove {
  margin-left: auto;
  background: none; border: none;
  color: var(--text-dim); font-size: 16px;
  cursor: pointer; padding: 2px 6px; border-radius: 4px;
}
.tca-remove:hover { color: var(--accent-danger); background: rgba(224,82,82,.1); }

.tca-fields { display: flex; flex-direction: column; gap: 8px; }
.tca-fields .form-row { margin-bottom: 0; }

.add-btn {
  width: 100%;
  padding: 9px;
  border: 1px dashed var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  margin-bottom: 16px;
  transition: all var(--transition);
}
.add-btn.add-trigger { border-color: #cfe4f6; color: #0d4f7a; background: rgba(13,79,122,.06); }
.add-btn.add-trigger:hover { background: #e7f2fb; border-color: #0d4f7a; }
.add-btn.add-condition { border-color: #efe2c2; color: #836000; background: rgba(131,96,0,.06); }
.add-btn.add-condition:hover { background: #fbf5e7; border-color: #836000; }
.add-btn.add-action { border-color: #d4ebdd; color: #1f6a3f; background: rgba(31,106,63,.06); }
.add-btn.add-action:hover { background: #eaf6ef; border-color: #1f6a3f; }
[data-theme="light"] .add-btn.add-trigger { border-color: #93c5fd; color: #1d4ed8; background: rgba(29,78,216,.05); }
[data-theme="light"] .add-btn.add-trigger:hover { background: #dbeafe; border-color: #1d4ed8; }
[data-theme="light"] .add-btn.add-condition { border-color: #fde68a; color: #a16207; background: rgba(161,98,7,.05); }
[data-theme="light"] .add-btn.add-condition:hover { background: #fef9c3; border-color: #a16207; }
[data-theme="light"] .add-btn.add-action { border-color: #86efac; color: #15803d; background: rgba(21,128,61,.05); }
[data-theme="light"] .add-btn.add-action:hover { background: #dcfce7; border-color: #15803d; }

/* ===== Bulk Actions ===== */
.bulk-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  margin-bottom: 12px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--signal);
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  animation: bulkBarIn .2s ease;
}
.reorder-arrows {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex-shrink: 0;
}
.reorder-arrows .btn-icon {
  font-size: 14px;
  line-height: 1;
  padding: 2px 4px;
  min-width: 0;
}
.reorder-arrows .btn-icon:disabled {
  opacity: 0.25;
  cursor: default;
}
.bulk-cb {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  accent-color: var(--accent);
  cursor: pointer;
}
.rule-card.bulk-selected {
  box-shadow: inset 3px 0 0 var(--signal);
}

/* ===== Error Dot ===== */
.rule-error-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent-danger);
  display: inline-block;
  flex-shrink: 0;
  margin-right: 2px;
  animation: pulse-error 2s infinite;
}
@keyframes pulse-error {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ===== Tag Badge ===== */
.badge-tag {
  background: var(--surface2);
  color: var(--text);
  border-color: transparent;
}

/* ===== Update Banner ===== */
#update-banner {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 20px;
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: 10px;
  box-shadow: var(--shadow);
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  animation: slideDown .3s ease;
}
#update-banner .update-banner-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
}
@keyframes bulkBarIn {
  from { opacity: 0; transform: translateY(-8px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes slideDown {
  from { opacity: 0; transform: translateX(-50%) translateY(-16px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Toast ===== */
.toast-container {
  position: fixed;
  bottom: 20px; right: 20px;
  z-index: 500;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 6px;
  padding: 10px 16px;
  font-size: 13px;
  animation: slideInRight .2s ease;
  min-width: 200px;
  box-shadow: var(--shadow);
}
.toast.success { border-left-color: var(--accent-success); }
.toast.error   { border-left-color: var(--accent-danger); }
.toast.warning { border-left-color: var(--accent-warning); }

@keyframes slideInRight {
  from { opacity: 0; transform: translateX(20px); }
  to   { opacity: 1; transform: translateX(0); }
}

/* ===== Empty state ===== */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}
.empty-state .empty-icon { font-size: 40px; margin-bottom: 12px; }
.empty-state h3 { font-size: 16px; margin-bottom: 8px; }
.empty-state p { font-size: 13px; }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

/* ===== Focus & Accessibility ===== */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button:focus-visible, .btn:focus-visible, .tab-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ===== Print ===== */
@media print {
  body { background: #fff !important; color: #000 !important; }
  header, .tab-bar, .btn, .toggle, #toast-container, .modal-overlay { display: none !important; }
  .tab-content { display: block !important; padding: 0; }
  .rule-card { break-inside: avoid; border: 1px solid #ccc; }
  .event-table th, .event-table td { border: 1px solid #ccc; }
}

/* ===== Responsive ===== */
/* Touch screens have no hover, so keep row controls fully visible */
@media (hover: none) {
  .reorder-arrows, .rule-actions .btn-icon { opacity: 1; }
}

/* Narrow screens: sidebar becomes a scrollable tab strip under the header */
@media (max-width: 860px) {
  #app { grid-template-columns: minmax(0, 1fr); grid-template-rows: var(--header-h) auto 1fr; }
  .tabs {
    grid-row: 2;
    flex-direction: row;
    height: auto;
    padding: 0 8px;
    border-right: 0;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    z-index: 90;
  }
  .tab-btn { border-left: 0; border-bottom: 3px solid transparent; padding: 10px 12px; }
  .tab-btn.active { border-bottom-color: var(--signal); }
  .tab-content { grid-column: 1; grid-row: 3; padding: 20px 16px; }
}

@media (max-width: 640px) {
  .status-grid { grid-template-columns: repeat(2, 1fr); }
  .form-row { flex-direction: column; }
  header h1 { font-size: 14px; }
  #tab-rules .rule-list-header { flex-wrap: wrap; gap: 8px; }
  #tab-rules .rule-list-header > div { flex-wrap: wrap; }

  /* Modal full-screen on mobile */
  .modal-overlay {
    padding: 0;
    align-items: flex-end;
  }
  .modal {
    max-width: 100%;
    height: auto;
    border-width: 1px 0 0;
    border-radius: var(--radius) var(--radius) 0 0;
    max-height: 92vh;
    max-height: 92dvh;
  }
  .modal-body {
    padding: 14px;
  }

  /* Larger touch targets */
  .btn { min-height: 42px; padding: 10px 14px; }
  .btn-sm { min-height: 36px; }
  .btn-icon { min-width: 36px; min-height: 36px; }
  .tab-btn { padding: 12px 12px; }
  .toggle { width: 44px; height: 24px; }
  .toggle-slider::before { width: 18px; height: 18px; }
  .toggle input:checked + .toggle-slider::before { transform: translateX(20px); }

  /* Stack rule card actions */
  .rule-card { flex-wrap: wrap; }
  .rule-actions { width: 100%; justify-content: flex-end; margin-top: 6px; }

  /* Wider rule list search */
  #rule-search { width: 100% !important; }

  /* Event table — hide details on tiny screens, keep key cols */
  .event-table th:last-child,
  .event-table td:last-child { display: none; }

  /* Reduce container padding */
  .tab-content { padding: 12px; }
  .container { padding: 0; }
}
