/* ===========================================================================
 * Automations view — cron-driven proactive agent runs
 * ===========================================================================
 * Mirrors bridges.css conventions so the two sibling views feel consistent.
 * Mobile reflow at the standard --bp-mobile (768px) breakpoint.
 */

#automations-view,
.openking-pane[data-pane="automations"] {
    padding: 24px 32px;
    background: var(--bg-body);
    overflow-y: auto;
    flex: 1;
    min-width: 0;
}

#automations-view .view-header.automations-header,
.openking-pane[data-pane="automations"] .view-header.automations-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

#automations-view .view-header h1,
.openking-pane[data-pane="automations"] .view-header h1 {
    margin: 0;
    font-size: 1.5rem;
}

.automations-subtitle {
    color: var(--text-secondary);
    margin-top: 4px;
    font-size: 0.9rem;
}

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

.automations-toolbar {
    display: flex;
    gap: 12px;
    align-items: center;
    margin: 16px 0;
}

.automations-toolbar input.search-input {
    flex: 1;
    min-width: 0;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 10px 14px;
    color: var(--text-primary);
    font-size: 0.9rem;
}

.automations-toolbar select.category-filter {
    min-width: 160px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 10px 14px;
    color: var(--text-primary);
    font-size: 0.9rem;
    cursor: pointer;
}

.automations-grid {
    display: grid;
    /* 360px min so the head row (status dot + title + 5 action buttons)
     * fits on a single line even when the title is medium-length. */
    grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
    gap: 18px;
}

.automations-empty {
    grid-column: 1 / -1;
    text-align: center;
    padding: 60px 20px;
    color: var(--text-secondary);
}

.automations-empty .empty-icon {
    font-size: 3rem;
    margin-bottom: 12px;
}

/* ============== Card ============================================== */

.automation-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 16px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-height: 200px;
    /* Critical: hides content that would otherwise bleed past the card
     * border when a meta-row contains an unbreakable string (long URL,
     * tightly-packed cron, etc.). Inner rows handle wrap individually. */
    overflow: hidden;
    min-width: 0;
}

.automation-card:hover {
    transform: translateY(-2px);
    border-color: rgba(243, 123, 32, 0.4);
    box-shadow: 0 12px 32px -12px rgba(0, 0, 0, 0.45),
                0 0 0 1px rgba(243, 123, 32, 0.15);
}

.automation-card.selected {
    border-color: var(--accent-color, #f37b20);
    box-shadow: 0 0 0 1px var(--accent-color, #f37b20),
                0 8px 24px -10px rgba(243, 123, 32, 0.3);
}

.automation-card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    min-width: 0;
}

.automation-card-title {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.automation-status-dot {
    flex-shrink: 0;
}

.automation-card-name {
    font-weight: 600;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

.automation-actions {
    display: flex;
    gap: 2px;
    flex-shrink: 0;
    align-items: center;
}

.automation-actions button {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-secondary);
    /* Tighter padding — 5 buttons in the head row need to fit alongside
     * the title without forcing the head to wrap. */
    padding: 4px 6px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.95rem;
    line-height: 1;
    transition: all 0.15s;
}

.automation-actions button:hover {
    background: var(--bg-surface-hover);
    border-color: var(--border-color);
    color: var(--text-primary);
}

.automation-actions button[data-action="run-now"]:hover {
    background: rgba(80, 220, 130, 0.15);
    border-color: rgba(80, 220, 130, 0.35);
    color: #80e7a3;
}

.automation-actions button.running {
    opacity: 0.7;
    cursor: progress;
    animation: openking-pulse 1.2s ease-in-out infinite;
}

@keyframes openking-pulse {
    0%, 100% { opacity: 0.5; }
    50% { opacity: 1; }
}

.auto-runnow-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.automation-card-meta {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
}

.automation-meta-row {
    font-size: 0.82rem;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

/* Prompt preview — pulled out of the meta-row stack into its own
 * styled box so long URLs / unbreakable tokens have a clean container
 * and the line-clamp behaves predictably. Label sits above the box so
 * users still know "this is what the agent will run on every tick". */
.automation-prompt-wrap {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.automation-prompt-label {
    font-size: 0.7rem;
    color: var(--text-tertiary, #8d92a8);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 600;
}

.automation-prompt-box {
    background: var(--bg-body);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 8px 10px;
    font-size: 0.78rem;
    line-height: 1.4;
    color: var(--text-secondary);
    font-style: italic;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow-wrap: anywhere;
    word-break: break-word;
    max-width: 100%;
    box-sizing: border-box;
}

.automation-meta-row .meta-key {
    color: var(--text-tertiary, #8d92a8);
    margin-right: 4px;
}

.automation-meta-row code {
    background: var(--bg-surface-hover);
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 0.78rem;
}

.automation-card-foot {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    margin-top: auto;
    padding-top: 8px;
    border-top: 1px solid var(--border-color);
}

.auto-badge {
    font-size: 0.72rem;
    padding: 3px 8px;
    border-radius: 100px;
    background: var(--bg-surface-hover);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
}

.auto-badge.failures {
    background: rgba(255, 92, 122, 0.12);
    color: #ff8d9d;
    border-color: rgba(255, 92, 122, 0.3);
}

.auto-badge.cron-missing {
    background: rgba(255, 180, 84, 0.12);
    color: #ffc97a;
    border-color: rgba(255, 180, 84, 0.35);
}

.auto-badge.cron-synced {
    background: rgba(80, 220, 130, 0.10);
    color: #80e7a3;
    border-color: rgba(80, 220, 130, 0.30);
}

.auto-badge.cooldown {
    background: rgba(255, 215, 90, 0.12);
    color: #f0d575;
    border-color: rgba(255, 215, 90, 0.35);
}

.auto-badge.source-tag {
    background: rgba(120, 140, 255, 0.10);
    color: #a3aef0;
    border-color: rgba(120, 140, 255, 0.30);
}

.auto-badge.activity {
    background: rgba(150, 200, 255, 0.10);
    color: #b8d4ff;
    border-color: rgba(150, 200, 255, 0.30);
}

.auto-badge.activity-warn {
    background: rgba(255, 170, 60, 0.12);
    color: #ffc97a;
    border-color: rgba(255, 170, 60, 0.35);
}

/* ============== Bulk select ====================================== */

.automation-bulk-cb {
    width: 14px;
    height: 14px;
    cursor: pointer;
    accent-color: var(--accent-color, #f37b20);
    /* Hidden until the user hovers the card or the card is already
     * selected. Keeps the title row uncluttered for the 95% case. */
    opacity: 0;
    transition: opacity 0.15s;
    flex-shrink: 0;
}

.automation-card:hover .automation-bulk-cb,
.automation-card.selected .automation-bulk-cb,
.automation-bulk-cb:checked,
.automation-bulk-cb:focus {
    opacity: 1;
}

.automation-card.selected {
    border-color: var(--accent-color, #f37b20);
    box-shadow: 0 0 0 1px var(--accent-color, #f37b20);
}

.automations-bulk-bar {
    position: sticky;
    bottom: 0;
    margin-top: 16px;
    padding: 12px 16px;
    background: var(--bg-surface);
    border: 1px solid var(--accent-color, #f37b20);
    border-radius: 14px;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    box-shadow: 0 -4px 16px -8px rgba(0, 0, 0, 0.3);
    z-index: 5;
}

.automations-bulk-bar[hidden] { display: none; }

.automations-bulk-count {
    font-weight: 600;
    color: var(--accent-color, #f37b20);
    margin-right: 4px;
    font-size: 0.9rem;
}

.automations-bulk-bar button {
    font-size: 0.85rem;
    padding: 6px 12px;
    border-radius: 8px;
}

.automations-warning-banner {
    background: rgba(255, 180, 84, 0.12);
    border: 1px solid rgba(255, 180, 84, 0.35);
    border-radius: 10px;
    padding: 10px 14px;
    margin: 0 0 12px 0;
    color: #ffc97a;
    font-size: 0.85rem;
    line-height: 1.5;
}

.automations-warning-banner code {
    background: rgba(0, 0, 0, 0.25);
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 0.78rem;
}

/* ============== Emergency pause button ============================ */

.automations-emergency-btn {
    background: rgba(255, 180, 84, 0.15);
    border: 1px solid rgba(255, 180, 84, 0.3);
    color: #ffc97a;
    border-radius: 10px;
    padding: 8px 14px;
    cursor: pointer;
    transition: all 0.15s;
    font-size: 0.85rem;
}

.automations-emergency-btn:hover {
    background: rgba(255, 92, 122, 0.2);
    border-color: rgba(255, 92, 122, 0.4);
    color: #ff8d9d;
}

/* ============== Form modal ======================================== */

/* Modal surface — automation/run-now/run-history use class="modal" not
 * "modal-content", so the base modal.css surface rule misses them and
 * the content renders fully transparent over the overlay. Paint it
 * here for every `.modal` produced by Automations + Open King views. */
.modal-overlay .modal,
.automation-modal {
    background: var(--bg-surface);
    border-radius: 16px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
    max-width: 640px;
    width: 100%;
    max-height: calc(100dvh - 32px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* The modal's body needs its own scroll context so the header/footer
 * stay pinned and only the form area scrolls. */
.modal-overlay .modal > .modal-body {
    overflow-y: auto;
    flex: 1;
    -webkit-overflow-scrolling: touch;
}

.automation-modal .form-group {
    margin-bottom: 16px;
}

.automation-modal .form-group label {
    display: block;
    margin-bottom: 6px;
    font-size: 0.85rem;
    color: var(--text-primary);
    font-weight: 500;
}

.automation-modal .form-control {
    width: 100%;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px 12px;
    color: var(--text-primary);
    font-size: 0.9rem;
    box-sizing: border-box;
}

.automation-modal textarea.form-control {
    resize: vertical;
    font-family: inherit;
}

.automation-modal .info-note {
    font-size: 0.78rem;
    color: var(--text-secondary);
    margin-top: 6px;
    line-height: 1.4;
}

.automation-modal .form-error {
    color: var(--danger, #ff5c7a);
    font-size: 0.85rem;
    margin-top: 8px;
    min-height: 1.2em;
}

/* ============== Output target rows ================================ */

.auto-target-row {
    background: var(--bg-surface-hover);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 10px;
    margin-bottom: 8px;
}

.auto-target-row-head {
    display: flex;
    gap: 8px;
    align-items: center;
}

.auto-target-row-head select {
    flex: 1;
}

.auto-target-remove {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    width: 30px;
    height: 30px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1.2rem;
    line-height: 1;
}

.auto-target-remove:hover {
    background: rgba(255, 92, 122, 0.15);
    border-color: rgba(255, 92, 122, 0.35);
    color: #ff8d9d;
}

.auto-target-row-detail {
    margin-top: 8px;
}

/* ============== Run history rows ================================== */

.auto-run-row {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background 0.15s;
}

.auto-run-row:hover,
.auto-run-row.expanded {
    background: var(--bg-surface-hover);
}

.auto-run-row-head {
    display: flex;
    gap: 8px;
    align-items: center;
}

.auto-run-source {
    font-size: 0.78rem;
    color: var(--text-secondary);
}

.auto-run-headline {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.86rem;
}

.auto-run-time {
    font-size: 0.78rem;
    color: var(--text-secondary);
    flex-shrink: 0;
}

.auto-run-detail {
    margin-top: 10px;
    font-size: 0.85rem;
    line-height: 1.5;
}

.auto-run-detail > div {
    margin-bottom: 6px;
}

.auto-run-detail pre {
    white-space: pre-wrap;
    background: var(--bg-body);
    padding: 8px 10px;
    border-radius: 6px;
    margin: 4px 0;
    font-size: 0.78rem;
    line-height: 1.4;
    max-height: 240px;
    overflow-y: auto;
    border: 1px solid var(--border-color);
}

.auto-run-error pre {
    color: #ff8d9d;
}

.auto-dispatch-pill {
    display: inline-block;
    font-size: 0.72rem;
    padding: 2px 8px;
    border-radius: 100px;
    margin-right: 4px;
    border: 1px solid var(--border-color);
}

.auto-dispatch-pill.ok {
    background: rgba(80, 220, 130, 0.12);
    color: #80e7a3;
    border-color: rgba(80, 220, 130, 0.35);
}

.auto-dispatch-pill.fail {
    background: rgba(255, 92, 122, 0.12);
    color: #ff8d9d;
    border-color: rgba(255, 92, 122, 0.3);
}

.auto-run-failures {
    margin-top: 10px;
    padding: 10px 12px;
    background: rgba(255, 92, 122, 0.06);
    border: 1px solid rgba(255, 92, 122, 0.2);
    border-radius: 8px;
}

.auto-run-failures > strong {
    display: block;
    margin-bottom: 8px;
    color: #ff8d9d;
    font-size: 0.85rem;
}

.auto-run-failure-card {
    background: var(--bg-body);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 8px 10px;
    margin-bottom: 6px;
}

.auto-run-failure-card:last-child { margin-bottom: 0; }

.auto-run-failure-head {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 4px;
}

.auto-run-failure-url {
    font-size: 0.72rem;
    color: var(--text-secondary);
    background: var(--bg-surface-hover);
    padding: 1px 6px;
    border-radius: 4px;
}

.auto-run-failure-msg {
    font-size: 0.82rem;
    color: var(--text-primary);
    line-height: 1.4;
    word-break: break-word;
}

/* ============== Mobile (≤768px) =================================== */

@media (max-width: 768px) {
    #automations-view,
.openking-pane[data-pane="automations"] {
        padding: 16px 12px;
    }

    #automations-view .view-header.automations-header,
.openking-pane[data-pane="automations"] .view-header.automations-header {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
    }

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

    .automations-header-actions > button {
        flex: 1;
    }

    .automations-toolbar {
        flex-direction: column;
        gap: 8px;
        margin: 12px 0;
    }

    .automations-toolbar input,
    .automations-toolbar select {
        width: 100%;
    }

    .automations-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .automation-card {
        min-height: 180px;
        padding: 14px;
    }

    .automation-actions button {
        min-width: 36px;
        min-height: 36px;
    }

    .automation-modal {
        max-width: 100%;
        margin: 10px;
    }

    .auto-run-row-head {
        flex-wrap: wrap;
    }

    .auto-run-time {
        font-size: 0.7rem;
    }
}
