:root {
  --z-node: 5;
  --z-gate: 10;
  --z-controls: 20;
  --z-header: 50;
  --z-toast: 999;
  --z-modal: 1000;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #f8fafc;
  color: #334155;
  font-family: "Segoe UI", "Microsoft JhengHei", Tahoma, Geneva, Verdana, sans-serif;
}
button, input, select { font: inherit; }

.header {
  z-index: var(--z-header);
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 14px 24px;
  background: #fff;
  box-shadow: 0 4px 10px -2px rgb(0 0 0 / 0.1);
}
.header-title-wrapper { display: flex; align-items: center; gap: 10px; }
.editable-title {
  display: inline-block;
  max-width: min(55vw, 760px);
  padding: 4px 8px;
  border-radius: 4px;
  color: #0f172a;
  font-size: 20px;
  font-weight: 700;
  line-height: 1.35;
  overflow-wrap: anywhere;
  cursor: text;
  transition: background .2s;
}
.editable-title:hover { background: #f1f5f9; }
.editable-title:focus { outline: 2px dashed #3b82f6; background: #f8fafc; }
.toolbar { display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.btn-action {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 8px 14px;
  border: 0;
  border-radius: 6px;
  background: #3b82f6;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
  transition: background .2s, transform .1s;
}
.btn-action:hover { background: #2563eb; }
.btn-action:active { transform: translateY(1px); }
.btn-danger { background: #ef4444; }
.btn-danger:hover { background: #dc2626; }
.btn-save { background: #10b981; }
.btn-save:hover { background: #059669; }
.btn-load { background: #f59e0b; }
.btn-load:hover { background: #d97706; }
.legend { display: flex; align-items: center; gap: 12px; margin-top: 5px; color: #64748b; font-size: 12px; flex-wrap: wrap; }

#editorShell { display: flex; flex: 1 1 auto; min-height: 0; }
.tree-container { flex: 1 1 auto; min-width: 0; overflow: auto; padding: 60px; cursor: grab; -webkit-overflow-scrolling: touch; }
.tree-container:active { cursor: grabbing; }
.tree { display: flex; justify-content: center; min-width: max-content; margin: 0 auto; padding-right: 60px; }
.tree ul { display: flex; justify-content: center; position: relative; padding-top: 40px; }
.tree li { position: relative; padding: 40px 15px 0; list-style: none; text-align: center; }
.tree li::before { content: ""; position: absolute; top: 0; left: 0; width: 100%; border-top: 2px solid #94a3b8; }
.tree li::after { content: ""; position: absolute; top: 0; left: 50%; width: 0; height: 40px; margin-left: -1px; border-left: 2px solid #94a3b8; }
.tree li:first-child::before { left: calc(50% - 1px); width: calc(50% + 1px); height: 40px; border-left: 2px solid #94a3b8; border-radius: 8px 0 0; }
.tree li:first-child::after, .tree li:last-child::after { display: none; }
.tree li:last-child::before { left: 0; width: calc(50% + 1px); height: 40px; border-right: 2px solid #94a3b8; border-radius: 0 8px 0 0; }
.tree li:only-child::before, .tree li:only-child::after { display: none; }
.tree li:only-child { padding-top: 0; }
.tree ul ul::before { content: ""; position: absolute; top: 0; left: 50%; width: 0; height: 40px; margin-left: -1px; border-left: 2px solid #94a3b8; }

.gate-wrapper { position: relative; display: inline-block; padding-bottom: 25px; }
.node {
  position: relative;
  z-index: var(--z-node);
  min-width: 180px;
  max-width: 220px;
  padding: 16px;
  border: 2px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 4px 6px -1px rgb(0 0 0 / .05);
  transition: box-shadow .2s, transform .2s;
}
.node:hover { box-shadow: 0 10px 15px -3px rgb(0 0 0 / .1); }
[contenteditable="true"]:focus { outline: 2px dashed #3b82f6; outline-offset: 4px; border-radius: 4px; background: #f8fafc; }
[contenteditable="true"][data-placeholder]:empty::before { content: attr(data-placeholder); color: #94a3b8; font-weight: 400; pointer-events: none; }
.title { min-height: 22px; margin-bottom: 8px; color: #1e293b; font-size: 16px; font-weight: 700; line-height: 1.3; overflow-wrap: anywhere; }
.desc { min-height: 20px; color: #64748b; font-size: 13px; line-height: 1.5; overflow-wrap: anywhere; }
.node.top { border-color: #ef4444; border-width: 3px; background: #fef2f2; }
.node.top .title { color: #b91c1c; }
.node.branch { border-color: #3b82f6; background: #eff6ff; }
.node.branch .title { color: #1d4ed8; }
.node.critical { border-color: #f97316; border-width: 3px; background: #fff7ed; }
.node.critical .title { color: #c2410c; }
.node.normal { border-color: #cbd5e1; background: #fff; }
.node-controls { display: none; position: absolute; top: -16px; right: -16px; z-index: var(--z-controls); gap: 4px; }
.node:hover .node-controls { display: flex; }
.ctrl-btn { display: inline-flex; align-items: center; justify-content: center; width: 32px; height: 32px; border: 0; border-radius: 50%; color: #fff; font-size: 16px; font-weight: 700; box-shadow: 0 2px 4px rgba(0,0,0,.2); cursor: pointer; transition: transform .1s; }
.ctrl-btn:hover { transform: scale(1.1); }
.btn-add { background: #10b981; }
.btn-del { background: #ef4444; }
.btn-color { background: #8b5cf6; font-size: 14px; }
.gate { position: absolute; bottom: -5px; left: 50%; z-index: var(--z-gate); display: flex; align-items: center; justify-content: center; width: 46px; height: 46px; border-radius: 50%; font-size: 12px; font-weight: 800; box-shadow: 0 2px 5px rgb(0 0 0 / .15); transform: translateX(-50%); cursor: pointer; user-select: none; }
.gate:hover { transform: translateX(-50%) scale(1.1); }
.gate.and { border: 2px solid #eab308; background: #fef08a; color: #a16207; }
.gate.or { border: 2px solid #94a3b8; background: #e2e8f0; color: #475569; }

.instruction-toast { position: fixed; right: 20px; bottom: 20px; z-index: var(--z-toast); max-width: min(520px, calc(100vw - 40px)); padding: 12px 44px 12px 20px; border-radius: 8px; background: rgba(15,23,42,.88); color: #fff; font-size: 13px; line-height: 1.6; box-shadow: 0 10px 15px -3px rgba(0,0,0,.1); }
.instruction-toast span { color: #fbbf24; font-weight: 700; }
.instruction-toast-toggle { position: absolute; top: 8px; right: 8px; display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; border: 1px solid rgba(255,255,255,.35); border-radius: 50%; background: rgba(255,255,255,.12); color: #fff; cursor: pointer; }
.instruction-toast.collapsed { max-width: 52px; padding: 10px; overflow: hidden; }
.instruction-toast.collapsed .instruction-toast-body { display: none; }
.instruction-toast.collapsed .instruction-toast-toggle { position: static; }
.modal-overlay { display: none; position: fixed; inset: 0; z-index: var(--z-modal); align-items: center; justify-content: center; padding: 20px; background: rgba(0,0,0,.5); backdrop-filter: blur(2px); }
.modal-overlay.active { display: flex; }
.modal-box { width: min(430px, 100%); padding: 24px 30px; border-radius: 12px; background: #fff; box-shadow: 0 20px 40px rgba(0,0,0,.2); }
.modal-box h3 { margin-bottom: 12px; color: #0f172a; font-size: 18px; }
.modal-box p { margin-bottom: 22px; color: #475569; font-size: 14px; line-height: 1.65; white-space: pre-line; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; }
.modal-btn { padding: 8px 20px; border: 0; border-radius: 6px; font-size: 14px; font-weight: 700; cursor: pointer; }
.modal-btn-cancel { background: #e2e8f0; color: #334155; }
.modal-btn-confirm { background: #ef4444; color: #fff; }
.modal-btn-ok { background: #3b82f6; color: #fff; }

/* Dedicated print document: measured off-screen, then rendered as a single A4 landscape report. */
#printDocument {
  --print-scale: 1;
  --print-title-size: 25px;
  --print-header-height: 78px;
  --print-node-width: 174px;
  --print-node-pad-x: 13px;
  --print-node-pad-y: 11px;
  --print-h-gap: 9px;
  --print-level-gap: 32px;
  --print-gate-size: 36px;
  display: none;
  color: #334155;
  background: #fff;
  print-color-adjust: exact;
  -webkit-print-color-adjust: exact;
}
#printHeader {
  display: flex;
  min-height: var(--print-header-height);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px 24px 13px;
  border-top: 4px solid #334155;
  border-bottom: 1.5px solid #cbd5e1;
  text-align: center;
}
.print-kicker { margin-bottom: 4px; color: #64748b; font-size: 9px; font-weight: 800; letter-spacing: .18em; }
#printTitle { max-width: 94%; color: #0f172a; font-size: var(--print-title-size); font-weight: 800; line-height: 1.25; overflow-wrap: anywhere; }
#printMeta { margin-top: 5px; color: #64748b; font-size: 10px; font-weight: 600; letter-spacing: .03em; }
#printViewport { position: relative; min-height: 0; overflow: hidden; }
#printTreeHost {
  position: absolute;
  top: 50%;
  left: 50%;
  width: max-content;
  min-width: max-content;
  margin: 0;
  padding: 0;
  transform: translate(-50%, -50%) scale(var(--print-scale));
  transform-origin: center center;
}
#printTreeHost > ul { padding-top: 0; }
#printTreeHost ul { padding-top: var(--print-level-gap); }
#printTreeHost li { padding: var(--print-level-gap) var(--print-h-gap) 0; }
/* A single-child branch has no li pseudo-line. Keep its node directly after the parent ul connector. */
#printTreeHost li:only-child { padding-top: 0; }
#printTreeHost li::after,
#printTreeHost ul ul::before { height: var(--print-level-gap); }
#printTreeHost li:first-child::before,
#printTreeHost li:last-child::before { height: var(--print-level-gap); }
#printTreeHost .node {
  min-width: var(--print-node-width);
  max-width: var(--print-node-width);
  padding: var(--print-node-pad-y) var(--print-node-pad-x);
  border-width: 1.5px;
  border-radius: 6px;
  box-shadow: none;
}
#printTreeHost .node.top,
#printTreeHost .node.critical { border-width: 2px; }
#printTreeHost .title { min-height: 17px; margin-bottom: 5px; font-size: 13px; line-height: 1.25; }
#printTreeHost .desc { min-height: 15px; font-size: 10px; line-height: 1.35; }
#printTreeHost .gate-wrapper { padding-bottom: calc(var(--print-gate-size) * .52); }
#printTreeHost .gate {
  bottom: calc(var(--print-gate-size) * -.12);
  width: var(--print-gate-size);
  height: var(--print-gate-size);
  border-width: 1.5px;
  font-size: 9px;
  box-shadow: none;
}
#printTreeHost .node-controls { display: none !important; }
#printTreeHost .node,
#printTreeHost .gate-wrapper,
#printTreeHost li,
#printTreeHost ul { break-inside: avoid !important; page-break-inside: avoid !important; }
#printFooter { display: flex; align-items: center; justify-content: space-between; min-height: 24px; padding: 5px 6px 0; border-top: 1px solid #e2e8f0; color: #94a3b8; font-size: 8px; font-weight: 700; letter-spacing: .12em; }
.print-placeholder { color: #94a3b8 !important; font-weight: 400 !important; }

#printDocument.print-measure {
  display: block;
  position: fixed;
  top: 0;
  left: -100000px;
  width: var(--print-page-width, 1062px);
  height: auto;
  visibility: hidden;
  pointer-events: none;
}
#printDocument.print-measure #printHeader,
#printDocument.print-measure #printFooter { display: none; }
#printDocument.print-measure #printViewport { position: static; width: max-content; height: auto; overflow: visible; }
#printDocument.print-measure #printTreeHost { position: static; transform: none; }

@media (max-width: 768px) {
  .header { flex-direction: column; align-items: stretch; padding: 12px 15px; gap: 10px; }
  .editable-title { max-width: none; padding: 2px 4px; font-size: 18px; }
  .toolbar { justify-content: flex-start; flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; }
  .legend, .instruction-toast { display: none !important; }
  .tree-container { padding: 30px 15px; }
  .tree { padding-right: 20px; }
  .node { min-width: 140px; max-width: 160px; padding: 12px; }
  .title { font-size: 15px; }
  .desc { font-size: 12px; }
  .node-controls { display: flex; top: -12px; right: -12px; opacity: .95; transform: scale(.85); }
  .gate { width: 40px; height: 40px; bottom: -2px; font-size: 11px; }
}

@page { size: A4 landscape; margin: 8mm; }
@media print {
  html, body { width: auto !important; height: auto !important; overflow: visible !important; background: #fff !important; }
  body > :not(#printDocument) { display: none !important; }
  #printDocument {
    display: grid !important;
    width: 100%;
    height: 193mm;
    max-height: 193mm;
    grid-template-rows: auto minmax(0, 1fr) auto;
    overflow: hidden;
    break-inside: avoid;
    page-break-inside: avoid;
  }
}
