/* SuperCal — Clean, professional calibration management UI */
:root {
    --red: #dc2626;
    --red-bg: #fef2f2;
    --amber: #d97706;
    --amber-bg: #fffbeb;
    --green: #16a34a;
    --green-bg: #f0fdf4;
    --grey: #6b7280;
    --grey-bg: #f3f4f6;
    --primary: #1a56db;
    --primary-hover: #1e40af;
    --primary-light: #eff6ff;
    --bg: #f8fafc;
    --card: #ffffff;
    --text: #1e293b;
    --text-light: #64748b;
    --border: #e2e8f0;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.1);
    --shadow-lg: 0 8px 30px rgba(0,0,0,0.12);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: var(--bg); color: var(--text); }

.hidden { display: none !important; }
.screen { min-height: 100vh; }

/* Login */
.login-container { max-width: 400px; margin: 0 auto; padding: 80px 20px; }
.login-logo { text-align: center; margin-bottom: 40px; }
.login-logo h1 { font-size: 2.5rem; color: var(--text); }
.accent { color: var(--primary); }
.tagline { color: var(--text-light); margin-top: 4px; }
.login-form { background: var(--card); padding: 32px; border-radius: var(--radius); box-shadow: var(--shadow); }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 0.875rem; font-weight: 600; margin-bottom: 4px; }
.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="password"],
.form-group input[type="number"],
.form-group input[type="date"],
.form-group input[type="file"],
.form-group select,
.form-group textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 0.9rem; font-family: inherit; background: var(--card); }
.form-group input:focus, .form-group select:focus, .form-group textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(26,86,219,0.1); }
.error-text { color: var(--red); font-size: 0.875rem; margin-top: 8px; }
.demo-hint { margin-top: 20px; padding: 12px; background: var(--grey-bg); border-radius: var(--radius); font-size: 0.8rem; color: var(--text-light); }
.demo-hint p { margin: 2px 0; }

/* Buttons */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 10px 20px; border: none; border-radius: var(--radius); font-size: 0.875rem; font-weight: 600; cursor: pointer; transition: all 0.15s; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover { background: var(--grey-bg); }
.btn-danger { background: var(--red); color: white; }
.btn-danger:hover { background: #b91c1c; }
.btn-full { width: 100%; justify-content: center; }
.btn-small { padding: 6px 12px; font-size: 0.8rem; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* Header */
.app-header { display: flex; justify-content: space-between; align-items: center; padding: 12px 24px; background: var(--card); border-bottom: 1px solid var(--border); }
.header-left { display: flex; align-items: center; gap: 16px; }
.header-logo { font-size: 1.5rem; }
.org-name { color: var(--text-light); font-size: 0.9rem; }
.header-right { display: flex; align-items: center; gap: 12px; }
.user-name { font-size: 0.875rem; font-weight: 600; }
.user-role { font-size: 0.75rem; color: var(--text-light); padding: 2px 8px; background: var(--grey-bg); border-radius: 10px; }

/* Nav */
.app-nav { display: flex; gap: 0; padding: 0 24px; background: var(--card); border-bottom: 1px solid var(--border); }
.nav-btn { padding: 12px 20px; border: none; background: none; font-size: 0.9rem; font-weight: 500; color: var(--text-light); cursor: pointer; border-bottom: 2px solid transparent; }
.nav-btn:hover { color: var(--text); }
.nav-btn.active { color: var(--primary); border-bottom-color: var(--primary); }

/* Views */
.view { padding: 24px; max-width: 1400px; margin: 0 auto; }

/* RAG Dashboard */
.rag-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; margin-bottom: 16px; }
.rag-card { padding: 32px 24px; border-radius: var(--radius); text-align: center; cursor: pointer; transition: transform 0.15s, box-shadow 0.15s; box-shadow: var(--shadow); }
.rag-card:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.15); }
.rag-count { font-size: 3rem; font-weight: 700; line-height: 1; }
.rag-label { font-size: 0.95rem; font-weight: 600; margin-top: 8px; }
.rag-red { background: var(--red-bg); color: var(--red); border: 2px solid var(--red); }
.rag-amber { background: var(--amber-bg); color: var(--amber); border: 2px solid var(--amber); }
.rag-green { background: var(--green-bg); color: var(--green); border: 2px solid var(--green); }
.rag-grey { background: var(--grey-bg); color: var(--grey); border: 2px solid var(--grey); }
.total-count { text-align: center; color: var(--text-light); font-size: 0.95rem; margin-bottom: 32px; }

/* Dashboard sections */
.dashboard-sections { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.dash-section { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.dash-section-title { padding: 16px 20px; font-size: 0.95rem; font-weight: 700; border-bottom: 1px solid var(--border); color: var(--text); }
.dash-list { max-height: 320px; overflow-y: auto; }
.dash-item { display: flex; align-items: center; gap: 12px; padding: 12px 20px; border-bottom: 1px solid var(--border); cursor: pointer; transition: background 0.1s; font-size: 0.875rem; }
.dash-item:last-child { border-bottom: none; }
.dash-item:hover { background: var(--primary-light); }
.dash-item-info { flex: 1; min-width: 0; }
.dash-item-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dash-item-sub { color: var(--text-light); font-size: 0.8rem; margin-top: 2px; }
.dash-item-date { font-size: 0.8rem; color: var(--text-light); white-space: nowrap; }
.dash-empty { padding: 24px 20px; color: var(--text-light); font-size: 0.875rem; text-align: center; }

/* Instruments Table */
.instruments-toolbar { display: flex; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }
.instruments-toolbar input, .instruments-toolbar select { padding: 8px 12px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 0.875rem; }
.instruments-toolbar input[type="text"] { flex: 1; min-width: 200px; }
.instruments-table-wrap { overflow-x: auto; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); }
.instruments-table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
.instruments-table th { text-align: left; padding: 12px 16px; background: var(--grey-bg); font-weight: 600; white-space: nowrap; border-bottom: 2px solid var(--border); }
.sortable-th { cursor: pointer; user-select: none; transition: background 0.1s; }
.sortable-th:hover { background: #e2e8f0; }
.sort-arrow { font-size: 0.7rem; color: var(--primary); }
.instruments-table td { padding: 10px 16px; border-bottom: 1px solid var(--border); }
.instruments-table tr { cursor: pointer; }
.instruments-table tr:hover { background: #f1f5f9; }
.status-badge { display: inline-block; padding: 3px 10px; border-radius: 12px; font-size: 0.75rem; font-weight: 700; text-transform: uppercase; }
.status-Overdue { background: var(--red-bg); color: var(--red); }
.status-DueSoon { background: var(--amber-bg); color: var(--amber); }
.status-InCalibration { background: var(--green-bg); color: var(--green); }
.status-OutOfService { background: var(--grey-bg); color: var(--grey); }
.cert-count-cell { text-align: center; }
.cert-count-pill { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 0.75rem; font-weight: 600; background: var(--primary-light); color: var(--primary); }

/* Audit */
.audit-toolbar { display: flex; align-items: center; gap: 16px; margin-bottom: 20px; }
.audit-result { font-weight: 600; }
.audit-result.intact { color: var(--green); }
.audit-result.tampered { color: var(--red); }
.audit-list { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); }
.audit-entry { padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 0.875rem; }
.audit-entry:last-child { border-bottom: none; }
.audit-meta { color: var(--text-light); font-size: 0.8rem; }
.audit-override { background: #fef3c7; border-left: 4px solid var(--amber); }
.audit-action { font-weight: 600; }

/* Slide-out Detail Panel */
.detail-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.3); z-index: 90; transition: opacity 0.2s; }
.detail-panel { position: fixed; top: 0; right: 0; bottom: 0; width: 680px; max-width: 100vw; background: var(--card); z-index: 100; box-shadow: -4px 0 24px rgba(0,0,0,0.15); display: flex; flex-direction: column; transform: translateX(100%); transition: transform 0.25s ease; }
.detail-panel.open { transform: translateX(0); }
.detail-panel-header { display: flex; justify-content: space-between; align-items: flex-start; padding: 24px 24px 16px; border-bottom: 1px solid var(--border); }
.detail-panel-title { font-size: 1.25rem; margin-bottom: 6px; }
.detail-panel-actions { display: flex; align-items: center; gap: 8px; }
.detail-close { background: none; border: none; font-size: 1.8rem; cursor: pointer; color: var(--text-light); line-height: 1; padding: 0 4px; }
.detail-close:hover { color: var(--text); }

/* Detail Tabs */
.detail-tabs { display: flex; border-bottom: 1px solid var(--border); padding: 0 24px; }
.detail-tab { padding: 12px 16px; border: none; background: none; font-size: 0.875rem; font-weight: 500; color: var(--text-light); cursor: pointer; border-bottom: 2px solid transparent; display: flex; align-items: center; gap: 6px; }
.detail-tab:hover { color: var(--text); }
.detail-tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-badge { display: inline-flex; align-items: center; justify-content: center; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; font-size: 0.7rem; font-weight: 700; background: var(--primary-light); color: var(--primary); }

.detail-panel-body { flex: 1; overflow-y: auto; padding: 24px; }
.tab-content { animation: fadeIn 0.15s ease; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.detail-item { padding: 0; }
.detail-label { font-size: 0.7rem; font-weight: 700; color: var(--text-light); text-transform: uppercase; letter-spacing: 0.5px; }
.detail-value { font-size: 0.95rem; margin-top: 2px; }
.detail-notes { margin-top: 20px; padding: 16px; background: var(--grey-bg); border-radius: var(--radius); font-size: 0.875rem; }
.detail-notes strong { display: block; margin-bottom: 4px; font-size: 0.75rem; text-transform: uppercase; color: var(--text-light); letter-spacing: 0.5px; }

/* Certificate List */
.cert-upload-area { margin-bottom: 16px; }
.cert-list { display: flex; flex-direction: column; gap: 8px; }
.cert-card { display: flex; align-items: center; gap: 16px; padding: 16px; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); transition: border-color 0.15s; }
.cert-card:hover { border-color: var(--primary); }
.cert-icon { width: 40px; height: 40px; background: var(--red-bg); color: var(--red); border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 0.7rem; font-weight: 700; flex-shrink: 0; }
.cert-info { flex: 1; min-width: 0; }
.cert-name { font-weight: 600; font-size: 0.875rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cert-meta { font-size: 0.8rem; color: var(--text-light); margin-top: 2px; }
.cert-actions { display: flex; gap: 6px; flex-shrink: 0; }
.cert-empty { padding: 32px; text-align: center; color: var(--text-light); }

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 200; }
.modal-content { background: var(--card); border-radius: var(--radius); max-height: 90vh; overflow-y: auto; position: relative; }
.modal-form { padding: 0; width: 90%; max-width: 700px; }
.modal-small { max-width: 520px; }
.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: 1.1rem; }
.modal-close { background: none; border: none; font-size: 1.5rem; cursor: pointer; color: var(--text-light); }
.modal-close:hover { color: var(--text); }

/* Forms */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; padding: 24px 24px 0; }
.form-actions { display: flex; justify-content: flex-end; gap: 12px; padding: 20px 24px; }
.form-check-group { display: flex; align-items: center; }
.form-check-group label { display: flex; align-items: center; gap: 8px; cursor: pointer; font-weight: normal; }
#instrument-form .form-group:last-of-type { padding: 0 24px; }
#instrument-form .error-text { padding: 0 24px 16px; }

/* Chat Widget */
.chat-widget { position: fixed; bottom: 24px; right: 24px; z-index: 200; }
.chat-toggle { display: flex; align-items: center; gap: 8px; padding: 12px 20px; background: var(--primary); color: white; border: none; border-radius: 24px; font-size: 0.9rem; font-weight: 600; cursor: pointer; box-shadow: 0 4px 12px rgba(26,86,219,0.4); }
.chat-toggle:hover { background: var(--primary-hover); }
.chat-icon { font-size: 1.2rem; }
.chat-panel { position: absolute; bottom: 60px; right: 0; width: 400px; height: 500px; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow-lg); display: flex; flex-direction: column; overflow: hidden; }
.chat-header { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; background: var(--primary); color: white; }
.chat-close { background: none; border: none; color: white; font-size: 1.3rem; cursor: pointer; }
.chat-messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.chat-msg { padding: 10px 14px; border-radius: 12px; font-size: 0.875rem; line-height: 1.5; max-width: 85%; white-space: pre-wrap; }
.chat-msg.user { background: var(--primary); color: white; align-self: flex-end; border-bottom-right-radius: 4px; }
.chat-msg.assistant { background: var(--grey-bg); color: var(--text); align-self: flex-start; border-bottom-left-radius: 4px; }
.chat-msg.typing { color: var(--text-light); font-style: italic; }
.chat-input-area { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); }
.chat-input-area input { flex: 1; padding: 8px 12px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 0.875rem; }

/* Analysis row tinting — subtle left border + background */
.instruments-table tr.analysis-row-excellent { border-left: 4px solid #16a34a; background: #f0fdf4; }
.instruments-table tr.analysis-row-good { border-left: 4px solid #86efac; background: #f7fef9; }
.instruments-table tr.analysis-row-slight-drift { border-left: 4px solid #fbbf24; background: #fffdf5; }
.instruments-table tr.analysis-row-drift { border-left: 4px solid #f97316; background: #fff9f5; }
.instruments-table tr.analysis-row-excessive-drift { border-left: 4px solid #dc2626; background: #fef5f5; }
.instruments-table tr.analysis-row-unstable { border-left: 4px solid #a78bfa; background: #faf8ff; }
.instruments-table tr.analysis-row-excellent:hover { background: #e2fbe8; }
.instruments-table tr.analysis-row-good:hover { background: #e8fbee; }
.instruments-table tr.analysis-row-slight-drift:hover { background: #fef9e7; }
.instruments-table tr.analysis-row-drift:hover { background: #fef3e7; }
.instruments-table tr.analysis-row-excessive-drift:hover { background: #fee7e7; }
.instruments-table tr.analysis-row-unstable:hover { background: #f0eaff; }

/* Analysis pill in table */
.analysis-pill { display: inline-block; padding: 3px 10px; border-radius: 12px; font-size: 0.7rem; font-weight: 700; white-space: nowrap; }
.analysis-pill.excellent { background: #dcfce7; color: #15803d; }
.analysis-pill.good { background: #f0fdf4; color: #16a34a; }
.analysis-pill.slight-drift { background: #fffbeb; color: #b45309; }
.analysis-pill.drift { background: #fff7ed; color: #c2410c; }
.analysis-pill.excessive-drift { background: #fef2f2; color: #dc2626; }
.analysis-pill.unstable { background: #faf5ff; color: #7c3aed; }

/* Analysis Tab */
.analysis-loading, .analysis-nodata { padding: 32px; text-align: center; color: var(--text-light); }
.analysis-hint { font-size: 0.8rem; margin-top: 8px; }
.analysis-header { margin-bottom: 24px; }
.analysis-rating-badge { display: inline-block; padding: 8px 20px; border-radius: 6px; font-size: 1.1rem; font-weight: 700; margin-bottom: 16px; }
.analysis-rating-badge.excellent { background: #dcfce7; color: #15803d; border: 2px solid #16a34a; }
.analysis-rating-badge.good { background: #f0fdf4; color: #16a34a; border: 2px solid #86efac; }
.analysis-rating-badge.slight-drift { background: #fffbeb; color: #b45309; border: 2px solid #fbbf24; }
.analysis-rating-badge.drift { background: #fff7ed; color: #c2410c; border: 2px solid #f97316; }
.analysis-rating-badge.excessive-drift { background: #fef2f2; color: #dc2626; border: 2px solid #dc2626; }
.analysis-rating-badge.unstable { background: #faf5ff; color: #7c3aed; border: 2px solid #a78bfa; }
.analysis-rating-badge.no-data { background: var(--grey-bg); color: var(--grey); border: 2px solid var(--grey); }

.analysis-stats { display: flex; gap: 24px; }
.analysis-stat { display: flex; flex-direction: column; align-items: center; }
.analysis-stat-value { font-size: 1.5rem; font-weight: 700; color: var(--text); }
.analysis-stat-label { font-size: 0.75rem; color: var(--text-light); text-transform: uppercase; letter-spacing: 0.5px; }

.analysis-testpoints { display: flex; flex-direction: column; gap: 12px; }
.tp-card { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.tp-card-header { display: flex; align-items: center; gap: 12px; padding: 12px 16px; cursor: pointer; transition: background 0.1s; }
.tp-card-header:hover { background: #f1f5f9; }
.tp-card-name { font-weight: 600; font-size: 0.9rem; flex: 1; }
.tp-card-rating { font-size: 0.75rem; font-weight: 700; padding: 3px 10px; border-radius: 12px; }
.tp-card-rating.excellent { background: #dcfce7; color: #15803d; }
.tp-card-rating.good { background: #f0fdf4; color: #16a34a; }
.tp-card-rating.slight-drift { background: #fffbeb; color: #b45309; }
.tp-card-rating.drift { background: #fff7ed; color: #c2410c; }
.tp-card-rating.excessive-drift { background: #fef2f2; color: #dc2626; }
.tp-card-rating.unstable { background: #faf5ff; color: #7c3aed; }
.tp-card-rating.no-data { background: var(--grey-bg); color: var(--grey); }
.tp-card-meta { font-size: 0.8rem; color: var(--text-light); }
.tp-card-detail { padding: 0 16px 16px; display: none; }
.tp-card.expanded .tp-card-detail { display: block; }
.tp-card-chevron { color: var(--text-light); transition: transform 0.2s; font-size: 0.8rem; }
.tp-card.expanded .tp-card-chevron { transform: rotate(90deg); }
.tp-readings-table { width: 100%; border-collapse: collapse; font-size: 0.8rem; margin-top: 8px; }
.tp-readings-table th { text-align: left; padding: 6px 10px; background: var(--grey-bg); font-weight: 600; }
.tp-readings-table td { padding: 6px 10px; border-bottom: 1px solid var(--border); }
.tp-deviation-bar { display: inline-block; height: 8px; border-radius: 4px; min-width: 4px; vertical-align: middle; }
.tp-deviation-bar.positive { background: var(--amber); }
.tp-deviation-bar.negative { background: var(--primary); }
.tp-deviation-bar.over-tolerance { background: var(--red); }

/* Measurement Results Modal & Table */
.modal-wide { max-width: 900px; }
.results-modal-body { padding: 16px 24px 24px; }
.results-toolbar { margin-bottom: 12px; }
.results-table-wrap { overflow-x: auto; max-height: 400px; overflow-y: auto; margin-bottom: 16px; }
.results-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.results-table th { text-align: left; padding: 8px 6px; background: var(--grey-bg); font-weight: 600; white-space: nowrap; position: sticky; top: 0; }
.results-table td { padding: 4px 4px; }
.results-table input, .results-table select { width: 100%; padding: 6px 8px; border: 1px solid var(--border); border-radius: 4px; font-size: 0.85rem; font-family: inherit; }
.results-table input[type="number"] { width: 100px; }
.results-table input[type="text"] { min-width: 120px; }
.results-table .btn-remove-row { background: none; border: none; color: var(--red); cursor: pointer; font-size: 1.1rem; padding: 4px; }
.results-table .btn-remove-row:hover { color: #991b1b; }

/* Cert card results badge */
.cert-results-badge { display: inline-block; padding: 2px 6px; border-radius: 8px; font-size: 0.7rem; font-weight: 600; margin-left: 8px; }
.cert-results-badge.has-results { background: #dcfce7; color: #15803d; }
.cert-results-badge.no-results { background: var(--grey-bg); color: var(--text-light); }

/* Toast notifications */
.toast { position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%); padding: 12px 24px; border-radius: var(--radius); font-size: 0.875rem; font-weight: 600; color: white; z-index: 300; animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s; pointer-events: none; }
.toast-success { background: var(--green); }
.toast-error { background: var(--red); }
@keyframes toastIn { from { opacity: 0; transform: translateX(-50%) translateY(20px); } to { opacity: 1; transform: translateX(-50%) translateY(0); } }
@keyframes toastOut { from { opacity: 1; } to { opacity: 0; } }

/* Prevent iOS zoom on focus — all inputs must be >= 16px on mobile */
@media (max-width: 768px) {
    input, select, textarea { font-size: 16px !important; }
}

/* Responsive */
@media (max-width: 768px) {
    .rag-grid { grid-template-columns: repeat(2, 1fr); }
    .dashboard-sections { grid-template-columns: 1fr; }
    .chat-panel { width: calc(100vw - 48px); right: -12px; }
    .detail-grid { grid-template-columns: 1fr; }
    .instruments-toolbar { flex-direction: column; }
    .detail-panel { width: 100vw; }
    .form-grid { grid-template-columns: 1fr; }
    .header-right { gap: 8px; }
    .user-role { display: none; }
}
