:root {
    --bg: #f8f9fa;
    --text: #212529;
    --border: #dee2e6;
    --primary: #0d6efd;
    --primary-hover: #0b5ed7;
    --danger: #dc3545;
    --secondary: #6c757d;
    --row-hover: #f1f3f5;
    --row-selected: #e7f1ff;
    
    --tag-item: rgba(220, 30, 30, 0.4);
    --tag-flg: rgba(30, 90, 220, 0.4);
    --tag-gas: rgba(0, 150, 60, 0.4);
    --tag-blt: rgba(160, 60, 200, 0.4);
    --tag-s: rgba(230, 130, 0, 0.4);
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Login */
.login-page {
    justify-content: center;
    align-items: center;
    background: #e9ecef;
}
.login-card {
    background: white;
    padding: 2rem;
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    width: 100%;
    max-width: 400px;
}
.login-card form div {
    margin-bottom: 1rem;
}
.login-card label {
    display: block;
    margin-bottom: 0.5rem;
}
.login-card input {
    width: 100%;
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    box-sizing: border-box;
}
.login-card button {
    width: 100%;
    padding: 0.75rem;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}
.error-msg {
    color: var(--danger);
    margin-top: 1rem;
    font-size: 0.9rem;
}

/* Main App */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    background: white;
    border-bottom: 1px solid var(--border);
}
.app-header h1 {
    margin: 0;
    font-size: 1.25rem;
    margin-right: 1.5rem;
}
.header-left, .header-right {
    display: flex;
    align-items: center;
    gap: 1rem;
}
.btn {
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: 500;
}
.btn.primary { background: var(--primary); color: white; }
.btn.secondary { background: var(--secondary); color: white; }
.btn.danger { background: var(--danger); color: white; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; pointer-events: none; }
input[type="checkbox"]:disabled { cursor: not-allowed; opacity: 0.45; }

.btn.done {
    background: #198754 !important;
    color: white;
    position: relative;
}
.btn.done::after {
    content: ' ✓';
    font-weight: 700;
}

.status-processing::before {
    content: '';
    display: inline-block;
    width: 10px; height: 10px;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    margin-right: 6px;
    vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}
.pane-left {
    width: 60%;
    min-width: 200px;
    max-width: 85%;
    display: flex;
    flex-direction: column;
    background: #e9ecef;
}

#pane-divider {
    width: 6px;
    cursor: col-resize;
    background: var(--border);
    flex-shrink: 0;
    transition: background 0.15s;
    position: relative;
    user-select: none;
}
#pane-divider:hover, #pane-divider.dragging { background: var(--primary); }
#pane-divider::after {
    content: '⋮⋮';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: #aaa;
    font-size: 14px;
    pointer-events: none;
}
.pdf-toolbar {
    padding: 0.5rem;
    background: white;
    border-bottom: 1px solid var(--border);
    display: flex;
    gap: 0.5rem;
    align-items: center;
}
#pdf-container {
    flex: 1;
    overflow: auto;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 1rem;
    cursor: grab;
}
#pdf-container.panning {
    cursor: grabbing;
    user-select: none;
}
#pdf-canvas {
    box-shadow: 0 2px 10px rgba(0,0,0,0.2);
}
#pdf-overlay {
    position: absolute;
    top: 1rem; /* matches padding of container */
    transform-origin: top left;
}

/* Tags on PDF */
.tag-box {
    position: absolute;
    box-sizing: border-box;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s;
    border: 2px solid transparent;
}
.tag-box.show-all {
    opacity: 0.3;
}
.tag-box.highlighted {
    opacity: 1 !important;
    border-width: 3px;
    z-index: 10;
    animation: pulse 1.5s infinite;
}
@keyframes pulse {
    0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(0,0,0,0.4); }
    70% { transform: scale(1.05); box-shadow: 0 0 0 10px rgba(0,0,0,0); }
    100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(0,0,0,0); }
}

.pane-right {
    flex: 1;
    min-width: 180px;
    display: flex;
    flex-direction: column;
    background: white;
    overflow: hidden;
}
.warnings-box {
    padding: 0.5rem 1rem;
    background: #fff3cd;
    color: #856404;
    border-bottom: 1px solid #ffeeba;
    font-size: 0.9rem;
    max-height: 100px;
    overflow-y: auto;
}
.table-container {
    flex: 1;
    overflow: auto;
}
table {
    width: 100%;
    border-collapse: collapse;
}
th, td {
    padding: 0.75rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
}
th {
    background: #f8f9fa;
    position: sticky;
    top: 0;
    z-index: 5;
}
tr {
    cursor: pointer;
}
tr:hover {
    background: var(--row-hover);
}
tr.selected {
    background: var(--row-selected);
}
.empty-state {
    text-align: center;
    color: var(--secondary);
    padding: 3rem !important;
}
.qty-cell {
    position: relative;
}
.qty-cell:hover::after {
    content: attr(title);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    white-space: nowrap;
    z-index: 20;
    font-size: 0.8rem;
}

/* Modal */
.modal {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.8);
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
}
.modal-content {
    background: white;
    padding: 1rem;
    border-radius: 8px;
    max-width: 90%;
    max-height: 90%;
    overflow: auto;
    position: relative;
}
.close-modal {
    position: absolute;
    top: 0.5rem; right: 1rem;
    font-size: 2rem;
    cursor: pointer;
}

/* ================= Part B: pipe length ================= */
.qty-cell.pending { color: var(--secondary); font-style: italic; }
.qty-cell.length-done { font-weight: 600; color: #0a7d32; }

/* dimension boxes on the drawing (hidden unless "Show dimensions" or highlighted) */
.dim-box {
    position: absolute;
    box-sizing: border-box;
    border: 2px solid transparent;
    opacity: 0;
    pointer-events: none;          /* never block clicks on tag boxes underneath */
    transition: opacity 0.2s;
}
.dim-box.show-all { opacity: 0.8; pointer-events: auto; }   /* hover shows "D21 = 2692 mm · pipe" */
.dim-box.role-pipe { border-color: rgba(0, 160, 60, 0.9); }
.dim-box.role-fitting { border-color: rgba(110, 110, 110, 0.9); }
.dim-box.role-reference { border-color: rgba(240, 140, 0, 0.9); }
.dim-box.role-tag { border-color: rgba(150, 60, 200, 0.9); }
.dim-box.role-unassigned { border-color: rgba(220, 0, 0, 0.9); }
.dim-box.highlighted {
    opacity: 1 !important;
    border-width: 3px;
    background: rgba(0, 160, 60, 0.15);
    z-index: 10;
    animation: pulse 1.5s infinite;
}

/* Gemini's route points and stretches */
.route-svg {
    position: absolute;
    left: 0; top: 0; width: 100%; height: 100%;
    pointer-events: none;
    display: none;
}
.route-svg.show-all { display: block; }
.route-svg polyline { fill: none; stroke: rgba(230, 0, 200, 0.8); stroke-width: 2; vector-effect: non-scaling-stroke; }
.route-svg circle { fill: rgba(230, 0, 200, 0.9); }

/* details panel under the BOM table */
.length-panel {
    border-top: 2px solid var(--border);
    padding: 0.5rem 1rem 1rem;
    max-height: 45%;
    overflow: auto;
    font-size: 0.85rem;
}
.length-status { padding: 0.5rem; border-radius: 4px; margin-bottom: 0.5rem; font-weight: 600; }
.length-status.pass { background: #d1e7dd; color: #0f5132; }
.length-status.fail { background: #f8d7da; color: #842029; }
.length-status.pending { background: #e2e3e5; color: #41464b; font-weight: 400; font-style: italic; }
.length-decision { margin-bottom: 0.5rem; }
.length-list { margin: 0.25rem 0 0.5rem; padding-left: 1.2rem; }
.checks { margin-bottom: 0.5rem; display: flex; flex-wrap: wrap; gap: 0.25rem; }
.check { padding: 0.1rem 0.4rem; border-radius: 3px; font-size: 0.75rem; }
.check.pass { background: #d1e7dd; }
.check.fail { background: #f8d7da; }
.check.skipped { background: #e2e3e5; }
.length-panel details { margin: 0.25rem 0; }
.length-panel summary { cursor: pointer; font-weight: 600; }
.mini-table th, .mini-table td { padding: 0.25rem 0.4rem; font-size: 0.78rem; }
.mini-table tr { cursor: default; }
.muted { color: var(--secondary); }

/* ================= Spool-only mode ================= */
.spool-panel h3 { margin: 0.25rem 0 0.5rem; font-size: 1rem; }
.spool-panel.spool-full { max-height: none; flex: 1; border-top: none; }
.spool-table .spool-row { cursor: pointer; }
.spool-table .spool-row.selected { background: var(--row-selected); }
.spool-dim {
    position: absolute;
    box-sizing: border-box;
    border: 2px solid rgba(0, 160, 60, 0.6);
    pointer-events: none;
}
.spool-dim.highlighted {
    border-width: 3px;
    border-color: rgba(0, 160, 60, 1);
    background: rgba(0, 160, 60, 0.18);
    z-index: 10;
    animation: pulse 1.5s infinite;
}

/* ================= Spool-only: route sequence ================= */
.spool-dim.fitting { border-color: rgba(110, 110, 110, 0.6); }
.spool-dim.fitting.highlighted { border-color: rgba(60, 60, 60, 1); background: rgba(110, 110, 110, 0.18); }
.seq-badge {
    width: auto !important; height: auto !important;
    border: none !important;
    transform: translate(6px, -120%);
    font: 700 11px/1 sans-serif;
    color: #14289f;
    background: rgba(255, 255, 255, 0.85);
    padding: 1px 3px;
    border-radius: 3px;
    white-space: nowrap;
}
.seq-badge.highlighted { background: #14289f; color: #fff; animation: pulse 1.5s infinite; }
.spool-table .seq-spool td:nth-child(2) { font-weight: 600; }
.spool-table .seq-gap td { background: #fff3cd; }
.seq-warn { color: #842029; }

/* spool table */
.spool-head { display: flex; justify-content: space-between; align-items: center; gap: 0.5rem; }
.spool-head .btn { padding: 0.2rem 0.6rem; font-size: 0.78rem; text-decoration: none; }
.spool-sup { position: absolute; border: 2px solid rgba(0, 140, 70, 0.55); border-radius: 2px; pointer-events: auto; }
.spool-sup.unplaced { border-color: rgba(200, 30, 30, 0.8); border-style: dashed; }
.spool-sup.highlighted { border-color: rgba(0, 120, 50, 1); background: rgba(0, 160, 80, 0.18); }
.check.info { background: #e7f1ff; color: #084298; }
.dim-breakdown { font-size: 0.88em; color: #495057; white-space: nowrap; }

.src-ok { color: #198754; font-weight: 600; }
.src-info { color: #0d6efd; cursor: help; }
