@font-face {
  font-family: "Noto Sans Thai";
  src: url("fonts/NotoSansThai-Regular.ttf") format("truetype");
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: "Noto Sans Thai";
  src: url("fonts/NotoSansThai-Bold.ttf") format("truetype");
  font-weight: 700;
  font-display: swap;
}

:root {
  --ink: #1a2b4a;
  --accent: #c0392b;
  --field-idle-border: rgba(21, 101, 192, 0);
  --field-hover-border: rgba(21, 101, 192, 0.55);
  --field-focus-bg: rgba(255, 244, 176, 0.55);
  --field-filled-bg: rgba(255, 255, 255, 0);
  --sidebar-w: 320px;
  --topbar-control-h: 42px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --fab-bottom: max(24px, var(--safe-bottom));
  --fields-fab-bottom: max(28px, var(--safe-bottom));
  --sidebar-bottom: max(90px, var(--safe-bottom));
}

* { box-sizing: border-box; }

html {
  width: 100%;
  height: calc(100vh + var(--safe-bottom));
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  width: 100%;
  height: calc(100vh + var(--safe-bottom));
  overflow: hidden;
  overscroll-behavior: none;
  font-family: -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  background: #eef0f3;
  color: #222;
}

@supports (height: 100dvh) {
  html,
  body {
    height: calc(100dvh + var(--safe-bottom));
  }
}

/* ---------------- Floating top bar ---------------- */
/* Three independent floating "pill" groups instead of one solid bar — the
   container itself is click-through (pointer-events:none) so it never
   blocks the canvas beneath it; each pill re-enables pointer events for
   itself. Nothing here touches the diagram's own styling (sheet, fields,
   wires layer, toggles, etc). */
.float-topbar {
  position: fixed;
  top: calc(16px + var(--safe-top));
  left: calc(16px + var(--safe-left));
  right: calc(16px + var(--safe-right));
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}

.float-pill {
  pointer-events: auto;
  display: flex;
  align-items: center;
  height: var(--topbar-control-h);
  box-sizing: border-box;
  gap: 8px;
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: blur(14px) saturate(1.4);
  -webkit-backdrop-filter: blur(14px) saturate(1.4);
  border: 1px solid rgba(15, 23, 42, 0.06);
  border-radius: 999px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.10), 0 1px 2px rgba(15, 23, 42, 0.06);
  padding: 4px 8px;
}

.float-title {
  pointer-events: auto;
  display: flex;
  align-items: center;
  height: var(--topbar-control-h);
  min-height: var(--topbar-control-h);
  box-sizing: border-box;
  gap: 0;
  padding: 0 10px 0 0;
  background: #1d213e;
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.18);
  overflow: hidden;
  text-decoration: none;
  max-width: 40vw;
}
.float-title-logo {
  display: block;
  width: auto;
  height: 100%;
  border-radius: 0;
  box-shadow: none;
  flex: none;
}
.float-title-text {
  color: #fff;
  font-size: 10.5px;
  font-weight: 800;
  line-height: 1.06;
  text-align: left;
  white-space: nowrap;
  letter-spacing: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
}

.float-pagetabs {
  padding: 4px;
  gap: 2px;
  background: rgba(241, 243, 248, 0.9);
}
.float-pagetab {
  border: none;
  background: none;
  color: #667;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 12.5px;
  font-weight: 600;
  padding: 0 16px;
  border-radius: 999px;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s ease, color 0.15s ease;
}
.float-pagetab.active {
  background: var(--ink);
  color: #fff;
  box-shadow: 0 2px 8px rgba(26, 43, 74, 0.3);
}

.float-actions {
  padding: 4px 8px;
}
.float-btn {
  border: none;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  font-size: 12.5px;
  font-weight: 700;
  padding: 0 18px;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease;
}
.float-btn:active {
  transform: scale(0.96);
}
.float-btn-primary {
  background: linear-gradient(135deg, #2e9e4f, #227a3d);
  color: #fff;
  box-shadow: 0 4px 14px rgba(46, 158, 79, 0.35);
}
.float-btn-primary:hover {
  box-shadow: 0 6px 18px rgba(46, 158, 79, 0.45);
  transform: translateY(-1px);
}
.float-btn-primary:disabled {
  opacity: 0.6;
  cursor: default;
  transform: none;
}
.float-btn-secondary {
  background: #eceff5;
  color: #444;
}
.float-btn-secondary:hover {
  background: #e3e7f0;
}
.float-btn-icon {
  padding: 0 10px;
}
.float-btn-save {
  background: var(--ink);
  color: #fff;
}
.float-btn-save:hover:not(:disabled) {
  box-shadow: 0 6px 18px rgba(26, 43, 74, 0.35);
  transform: translateY(-1px);
}
.float-btn-save:disabled {
  background: #e4e6ee;
  color: #9aa1b5;
  cursor: default;
}

.float-title,
.float-title *,
.float-pill,
.float-pill *,
.fab-fit,
.fab-fit *,
.fab-fields,
.fab-fields *,
.sidebar-header,
.sidebar-nav-label,
.sidebar-page-tab,
.sidebar-tab,
.sidebar-section-title,
.sidebar-group-header,
.sidebar-group-header *,
.sidebar-row-label,
.sidebar-row-switch-line span,
.sidebar-field-label,
.modal-actions button {
  user-select: none;
  -webkit-user-select: none;
}

/* Floating action button that opens the sidebar — deliberately separate
   from the top bar, bottom-right, classic FAB placement. */
.fab-fields,
.fab-fit {
  position: fixed;
  bottom: var(--fab-bottom);
  z-index: 40;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  background: var(--ink);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 10px 28px rgba(26, 43, 74, 0.4), 0 2px 6px rgba(26, 43, 74, 0.25);
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.fab-fields {
  right: calc(28px + var(--safe-right));
  bottom: var(--fields-fab-bottom);
  box-shadow: 0 8px 18px rgba(26, 43, 74, 0.28), 0 1px 4px rgba(26, 43, 74, 0.18);
}
.fab-fit {
  left: calc(24px + var(--safe-left));
  background: #fff;
  color: var(--ink);
  border: 1px solid rgba(15, 23, 42, 0.08);
  box-shadow: 0 10px 28px rgba(15, 23, 42, 0.16), 0 2px 6px rgba(15, 23, 42, 0.12);
}
.fab-fields:hover,
.fab-fit:hover {
  transform: translateY(-2px) scale(1.04);
  box-shadow: 0 14px 32px rgba(26, 43, 74, 0.45), 0 2px 6px rgba(26, 43, 74, 0.3);
}
.fab-fields:hover {
  box-shadow: 0 10px 22px rgba(26, 43, 74, 0.32), 0 2px 5px rgba(26, 43, 74, 0.2);
}
.fab-fit:hover {
  box-shadow: 0 14px 32px rgba(15, 23, 42, 0.22), 0 2px 6px rgba(15, 23, 42, 0.14);
}
.fab-fields.active {
  background: #f46e16;
  box-shadow: 0 8px 18px rgba(244, 110, 22, 0.32), 0 1px 4px rgba(244, 110, 22, 0.18);
}
.fab-icon {
  flex: none;
}
.fab-icon-close {
  display: none;
}
.fab-fields.active .fab-icon-menu {
  display: none;
}
.fab-fields.active .fab-icon-close {
  display: block;
}

.body-wrap {
  display: flex;
  align-items: flex-start;
  width: 100vw;
  height: calc(100vh + var(--safe-bottom));
  overflow: hidden;
}

.sheet-wrap {
  /* the sidebar is a fixed overlay again (doesn't consume flex space), so
     this doesn't need an asymmetric right edge for it anymore */
  flex: 1;
  min-width: 0;
  width: 100%;
  height: calc(100vh + var(--safe-bottom));
  overflow: hidden;
  position: relative;
  touch-action: none;
}

@supports (height: 100dvh) {
  .body-wrap,
  .sheet-wrap {
    height: calc(100dvh + var(--safe-bottom));
  }
}
.sheet-wrap.panning {
  cursor: grabbing;
}
.sheet-wrap.panning,
.sheet-wrap.panning * {
  user-select: none;
  -webkit-user-select: none;
}

/* Everything that used to be plain CSS padding/centering on .sheet-wrap is
   now a JS-driven transform on this wrapper instead (see setupPanZoom() in
   app.js) — translate/scale are set directly as inline style so the
   diagram can be freely panned and zoomed with trackpad/mouse/touch. The
   .sheet element itself (below) is completely untouched by this: scaling
   a parent scales every descendant — fields, wires, toggle dots — as one
   rigid unit, so none of the diagram's own pt-based geometry needs to
   know or care that a zoom is happening. */
.sheet-viewport {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
}

/* The canvas is sized in physical units (pt), matching the original PDF
   page exactly (1190.55 x 841.89 pt = A3 landscape). This is deliberate:
   it must NEVER shrink to fit the viewport or make room for the sidebar —
   every field position, font size, and toggle-dot size is computed as a
   fraction of this box, and the final output is a PDF export, so the box
   has to stay true to the real page at all times. Pan/zoom (see
   .sheet-viewport above) is how narrower windows or a partially-covering
   sidebar still reach every part of the canvas, instead of the old
   scrollbar-based fallback. */
.sheet {
  position: relative;
  width: 1190.55pt;
  height: 841.89pt;
  background: #fff;
  box-shadow: 0 4px 24px rgba(0,0,0,0.18);
}

.sheet .bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
  user-select: none;
}

.fields-layer {
  position: absolute;
  inset: 0;
}

.wires-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.wires-layer polyline {
  fill: none;
  stroke-width: 1;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.wires-layer polyline.role-I { stroke: #00adef; }
.wires-layer polyline.role-O { stroke: #f46e16; }
.wires-layer polyline.role-COM { stroke: #e6191c; }
.wires-layer .role-O-arrow { fill: #231f20; }

/* Page 1 Low Voltage — CN1-24 common-section numbered terminals: exact
   match to the calibration tool, 6px, sharp-cornered blue square for
   Input, circle for Output. Dedicated to this zone only — do not resize
   via a shared/modifier class, see .p2-toggle below for why. */
.cn-toggle {
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  cursor: pointer;
  border: 1px solid #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.5), 0 1px 3px rgba(0,0,0,0.4);
  background: #111111;
  z-index: 5;
}
.cn-toggle.on { background: #f46e16; }
.cn-toggle.input { border-radius: 0; }
.cn-toggle.input.on { background: #00adef; }

/* Page 2's 3 leader-line toggles — a fully independent class from
   .cn-toggle (Page 1 Low Voltage) and .calib-dot (Page 1 High Voltage),
   not a size modifier layered on top of either one. Changing this zone's
   size/shape can never bleed into the other two, and vice versa. */
.p2-toggle {
  position: absolute;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.5), 0 1px 3px rgba(0,0,0,0.4);
  background: #111111;
  z-index: 5;
}
.p2-toggle.on { background: #f46e16; }
.p2-toggle.input { border-radius: 0; }
.p2-toggle.input.on { background: #00adef; }

.field-outer {
  position: absolute;
}

.field-outer.unused .field-input {
  display: none;
}

/* Matches the calibration tool's dot exactly: same size, same color, same
   centering. This is a toggle only — no text box, no wire, not approved yet. */
.calib-dot {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.5), 0 1px 3px rgba(0,0,0,0.4);
  background: #111111;
  z-index: 5;
}
.calib-dot.on {
  background: #f46e16;
}

.field-input {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  resize: none;
  background: var(--field-filled-bg);
  font-family: "Noto Sans Thai", -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  color: #110f0d;
  padding: 0 1px;
  box-shadow: 0 0 0 1px var(--field-idle-border) inset;
  border-radius: 2px;
  overflow: hidden;
  white-space: nowrap;
  z-index: 1;
}

.field-input:hover {
  box-shadow: 0 0 0 1px var(--field-hover-border) inset;
  z-index: 2;
}

/* Diagram-side fields are display-only now (typing happens in the popup /
   sidebar), so no dashed "click here" hint and no pointer/focus affordances. */
.field-input.display-only {
  pointer-events: none;
  cursor: default;
  user-select: none;
  -webkit-user-select: none;
}

/* The dashed empty-field hint is reserved for the header fields (Project
   Name, Room Type, dates, Project Number) — diagram fields never show it. */
.field-input.header-field.empty {
  outline: 1px dashed #b0b6c2;
  outline-offset: 0;
}

.field-input:focus {
  background: var(--field-focus-bg);
  box-shadow: 0 0 0 1.5px var(--accent) inset;
  z-index: 3;
}

.field-input.header-field {
  color: #2e3192;
}

.field-input.rotated {
  display: block; /* flex+vertical-rl don't mix reliably; keep this one block-based */
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  text-align: left;
  width: 100%;
  height: 100%;
  white-space: nowrap;
  overflow: hidden;
}

.field-input.rotated.reverse {
  text-align: right;
}

.field-input.multiline {
  width: 100%;
  white-space: pre-line;
  overflow: hidden;
}

/* Native date-picker header fields */
.field-input.date-input {
  border: none;
  background: transparent;
  font: inherit;
  color: #2e3192;
  padding: 0;
}

/* ---------------- Custom modal (title + textbox popup) ---------------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 25, 40, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.modal-overlay[hidden] { display: none; }

.modal-box {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.3);
  width: min(360px, 92vw);
  padding: 18px 20px;
}
.modal-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: 12px;
}
.modal-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.modal-title-row .modal-title {
  margin-bottom: 0;
}
.history-list {
  max-height: 320px;
  overflow-y: auto;
}
.history-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 2px;
  border-bottom: 1px solid #eceff5;
  font-size: 13px;
}
.history-row:last-child {
  border-bottom: none;
}
.history-row-user {
  font-weight: 600;
  color: var(--ink);
}
.history-row-time {
  color: #8a90a3;
  font-size: 12px;
  white-space: nowrap;
}
.history-empty {
  color: #8a90a3;
  font-size: 13px;
  padding: 12px 2px;
}
.modal-input {
  width: 100%;
  font-size: 14px;
  padding: 8px 10px;
  border: 1.5px solid #c7cde0;
  border-radius: 6px;
  outline: none;
  font-family: "Noto Sans Thai", -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
}
.modal-input:focus {
  border-color: var(--ink);
}
.modal-hint {
  font-size: 11px;
  color: #8a90a3;
  margin-top: 6px;
}
.modal-message {
  color: #555b6d;
  font-size: 13px;
  line-height: 1.45;
}
.modal-message.is-error {
  color: var(--accent);
  font-weight: 600;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}
.modal-actions button {
  padding: 7px 16px;
  border-radius: 6px;
  border: none;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.modal-cancel {
  background: #eceff5;
  color: #444;
}
.modal-save {
  background: var(--ink);
  color: #fff;
}
.modal-danger {
  background: #c0392b;
  color: #fff;
}
.modal-danger:hover {
  background: #a93226;
}
.export-warning-box {
  width: min(420px, 92vw);
}
.export-warning-actions {
  justify-content: space-between;
}

/* ---------------- Sidebar ---------------- */
/* True overlay again — confirmed by testing both ways that this is the
   only version that doesn't have rendering glitches: it never touches
   .sheet-wrap's width, so opening/closing it never triggers a flex-layout
   recalculation on the canvas side (the push-based version animated
   width/margin directly, which forces Safari to recompute layout on every
   frame and was flashing the body's gray background mid-transition).
   Mitigates "hides the diagram" with translucency (matching the glassy
   top bar) and a slightly narrower width — plus the diagram is still
   horizontally scrollable underneath, so anything covered can be scrolled
   into view. Slides with transform only (compositor-only, no reflow). */
.sidebar {
  width: 300px;
  position: fixed;
  right: calc(16px + var(--safe-right));
  bottom: var(--sidebar-bottom);
  height: min(775px, calc(100vh - 186px - var(--safe-top)));
  z-index: 45;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(20px) saturate(1.3);
  -webkit-backdrop-filter: blur(20px) saturate(1.3);
  border-radius: 18px;
  border: 1px solid rgba(15, 23, 42, 0.06);
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.22), 0 2px 8px rgba(15, 23, 42, 0.08);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 1;
  transform: translateX(0);
  will-change: transform, opacity;
  transition:
    transform 0.22s cubic-bezier(0.32, 0.72, 0, 1),
    opacity 0.22s cubic-bezier(0.32, 0.72, 0, 1);
}

@supports (height: 100dvh) {
  .sidebar {
    height: min(775px, calc(100dvh - 186px - var(--safe-top)));
  }
}
.sidebar.collapsed {
  transform: translateX(calc(100% + 32px));
  opacity: 0;
  pointer-events: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid #eceff5;
  font-weight: 700;
  color: var(--ink);
  font-size: 14px;
  flex: none;
  white-space: nowrap;
  /* matches .sidebar's own radius instead of relying purely on its
     overflow:hidden to clip a sharp-cornered child — clipping a rectangular
     child against a rounded parent is a known spot for Safari to render a
     thin seam showing whatever's behind, right at the rounded edge */
  border-top-left-radius: 18px;
  border-top-right-radius: 18px;
  background: #fff;
  user-select: none;
  -webkit-user-select: none;
}
.sidebar.is-dragging,
.sidebar.is-dragging * {
  user-select: none;
  -webkit-user-select: none;
}
.sidebar-close-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: #f1f3f8;
  color: #667;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease;
}
.sidebar-close-btn:hover {
  background: #e3e7f0;
  color: var(--ink);
}

/* Everything below the fixed header scrolls together as one column:
   Project Details, then the High/Low Voltage tabs and their body. */
.sidebar-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  border-bottom-left-radius: 18px;
  border-bottom-right-radius: 18px;
  background: #fff;
}

#projectDetailsGroup {
  flex: none;
}
/* Make it unmistakably a collapsible section, not just a static heading —
   bigger/bolder caret, a hover state, and a bit more visual weight than
   the plainer HV/LV group headers. */
#projectDetailsGroup .sidebar-group-header {
  background: #f5f6fa;
  padding: 12px 10px;
  font-size: 13px;
  border-bottom: 1px solid #dde1ea;
}
#projectDetailsGroup .sidebar-group-header:hover {
  background: #eceff5;
}
#projectDetailsGroup .sidebar-group-caret {
  font-size: 13px;
  font-weight: 900;
  color: var(--ink);
}
#projectDetailsGroup .sidebar-group-body {
  padding: 14px 10px 6px;
}

/* Material-style floating label — Project Details only. Label sits inside
   the box like a placeholder until the input has a value or focus, then
   floats up to sit on the border, matching the reference Google sign-up
   form. Driven by :placeholder-shown (needs input.placeholder=" " in the
   markup); date inputs get the always-floated variant instead since a
   native date input is never meaningfully "empty" (defaults to today). */
.pd-field {
  position: relative;
  margin-bottom: 16px;
}
.pd-field:last-child {
  margin-bottom: 4px;
}
.pd-input {
  width: 100%;
  box-sizing: border-box;
  font-size: 13px;
  padding: 13px 10px 9px;
  border: 1.5px solid #c7cde0;
  border-radius: 6px;
  background: #fff;
  font-family: "Noto Sans Thai", -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  color: #222;
}
.pd-input:focus {
  outline: none;
  border-color: var(--ink);
}
.pd-label {
  position: absolute;
  left: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 13px;
  color: #8a90a3;
  background: #fff;
  padding: 0 4px;
  pointer-events: none;
  transition: top 0.12s ease, font-size 0.12s ease, color 0.12s ease;
  max-width: calc(100% - 16px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pd-input:focus + .pd-label,
.pd-input:not(:placeholder-shown) + .pd-label,
.pd-label.pd-label-float {
  top: 0;
  font-size: 10.5px;
  transform: translateY(-50%);
}
.pd-input:focus + .pd-label {
  color: var(--ink);
}

.sidebar-nav-label {
  flex: none;
  padding: 12px 14px 6px;
  color: #7a8193;
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 0.8px;
  line-height: 1;
  text-transform: uppercase;
  background: #fff;
}
.sidebar-nav-label[hidden] {
  display: none;
}

/* Primary page navigation. This changes the visible drawing page too, so it
   is intentionally larger and stronger than the Field Type control below. */
.sidebar-page-tabs {
  display: flex;
  gap: 4px;
  padding: 0 12px 10px;
  flex: none;
  position: sticky;
  top: 0;
  background: #fff;
  z-index: 3;
}
.sidebar-page-tab {
  flex: 1;
  padding: 10px 6px;
  border: none;
  border-radius: 8px;
  background: #eef1f6;
  font-size: 13px;
  font-weight: 700;
  color: #667;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}
.sidebar-page-tab.active {
  background: var(--ink);
  color: #fff;
}

.sidebar-tabs {
  display: flex;
  gap: 6px;
  padding: 0 12px 10px;
  flex: none;
  position: sticky;
  top: 69px;
  z-index: 2;
  background: #fff;
}
.sidebar-tabs[hidden] {
  display: none;
}
.sidebar-tab {
  flex: 1;
  padding: 8px 6px;
  border: 1px solid #dde1ea;
  border-radius: 8px;
  background: #fff;
  font-size: 12.5px;
  font-weight: 600;
  color: #667;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
  -webkit-user-select: none;
}
.sidebar-tab.active {
  background: #eef1f6;
  color: var(--ink);
  border-color: rgba(26, 43, 74, 0.28);
  box-shadow: inset 0 0 0 1px rgba(26, 43, 74, 0.08);
}

.sidebar-section-title {
  flex: none;
  padding: 2px 14px 10px;
  color: var(--ink);
  font-size: 13px;
  font-weight: 800;
  background: #fff;
}
.sidebar-section-title[hidden] {
  display: none;
}

.sidebar-body {
  flex: none;
  padding: 4px 12px;
}

/* Low Voltage fields are grouped by physical CN block, collapsed by
   default, so 24 terminals don't all dump into one long flat list. */
.sidebar-group {
  border-bottom: 1px solid #eceff5;
  padding: 2px 0;
}
.sidebar-group-header {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 6px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--ink);
  text-align: left;
  user-select: none;
  -webkit-user-select: none;
}
.sidebar-group-caret {
  display: inline-block;
  font-size: 10px;
  color: #8a90a3;
  transition: transform 0.15s ease;
}
.sidebar-group-header.open .sidebar-group-caret {
  transform: rotate(90deg);
}
.sidebar-group-count {
  margin-left: auto;
  background: #2e7d32;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
}
.sidebar-group-body {
  padding: 4px 0 10px;
}

.sidebar-row {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 13px 10px 15px;
  border-bottom: 1px solid #f1f2f6;
  width: 100%;
  box-sizing: border-box;
  border-radius: 8px;
}
.sidebar-row:hover {
  background: #f8f9fc;
}
.sidebar-group-body .sidebar-row {
  padding: 12px 10px 13px;
  border-bottom: none;
  margin: 2px 0;
}
.sidebar-group-body .sidebar-row:not(:last-child) {
  border-bottom: 1px solid #f1f2f6;
}
.sidebar-row-top {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
}
.sidebar-row-label {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar-row-switches {
  display: flex;
  align-items: center;
  gap: 14px;
  flex: none;
}
.sidebar-row-switch-line {
  display: flex;
  align-items: center;
  gap: 7px;
}
.sidebar-row-switch-line span {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.3px;
  color: #9aa0b0;
}
.sidebar-row-switch-line.role-i span { color: #00adef; }
.sidebar-row-switch-line.role-o span { color: #f46e16; }
.sidebar-field {
  position: relative;
  width: 100%;
}
.sidebar-field[hidden] {
  display: none;
}
.sidebar-row input.sidebar-textbox {
  width: 100%;
  box-sizing: border-box;
  font-size: 13px;
  padding: 13px 10px 9px;
  border: 1.5px solid #c7cde0;
  border-radius: 6px;
  background: #fff;
  color: #222;
  transition: background 0.12s ease, border-color 0.12s ease;
  font-family: "Noto Sans Thai", -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
}
.sidebar-row input.sidebar-textbox:focus {
  outline: none;
  border-color: var(--ink);
  background: #f8fbff;
}
.sidebar-row input.sidebar-textbox[hidden] {
  display: none;
}
.sidebar-field-label {
  position: absolute;
  left: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 13px;
  color: #8a90a3;
  background: #fff;
  padding: 0 4px;
  pointer-events: none;
  transition: top 0.12s ease, font-size 0.12s ease, color 0.12s ease, background 0.12s ease;
  max-width: calc(100% - 16px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar-row input.sidebar-textbox:focus + .sidebar-field-label,
.sidebar-row input.sidebar-textbox:not(:placeholder-shown) + .sidebar-field-label {
  top: 0;
  font-size: 10.5px;
  transform: translateY(-50%);
}
.sidebar-row input.sidebar-textbox:focus + .sidebar-field-label {
  color: var(--ink);
  background: #f8fbff;
}
/* Scoped to Project Details specifically, not the shared .sidebar-textbox
   rule — so this never bleeds into the High Voltage/Low Voltage rows. */
#projectDetailsBody input.sidebar-textbox {
  font-weight: 500;
}
/* ON but still empty — nudge the user to fill it in or turn it back off */
.sidebar-row input.sidebar-textbox.needs-fill {
  border-color: #e53935;
  background: #fff5f5;
}
.sidebar-row input.sidebar-textbox.needs-fill + .sidebar-field-label {
  color: #c62828;
  background: #fff5f5;
}
.sidebar-row input.sidebar-textbox.needs-fill:focus,
.sidebar-row input.sidebar-textbox.needs-fill:focus + .sidebar-field-label {
  background: #fff5f5;
}
.sidebar-row input.sidebar-textbox.shake {
  animation: sidebar-shake 0.4s ease;
}
@keyframes sidebar-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

/* Custom toggle switch — not a checkbox */
.switch {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
  flex: none;
  cursor: pointer;
}
.switch input { display: none; }
.switch .track {
  position: absolute;
  inset: 0;
  background: #e4e6ee;
  border: 1px solid #cdd2e0;
  border-radius: 999px;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.switch .thumb {
  position: absolute;
  top: 1.5px;
  left: 1.5px;
  width: 15px;
  height: 15px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,0.35);
  transition: transform 0.15s ease;
}
.switch:hover .track {
  border-color: #aeb4c6;
}
.switch input:checked + .track {
  background: #2e7d32;
  border-color: transparent;
}
.switch input:checked + .track .thumb {
  transform: translateX(16px);
}
.switch.role-i input:checked + .track { background: #00adef; }
.switch.role-o input:checked + .track { background: #f46e16; }

@media (max-width: 720px) {
  :root {
    --mobile-drawer-h: min(68vh, 560px);
  }

  .float-topbar {
    align-items: center;
    gap: 8px;
  }

  .float-title-logo {
    width: auto;
    height: 100%;
  }

  .float-title {
    gap: 0;
    height: var(--topbar-control-h);
    min-height: var(--topbar-control-h);
    padding: 0;
  }

  .float-title-text {
    display: none;
  }

  .float-pagetab {
    padding: 0 11px;
  }

  .float-btn {
    padding: 0 14px;
  }

  .modal-input,
  .pd-input,
  .sidebar-row input.sidebar-textbox {
    font-size: 16px;
  }

  .sidebar {
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: calc(var(--mobile-drawer-h) + var(--safe-bottom));
    border-radius: 18px 18px 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
    transform: translateY(0);
    box-shadow: 0 -18px 44px rgba(15, 23, 42, 0.24), 0 -2px 8px rgba(15, 23, 42, 0.08);
  }

  .sidebar.collapsed {
    transform: translateY(calc(100% + 24px + var(--safe-bottom)));
    opacity: 0;
    pointer-events: none;
  }

  .sidebar-header {
    position: relative;
    justify-content: center;
    min-height: 76px;
    padding: 22px 82px 14px;
    border-top-left-radius: 18px;
    border-top-right-radius: 18px;
    touch-action: none;
  }

  .sidebar-header .fab-fields {
    position: absolute;
    top: 50%;
    right: calc(16px + var(--safe-right));
    bottom: auto;
    width: 48px;
    height: 48px;
    transform: translateY(-50%);
    z-index: 1;
  }

  .sidebar-header .fab-fields:hover {
    transform: translateY(-50%) scale(1.04);
  }

  .sidebar-header::before {
    content: "";
    position: absolute;
    top: 8px;
    left: 50%;
    width: 44px;
    height: 4px;
    border-radius: 999px;
    background: #c8ceda;
    transform: translateX(-50%);
  }

  .sidebar-scroll {
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    padding-bottom: var(--safe-bottom);
  }

  .sidebar-page-tabs,
  .sidebar-tabs {
    position: sticky;
    top: 0;
    z-index: 2;
    background: #fff;
  }

  .sidebar-nav-label {
    padding-top: 10px;
  }

  .fab-fields.active {
    bottom: calc(var(--mobile-drawer-h) + max(16px, var(--safe-bottom)));
    z-index: 46;
  }

  .sidebar-header .fab-fields.active {
    bottom: auto;
    z-index: 1;
  }
}

@media print {
  html,
  body {
    overflow: visible;
  }

  .float-topbar, .fab-fields, .fab-fit, .sidebar, .calib-dot, .cn-toggle, .p2-toggle, .modal-overlay {
    display: none !important;
  }
  .body-wrap { display: block; }
  .sheet-wrap { padding: 0; overflow: visible; height: auto; }
  .sheet-viewport { position: static; transform: none !important; }
  .sheet {
    /* keep the true pt size here too — the print engine scales the whole
       page to fit paper, which preserves every relative measurement made
       against it on screen instead of us re-stretching it to "100%" */
    margin: 0;
    box-shadow: none;
    page-break-after: always;
  }
  .sheet[hidden] { display: block !important; }
  input[type="date"]::-webkit-calendar-picker-indicator { display: none; }
}

/* ---------------- Auth / admin / project-list pages ---------------- */
/* Shared minimal-card look for login, admin, and the project list — same
   ink/accent colors and .pd-field/.modal-box language as the diagram app,
   just not living inside the sidebar or a popup overlay. */
body.auth-body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}
@supports (height: 100dvh) {
  body.auth-body { height: 100dvh; }
}

.auth-card {
  width: min(340px, 92vw);
  background: #fff;
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.22), 0 2px 8px rgba(15, 23, 42, 0.08);
}
.auth-card-header {
  background: #1d213e;
  padding: 26px 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.auth-logo {
  height: 38px;
  display: block;
}
.auth-form {
  padding: 24px 22px 22px;
}
.auth-error {
  color: var(--accent);
  font-size: 12px;
  margin: -8px 0 14px;
}
.auth-error[hidden] {
  display: none;
}
.auth-submit {
  width: 100%;
  height: 42px;
  margin-top: 4px;
}
.auth-submit:disabled {
  opacity: 0.6;
  cursor: default;
}

/* Signed-in shell shared by admin.html and projects.html: the same floating
   topbar look as the diagram, plus a small user chip with sign-out. */
.app-body {
  display: block;
  overflow: auto;
  height: auto;
  min-height: 100vh;
}
.app-content {
  max-width: 960px;
  margin: 0 auto;
  padding: calc(96px + var(--safe-top)) 24px 48px;
}
.user-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px 4px 14px;
}
.user-chip-name {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--ink);
  white-space: nowrap;
}
.user-chip-link {
  font-size: 12px;
  font-weight: 600;
  color: #667;
  text-decoration: none;
  white-space: nowrap;
}
.user-chip-link:hover {
  color: var(--ink);
}
.user-chip button.float-btn {
  padding: 0 14px;
}

/* ---------------- Admin: user table ---------------- */
.admin-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.06);
}
.admin-table th,
.admin-table td {
  padding: 12px 14px;
  text-align: left;
  font-size: 13px;
  border-bottom: 1px solid #eceff5;
}
.admin-table th {
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: #7a8193;
  background: #f8f9fc;
}
.admin-table tbody tr {
  cursor: pointer;
  transition: background 0.12s ease;
}
.admin-table tbody tr:hover {
  background: #f8f9fc;
}
.admin-table tr:last-child td {
  border-bottom: none;
}
.admin-col-scope {
  color: #667;
}
.admin-remove-btn {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: none;
  background: #f1f3f8;
  color: #a33;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
}
.admin-remove-btn:hover {
  background: #fbe2e2;
}
.field-hint {
  font-size: 11px;
  color: #8a90a3;
  margin: -10px 0 4px;
}
.admin-modal-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 14px 0 4px;
  font-size: 13px;
  color: var(--ink);
  font-weight: 600;
}

/* ---------------- Projects list ---------------- */
.projects-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 14px;
}
.project-card {
  display: block;
  background: #fff;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.06);
  text-decoration: none;
  color: inherit;
  transition: box-shadow 0.15s ease, transform 0.15s ease;
}
.project-card:hover {
  box-shadow: 0 10px 26px rgba(15, 23, 42, 0.16);
  transform: translateY(-2px);
}
.project-card-name {
  font-size: 14.5px;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: 4px;
}
.project-card-meta {
  font-size: 12px;
  color: #8a90a3;
}
.projects-empty {
  color: #8a90a3;
  font-size: 13px;
  padding: 40px 0;
  text-align: center;
}
