:root {
  /* COLORS */
  --bg-body: #000;

  /* Machine 1 (Blue) */
  --m1-prim: #007acc;
  --m1-dark: #005f8f;
  --m1-bg: #0e151c;
  --m1-border: #3498db;

  /* Machine 2 (Yellow) */
  --m2-prim: #f1c40f;
  --m2-dark: #b7950b;
  --m2-text: #111;
  --m2-bg: #1a1914;
  --m2-border: #f39c12;

  /* Functional */
  --success: #27ae60;
  --danger: #c0392b;
  --ready: #e67e22;
  --disabled: #353b48;
  --text-main: #ffffff;
  --btn-bg: #2d3436;
  --btn-border: #444;

  --header-height: 70px;
  --radius: 6px;
  --panel-gap: 8px;
  --slider-col-width: 95px;
  --slider-thumb-w: 70px;
  --slider-thumb-h: 50px;
  --slider-edge-gap: calc((var(--slider-col-width) - var(--slider-thumb-w)) / 2);
  --jog-btn-size: calc((600px - var(--header-height) - (6 * var(--panel-gap))) / 5);
  --ctrl-col-left: calc(var(--slider-col-width) + (2 * var(--panel-gap)));
  --machine-active-width: 754px;
  --machine-inactive-width: 270px;
}

* { box-sizing: border-box; user-select: none; font-family: 'Segoe UI', Roboto, sans-serif; -webkit-user-drag: none; }

body {
  margin: 0;
  padding: 20px;
  width: 100vw;
  height: 100vh;
  background: #000;
  color: var(--text-main);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

/* --- SCREEN CONTAINER --- */
#hmi-screen {
  width: 1024px;
  height: 600px;
  background: #000;
  position: relative;
  display: flex;
  overflow: hidden;
  border: 1px solid #333;
  box-shadow: 0 0 50px rgba(0,0,0,0.8);
  flex-shrink: 0;
}

/* --- MACHINE CONTAINER --- */
.machine {
  height: 100%;
  display: flex;
  flex-direction: column;
  border-right: 1px solid #222;
  transition: width 0.4s cubic-bezier(0.2, 0.8, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.machine.active { width: var(--machine-active-width); z-index: 10; }
.machine.inactive { width: var(--machine-inactive-width); opacity: 0.5; }

#m1 { background: var(--m1-bg); border-right: 2px solid #222; }
#m2 { background: var(--m2-bg); border-left: 2px solid #222; }

/* --- HEADER --- */
.header {
  height: var(--header-height);
  width: 100%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  position: relative;
  z-index: 20;
  padding: 0 15px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.3);
  overflow: hidden;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  height: 100%;
  position: relative;
  z-index: 1;
}

#m1 .header {
  background: var(--m1-bg);
  color: white;
}
#m2 .header {
  background: var(--m2-bg);
  color: black;
}

#m1 .header::after,
#m2 .header::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  z-index: 0;
}

#m1 .header::after {
  left: var(--ctrl-col-left);
  right: 0;
  background: var(--m1-prim);
  border-bottom-left-radius: var(--radius);
}

#m2 .header::after {
  left: 0;
  right: var(--ctrl-col-left);
  background: var(--m2-prim);
  border-bottom-right-radius: var(--radius);
}

.header-title {
  font-size: 20px;
  font-weight: bold;
  line-height: 1;
  text-transform: uppercase;
  white-space: nowrap;
  opacity: 1;
  transition: opacity 0.2s;
  font-family: 'Segoe UI', sans-serif;
}
.machine.inactive .header-title { opacity: 0; }

.header-status {
  font-size: 20px;
  font-weight: bold;
  line-height: 1;
  text-transform: uppercase;
  min-width: 100px;
  text-align: right;
  font-family: 'Segoe UI', sans-serif;
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
}

#m2 .header-status { right: 210px; }
.machine.inactive .header-status { display: none; }

/* Position Display */
.pos-display {
  position: absolute;
  top: 0;
  height: 100%;
  width: var(--jog-btn-size);
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  z-index: 1;
}
.pos-display:active { background: rgba(0,0,0,0.1); }
#m1 .pos-display { left: var(--ctrl-col-left); }
#m2 .pos-display { right: var(--ctrl-col-left); }

.pos-val {
  font-family: 'Segoe UI', sans-serif;
  font-size: 34px;
  font-weight: bold;
  line-height: 1;
  text-transform: uppercase;
}
.pos-unit {
  font-family: 'Segoe UI', sans-serif;
  font-size: 16px;
  font-weight: normal;
  line-height: 1;
  opacity: 0.8;
  margin-top: 0;
}

/* --- BODY (530px) --- */
.machine-body {
  flex: 1;
  display: flex;
  flex-direction: row;
  height: 100%;
  padding: var(--panel-gap);
  gap: var(--panel-gap);
  position: relative;
  overflow: visible;
}

/* Columns */
.col-slider {
  width: var(--slider-col-width);
  flex: 0 0 var(--slider-col-width);
  position: relative;
  z-index: 25;
  align-self: flex-start;
  height: calc(100% + var(--header-height) + (2 * var(--panel-gap)));
  margin-top: calc(-1 * (var(--header-height) + var(--panel-gap)));
  margin-bottom: calc(-1 * var(--panel-gap));
  --forward-blocked-height: 0%;
  background: transparent;
  border: none;
  overflow: hidden;
}

.col-ctrl {
  width: var(--jog-btn-size);
  flex: 0 0 var(--jog-btn-size);
  display: flex;
  flex-direction: column;
  gap: var(--panel-gap);
  height: 100%;
}

.col-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--panel-gap);
  overflow: hidden;
  height: 100%;
}

/* Order */
#m1 .col-slider { order: 1; }
#m1 .col-ctrl { order: 2; }
#m1 .col-main { order: 3; }

#m2 .col-main { order: 1; }
#m2 .col-ctrl { order: 2; }
#m2 .col-slider { order: 3; }

/* --- SIDEBAR BUTTONS --- */
.ctrl-btn {
  flex: 0 0 var(--jog-btn-size);
  width: 100%;
  min-height: var(--jog-btn-size);
  padding: 0;
  background: var(--btn-bg);
  border: 1px solid var(--btn-border);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
  cursor: pointer;
  transition: background 0.1s;
}
.ctrl-btn:active { background: #555; }
.ctrl-btn svg { display: block; width: 40px; height: 40px; fill: white; }
.ctrl-btn.is-blocked-forward,
.ctrl-btn:disabled.is-blocked-forward {
  background: #9b2d27;
  border-color: #cf3b31;
  cursor: not-allowed;
  opacity: 1;
}
.ctrl-btn.is-blocked-forward svg,
.ctrl-btn:disabled.is-blocked-forward svg {
  fill: #ffd7d3;
}
.ctrl-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.btn-stop {
  background: var(--danger);
  border-color: #922b21;
  flex: 0 0 var(--jog-btn-size);
  width: 100%;
  min-height: var(--jog-btn-size);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #922b21;
  cursor: pointer;
}
.btn-stop:active { background: #7b241c; }
.btn-stop span { font-size: 22px; font-weight: bold; color: white; }

/* --- SLIDER GEOMETRY --- */
.slider-rail {
  position: absolute;
  top: calc(var(--slider-edge-gap) + (var(--slider-thumb-h) / 2));
  bottom: calc(var(--slider-edge-gap) + (var(--slider-thumb-h) / 2));
  left: 50%;
  width: 6px;
  margin-left: -3px;
  background: #444;
  border-radius: 3px;
}
.col-slider.jog-forward-blocked .slider-rail::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: var(--forward-blocked-height);
  background: linear-gradient(180deg, #ff5a4d 0%, #a11e16 100%);
  border-radius: 3px;
  box-shadow: 0 0 8px rgba(255, 69, 58, 0.65);
}
.thumb {
  width: var(--slider-thumb-w);
  height: var(--slider-thumb-h);
  border-radius: 8px;
  position: absolute;
  left: 50%;
  margin-left: calc(var(--slider-thumb-w) / -2);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid rgba(255,255,255,0.5);
  box-shadow: 0 4px 10px rgba(0,0,0,0.8);
}
.thumb-ghost { z-index: 10; cursor: grab; background: #555; }
.thumb-ghost:active { background: white; cursor: grabbing; border-color: white; }
.thumb-ghost svg { width: 24px; height: 24px; fill: rgba(255,255,255,0.8); }
.thumb-actual { z-index: 5; opacity: 0.6; pointer-events: none; transition: bottom 0.1s linear; border: none; }

#m1 .thumb-ghost { background: var(--m1-prim); border-color: white; }
#m1 .thumb-actual { background: var(--m1-dark); }
#m2 .thumb-ghost { background: var(--m2-prim); border-color: #000; }
#m2 .thumb-ghost svg { fill: black; }
#m2 .thumb-actual { background: var(--m2-dark); }

/* --- ACTIVE PANEL CONTENT --- */
.active-view {
  display: none;
  flex-direction: column;
  height: 100%;
  gap: var(--panel-gap);
}
.machine.active .active-view { display: flex; }

/* Dashboard */
.dash-row { display: flex; gap: var(--panel-gap); height: var(--jog-btn-size); flex-shrink: 0; }

.dash-box {
  flex: 1;
  background: rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: var(--radius);
  display: flex;
  flex-direction: row;
  align-items: stretch;
  padding: 0;
  overflow: hidden;
}

.dash-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.dash-lbl { font-size: 11px; color: #aaa; text-transform: uppercase; margin-bottom: 4px; font-weight: bold; }
.dash-val { font-size: 38px; font-weight: bold; font-family: 'Consolas', monospace; color: white; }
.unit { font-size: 16px; color: #888; font-weight: normal; margin-left: 5px; }

.tare-btn {
  width: 70px;
  background: #222;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: bold;
  color: #777;
  cursor: pointer;
  transition: 0.2s;
  flex-shrink: 0;
}
.tare-btn:active { background: #555; color: white; }
.tare-btn.left { border-right: 1px solid #555; }
.tare-btn.right { border-left: 1px solid #555; }
.tare-btn.is-disabled {
  opacity: 0.45;
  cursor: not-allowed;
  pointer-events: none;
}

/* Parameters */
.param-row { display: flex; gap: var(--panel-gap); height: var(--jog-btn-size); flex-shrink: 0; }
.param-box {
  flex: 1;
  background: #333;
  border-radius: var(--radius);
  border: 1px solid #555;
  display: flex;
  flex-direction: row;
  overflow: hidden;
  position: relative;
  align-items: stretch;
}
.param-lbl {
  position: absolute;
  top: 6px;
  width: 100%;
  text-align: center;
  font-size: 11px;
  font-weight: bold;
  color: #aaa;
  text-transform: uppercase;
  letter-spacing: 0;
  z-index: 5;
  pointer-events: none;
}
.param-body { flex: 1; display: flex; align-items: center; justify-content: center; padding-top: 15px; }
.param-val {
  font-size: 34px;
  font-weight: bold;
  color: white;
  cursor: pointer;
  text-align: center;
  width: 100%;
  font-family: 'Consolas', monospace;
}

.toggle-btn { width: 70px; background: #222; border-color: #555; display: flex; flex-direction: column; cursor: pointer; height: 100%; flex-shrink: 0; }
.toggle-left { border-right: 1px solid #555; }
.toggle-right { border-left: 1px solid #555; }

.t-opt { flex: 1; display: flex; align-items: center; justify-content: center; font-size: 13px; color: #777; font-weight: bold; transition: 0.2s; }
.t-opt.active { background: #fff; color: #000; font-weight: 800; }

/* Graph */
.graph-area {
  flex: 0 0 calc((2 * var(--jog-btn-size)) + var(--panel-gap));
  min-height: calc((2 * var(--jog-btn-size)) + var(--panel-gap));
  background: rgba(0,0,0,0.5);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: var(--radius);
  position: relative;
  overflow: hidden;
}
canvas { width: 100%; height: 100%; }

/* Bottom Buttons */
.btn-row {
  height: var(--jog-btn-size);
  display: flex;
  gap: var(--panel-gap);
  flex-shrink: 0;
  margin-bottom: 0px;
}

.btn-big {
  flex: 1;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 20px;
  font-weight: bold;
  text-transform: uppercase;
  color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--btn-bg);
  border: 1px solid var(--btn-border);
  font-family: 'Segoe UI', sans-serif;
  position: relative;
  overflow: hidden;
}
.btn-big:active { transform: scale(0.98); }
.btn-big:disabled,
.btn-big.is-disabled {
  opacity: 0.45;
  cursor: not-allowed;
  pointer-events: none;
}

.b-lib { background: var(--btn-bg); }
.b-lib.active { background: var(--ready); color: black; }

.b-start { background: var(--success); }
.b-end { background: var(--danger); }

.b-home-grey { background: var(--btn-bg); color: #888; }
.b-home-orange { background: var(--ready); color: black; }

.btn-sub { font-size: 10px; opacity: 0.8; font-weight: normal; margin-top: 4px; }
.hold-overlay { position: absolute; left: 0; top: 0; height: 100%; width: 0%; background: rgba(255,255,255,0.3); pointer-events: none; }

/* --- INACTIVE VIEW (ACTIVATION STRIP) --- */
.inactive-view {
  display: none;
  flex-direction: column;
  flex: 1;
  height: 100%;
  align-items: center;
  justify-content: center;
  text-align: center;
  cursor: pointer;
  transition: background 0.2s;
  background: rgba(255, 255, 255, 0.02);
  padding: var(--panel-gap);
}
.inactive-view:hover { background: rgba(255, 255, 255, 0.08); }
.machine.inactive .inactive-view { display: flex; }

.v-container {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  transform: rotate(180deg);
  display: flex;
  align-items: center;
  gap: var(--panel-gap);
  white-space: nowrap;
}

.v-text {
  font-family: 'Segoe UI', Roboto, sans-serif;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 2px;
}

#m1 .v-text { color: var(--m1-prim); text-shadow: 0 0 10px rgba(0, 122, 204, 0.4); }
#m2 .v-text { color: var(--m2-prim); text-shadow: 0 0 10px rgba(241, 196, 15, 0.4); }

/* --- LIBRARY OVERLAY --- */
.lib-overlay {
  position: absolute;
  top: var(--header-height);
  left: 0;
  width: 100%;
  height: calc(600px - var(--header-height));
  background: rgba(10, 10, 10, 0.98);
  z-index: 60;
  display: none;
  flex-direction: column;
  padding: 20px;
}
.lib-header {
  color: white;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 20px;
  border-bottom: 1px solid #444;
  padding-bottom: 10px;
  display: flex;
  justify-content: space-between;
}
.lib-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; overflow-y: auto; flex: 1; }

.lib-tile {
  height: 180px;
  background: #333;
  border: 1px solid #555;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: bold;
  color: #ccc;
  cursor: pointer;
}
.lib-tile.special { border: 2px solid var(--success); color: var(--success); background: rgba(39, 174, 96, 0.1); }
.tile-sub { font-size: 13px; color: #888; margin-top: 8px; font-weight: normal; text-transform: uppercase; }

.lib-modal {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 500px;
  height: 400px;
  background: #222;
  border: 2px solid #555;
  border-radius: 10px;
  display: none;
  flex-direction: column;
  padding: 20px;
  z-index: 70;
  box-shadow: 0 0 50px black;
}
.modal-sub {
  font-size: 14px;
  color: #aaa;
  text-align: center;
  margin-bottom: 15px;
  margin-top: -15px;
  text-transform: uppercase;
  font-weight: bold;
}

.lib-row { margin-bottom: 20px; }
.lib-val-edit { background:#111; padding:15px; text-align:center; font-size:24px; font-weight:bold; border:1px solid #444; color: white; cursor: pointer; }
.lib-label { color: #888; font-size: 12px; margin-bottom: 5px; font-weight: bold; display: block; }

.opt-row { display: flex; gap: 10px; }
.opt-btn { flex: 1; padding: 15px; background: #333; border: 1px solid #555; color: #ccc; font-weight: bold; text-align: center; cursor: pointer; }
.opt-btn.sel { background: var(--success); color: white; border-color: var(--success); }

.confirm-btn { margin-top: auto; padding: 20px; background: var(--success); color: white; font-weight: bold; font-size: 20px; border: none; border-radius: 6px; width: 100%; cursor: pointer; }

/* Scroller */
.scroller-overlay { position: absolute; top:0; left:0; width:100%; height:100%; background: rgba(0,0,0,0.95); z-index: 80; display: none; justify-content: center; align-items: center; }
.scroller-box { width: 300px; height: 500px; background: #222; border: 2px solid #555; border-radius: 12px; display: flex; flex-direction: column; }
.scroller-list { flex: 1; overflow-y: auto; }
.scroller-item { padding: 20px; text-align: center; border-bottom: 1px solid #444; font-size: 28px; font-weight: bold; cursor: pointer; color: #888; }

/* Numpad */
.numpad-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.93);
  z-index: 95;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 18px;
}

.numpad-window {
  width: min(380px, calc(100% - 24px));
  height: calc(100% - 8px);
  background: #1c1c1c;
  border: 2px solid #4b4b4b;
  border-radius: 12px;
  box-shadow: 0 0 50px rgba(0, 0, 0, 0.9);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.numpad-window.with-presets {
  width: min(760px, calc(100% - 24px));
}

.numpad-content {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: row;
  gap: 12px;
}

.numpad-title {
  font-size: 14px;
  font-weight: 700;
  color: #9d9d9d;
  text-align: center;
  letter-spacing: 0.6px;
  text-transform: uppercase;
}

.numpad-display {
  height: 86px;
  border: 1px solid #575757;
  background: #0f0f0f;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 10px;
  border-radius: 8px;
}

.numpad-clear-btn {
  width: 72px;
  height: 100%;
  border: 1px solid #9b2d27;
  border-radius: 6px;
  background: #c0392b;
  color: #fff;
  font-size: 34px;
  font-weight: 800;
  cursor: pointer;
}

.numpad-clear-btn:active {
  background: #992d22;
}

.numpad-display-val {
  flex: 1;
  min-width: 0;
  text-align: right;
  font-size: 48px;
  font-family: 'Consolas', monospace;
  font-weight: 700;
}
.numpad-display-val.numpad-display-invalid {
  color: #ff5a4d;
}

.numpad-grid {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.numpad-key {
  border: 1px solid #666;
  border-radius: 8px;
  background: #2f2f2f;
  color: #fff;
  width: 100%;
  height: 100%;
  min-height: 0;
  font-size: clamp(22px, 5.2vh, 48px);
  font-weight: 700;
  cursor: pointer;
}

.numpad-key:active {
  background: #4a4a4a;
}

.numpad-key-enter {
  background: #23934f;
  border-color: #23934f;
  font-size: clamp(18px, 3.2vh, 30px);
}

.numpad-key-enter:active {
  background: #1b743e;
}

.numpad-key-disabled,
.numpad-key:disabled {
  opacity: 0.35;
  cursor: default;
}

.numpad-key:disabled:active {
  background: #2f2f2f;
}
.numpad-key.numpad-key-enter:disabled {
  background: #8f2f2f;
  border-color: #c64a4a;
  color: #ffd4d4;
  opacity: 1;
  cursor: not-allowed;
}
.numpad-key.numpad-key-enter:disabled:active {
  background: #8f2f2f;
}

.numpad-presets {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: none;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  grid-auto-rows: minmax(0, 1fr);
  gap: 10px;
  align-content: stretch;
  overflow-y: auto;
}

.numpad-window.with-presets .numpad-presets {
  display: grid;
  padding-left: 14px;
  border-left: 2px solid #474747;
}

.numpad-presets-empty {
  display: none;
  width: 0;
  height: 0;
}

.numpad-preset-key {
  font-size: clamp(22px, 4.5vh, 42px);
  background: #2a2a2a;
  border-color: #5e5e5e;
}

.numpad-preset-key:active {
  background: #3d3d3d;
}

.numpad-preset-key.numpad-preset-key-disabled,
.numpad-preset-key:disabled {
  background: #242424;
  border-color: #484848;
  color: #7a7a7a;
  opacity: 1;
  cursor: not-allowed;
}

.numpad-preset-key.numpad-preset-key-disabled:active,
.numpad-preset-key:disabled:active {
  background: #242424;
}

.numpad-window.no-decimal [data-numpad-key="decimal"] {
  display: none;
}

.numpad-window.no-decimal .numpad-key-zero {
  grid-column: span 2;
}

.numpad-window.theme-blue .numpad-presets {
  border-left-color: rgba(52, 152, 219, 0.6);
}

.numpad-window.theme-yellow .numpad-presets {
  border-left-color: rgba(241, 196, 15, 0.7);
}

.numpad-window.theme-blue .numpad-preset-key {
  background: #1d425f;
  border-color: #3c7fab;
  color: #e9f7ff;
}

.numpad-window.theme-blue .numpad-preset-key:active {
  background: #26608a;
}

.numpad-window.theme-blue .numpad-preset-key.numpad-preset-key-disabled,
.numpad-window.theme-blue .numpad-preset-key:disabled {
  background: #273746;
  border-color: #425766;
  color: #8ea3b4;
}

.numpad-window.theme-yellow .numpad-preset-key {
  background: #a4871f;
  border-color: #f1cf53;
  color: #1b1b1b;
}

.numpad-window.theme-yellow .numpad-preset-key:active {
  background: #c9aa2e;
}

.numpad-window.theme-yellow .numpad-preset-key.numpad-preset-key-disabled,
.numpad-window.theme-yellow .numpad-preset-key:disabled {
  background: #5b5230;
  border-color: #8d7b36;
  color: #d1c089;
}

/* Utility */
button { touch-action: none; }
