:root {
  --bg: #f3f4f6; --card: #fff; --text: #111827; --muted: #6b7280; --line: #e5e7eb;
  --primary: #1d4ed8; --primary-ink: #fff; --danger: #dc2626; --danger-bg: #fef2f2; --danger-line: #fecaca;
  --ok: #15803d; --ok-bg: #f0fdf4; --head: #1f2a37; --head-ink: #f9fafb;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1016; --card: #151c24; --text: #e5e7eb; --muted: #9ca3af; --line: #26303b;
    --primary: #3b82f6; --danger: #f87171; --danger-bg: #2a1215; --danger-line: #5b1d22;
    --ok: #4ade80; --ok-bg: #0f2418; --head: #0f151c;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 17px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, input, textarea, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }

header.top { position: sticky; top: 0; z-index: 5; background: var(--head); color: var(--head-ink); display: flex; align-items: center; gap: 12px; padding: 12px 16px; }
header.top h1 { font-size: 20px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
header.top .btn { background: rgba(255,255,255,.12); color: var(--head-ink); }

main { max-width: 1100px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 16px; }
.card h2 { font-size: 18px; margin-bottom: 12px; }

.btn { border: 0; border-radius: 12px; padding: 12px 18px; min-height: 48px; cursor: pointer; font-weight: 600; background: var(--line); }
.btn.primary { background: var(--primary); color: var(--primary-ink); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.big { font-size: 19px; padding: 16px 22px; }
.btn:disabled { opacity: .5; }

/* Qizil ogohlantirish: shu bo'limga qarshi ochiq shikoyatlar */
.alarm { background: var(--danger-bg); border: 2px solid var(--danger); border-radius: 14px; padding: 14px 16px; animation: pulse 2s ease-in-out infinite; }
.alarm h2 { color: var(--danger); font-size: 19px; margin-bottom: 8px; }
.alarm-item { display: flex; gap: 12px; align-items: center; justify-content: space-between; padding: 10px 0; border-top: 1px solid var(--danger-line); }
.alarm-item:first-of-type { border-top: 0; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--danger) 18%, transparent); } }
@media (prefers-reduced-motion: reduce) { .alarm { animation: none; } }
.allgood { background: var(--ok-bg); color: var(--ok); border-radius: 14px; padding: 12px 16px; font-weight: 600; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: 14px; }
td.r, th.r { text-align: right; }
.tablewrap { overflow-x: auto; }

.stages { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.stage-card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 14px; cursor: pointer; text-align: left; }
.stage-card.bad { border: 2px solid var(--danger); background: var(--danger-bg); }
.stage-card .n { font-size: 13px; color: var(--muted); }
.stage-card .name { font-size: 18px; font-weight: 700; }
.stage-card .badge { display: inline-block; margin-top: 8px; font-size: 14px; font-weight: 600; }
.stage-card.bad .badge { color: var(--danger); }

.tabs { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; }
.tabs .btn { white-space: nowrap; }
.tabs .btn.on { background: var(--primary); color: var(--primary-ink); }

.pill { display: inline-block; padding: 2px 10px; border-radius: 99px; font-size: 13px; font-weight: 600; }
.pill.open { background: var(--danger-bg); color: var(--danger); }
.pill.resolved { background: var(--ok-bg); color: var(--ok); }

/* Kirish */
.login { max-width: 420px; margin: 10vh auto; padding: 16px; }
.login .card { display: grid; gap: 14px; padding: 24px; }
label { display: grid; gap: 6px; font-size: 15px; color: var(--muted); }
input, textarea, select { width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 12px; font-size: 17px; }
.err { color: var(--danger); min-height: 1em; margin: 0; }

dialog { border: 0; border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 32px)); background: var(--card); color: var(--text); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog form { display: grid; gap: 14px; padding: 20px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips button { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 10px 14px; min-height: 48px; cursor: pointer; }
.chips button.on { background: var(--danger); color: #fff; border-color: var(--danger); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.actions { display: flex; justify-content: flex-end; gap: 10px; }
.inline { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.inline input { width: auto; flex: 1; min-width: 140px; }
@media (max-width: 520px) { .row2 { grid-template-columns: 1fr; } }

/* ---- Ishlab chiqarish ---- */
:root { --okbtn: #15803d; --warn: #b45309; --warn-bg: #fffbeb; }
@media (prefers-color-scheme: dark) { :root { --okbtn: #16a34a; --warn: #fbbf24; --warn-bg: #2a2110; } }
.btn.ok { background: var(--okbtn); color: #fff; }
.btn.small { min-height: 40px; padding: 8px 12px; font-size: 15px; }
header.top .btn.flag { background: rgba(248,113,113,.18); color: #fecaca; }
.pill.demo { background: #fde68a; color: #78350f; }
.alarm-mini { display: flex; align-items: center; justify-content: space-between; gap: 12px; background: var(--danger-bg); border: 1px solid var(--danger); color: var(--danger); border-radius: 12px; padding: 10px 14px; font-weight: 600; }
.route { color: var(--muted); font-size: 15px; margin: -4px 0 12px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; }
.tile { text-align: left; background: var(--bg); border: 1px solid var(--line); border-radius: 14px; padding: 16px; min-height: 104px; cursor: pointer; display: grid; gap: 6px; align-content: start; }
.tile:active { transform: scale(.98); }
.tile .t-name { font-size: 18px; font-weight: 700; }
.tile .t-sub { font-size: 14px; color: var(--muted); }
.tile.short { border-color: var(--warn); }
.tile.short .t-sub.w { color: var(--warn); }
.qty { display: grid; grid-template-columns: 64px 1fr 64px; gap: 10px; }
.qty input { text-align: center; font-size: 32px; font-weight: 700; padding: 8px; }
.qbtn { border: 1px solid var(--line); background: var(--bg); border-radius: 12px; font-size: 30px; font-weight: 700; cursor: pointer; }
td.w, .w { color: var(--warn); font-weight: 600; }
.status-done { color: var(--ok); font-weight: 600; }
.status-failed { color: var(--danger); font-weight: 600; }
.status-canceled { color: var(--muted); text-decoration: line-through; }
.status-pending { color: var(--muted); }
details.card > summary { cursor: pointer; font-weight: 700; font-size: 18px; list-style: none; }
details.card > summary::after { content: '▾'; float: right; color: var(--muted); }
details.card[open] > summary { margin-bottom: 12px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--okbtn); color: #fff; padding: 14px 22px; border-radius: 14px; font-weight: 700; box-shadow: 0 8px 24px rgba(0,0,0,.25); z-index: 50; max-width: calc(100vw - 32px); }
.toast.bad { background: var(--danger); }
.total-row td { font-weight: 700; border-bottom: 0; }

/* ---- Bo'lim ekrani v2: oynalar, qidiruv, ixcham ro'yxat ---- */
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 4px; }
.seg button { border: 0; background: transparent; border-radius: 10px; min-height: 48px; font-weight: 700; font-size: 17px; cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--primary); color: var(--primary-ink); }
.search { font-size: 19px; padding: 14px 16px; border-radius: 12px; }
.small-search { font-size: 16px; padding: 10px 12px; margin-bottom: 8px; }
.route { margin: 10px 0 8px; }
.sub { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 12px 0 6px; }
.rows { display: grid; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; text-align: left; background: var(--card); border: 0; border-top: 1px solid var(--line); padding: 14px 16px; min-height: 56px; cursor: pointer; font-size: 17px; }
.row:first-child { border-top: 0; }
.row:active { background: var(--bg); }
.row .r-main { display: grid; gap: 2px; min-width: 0; }
.row .r-name { font-weight: 600; overflow-wrap: anywhere; }
.row .r-warn { font-size: 13px; }
.row .r-side { white-space: nowrap; font-size: 16px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.stock-rows { max-height: 60vh; overflow: auto; }
.srow { display: flex; justify-content: space-between; gap: 12px; padding: 10px 2px; border-top: 1px solid var(--line); }
.srow:first-child { border-top: 0; }
.srow[hidden] { display: none; }
header.top .btn.flag { background: rgba(248,113,113,.18); color: #fecaca; font-size: 18px; }
tr.status-canceled td { color: var(--muted); text-decoration: line-through; }
@media (max-width: 720px) { .two { grid-template-columns: 1fr; } }
.rows-head { display: flex; justify-content: space-between; gap: 12px; padding: 8px 16px; font-size: 13px; color: var(--muted); background: var(--bg); }

/* ---- Katalog ---- */
.catalog { display: grid; grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 12px; }
.pcard { display: grid; grid-template-rows: auto 1fr auto; gap: 8px; text-align: left; background: var(--bg); border: 1px solid var(--line); border-radius: 14px; padding: 10px; cursor: pointer; min-height: 220px; }
.pcard:active { transform: scale(.98); }
.pcard:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; }
.pimg { display: grid; place-items: center; aspect-ratio: 4 / 3; border-radius: 10px; overflow: hidden; background: hsl(var(--h) 55% 88%); }
.pimg img { width: 100%; height: 100%; object-fit: cover; }
.pini { font-size: 34px; font-weight: 800; letter-spacing: .02em; color: hsl(var(--h) 45% 32%); }
@media (prefers-color-scheme: dark) { .pimg { background: hsl(var(--h) 30% 22%); } .pini { color: hsl(var(--h) 60% 78%); } }
.pname { font-weight: 700; font-size: 16px; line-height: 1.25; overflow-wrap: anywhere; }
.pmeta { display: grid; gap: 2px; font-size: 13px; }
.pok { color: var(--ok); }
@media (max-width: 520px) { .catalog { grid-template-columns: repeat(2, 1fr); gap: 10px; } .pcard { min-height: 200px; } }
.dhead { display: grid; grid-template-columns: 120px 1fr; gap: 16px; align-items: center; }
.dhead h2 { margin-bottom: 6px; }
.dhead p { margin: 0; }
.dimg { width: 120px; }
.dimg .pini { font-size: 40px; }
@media (max-width: 520px) { .dhead { grid-template-columns: 84px 1fr; } .dimg { width: 84px; } .dimg .pini { font-size: 28px; } }
/* "Omborda bor" tavsiyasi o'ng tomonda — o'ng qo'l bilan bosishga qulay */
#produce-quick { justify-content: flex-end; }
/* Xomashyo tahrirlash (1-bo'lim) */
.mats .mat-in { width: 96px; text-align: right; padding: 8px 10px; font-size: 17px; }
.mats .mat-in.changed { border-color: var(--warn); background: var(--warn-bg); }
.xbtn { border: 1px solid var(--line); background: transparent; border-radius: 10px; width: 40px; height: 40px; font-size: 22px; cursor: pointer; color: var(--muted); }
.addmat { margin-top: 10px; display: grid; gap: 8px; justify-items: end; }
.addmat #addmat-box { width: 100%; }
.addmat-list { max-height: 220px; overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
.srow.pick { width: 100%; background: transparent; border: 0; border-top: 1px solid var(--line); text-align: left; cursor: pointer; padding: 12px; font-size: 16px; }
.srow.pick:first-child { border-top: 0; }
dialog { max-height: calc(100vh - 32px); overflow: auto; }
.target { font-size: 14px; }
.target select { font-size: 17px; padding: 12px; }
.target select.changed { border-color: var(--warn); background: var(--warn-bg); }
/* Qayerdan → Qayerga */
.flow { display: grid; grid-template-columns: 1fr auto 1fr; gap: 10px; align-items: end; }
.flow-box { display: grid; gap: 6px; min-width: 0; }
.flow-lbl { font-size: 13px; color: var(--muted); }
.flow-from { display: flex; align-items: center; min-height: 50px; padding: 0 14px; border-radius: 10px; background: var(--line); font-size: 17px; font-weight: 600; overflow-wrap: anywhere; }
.flow select { font-size: 17px; font-weight: 600; min-height: 50px; padding: 0 12px; }
.flow select.changed { border-color: var(--warn); background: var(--warn-bg); }
.flow-arrow { font-size: 30px; font-weight: 700; color: var(--primary); padding-bottom: 6px; }
/* Dialog ichidagi elementlar oynadan kengayib ketmasin (telefon) */
dialog form > * { min-width: 0; }
.flow select { width: 100%; min-width: 0; }
@media (max-width: 520px) {
  .flow { grid-template-columns: 1fr; gap: 6px; }
  .flow-arrow { transform: rotate(90deg); justify-self: center; padding: 0; line-height: 1; }
  .mats .mat-in { width: 72px; }
}
