:root {
  --bg: #eef0f2; --card: #fff; --ink: #14171a; --muted: #5d6873; --line: #d3d9df; --field: #f7f8f9;
  --accent: #d9480f; --accent-ink: #fff; --ok: #2b8a3e; --ok-bg: #e6f4ea; --err: #c92a2a; --err-bg: #fdecec;
  --warn-bg: #fff4d6; --warn-ink: #5c4300;
  --seg1: #34506b; --seg2: #4a6f93; --trim: #aab3bc; --scrap: #c92a2a; --keep: #2b8a3e;
  --r: 16px; --nav: 64px; --wrap: 640px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1215; --card: #1a1f24; --ink: #e9ecef; --muted: #9aa6b2; --line: #2e363e; --field: #13171b;
    --ok: #51cf66; --ok-bg: #13301b; --err: #ff6b6b; --err-bg: #3a1717; --warn-bg: #3a2f10; --warn-ink: #ffe08a;
    --seg1: #4f7aa3; --seg2: #6f97bf; --trim: #59636d; --scrap: #ff6b6b; --keep: #51cf66;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Georgian", sans-serif;
  font-variant-numeric: tabular-nums; -webkit-tap-highlight-color: transparent;
}
#app { max-width: var(--wrap); margin: 0 auto; padding: 0 12px calc(var(--nav) + 110px + env(safe-area-inset-bottom)); }
h1, h2 { margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 20px; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
svg { width: 20px; height: 20px; flex: none; }

header.top {
  position: sticky; top: 0; z-index: 5; background: var(--bg);
  padding: calc(12px + env(safe-area-inset-top)) 4px 10px; display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
}
header.top h1 { font-size: 28px; }
.brand { font-size: 13px; color: var(--muted); font-weight: 600; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; margin: 0 0 12px; }
.muted { color: var(--muted); } .small { font-size: 14px; }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; font-size: 17px; }

/* fields */
.fld { display: block; min-width: 0; }
.fld .lbl { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin: 0 0 4px; }
.fld small { display: block; color: var(--muted); font-size: 13px; margin-top: 4px; }
.inp { position: relative; display: block; }
.inp i { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); font-style: normal; font-size: 14px; color: var(--muted); pointer-events: none; }
input, select {
  font: inherit; font-size: 17px; width: 100%; min-height: 50px; padding: 10px 12px; margin: 0;
  border: 1px solid var(--line); border-radius: 12px; background: var(--field); color: var(--ink);
}
.inp input:has(+ i) { padding-right: 44px; }
select {
  appearance: none; -webkit-appearance: none; padding-right: 36px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 19px) 50%, calc(100% - 13px) 50%; background-size: 6px 6px; background-repeat: no-repeat;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; background-color: var(--card); }
input::placeholder { color: var(--muted); opacity: .6; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.grid-pa { display: grid; grid-template-columns: 1fr 108px; gap: 10px; margin-top: 10px; }
.grid-fq { display: grid; grid-template-columns: 1fr 88px; gap: 10px; margin-top: 10px; }
.row-del { display: grid; grid-template-columns: 1fr auto; gap: 6px; align-items: end; }
label.check { display: flex; align-items: center; gap: 12px; min-height: 48px; margin-top: 6px; font-weight: 600; }
label.check input { width: 24px; height: 24px; min-height: 0; accent-color: var(--accent); flex: none; }

/* buttons */
.btn {
  appearance: none; min-height: 50px; padding: 0 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); font-weight: 650; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; cursor: default; transform: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.big { width: 100%; min-height: 58px; font-size: 18px; border-radius: 14px; box-shadow: 0 6px 20px rgba(217, 72, 15, .28); }
.btn.ghost { background: transparent; border-style: dashed; border-width: 1.5px; }
.btn.wide { width: 100%; margin-top: 10px; }
.btn.small { min-height: 40px; padding: 0 12px; font-size: 15px; }
.btn.link { border: 0; background: none; color: var(--muted); min-height: 48px; font-weight: 600; text-decoration: underline; text-underline-offset: 3px; display: flex; margin: 4px auto 0; }
.btn.danger { color: var(--err); }
.icon-btn { width: 48px; height: 50px; border: 0; background: none; color: var(--muted); border-radius: 12px; display: grid; place-items: center; }
.icon-btn:active { background: var(--bg); }

/* order */
.item-h { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: center; }
.item-h .num { font-weight: 800; font-size: 15px; background: var(--bg); border-radius: 10px; padding: 6px 10px; max-width: 34vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-h select { font-weight: 650; }
.kind { margin: 8px 0 0; font-size: 13px; color: var(--muted); font-weight: 600; }
.adders { margin: 4px 0 0; }
details > summary { list-style: none; cursor: pointer; }
details > summary::-webkit-details-marker { display: none; }
.parts { margin-top: 12px; border-top: 1px solid var(--line); }
.parts > summary { display: flex; justify-content: space-between; align-items: center; min-height: 44px; padding-top: 10px; font-weight: 650; color: var(--muted); }
.parts > summary::after, .sec > summary::after, .tpl > summary::after { content: ""; width: 8px; height: 8px; border: solid var(--muted); border-width: 0 2px 2px 0; transform: rotate(45deg); margin: 0 6px 4px 10px; transition: transform .15s; }
details[open] > summary::after { transform: rotate(-135deg); margin-bottom: -2px; }
.parts > summary .pc { margin-left: auto; }
.plist { list-style: none; margin: 4px 0 0; padding: 0; }
.plist li { display: grid; grid-template-columns: 1fr auto 3.2em; gap: 8px; align-items: baseline; padding: 7px 0; border-top: 1px dashed var(--line); }
.plist li small { display: block; color: var(--muted); }
.plist .n { color: var(--muted); text-align: right; }
.bad, .plist li.bad b { color: var(--err); }

.cta { position: fixed; left: 0; right: 0; bottom: calc(var(--nav) + env(safe-area-inset-bottom)); padding: 18px 12px 10px; background: linear-gradient(to bottom, transparent, var(--bg) 40%); z-index: 6; pointer-events: none; }
.cta > div { max-width: var(--wrap); margin: 0 auto; pointer-events: auto; }

/* nav */
nav.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: flex; justify-content: center;
  height: calc(var(--nav) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
nav.tabs button { position: relative; flex: 1; max-width: 220px; border: 0; background: none; color: var(--muted); font-size: 12px; font-weight: 650; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; }
nav.tabs svg { width: 24px; height: 24px; }
nav.tabs button[aria-current="page"] { color: var(--accent); }
nav.tabs .dot { position: absolute; top: 10px; left: calc(50% + 10px); width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }

/* notes */
.note { border-radius: 12px; padding: 12px 14px; margin: 0 0 12px; font-size: 15px; display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.note.warn { background: var(--warn-bg); color: var(--warn-ink); }
.note.err { background: var(--err-bg); color: var(--err); display: block; }
#toast { position: fixed; left: 50%; bottom: calc(var(--nav) + 90px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px); opacity: 0; transition: .2s; background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 12px; font-weight: 600; z-index: 20; pointer-events: none; max-width: 90vw; text-align: center; }
#toast.on { opacity: 1; transform: translate(-50%, 0); }

/* result */
.summary table { width: 100%; border-collapse: collapse; margin-top: 8px; }
.summary th, .summary td { text-align: left; padding: 8px 0; border-top: 1px solid var(--line); vertical-align: top; }
.summary th { font-weight: 600; padding-right: 12px; }
.summary td { font-weight: 750; text-align: right; }
.summary tr.tot th, .summary tr.tot td { border-top-width: 2px; }
.kpi { margin: 8px 0 0; color: var(--muted); }
.kpi b { color: var(--ink); }
.prof { margin: 22px 0 0; }
.prof-h { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 0 4px; }
.badge { display: inline-flex; align-items: center; gap: 4px; background: var(--ok-bg); color: var(--ok); font-size: 13px; font-weight: 750; padding: 4px 10px 4px 8px; border-radius: 99px; }
.badge svg { width: 16px; height: 16px; }
.stats { margin: 6px 4px 10px; color: var(--muted); font-size: 15px; }
.stats b { color: var(--ink); }
.chip { display: inline-block; padding: 2px 8px; border-radius: 99px; font-weight: 700; font-size: 14px; margin: 2px 4px 2px 0; }
.chip.keep { background: var(--ok-bg); color: var(--ok); }

.bar { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 12px 14px; margin: 0 0 10px; transition: opacity .15s; }
.bar.done { opacity: .5; }
.bar.done .diagram { filter: grayscale(1); }
.bar-h { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.bar-t { font-weight: 800; font-size: 17px; }
.mult { font-weight: 800; font-size: 17px; color: var(--accent); }
.tick { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: none; border-radius: 99px; min-height: 40px; padding: 0 12px; font-size: 14px; font-weight: 650; color: var(--muted); }
.tick svg { width: 16px; height: 16px; }
.tick[aria-pressed="true"] { background: var(--ok); border-color: var(--ok); color: #fff; }
.diagram { display: flex; gap: 2px; height: 34px; margin: 10px 0 4px; border-radius: 6px; overflow: hidden; background: var(--line); }
.seg { container-type: inline-size; flex: none; min-width: 2px; background: var(--seg1); color: #fff; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.seg:nth-child(even) { background: var(--seg2); }
.seg em { font-style: normal; font-size: 11px; font-weight: 750; white-space: nowrap; }
.seg.trim { background: var(--trim); }
@container (max-width: 34px) { .seg em { display: none; } }
.seg.rest { flex: 1; background: repeating-linear-gradient(-45deg, var(--card) 0 5px, transparent 5px 9px), var(--scrap); color: var(--scrap); }
.seg.rest.keep { background: repeating-linear-gradient(-45deg, var(--card) 0 5px, transparent 5px 9px), var(--keep); color: var(--keep); }
.seg.rest em { background: var(--card); padding: 0 4px; border-radius: 4px; }
.cuts { list-style: none; margin: 0; padding: 0; }
.cuts li { display: grid; grid-template-columns: auto auto 1fr; gap: 2px 10px; align-items: baseline; padding: 9px 0; border-top: 1px solid var(--line); }
.cuts li:first-child { border-top: 0; }
.cuts .len { font-size: 22px; font-weight: 800; letter-spacing: -0.01em; }
.cuts .n { font-weight: 750; color: var(--accent); }
.cuts .ang { justify-self: end; font-size: 13px; font-weight: 650; padding: 3px 9px; border-radius: 99px; background: var(--bg); white-space: nowrap; }
.cuts .lbls { grid-column: 1 / -1; font-size: 14px; color: var(--muted); }
.rest-line { margin: 4px 0 0; padding-top: 8px; border-top: 1px dashed var(--line); font-size: 14px; color: var(--scrap); }
.rest-line.keep { color: var(--keep); }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 18px; }
.spinner { display: block; width: 32px; height: 32px; margin: 0 auto 12px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* settings */
.sec { padding: 0; }
.sec > summary { display: flex; align-items: center; justify-content: space-between; min-height: 58px; padding: 0 14px; font-weight: 750; font-size: 18px; }
.sec-b { padding: 0 14px 14px; }
.sec-b > p:first-child { margin-top: 0; }
.seg-ctl { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.seg-ctl .btn { padding: 0 6px; font-size: 15px; }
.seg-ctl .btn.on { background: var(--ink); color: var(--card); border-color: var(--ink); }
.sub { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-top: 10px; }
.sub .fld + .fld { margin-top: 10px; }
.tpl > summary { display: flex; align-items: center; min-height: 32px; font-weight: 700; }
.tpl > summary span { flex: 1; }
.tpl > summary small { color: var(--muted); font-weight: 600; }
.tpl[open] > summary { margin-bottom: 10px; }
.part { background: var(--bg); border-radius: 12px; padding: 10px; margin: 10px 0; }
.part input, .part select { background-color: var(--card); }
.pv { display: block; margin-top: 6px; color: var(--muted); font-size: 14px; }
.pv b { color: var(--ink); }
.stock-row { display: grid; grid-template-columns: 1.3fr 1fr 64px auto; gap: 8px; align-items: end; }
.stock-row .fld .lbl { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.formula input { font-family: ui-monospace, "SF Mono", Menlo, monospace; }

.print-only { display: none; }

@media (min-width: 700px) {
  header.top h1 { font-size: 32px; }
}

@media print {
  :root { --bg: #fff; --card: #fff; --ink: #000; --muted: #333; --line: #999; --seg1: #444; --seg2: #777; --trim: #bbb; }
  .no-print, nav.tabs, .cta, header.top, #toast { display: none !important; }
  body { background: #fff; font-size: 12px; }
  #app { max-width: none; padding: 0; }
  .print-only { display: block; font-weight: 700; margin: 0 0 8px; }
  .card, .bar { break-inside: avoid; border-color: #999; }
  .bar.done { opacity: 1; }
  .seg, .chip, .badge, .tick { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .cuts .len { font-size: 16px; }
}
