:root{
  --bg:#0E1116;
  --bg-raised:#161B22;
  --bg-elevated:#1C232C;
  --line:#262E38;
  --line-soft:#1C232C;
  --text:#E8EAED;
  --text-dim:#8B95A1;
  --text-faint:#5B6472;
  --accent:#FF7A33;
  --accent-dim:#9C4D20;
  --accent-glow:rgba(255,122,51,0.18);
  --good:#3DDC97;
  --good-dim:rgba(61,220,151,0.14);
  --bad:#FF5D5D;
  --bad-dim:rgba(255,93,93,0.14);
  --radius:14px;
  --radius-sm:8px;
  --mono:'JetBrains Mono', monospace;
  --sans:'Inter', -apple-system, sans-serif;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*{
  margin:0; padding:0; box-sizing:border-box;
  -webkit-tap-highlight-color: transparent;
  touch-action: pan-x pan-y;
  -webkit-user-select:none; user-select:none;
}

html,body{
  height:100%; overflow:hidden;
  background:var(--bg);
  color:var(--text);
  font-family:var(--sans);
  -webkit-font-smoothing:antialiased;
}

#app{
  position:fixed; inset:0;
  display:flex; flex-direction:column;
}

/* ====== TOP BAR ====== */
#topbar{
  display:flex; align-items:center; gap:10px;
  padding:14px 16px 10px;
  background:linear-gradient(180deg, var(--bg) 60%, transparent);
  position:relative; z-index:5;
  flex-shrink:0;
}
#topbar .brand{
  display:flex; align-items:center; gap:8px;
  flex:1; min-width:0;
}
#topbar .brand-icon{
  width:30px; height:30px; flex-shrink:0;
  display:flex; align-items:center; justify-content:center;
  background:var(--bg-elevated);
  border:1px solid var(--line);
  border-radius:8px;
}
#topbar .brand-icon svg{ width:16px; height:16px; }
#fileTitle{
  font-family:var(--mono);
  font-size:13px; font-weight:500;
  white-space:nowrap; overflow:hidden; text-overflow:ellipsis;
  color:var(--text);
  letter-spacing:-0.01em;
}
#fileSub{
  font-size:10.5px; color:var(--text-faint);
  white-space:nowrap;
  margin-top:1px;
}
#statusDot{
  width:7px; height:7px; border-radius:50%;
  background:var(--text-faint);
  flex-shrink:0;
  transition:background .3s;
}
#statusDot.loading{ background:var(--accent); animation:pulse 1.1s infinite; }
#statusDot.ready{ background:var(--good); }
#statusDot.error{ background:var(--bad); }
@keyframes pulse{
  0%,100%{ box-shadow:0 0 0 0 var(--accent-glow); }
  50%{ box-shadow:0 0 0 5px transparent; }
}

/* ====== VIEWPORT ====== */
#viewport{
  flex:1; position:relative;
  min-height:0;
  background:
    radial-gradient(ellipse 80% 60% at 50% 35%, rgba(255,122,51,0.05), transparent 70%),
    var(--bg);
  transition:background .3s ease;
}

/* Och (yorug') fon varianti */
#viewport.light-bg{
  background:
    radial-gradient(ellipse 80% 60% at 50% 35%, rgba(255,122,51,0.07), transparent 70%),
    #EAEDF1;
}
#viewport.light-bg .corner-mark svg{ stroke:#A4ACB8; }
#viewport.light-bg #countBadge{
  background:rgba(255,255,255,0.85);
  border-color:rgba(0,0,0,0.08);
  color:#5B6472;
}
#viewport.light-bg #countBadge b{ color:#1C232C; }
#viewport.light-bg .vctrl-btn{
  background:rgba(255,255,255,0.85);
  border-color:rgba(0,0,0,0.08);
  color:#5B6472;
}
#viewport.light-bg .vctrl-btn:active, #viewport.light-bg .vctrl-btn.active{
  background:var(--accent); color:#1a0e06; border-color:var(--accent);
}
#viewport.light-bg #selTag{
  background:#fff;
  box-shadow:0 8px 24px rgba(0,0,0,0.12), 0 0 20px rgba(255,122,51,0.25);
}
#viewport.light-bg #selTag .name{ color:#1C232C; }
#viewport.light-bg .dim-label{
  background:rgba(255,255,255,0.9);
  border-color:rgba(0,0,0,0.1);
  color:#1C232C;
}
#bgToggleBtn.active{ background:var(--accent); color:#1a0e06; border-color:var(--accent); }

#container{
  position:absolute; inset:0;
  touch-action:none;
}
#container canvas{ display:block; }

.corner-mark{
  position:absolute; width:18px; height:18px;
  pointer-events:none; opacity:0.35; z-index:2;
}
.corner-mark svg{ width:100%; height:100%; stroke:var(--text-faint); }
.cm-tl{ top:10px; left:10px; }
.cm-tr{ top:10px; right:10px; transform:scaleX(-1); }
.cm-bl{ bottom:10px; left:10px; transform:scaleY(-1); }
.cm-br{ bottom:10px; right:10px; transform:scale(-1,-1); }

#loadOverlay{
  position:absolute; inset:0;
  display:flex; flex-direction:column; align-items:center; justify-content:center;
  gap:14px; z-index:10;
  background:var(--bg);
  transition:opacity .4s ease;
}
#loadOverlay.hidden{ opacity:0; pointer-events:none; }
.spinner{
  width:34px; height:34px;
  border:2px solid var(--line);
  border-top-color:var(--accent);
  border-radius:50%;
  animation:spin 0.8s linear infinite;
}
@keyframes spin{ to{ transform:rotate(360deg); } }
#loadText{
  font-family:var(--mono); font-size:12px;
  color:var(--text-dim); letter-spacing:0.02em;
  text-align:center; padding:0 30px;
}
#loadError{
  display:none;
  font-family:var(--mono); font-size:11.5px;
  color:var(--bad); text-align:center; padding:0 30px;
  max-width:320px; line-height:1.5;
}

#countBadge{
  position:absolute; top:14px; left:50%; transform:translateX(-50%);
  background:rgba(22,27,34,0.88);
  backdrop-filter:blur(8px);
  border:1px solid var(--line);
  border-radius:100px;
  padding:6px 14px;
  font-family:var(--mono); font-size:11px; color:var(--text-dim);
  display:flex; align-items:center; gap:6px;
  z-index:4;
  opacity:0; transition:opacity .3s;
}
#countBadge.show{ opacity:1; }
#countBadge b{ color:var(--text); font-weight:600; }

#viewControls{
  position:absolute; right:12px; top:50%; transform:translateY(-50%);
  display:flex; flex-direction:column; gap:8px;
  z-index:4;
}
.vctrl-btn{
  width:38px; height:38px;
  background:rgba(22,27,34,0.88);
  backdrop-filter:blur(8px);
  border:1px solid var(--line);
  border-radius:10px;
  display:flex; align-items:center; justify-content:center;
  color:var(--text-dim);
  cursor:pointer;
  transition:background .15s, color .15s, border-color .15s;
}
.vctrl-btn svg{ width:18px; height:18px; }
.vctrl-btn:active, .vctrl-btn.active{
  background:var(--accent); color:#1a0e06; border-color:var(--accent);
}
.preset-btn{
  font-size:10px; font-weight:600; letter-spacing:.03em;
  font-family:var(--mono);
}
.vctrl-sep{
  height:1px; background:var(--line); margin:2px 4px;
}

#axisIndicator{
  position:absolute; left:12px; bottom:12px;
  width:54px; height:54px;
  z-index:4; opacity:0.85;
}

#selTag{
  position:absolute; left:50%; bottom:16px; transform:translateX(-50%) translateY(8px);
  background:var(--bg-elevated);
  border:1px solid var(--accent);
  border-radius:var(--radius-sm);
  padding:8px 12px;
  font-family:var(--mono);
  display:flex; align-items:center; gap:10px;
  z-index:4; opacity:0; pointer-events:none;
  transition:opacity .25s, transform .25s;
  box-shadow:0 8px 24px rgba(0,0,0,0.4), 0 0 20px rgba(255,122,51,0.35);
  max-width:90%;
}
#selTag.show{ opacity:1; transform:translateX(-50%) translateY(0); pointer-events:auto; animation: selGlow 1.8s ease-in-out infinite; }
@keyframes selGlow {
  0%,100%{ box-shadow:0 8px 24px rgba(0,0,0,0.4), 0 0 12px rgba(255,122,51,0.2); }
  50%{ box-shadow:0 8px 24px rgba(0,0,0,0.4), 0 0 28px rgba(255,122,51,0.55); }
}
#selTag .dot{ width:8px; height:8px; border-radius:2px; background:var(--accent); flex-shrink:0; }
#selTag .name{ font-size:12.5px; font-weight:600; color:var(--text); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
#selTag .dims{ font-size:11px; color:var(--text-dim); white-space:nowrap; }

/* ====== BOTTOM SHEET ====== */
#sheet{
  background:var(--bg-raised);
  border-top:1px solid var(--line);
  border-radius:18px 18px 0 0;
  position:relative;
  flex-shrink:0;
  display:flex; flex-direction:column;
  max-height:62vh;
  transition:max-height .35s cubic-bezier(.32,.72,0,1);
  z-index:6;
  box-shadow:0 -8px 30px rgba(0,0,0,0.35);
}
#sheet.collapsed{ max-height:128px; }

#sheetHandle{
  display:flex; flex-direction:column; align-items:center;
  padding:9px 0 6px; cursor:pointer; flex-shrink:0;
}
#sheetHandle .bar{
  width:36px; height:4px; border-radius:3px;
  background:var(--line);
}

#sheetBody{
  flex:1; overflow-y:auto; overflow-x:hidden;
  padding:0 16px calc(14px + var(--safe-bottom));
  -webkit-overflow-scrolling:touch;
}
#sheetBody::-webkit-scrollbar{ width:4px; }
#sheetBody::-webkit-scrollbar-thumb{ background:var(--line); border-radius:2px; }

.sheet-section{ margin-bottom:16px; }
.sheet-label{
  font-family:var(--mono); font-size:10.5px; font-weight:600;
  color:var(--text-faint); letter-spacing:0.08em; text-transform:uppercase;
  margin-bottom:8px; display:flex; align-items:center; justify-content:space-between;
}

#searchRow{ display:flex; gap:8px; }
#searchInput{
  flex:1; background:var(--bg-elevated);
  border:1px solid var(--line);
  border-radius:var(--radius-sm);
  padding:11px 12px;
  color:var(--text);
  font-family:var(--sans); font-size:14px;
  outline:none;
  transition:border-color .15s;
}
#searchInput:focus{ border-color:var(--accent-dim); }
#searchInput::placeholder{ color:var(--text-faint); }
#scanBtn{
  width:44px; flex-shrink:0;
  background:var(--bg-elevated);
  border:1px solid var(--line);
  border-radius:var(--radius-sm);
  color:var(--text-dim);
  display:flex; align-items:center; justify-content:center;
  cursor:pointer;
}
#scanBtn svg{ width:19px; height:19px; }
#scanBtn:active{ background:var(--accent); color:#1a0e06; border-color:var(--accent); }

#filterChips{ display:flex; gap:6px; overflow-x:auto; padding-bottom:2px; margin-top:10px; }
#filterChips::-webkit-scrollbar{ display:none; }
.chip{
  flex-shrink:0;
  font-family:var(--mono); font-size:11px; font-weight:500;
  padding:6px 11px; border-radius:100px;
  background:var(--bg-elevated);
  border:1px solid var(--line);
  color:var(--text-dim);
  cursor:pointer; white-space:nowrap;
  display:flex; align-items:center; gap:5px;
}
.chip .n{ opacity:0.6; }
.chip.active{ background:var(--accent-glow); border-color:var(--accent-dim); color:var(--accent); }

#hlToggle{ display:flex; gap:6px; }
.hl-opt{
  flex:1; text-align:center;
  font-size:12px; font-weight:500;
  padding:8px; border-radius:var(--radius-sm);
  background:var(--bg-elevated);
  border:1px solid var(--line);
  color:var(--text-dim);
  cursor:pointer;
}
.hl-opt.active{ background:rgba(255,122,51,0.12); border-color:var(--accent-dim); color:var(--accent); }

.part-row{
  display:flex; align-items:center; gap:10px;
  padding:10px 0;
  border-bottom:1px solid var(--line-soft);
  cursor:pointer;
}
.part-row:last-child{ border-bottom:none; }
.part-row .swatch{
  width:30px; height:30px; border-radius:6px; flex-shrink:0;
  border:1px solid var(--line);
}
.part-row .info{ flex:1; min-width:0; }
.part-row .pname{
  font-family:var(--mono); font-size:12.5px; font-weight:600; color:var(--text);
  white-space:nowrap; overflow:hidden; text-overflow:ellipsis;
}
.part-row .ptype{
  font-size:11px; color:var(--text-dim); margin-top:1px;
  white-space:nowrap; overflow:hidden; text-overflow:ellipsis;
}
.part-row .pdims{
  font-family:var(--mono); font-size:11px; color:var(--text-faint);
  flex-shrink:0; text-align:right;
}
.part-row.matched .pname{ color:var(--accent); }
.empty-hint{
  text-align:center; padding:20px 10px;
  font-size:12.5px; color:var(--text-faint);
}
.tag-pill{
  display:inline-block; font-family:var(--mono); font-size:10px;
  padding:2px 7px; border-radius:5px; margin-top:3px;
}
.tag-pill.exact{ background:var(--good-dim); color:var(--good); }
.tag-pill.similar{ background:rgba(255,122,51,0.12); color:var(--accent); }

.scanned-badge{
  display:inline-flex; align-items:center; justify-content:center;
  width:14px; height:14px; margin-left:5px; border-radius:50%;
  background:var(--good-dim); color:var(--good);
  font-size:9px; font-weight:700; line-height:1; vertical-align:middle;
}

.help-text{
  font-size:11.5px; color:var(--text-faint); line-height:1.6;
}
.help-text b{ color:var(--text-dim); font-weight:600; }

/* QR scanner overlay */
#qrScanner{
  display:none;
  position:fixed; inset:0; z-index:100;
  background:rgba(8,10,13,0.97);
  flex-direction:column; align-items:center; justify-content:center;
  gap:18px;
}
#qrScanner.show{ display:flex; }
#qrFrame{ position:relative; width:78%; max-width:340px; }
#qrVideo{
  width:100%; aspect-ratio:1; object-fit:cover;
  border-radius:16px; background:#000;
  border:1px solid var(--line);
}
.qr-corner{
  position:absolute; width:26px; height:26px;
  border:2.5px solid var(--accent);
}
.qr-c-tl{ top:-1px; left:-1px; border-right:none; border-bottom:none; border-radius:14px 0 0 0; }
.qr-c-tr{ top:-1px; right:-1px; border-left:none; border-bottom:none; border-radius:0 14px 0 0; }
.qr-c-bl{ bottom:-1px; left:-1px; border-right:none; border-top:none; border-radius:0 0 0 14px; }
.qr-c-br{ bottom:-1px; right:-1px; border-left:none; border-top:none; border-radius:0 0 14px 0; }
.qr-scanline{
  position:absolute; left:6%; right:6%; height:2px;
  background:linear-gradient(90deg, transparent, var(--accent), transparent);
  animation:qrsweep 1.8s ease-in-out infinite;
}
@keyframes qrsweep{
  0%,100%{ top:8%; opacity:0.3; }
  50%{ top:88%; opacity:1; }
}
#qrStatus{
  font-family:var(--mono); font-size:12.5px; color:var(--text-dim);
  text-align:center; max-width:300px; line-height:1.6; min-height:20px;
}
#qrCloseBtn{
  position:absolute; top:max(20px, env(safe-area-inset-top)); right:20px;
  width:36px; height:36px;
  background:var(--bg-elevated); border:1px solid var(--line);
  border-radius:50%;
  display:flex; align-items:center; justify-content:center;
  color:var(--text-dim); cursor:pointer;
}
#qrCloseBtn svg{ width:16px; height:16px; }

.mobile-only{ display:none; }
@media (max-width:768px){
  .mobile-only{ display:flex; }
  .desktop-only{ display:none; }
}
@media (min-width:769px){
  #app{ max-width:520px; margin:0 auto; box-shadow:0 0 60px rgba(0,0,0,0.5); }
  body{ background:#06070a; }
}

.flexrow{ display:flex; align-items:center; }
.spacer{ flex:1; }

/* ====== 3D O'LCHOVLAR ====== */
.dim-label{
  position:absolute;
  transform:translate(-50%, -50%);
  background:rgba(10,12,15,0.82);
  backdrop-filter:blur(6px);
  border:1px solid rgba(255,255,255,0.12);
  border-radius:5px;
  padding:2px 7px;
  font-family:var(--mono);
  font-size:10.5px;
  font-weight:600;
  color:#fff;
  white-space:nowrap;
  pointer-events:none;
  letter-spacing:.02em;
  line-height:1.6;
}
/* ====== 2D CHIZMA TUGMASI (selTag ichida) ====== */
#open2DBtn{
  display:none;
  align-items:center; gap:4px;
  margin-left:6px; padding:3px 8px 3px 6px;
  background:var(--accent); color:#1a0e06;
  border-radius:6px; cursor:pointer; flex-shrink:0;
  font-family:var(--mono); font-size:11px; font-weight:700;
}
#open2DBtn svg{ width:14px; height:14px; }
#open2DBtn.show{ display:flex; }

/* ====== 2D CHIZMA MODAL ====== */
#drawing2DModal{
  position:fixed; inset:0; z-index:300;
  background:#f0f2f5;
  display:none; flex-direction:column;
}
#drawing2DModal.show{ display:flex; }
#drawing2DHeader{
  display:flex; align-items:center; justify-content:space-between;
  padding:12px 16px; border-bottom:1px solid #d5d9e0;
  background:#fff; flex-shrink:0; gap:8px;
}
#drawing2DTitle{
  font-size:13px; font-weight:600; color:#1a2030;
  overflow:hidden; text-overflow:ellipsis; white-space:nowrap; flex:1;
}
#drawing2DCloseBtn{
  width:36px; height:36px; border-radius:10px; flex-shrink:0;
  display:flex; align-items:center; justify-content:center;
  color:#555; cursor:pointer; background:#f0f2f5;
}
#drawing2DCloseBtn:active{ background:#e0e4ea; }
#drawing2DCloseBtn svg{ width:18px; height:18px; }
#drawing2DBody{
  flex:1; display:flex; flex-direction:column;
  overflow:hidden; /* child handles scroll */
}
#drawing2DSvgWrap{
  flex:1; overflow:hidden; position:relative;
  background:#fff; margin:10px 10px 0 10px;
  border-radius:12px; box-shadow:0 2px 12px #0001;
  cursor:grab; user-select:none; touch-action:none;
}
#drawing2DSvgWrap svg{
  position:absolute; inset:0;
  width:100%; height:100%;
  display:block;
  /* uzun/tor panellar ham (masalan 2486x417mm) ekranga to'liq sig'ishi uchun */
  object-fit:contain;
  /* transform applied by JS */
}
#drawing2DLegend{
  display:flex; flex-wrap:wrap; gap:6px 14px;
  padding:10px 14px 14px;
  font-family:var(--mono); font-size:11px; color:#555;
  flex-shrink:0; overflow-y:auto; max-height:90px;
}
#drawing2DLegend .lg-item{
  display:flex; align-items:center; gap:5px;
  background:#fff; border-radius:6px; padding:3px 8px;
  border:1px solid #e0e3ea;
}
#drawing2DLegend .lg-dot{
  width:10px; height:10px; border-radius:50%; flex-shrink:0;
}
#drawing2DLegend .lg-line{
  width:14px; height:3px; border-radius:2px; flex-shrink:0;
}
#drawing2DEmpty{
  color:#aaa; font-size:13px; text-align:center; padding:40px 20px;
}
/* Hint text on SVG wrap */
#drawing2DSvgWrap::after{
  content:"Ikkita barmoq — zoom · Bir barmoq — siljit · 2× bosish — reset";
  position:absolute; bottom:8px; left:50%; transform:translateX(-50%);
  font-size:10px; color:#aaa; white-space:nowrap; pointer-events:none;
  font-family:var(--mono);
}
