/* Outlook-like Rules UI */

:root {
    --rules-bg: #f5f6f8;
    --rules-surface: #ffffff;
    --rules-border: #e1e5ea;
    --rules-text: #242424;
    --rules-muted: #5e5e5e;
    --rules-strong: #1a1a1a;
    --rules-brand: #0f6cbd;
    --rules-brand-hover: #0d5fa7;
    --rules-brand-weak: #e6f0fb;
    --rules-danger: #c42b1c;
}

.rules-shell {
    min-height: 100%;
    padding: 16px 18px;
    background: var(--rules-bg);
    display: flex;
    flex-direction: column;
    gap: 14px;
    color: var(--rules-text);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

body.modal-open { overflow: hidden; }

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.rules-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    z-index: 11000;
}

.rules-overlay.show {
    opacity: 1;
    pointer-events: auto;
}

.rules-overlay-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
}

.rules-overlay-dialog {
    position: relative;
    background: var(--rules-surface);
    border-radius: 12px;
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.18);
    width: 960px;
    max-width: calc(100vw - 48px);
    max-height: calc(100vh - 48px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.rules-overlay-topbar {
    display: flex;
    justify-content: flex-end;
    padding: 10px 12px 0;
}

.rules-overlay-close {
    border: none;
    background: none;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: grid;
    place-items: center;
    color: var(--rules-muted);
    cursor: pointer;
    transition: background-color 0.15s, color 0.15s;
}

.rules-overlay-close:hover { background: #f3f4f6; color: var(--rules-strong); }

.rules-overlay-body {
    position: relative;
    overflow: auto;
    padding: 0 16px 16px;
}

.rules-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    background: var(--rules-surface);
    border: 1px solid var(--rules-border);
    border-radius: 10px;
    padding: 14px 16px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.rules-heading {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.rules-page-title {
    font-size: 16px;
    font-weight: 700;
    color: var(--rules-strong);
}

.rules-page-desc {
    font-size: 13px;
    color: var(--rules-muted);
    max-width: 540px;
    line-height: 1.4;
}

.rules-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--rules-surface);
    border: 1px solid var(--rules-border);
    border-radius: 10px;
    padding: 10px 12px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
    flex-wrap: wrap; /* Allow toolbar buttons to wrap */
    gap: 8px; /* Add gap between toolbar buttons */
    gap: 8px;
}

.rules-titleblock {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.rules-subtitle {
    font-size: 12px;
    color: var(--rules-muted);
}

.rules-actions {
    display: flex;
    align-items: center;
    gap: 8px; /* Maintain gap between actions */
    flex-wrap: wrap; /* Allow actions to wrap */
    flex-wrap: wrap;
}

.rules-btn {
    border: 1px solid var(--rules-border);
    background: var(--rules-surface);
    color: var(--rules-text);
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s, box-shadow 0.15s;
}

.rules-btn svg { color: currentColor; }

.rules-btn:hover {
    background: #f3f4f6;
    border-color: #d1d5db;
}

.rules-btn.primary {
    background: var(--rules-brand);
    border-color: var(--rules-brand);
    color: #fff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.rules-btn.primary:hover { background: var(--rules-brand-hover); }

.rules-btn.ghost { background: #f8fafc; }

.rules-link {
    color: var(--rules-brand);
    text-decoration: none;
    font-weight: 600;
}

.rules-link:hover { text-decoration: underline; }

.rules-footnote {
    font-size: 12px;
    color: var(--rules-muted);
    padding: 4px 2px;
}

.rules-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.rule-card {
    background: var(--rules-surface);
    border: 1px solid var(--rules-border);
    border-radius: 10px;
    padding: 12px 14px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
    transition: border-color 0.15s, box-shadow 0.15s;
}

.rule-card:hover {
    border-color: #d0d7e2;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.06);
}

.rule-card.is-disabled {
    opacity: 0.65;
    background: #f9fbfd;
}

.rule-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.rule-leading {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.rule-handle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border: 1px solid var(--rules-border);
    border-radius: 8px;
    background: #f7f9fc;
    color: var(--rules-muted);
}

.rule-name {
    font-weight: 700;
    color: var(--rules-strong);
    font-size: 14px;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.rule-badges {
    display: inline-flex;
    gap: 6px;
    align-items: center;
}

.rule-pill {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 6px;
    border-radius: 999px;
    font-size: 11px;
    border: 1px solid var(--rules-border);
    color: var(--rules-muted);
    background: #f3f4f6;
}

.rule-pill.error { color: var(--rules-danger); border-color: #f6d6d3; background: #fff2f0; }
.rule-pill.readonly { color: var(--rules-muted); }

.rule-controls {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
}

.rules-switch {
    position: relative;
    display: inline-flex;
    align-items: center;
    width: 46px;
    height: 24px;
}

.rules-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.rules-switch-slider {
    position: absolute;
    inset: 0;
    background: #d1d5db;
    border-radius: 999px;
    transition: background 0.2s;
}

.rules-switch-slider::before {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    left: 2px;
    top: 2px;
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
    transition: transform 0.2s;
}

.rules-switch input:checked + .rules-switch-slider { background: var(--rules-brand); }
.rules-switch input:checked + .rules-switch-slider::before { transform: translateX(22px); }
.rules-switch input:disabled + .rules-switch-slider { opacity: 0.5; }

.rules-icon-btn {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    border: 1px solid var(--rules-border);
    background: var(--rules-surface);
    display: grid;
    place-items: center;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s, color 0.15s;
}

.rules-icon-btn:hover {
    background: #f3f4f6;
    border-color: #d1d5db;
}

.rules-icon-btn.subtle { color: var(--rules-muted); }
.rules-icon-btn.danger:hover { border-color: #f4c7c3; background: #fff5f4; color: var(--rules-danger); }
.rules-icon-btn:disabled { cursor: not-allowed; opacity: 0.6; }

.rule-desc {
    margin: 8px 0 6px;
    color: var(--rules-muted);
    font-size: 13px;
}

.rule-desc.is-hidden { display: none; }

.rule-meta-line {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    font-size: 13px;
    color: var(--rules-muted);
}

.rule-chip.label {
    background: #eef2ff;
    color: #1d4ed8;
    border-radius: 999px;
    padding: 2px 8px;
    font-weight: 700;
    font-size: 12px;
}

.rule-text { color: var(--rules-text); }
.rule-separator { color: var(--rules-muted); }

.rule-footer {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--rules-muted);
}

.rule-link {
    border: none;
    background: none;
    padding: 0;
    color: var(--rules-brand);
    font-weight: 600;
    cursor: pointer;
}

.rule-link:hover { text-decoration: underline; }
.rule-link.danger { color: var(--rules-danger); }
.rule-link:disabled { color: #9aa3ad; cursor: not-allowed; text-decoration: none; }
.rule-dot { color: var(--rules-muted); }

.rules-empty {
    background: var(--rules-surface);
    border: 1px dashed var(--rules-border);
    border-radius: 12px;
    padding: 32px 24px;
    text-align: center;
    display: grid;
    gap: 12px;
    justify-items: center;
}

.rules-empty .empty-title { font-size: 18px; font-weight: 700; color: var(--rules-strong); }
.rules-empty .empty-copy { color: var(--rules-muted); font-size: 13px; max-width: 360px; }

.rules-loading, .rules-error {
    background: var(--rules-surface);
    border: 1px solid var(--rules-border);
    border-radius: 12px;
    padding: 24px;
    display: grid;
    gap: 12px;
    justify-items: center;
    text-align: center;
}

.rules-error .error-icon svg { stroke: var(--rules-danger); }

.spinner {
    width: 28px;
    height: 28px;
    border: 3px solid var(--rules-border);
    border-top-color: var(--rules-brand);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* Modal Styles (Rule Editor) */
.modal {
    position: fixed;
    inset: 0;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.modal.show { opacity: 1; }

.modal-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
}

.modal-content {
    position: relative;
    background: #ffffff;
    border-radius: 10px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
    max-height: 90vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    width: 660px;
    max-width: 92vw;
}

.modal-large { width: 920px; }

.modal-header {
    padding: 18px 22px;
    border-bottom: 1px solid var(--rules-border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h2 {
    margin: 0;
    font-size: 18px;
    font-weight: 700;
    color: var(--rules-strong);
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    color: var(--rules-muted);
    cursor: pointer;
    width: 32px;
    height: 32px;
    display: grid;
    place-items: center;
    border-radius: 8px;
    transition: background-color 0.2s, color 0.2s;
}

.modal-close:hover { background: #f3f4f6; color: var(--rules-strong); }

.modal-body {
    padding: 22px;
    overflow-y: auto;
    flex: 1;
}

.modal-footer {
    padding: 14px 22px;
    border-top: 1px solid var(--rules-border);
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}

/* Rule Form */
.rule-form { display: flex; flex-direction: column; gap: 18px; }

.form-section {
    border: 1px solid var(--rules-border);
    border-radius: 8px;
    padding: 14px;
    background: #fbfcfe;
}

.builder-section {
    margin-top: 12px;
    padding: 12px;
    border: 1px dashed var(--rules-border);
    border-radius: 8px;
    background: #f7f9fc;
}

.builder-controls {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    align-items: flex-end;
}

.builder-controls .form-group {
    flex: 1 1 220px;
    margin-bottom: 0;
}

.form-section h3 { margin: 0 0 10px 0; font-size: 15px; font-weight: 700; color: var(--rules-strong); }
.section-description { margin: 0 0 12px 0; font-size: 13px; color: var(--rules-muted); }
.form-group { margin-bottom: 12px; }
.form-group:last-child { margin-bottom: 0; }
.form-group label { display: block; margin-bottom: 6px; font-weight: 600; font-size: 13px; color: var(--rules-strong); }

.form-control {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid #cfd7e3;
    border-radius: 6px;
    font-size: 14px;
    font-family: inherit;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.form-control:focus {
    outline: none;
    border-color: var(--rules-brand);
    box-shadow: 0 0 0 1px var(--rules-brand-weak);
}

.form-group small { display: block; margin-top: 4px; font-size: 12px; color: var(--rules-muted); }

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

.condition-item,
.action-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px;
    background: #ffffff;
    border: 1px solid #e4e7ec;
    border-radius: 8px;
    margin-bottom: 8px;
}

.condition-type,
.action-type {
    font-weight: 600;
    color: var(--rules-brand);
    min-width: 180px;
    font-size: 13px;
}

.condition-value,
.action-value { flex: 1; font-size: 14px; }
.condition-item input.condition-value { padding: 6px 10px; border: 1px solid #d0d7e2; border-radius: 6px; }

.btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    padding: 6px;
    border: 1px solid var(--rules-border);
    background: var(--rules-surface);
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s, color 0.15s;
}

.btn-icon:hover { background: #f3f4f6; border-color: #d1d5db; }
.btn-danger { color: var(--rules-danger); border-color: #f4c7c3; background: #fff; }
.btn-danger:hover { background: #fff5f4; }

.empty-message {
    padding: 18px;
    text-align: center;
    color: #8a94a6;
    font-style: italic;
    font-size: 14px;
    background: #ffffff;
    border: 1px dashed #d0d7e2;
    border-radius: 8px;
}

/* Responsive */
@media (max-width: 768px) {
    .rules-header,
    .rules-toolbar { flex-direction: column; align-items: flex-start; gap: 10px; }
    .rule-top { flex-direction: column; align-items: flex-start; }
    .rule-controls { align-self: flex-end; }
    .form-row { grid-template-columns: 1fr; }
    .modal-content { width: 100%; max-width: 100vw; border-radius: 0; max-height: 100vh; }
    .condition-item,
    .action-item { flex-direction: column; align-items: flex-start; }
    .condition-type,
    .action-type { min-width: auto; }
}

/* Notifications */
.notification {
    position: fixed;
    top: 20px;
    right: 20px;
    padding: 12px 20px;
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 10000;
    display: flex;
    align-items: center;
    gap: 12px;
    animation: slideIn 0.3s ease;
}

@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

.notification.success { border-left: 4px solid #107c10; }
.notification.error { border-left: 4px solid var(--rules-danger); }
.notification.info { border-left: 4px solid var(--rules-brand); }

/* ===== DARK THEME OVERRIDES ===== */
body.theme-dark .rules-shell {
    background: #1a1a1a;
    color: var(--text);
}

body.theme-dark .rules-overlay-backdrop {
    background: rgba(8, 12, 18, 0.6);
}

body.theme-dark .rules-overlay-dialog,
body.theme-dark .rules-header,
body.theme-dark .rules-toolbar,
body.theme-dark .rule-card,
body.theme-dark .rules-empty,
body.theme-dark .rules-loading,
body.theme-dark .rules-error {
    background: #1a1a1a;
    color: var(--text);
    border-color: #2a2a2a;
}

body.theme-dark .rules-page-title,
body.theme-dark .rule-name,
body.theme-dark .rules-page-desc,
body.theme-dark .rules-subtitle,
body.theme-dark .rule-desc,
body.theme-dark .rule-meta-line,
body.theme-dark .rules-footnote {
    color: var(--text);
}

body.theme-dark .rules-overlay-close {
    color: #cbd5e1;
}

body.theme-dark .rules-overlay-close:hover {
    background: #1b2433;
    color: #e5e7eb;
}

body.theme-dark .rules-btn,
body.theme-dark .rules-icon-btn,
body.theme-dark .rule-pill,
body.theme-dark .rules-switch-slider,
body.theme-dark .btn-icon {
    background: #232323;
    color: #e5e7eb;
    border-color: #2a2a2a;
}

body.theme-dark .rules-btn:hover,
body.theme-dark .rules-icon-btn:hover,
body.theme-dark .btn-icon:hover {
    background: #2a2a2a;
    border-color: #303030;
}

body.theme-dark .rules-btn.primary {
    background: #0a64ff;
    border-color: #0a64ff;
    color: #f8fafc;
}

body.theme-dark .rules-switch-slider {
    background: #2a2a2a;
}

body.theme-dark .rules-switch-slider::before {
    background: #e5e7eb;
}

body.theme-dark .rule-pill.error {
    background: #2b1a20;
    border-color: #4a2a31;
    color: #ffb3bb;
}

body.theme-dark .rule-link {
    color: #9fc3ff;
}

body.theme-dark .form-section,
body.theme-dark .builder-section,
body.theme-dark .condition-item,
body.theme-dark .action-item,
body.theme-dark .empty-message,
body.theme-dark .modal-content,
body.theme-dark .modal-header,
body.theme-dark .modal-body,
body.theme-dark .modal-footer {
    background: #1a1a1a;
    color: var(--text);
    border-color: #2a2a2a;
}

body.theme-dark .form-control,
body.theme-dark .condition-item input.condition-value {
    background: #202020;
    color: var(--text);
    border-color: #2a2a2a;
}

body.theme-dark .form-group label,
body.theme-dark .form-section h3,
body.theme-dark .section-description {
    color: var(--text);
}