/* ═══════════════════════════════════════════════════════════════════════════
   TA252602002 Grounding Tester — ISA 101 Compliant HMI Design System
   ───────────────────────────────────────────────────────────────────────────
   ISA-101.01-2015 High Performance HMI Guidelines:
   • LIGHT GRAY backgrounds — neutral, low-fatigue, high readability
   • Color reserved ONLY for status/alarm indication
   • Flat design — no gratuitous gradients or 3D effects
   • Consistent layout with clear hierarchy (L1→L4)
   • Redundant coding — shape + color + text for status
   • Sans-serif typography, high contrast dark text on light background

   NOTE: ISA 101 supports both light-on-dark and dark-on-light schemes.
   Light gray backgrounds (#C0-#E0 range) with dark text are ISA-compliant
   and are the more traditional choice in industrial HMI environments,
   providing higher readability under ambient lighting conditions.
   ═══════════════════════════════════════════════════════════════════════════ */

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

/* ─── ISA 101 Color Tokens (Light Gray Theme) ──────────────────────────── */
:root {
    /* ISA Gray Scale — light gray backgrounds for readability */
    --bg-primary: #d9d9d9;          /* Main background (ISA light gray) */
    --bg-secondary: #e4e4e4;        /* Lighter panels / inset areas */
    --bg-card: #efefef;             /* Card / widget surfaces */
    --bg-card-hover: #f5f5f5;       /* Card hover */
    --bg-input: #ffffff;            /* Form inputs */
    --bg-header: #3a3a3a;           /* Top navigation (dark header bar) */
    --bg-table-header: #c8c8c8;     /* Table header row */
    --bg-table-alt: rgba(0,0,0,0.02); /* Alternating table row */

    /* Borders */
    --border-default: #b0b0b0;
    --border-light: #c5c5c5;
    --border-focus: #4a90d9;

    /* Text — dark on light for readability */
    --text-primary: #1a1a1a;        /* Primary text (near black) */
    --text-secondary: #4a4a4a;      /* Secondary / labels */
    --text-muted: #777777;          /* Muted / hints */
    --text-on-dark: #f0f0f0;        /* Text on dark surfaces (header) */

    /* ISA 18.2 Alarm Colors — RESERVED for status indication only */
    --alarm-critical: #CC0000;      /* Priority 4 — Critical alarm (Red) */
    --alarm-high: #E67300;          /* Priority 3 — High alarm (Orange) */
    --alarm-medium: #CC9900;        /* Priority 2 — Medium / Warning (Amber) */
    --alarm-low: #0077B3;           /* Priority 1 — Low / Info (Blue) */
    --alarm-diagnostic: #7B4DB3;    /* Priority 0 — Diagnostic (Purple) */

    /* Process Status Colors — slightly muted for light background */
    --status-normal: #8a8a8a;       /* Normal operation — gray (no color!) */
    --status-pass: #2E7D32;         /* Pass / OK — dark green */
    --status-fail: #CC0000;         /* Fail / Alarm — dark red */
    --status-warning: #CC9900;      /* Warning — amber */
    --status-info: #0077B3;         /* Informational — blue */

    /* Status backgrounds (subtle tinting) */
    --status-pass-bg: rgba(46, 125, 50, 0.10);
    --status-fail-bg: rgba(204, 0, 0, 0.08);
    --status-warning-bg: rgba(204, 153, 0, 0.10);
    --status-info-bg: rgba(0, 119, 179, 0.08);

    /* Spacing & Radius (ISA: clean, structured, minimal roundness) */
    --radius-sm: 4px;
    --radius-md: 6px;
    --radius-lg: 8px;
    --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.12);
    --transition: all 0.15s ease;
}

/* ─── Reset & Base ──────────────────────────────────────────────────────── */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html { scroll-behavior: smooth; }

body {
    font-family: 'Inter', 'Segoe UI', -apple-system, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    min-height: 100vh;
    overflow-x: hidden;
    font-size: 14px;
}

/* ─── Scrollbar (subtle) ────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg-secondary); }
::-webkit-scrollbar-thumb { background: #aaa; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #888; }

/* ─── Minimal Animations (ISA: avoid distracting effects) ───────────────── */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.animate-in { animation: fadeIn 0.3s ease forwards; }

/* ─── Login Page ────────────────────────────────────────────────────────── */
.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 20px;
    background: var(--bg-primary);
}

.login-card {
    background: var(--bg-card);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    padding: 40px 36px;
    width: 100%;
    max-width: 400px;
    box-shadow: var(--shadow-card);
}

.login-logo {
    text-align: center;
    margin-bottom: 28px;
}

.login-logo .icon {
    font-size: 36px;
    display: block;
    margin-bottom: 10px;
}

.login-logo h1 {
    font-size: 18px;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.01em;
}

.login-logo .subtitle {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 4px;
}

.login-error {
    background: var(--status-fail-bg);
    color: var(--status-fail);
    border: 1px solid rgba(204, 0, 0, 0.25);
    border-radius: var(--radius-sm);
    padding: 8px 12px;
    font-size: 13px;
    margin-bottom: 14px;
    display: none;
}

/* ─── Form Elements ─────────────────────────────────────────────────────── */
.form-group {
    margin-bottom: 14px;
}

.form-group label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.form-input {
    width: 100%;
    padding: 10px 12px;
    background: var(--bg-input);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 14px;
    transition: var(--transition);
    outline: none;
}

.form-input:focus {
    border-color: var(--border-focus);
    box-shadow: 0 0 0 2px rgba(74, 144, 217, 0.2);
}

.form-input::placeholder { color: var(--text-muted); }

select.form-input {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' fill='%234a4a4a' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    padding-right: 30px;
}

/* ─── Buttons (flat, ISA-consistent) ────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 16px;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    text-decoration: none;
    white-space: nowrap;
}

.btn-primary {
    background: #4a6e8a;
    color: #fff;
    border-color: #3a5e7a;
}

.btn-primary:hover {
    background: #5a7e9a;
}

.btn-secondary {
    background: var(--bg-card);
    color: var(--text-secondary);
    border: 1px solid var(--border-default);
}

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

.btn-danger {
    background: #8b3a3a;
    color: #fff;
    border-color: #7a2a2a;
}

.btn-danger:hover { background: #9b4a4a; }

.btn-sm { padding: 5px 12px; font-size: 12px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ─── Dashboard Header (kept dark — ISA standard nav bar) ───────────────── */
.dashboard-header {
    background: var(--bg-header);
    border-bottom: 1px solid #2a2a2a;
    padding: 10px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 8px;
}

.header-brand .icon { font-size: 20px; }

.header-brand h1 {
    font-size: 15px;
    font-weight: 700;
    color: var(--text-on-dark);
}

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

/* Override button colors for dark header */
.header-actions .btn-secondary {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-on-dark);
    border-color: rgba(255, 255, 255, 0.2);
}
.header-actions .btn-secondary:hover {
    background: rgba(255, 255, 255, 0.18);
}

.user-info {
    display: flex;
    align-items: center;
    gap: 6px;
    color: #cccccc;
    font-size: 12px;
}

.role-badge {
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 3px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.role-badge.master {
    background: #4a6e8a;
    color: #fff;
}

.role-badge.user {
    background: rgba(255, 255, 255, 0.15);
    color: #ccc;
}

/* ─── ISA Level Indicator ───────────────────────────────────────────────── */
.level-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    color: var(--text-muted);
    margin-bottom: 16px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
}

.level-indicator .level-tag {
    background: var(--bg-header);
    border: 1px solid var(--bg-header);
    padding: 2px 8px;
    border-radius: 3px;
    color: var(--text-on-dark);
    font-size: 10px;
}

.level-indicator .separator {
    color: var(--text-muted);
}

/* ─── Dashboard Main Content ────────────────────────────────────────────── */
.dashboard-main {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px 24px;
}

.section {
    margin-bottom: 24px;
}

.section-header {
    margin-bottom: 12px;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
}

.section-header h2 {
    font-size: 14px;
    font-weight: 700;
    color: var(--text-primary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    display: flex;
    align-items: center;
    gap: 6px;
}

.section-header .section-subtitle {
    font-size: 12px;
    color: var(--text-muted);
}

/* ─── KPI Cards (ISA: clean numeric displays) ───────────────────────────── */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 12px;
}

.kpi-card {
    background: var(--bg-card);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    padding: 16px 18px;
    box-shadow: var(--shadow-card);
    transition: var(--transition);
    position: relative;
}

/* Thin top border indicator — subtle, ISA-appropriate */
.kpi-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: var(--status-normal);
    border-radius: var(--radius-md) var(--radius-md) 0 0;
}

.kpi-card:hover { background: var(--bg-card-hover); }

.kpi-icon {
    font-size: 18px;
    margin-bottom: 6px;
    display: block;
}

.kpi-value {
    font-family: 'Roboto Mono', 'Consolas', monospace;
    font-size: 28px;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1.1;
}

.kpi-label {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

/* ─── Map ───────────────────────────────────────────────────────────────── */
.map-wrapper {
    background: var(--bg-card);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    overflow: hidden;
    box-shadow: var(--shadow-card);
}

#map {
    width: 100%;
    height: 420px;
    z-index: 1;
}

/* Leaflet popup — styled to match ISA light palette */
.leaflet-popup-content-wrapper {
    background: var(--bg-card) !important;
    color: var(--text-primary) !important;
    border-radius: var(--radius-md) !important;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15) !important;
    border: 1px solid var(--border-default) !important;
}

.leaflet-popup-tip {
    background: var(--bg-card) !important;
    border: 1px solid var(--border-default) !important;
}

.leaflet-popup-content {
    margin: 10px 14px !important;
    font-family: 'Inter', sans-serif !important;
    font-size: 12px !important;
    line-height: 1.5 !important;
}

.popup-title {
    font-weight: 700;
    font-size: 13px;
    margin-bottom: 4px;
    color: var(--text-primary);
}

.popup-info {
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.popup-status {
    margin-bottom: 8px;
}

.popup-link {
    color: var(--border-focus);
    cursor: pointer;
    font-weight: 600;
    text-decoration: none;
    font-size: 11px;
}

.popup-link:hover { text-decoration: underline; }

/* ─── Data Tables (ISA: clean, high contrast, structured) ───────────────── */
.table-wrapper {
    background: var(--bg-card);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    overflow: hidden;
    box-shadow: var(--shadow-card);
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.data-table thead {
    background: var(--bg-table-header);
    position: sticky;
    top: 0;
    z-index: 5;
}

.data-table th {
    padding: 10px 14px;
    text-align: left;
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-bottom: 2px solid var(--border-default);
    white-space: nowrap;
}

.data-table td {
    padding: 8px 14px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
    color: var(--text-primary);
    vertical-align: middle;
    font-family: 'Roboto Mono', monospace;
    font-size: 12px;
}

.data-table td.text-label {
    font-family: 'Inter', sans-serif;
    font-size: 13px;
}

.data-table tbody tr { transition: var(--transition); }
.data-table tbody tr:hover { background: rgba(0, 0, 0, 0.04); }
.data-table tbody tr:nth-child(even) { background: var(--bg-table-alt); }
.data-table tbody tr:nth-child(even):hover { background: rgba(0, 0, 0, 0.04); }

/* ─── ISA Status Indicators (Shape + Color + Text — redundant coding) ──── */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 8px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: 600;
    font-family: 'Inter', sans-serif;
    white-space: nowrap;
}

/* ▲ Triangle shape for FAIL — red */
.badge-fail {
    background: var(--status-fail-bg);
    color: var(--status-fail);
    border: 1px solid rgba(204, 0, 0, 0.3);
}

.badge-fail::before {
    content: '▲';
    font-size: 8px;
}

/* ◆ Diamond shape for WARNING — amber */
.badge-warning {
    background: var(--status-warning-bg);
    color: var(--status-warning);
    border: 1px solid rgba(204, 153, 0, 0.3);
}

.badge-warning::before {
    content: '◆';
    font-size: 8px;
}

/* ● Circle shape for PASS — green */
.badge-pass {
    background: var(--status-pass-bg);
    color: var(--status-pass);
    border: 1px solid rgba(46, 125, 50, 0.3);
}

.badge-pass::before {
    content: '●';
    font-size: 8px;
}

/* ○ Circle for INFO */
.badge-info {
    background: var(--status-info-bg);
    color: var(--status-info);
    border: 1px solid rgba(0, 119, 179, 0.3);
}

.badge-info::before {
    content: '○';
    font-size: 8px;
}

/* ─ Dash for NEUTRAL / no data */
.badge-neutral {
    background: rgba(138, 138, 138, 0.1);
    color: var(--text-muted);
    border: 1px solid rgba(138, 138, 138, 0.25);
}

.badge-neutral::before {
    content: '─';
    font-size: 8px;
}

/* ─── Detail Panel ──────────────────────────────────────────────────────── */
.detail-panel {
    background: var(--bg-card);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    padding: 20px;
    box-shadow: var(--shadow-card);
}

.detail-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 16px;
    flex-wrap: wrap;
    gap: 12px;
}

.detail-title {
    font-size: 16px;
    font-weight: 700;
}

.detail-meta {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 10px;
    margin-bottom: 20px;
    padding: 14px;
    background: var(--bg-secondary);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-default);
}

.meta-item {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.meta-label {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
}

.meta-value {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-primary);
    font-family: 'Roboto Mono', monospace;
}

.detail-subsection {
    margin-top: 20px;
}

.detail-subsection h3 {
    font-size: 13px;
    font-weight: 700;
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 6px;
}

/* ─── Terminal Tabs ─────────────────────────────────────────────────────── */
.terminal-tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}

.terminal-tab {
    padding: 6px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-default);
    background: transparent;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
}

.terminal-tab:hover {
    background: rgba(0, 0, 0, 0.04);
    color: var(--text-primary);
}

.terminal-tab.active {
    background: #4a6e8a;
    color: #fff;
    border-color: #3a5e7a;
}

/* ─── Chart Container ───────────────────────────────────────────────────── */
.chart-container {
    background: var(--bg-secondary);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-default);
    padding: 14px;
    position: relative;
    height: 300px;
}

/* ─── Recommendation Cards (ISA: structured, color-on-left-edge only) ──── */
.recommendations-grid {
    display: grid;
    gap: 8px;
}

.recommendation-card {
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-default);
    border-left: 4px solid;
    background: var(--bg-secondary);
}

.recommendation-card.rec-pass { border-left-color: var(--status-pass); }
.recommendation-card.rec-fail { border-left-color: var(--status-fail); }
.recommendation-card.rec-warning { border-left-color: var(--status-warning); }
.recommendation-card.rec-info { border-left-color: var(--status-info); }

.rec-terminal {
    font-weight: 700;
    font-size: 13px;
    margin-bottom: 4px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.rec-message {
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* ─── Modal ─────────────────────────────────────────────────────────────── */
.modal-overlay {
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.4);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal-content {
    background: var(--bg-card);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    padding: 24px;
    width: 100%;
    max-width: 520px;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

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

.modal-close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 20px;
    cursor: pointer;
    padding: 2px;
    transition: var(--transition);
    line-height: 1;
}

.modal-close:hover { color: var(--text-primary); }

.modal-divider {
    border: none;
    border-top: 1px solid var(--border-default);
    margin: 14px 0;
}

/* ─── Cell status highlighting (subtle background shift) ────────────────── */
.cell-pass { background: rgba(46, 125, 50, 0.06) !important; }
.cell-fail { background: rgba(204, 0, 0, 0.06) !important; }
.cell-warning { background: rgba(204, 153, 0, 0.06) !important; }

/* ─── Latest Data Panel ─────────────────────────────────────────────────── */
.latest-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.latest-timestamp {
    font-size: 11px;
    color: var(--text-muted);
    font-family: 'Roboto Mono', monospace;
}

/* ─── Utilities ─────────────────────────────────────────────────────────── */
.hidden { display: none !important; }
.text-pass { color: var(--status-pass); }
.text-fail { color: var(--status-fail); }
.text-warning { color: var(--status-warning); }
.text-muted { color: var(--text-muted); }
.mt-2 { margin-top: 8px; }
.mt-4 { margin-top: 16px; }
.mb-2 { margin-bottom: 8px; }
.mb-4 { margin-bottom: 16px; }
.flex { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-2 { gap: 8px; }
.gap-3 { gap: 12px; }

/* ─── Responsive ────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
    .dashboard-header {
        padding: 10px 14px;
        flex-direction: column;
        gap: 8px;
    }
    .dashboard-main { padding: 14px; }
    .kpi-grid { grid-template-columns: repeat(2, 1fr); }
    .detail-meta { grid-template-columns: 1fr; }
    .header-actions { width: 100%; justify-content: flex-end; }
    #map { height: 280px; }
    .login-card { padding: 28px 20px; }
    .modal-content { padding: 18px; }
    .data-table { font-size: 11px; }
    .data-table th, .data-table td { padding: 6px 8px; }
}

@media (max-width: 480px) {
    .kpi-grid { grid-template-columns: 1fr; }
}
