/* AppPush-inspired light console */
:root {
    --bg: #F4F5F8;
    --surface: #FFFFFF;
    --rail-bg: #FFFFFF;
    --subnav-bg: #FAFBFC;
    --ink: #1F2329;
    --ink-secondary: #4E5969;
    --ink-muted: #86909C;
    --accent: #5B5CE2;
    --accent-hover: #4A4BD0;
    --accent-soft: #EEF0FF;
    --border: #E5E6EB;
    --border-strong: #C9CDD4;
    --success: #00B42A;
    --success-soft: #E8FFEA;
    --warning: #FF7D00;
    --warning-soft: #FFF7E8;
    --danger: #F53F3F;
    --danger-soft: #FFECE8;
    --info: #3491FA;
    --info-soft: #E8F3FF;
    --display: #722ED1;
    --display-soft: #F5E8FF;
    --shadow-sm: 0 1px 2px rgba(31, 35, 41, 0.04);
    --shadow-md: 0 4px 16px rgba(31, 35, 41, 0.06);
    --rail-width: 72px;
    --subnav-width: 200px;
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 12px;
    --ease: 160ms cubic-bezier(0.25, 1, 0.5, 1);
    --z-rail: 30;
    --z-subnav: 20;
}

*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
    font-size: 14px;
    line-height: 1.5;
    color: var(--ink-secondary);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}

.app-shell {
    display: flex;
    min-height: 100vh;
}

/* ===== Rail ===== */
.rail {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: var(--rail-width);
    background: var(--rail-bg);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    align-items: center;
    z-index: var(--z-rail);
    padding: 16px 0;
}

.rail-brand {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--accent);
    margin-bottom: 20px;
}

.rail-nav {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
    width: 100%;
    padding: 0 10px;
}

.rail-item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    width: 100%;
    padding: 10px 4px;
    border: none;
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--ink-muted);
    font-size: 11px;
    cursor: pointer;
    transition: background var(--ease), color var(--ease);
}

.rail-item:hover {
    background: var(--bg);
    color: var(--ink);
}

.rail-item:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.rail-item.active {
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 500;
}

.rail-dot {
    position: absolute;
    top: 8px;
    right: 12px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--accent);
}

.rail-dot[hidden] {
    display: none;
}

.rail-footer {
    padding: 12px 0 4px;
}

.status-dot {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--warning-soft);
}

.status-dot-core {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--warning);
}

.status-dot.connected {
    background: var(--success-soft);
}

.status-dot.connected .status-dot-core {
    background: var(--success);
    animation: pulse 2s ease-out infinite;
}

.status-dot.disconnected {
    background: var(--danger-soft);
}

.status-dot.disconnected .status-dot-core {
    background: var(--danger);
}

@keyframes pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.55; transform: scale(1.15); }
}

/* ===== Subnav ===== */
.subnav {
    position: fixed;
    top: 0;
    left: var(--rail-width);
    bottom: 0;
    width: var(--subnav-width);
    background: var(--subnav-bg);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: var(--z-subnav);
}

.subnav-brand {
    padding: 20px 18px 16px;
    border-bottom: 1px solid var(--border);
}

.subnav-brand strong {
    display: block;
    font-size: 15px;
    font-weight: 600;
    color: var(--ink);
}

.subnav-brand span {
    font-size: 12px;
    color: var(--ink-muted);
}

.subnav-list {
    flex: 1;
    padding: 12px 10px;
    overflow-y: auto;
}

.subnav-list[hidden] {
    display: none;
}

.subnav-group {
    padding: 8px 10px 6px;
    font-size: 12px;
    font-weight: 500;
    color: var(--ink-muted);
}

.subnav-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 9px 12px;
    margin-bottom: 2px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    font-size: 13px;
    color: var(--ink-secondary);
    cursor: pointer;
    text-align: left;
    transition: background var(--ease), color var(--ease);
}

.subnav-item:hover {
    background: rgba(91, 92, 226, 0.06);
    color: var(--ink);
}

.subnav-item:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

.subnav-item.active {
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 500;
}

.subnav-count {
    font-size: 11px;
    font-variant-numeric: tabular-nums;
    min-width: 22px;
    padding: 1px 7px;
    border-radius: 10px;
    background: var(--border);
    color: var(--ink-muted);
    text-align: center;
}

.subnav-item.active .subnav-count {
    background: rgba(91, 92, 226, 0.18);
    color: var(--accent);
}

.subnav-footer {
    padding: 14px 18px;
    border-top: 1px solid var(--border);
}

.last-updated {
    font-size: 12px;
    color: var(--ink-muted);
    line-height: 1.45;
}

/* ===== Workspace ===== */
.workspace {
    flex: 1;
    margin-left: calc(var(--rail-width) + var(--subnav-width));
    padding: 28px 36px 48px;
    background: var(--surface);
    min-height: 100vh;
}

.panel {
    display: none;
    max-width: 1080px;
}

.panel.active {
    display: block;
}

.page-header {
    margin-bottom: 28px;
}

.page-header-row {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
}

.page-header h1 {
    font-size: 22px;
    font-weight: 600;
    color: var(--ink);
    line-height: 1.35;
    margin-bottom: 6px;
    text-wrap: balance;
}

.page-header p {
    font-size: 14px;
    color: var(--ink-muted);
    max-width: 56ch;
}

/* Buttons */
.btn-secondary {
    background: var(--surface);
    color: var(--ink-secondary);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: border-color var(--ease), color var(--ease), background var(--ease);
    white-space: nowrap;
}

.btn-secondary:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--accent-soft);
}

.btn-secondary:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.clear-btn:hover {
    border-color: var(--danger);
    color: var(--danger);
    background: var(--danger-soft);
}

/* KPI */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px;
    margin-bottom: 20px;
}

.kpi {
    padding: 18px 20px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    transition: box-shadow var(--ease), border-color var(--ease);
}

.kpi:hover {
    border-color: var(--border-strong);
    box-shadow: var(--shadow-sm);
}

.kpi-label {
    font-size: 13px;
    color: var(--ink-muted);
    margin-bottom: 8px;
}

.kpi-value {
    font-size: 28px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: var(--ink);
    line-height: 1.15;
}

.kpi-value.accent { color: var(--accent); }
.kpi-value.success { color: var(--success); }
.kpi-value.danger { color: var(--danger); }
.kpi-value.display { color: var(--display); }

.kpi-sub {
    margin-top: 6px;
    font-size: 12px;
    color: var(--ink-muted);
}

/* Cards */
.card-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 14px;
}

.card {
    padding: 20px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
}

.card-wide {
    grid-column: 1 / -1;
}

.card-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.card-head h2 {
    font-size: 15px;
    font-weight: 600;
    color: var(--ink);
}

.card-meta {
    font-size: 12px;
    color: var(--ink-muted);
}

.bars {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.bar-row {
    display: grid;
    grid-template-columns: 88px 1fr 36px;
    align-items: center;
    gap: 12px;
}

.bar-label {
    font-size: 13px;
    color: var(--ink-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.bar-track {
    height: 8px;
    background: var(--border);
    border-radius: 4px;
    overflow: hidden;
}

.bar-fill {
    height: 100%;
    border-radius: 4px;
    background: var(--accent);
    transition: width 360ms cubic-bezier(0.25, 1, 0.5, 1);
}

.bar-fill.success { background: var(--success); }
.bar-fill.warning { background: var(--warning); }
.bar-fill.danger { background: var(--danger); }
.bar-fill.info { background: var(--info); }
.bar-fill.display { background: var(--display); }
.bar-fill.muted { background: var(--ink-muted); }

.bar-count {
    font-size: 13px;
    font-weight: 500;
    font-variant-numeric: tabular-nums;
    color: var(--ink);
    text-align: right;
}

.stats-empty {
    font-size: 13px;
    color: var(--ink-muted);
    padding: 4px 0;
}

.spark-wrap {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.spark {
    display: flex;
    align-items: flex-end;
    gap: 3px;
    height: 72px;
}

.spark-bar {
    flex: 1;
    min-width: 4px;
    border-radius: 2px 2px 0 0;
    background: var(--accent-soft);
    transition: height 360ms cubic-bezier(0.25, 1, 0.5, 1), background var(--ease);
}

.spark-bar.has-data {
    background: var(--accent);
}

.spark-bar:hover {
    background: var(--accent-hover);
}

.spark-labels {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    color: var(--ink-muted);
}

/* Receipt list */
.receipt-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-height: calc(100vh - 180px);
    overflow-y: auto;
    padding-right: 4px;
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}

.receipt-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px 18px;
    box-shadow: var(--shadow-sm);
    transition: border-color var(--ease), box-shadow var(--ease);
}

.receipt-card:hover {
    border-color: var(--border-strong);
    box-shadow: var(--shadow-md);
}

.receipt-card.new {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
    animation: fadeHighlight 2.2s forwards;
}

@keyframes fadeHighlight {
    from { box-shadow: 0 0 0 3px var(--accent-soft); }
    to { box-shadow: var(--shadow-sm); }
}

.receipt-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 14px;
    flex-wrap: wrap;
    gap: 8px;
}

.receipt-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.receipt-time {
    font-size: 12px;
    color: var(--ink-muted);
    font-variant-numeric: tabular-nums;
}

.receipt-badge {
    font-size: 11px;
    font-weight: 500;
    padding: 3px 8px;
    border-radius: 4px;
}

.badge-push-success { background: var(--accent-soft); color: var(--accent); }
.badge-delivery { background: var(--success-soft); color: #009A29; }
.badge-click { background: var(--info-soft); color: #0E42D2; }
.badge-undelivered { background: var(--danger-soft); color: #CB2634; }
.badge-display { background: var(--display-soft); color: var(--display); }
.badge-online { background: #F0E8FF; color: #531DAB; }
.badge-geofence { background: var(--display-soft); color: var(--display); }
.badge-unknown { background: var(--border); color: var(--ink-muted); }
.badge-batch { background: var(--warning); color: #fff; }
.badge-sms { background: var(--success-soft); color: #009A29; }
.badge-app-push { background: var(--danger-soft); color: #CB2634; }

.field-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 10px 18px;
    margin-bottom: 10px;
}

.field-item {
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.field-label {
    font-size: 12px;
    font-weight: 500;
    color: var(--ink-muted);
}

.field-value {
    font-size: 14px;
    color: var(--ink);
    word-break: break-all;
}

.field-value.mono {
    font-family: "SF Mono", "Fira Code", "Cascadia Code", "Courier New", monospace;
    font-size: 13px;
}

.field-value.nested {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
    font-family: "SF Mono", "Fira Code", "Cascadia Code", "Courier New", monospace;
    font-size: 12px;
    white-space: pre-wrap;
    max-height: 120px;
    overflow-y: auto;
    color: var(--ink-secondary);
}

.raw-toggle {
    margin-top: 2px;
}

.raw-toggle summary {
    font-size: 12px;
    font-weight: 500;
    color: var(--accent);
    cursor: pointer;
    user-select: none;
    padding: 6px 0;
    list-style: none;
}

.raw-toggle summary::-webkit-details-marker {
    display: none;
}

.raw-toggle summary::before {
    content: "▸ ";
    display: inline-block;
    transition: transform var(--ease);
}

.raw-toggle[open] summary::before {
    transform: rotate(90deg);
}

.raw-toggle summary:hover {
    color: var(--accent-hover);
}

.raw-json {
    margin-top: 4px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
    font-family: "SF Mono", "Fira Code", "Cascadia Code", "Courier New", monospace;
    font-size: 12px;
    white-space: pre-wrap;
    word-break: break-all;
    max-height: 240px;
    overflow-y: auto;
    color: var(--ink-secondary);
}

.state-box {
    text-align: center;
    padding: 56px 24px;
    color: var(--ink-muted);
    font-size: 14px;
    border: 1px dashed var(--border);
    border-radius: var(--radius-lg);
    background: var(--bg);
}

.state-box.error {
    color: var(--danger);
    border-color: #FFCCC7;
    background: var(--danger-soft);
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    .status-dot.connected .status-dot-core {
        animation: none;
    }
}

@media (max-width: 960px) {
    .card-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 768px) {
    .rail,
    .subnav {
        position: relative;
        width: 100%;
        height: auto;
        bottom: auto;
        left: auto;
    }

    .rail {
        flex-direction: row;
        padding: 8px 12px;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }

    .rail-brand {
        margin-bottom: 0;
        margin-right: 8px;
    }

    .rail-nav {
        flex-direction: row;
        overflow-x: auto;
        padding: 0;
    }

    .rail-item {
        flex-direction: row;
        gap: 6px;
        padding: 8px 12px;
        white-space: nowrap;
    }

    .rail-footer {
        padding: 0 0 0 8px;
    }

    .subnav {
        border-right: none;
        border-bottom: 1px solid var(--border);
        max-height: none;
    }

    .subnav-list {
        display: flex;
        flex-wrap: wrap;
        gap: 4px;
        padding: 10px;
    }

    .subnav-list[hidden] {
        display: none;
    }

    .subnav-group {
        width: 100%;
    }

    .subnav-item {
        width: auto;
    }

    .workspace {
        margin-left: 0;
        padding: 20px 16px 32px;
    }

    .app-shell {
        flex-direction: column;
    }

    .receipt-list {
        max-height: none;
    }

    .field-grid {
        grid-template-columns: 1fr;
    }

    .bar-row {
        grid-template-columns: 72px 1fr 32px;
        gap: 8px;
    }
}
