/* =====================================================================
   Custodia Manager portal: mobile-first styles on top of Bootstrap 5.3
   ===================================================================== */
:root {
    --fc-sidebar-w: 248px;
    --fc-topbar-h: 60px;
    --fc-bottomnav-h: 64px;
    --fc-radius: .85rem;
    --fc-sidebar-bg: #111827;
    --fc-sidebar-fg: #d1d5db;
    --fc-sidebar-hover: #1f2937;
    --fc-primary: #2563eb;
    --fc-app-bg: #f3f4f6;
    --fc-card-shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
    --bs-primary: #2563eb;
    --bs-primary-rgb: 37, 99, 235;
    --bs-link-color: #2563eb;
    --bs-link-color-rgb: 37, 99, 235;
    --bs-body-font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Arabic", "Helvetica Neue", Arial, sans-serif;
}
[data-bs-theme="dark"] {
    --fc-app-bg: #0b1020;
    --fc-card-shadow: 0 1px 2px rgba(0, 0, 0, .4);
}
html, body { height: 100%; }
body { background: var(--fc-app-bg); -webkit-tap-highlight-color: transparent; overflow-x: hidden; }
.min-w-0 { min-width: 0; }
.skip-link { position: absolute; left: 1rem; top: .5rem; z-index: 2000; background: var(--bs-body-bg); padding: .5rem 1rem; border-radius: .5rem; }
.btn-primary { --bs-btn-bg: #2563eb; --bs-btn-border-color: #2563eb; --bs-btn-hover-bg: #1d4ed8; --bs-btn-hover-border-color: #1d4ed8; --bs-btn-active-bg: #1e40af; }
.btn-outline-primary { --bs-btn-color: #2563eb; --bs-btn-border-color: #2563eb; --bs-btn-hover-bg: #2563eb; --bs-btn-hover-border-color: #2563eb; --bs-btn-active-bg: #1d4ed8; }
[data-bs-theme="dark"] .btn-light { --bs-btn-bg: #1f2937; --bs-btn-border-color: #374151; --bs-btn-color: #e5e7eb; --bs-btn-hover-bg: #374151; --bs-btn-hover-color: #fff; }

/* touch targets: at least 44px */
.btn-touch { min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: .4rem; }
.btn-icon { width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center; border: 0; border-radius: 50%; color: var(--bs-body-color); font-size: 1.2rem; }
.btn-icon:hover, .btn-icon:focus-visible { background: rgba(var(--bs-emphasis-color-rgb), .07); }
.btn-icon.btn-sm { width: 38px; height: 38px; font-size: 1.05rem; }
.form-control-lg, .form-select-lg { font-size: 1rem; } /* 16px: no iOS zoom on focus */
input, select, textarea { font-size: max(16px, 1em); }

/* ------------------------------------------------------------- layout */
.app { min-height: 100vh; display: flex; }
.app-main { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 100vh; }
.app-content { flex: 1; padding: 1rem .85rem calc(var(--fc-bottomnav-h) + 1.25rem + env(safe-area-inset-bottom)); outline: none; }
.app-topbar {
    position: sticky; top: 0; z-index: 1020; height: var(--fc-topbar-h);
    display: flex; align-items: center; gap: .5rem; padding: 0 .5rem 0 .5rem;
    background: color-mix(in srgb, var(--bs-body-bg) 92%, transparent); backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--bs-border-color);
}
.topbar-title { flex: 1; }
.topbar-actions { display: flex; align-items: center; gap: .15rem; }
.app-progress { position: sticky; top: var(--fc-topbar-h); z-index: 1019; height: 3px; overflow: hidden; }
.app-progress span { display: block; height: 100%; width: 30%; background: var(--fc-primary); transform: translateX(-120%); opacity: 0; }
.app-progress.active span { opacity: 1; animation: fc-progress 1.1s ease-in-out infinite; }
@keyframes fc-progress { 0% { transform: translateX(-120%); } 100% { transform: translateX(380%); } }
@media (prefers-reduced-motion: reduce) { .app-progress.active span { animation: none; transform: none; width: 100%; } }
.app-footer { justify-content: space-between; gap: 1rem; padding: .5rem 1.5rem; font-size: .8rem; color: var(--bs-secondary-color); border-top: 1px solid var(--bs-border-color); background: var(--bs-body-bg); }

/* ------------------------------------------------------------- sidebar */
.app-sidebar { --bs-offcanvas-width: 280px; background: var(--fc-sidebar-bg) !important; color: var(--fc-sidebar-fg); }
.app-sidebar.offcanvas-lg { display: flex; flex-direction: column; }
.sidebar-brand { display: flex; align-items: center; gap: .75rem; padding: 1.1rem 1.1rem 1.25rem; }
.brand-icon { width: 38px; height: 38px; border-radius: 10px; background: var(--fc-primary); color: #fff; display: inline-flex; align-items: center; justify-content: center; font-size: 1.15rem; flex-shrink: 0; }
.brand-name { font-weight: 700; color: #fff; line-height: 1.1; }
.brand-sub { font-size: .75rem; color: #9ca3af; }
.sidebar-nav { flex: 1; overflow-y: auto; padding: 0 .65rem; display: flex; flex-direction: column; gap: 2px; }
.nav-item-link { display: flex; align-items: center; gap: .8rem; min-height: 46px; padding: .55rem .85rem; border-radius: .6rem; color: var(--fc-sidebar-fg); text-decoration: none; font-size: .95rem; }
.nav-item-link i { font-size: 1.1rem; width: 1.3rem; text-align: center; }
.nav-item-link:hover { background: var(--fc-sidebar-hover); color: #fff; }
.nav-item-link.active { background: var(--fc-primary); color: #fff; }
.nav-section { margin: 1rem .85rem .35rem; font-size: .7rem; text-transform: uppercase; letter-spacing: .06em; color: #6b7280; }
.sidebar-user { border-top: 1px solid #1f2937; padding: 1rem; }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: #374151; color: #fff; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; }
.avatar-lg { width: 52px; height: 52px; font-size: 1.4rem; background: var(--fc-primary); }
.btn-sidebar { background: var(--fc-sidebar-hover); color: #fff; border: 1px solid #374151; min-height: 38px; }
.btn-sidebar:hover { background: #374151; color: #fff; }

@media (min-width: 992px) {
    .app-sidebar { position: sticky; top: 0; height: 100vh; width: var(--fc-sidebar-w); flex-shrink: 0; }
    .app-topbar { padding: 0 1.25rem 0 1.5rem; }
    .app-content { padding: 1.5rem 1.5rem 1.5rem; }
}

/* ------------------------------------------------------------- bottom navigation (phones) */
.app-bottomnav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 1030;
    display: grid; grid-template-columns: repeat(5, 1fr);
    height: calc(var(--fc-bottomnav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    background: var(--bs-body-bg); border-top: 1px solid var(--bs-border-color); box-shadow: 0 -4px 16px rgba(0, 0, 0, .05);
}
.app-bottomnav a, .app-bottomnav button {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    color: var(--bs-secondary-color); text-decoration: none; font-size: .7rem; background: none; border: 0; padding: 0;
}
.app-bottomnav i { font-size: 1.3rem; }
.app-bottomnav .active { color: var(--fc-primary); font-weight: 600; }

/* ------------------------------------------------------------- cards / sections */
.page-card { border: 1px solid var(--bs-border-color-translucent); border-radius: var(--fc-radius); box-shadow: var(--fc-card-shadow); }
.card-section-title { font-weight: 700; margin-bottom: .75rem; display: flex; align-items: center; gap: .5rem; }
.form-narrow { max-width: 560px; }
.form-wide { max-width: 980px; }
.empty-state { text-align: center; color: var(--bs-secondary-color); padding: 2rem 1rem; }
.empty-state i { font-size: 2.2rem; display: block; margin-bottom: .35rem; opacity: .6; }

/* KPI cards */
.kpi { position: relative; display: block; height: 100%; padding: .9rem 1rem; border-radius: var(--fc-radius); background: var(--bs-body-bg);
       border: 1px solid var(--bs-border-color-translucent); box-shadow: var(--fc-card-shadow); color: var(--bs-body-color); text-decoration: none; overflow: hidden;
       transition: transform .12s ease, box-shadow .12s ease; }
.kpi:hover { transform: translateY(-2px); box-shadow: 0 6px 18px rgba(16, 24, 40, .12); color: var(--bs-body-color); }
.kpi-icon { position: absolute; right: .8rem; top: .7rem; font-size: 1.5rem; opacity: .35; }
.kpi-label { font-size: .8rem; color: var(--bs-secondary-color); font-weight: 600; padding-right: 2rem; }
.kpi-value { font-size: 1.85rem; font-weight: 800; line-height: 1.15; }
.kpi-sub { font-size: .72rem; color: var(--bs-secondary-color); }
.kpi::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--kpi, #6b7280); }
.kpi .kpi-value, .kpi .kpi-icon { color: var(--kpi, inherit); }
.kpi-blue { --kpi: #2563eb; } .kpi-green { --kpi: #16a34a; } .kpi-gray { --kpi: #6b7280; } .kpi-orange { --kpi: #d97706; }
.kpi-red { --kpi: #dc2626; } .kpi-indigo { --kpi: #4f46e5; }
@media (min-width: 768px) { .kpi-value { font-size: 2.1rem; } .kpi { padding: 1.1rem 1.2rem; } }

.quick-actions, .toolbar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.quick-actions .btn { flex: 1 1 auto; }
@media (min-width: 768px) { .quick-actions .btn { flex: 0 0 auto; } }
.toolbar-search { flex: 1 1 260px; }

/* chips + scrollable tabs */
.chips, .tabs-scroll { display: flex; gap: .4rem; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; padding-bottom: 2px; }
.chips::-webkit-scrollbar, .tabs-scroll::-webkit-scrollbar { display: none; }
.chip { flex: 0 0 auto; padding: .45rem .9rem; min-height: 38px; display: inline-flex; align-items: center; border-radius: 999px; border: 1px solid var(--bs-border-color);
        background: var(--bs-body-bg); color: var(--bs-body-color); text-decoration: none; font-size: .875rem; }
.chip.active { background: var(--fc-primary); border-color: var(--fc-primary); color: #fff; }
.tabs-scroll { border-bottom: 1px solid var(--bs-border-color); gap: .25rem; }
.tab-link { flex: 0 0 auto; display: inline-flex; align-items: center; gap: .4rem; min-height: 44px; padding: .5rem .9rem; color: var(--bs-secondary-color); text-decoration: none;
            border-bottom: 3px solid transparent; font-weight: 500; white-space: nowrap; }
.tab-link:hover { color: var(--bs-body-color); }
.tab-link.active { color: var(--fc-primary); border-bottom-color: var(--fc-primary); font-weight: 600; }
.summary-chips { display: flex; flex-wrap: wrap; gap: .35rem 1rem; font-size: .85rem; color: var(--bs-secondary-color); }
.filter-bar .form-check { min-height: 38px; display: flex; align-items: center; gap: .5rem; }

/* status badges (same palette as the WPF app) */
.badge.st { font-weight: 600; letter-spacing: .01em; color: #fff; background: #6b7280; }
.st-Pending, .st-InitiatedShutdown, .st-Operator { background: #2563eb !important; }
.st-Deleted, .st-Startup, .st-Online { background: #16a34a !important; }
.st-NotFound { background: #0d9488 !important; }
.st-Failed { background: #d97706 !important; }
.st-GaveUp, .st-Rejected, .st-UnexpectedShutdown { background: #dc2626 !important; }
.st-Cancelled, .st-CleanShutdown, .st-Offline, .st-Disabled { background: #6b7280 !important; }

.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex-shrink: 0; vertical-align: middle; margin-right: .35rem; }
.dot-on { background: #16a34a; box-shadow: 0 0 0 3px rgba(22, 163, 74, .18); }
.dot-off { background: #9ca3af; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: #9ca3af; display: inline-block; }
.status-dot.ok { background: #16a34a; } .status-dot.bad { background: #dc2626; }

/* lists on the dashboard */
.attention-list .list-group-item { background: transparent; min-height: 60px; }
.upcoming-list { max-height: 460px; overflow-y: auto; }
.upcoming-list li { padding: .6rem .25rem; border-bottom: 1px solid var(--bs-border-color-translucent); }
.upcoming-list li.is-overdue { border-left: 3px solid #dc2626; padding-left: .6rem; background: rgba(220, 38, 38, .05); }
.activity-list li { display: flex; gap: .75rem; align-items: flex-start; padding: .6rem 0; border-bottom: 1px solid var(--bs-border-color-translucent); }
.activity-list li:last-child { border-bottom: 0; }
.activity-icon { width: 34px; height: 34px; border-radius: 50%; background: rgba(var(--bs-primary-rgb), .1); color: var(--fc-primary); display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; }

/* agent details */
.machine-icon { width: 52px; height: 52px; border-radius: 14px; background: rgba(var(--bs-primary-rgb), .1); color: var(--fc-primary); display: inline-flex; align-items: center; justify-content: center; font-size: 1.5rem; flex-shrink: 0; }
.meta-line { display: flex; flex-wrap: wrap; gap: .25rem 1rem; }
.header-actions { width: 100%; }
.header-actions form, .header-actions .btn { width: 100%; }
@media (min-width: 768px) { .header-actions, .header-actions form, .header-actions .btn { width: auto; } }
.count-card { padding: .8rem 1rem; border-radius: var(--fc-radius); background: var(--bs-body-bg); border: 1px solid var(--bs-border-color-translucent); }
.count-value { font-size: 1.6rem; font-weight: 800; }
.power-card { height: 100%; padding: 1rem 1.1rem; border-radius: var(--fc-radius); background: var(--bs-body-bg); border: 1px solid var(--bs-border-color-translucent); box-shadow: var(--fc-card-shadow); }
.power-time { font-size: 1.25rem; font-weight: 700; margin-top: .35rem; }
.usage-cell .progress { height: 18px; min-width: 120px; }
.kv { margin: 0; }
.kv dt { font-size: .8rem; color: var(--bs-secondary-color); font-weight: 600; }
.kv dd { margin-bottom: .6rem; word-break: break-word; }
@media (min-width: 768px) {
    .kv-grid { display: grid; grid-template-columns: 210px 1fr; column-gap: 1rem; }
    .kv-grid dt { padding: .35rem 0; border-bottom: 1px solid var(--bs-border-color-translucent); }
    .kv-grid dd { padding: .35rem 0; margin: 0; border-bottom: 1px solid var(--bs-border-color-translucent); }
}

/* text helpers */
.path-text { font-family: var(--bs-font-monospace); font-size: .9em; word-break: break-all; }
.msg-text { display: inline-block; max-width: 260px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom; }
.msg-text.wide { max-width: 520px; }

/* =====================================================================
   Responsive tables: real tables on lg+, cards on phones / tablets
   (each <td> carries data-label; .rt-title = card heading)
   ===================================================================== */
.rtable { --bs-table-bg: transparent; margin-bottom: 0; }
.rtable th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--bs-secondary-color); white-space: nowrap; }
.rtable .w-check { width: 42px; }
.rtable .w-actions { width: 52px; }
.items-table tr.is-overdue > td { background: rgba(220, 38, 38, .06); }
.items-table tr.is-removed, tr.is-removed { opacity: .6; }
.items-table tr.is-selected > td { background: rgba(var(--bs-primary-rgb), .1); }
.items-table tr.item-row:focus-visible { outline: 2px solid var(--fc-primary); outline-offset: -2px; }

@media (min-width: 992px) {
    .rtable td { vertical-align: middle; }
    .rtable .path-text { display: inline-block; max-width: 460px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom; word-break: normal; }
    .rtable tr.row-link { cursor: pointer; }
    .items-table tr.item-row { cursor: default; }
}

@media (max-width: 991.98px) {
    .rtable thead { display: none; }
    .rtable, .rtable tbody { display: block; width: 100%; }
    .rtable tr {
        position: relative; display: flex; flex-direction: column; gap: .1rem;
        margin: .65rem; padding: .8rem .9rem .7rem; border-radius: var(--fc-radius);
        background: var(--bs-body-bg); border: 1px solid var(--bs-border-color-translucent); box-shadow: var(--fc-card-shadow);
    }
    .rtable tr[hidden] { display: none; }
    .rtable td { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: .2rem 0; border: 0; text-align: right; font-size: .9rem; }
    .rtable td::before { content: attr(data-label); flex-shrink: 0; text-align: left; font-size: .78rem; font-weight: 600; color: var(--bs-secondary-color); }
    .rtable td.rt-title { order: -1; display: block; text-align: left; font-weight: 600; font-size: .98rem; padding: 0 2.6rem .35rem 0; margin-bottom: .2rem; border-bottom: 1px solid var(--bs-border-color-translucent); }
    .rtable td.rt-title::before, .rtable td.rt-actions::before, .rtable td.rt-check::before { content: none; }
    .rtable td.rt-actions { position: absolute; top: .35rem; right: .3rem; padding: 0; }
    .rtable td.rt-check { position: absolute; top: .65rem; left: .75rem; padding: 0; }
    .rtable tr.has-check { padding-left: 2.85rem; }
    .rtable td.rt-hide-sm { display: none; }
    .rtable .msg-text { max-width: 60vw; white-space: normal; text-align: right; }
    .rtable td .badge { white-space: normal; }
    .items-table tr.is-overdue { border-left: 4px solid #dc2626; }
    .items-table tr.is-overdue > td, .items-table tr.is-selected > td { background: transparent; }
    .items-table tr.is-selected { box-shadow: 0 0 0 2px var(--fc-primary); }
    .items-table tr.item-row { cursor: pointer; }
    .rtable .form-check-input { width: 1.35em; height: 1.35em; }
    .page-card > .card-body.p-0 { background: transparent; }
}

/* ------------------------------------------------------------- selection bar (multi-select actions) */
.selection-bar {
    position: fixed; z-index: 1035; left: .75rem; right: .75rem; bottom: calc(var(--fc-bottomnav-h) + .6rem + env(safe-area-inset-bottom));
    display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .6rem .75rem .6rem 1rem;
    border-radius: 1rem; background: #111827; color: #fff;
}
.selection-bar[hidden] { display: none; }
@media (min-width: 992px) {
    .selection-bar { left: calc(var(--fc-sidebar-w) + 1.5rem); right: 1.5rem; bottom: 1.25rem; }
}
body.has-selection .app-content { padding-bottom: calc(var(--fc-bottomnav-h) + 6rem); }

/* ------------------------------------------------------------- dialogs, sheet, toasts */
.modal-content { border-radius: 1rem; border: 0; }
.modal-fullscreen-sm-down .modal-content, .modal-fullscreen-md-down .modal-content { border-radius: 0; }
@media (min-width: 576px) { .modal-fullscreen-sm-down .modal-content { border-radius: 1rem; } }
@media (min-width: 768px) { .modal-fullscreen-md-down .modal-content { border-radius: 1rem; } }
.modal-footer .btn { min-width: 110px; }
@media (max-width: 575.98px) { .modal-footer { flex-wrap: nowrap; } .modal-footer .btn { flex: 1; } }
.confirm-icon { width: 44px; height: 44px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; font-size: 1.3rem; background: rgba(var(--bs-primary-rgb), .12); color: var(--fc-primary); }
.confirm-icon.danger { background: rgba(220, 38, 38, .12); color: #dc2626; }
.confirm-message { white-space: pre-line; word-break: break-word; color: var(--bs-secondary-color); }
.quick-chips { display: flex; flex-wrap: wrap; gap: .35rem; }
.quick-chips .btn { min-height: 36px; border-radius: 999px; }
.bulk-help { background: var(--bs-tertiary-bg); border: 1px solid var(--bs-border-color); border-radius: .6rem; padding: .6rem .8rem; }
.item-sheet { --bs-offcanvas-height: auto; max-height: 88vh; border-radius: 1.1rem 1.1rem 0 0; padding-bottom: env(safe-area-inset-bottom); }
.item-sheet .offcanvas-header { position: relative; padding-top: 1.4rem; align-items: flex-start; gap: .75rem; }
.sheet-handle { position: absolute; top: .55rem; left: 50%; width: 42px; height: 5px; border-radius: 3px; background: var(--bs-border-color); transform: translateX(-50%); }
.sheet-details { display: grid; grid-template-columns: auto 1fr; gap: .3rem 1rem; font-size: .92rem; margin-bottom: 1rem; }
.sheet-details dt { color: var(--bs-secondary-color); font-weight: 600; font-size: .8rem; padding-top: .1rem; }
.sheet-details dd { margin: 0; text-align: right; word-break: break-word; }
.app-toasts { right: .75rem; left: .75rem; bottom: calc(var(--fc-bottomnav-h) + .75rem + env(safe-area-inset-bottom)); z-index: 1090; display: flex; flex-direction: column; align-items: center; }
.app-toasts .toast { width: 100%; max-width: 420px; }
@media (min-width: 992px) { .app-toasts { left: auto; right: 1.25rem; bottom: 1.25rem; align-items: flex-end; } }
.sticky-actions { position: sticky; bottom: calc(var(--fc-bottomnav-h) + env(safe-area-inset-bottom)); z-index: 5; display: flex; gap: .5rem; flex-wrap: wrap; padding: .75rem 0; background: var(--fc-app-bg); }
.sticky-actions .btn { flex: 1 1 auto; }
@media (min-width: 992px) { .sticky-actions { bottom: 0; } .sticky-actions .btn { flex: 0 0 auto; } }

/* ------------------------------------------------------------- sign-in screens */
.auth-body { background: linear-gradient(160deg, #111827 0%, #1e3a8a 100%); min-height: 100vh; }
[data-bs-theme="dark"] .auth-body { background: linear-gradient(160deg, #030712 0%, #172554 100%); }
.auth-wrap { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 1.5rem 1rem calc(1.5rem + env(safe-area-inset-bottom)); }
.auth-brand { display: flex; align-items: center; gap: .75rem; margin-bottom: 1.25rem; }
.auth-card { width: 100%; max-width: 440px; border: 0; border-radius: 1.1rem; }
