@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

:root {
    --bg: #eef1f6;
    --card-bg: #ffffff;
    --text-dark: #1c2333;
    --text-muted: #6b7280;
    --label: #404146;
    --brand: #023e81;
    --brand-light: #0d6dc7;
    --good: #15a24a;
    --good-soft: #2ecc71;
    --good-tint: #e9f9ee;
    --bad: #d6391f;
    --bad-soft: #ef5b3f;
    --bad-tint: #fdeeec;
    --blue: #1465ad;
    --blue-tint: #e9f2fb;
    --down: #c1c8d1;
    --border: #e6eaf0;
    --shadow: 0 2px 10px rgba(20, 40, 80, 0.06), 0 1px 2px rgba(20, 40, 80, 0.04);
    --shadow-hover: 0 10px 28px rgba(20, 40, 80, 0.12);
    --radius: 14px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: "Inter", "Segoe UI", Arial, sans-serif;
    background: var(--bg);
    color: var(--text-dark);
    -webkit-font-smoothing: antialiased;
}

/* ---------- Header ---------- */
.app-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 26px;
    background: var(--card-bg);
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 10;
    border-bottom: 3px solid var(--brand);
}

.app-header img {
    width: 30px;
    height: 30px;
    border-radius: 7px;
    object-fit: cover;
}

.app-header h1 {
    font-size: 18px;
    font-weight: 800;
    color: var(--brand);
    margin: 0;
    letter-spacing: .01em;
}

.app-header .header-spacer { flex: 1; }

.last-updated {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 6px;
}

.last-updated .live-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--good-soft);
    box-shadow: 0 0 0 0 rgba(46, 204, 113, .6);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(46, 204, 113, .55); }
    70% { box-shadow: 0 0 0 6px rgba(46, 204, 113, 0); }
    100% { box-shadow: 0 0 0 0 rgba(46, 204, 113, 0); }
}

/* ---------- Layout shell ---------- */
.dashboard {
    max-width: 1900px;
    margin: 0 auto;
    padding: 20px 24px 18px;
}

/* ---------- Filter bar ---------- */
.filter-bar {
    display: flex;
    align-items: stretch;
    gap: 14px;
    margin-bottom: 18px;
    flex-wrap: wrap;
}

.filter-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow);
    padding: 8px 16px;
    transition: box-shadow .15s ease, transform .15s ease;
    min-width: 150px;
}

.filter-card:hover { box-shadow: var(--shadow-hover); transform: translateY(-1px); }

.filter-card label {
    display: block;
    font-size: 11.5px;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: .03em;
    margin-bottom: 4px;
}

.filter-card select,
.filter-card input[type="datetime-local"] {
    width: 100%;
    background: #f7f8fa;
    border: 1px solid #dde2e9;
    border-radius: 8px;
    padding: 7px 10px;
    font-size: 13px;
    font-family: inherit;
    font-weight: 600;
    color: #222;
}

.filter-card select:focus,
.filter-card input[type="datetime-local"]:focus {
    outline: none;
    border-color: var(--brand-light);
    box-shadow: 0 0 0 3px rgba(13, 109, 199, .12);
}

.filter-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow);
    padding: 8px;
}

.btn-link {
    border: none;
    background: transparent;
    color: var(--brand);
    font-family: inherit;
    font-weight: 700;
    font-size: 13.5px;
    padding: 11px 18px;
    cursor: pointer;
    border-radius: 8px;
    transition: background .15s ease, transform .1s ease, box-shadow .15s ease;
}

.btn-link:hover { background: #f0f4fa; }
.btn-link:active { transform: scale(.97); }
.btn-link:disabled { color: #9aa5b8; cursor: not-allowed; }

.btn-primary {
    background: linear-gradient(135deg, var(--brand-light), var(--brand));
    color: #fff;
    box-shadow: 0 4px 12px rgba(2, 62, 129, .25);
}
.btn-primary:hover { background: linear-gradient(135deg, #1278d6, var(--brand)); box-shadow: 0 6px 16px rgba(2, 62, 129, .32); }

.filter-divider {
    width: 1px;
    height: 22px;
    background: var(--border);
}

/* ---------- Generic card ---------- */
.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px;
    transition: box-shadow .2s ease;
}

.card-title {
    text-align: center;
    font-size: 13.5px;
    font-weight: 700;
    color: var(--label);
    margin: 0 0 10px;
    letter-spacing: .01em;
}

/* ---------- Top row ---------- */
/* Fixed row height is deliberate: Chart.js canvases below are responsive
   with maintainAspectRatio:false, so if their container's height is left to
   depend on flex-grow/content instead of a hard bound, a resize feedback
   loop can make the canvas (and the whole grid row, since siblings stretch
   to match) grow taller on every reflow. */
.top-row {
    display: grid;
    grid-template-columns: 1fr 1fr 1.7fr 2.7fr;
    grid-auto-rows: 400px;
    gap: 16px;
    margin-bottom: 16px;
}

/* Client/ID/Model list */
.job-list-card { height: 100%; display: flex; flex-direction: column; gap: 6px; padding: 16px; overflow-y: auto; }
.job-list-item {
    color: var(--good);
    font-weight: 700;
    font-size: 13.5px;
    line-height: 1.4;
    cursor: pointer;
    padding: 8px 10px;
    border-radius: 8px;
    border-left: 3px solid transparent;
    transition: background .15s ease, border-color .15s ease;
}
.job-list-item:hover { background: var(--good-tint); border-left-color: var(--good); }
.job-list-empty { color: var(--text-muted); font-size: 13px; font-weight: 500; padding: 8px 10px; }

/* Stat tiles */
.stat-stack { display: flex; flex-direction: column; gap: 12px; height: 100%; }
.stat-tile {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 10px;
    text-align: center;
    gap: 4px;
    border-left: 4px solid transparent;
    transition: box-shadow .2s ease;
}
.stat-tile:hover { box-shadow: var(--shadow-hover); }
.stat-tile .stat-value-row { display: flex; align-items: center; gap: 8px; }
.stat-icon {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--good-soft);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    color: #fff;
}
.stat-tile .stat-value { font-size: 23px; font-weight: 800; letter-spacing: -.01em; }
.stat-tile .stat-label { font-size: 10.5px; font-weight: 700; text-transform: uppercase; color: var(--text-muted); letter-spacing: .04em; }
.stat-tile.runtime { border-left-color: var(--good); }
.stat-tile.runtime .stat-value { color: var(--good); font-size: 17px; }
.stat-tile.total { border-left-color: var(--blue); background: linear-gradient(180deg, var(--blue-tint), var(--card-bg) 55%); }
.stat-tile.total .stat-value { color: var(--blue); }
.stat-tile.good { border-left-color: var(--good); background: linear-gradient(180deg, var(--good-tint), var(--card-bg) 55%); }
.stat-tile.good .stat-value { color: var(--good); }
.stat-tile.notgood { border-left-color: var(--bad); background: linear-gradient(180deg, var(--bad-tint), var(--card-bg) 55%); }
.stat-tile.notgood .stat-value { color: var(--bad); }

/* Donut + defects chart cards */
.chart-card { height: 100%; display: flex; flex-direction: column; }
.chart-box { position: relative; flex: 1; min-height: 0; }

.donut-center-label {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    pointer-events: none;
}
.donut-center-label .donut-total { font-size: 22px; font-weight: 800; color: var(--text-dark); display: block; }
.donut-center-label .donut-caption { font-size: 10.5px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }

/* ---------- Bottom section ---------- */
.bottom-section {
    display: block;
}

.bottom-charts {
    display: grid;
    grid-template-columns: 1.4fr 1fr;
    gap: 16px;
    align-items: stretch;
}

.machine-status-card {
    height: 340px;
    display: flex;
    flex-direction: column;
}

.machine-status-card .timeline-track,
.machine-status-card .timeline-empty {
    margin-top: auto;
}

/* Fixed height for the same reason as .top-row above: the Chart.js canvas
   inside .hourly-card needs a container whose height doesn't depend on the
   canvas itself, or resizing can spiral. */
.hourly-card { height: 340px; display: flex; flex-direction: column; }
.hourly-card .chart-box { flex: 1; min-height: 0; }

/* Machine status timeline */
.timeline-track {
    display: flex;
    width: 100%;
    height: 46px;
    border-radius: 8px;
    overflow: hidden;
    background: #eef1f6;
    border: 1px solid var(--border);
}
.timeline-segment { height: 100%; transition: opacity .15s ease; }
.timeline-segment:hover { opacity: .85; }
.timeline-segment.good { background: #1fae4a; }
.timeline-segment.bad { background: #e3613d; }
.timeline-segment.down { background: var(--down); }
.timeline-empty { color: var(--text-muted); font-size: 13px; text-align: center; padding: 16px 0; }
.timeline-axis {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    margin-top: 6px;
}
.timeline-legend {
    display: flex;
    justify-content: center;
    gap: 18px;
    margin-top: 10px;
    font-size: 12px;
    color: var(--label);
    font-weight: 600;
}
.timeline-legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.legend-dot.good { background: #1fae4a; }
.legend-dot.bad { background: #e3613d; }
.legend-dot.down { background: var(--down); }

.dashboard-message {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px;
    text-align: center;
    color: var(--text-muted);
    font-weight: 700;
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}
.dashboard-message.is-error { color: var(--bad); }

.spinner {
    width: 16px;
    height: 16px;
    border: 2.5px solid rgba(2, 62, 129, .18);
    border-top-color: var(--brand);
    border-radius: 50%;
    animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#dashboard-content {
    animation: fade-in .35s ease;
}
@keyframes fade-in {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Thin, branded scrollbars for the scrollable panels */
.job-list-card::-webkit-scrollbar { width: 6px; }
.job-list-card::-webkit-scrollbar-thumb { background: #c7d0dc; border-radius: 999px; }
.job-list-card::-webkit-scrollbar-track { background: transparent; }

.app-footer {
    text-align: center;
    font-size: 12px;
    color: var(--text-muted);
    padding: 16px 0 22px;
}

.defect-breakdown-modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
}

.defect-breakdown-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.35);
}

.defect-breakdown-card {
    position: relative;
    width: min(760px, calc(100vw - 32px));
    max-height: calc(100vh - 48px);
    overflow: auto;
    margin: 24px auto;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 14px 34px rgba(20, 40, 80, 0.25);
    padding: 14px 16px;
}

.defect-breakdown-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    border-bottom: 1px solid var(--border);
    padding-bottom: 10px;
    margin-bottom: 12px;
}

.defect-breakdown-header h3 {
    margin: 0;
    font-size: 16px;
    color: var(--text-dark);
}

.defect-breakdown-close {
    border: 1px solid var(--border);
    background: #f7f8fa;
    color: #4b5563;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 700;
}

.defect-breakdown-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
}

.breakdown-column {
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px;
    background: #fbfcff;
}

.breakdown-column h4 {
    margin: 0 0 8px;
    font-size: 13px;
    color: var(--label);
}

.breakdown-column ol {
    margin: 0;
    padding-left: 20px;
}

.breakdown-column li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    font-size: 12.5px;
    padding: 4px 0;
    color: var(--text-dark);
}

.breakdown-column .name {
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.breakdown-column .count {
    color: #111827;
    font-weight: 700;
}

.breakdown-empty {
    font-size: 12.5px;
    color: var(--text-muted);
}

@media (max-width: 1400px) {
    .top-row { grid-template-columns: 1fr 1fr; }
    .bottom-charts { grid-template-columns: 1fr; }
}

@media (max-width: 860px) {
    .defect-breakdown-grid { grid-template-columns: 1fr; }
}
