:root {
    --bg: #eef1f6;
    --surface: #ffffff;
    --surface-2: #f7f9fc;
    --border: #e2e8f0;
    --text: #1e293b;
    --text-muted: #64748b;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --radius: 14px;
    --radius-sm: 8px;
    --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 4px 16px rgba(15, 23, 42, 0.04);

    --status-printing: #3b82f6;
    --status-paused: #f59e0b;
    --status-complete: #8b5cf6;
    --status-error: #ef4444;
    --status-standby: #22c55e;
    --status-offline: #94a3b8;

    --filament-ok: #16a34a;
    --filament-runout: #dc2626;
    --filament-muted: #94a3b8;
}

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

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.45;
}

.hidden { display: none !important; }

/* Login */
.login-screen { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-box {
    background: var(--surface);
    padding: 2rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    width: 340px;
}
.login-box h1 { margin-bottom: 1rem; font-size: 1.5rem; }
.login-box input {
    width: 100%;
    padding: .7rem .85rem;
    margin-bottom: .8rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
}
.login-box button {
    width: 100%;
    padding: .75rem;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-weight: 600;
}
.login-box button:hover { background: var(--accent-hover); }
.error { color: var(--status-error); font-size: .85rem; margin-top: .5rem; }

/* Layout */
.app { max-width: 1440px; margin: 0 auto; padding: 1.25rem; }
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--surface);
    padding: 1rem 1.5rem;
    border-radius: var(--radius);
    margin-bottom: 1.25rem;
    box-shadow: var(--shadow);
}
.header h1 { font-size: 1.35rem; font-weight: 700; }
.nav-btn {
    margin-left: .5rem;
    padding: .5rem 1rem;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: .9rem;
    transition: all .15s;
}
.nav-btn:hover { border-color: var(--accent); color: var(--accent); }
.nav-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Stats */
.stats-bar { display: flex; gap: .75rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
.stat-card {
    background: var(--surface);
    padding: .85rem 1.25rem;
    border-radius: var(--radius-sm);
    min-width: 110px;
    box-shadow: var(--shadow);
    border-left: 3px solid var(--border);
}
.stat-card .value { font-size: 1.75rem; font-weight: 700; line-height: 1.1; }
.stat-card .label { color: var(--text-muted); font-size: .8rem; margin-top: .15rem; }
.stat-printing { border-left-color: var(--status-printing); }
.stat-paused { border-left-color: var(--status-paused); }
.stat-complete { border-left-color: var(--status-complete); }
.stat-error { border-left-color: var(--status-error); }
.stat-standby { border-left-color: var(--status-standby); }
.stat-offline { border-left-color: var(--status-offline); }

/* Farm section */
.farm-section {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.25rem;
    margin-bottom: 1.25rem;
    box-shadow: var(--shadow);
}
.farm-header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}
.farm-section h2 {
    display: flex;
    align-items: center;
    gap: .5rem;
    font-size: 1.1rem;
}
.farm-meta { color: var(--text-muted); font-size: .82rem; }
.farm-header-meta {
    display: flex;
    align-items: center;
    gap: .65rem;
    flex-wrap: wrap;
    justify-content: flex-end;
}
.alice-badge {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    padding: .25rem .6rem;
    border-radius: 999px;
    background: linear-gradient(135deg, #ede9fe 0%, #dbeafe 100%);
    border: 1px solid #c4b5fd;
    color: #5b21b6;
    font-size: .78rem;
    font-weight: 600;
}
.alice-icon { font-size: .9rem; line-height: 1; }
.farm-forecast {
    display: inline-flex;
    align-items: baseline;
    gap: .3rem;
    padding: .25rem .65rem;
    border-radius: 999px;
    background: #ecfdf5;
    border: 1px solid #86efac;
    color: #166534;
    font-size: .78rem;
    font-weight: 600;
}
.farm-forecast b { font-weight: 800; }
.farm-forecast-sub {
    font-weight: 500;
    color: #15803d;
    opacity: .85;
}
.farm-forecast-muted {
    background: #f8fafc;
    border-color: var(--border);
    color: var(--text-muted);
    font-weight: 500;
}
.farm-forecast-next {
    background: #eff6ff;
    border-color: #93c5fd;
    color: #1e40af;
}
.farm-forecast-next .farm-forecast-sub {
    color: #1d4ed8;
}
.farm-forecast-next.farm-forecast-muted {
    background: #f8fafc;
    border-color: var(--border);
    color: var(--text-muted);
}
.farm-section.farm-offline {
    opacity: .92;
    border: 1px dashed var(--border);
}
.farm-offline-msg { font-style: italic; }
.printer-card-unavailable {
    opacity: .65;
    background: #f1f5f9;
    border-style: dashed;
}
.printer-card-unavailable .card-title h3 { color: var(--text-muted); }
.unavailable-hint {
    color: var(--text-muted);
    font-size: .82rem;
    margin-top: -.25rem;
}
.empty-state { color: var(--text-muted); font-size: .9rem; padding: .5rem 0; }

.status-dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.status-dot.online { background: var(--status-standby); box-shadow: 0 0 0 3px rgba(34, 197, 94, .2); }
.status-dot.offline { background: var(--status-offline); }

/* Printer grid */
.printer-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1rem;
}

/* Printer card */
.printer-card {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.1rem;
    display: flex;
    flex-direction: column;
    gap: .85rem;
    transition: box-shadow .15s, border-color .15s;
}
.printer-card:hover {
    box-shadow: var(--shadow);
    border-color: #cbd5e1;
}
.printer-card[data-state="printing"] { border-top: 3px solid var(--status-printing); }
.printer-card[data-state="paused"] { border-top: 3px solid var(--status-paused); }
.printer-card[data-state="error"] { border-top: 3px solid var(--status-error); }
.printer-card[data-state="complete"] { border-top: 3px solid var(--status-complete); }
.printer-card[data-state="standby"] { border-top: 3px solid var(--status-standby); }
.printer-card[data-state="offline"] { border-top: 3px solid var(--status-offline); opacity: .85; }

.card-top { display: flex; flex-direction: column; gap: .35rem; }
.card-title {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: .6rem;
}
.card-title h3 {
    font-size: .95rem;
    font-weight: 600;
    line-height: 1.3;
    flex: 1;
    min-width: 0;
}
.card-message {
    font-size: .78rem;
    color: var(--status-error);
    background: #fef2f2;
    padding: .35rem .55rem;
    border-radius: 6px;
}

/* Status badge */
.status-badge {
    font-size: .7rem;
    font-weight: 600;
    padding: .2rem .55rem;
    border-radius: 999px;
    white-space: nowrap;
    text-transform: uppercase;
    letter-spacing: .03em;
}
.status-badge.status-printing { background: #dbeafe; color: #1d4ed8; }
.status-badge.status-paused { background: #ffedd5; color: #c2410c; }
.status-badge.status-complete { background: #ede9fe; color: #6d28d9; }
.status-badge.status-error { background: #fee2e2; color: #b91c1c; }
.status-badge.status-standby { background: #dcfce7; color: #15803d; }
.status-badge.status-offline { background: #f1f5f9; color: #64748b; }

/* Progress */
.progress-section { display: flex; flex-direction: column; gap: .45rem; }
.progress-filename, .idle-filename {
    font-size: .78rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.progress-row {
    display: flex;
    align-items: center;
    gap: .65rem;
}
.progress-bar {
    flex: 1;
    height: 8px;
    background: #e2e8f0;
    border-radius: 999px;
    overflow: hidden;
}
.progress-fill {
    height: 100%;
    border-radius: 999px;
    transition: width .4s ease;
    background: linear-gradient(90deg, #60a5fa, #3b82f6);
}
.progress-fill.status-printing {
    background: linear-gradient(90deg, #60a5fa, #3b82f6);
}
.progress-fill.status-paused {
    background: linear-gradient(90deg, #fdba74, #f59e0b);
}
.progress-fill.status-complete {
    background: linear-gradient(90deg, #a78bfa, #8b5cf6);
}
.printer-card[data-state="printing"] .progress-fill {
    animation: progress-pulse 2s ease-in-out infinite;
}
.printer-card[data-state="paused"] .progress-fill {
    opacity: .75;
}
@keyframes progress-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: .85; }
}
.progress-value {
    font-size: .82rem;
    font-weight: 700;
    color: var(--text);
    min-width: 2.5rem;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.time-row {
    display: flex;
    flex-wrap: wrap;
    gap: .4rem;
}
.time-chip {
    font-size: .72rem;
    color: var(--text-muted);
    background: var(--surface);
    border: 1px solid var(--border);
    padding: .15rem .5rem;
    border-radius: 6px;
    font-variant-numeric: tabular-nums;
}
.time-chip.time-remaining {
    color: #1d4ed8;
    border-color: #bfdbfe;
    background: #eff6ff;
    font-weight: 500;
}

/* Metrics */
.metrics-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: .5rem;
}
.metric {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: .45rem .6rem;
}
.metric-label {
    display: block;
    font-size: .68rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: .04em;
    margin-bottom: .1rem;
}
.metric-value {
    font-size: .88rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}
.metric-target {
    font-weight: 400;
    color: var(--text-muted);
    font-size: .82rem;
}

/* Filament */
.filament-value {
    display: flex;
    align-items: center;
    gap: .35rem;
}
.filament-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    flex-shrink: 0;
}
.metric.filament-ok .filament-dot { background: var(--filament-ok); box-shadow: 0 0 0 2px rgba(22, 163, 74, .2); }
.metric.filament-ok .metric-value { color: var(--filament-ok); }
.metric.filament-runout { border-color: #fecaca; background: #fef2f2; }
.metric.filament-runout .filament-dot { background: var(--filament-runout); box-shadow: 0 0 0 2px rgba(220, 38, 38, .2); }
.metric.filament-runout .metric-value { color: var(--filament-runout); }
.metric.filament-muted .filament-dot { background: var(--filament-muted); }
.metric.filament-muted .metric-value { color: var(--text-muted); font-weight: 500; }

/* Actions */
.printer-actions {
    display: flex;
    gap: .4rem;
    flex-wrap: wrap;
    margin-top: auto;
    padding-top: .15rem;
}
.printer-actions button {
    flex: 1;
    min-width: 0;
    padding: .4rem .5rem;
    font-size: .78rem;
    font-weight: 500;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    cursor: pointer;
    transition: all .12s;
}
.printer-actions button:hover { border-color: var(--accent); color: var(--accent); }
.printer-actions button.danger { color: var(--status-error); border-color: #fecaca; }
.printer-actions button.danger:hover { background: #fef2f2; border-color: var(--status-error); }

/* Farms table */
.toolbar { margin-bottom: 1rem; }
.toolbar button, .modal-actions button {
    padding: .6rem 1.2rem;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-weight: 500;
}
.toolbar button:hover, .modal-actions button:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--surface-2) !important; color: var(--text) !important; border: 1px solid var(--border) !important; }

.farms-table {
    width: 100%;
    background: var(--surface);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
}
.farms-table th, .farms-table td { padding: .8rem 1rem; text-align: left; border-bottom: 1px solid var(--border); }
.farms-table th { background: var(--surface-2); font-size: .82rem; color: var(--text-muted); font-weight: 600; }
.farms-table tr:hover { background: var(--surface-2); }
.farms-table button {
    padding: .35rem .65rem;
    font-size: .8rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    cursor: pointer;
    margin-right: .3rem;
}

/* Modal */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    backdrop-filter: blur(2px);
}
.modal-content {
    background: var(--surface);
    padding: 1.5rem;
    border-radius: var(--radius);
    width: 90%;
    max-width: 600px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(15, 23, 42, .2);
}
.modal-content label { display: block; margin-bottom: 1rem; }
.modal-content input {
    width: 100%;
    padding: .5rem;
    margin-top: .3rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}
.printer-row { display: grid; grid-template-columns: 1fr 1fr auto; gap: .5rem; margin-bottom: .5rem; align-items: end; }
.printer-row input { padding: .45rem; border: 1px solid var(--border); border-radius: var(--radius-sm); }
.modal-actions { margin-top: 1rem; display: flex; gap: .5rem; flex-wrap: wrap; }
.api-key {
    margin-top: 1rem;
    font-size: .8rem;
    word-break: break-all;
    background: var(--surface-2);
    padding: .5rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
}

/* Job statistics */
.stats-toolbar {
    display: flex;
    align-items: end;
    gap: 1rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}
.stats-toolbar label {
    display: flex;
    flex-direction: column;
    gap: .3rem;
    font-size: .85rem;
    color: var(--text-muted);
}
.stats-toolbar select {
    padding: .45rem .6rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    font-size: .9rem;
}
.section-title {
    margin: 1.25rem 0 .75rem;
    font-size: 1rem;
}
.jobs-total-card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 1.25rem 1.5rem;
    box-shadow: var(--shadow);
    margin-bottom: 1rem;
    border-left: 4px solid var(--accent);
}
.jobs-total-value {
    font-size: 2.2rem;
    font-weight: 700;
    line-height: 1.1;
}
.jobs-total-label {
    color: var(--text-muted);
    font-size: .9rem;
    margin-top: .25rem;
}
.jobs-count {
    font-weight: 700;
    font-size: 1.1rem;
}
.jobs-by-printer {
    font-size: .82rem;
}
.printer-chip {
    display: inline-block;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: .15rem .45rem;
    margin: .1rem .2rem .1rem 0;
}
.jobs-summary-table {
    margin-bottom: .5rem;
}

@media (max-width: 640px) {
    .printer-grid { grid-template-columns: 1fr; }
    .metrics-grid { grid-template-columns: 1fr 1fr; }
    .header { flex-direction: column; gap: .75rem; align-items: flex-start; }
}
