:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --secondary: #64748b;
    --bg: #f1f5f9;
    --surface: #ffffff;
    --border: #e2e8f0;
    --text: #1e293b;
    --muted: #64748b;
    --danger: #dc2626;
    --success: #16a34a;
    --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 1px 2px rgba(15, 23, 42, .04);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
}

.container { max-width: 1280px; margin: 0 auto; padding: 16px; }

header { margin-bottom: 18px; }

header h1 {
    font-size: 20px;
    margin: 0 0 12px;
    color: var(--text);
}

.nav-tabs {
    display: flex;
    gap: 6px;
    border-bottom: 2px solid var(--border);
    flex-wrap: wrap;
}

.nav-btn {
    background: none;
    border: none;
    padding: 10px 16px;
    cursor: pointer;
    font-size: 14px;
    color: var(--muted);
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: color .15s, border-color .15s;
}

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

.nav-btn.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    font-weight: 600;
}

/* ── Query section ──────────────────────────────── */
.query-section { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 18px; box-shadow: var(--shadow); }

.query-section h2 { margin: 0 0 14px; font-size: 16px; }

.filter-form {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 10px 14px;
    margin-bottom: 14px;
}

.filter-item { display: flex; flex-direction: column; gap: 4px; }

.filter-item label { font-size: 12px; color: var(--muted); font-weight: 500; }

.filter-item input[type="text"],
.filter-item select {
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    background: #fff;
    color: var(--text);
}

.filter-item input[type="text"]:focus { outline: none; border-color: var(--primary); }

.filter-item.checkbox-inline { flex-direction: row; align-items: center; gap: 8px; }
.filter-item.checkbox-inline label { font-size: 14px; color: var(--text); }

.query-actions { display: flex; align-items: center; gap: 14px; margin-bottom: 8px; }

.result-count { color: var(--muted); font-size: 13px; }

/* ── Buttons ─────────────────────────────────────── */
.btn-primary {
    background: var(--primary);
    color: #fff;
    border: none;
    padding: 9px 18px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-primary:disabled { opacity: .6; cursor: default; }

.search-tabs { display: flex; gap: 6px; margin: 6px 0 12px; }
.tab-btn {
    background: #f8fafc;
    border: 1px solid var(--border);
    padding: 7px 14px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    color: var(--muted);
}
.tab-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ── Table ───────────────────────────────────────── */
.table-wrap {
    overflow: auto;                 /* horizontal + vertical scrollbar inside the table area */
    max-height: calc(100vh - 220px); /* cap height so long result sets scroll within the panel, not the whole page */
    margin-top: 10px;
}

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

table.data-table th, table.data-table td {
    text-align: left;
    padding: 9px 12px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

table.data-table th {
    background: #f8fafc;
    color: var(--muted);
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 2;
}

table.data-table tbody tr { cursor: pointer; }
table.data-table tbody tr:hover { background: #f1f5f9; }
table.data-table tbody tr:nth-child(even) { background: #fcfdff; }
table.data-table tbody tr:nth-child(even):hover { background: #f1f5f9; }

.cell-id { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--secondary); }
/* Datasource identity = the ValVue installation id string. Always rendered IN FULL
   (never truncated) so two stations can be told apart; wraps instead of forcing a
   very wide column. Overrides the table-wide white-space: nowrap. */
.cell-mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--secondary); white-space: normal; word-break: break-all; min-width: 180px; max-width: 360px; }
.val-mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; word-break: break-all; }

/* ── Messages ────────────────────────────────────── */
.error-message {
    background: #fef2f2;
    color: var(--danger);
    border: 1px solid #fecaca;
    padding: 10px 14px;
    border-radius: 6px;
    margin: 10px 0;
}
.loading { padding: 24px; text-align: center; color: var(--muted); }
.no-data { padding: 24px; text-align: center; color: var(--muted); }

.hidden { display: none !important; }

/* ── Modal ───────────────────────────────────────── */
.modal {
    position: fixed; inset: 0;
    background: rgba(15, 23, 42, .45);
    display: flex; align-items: center; justify-content: center;
    z-index: 1000; padding: 20px;
}
.modal-content {
    background: #fff; border-radius: 10px; max-width: 760px; width: 100%;
    max-height: 88vh; overflow: auto; box-shadow: 0 10px 40px rgba(0,0,0,.25);
}
.modal-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 16px; }
.close-btn { background: none; border: none; font-size: 24px; cursor: pointer; color: var(--muted); line-height: 1; }
.modal-body { padding: 18px; }

.detail-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 10px 18px;
}
.detail-item { display: flex; flex-direction: column; gap: 2px; border-bottom: 1px dashed var(--border); padding-bottom: 6px; }
.detail-item .lbl { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.detail-item .val { font-size: 13px; word-break: break-word; }
.detail-section { margin-top: 16px; }
.detail-section h3 { font-size: 14px; margin: 0 0 8px; color: var(--text); }
.xml-code {
    background: #0f172a; color: #e2e8f0; padding: 12px; border-radius: 6px;
    overflow: auto; max-height: 360px; font-size: 12px; line-height: 1.5;
    white-space: pre; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

/* Bulk actions (devices page multi-select export/import) */
.bulk-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    margin: 10px 0;
    padding: 10px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: var(--shadow);
}
.btn-secondary {
    background: var(--secondary);
    color: #fff;
    border: none;
    border-radius: 6px;
    padding: 7px 14px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s ease;
}
.btn-secondary:hover { background: #475569; }
.bulk-status {
    font-size: 13px;
    color: var(--success);
    margin-left: auto;
    font-weight: 600;
}
.bulk-status.error { color: var(--danger); }
/* Export / Import panel (valveSn / positionerSn / positionerId only) */
.io-panel {
    margin: 10px 0;
    padding: 12px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 3px solid var(--primary);
    border-radius: 8px;
    box-shadow: var(--shadow);
}
.io-head {
    font-size: 13px;
    font-weight: 700;
    color: var(--text);
    margin-bottom: 10px;
    letter-spacing: .2px;
}
.io-row {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    flex-wrap: wrap;
    margin-bottom: 8px;
}
.io-row:last-child { margin-bottom: 0; align-items: center; }
.io-field { display: flex; flex-direction: column; gap: 4px; }
.io-field span { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); font-weight: 600; }
.io-field input {
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 13px;
    min-width: 210px;
    background: #fff;
    color: var(--text);
}
.io-field input:focus { outline: 2px solid var(--primary); outline-offset: 1px; border-color: var(--primary); }
.io-hint { font-size: 12px; color: var(--muted); }
.io-panel .bulk-status { margin-left: 0; }
/* Per-row export button on the devices table (one device, one Positioner ID) */
.row-export-btn {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--primary);
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
}
.row-export-btn:hover { background: #f1f5f9; border-color: var(--primary); }

.cell-select { width: 34px; text-align: center; }
.cell-select-h { width: 34px; }
.cell-select input { cursor: pointer; }

/* ── Pagination ───────────────────────────────────── */
.pagination {
    display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
    margin-top: 12px; padding: 10px 2px;
}
.pagination .page-info { color: var(--muted); font-size: 13px; }
.pagination .page-controls { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; }
.page-btn {
    background: #fff; border: 1px solid var(--border); color: var(--text);
    padding: 6px 10px; border-radius: 6px; cursor: pointer; font-size: 13px; min-width: 34px;
}
.page-btn:hover:not(:disabled) { background: #f1f5f9; }
.page-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); font-weight: 600; }
.page-btn:disabled { opacity: .5; cursor: default; }
.page-ellipsis { color: var(--muted); padding: 0 4px; }
.page-size { margin-left: auto; color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 6px; }
.page-size select { padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; background: #fff; color: var(--text); }

/* ── Label context snapshots (frozen at label-save time) ──────── */
.ctx-chips { display: flex; gap: 3px; }

.ctx-chip {
    display: inline-flex; align-items: center; justify-content: center;
    width: 18px; height: 18px; border-radius: 4px;
    font-size: 10px; font-weight: 700; line-height: 1;
    background: #e2e8f0; color: #94a3b8; border: 1px solid var(--border);
    cursor: default;
}

.ctx-chip.on { background: #dcfce7; color: #15803d; border-color: #86efac; }

.snap-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 10px;
    margin-top: 6px;
}

.snap-panel {
    border: 1px solid var(--border); border-radius: 6px;
    padding: 8px 10px; background: #f8fafc;
}

.snap-title {
    font-size: 11px; font-weight: 600; color: var(--muted);
    text-transform: uppercase; letter-spacing: .03em;
    margin-bottom: 6px; padding-bottom: 4px;
    border-bottom: 1px solid var(--border);
}

.snap-kv > div { font-size: 12px; line-height: 1.7; word-break: break-word; }
.snap-kv .k { color: var(--muted); }
.snap-kv .v { color: var(--text); }
.snap-dash { color: #cbd5e1; }

.snap-empty { font-size: 12px; color: var(--muted); font-style: italic; }

.snap-rec {
    font-size: 12px; line-height: 1.5;
    padding: 6px 0; border-bottom: 1px dashed var(--border);
    word-break: break-word;
}

.snap-rec:last-child { border-bottom: none; padding-bottom: 0; }

.snap-rec-type {
    display: inline-block; margin-right: 6px;
    padding: 1px 6px; border-radius: 10px;
    background: #dbeafe; color: var(--primary-dark);
    font-size: 10px; font-weight: 700;
    text-transform: uppercase; letter-spacing: .03em;
}

.snap-rec-meta { margin-top: 3px; font-size: 11px; color: var(--muted); }

.snap-crumb { font-size: 12px; line-height: 1.8; word-break: break-word; }
.snap-crumb .sep { margin: 0 5px; color: #cbd5e1; }
.snap-crumb .nt { font-size: 10px; color: var(--muted); }

.btn-danger {
    background: #e0483d;
    color: #fff;
    border: none;
    border-radius: 6px;
    padding: 7px 14px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s ease;
}
.btn-danger:hover { filter: brightness(0.95); }
