:root {
  --bg: #0d1015;
  --panel: #151a21;
  --panel2: #1b222b;
  --line: #262f3a;
  --text: #e6ebf1;
  --muted: #8b97a6;
  --accent: #4cc2ff;
  --accent2: #2b8fd6;
  --ok: #3ccf8e;
  --warn: #ffb547;
  --bad: #ff5d5d;
  --tool: #ff7a45;
  --bed: #4cc2ff;
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
button { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 7px 12px;
  cursor: pointer; transition: background .15s, border-color .15s; white-space: nowrap; }
button:hover { border-color: #3a4757; background: #212a35; }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: var(--accent2); border-color: var(--accent2); color: #fff; }
button.primary:hover { background: #3399e0; }
button.danger { background: #3a1d1f; border-color: #6b2a2d; color: #ffb4b4; }
button.danger:hover { background: #4a2225; }
button.ghost { background: transparent; }
button.small { padding: 4px 9px; font-size: 12px; }
input[type=text], input[type=number], input[type=password], select, textarea {
  background: #0f1319; border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; outline: none; min-width: 0; }
input:focus, select:focus, textarea:focus { border-color: var(--accent2); }
input[type=range] { accent-color: var(--accent); }
label { color: var(--muted); font-size: 12.5px; }
.mono { font-family: var(--mono); }
.muted { color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.nowrap { flex-wrap: nowrap; }
.grow { flex: 1; min-width: 0; }
.spacer { flex: 1; }
.hidden { display: none !important; }

/* layout */
.app { display: grid; grid-template-columns: 210px 1fr; grid-template-rows: 56px 1fr; height: 100vh; }
header { grid-column: 1 / -1; display: flex; align-items: center; gap: 14px; padding: 0 16px;
  border-bottom: 1px solid var(--line); background: #10141a; position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; letter-spacing: .3px; font-size: 16px; }
.brand svg { width: 26px; height: 26px; }
.brand .sub { color: var(--muted); font-weight: 500; font-size: 13px; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 99px;
  background: var(--panel2); border: 1px solid var(--line); font-size: 12.5px; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--ok); } .dot.warn { background: var(--warn); } .dot.bad { background: var(--bad); }
.dot.info { background: var(--accent); }
.hdr-temps { display: flex; gap: 12px; font-size: 13px; color: var(--muted); }
.hdr-temps b { color: var(--text); font-weight: 600; }
nav { border-right: 1px solid var(--line); padding: 12px 8px; display: flex; flex-direction: column; gap: 2px; background: #0f1318; }
nav button { display: flex; align-items: center; gap: 10px; text-align: left; border: 0; background: transparent;
  padding: 9px 12px; border-radius: 8px; color: var(--muted); }
nav button svg { width: 18px; height: 18px; flex: none; }
nav button.active { background: var(--panel2); color: var(--text); }
nav button:hover { background: #18202a; color: var(--text); }
nav .estop { margin-top: auto; background: #3a1d1f; color: #ffb4b4; justify-content: center; font-weight: 700; }
main { overflow: auto; padding: 18px; }
.page { display: none; max-width: 1400px; margin: 0 auto; }
.page.active { display: block; }
h2 { font-size: 18px; margin: 0 0 14px; }
h3 { font-size: 14px; margin: 0 0 10px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .6px; }
.grid { display: grid; gap: 14px; }
.dash { grid-template-columns: minmax(0, 1.55fr) minmax(320px, 1fr); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; min-width: 0; }
.card + .card { margin-top: 14px; }
.grid > .card + .card, .col > .card + .card { margin-top: 0; }
.col { display: flex; flex-direction: column; gap: 14px; min-width: 0; }

/* cameras */
.cams-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.seg { display: inline-flex; background: #0f1319; border: 1px solid var(--line); border-radius: 9px; padding: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 11px; border-radius: 7px; color: var(--muted); }
.seg button.on { background: var(--panel2); color: var(--text); }
.cam-wrap { position: relative; background: #07090c; border-radius: 10px; overflow: hidden; aspect-ratio: 4 / 3; }
.cam-wrap img { width: 100%; height: 100%; object-fit: contain; display: block; }
.cam-wrap .empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); text-align: center; padding: 20px; }
.cam-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.cam-grid .cam-wrap .label { position: absolute; left: 8px; top: 8px; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }

/* job */
.job { display: flex; gap: 14px; }
.job .thumb { width: 92px; height: 92px; border-radius: 10px; background: #0f1319 center/contain no-repeat; flex: none; border: 1px solid var(--line); }
.job .name { font-weight: 600; font-size: 15px; word-break: break-all; }
.bar { height: 9px; background: #0f1319; border-radius: 99px; overflow: hidden; border: 1px solid var(--line); }
.bar > div { height: 100%; background: linear-gradient(90deg, var(--accent2), var(--accent)); width: 0; transition: width .6s; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 12px; }
.stat .k { color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .5px; }
.stat .v { font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; }
.banner { border-radius: 10px; padding: 12px; margin-top: 12px; border: 1px solid; }
.banner.alert { background: #2c1517; border-color: #6b2a2d; }
.banner.warn { background: #2b2213; border-color: #6a5223; }
.banner.info { background: #142330; border-color: #23506e; }
.banner img { max-width: 100%; border-radius: 8px; margin: 8px 0; display: block; }

/* temps */
canvas.chart { width: 100%; height: 170px; display: block; }
.temp-row { display: grid; grid-template-columns: 74px 1fr 92px auto; gap: 8px; align-items: center; margin-top: 8px; }
.temp-row .name { font-weight: 600; }
.temp-row .cur { font-variant-numeric: tabular-nums; }
.temp-row input { width: 92px; }
.chip { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; }

/* AI */
.gauge { display: flex; align-items: center; gap: 14px; }
.gauge .ring { --p: 0; --c: var(--ok); width: 76px; height: 76px; border-radius: 50%; flex: none;
  background: conic-gradient(var(--c) calc(var(--p) * 1%), #222b35 0); display: grid; place-items: center; }
.gauge .ring > div { width: 60px; height: 60px; border-radius: 50%; background: var(--panel); display: grid; place-items: center;
  font-weight: 700; font-size: 17px; }
.sig { display: grid; grid-template-columns: 1fr 110px 38px; gap: 8px; align-items: center; padding: 5px 0; border-bottom: 1px dashed #222a33; }
.sig:last-child { border-bottom: 0; }
.sig .lbl { font-size: 13px; }
.sig label.lbl { flex-wrap: nowrap; align-items: flex-start; }
.sig label.lbl input { margin-top: 3px; }
.sig .msg { grid-column: 1 / -1; color: var(--muted); font-size: 12px; margin-top: -3px; }
.sig .mini { height: 6px; background: #0f1319; border-radius: 99px; overflow: hidden; }
.sig .mini > div { height: 100%; width: 0; transition: width .5s, background .5s; }
.sig .val { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; }
.sig.off { opacity: .45; }

/* files */
.drop { border: 2px dashed #2c3743; border-radius: var(--radius); padding: 22px; text-align: center; color: var(--muted); transition: .15s; }
.drop.over { border-color: var(--accent); background: #10202c; color: var(--text); }
.files { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; margin-top: 14px; }
.file { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.file .img { aspect-ratio: 16 / 10; background: #0f1319 center/contain no-repeat; border-bottom: 1px solid var(--line); }
.file .body { padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.file .fname { font-weight: 600; word-break: break-all; }
.file .meta { color: var(--muted); font-size: 12px; display: flex; gap: 10px; flex-wrap: wrap; }
.file .actions { display: flex; gap: 6px; margin-top: auto; }

/* control */
.jog { display: grid; grid-template-columns: repeat(3, 58px); grid-template-rows: repeat(3, 58px); gap: 6px; }
.jog button, .zjog button { font-weight: 700; }
.zjog { display: grid; grid-template-rows: repeat(3, 58px); gap: 6px; width: 70px; }
.steps button.on { background: var(--accent2); border-color: var(--accent2); color: #fff; }

/* terminal */
.term { height: calc(100vh - 230px); min-height: 300px; overflow: auto; background: #07090c; border: 1px solid var(--line);
  border-radius: 10px; padding: 10px; font: 12px/1.5 var(--mono); white-space: pre-wrap; word-break: break-all; }
.term .send { color: #9ad1ff; } .term .recv { color: #c9d1d9; } .term .info { color: var(--warn); }

/* tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); font-size: 13px; vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 11.5px; border: 1px solid var(--line); }
.badge.done { color: var(--ok); border-color: #1f5a40; } .badge.failed, .badge.ai_pause { color: var(--bad); border-color: #6b2a2d; }
.badge.cancelled, .badge.ai_warning { color: var(--warn); border-color: #6a5223; }

.events { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.event { background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.event img { width: 100%; display: block; aspect-ratio: 4 / 3; object-fit: cover; background: #07090c; }
.event .b { padding: 8px 10px; font-size: 12.5px; display: flex; flex-direction: column; gap: 6px; }

/* forms */
.form { display: grid; grid-template-columns: 180px 1fr; gap: 10px 14px; align-items: center; }
.form > label { text-align: right; }
.form .help { grid-column: 2; color: var(--muted); font-size: 12px; margin-top: -6px; }
.cam-cfg { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 10px; background: var(--panel2); }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(4, 6, 9, .72); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px; width: min(560px, 100%);
  max-height: calc(100vh - 32px); overflow: auto; }
.modal.wide { width: min(1100px, 100%); }
.modal h2 { margin-bottom: 8px; }
.calib-stage { position: relative; user-select: none; touch-action: none; background: #000; border-radius: 10px; overflow: hidden; }
.calib-stage img { width: 100%; display: block; }
.handle { position: absolute; width: 26px; height: 26px; margin: -13px 0 0 -13px; border-radius: 50%; border: 2px solid #fff;
  background: rgba(76, 194, 255, .35); cursor: grab; display: grid; place-items: center; font-size: 10px; font-weight: 700; color: #fff;
  box-shadow: 0 0 0 2px rgba(0, 0, 0, .5); }
.handle::after { content: ""; position: absolute; width: 4px; height: 4px; border-radius: 50%; background: #ff3b3b; }
.handle span { position: absolute; top: 24px; white-space: nowrap; background: rgba(0,0,0,.7); padding: 1px 5px; border-radius: 4px; }
.loupe { position: absolute; width: 130px; height: 130px; border-radius: 50%; border: 2px solid #fff; pointer-events: none; display: none;
  box-shadow: 0 4px 18px rgba(0,0,0,.6); overflow: hidden; }
.toast-box { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--panel2); border: 1px solid var(--line); border-left: 4px solid var(--accent); padding: 10px 14px; border-radius: 9px;
  max-width: 360px; box-shadow: 0 6px 20px rgba(0,0,0,.45); }
.toast.bad { border-left-color: var(--bad); } .toast.warn { border-left-color: var(--warn); } .toast.ok { border-left-color: var(--ok); }
.login { max-width: 340px; margin: 12vh auto; }

@media (max-width: 1100px) { .dash { grid-template-columns: 1fr; } }
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 52px 1fr 60px; }
  header { padding: 0 12px; gap: 8px; }
  .hdr-temps, .brand .sub { display: none; }
  nav { grid-row: 3; flex-direction: row; border-right: 0; border-top: 1px solid var(--line); padding: 4px 2px; gap: 0; }
  nav button { flex: 1 1 0; min-width: 0; flex-direction: column; gap: 2px; font-size: 9.5px; padding: 6px 1px; }
  nav button span { display: block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  nav .estop { margin: 0; padding: 6px 2px; font-size: 11px; flex: 0 0 44px; }
  .dash > .col:last-child { order: -1; }
  main { padding: 12px; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .form { grid-template-columns: 1fr; } .form > label { text-align: left; } .form .help { grid-column: 1; }
  .cam-grid { grid-template-columns: 1fr; }
  .temp-row { grid-template-columns: 60px 1fr 78px; } .temp-row .presets { grid-column: 1 / -1; }
}
