/* Factory Edge Box — Production Dashboard */

:root {
    --bg-body: #0b0f14;
    --bg-nav: #111820;
    --bg-card: #151d28;
    --bg-card-hover: #1a2435;
    --bg-surface: #1c2636;
    --bg-input: #111820;
    --text-primary: #f0f4f8;
    --text-secondary: #7a8ba0;
    --text-muted: #4a5b6e;
    --accent: #3b82f6;
    --accent-glow: rgba(59, 130, 246, 0.25);
    --green: #22c55e;
    --green-glow: rgba(34, 197, 94, 0.3);
    --green-dim: #166534;
    --yellow: #eab308;
    --yellow-glow: rgba(234, 179, 8, 0.3);
    --yellow-dim: #854d0e;
    --red: #ef4444;
    --red-glow: rgba(239, 68, 68, 0.3);
    --red-dim: #991b1b;
    --orange: #f97316;
    --orange-glow: rgba(249, 115, 22, 0.3);
    --border: rgba(255,255,255,0.06);
    --border-light: rgba(255,255,255,0.1);
    --radius: 16px;
    --radius-sm: 10px;
    --shadow: 0 4px 24px rgba(0,0,0,0.3);
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

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

body {
    font-family: var(--font);
    background: var(--bg-body);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
    -webkit-user-select: none;
    user-select: none;
    -webkit-font-smoothing: antialiased;
}

/* ============ TOP NAV ============ */
.topnav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 24px;
    height: 60px;
    background: var(--bg-nav);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 50;
    backdrop-filter: blur(12px);
}

.topnav-left, .topnav-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
}

.logo-icon {
    width: 28px;
    height: 28px;
    color: var(--accent);
}

.logo-text {
    font-size: 17px;
    font-weight: 700;
    letter-spacing: -0.3px;
}

.conn-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    position: relative;
}

.conn-online {
    background: var(--green);
    box-shadow: 0 0 8px var(--green-glow);
}

.conn-online::after {
    content: '';
    position: absolute;
    inset: -3px;
    border-radius: 50%;
    background: var(--green);
    opacity: 0.4;
    animation: pulse-ring 2s ease-out infinite;
}

@keyframes pulse-ring {
    0% { transform: scale(1); opacity: 0.4; }
    100% { transform: scale(2.2); opacity: 0; }
}

.conn-offline {
    background: var(--red);
}

.shift-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
    padding: 5px 14px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
}

.shift-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--green);
}

.clock {
    font-size: 15px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: var(--text-secondary);
    letter-spacing: 0.5px;
}

.lang-btn {
    background: var(--bg-surface);
    color: var(--text-secondary);
    border: 1px solid var(--border-light);
    padding: 5px 12px;
    border-radius: 8px;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
    letter-spacing: 0.5px;
    transition: all 0.15s;
    min-height: 32px;
}

.lang-btn:hover { background: var(--bg-card-hover); color: var(--text-primary); }

/* ============ VIEW TOGGLE ============ */
.view-toggle {
    display: flex;
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
    border-radius: 8px;
    overflow: hidden;
}

.view-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
}

.view-btn svg {
    width: 14px;
    height: 14px;
}

.view-btn:hover {
    color: var(--text-secondary);
}

.view-btn.active {
    background: var(--accent);
    color: white;
}

.view-btn.active svg {
    stroke: white;
}

/* Report Button */
.report-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
    border-radius: 8px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s;
}

.report-btn svg {
    width: 18px;
    height: 18px;
}

.report-btn:hover {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

/* Install PWA Button */
.install-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: var(--green);
    border: none;
    border-radius: 8px;
    color: white;
    cursor: pointer;
    transition: all 0.15s;
    animation: pulse-install 2s ease-in-out infinite;
}

.install-btn svg {
    width: 18px;
    height: 18px;
}

.install-btn:hover {
    background: #16a34a;
    transform: scale(1.05);
}

@keyframes pulse-install {
    0%, 100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.4); }
    50% { box-shadow: 0 0 0 8px rgba(34, 197, 94, 0); }
}

/* ============ KPI STRIP ============ */
.kpi-strip {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1px;
    background: var(--border);
    border-bottom: 1px solid var(--border);
}

.kpi-card {
    background: var(--bg-nav);
    padding: 18px 24px;
    position: relative;
}

.kpi-value {
    display: flex;
    align-items: baseline;
    gap: 2px;
    margin-bottom: 4px;
}

.kpi-num {
    font-size: 32px;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    letter-spacing: -1px;
    line-height: 1;
}

.kpi-denom {
    font-size: 18px;
    font-weight: 500;
    color: var(--text-muted);
}

.kpi-unit {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-muted);
}

.kpi-label {
    font-size: 12px;
    font-weight: 500;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.8px;
}

.kpi-bar {
    margin-top: 10px;
    height: 4px;
    background: var(--bg-surface);
    border-radius: 2px;
    overflow: hidden;
}

.kpi-bar-fill {
    height: 100%;
    border-radius: 2px;
    background: var(--green);
    transition: width 0.8s cubic-bezier(0.22, 1, 0.36, 1);
}

.kpi-bar-blue { background: var(--accent); }

/* Financial View */
.kpi-currency {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-muted);
    margin-right: 2px;
}

.financial-view.hidden,
.technical-view.hidden {
    display: none !important;
}

body.financial-mode .technical-view {
    display: none !important;
}

body.financial-mode .financial-view {
    display: flex !important;
}

body.financial-mode .financial-view.hidden {
    display: flex !important;
}

/* KPI Trend Indicator */
.kpi-trend {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    font-weight: 600;
    margin-top: 6px;
}

.kpi-trend.trend-up {
    color: var(--red);
}

.kpi-trend.trend-down {
    color: var(--green);
}

.kpi-trend.trend-neutral {
    color: var(--text-muted);
}

.kpi-trend svg {
    width: 12px;
    height: 12px;
}

/* Loss highlight in financial mode */
body.financial-mode .kpi-card[data-kpi="downtime"] .kpi-num {
    color: var(--red);
}

body.financial-mode .kpi-card[data-kpi="incidents"] .kpi-num {
    color: var(--orange);
}

/* ============ MACHINE GRID ============ */
.machines-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 20px;
    padding: 28px;
}

.loading-container {
    grid-column: 1 / -1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 20px;
    gap: 16px;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-light);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.loading-text {
    color: var(--text-secondary);
    font-size: 15px;
}

/* Machine Card */
.machine-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0;
    cursor: pointer;
    transition: all 0.2s ease;
    overflow: hidden;
    position: relative;
}

.machine-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    border-radius: var(--radius) var(--radius) 0 0;
}

.machine-card.card-RUNNING::before { background: var(--green); }
.machine-card.card-HEAVY_LOAD::before { background: var(--orange); }
.machine-card.card-IDLE::before { background: var(--yellow); }
.machine-card.card-OFF::before { background: var(--red); }

.machine-card:hover {
    border-color: var(--border-light);
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.machine-card:active { transform: translateY(0); }

.card-body { padding: 22px 24px 20px; }

.card-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 18px;
}

.card-machine-name {
    font-size: 17px;
    font-weight: 700;
    letter-spacing: -0.2px;
    line-height: 1.3;
}

.card-location {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 2px;
}

.card-state-badge {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 0.3px;
    white-space: nowrap;
}

.badge-RUNNING { background: rgba(34,197,94,0.12); color: var(--green); }
.badge-HEAVY_LOAD { background: rgba(249,115,22,0.12); color: var(--orange); }
.badge-IDLE { background: rgba(234,179,8,0.12); color: var(--yellow); }
.badge-OFF { background: rgba(239,68,68,0.12); color: var(--red); }

.badge-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

.badge-RUNNING .badge-dot { background: var(--green); box-shadow: 0 0 6px var(--green-glow); }
.badge-HEAVY_LOAD .badge-dot { background: var(--orange); box-shadow: 0 0 6px var(--orange-glow); }
.badge-IDLE .badge-dot { background: var(--yellow); box-shadow: 0 0 6px var(--yellow-glow); }
.badge-OFF .badge-dot { background: var(--red); box-shadow: 0 0 6px var(--red-glow); }

.badge-RUNNING .badge-dot,
.badge-HEAVY_LOAD .badge-dot {
    animation: dot-pulse 2s ease-in-out infinite;
}

@keyframes dot-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.card-center {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-bottom: 18px;
}

.card-duration {
    font-size: 28px;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.5px;
    line-height: 1;
}

.card-duration-label {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 2px;
}

/* State timeline mini bar */
.card-timeline {
    display: flex;
    height: 8px;
    border-radius: 4px;
    overflow: hidden;
    background: var(--bg-surface);
    margin-bottom: 18px;
    gap: 1px;
}

.tl-segment {
    height: 100%;
    min-width: 2px;
    transition: width 0.5s;
}

.tl-RUNNING { background: var(--green); }
.tl-HEAVY_LOAD { background: var(--orange); }
.tl-IDLE { background: var(--yellow); }
.tl-OFF { background: var(--red); }

/* Card metrics row */
.card-metrics {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1px;
    background: var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}

.card-metric {
    background: var(--bg-surface);
    padding: 12px;
    text-align: center;
}

.card-metric-value {
    font-size: 18px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
}

.card-metric-label {
    font-size: 10px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.6px;
    margin-top: 2px;
}

/* Financial Card Metric */
.card-metric-financial {
    display: none;
}

body.financial-mode .card-metric-technical {
    display: none;
}

body.financial-mode .card-metric-financial {
    display: block;
}

.card-metric-value.loss {
    color: var(--red);
}

.card-metric-value.saving {
    color: var(--green);
}

/* Machine card state duration - show loss in financial mode */
.card-duration-loss {
    display: none;
    color: var(--red);
    font-size: 14px;
    font-weight: 600;
    margin-top: 4px;
}

body.financial-mode .card-duration-loss {
    display: block;
}

body.financial-mode .machine-card.card-OFF .card-duration {
    color: var(--red);
}

/* ============ MODAL ============ */
.modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

.modal.hidden { display: none; }

.modal-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(4px);
}

.modal-panel {
    position: relative;
    background: var(--bg-card);
    border-radius: var(--radius) var(--radius) 0 0;
    width: 100%;
    max-width: 560px;
    max-height: 90vh;
    overflow-y: auto;
    border: 1px solid var(--border-light);
    border-bottom: none;
    animation: slide-up 0.25s ease-out;
}

@keyframes slide-up {
    from { transform: translateY(100px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

@media (min-width: 768px) {
    .modal { align-items: center; }
    .modal-panel {
        border-radius: var(--radius);
        border-bottom: 1px solid var(--border-light);
        max-height: 85vh;
    }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    font-size: 18px;
    font-weight: 700;
}

.modal-subtitle {
    font-size: 13px;
    color: var(--text-muted);
}

.btn-icon {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: var(--bg-surface);
    border-radius: 10px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.15s;
    flex-shrink: 0;
}

.btn-icon svg { width: 20px; height: 20px; }
.btn-icon:hover { background: var(--bg-card-hover); color: var(--text-primary); }

.modal-body { padding: 24px; }

/* State Hero */
.state-hero {
    display: flex;
    align-items: center;
    gap: 24px;
    margin-bottom: 24px;
}

.state-ring {
    width: 88px;
    height: 88px;
    border-radius: 50%;
    padding: 4px;
    flex-shrink: 0;
}

.state-ring.state-RUNNING { background: conic-gradient(var(--green) 0%, var(--green) 100%); }
.state-ring.state-HEAVY_LOAD { background: conic-gradient(var(--orange) 0%, var(--orange) 100%); }
.state-ring.state-IDLE { background: conic-gradient(var(--yellow) 0%, var(--yellow) 100%); }
.state-ring.state-OFF { background: conic-gradient(var(--red) 0%, var(--red) 100%); }

.state-ring-inner {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: var(--bg-card);
    display: flex;
    align-items: center;
    justify-content: center;
}

.state-ring-label {
    font-size: 13px;
    font-weight: 800;
    letter-spacing: 0.5px;
}

.state-hero-duration {
    font-size: 36px;
    font-weight: 800;
    letter-spacing: -1px;
    line-height: 1;
}

.state-hero-sublabel {
    font-size: 13px;
    color: var(--text-muted);
    margin-top: 4px;
}

/* Detail Metrics */
.detail-metrics {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    margin-bottom: 24px;
}

.detail-metric {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--bg-surface);
    border-radius: var(--radius-sm);
    padding: 14px 16px;
}

.detail-metric-icon {
    width: 20px;
    height: 20px;
    color: var(--accent);
    flex-shrink: 0;
}

.detail-metric-value {
    font-size: 18px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
}

.detail-metric-label {
    font-size: 11px;
    color: var(--text-muted);
}

/* Shift Summary */
.shift-summary {
    margin-bottom: 24px;
}

.shift-summary h4 {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 12px;
}

.shift-bar {
    display: flex;
    height: 12px;
    border-radius: 6px;
    overflow: hidden;
    background: var(--bg-surface);
    gap: 2px;
}

.shift-legend {
    display: flex;
    gap: 16px;
    margin-top: 10px;
    flex-wrap: wrap;
}

.shift-legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--text-secondary);
}

.shift-legend-dot {
    width: 8px;
    height: 8px;
    border-radius: 2px;
}

/* Action Button */
.btn-action {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
    padding: 16px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.15s;
    min-height: 56px;
    margin-bottom: 24px;
}

.btn-action svg { width: 20px; height: 20px; }
.btn-action:hover { background: #2563eb; }
.btn-action:active { transform: scale(0.98); }

/* Incidents */
.incidents-section h4 {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 12px;
}

.incident-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 16px;
    background: var(--bg-surface);
    border-radius: var(--radius-sm);
    margin-bottom: 8px;
    font-size: 14px;
    transition: background 0.1s;
}

.incident-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.incident-cat {
    background: var(--bg-card);
    padding: 3px 10px;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    color: var(--text-secondary);
}

.incident-desc {
    color: var(--text-primary);
}

.incident-right {
    text-align: right;
    color: var(--text-muted);
    font-size: 12px;
    white-space: nowrap;
}

.incident-empty {
    text-align: center;
    padding: 24px;
    color: var(--text-muted);
    font-size: 14px;
}

/* Category Grid */
.category-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.category-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 20px 12px;
    background: var(--bg-surface);
    border: 2px solid transparent;
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--text-primary);
    font-size: 13px;
    font-weight: 600;
    transition: all 0.15s;
    min-height: 90px;
}

.category-btn:hover { border-color: var(--border-light); background: var(--bg-card-hover); }
.category-btn:active { border-color: var(--accent); }

.cat-icon {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.cat-icon svg { width: 22px; height: 22px; }

.cat-mechanical { background: rgba(239,68,68,0.12); color: var(--red); }
.cat-electrical { background: rgba(234,179,8,0.12); color: var(--yellow); }
.cat-material { background: rgba(59,130,246,0.12); color: var(--accent); }
.cat-quality { background: rgba(34,197,94,0.12); color: var(--green); }
.cat-changeover { background: rgba(249,115,22,0.12); color: var(--orange); }
.cat-planned { background: rgba(139,92,246,0.12); color: #8b5cf6; }
.cat-operator { background: rgba(236,72,153,0.12); color: #ec4899; }
.cat-unknown { background: rgba(107,114,128,0.12); color: #9ca3af; }

/* Incident Form */
.incident-form { display: flex; flex-direction: column; gap: 12px; }
.incident-form.hidden { display: none; }

.selected-category {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: var(--bg-surface);
    border-radius: var(--radius-sm);
    font-weight: 600;
}

.btn-text {
    background: none;
    border: none;
    color: var(--accent);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}

.input-field {
    width: 100%;
    padding: 14px 16px;
    font-size: 15px;
    font-family: var(--font);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-primary);
    min-height: 52px;
    resize: vertical;
    transition: border-color 0.15s;
}

.input-field:focus { outline: none; border-color: var(--accent); }
.input-field::placeholder { color: var(--text-muted); }

.btn-primary {
    width: 100%;
    padding: 16px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    min-height: 56px;
    transition: all 0.15s;
}

.btn-primary:hover { background: #2563eb; }
.btn-primary:active { transform: scale(0.98); }

/* Toast */
.toast {
    position: fixed;
    bottom: 32px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--green);
    color: white;
    padding: 14px 28px;
    border-radius: var(--radius-sm);
    font-size: 15px;
    font-weight: 600;
    z-index: 200;
    box-shadow: var(--shadow);
    animation: toast-in 0.3s ease-out;
}

.toast.hidden { display: none; }

@keyframes toast-in {
    from { transform: translateX(-50%) translateY(20px); opacity: 0; }
    to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* ============ MACHINE SVG ILLUSTRATIONS ============ */
.machine-svg-container {
    background: linear-gradient(180deg, rgba(15,23,35,0.6) 0%, var(--bg-card) 100%);
    border-radius: 8px;
    padding: 12px 8px 4px;
    margin-bottom: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    position: relative;
}

.machine-svg-container::before {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(ellipse at center bottom, rgba(59,130,246,0.04) 0%, transparent 70%);
    pointer-events: none;
}

.machine-svg {
    width: 100%;
    max-height: 130px;
    opacity: 0.9;
    transition: opacity 0.5s;
}

.machine-svg.anim-active { opacity: 1; }
.machine-svg.anim-idle { opacity: 0.6; }
.machine-svg.anim-off { opacity: 0.35; }

/* CNC Lathe Animations */
.anim-active .cnc-chuck {
    animation: cnc-spin 2s linear infinite;
    transform-origin: 100px 91px;
}

@keyframes cnc-spin {
    to { transform: rotate(360deg); }
}

.anim-active .cnc-tool-post {
    animation: tool-feed 4s ease-in-out infinite alternate;
}

@keyframes tool-feed {
    0% { transform: translateX(0); }
    100% { transform: translateX(30px); }
}

.cnc-sparks .spark {
    animation: spark-fly 0.6s ease-out infinite;
}

.spark.s1 { animation-delay: 0s; }
.spark.s2 { animation-delay: 0.08s; }
.spark.s3 { animation-delay: 0.15s; }
.spark.s4 { animation-delay: 0.22s; }
.spark.s5 { animation-delay: 0.3s; }
.spark.s6 { animation-delay: 0.38s; }
.spark.s7 { animation-delay: 0.45s; }
.spark.s8 { animation-delay: 0.52s; }

@keyframes spark-fly {
    0% { opacity: 1; transform: translate(0, 0) scale(1); }
    100% { opacity: 0; transform: translate(var(--sx, 8px), var(--sy, -12px)) scale(0); }
}

.spark.s1 { --sx: 10px; --sy: -15px; }
.spark.s2 { --sx: 14px; --sy: -20px; }
.spark.s3 { --sx: -6px; --sy: -18px; }
.spark.s4 { --sx: 16px; --sy: -8px; }
.spark.s5 { --sx: 8px; --sy: -22px; }
.spark.s6 { --sx: -8px; --sy: -12px; }
.spark.s7 { --sx: 18px; --sy: -16px; }
.spark.s8 { --sx: -10px; --sy: -20px; }

.cnc-coolant .coolant-drop {
    animation: coolant-drip 1.2s ease-in infinite;
}
.coolant-drop.c1 { animation-delay: 0s; }
.coolant-drop.c2 { animation-delay: 0.4s; }
.coolant-drop.c3 { animation-delay: 0.8s; }

@keyframes coolant-drip {
    0% { opacity: 0.6; transform: translateY(0); }
    100% { opacity: 0; transform: translateY(14px); }
}

/* Press Brake Animations */
.anim-active .press-ram {
    animation: press-stroke 1.8s ease-in-out infinite;
}

@keyframes press-stroke {
    0%, 100% { transform: translateY(0); }
    15% { transform: translateY(0); }
    40% { transform: translateY(16px); }
    55% { transform: translateY(16px); }
    80% { transform: translateY(0); }
}

.press-force {
    animation: force-pulse 1.8s ease-in-out infinite;
}
.press-force.f1 { animation-delay: 0s; }
.press-force.f2 { animation-delay: 0.1s; }
.press-force.f3 { animation-delay: 0.2s; }

@keyframes force-pulse {
    0%, 30%, 100% { opacity: 0; }
    35%, 55% { opacity: 0.8; }
    60% { opacity: 0; }
}

/* Injection Molder Animations */
.anim-active .inj-screw {
    animation: screw-rotate 1.5s linear infinite;
    transform-origin: 97px 73px;
}

@keyframes screw-rotate {
    to { transform: translateX(5px); }
}

.pellet-fall {
    animation: pellet-drop 0.8s ease-in infinite;
}

@keyframes pellet-drop {
    0% { opacity: 0.7; transform: translateY(0); }
    100% { opacity: 0; transform: translateY(20px); }
}

.inj-heat-shimmer .shimmer {
    animation: shimmer-rise 2s ease-out infinite;
}
.shimmer.sh1 { animation-delay: 0s; }
.shimmer.sh2 { animation-delay: 0.6s; }
.shimmer.sh3 { animation-delay: 1.2s; }

@keyframes shimmer-rise {
    0% { opacity: 0.4; transform: translateY(0); }
    100% { opacity: 0; transform: translateY(-12px); }
}

.anim-active .mold-moving {
    animation: mold-clamp 3s ease-in-out infinite;
}

@keyframes mold-clamp {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-2px); }
    50% { transform: translateX(-2px); }
    70% { transform: translateX(6px); }
    85% { transform: translateX(6px); }
}

.anim-active .flow-stream {
    animation: inject-flow 3s ease-in-out infinite;
}

@keyframes inject-flow {
    0%, 15% { opacity: 0; width: 0; }
    20%, 50% { opacity: 0.7; width: 12px; }
    55%, 100% { opacity: 0; width: 0; }
}

.ejected-part {
    animation: part-eject 3s ease-out infinite;
}

@keyframes part-eject {
    0%, 80% { opacity: 0; transform: translateY(0); }
    85% { opacity: 1; transform: translateY(0); }
    100% { opacity: 0; transform: translateY(15px); }
}

/* Generic Machine Animations */
.anim-active .gen-gear {
    animation: gear-spin 3s linear infinite;
    transform-origin: 185px 85px;
}

@keyframes gear-spin {
    to { transform: rotate(360deg); }
}

.gen-belt .belt-dot {
    animation: belt-move 2s linear infinite;
}
.belt-dot.bd1 { animation-delay: 0s; }
.belt-dot.bd2 { animation-delay: 0.5s; }
.belt-dot.bd3 { animation-delay: 1s; }
.belt-dot.bd4 { animation-delay: 1.5s; }

@keyframes belt-move {
    0% { transform: translateX(0); opacity: 0.7; }
    80% { transform: translateX(40px); opacity: 0.7; }
    100% { transform: translateX(50px); opacity: 0; }
}

/* Idle breathing effect */
.anim-idle .machine-svg {
    animation: idle-breathe 3s ease-in-out infinite;
}

@keyframes idle-breathe {
    0%, 100% { opacity: 0.5; }
    50% { opacity: 0.7; }
}

/* ============ RESPONSIVE ============ */
@media (max-width: 900px) {
    .kpi-strip { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
    /* Topnav mobile - stacked layout */
    .topnav {
        flex-direction: column;
        height: auto;
        padding: 10px 12px;
        gap: 10px;
    }

    .topnav-left {
        width: 100%;
        justify-content: space-between;
    }

    .topnav-right {
        width: 100%;
        display: flex;
        gap: 6px;
        justify-content: center;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 2px;
    }

    .logo-text {
        font-size: 14px;
        line-height: 1.2;
    }

    .clock {
        font-size: 12px;
        white-space: nowrap;
    }

    .shift-badge { display: none; }

    /* View buttons - icon only */
    .view-toggle {
        flex-shrink: 0;
    }
    .view-toggle span { display: none; }
    .view-btn {
        padding: 8px;
        min-width: 36px;
        min-height: 36px;
    }
    .view-btn svg {
        width: 16px;
        height: 16px;
    }

    /* Compact buttons */
    .report-btn,
    .install-btn {
        width: 36px;
        height: 36px;
        flex-shrink: 0;
    }
    .report-btn svg,
    .install-btn svg {
        width: 18px;
        height: 18px;
    }

    .lang-btn {
        padding: 6px 10px;
        min-height: 36px;
        font-size: 11px;
        flex-shrink: 0;
    }
}

@media (max-width: 600px) {
    /* COMPACT MOBILE CARDS */
    .machines-grid {
        grid-template-columns: 1fr;
        padding: 12px;
        gap: 12px;
    }

    .kpi-strip { grid-template-columns: repeat(2, 1fr); }

    .kpi-card {
        padding: 14px 16px;
    }

    .kpi-num { font-size: 24px; }
    .kpi-label { font-size: 10px; }
    .kpi-denom, .kpi-unit { font-size: 14px; }

    .detail-metrics { grid-template-columns: 1fr; }

    /* Card optimizations */
    .card-body {
        padding: 16px;
    }

    .card-machine-name {
        font-size: 15px;
    }

    .card-state-badge {
        padding: 5px 10px;
        font-size: 11px;
    }

    .card-duration {
        font-size: 24px;
    }

    .card-metric {
        padding: 10px 8px;
    }

    .card-metric-value {
        font-size: 15px;
    }

    .card-metric-label {
        font-size: 9px;
    }

    .machine-svg-container {
        padding: 8px 4px 2px;
        margin-bottom: 12px;
    }

    .machine-svg {
        max-height: 100px;
    }

    /* Modal fullscreen on mobile */
    .modal-panel {
        max-height: 95vh;
        border-radius: 16px 16px 0 0;
    }

    .modal-body {
        padding: 16px;
    }

    .category-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }

    .category-btn {
        padding: 14px 8px;
        min-height: 80px;
        font-size: 12px;
    }

    .cat-icon {
        width: 36px;
        height: 36px;
    }

    .cat-icon svg {
        width: 18px;
        height: 18px;
    }

    .btn-action,
    .btn-primary {
        min-height: 52px;
        font-size: 15px;
    }

    .state-hero {
        gap: 16px;
    }

    .state-ring {
        width: 72px;
        height: 72px;
    }

    .state-hero-duration {
        font-size: 28px;
    }
}

@media (max-width: 400px) {
    .topnav {
        padding: 8px 10px;
        gap: 8px;
    }

    .logo {
        gap: 6px;
    }

    .logo-icon {
        width: 20px;
        height: 20px;
    }

    .logo-text {
        font-size: 12px;
    }

    .topnav-right {
        gap: 4px;
    }

    .view-btn {
        padding: 6px;
        min-width: 32px;
        min-height: 32px;
    }

    .view-btn svg {
        width: 14px;
        height: 14px;
    }

    .report-btn,
    .install-btn {
        width: 32px;
        height: 32px;
    }

    .report-btn svg,
    .install-btn svg {
        width: 16px;
        height: 16px;
    }

    .clock {
        font-size: 10px;
    }

    .lang-btn {
        padding: 4px 6px;
        min-height: 32px;
        font-size: 9px;
    }

    .kpi-num { font-size: 20px; }
    .kpi-card { padding: 12px; }

    .card-body { padding: 12px; }
    .card-duration { font-size: 20px; }
}

/* Touch improvements for all mobile */
@media (hover: none) and (pointer: coarse) {
    .machine-card:hover {
        transform: none;
        box-shadow: none;
    }

    .machine-card:active {
        transform: scale(0.98);
        background: var(--bg-card-hover);
    }

    .btn-action:hover,
    .btn-primary:hover,
    .category-btn:hover,
    .view-btn:hover,
    .report-btn:hover {
        background: inherit;
    }

    .btn-action:active,
    .btn-primary:active {
        transform: scale(0.98);
    }

    .category-btn:active {
        border-color: var(--accent);
        background: var(--bg-card-hover);
    }
}

/* Safe area for notched phones */
@supports (padding-top: env(safe-area-inset-top)) {
    .topnav {
        padding-top: max(10px, env(safe-area-inset-top));
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
    }

    .machines-grid {
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
        padding-bottom: max(12px, env(safe-area-inset-bottom));
    }

    .modal-panel {
        padding-bottom: env(safe-area-inset-bottom);
    }

    .toast {
        bottom: max(32px, calc(env(safe-area-inset-bottom) + 16px));
    }
}

/* ============ WOW-FACTOR ANIMATIONS ============ */

/* Shake animation for alerts */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-4px); }
    20%, 40%, 60%, 80% { transform: translateX(4px); }
}

.shake-animation {
    animation: shake 0.5s ease-in-out;
}

/* Pulse animation for loss increases */
@keyframes pulse-loss {
    0% { transform: scale(1); }
    25% { transform: scale(1.05); }
    50% { transform: scale(1); }
    75% { transform: scale(1.03); }
    100% { transform: scale(1); }
}

.pulse-animation {
    animation: pulse-loss 0.6s ease-out;
}

/* Flash red animation for money loss */
@keyframes flash-red {
    0% { color: var(--red); text-shadow: 0 0 20px var(--red-glow); transform: scale(1.1); }
    50% { color: var(--red); text-shadow: 0 0 30px var(--red-glow); transform: scale(1.15); }
    100% { color: inherit; text-shadow: none; transform: scale(1); }
}

.flash-loss {
    animation: flash-red 1s ease-out;
}

/* Urgent loss card styling when machine is OFF */
.kpi-card.loss-active {
    border-color: var(--red);
    box-shadow: 0 0 20px var(--red-glow);
    animation: loss-glow 2s ease-in-out infinite;
}

@keyframes loss-glow {
    0%, 100% { box-shadow: 0 0 15px var(--red-glow); }
    50% { box-shadow: 0 0 30px var(--red-glow), 0 0 40px rgba(239, 68, 68, 0.2); }
}

/* Big loss number pulsing */
.kpi-num.loss-counting {
    animation: count-up 0.3s ease-out;
}

@keyframes count-up {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); color: var(--red); }
    100% { transform: scale(1); }
}

/* Machine card urgent state */
.machine-card.card-OFF {
    animation: card-alert-pulse 3s ease-in-out infinite;
}

@keyframes card-alert-pulse {
    0%, 100% { border-color: var(--red); }
    50% { border-color: rgba(239, 68, 68, 0.6); box-shadow: 0 0 20px var(--red-glow); }
}

/* Toast with alert styling */
.toast.toast-alert {
    background: var(--red);
    animation: toast-alert-in 0.3s ease-out;
}

@keyframes toast-alert-in {
    0% { transform: translateX(-50%) translateY(20px) scale(0.9); opacity: 0; }
    50% { transform: translateX(-50%) translateY(-5px) scale(1.05); }
    100% { transform: translateX(-50%) translateY(0) scale(1); opacity: 1; }
}

/* Counter tick animation */
@keyframes tick-up {
    0% { opacity: 1; transform: translateY(0); }
    50% { opacity: 0.7; transform: translateY(-2px); }
    100% { opacity: 1; transform: translateY(0); }
}

.tick-animation {
    animation: tick-up 0.2s ease-out;
}

/* Sound enabled indicator */
.sound-enabled::after {
    content: '';
    position: absolute;
    top: 4px;
    right: 4px;
    width: 6px;
    height: 6px;
    background: var(--green);
    border-radius: 50%;
}

/* MOBILE COMPACT MODE - Smaller cards for phones */
@media (max-width: 600px) {
    .machines-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        padding: 8px !important;
        gap: 8px !important;
    }

    .machine-card {
        border-radius: 12px;
    }

    .card-body {
        padding: 12px 10px !important;
    }

    .card-machine-name {
        font-size: 13px !important;
        line-height: 1.2;
    }

    .card-location {
        font-size: 9px !important;
    }

    .card-state-badge {
        padding: 3px 6px !important;
        font-size: 9px !important;
    }

    .card-duration {
        font-size: 18px !important;
    }

    .card-duration-label {
        font-size: 8px !important;
    }

    .card-metrics {
        gap: 4px !important;
    }

    .card-metric {
        padding: 6px 4px !important;
    }

    .card-metric-value {
        font-size: 12px !important;
    }

    .card-metric-label {
        font-size: 7px !important;
    }

    .machine-svg-container {
        padding: 4px 2px 0 !important;
        margin-bottom: 8px !important;
    }

    .machine-svg {
        max-height: 60px !important;
    }

    .card-timeline {
        height: 3px !important;
        margin-top: 8px !important;
    }

    /* Smaller KPIs on mobile */
    .kpi-strip {
        padding: 8px !important;
        gap: 6px !important;
    }

    .kpi-card {
        padding: 10px 8px !important;
        border-radius: 10px !important;
    }

    .kpi-num {
        font-size: 20px !important;
    }

    /* Compact topnav */
    .topnav {
        padding: 8px 10px !important;
    }

    .logo-text {
        font-size: 12px !important;
    }
}

/* Even smaller for very small phones */
@media (max-width: 400px) {
    .card-machine-name {
        font-size: 11px !important;
    }

    .card-duration {
        font-size: 16px !important;
    }

    .machine-svg {
        max-height: 50px !important;
    }

    .kpi-num {
        font-size: 18px !important;
    }
}
