:root {
    --bg-dark: #0f172a;
    --accent: #f97316;
    --text-light: #f8fafc;
    --card-bg: #1e293b;
    --border: #334155;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    font-family: 'Inter', sans-serif;
    background: radial-gradient(circle at top right, #1e293b 0%, #0f172a 45%, #020617 100%);
    color: var(--text-light);
}

body.portal-dashboard-body {
    display: block;
    padding: 24px;
    width: 100%;
}

.portal-card {
    width: 100%;
    max-width: 420px;
    background: rgba(30, 41, 59, 0.92);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 28px;
    box-shadow: 0 20px 40px rgba(2, 6, 23, 0.55);
}

h1 {
    margin-top: 0;
    margin-bottom: 16px;
    font-size: 1.8rem;
}

p {
    color: #cbd5e1;
}

label {
    display: block;
    margin-top: 14px;
    margin-bottom: 6px;
    font-weight: 700;
}

input {
    width: 100%;
    padding: 11px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: #0b1120;
    color: var(--text-light);
}

button,
.portal-link {
    display: inline-block;
    margin-top: 18px;
    padding: 11px 16px;
    border-radius: 6px;
    border: none;
    background: var(--accent);
    color: #fff;
    text-decoration: none;
    font-weight: 700;
    cursor: pointer;
}

.portal-link {
    background: transparent;
    border: 1px solid var(--border);
    margin-left: 8px;
}

.notice {
    padding: 10px 12px;
    border-radius: 6px;
    margin: 12px 0;
    background: #1d4ed8;
    color: #e0ecff;
}

.error {
    background: #7f1d1d;
    color: #fee2e2;
}

.hint-text {
    margin: 8px 2px 2px;
    font-size: 0.92rem;
    color: #cbd5e1;
}

.portal-dashboard-shell {
    width: 100%;
    max-width: 1240px;
    margin: 0 auto;
}

.portal-dashboard-card {
    max-width: none;
}

.portal-dashboard-grid {
    display: grid;
    grid-template-columns: minmax(280px, 420px) minmax(0, 1fr);
    gap: 16px;
    align-items: start;
}

.portal-graph-stack {
    display: flex;
    flex-direction: column;
    gap: 16px;
    width: 100%;
}

.portal-panel {
    width: 100%;
    max-width: none;
    background: rgba(30, 41, 59, 0.92);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 18px;
    box-shadow: 0 12px 30px rgba(2, 6, 23, 0.35);
}

.portal-app-header {
    margin-bottom: 6px;
}

.portal-app-header h1 {
    margin-bottom: 0;
}

.portal-heading-row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.35rem 1.2rem;
    width: 100%;
    flex: 1 1 auto;
}

.portal-heading-brand {
    display: inline-flex;
    align-items: center;
    flex-wrap: nowrap;
    white-space: nowrap;
    gap: 0.5rem;
}

.portal-heading-logo {
    width: auto !important;
    max-width: none !important;
    height: 1.1em !important;
    max-height: 1.1em !important;
    display: inline-block;
    vertical-align: middle;
    flex: 0 0 auto;
    object-fit: contain;
}

.portal-heading-brand span {
    display: inline-block;
    line-height: 1;
}

.portal-signed-in {
    margin: 0 0 0 auto;
    font-size: 0.86rem;
    line-height: 1.2;
    color: #94a3b8;
}

.portal-sites-panel h2,
.portal-metrics-card h2 {
    margin: 0 0 10px;
    font-size: 1.15rem;
}

.portal-dashboard-footer {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-top: 18px;
    padding-bottom: 8px;
}

.portal-dashboard-footer .portal-link {
    display: block;
    width: min(220px, 100%);
    margin-left: 0;
    margin-top: 0;
    text-align: center;
}

.portal-dashboard-footer form {
    display: block;
    width: min(220px, 100%);
    margin: 0;
}

.portal-dashboard-footer form .portal-link {
    width: 100%;
}

.portal-link-admin {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.portal-link-admin:hover {
    background: #ea580c;
    border-color: #ea580c;
}

.portal-link-hidden {
    display: none !important;
}

.portal-dashboard-header {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    align-items: flex-start;
    margin-bottom: 8px;
}

.portal-kicker {
    margin: 0 0 8px;
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: #fdba74;
}

.portal-empty-state {
    padding: 24px 0 4px;
}

.portal-empty-state h2 {
    margin: 0 0 10px;
}

.portal-device-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
    font-size: 0.95rem;
}

.portal-device-table th,
.portal-device-table td {
    text-align: left;
    padding: 10px 10px;
    border-bottom: 1px solid rgba(148, 163, 184, 0.25);
}

.portal-device-table th {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: #cbd5e1;
}

.portal-device-table tbody tr:hover {
    background: rgba(148, 163, 184, 0.08);
}

.portal-device-link {
    color: #cbd5e1;
    text-decoration: none;
    border-bottom: 1px dotted rgba(203, 213, 225, 0.45);
}

.portal-device-link:hover {
    color: #ffffff;
    border-bottom-color: rgba(255, 255, 255, 0.65);
}

.portal-device-link.is-active {
    color: #fdba74;
    border-bottom-color: rgba(253, 186, 116, 0.8);
}

.portal-metrics-card {
    border: 1px solid var(--border);
    border-radius: 10px;
    background: rgba(11, 17, 32, 0.68);
    padding: 16px;
    position: relative;
}

.portal-card-header {
    margin-bottom: 8px;
    padding-right: 36px;
}

.portal-card-header .portal-device-title {
    margin: 0;
    width: 100%;
}

.portal-cog-btn {
    position: absolute;
    top: 12px;
    right: 12px;
    background: none;
    border: none;
    color: #94a3b8;
    font-size: 1.1rem;
    line-height: 1;
    padding: 4px 6px;
    cursor: pointer;
    border-radius: 4px;
    transition: color 0.15s;
    z-index: 1;
}

.portal-cog-btn:hover {
    color: #f8fafc;
}

.portal-device-name-input {
    font-family: inherit;
    font-size: inherit;
    font-weight: 700;
    background: rgba(255,255,255,0.07);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-light);
    padding: 1px 6px;
    max-width: 100%;
}

.portal-metrics-card h2 {
    margin: 0 0 8px;
    font-size: 1.15rem;
}

.portal-device-title {
    display: block;
    max-width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.portal-device-friendly,
.portal-device-id {
    white-space: nowrap;
}

.portal-metrics-card p {
    margin: 0 0 10px;
    font-size: 0.9rem;
}

.portal-chart-wrap {
    position: relative;
    width: 100%;
    height: 380px;
}

.portal-still-strip {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.75rem;
    margin-bottom: 0.5rem;
    width: 100%;
    min-width: 0;
}

.portal-still-item {
    display: block;
    width: 100%;
    min-width: 0;
}

.portal-still-item.is-hidden {
    display: none;
}

.portal-still-display {
    position: relative;
    width: 100%;
    border-radius: 10px;
    border: 1px solid rgba(148, 163, 184, 0.35);
    background: #020617;
    overflow: hidden;
}


.portal-still-label {
    position: absolute;
    top: 8px;
    right: 8px;
    left: auto;
    z-index: 2;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: #e2e8f0;
    background: rgba(15, 23, 42, 0.76);
    padding: 0.2rem 0.45rem;
    border-radius: 999px;
}

.portal-still-image {
    display: block;
    width: 100%;
    max-width: 100%;
    height: auto;
    aspect-ratio: 16 / 9;
    object-fit: cover;
    background: #334155;
}

.portal-metrics-card {
    min-width: 0;
}

[id^="deviceMetricsChart"] {
    width: 100%;
    height: 100% !important;
    min-height: 0;
}

.portal-device-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

.portal-action-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-top: 0;
    padding: 0.4rem 0.9rem;
    background: #1e40af;
    color: #e2e8f0;
    border: none;
    border-radius: 6px;
    font-size: 0.8rem;
    font-family: inherit;
    line-height: 1.2;
    cursor: pointer;
    transition: background 0.15s;
}

.portal-action-btn:hover:not(:disabled) {
    background: #2563eb;
}

.portal-action-btn-spray,
.portal-action-btn[data-action="spray_water"] {
    background: #166534;
}

.portal-action-btn-spray:hover:not(:disabled),
.portal-action-btn[data-action="spray_water"]:hover:not(:disabled) {
    background: #15803d;
}

.portal-action-btn:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.portal-action-btn-spray:link,
.portal-action-btn-spray:visited,
.portal-action-btn-spray:hover,
.portal-action-btn-spray:focus {
    color: #e2e8f0;
    text-decoration: none;
}

.portal-device-actions a.portal-action-btn,
.portal-device-actions a.portal-action-btn:link,
.portal-device-actions a.portal-action-btn:visited,
.portal-device-actions a.portal-action-btn:hover,
.portal-device-actions a.portal-action-btn:focus,
.portal-device-actions a.portal-action-btn:active {
    text-decoration: none;
}

.portal-action-status {
    font-size: 0.8rem;
    color: #94a3b8;
}

.portal-action-ok {
    color: #34d399;
}

.portal-action-err {
    color: #f87171;
}

.portal-admin-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 16px;
}

.portal-admin-panel h2 {
    margin: 0 0 8px;
}

.portal-admin-panel p {
    margin: 0 0 14px;
}

.portal-admin-panel-impersonate {
    background: rgba(124, 45, 18, 0.92);
    border-color: rgba(194, 65, 12, 0.7);
}

.portal-admin-panel-impersonate p,
.portal-admin-panel-impersonate .portal-admin-list {
    color: #ffedd5;
}

.portal-admin-form {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.portal-admin-form button {
    align-self: flex-start;
}

.portal-admin-link-fields {
    display: grid;
    grid-template-columns: repeat(2, minmax(140px, 220px));
    gap: 12px;
    align-items: end;
}

.portal-admin-link-fields > div {
    width: 100%;
}

.portal-admin-link-fields label {
    margin-top: 0;
}

.portal-admin-list {
    margin: 16px 0 0;
    padding-left: 18px;
    color: #cbd5e1;
}

.portal-admin-list li + li {
    margin-top: 6px;
}

.portal-admin-list-separator {
    color: #fdba74;
}

@media (max-width: 860px) {
    .portal-admin-grid {
        grid-template-columns: 1fr;
    }

    .portal-admin-link-fields {
        grid-template-columns: 1fr;
    }

    .portal-still-strip {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 520px) {
    body {
        padding: 14px;
    }

    body.portal-dashboard-body {
        padding: 14px;
    }

    .portal-card {
        padding: 20px;
    }

    .portal-dashboard-header {
        flex-direction: column;
    }

    .portal-signed-in {
        margin-left: 0;
        font-size: 0.8rem;
    }

    .portal-panel {
        padding: 14px;
    }

    .portal-metrics-card h2 {
        font-size: 1rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .portal-chart-wrap {
        height: 225px;
    }

}

    .portal-rename-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.25rem;
    max-width: calc(50% - 0.375rem);
}

@media (max-width: 860px) {
    .portal-rename-row {
        max-width: 100%;
    }
}

.portal-rename-input {
    font-family: 'Inter', sans-serif;
    font-size: 0.8rem;
    flex: 1;
    min-width: 0;
}

/* ── Config Panel ─────────────────────────────────────────────────────────── */

    .portal-cog-btn.is-active {
        color: #f97316;
        transform: rotate(45deg);
        transition: color 0.15s, transform 0.2s;
    }

    .portal-cog-btn {
        transition: color 0.15s, transform 0.2s;
    }

    .portal-config-panel {
        display: none;
        flex-direction: column;
        gap: 1rem;
        margin-top: 0.25rem;
    }

    .portal-config-panel.is-open {
        display: flex;
    }

    .portal-settings-actions-row {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        gap: 0.5rem;
    }

    .portal-settings-action-btn {
        background: rgba(148, 163, 184, 0.16);
        border: 1px solid rgba(148, 163, 184, 0.36);
        color: #cbd5e1;
        font-size: 0.72rem;
        letter-spacing: 0.04em;
        text-transform: uppercase;
        padding: 0.28rem 0.62rem;
    }

    .portal-settings-action-btn:hover:not(:disabled) {
        background: rgba(148, 163, 184, 0.26);
        color: #f8fafc;
    }

    .portal-cam-save-btn {
        background: rgba(234, 88, 12, 0.18);
        border: 1px solid rgba(234, 88, 12, 0.55);
        color: #fb923c;
    }

    .portal-cam-save-btn:hover:not(:disabled) {
        background: rgba(234, 88, 12, 0.32);
        color: #fed7aa;
    }

    .portal-camera-settings-grid {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.75rem;
    }

    .portal-camera-settings-grid.is-single-camera {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

@media (max-width: 860px) {
    .portal-camera-settings-grid,
    .portal-camera-settings-grid.is-single-camera {
        grid-template-columns: 1fr;
    }
}

    .portal-cam-settings-card {
        background: rgba(255, 255, 255, 0.04);
        border: 1px solid rgba(148, 163, 184, 0.18);
        border-radius: 8px;
        padding: 12px 14px;
        display: flex;
        flex-direction: column;
        gap: 10px;
    }

    .portal-cam-settings-card.is-hidden {
        display: none;
    }

    .portal-cam-settings-title {
        margin: 0 0 2px;
        font-size: 0.75rem;
        font-weight: 700;
        letter-spacing: 0.07em;
        text-transform: uppercase;
        color: #fdba74;
    }

    .portal-settings-row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.6rem;
        min-height: 26px;
    }

    .portal-settings-label {
        font-size: 0.8rem;
        color: #94a3b8;
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* Toggle switch */
    .portal-toggle-switch {
        position: relative;
        display: inline-flex;
        align-items: center;
        cursor: pointer;
        margin-top: 0;
    }

    .portal-toggle-switch input {
        position: absolute;
        width: 1px;
        height: 1px;
        opacity: 0;
        pointer-events: none;
        margin: 0;
    }

    .portal-toggle-track {
        position: relative;
        display: inline-block;
        width: 36px;
        height: 20px;
        background: #334155;
        border-radius: 999px;
        transition: background 0.2s;
        flex-shrink: 0;
    }

    .portal-toggle-switch input:checked ~ .portal-toggle-track {
        background: #f97316;
    }

    .portal-toggle-thumb {
        position: absolute;
        top: 2px;
        left: 2px;
        width: 16px;
        height: 16px;
        background: #f8fafc;
        border-radius: 50%;
        transition: transform 0.2s;
        pointer-events: none;
    }

    .portal-toggle-switch input:checked ~ .portal-toggle-track .portal-toggle-thumb {
        transform: translateX(16px);
    }

    /* Threshold slider */
    .portal-settings-slider-row {
        display: flex;
        align-items: center;
        gap: 0.45rem;
        flex: 1;
        min-width: 0;
        justify-content: flex-end;
    }

    .portal-settings-slider {
        flex: 1;
        max-width: 100px;
        accent-color: #f97316;
        cursor: pointer;
        margin-top: 0;
        min-width: 0;
    }

    .portal-settings-slider-val {
        font-size: 0.76rem;
        color: #e2e8f0;
        min-width: 2.6ch;
        text-align: right;
        font-variant-numeric: tabular-nums;
    }

    /* Settings input */
    .portal-settings-model-input {
        flex: 1;
        min-width: 0;
        padding: 4px 8px;
        font-size: 0.75rem;
        background: rgba(11, 17, 32, 0.7);
        border: 1px solid rgba(148, 163, 184, 0.22);
        border-radius: 4px;
        color: #cbd5e1;
        font-family: 'Courier New', Courier, monospace;
        width: 100%;
        margin-top: 0;
    }

    .portal-settings-model-input:focus {
        outline: 2px solid rgba(249, 115, 22, 0.5);
        outline-offset: 1px;
        border-color: rgba(249, 115, 22, 0.4);
    }

    .portal-device-settings-card {
        border-color: rgba(251, 146, 60, 0.35);
    }

    .portal-device-settings-card .portal-settings-row {
        align-items: center;
    }

    .portal-device-settings-card .portal-settings-label {
        white-space: normal;
    }

    @media (max-width: 520px) {
        .portal-camera-settings-grid {
            grid-template-columns: 1fr;
        }
    }
