:root {
    --bg: #f1f5f9;
    --card: #ffffff;
    --border: #e2e8f0;
    --text: #0f172a;
    --muted: #64748b;
    --pusher: #6366f1;
    --worker: #14b8a6;
    --shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    font-size: 14px;
}

.page {
    max-width: 1400px;
    margin: 0 auto;
    padding: 24px;
}

/* Header */
.page-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 20px;
}
.page-header h1 { margin: 6px 0 2px; font-size: 24px; }
.subtitle { margin: 0; color: var(--muted); }
.back-link { color: var(--muted); text-decoration: none; font-size: 13px; }
.back-link:hover { color: var(--text); }

/* Buttons */
.btn {
    border: 1px solid var(--border);
    background: var(--card);
    color: var(--text);
    border-radius: 10px;
    padding: 7px 14px;
    font-size: 13px;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: #f8fafc; }
.btn-primary { background: #0f172a; color: #fff; border-color: #0f172a; }
.btn-primary:hover { background: #1e293b; }
.btn.active { background: #0f172a; color: #fff; border-color: #0f172a; }

.btn-row { display: flex; flex-wrap: wrap; gap: 6px; }

/* Filters */
.filters {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 20px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 14px 18px;
    box-shadow: var(--shadow);
    margin-bottom: 16px;
}
.filter-group { display: flex; align-items: center; gap: 10px; }
.filter-label {
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.select {
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 7px 12px;
    font-size: 13px;
    background: var(--card);
    color: var(--text);
    min-width: 180px;
    max-width: 280px;
}

/* Tom Select (searchable keyword / account dropdowns) */
.ts-wrapper { min-width: 200px; max-width: 280px; }
.ts-control {
    border-radius: 10px !important;
    border-color: var(--border) !important;
    min-height: 36px;
    font-size: 13px;
}

/* Range slider (used for success rate filter) */
.range-value {
    font-size: 13px;
    font-weight: 600;
    min-width: 38px;
    text-align: center;
    padding: 3px 8px;
    border-radius: 8px;
    background: #0f172a;
    color: #fff;
}
.range-slider {
    -webkit-appearance: none;
    appearance: none;
    width: 200px;
    height: 6px;
    border-radius: 999px;
    background: #e2e8f0;
    outline: none;
    cursor: pointer;
}
.range-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #0f172a;
    border: 2px solid #fff;
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.3);
    cursor: pointer;
}
.range-slider::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #0f172a;
    border: 2px solid #fff;
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.3);
    cursor: pointer;
}

/* Status / errors */
.status {
    color: var(--muted);
    font-size: 20px;
    font-weight: 700;
    margin: 8px 2px 16px;
    min-height: 28px;
}
.status.loading { color: #0f172a; }
.error-banner {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #b91c1c;
    border-radius: 12px;
    padding: 12px 16px;
    margin-bottom: 16px;
}

/* Tables */
.tables-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 20px;
}
/* min-width:0 lets the grid/flex items shrink to the column width so the wide
   table scrolls horizontally instead of stretching the card past the charts. */
.col { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.col-title { margin: 0; font-size: 16px; }
.col-title.pusher { color: var(--pusher); }
.col-title.worker { color: var(--worker); }

.table-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 14px 16px;
    box-shadow: var(--shadow);
    min-width: 0;
}
.table-card h3 { margin: 0 0 10px; font-size: 14px; }
.hint { font-weight: 400; color: var(--muted); font-size: 12px; }

.table-scroll { overflow-x: auto; }
.monitor-section .table-scroll { max-height: 420px; overflow-y: auto; }
.tables-grid .table-scroll { max-height: 320px; overflow-y: auto; }
table { border-collapse: collapse; width: 100%; font-size: 12px; }
thead th {
    text-align: left;
    padding: 7px 10px;
    background: #f8fafc;
    color: var(--muted);
    font-weight: 600;
    white-space: nowrap;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
}
tbody td {
    padding: 6px 10px;
    border-bottom: 1px solid #f1f5f9;
    white-space: nowrap;
}
tbody tr:hover { background: #f8fafc; }
.empty { color: var(--muted); padding: 14px 4px; font-style: italic; }

/* Charts */
.charts-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 16px;
}
.chart-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 16px 18px;
    box-shadow: var(--shadow);
}
.chart-card h3 {
    margin: 0 0 6px;
    display: flex;
    flex-direction: column;
    gap: 1px;
}
.chart-card h3 .hint { line-height: 1.2; }
.chart-card h3 .chart-title { font-size: 14px; }
.legend { display: flex; gap: 16px; margin-bottom: 14px; }
.legend-item { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.swatch.pusher { background: var(--pusher); }
.swatch.worker { background: var(--worker); }

.bars { display: flex; flex-direction: column; gap: 14px; max-height: 340px; overflow-y: auto; }
.bar-group { display: flex; flex-direction: column; gap: 4px; }
.bar-label {
    font-size: 12px;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.bar-line { display: flex; align-items: center; gap: 8px; }
.bar-track { flex: 1; background: #f1f5f9; border-radius: 6px; height: 16px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 6px; min-width: 2px; transition: width 0.3s; }
.bar-fill.pusher { background: var(--pusher); }
.bar-fill.worker { background: var(--worker); }
.bar-value { font-size: 11px; color: var(--muted); width: 42px; text-align: right; }
.bar-group.clickable { cursor: pointer; border-radius: 6px; padding: 2px 4px; margin: 0 -4px; transition: background 0.15s; }
.bar-group.clickable:hover { background: rgba(99, 102, 241, 0.07); }
.bar-group.clickable.active { background: rgba(99, 102, 241, 0.13); }

/* Keyword monitoring section */
.monitor-section { margin-bottom: 20px; }
.monitor-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    margin-bottom: 10px;
}
.section-title { margin: 0; font-size: 18px; }
.monitor-controls { display: flex; align-items: center; gap: 10px; }

#monitor-table td { vertical-align: middle; }
#monitor-table .kw { font-weight: 600; }

/* Mini pusher/worker bars inside a row */
.mini-bars { display: flex; flex-direction: column; gap: 3px; min-width: 130px; }
.mini-line { display: flex; align-items: center; gap: 6px; }
.mini-track { flex: 1; background: #f1f5f9; border-radius: 5px; height: 9px; overflow: hidden; }
.mini-fill { height: 100%; border-radius: 5px; min-width: 2px; }
.mini-fill.pusher { background: var(--pusher); }
.mini-fill.worker { background: var(--worker); }
.mini-val { width: 34px; text-align: right; font-size: 11px; color: var(--muted); }

/* On-time badge */
.ontime-badge {
    display: inline-block;
    font-size: 12px;
    font-weight: 600;
    padding: 3px 10px;
    border-radius: 999px;
    white-space: nowrap;
}
.ontime-badge.ok { background: #ecfdf5; color: #047857; }
.ontime-badge.late { background: #fef2f2; color: #b91c1c; }

/* Status bars in monitoring table — timeline of interval slots (green=ok, yellow=late) */
.status-bars {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    align-content: flex-start;
    max-width: 110px;
}
.status-bar {
    flex: 0 0 10px;
    width: 10px;
    height: 10px;
    border-radius: 2px;
    cursor: help;
    transition: opacity 0.15s;
}
.status-bar:hover { opacity: 0.7; }
.status-bar.ok   { background: #22c55e; }
.status-bar.late { background: #eab308; }

/* Instant custom tooltip for status bars */
.status-tip {
    position: fixed;
    background: #1e293b;
    color: #f8fafc;
    font-size: 12px;
    padding: 5px 10px;
    border-radius: 7px;
    pointer-events: none;
    white-space: nowrap;
    z-index: 9999;
    display: none;
}

/* Inline icon button (Success Rate "Detail" toggle) */
.icon-btn {
    border: 1px solid var(--border);
    background: var(--card);
    border-radius: 8px;
    padding: 4px 8px;
    font-size: 12px;
    cursor: pointer;
}
.icon-btn:hover { background: #f8fafc; }

/* Success rate badge */
.rate-badge {
    display: inline-block;
    font-size: 12px;
    font-weight: 700;
    padding: 3px 9px;
    border-radius: 999px;
    white-space: nowrap;
}
.rate-good { background: #ecfdf5; color: #047857; }
.rate-warn { background: #fffbeb; color: #b45309; }
.rate-bad  { background: #fef2f2; color: #b91c1c; }
.rate-na   { background: #f1f5f9; color: var(--muted); }
.rate-sub  { font-size: 11px; color: var(--muted); margin-top: 2px; }
.rate-detail-btn {
    display: block;
    margin-top: 4px;
    font-size: 11px;
    padding: 2px 6px;
}

/* Skeleton loading placeholders (charts, monitoring tables, log tables) */
.skeleton {
    position: relative;
    overflow: hidden;
    background: #e7edf4;
    border-radius: 6px;
}
.skeleton::after {
    content: "";
    position: absolute;
    inset: 0;
    transform: translateX(-100%);
    background: linear-gradient(
        90deg,
        rgba(255, 255, 255, 0) 0,
        rgba(255, 255, 255, 0.65) 50%,
        rgba(255, 255, 255, 0) 100%
    );
    animation: skeleton-shimmer 1.3s ease-in-out infinite;
}
@keyframes skeleton-shimmer {
    100% { transform: translateX(100%); }
}
/* Chart skeleton: label line + two bar tracks per group */
.skel-bar-label { width: 38%; height: 12px; margin-bottom: 6px; }
.skel-bar-track { width: 100%; height: 16px; }
/* Table skeleton cell */
.skel-cell { width: 100%; height: 12px; }
/* Respect reduced-motion preferences */
@media (prefers-reduced-motion: reduce) {
    .skeleton::after { animation: none; }
}

/* DB-unreachable warning row (obj_management config not connected) */
.db-warning-row td {
    background: #fffbeb;
    border-bottom: 1px solid #fde68a;
    color: #92400e;
    font-size: 12.5px;
    line-height: 1.5;
    padding: 10px 14px;
    white-space: normal;
}
.db-warning-row strong { color: #b45309; }

/* Missed slots detail row */
.missed-detail-row td {
    background: #fefce8;
    border-bottom: 1px solid #fde68a;
    padding: 10px 14px;
}
.missed-title {
    font-size: 12px;
    font-weight: 700;
    color: #b45309;
    margin-bottom: 6px;
}
.missed-list {
    margin: 0;
    padding-left: 18px;
    font-size: 12px;
    color: #92400e;
    line-height: 1.8;
}

/* Timezone note above log tables */
.tz-note { font-size: 12px; color: var(--muted); margin: 0 2px 8px; }

/* Responsive */
@media (max-width: 900px) {
    .tables-grid, .charts-grid { grid-template-columns: 1fr; }
}
