:root {
  --navy: #1f2937;
  --blue: #475569;
  --ink: #16202b;
  --muted: #5d6b7a;
  --line: #d9dee5;
  --bg: #f3f5f8;
  --card: #fff;
  --danger: #c62828;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); font: 15px/1.4 var(--f-body, -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif); overscroll-behavior: none; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input, select, textarea { font: inherit; }
.screen { position: fixed; inset: 0; display: flex; flex-direction: column; }
.muted { color: var(--muted); }
.err { color: var(--danger); min-height: 1.2em; }

.btn { border: 1px solid var(--line); background: #fff; border-radius: var(--r-btn, 10px); font-weight: 600; padding: 8px 14px; white-space: nowrap; }
.btn.primary { background: var(--navy); color: #fff; border-color: var(--navy); }
.btn.ghost { background: transparent; border-color: transparent; color: #fff; }
.btn.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn:disabled { opacity: .5; }

/* login */
#login { align-items: center; justify-content: center; background: var(--navy); padding: 16px; }
.card { background: var(--card); border-radius: 16px; padding: 22px; box-shadow: 0 10px 30px rgba(0,0,0,.18); }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 12px; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.login-logo { width: 64px; height: 64px; border-radius: 14px; }
/* a company's own logo keeps its shape */
.login-logo.own { width: auto; max-width: 200px; object-fit: contain; border-radius: 6px; }
.brand img.own { width: auto; max-width: 120px; object-fit: contain; padding: 2px 4px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label input, label select, label textarea { padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; color: var(--ink); font-size: 16px; background: #fff; }

/* top bar */
.topbar { display: flex; align-items: center; gap: 8px; background: var(--navy); color: #fff; padding: calc(8px + var(--safe-top)) 12px 8px; min-height: 52px; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
.topbar::-webkit-scrollbar { display: none; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.brand img { width: 30px; height: 30px; border-radius: 7px; background: #fff; }
.spacer { flex: 1; }
.topbar .btn:not(.primary):not(.ghost) { background: rgba(255,255,255,.12); border-color: rgba(255,255,255,.2); color: #fff; }
.topbar .btn.primary { background: #fff; color: var(--navy); border-color: #fff; font-weight: 600; }
.icon-btn { width: 38px; height: 38px; border-radius: 10px; border: 1px solid rgba(255,255,255,.2); background: rgba(255,255,255,.12); color: #fff; font-size: 19px; line-height: 1; flex: none; }
.tog { height: 38px; border-radius: 10px; border: 1px solid rgba(255,255,255,.2); background: rgba(255,255,255,.12); color: #fff; padding: 0 12px; }
.tog.on { background: #fff; color: var(--navy); }
.tb-group { display: flex; align-items: center; gap: 4px; flex: none; }
.sheet-size span { font-size: 13px; min-width: 74px; text-align: center; }
.title-btn { background: none; border: 0; color: #fff; text-align: left; display: flex; flex-direction: column; padding: 0 4px; max-width: 260px; flex: none; }
.title-btn span { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title-btn small { opacity: .75; font-size: 12px; }
.save-state { font-size: 12px; opacity: .8; white-space: nowrap; flex: none; }

/* list */
.list-wrap { flex: 1; overflow: auto; padding: 16px 16px calc(24px + var(--safe-bottom)); max-width: 900px; width: 100%; margin: 0 auto; }
.list-head { display: flex; gap: 10px; margin-bottom: 14px; }
.list-head input { flex: 1; min-width: 0; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; font-size: 16px; }
.drawings { display: grid; gap: 10px; }
.drow { display: flex; align-items: center; gap: 12px; background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; text-align: left; width: 100%; }
.drow b { display: block; }
.drow .meta { color: var(--muted); font-size: 13px; }
.drow .sq { margin-left: auto; color: var(--navy); font-weight: 600; white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; }

/* editor */
.ed-body { position: relative; flex: 1; min-height: 0; display: flex; }
.canvas { position: relative; flex: 1; min-width: 0; background: var(--sunk, #e7ebf0); overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none; }
.canvas-svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.canvas-svg .is-sel { filter: drop-shadow(0 0 2px rgba(30,136,229,.9)); }
.canvas-svg .lbl, .canvas-svg .hdl { cursor: pointer; }
.tools { display: flex; flex-direction: column; gap: 4px; padding: 8px 6px calc(8px + var(--safe-bottom)); background: #fff; border-left: 1px solid var(--line); overflow-y: auto; flex: none; width: 74px; }
.tools button { border: 0; background: none; border-radius: 10px; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 7px 2px; font-size: 11px; color: var(--muted); }
.tools button .ti { font-size: 21px; line-height: 1.1; color: var(--ink); }
.tools button.on { background: var(--accent-soft, #e3f0fd); color: var(--accent-ink, var(--navy)); }
.tools .beam-i { color: #c62828; font-weight: 900; }
.tools .ribbon-i { color: #2e7d32; font-weight: 900; }
.tools .snap { margin-top: auto; }
.tools .snap:not(.on) .ti { opacity: .35; }
.tools .done { background: var(--navy); color: #fff; }
.tools .done .ti { color: #fff; }
.selbar { position: absolute; left: 50%; bottom: calc(14px + var(--safe-bottom)); transform: translateX(calc(-50% - 37px)); display: flex; gap: 4px; background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 6px; box-shadow: 0 6px 20px rgba(0,0,0,.15); max-width: calc(100% - 110px); overflow-x: auto; }
.selbar button { border: 0; background: var(--bg, #f1f4f8); border-radius: 9px; padding: 8px 11px; white-space: nowrap; font-size: 14px; }
.selbar button[data-act="delete"] { color: var(--danger); }
.hint { position: absolute; left: 12px; top: 10px; background: color-mix(in srgb, var(--navy) 88%, transparent); color: #fff; font-size: 13px; padding: 6px 10px; border-radius: 9px; pointer-events: none; max-width: calc(100% - 110px); }
.hint:empty { display: none; }

/* modals */
.modal-back { position: fixed; inset: 0; background: rgba(10,20,35,.45); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 50; }
.modal { background: #fff; border-radius: 16px; width: 100%; max-width: 440px; max-height: calc(100% - 20px); overflow: auto; padding: 18px; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal.wide { max-width: 640px; }
.modal h2 { margin: 0 0 10px; font-size: 18px; }
.modal .row, .side-panel .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }
.modal .grid2, .side-panel .grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.modal .sec, .side-panel .sec { margin-top: 14px; font-weight: 700; font-size: 14px; color: var(--navy); }
.seg { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.seg button { border: 1px solid var(--line); background: #fff; border-radius: 9px; padding: 7px 12px; }
.seg button.on { background: var(--navy); color: #fff; border-color: var(--navy); }
.opt-line { display: flex; align-items: center; gap: 10px; margin-top: 8px; font-size: 14px; }
.opt-line > span { width: 64px; color: var(--muted); }

.lenbox { font-size: 30px; font-weight: 700; text-align: center; padding: 10px; border: 2px solid var(--blue); border-radius: 12px; min-height: 58px; letter-spacing: .5px; }
.lenbox.bad { border-color: var(--danger); }
.keypad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 12px; }
.keypad button { border: 1px solid var(--line); background: var(--bg, #f6f8fb); border-radius: 12px; padding: 14px 0; font-size: 22px; }
.keypad .ok { background: var(--navy); color: #fff; border-color: var(--navy); grid-column: span 2; font-size: 18px; font-weight: 700; }
.lenhint { text-align: center; color: var(--muted); font-size: 13px; margin-top: 6px; min-height: 1.2em; }

.joblist { max-height: 46vh; overflow: auto; border: 1px solid var(--line); border-radius: 10px; margin-top: 8px; }
.joblist button { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: #fff; padding: 10px 12px; }
.joblist button.on { background: var(--accent-soft, #e3f0fd); }
.joblist small { color: var(--muted); }

.shape-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.shape-grid .sh { position: relative; border: 1px solid var(--line); border-radius: 12px; background: #fff; padding: 10px 6px; display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: 13px; }
.shape-grid svg { width: 64px; height: 48px; }
.shape-grid .x { position: absolute; top: 2px; right: 6px; border: 0; background: none; color: var(--danger); font-size: 16px; }

.takeoff table { width: 100%; border-collapse: collapse; font-size: 14px; }
.takeoff td { padding: 6px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
.takeoff td:first-child { color: var(--muted); width: 42%; }
.menu-list { display: flex; flex-direction: column; gap: 8px; }
.menu-list .btn { text-align: left; }

.toast { position: fixed; left: 50%; bottom: calc(20px + var(--safe-bottom)); transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 12px; z-index: 80; font-size: 14px; max-width: calc(100% - 32px); text-align: center; }

@media (max-width: 760px) {
  .tools { width: 60px; padding: 6px 3px; }
  .tools button { font-size: 10px; padding: 5px 0; }
  .tools button .ti { font-size: 18px; }
  .sheet-size span { min-width: 58px; font-size: 12px; }
  .title-btn { max-width: 150px; }
  #infoBtn { display: none; }
  .selbar { transform: translateX(calc(-50% - 30px)); max-width: calc(100% - 80px); }
  .hint { max-width: calc(100% - 80px); }
}
@media print {
  .topbar, .tools, .selbar, .hint { display: none !important; }
}

.sugg { position: absolute; left: 0; right: 0; top: 100%; z-index: 20; background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 6px 20px rgba(0,0,0,.15); max-height: 260px; overflow: auto; }
.sugg button { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); background: #fff; padding: 9px 12px; font-size: 15px; }
.sugg small { color: var(--muted); }

.topbar .btn.price-btn.has-price { background: #2e7d32; border-color: #2e7d32; font-weight: 700; }
.price-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.price-head .big { font-size: 32px; font-weight: 800; color: var(--navy); line-height: 1.1; }
.price-warn { background: #fff7e0; border: 1px solid #f0d58a; border-radius: 10px; padding: 8px 10px; font-size: 13px; margin-top: 10px; display: grid; gap: 4px; }
.price-dlg select { padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; font-size: 16px; background: #fff; color: var(--ink); }
.mat { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 6px; }
.mat th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 4px 6px; }
.mat td { padding: 5px 6px; border-top: 1px solid var(--line); vertical-align: middle; }
.mat td:nth-child(n+2) { text-align: right; white-space: nowrap; }
.mat tr.grp td { background: var(--bg, #f3f6f9); font-weight: 600; color: var(--navy); text-align: left; }
.mat tr.miss td { background: #fff4f2; }
.mat .need { color: #c62828; font-size: 12px; }
.mat input.qty { width: 64px; padding: 6px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px; text-align: right; }
.mat .reset { border: 0; background: #eef2f6; border-radius: 6px; font-size: 11px; padding: 2px 6px; color: var(--muted); }
.mat.sum td:first-child { text-align: left; }
.mat.sum tr.tot td { font-weight: 700; color: var(--navy); }
.plist input { width: 100px; padding: 6px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px; text-align: right; }
.extra-add { display: grid; grid-template-columns: 1fr 70px 90px auto; gap: 6px; margin-top: 8px; }
.extra-add input { padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; font-size: 15px; min-width: 0; }
.opt-line { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 6px; font-size: 14px; }
.opt-line .seg { margin-top: 0; justify-content: flex-end; }
.opt-line .seg button { padding: 6px 10px; font-size: 13px; }
.price-dlg label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.price-dlg label input[type=number] { padding: 9px 10px; border: 1px solid var(--line); border-radius: 10px; font-size: 16px; color: var(--ink); width: 100%; box-sizing: border-box; }
@media (max-width: 560px) { .price-dlg .grid2 { grid-template-columns: 1fr 1fr !important; } }
.opt-line > span { white-space: nowrap; }

.up-bar { height: 8px; background: #eef2f6; border-radius: 4px; overflow: hidden; margin: 8px 0; }
.up-bar div { height: 100%; width: 0; background: #2e7d32; transition: width .2s; }
.up-log { max-height: 240px; overflow: auto; font-size: 13px; display: grid; gap: 3px; color: var(--muted); }
.up-log .bad { color: #c62828; }
.list-head { flex-wrap: wrap; }

/* price panel beside the drawing */
.side-panel { flex: none; width: 620px; max-width: 54%; overflow-x: hidden; background: #fff; border-left: 1px solid var(--line); overflow-y: auto; padding: 14px 16px calc(16px + var(--safe-bottom)); box-sizing: border-box; -webkit-overflow-scrolling: touch; }
.side-panel[hidden] { display: none; }
.side-panel h2 { margin: 0; font-size: 18px; }
@media (max-width: 1100px) { .side-panel .grid2 { grid-template-columns: 1fr 1fr !important; } }
.side-panel .price-head .big { font-size: 28px; }
@media (max-width: 900px) and (orientation: portrait) {
  .ed-body { flex-wrap: wrap; }
  .ed-body.has-panel .canvas { flex: 1 1 calc(100% - 74px); height: 50%; }
  .side-panel { order: 3; width: 100%; max-width: none; height: 50%; border-left: 0; border-top: 1px solid var(--line); }
}
.ed-body.has-panel .tools, .ed-body.has-panel .selbar, .ed-body.has-panel .hint { display: none !important; }
.ed-body.has-panel .canvas { cursor: grab; }

/* Pricing setup */
.ps-exp { display: grid; grid-template-columns: 1fr 130px 40px; gap: 6px; margin-top: 6px; }
.ps-exp input { width: 100%; }
.ps-tag { font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 8px; margin-left: 6px; }
.ps-tag.lo { background: #fff4f2; color: #c62828; }
.ps-tag.hi { background: #fff7e0; color: #8a5a00; }
.ps-tag.ok { background: #e8f5e9; color: #2e7d32; }
@media (max-width: 520px) { .ps-exp { grid-template-columns: 1fr 100px 36px; } }

/* Guided pricing setup */
.wz-steps { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.wz-dot { width: 30px; height: 30px; border-radius: 50%; border: 1px solid var(--line); background: var(--card); font-size: 13px; font-weight: 600; color: var(--muted); padding: 0; }
.wz-dot.done { background: #e8f5e9; border-color: #a5d6a7; color: #2e7d32; }
.wz-dot.on { background: var(--blue); border-color: var(--blue); color: #fff; }
.wz-body h2 { margin-top: 4px; }
.wz-list { margin: 8px 0 12px; padding-left: 22px; line-height: 1.7; }
.wz-unit { font-weight: 400; font-size: 12px; }
.wz-line { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 8px; }
.wz-grp { border: 1px solid var(--line); border-radius: 10px; margin-top: 8px; overflow: hidden; }
.wz-grp-h { display: flex; justify-content: space-between; align-items: center; width: 100%; padding: 12px 14px; background: var(--card); border: 0; font-weight: 600; text-align: left; }
.wz-grp .plist { margin: 0 10px 10px; width: calc(100% - 20px); }
.wz-count { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 10px; background: #fff7e0; color: #8a5a00; }
.wz-count.ok { background: #e8f5e9; color: #2e7d32; }
.wz-check { margin-top: 10px; padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.wz-check.ok { background: #e8f5e9; color: #1b5e20; }
.wz-check.off { background: #fff7e0; color: #6d4700; }
.wz-total { margin-top: 10px; }
.wz-nav { margin-top: 16px; flex-wrap: wrap; }
.setup-banner { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 0 0 12px; padding: 14px 16px; border-radius: 12px; background: var(--card); border: 2px solid var(--blue); }
.setup-banner .meta { color: var(--muted); font-size: 13px; margin-top: 2px; }
@media (max-width: 520px) { .wz-line { grid-template-columns: 1fr; } .setup-banner { flex-direction: column; align-items: stretch; } }
.wz-body .grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (max-width: 520px) { .wz-body .grid3 { grid-template-columns: 1fr 1fr; } }
.wz-adv { margin-top: 14px; }
.wz-adv summary { cursor: pointer; font-weight: 600; padding: 6px 0; }
.wz-jump { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.wz-body .grid3 label { min-width: 0; justify-content: flex-end; }
.wz-body .grid3 input { min-width: 0; width: 100%; box-sizing: border-box; }
.send-list { margin: 4px 0 10px; padding-left: 20px; line-height: 1.5; }
.wz-mode { margin: 4px 0 6px; }
.wz-crew { border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-top: 8px; }
.wz-crew-h { display: grid; grid-template-columns: 1fr auto 40px; gap: 8px; align-items: center; margin-bottom: 6px; }
.wz-crew-h input { width: 100%; box-sizing: border-box; font-weight: 600; }
.wz-crew .grid3 { margin-top: 6px; }
#wz .btn.ghost { color: var(--navy); }
#wz .btn.ghost:hover { background: #f1f3f6; }
.wz-cap td:last-child { white-space: nowrap; text-align: right; vertical-align: top; }

.wz-bar { height: 10px; border-radius: 5px; background: var(--navy, #1f3a5f); opacity: .75; min-width: 2px; }
.wz-months td { padding-top: 6px; padding-bottom: 6px; }
