:root{
  --bg:#dfe5ec;
  --bg-2:#edf2f7;
  --panel:#ffffff;
  --panel-2:#f7f9fc;
  --panel-3:#eef3f8;
  --chrome:#24384e;
  --chrome-2:#1c2d40;
  --chrome-3:#132030;
  --line:rgba(46,66,92,0.14);
  --line-strong:rgba(47,127,217,0.42);
  --text:#132133;
  --muted:#5c6c80;
  --accent:#2f7fd9;
  --accent-2:#6ca9e8;
  --warn:#f5c94e;
  --danger:#ff7f99;
  --shadow:0 10px 22px rgba(16,26,38,0.12);
  --radius:12px;
  --radius-sm:9px;
  --mono:"Consolas","SFMono-Regular",ui-monospace,monospace;
  --sans:"Segoe UI Variable","Segoe UI",Tahoma,Arial,sans-serif;
}

*{box-sizing:border-box}
html,body{min-height:100%}
html{scroll-behavior:smooth}
html,body{overflow-x:hidden}
body{
  margin:0;
  color:var(--text);
  font-family:var(--sans);
  background:
    radial-gradient(circle at top left, rgba(64,124,201,0.08), transparent 28%),
    radial-gradient(circle at top right, rgba(64,124,201,0.05), transparent 30%),
    linear-gradient(180deg,#e7edf4 0%, #dfe5ec 48%, #d7dee7 100%);
  background-attachment:fixed;
  -webkit-font-smoothing:antialiased;
  text-rendering:optimizeLegibility;
}

button,input,textarea,select{font:inherit}
button{cursor:pointer}
input,textarea,select{outline:none}

.app-shell{
  min-height:100vh;
  display:grid;
  grid-template-rows:auto auto auto minmax(0,1fr) auto auto;
  gap:8px;
  padding:8px;
}

.titlebar,.ribbon-tabs,.toolbar,.panel,.statusbar{
  box-shadow:var(--shadow);
}

.titlebar{
  position:sticky;
  top:8px;
  z-index:20;
  display:flex;
  justify-content:space-between;
  align-items:center;
  gap:14px;
  padding:10px 12px;
  border:1px solid rgba(20,33,51,0.12);
  background:linear-gradient(180deg,var(--chrome),var(--chrome-3));
  border-radius:12px;
  color:#fff;
}

.brand-block{
  display:flex;
  align-items:center;
  gap:14px;
  min-width:0;
}
.brand{
  color:#fff;
  text-decoration:none;
  font-size:20px;
  font-weight:800;
  letter-spacing:-0.05em;
}
.brand span{color:#9fd0ff}

.project-meta{display:flex;flex-direction:column;min-width:0}
.project-name{
  font-size:12px;
  font-weight:800;
  text-transform:uppercase;
  letter-spacing:0.1em;
}
.project-path{
  font-family:var(--mono);
  font-size:11px;
  opacity:0.84;
}

.top-status{
  display:flex;
  flex-wrap:wrap;
  justify-content:flex-end;
  gap:8px;
}

.menu-bar{
  display:flex;
  gap:4px;
  align-items:center;
  padding:2px 2px 0;
  flex-wrap:wrap;
}
.menu-item{
  border:none;
  background:transparent;
  color:#4d6078;
  font-size:11px;
  font-weight:700;
  padding:3px 7px;
  border-radius:6px;
  letter-spacing:0.02em;
}
.menu-item:hover{
  background:rgba(47,127,217,0.1);
  color:#173456;
}
.menu-item.active{
  background:rgba(47,127,217,0.14);
  color:#173456;
}
.pill{
  display:inline-flex;
  align-items:center;
  padding:7px 10px;
  border-radius:999px;
  border:1px solid rgba(176,207,249,0.16);
  background:rgba(255,255,255,0.08);
  color:#eef5ff;
  font-size:12px;
  font-weight:700;
}
.pill.live,.pill.ok{background:rgba(181,228,201,0.18);color:#eafff1;border-color:rgba(181,228,201,0.34)}
.pill.warn{background:rgba(255,215,117,0.18);color:#fff7da;border-color:rgba(255,215,117,0.34)}
.pill.bad{background:rgba(255,170,186,0.18);color:#ffe2e8;border-color:rgba(255,170,186,0.34)}

.ribbon-tabs{
  display:flex;
  gap:6px;
  padding:0 2px 1px;
}
.ribbon-tab{
  border:1px solid rgba(46,66,92,0.16);
  border-bottom:none;
  border-radius:12px 12px 0 0;
  background:linear-gradient(180deg,#f9fbfd,#edf2f7);
  color:#44576b;
  padding:8px 12px;
  font-weight:700;
  font-size:12px;
  letter-spacing:0.01em;
}
.ribbon-tab.active{
  background:linear-gradient(180deg,#ffffff,#eef4fa);
  color:#1a3552;
  border-color:rgba(47,127,217,0.36);
}

.toolbar{
  display:flex;
  justify-content:space-between;
  align-items:center;
  gap:10px;
  flex-wrap:wrap;
  padding:10px 11px;
  border:1px solid rgba(46,66,92,0.14);
  background:linear-gradient(180deg,#f8fafc,#eef3f8);
  border-radius:0 12px 12px 12px;
}
.toolbar-group{
  display:flex;
  gap:6px;
  flex-wrap:wrap;
}

.tool-btn{
  border:1px solid rgba(46,66,92,0.16);
  background:linear-gradient(180deg,#ffffff,#edf2f7);
  color:#203246;
  border-radius:10px;
  padding:8px 11px;
  font-weight:700;
  font-size:12px;
  transition:transform .12s ease, border-color .12s ease, box-shadow .12s ease, background .12s ease;
}
.tool-btn:hover{
  transform:translateY(-1px);
  border-color:var(--line-strong);
  box-shadow:0 0 0 1px rgba(47,127,217,0.08) inset;
}
.tool-btn.primary{
  background:linear-gradient(180deg,#4fa0eb,#2f7fd9);
  border-color:rgba(47,127,217,0.48);
  color:#fff;
}
.tool-btn.danger{
  background:linear-gradient(180deg,rgba(255,126,151,0.12),rgba(255,126,151,0.08));
  border-color:rgba(255,126,151,0.28);
  color:#ffdbe3;
}
.tool-btn.small{padding:8px 11px;border-radius:9px}

.workspace{
  display:grid;
  grid-template-columns:minmax(256px,288px) minmax(0,1fr) minmax(294px,330px);
  gap:8px;
  min-height:0;
}

.sidebar,.editor{
  min-height:0;
  display:grid;
  gap:8px;
}

.panel{
  border-radius:12px;
  border:1px solid rgba(46,66,92,0.14);
  background:linear-gradient(180deg,#ffffff,#f7f9fc);
  padding:12px;
  position:relative;
  overflow:hidden;
}

.panel::before{
  content:"";
  position:absolute;
  inset:0;
  background:linear-gradient(180deg,rgba(47,127,217,0.02),transparent 28%);
  pointer-events:none;
}

.device-panel{padding-bottom:12px}
.device-overview{
  display:grid;
  grid-template-columns:repeat(4,minmax(0,1fr));
  gap:8px;
}
.device-card{
  border:1px solid rgba(46,66,92,0.14);
  border-radius:12px;
  background:linear-gradient(180deg,#ffffff,#f4f7fb);
  padding:10px;
  min-height:80px;
  color:var(--text);
}
.device-card .title{
  display:flex;
  justify-content:space-between;
  gap:8px;
  font-size:12px;
  font-weight:800;
  margin-bottom:8px;
}
.device-card .sub{
  color:var(--muted);
  font-size:12px;
  line-height:1.45;
}
.device-card .chip{
  display:inline-flex;
  align-items:center;
  padding:5px 8px;
  border-radius:999px;
  border:1px solid rgba(47,127,217,0.18);
  background:rgba(47,127,217,0.08);
  color:#173456;
  font-size:11px;
  font-weight:700;
  white-space:nowrap;
}
.device-card .chip.on{background:rgba(103,232,165,0.12);border-color:rgba(103,232,165,0.24);color:#14633f}
.device-card .chip.off{background:#f4f7fb;color:var(--muted)}

.panel-head{
  display:flex;
  justify-content:space-between;
  align-items:baseline;
  gap:10px;
  margin-bottom:10px;
}
.panel-head #workspace-title{
  margin:0;
  font-size:17px;
  line-height:1.1;
}
.panel-head h2{
  margin:0;
  font-size:13px;
  font-weight:800;
  color:#18314d;
  letter-spacing:0.01em;
}
.panel-head #workspace-tag{
  white-space:nowrap;
}
.panel-tag{
  display:inline-flex;
  align-items:center;
  padding:4px 8px;
  border-radius:999px;
  border:1px solid rgba(47,127,217,0.14);
  background:rgba(47,127,217,0.08);
  color:#173456;
  font-size:11px;
  font-weight:700;
}
.panel-footer{margin-top:12px}

.view-switcher{
  display:flex;
  gap:6px;
  flex-wrap:wrap;
  margin-bottom:10px;
}
.view-tab{
  border:1px solid rgba(46,66,92,0.16);
  background:linear-gradient(180deg,#ffffff,#edf2f7);
  color:#44576b;
  border-radius:999px;
  padding:6px 10px;
  font-size:12px;
  font-weight:700;
  letter-spacing:0.01em;
}
.view-tab.active{
  background:linear-gradient(180deg,#ffffff,#eef4fa);
  border-color:rgba(47,127,217,0.42);
  color:#173456;
}
.main-stage{
  display:grid;
  gap:8px;
}
.stage-card{
  border:1px solid rgba(46,66,92,0.14);
  border-radius:12px;
  background:linear-gradient(180deg,#ffffff,#f7f9fc);
  padding:11px;
}
.stage-grid{
  display:grid;
  gap:8px;
}
.stage-tiles{
  display:grid;
  grid-template-columns:repeat(4,minmax(0,1fr));
  gap:8px;
}
.stage-tile{
  padding:10px 11px;
  border:1px solid rgba(46,66,92,0.14);
  border-radius:12px;
  background:linear-gradient(180deg,#ffffff,#f4f7fb);
}
.stage-tile .k{font-size:12px;color:var(--muted);margin-bottom:8px}
.stage-tile .v{font-size:17px;font-weight:800;color:#173456;letter-spacing:-0.02em}
.stage-tile .s{font-size:12px;color:var(--muted);margin-top:6px;line-height:1.45}
.stage-split{
  display:grid;
  grid-template-columns:repeat(2,minmax(0,1fr));
  gap:8px;
}
.device-network-grid{
  grid-template-columns:repeat(auto-fit,minmax(240px,1fr));
}
.portal-start{
  padding:14px;
}
.portal-steps{
  display:grid;
  gap:10px;
}
.portal-step{
  display:grid;
  grid-template-columns:auto minmax(0,1fr);
  gap:12px;
  align-items:center;
  width:100%;
  text-align:left;
  padding:12px 14px;
  border:1px solid rgba(46,66,92,0.16);
  border-radius:12px;
  background:linear-gradient(180deg,#ffffff,#f5f8fc);
  color:var(--text);
}
.portal-step:hover{
  border-color:rgba(47,127,217,0.28);
  box-shadow:0 0 0 1px rgba(47,127,217,0.05) inset;
}
.portal-step-index{
  width:30px;
  height:30px;
  border-radius:999px;
  display:inline-flex;
  align-items:center;
  justify-content:center;
  font-weight:800;
  background:rgba(47,127,217,0.1);
  color:#173456;
  border:1px solid rgba(47,127,217,0.14);
}
.portal-step-title{
  font-weight:800;
  color:#173456;
  margin-bottom:3px;
}
.portal-step-detail{
  color:var(--muted);
  font-size:12px;
  line-height:1.45;
}
.network-toolbar{
  display:flex;
  flex-wrap:wrap;
  gap:8px;
  margin-bottom:10px;
}
.network-canvas{
  border:1px solid rgba(46,66,92,0.14);
  border-radius:12px;
  background:
    linear-gradient(180deg,#ffffff,#f6f9fc),
    repeating-linear-gradient(0deg, transparent 0 31px, rgba(46,66,92,0.035) 31px 32px);
  overflow:hidden;
}
.network-canvas svg{
  display:block;
  width:100%;
  height:auto;
}
.net-line{
  stroke:#51b15a;
  stroke-width:4;
  fill:none;
  stroke-linecap:round;
  stroke-linejoin:round;
}
.net-line.danger{
  stroke:#d65b5b;
}
.net-device rect{
  fill:#e6ebf2;
  stroke:#9aa8b8;
  stroke-width:1.2;
}
.net-device .net-screen{
  fill:#ffffff;
}
.net-device .net-module{
  fill:#ced8e3;
}
.net-device .net-module.accent{
  fill:#7de08a;
}
.net-device.selected rect{
  stroke:#2f7fd9;
  stroke-width:1.8;
}
.net-label{
  font:700 16px var(--sans);
  fill:#173456;
}
.net-sub{
  font:400 12px var(--sans);
  fill:#4f5f72;
}
.stage-device-card{
  min-height:auto;
  padding:10px;
  background:#f9fbfd;
}

.tree-list,.tag-table,.watch-table,.diagnostics,.trace-list,.report-list,.inspector,.palette,.routine-editor{
  display:grid;
  gap:7px;
}

.tree-node,.tag-row,.watch-row,.diag-row,.trace-item,.report-item,.routine-card,.rung-card,.element-card,.inspect-block,.palette-btn{
  border:1px solid rgba(46,66,92,0.14);
  border-radius:12px;
  background:#ffffff;
}

.tree-node,.tag-row,.watch-row,.diag-row,.trace-item,.report-item,.inspect-block{
  padding:10px;
}

.tree-node{
  display:grid;
  gap:6px;
  cursor:pointer;
  transition:background .12s ease,border-color .12s ease,transform .12s ease,box-shadow .12s ease;
}
.tree-node.section{
  background:linear-gradient(180deg,rgba(47,127,217,0.07),rgba(255,255,255,0.95));
}
.tree-node.group{
  padding-left:18px;
}
.tree-node.leaf{
  padding-left:30px;
}
.tree-node:hover,.tag-row:hover,.watch-row:hover,.diag-row:hover,.trace-item:hover,.report-item:hover,.routine-card:hover,.rung-card:hover,.element-card:hover,.inspect-block:hover,.palette-btn:hover{
  border-color:rgba(47,127,217,0.24);
  box-shadow:0 0 0 1px rgba(47,127,217,0.05) inset;
}
.tree-node.active,.routine-card.active,.rung-card.active,.element-card.active{
  border-color:rgba(47,127,217,0.48);
  box-shadow:0 0 0 1px rgba(47,127,217,0.08) inset, 0 0 0 1px rgba(47,127,217,0.06);
}
.tree-title{
  display:flex;
  justify-content:space-between;
  gap:8px;
  font-weight:800;
}
.tree-icon{
  width:18px;height:18px;display:inline-flex;align-items:center;justify-content:center;
  border-radius:4px;background:rgba(47,127,217,0.1);color:#173456;font-size:11px;font-weight:900;
  box-shadow:0 0 0 1px rgba(47,127,217,0.06) inset;
}
.tree-sub,.meta,.hint,.diag-sub,.trace-sub,.watch-sub,.report-sub,.inspect-note{
  color:var(--muted);
  font-size:12px;
  line-height:1.5;
}
.watch-title{
  font-weight:800;
  color:#ecf4ff;
}

.routine-summary{
  border:1px dashed rgba(47,127,217,0.22);
  border-radius:12px;
  padding:10px 12px;
  background:rgba(47,127,217,0.05);
  color:#173456;
  margin-bottom:9px;
}

.tab-strip{display:flex;gap:8px;flex-wrap:wrap}
.tab{
  border:1px solid rgba(46,66,92,0.16);
  background:linear-gradient(180deg,#ffffff,#edf2f7);
  color:#44576b;
  border-radius:999px;
  padding:6px 10px;
  font-size:12px;
  font-weight:700;
}
.tab.active{
  background:linear-gradient(180deg,#ffffff,#eef4fa);
  border-color:rgba(47,127,217,0.42);
  color:#173456;
}

.canvas-panel{display:grid;min-height:0}
.routine-editor{
  min-height:0;
  overflow:auto;
  padding-right:2px;
}

.rung-card{
  padding:11px;
  display:grid;
  gap:10px;
}
.rung-head{
  display:grid;
  grid-template-columns:minmax(0,1fr) auto;
  gap:10px;
  align-items:start;
}
.rung-fields{
  display:grid;
  gap:8px;
}
.field{
  width:100%;
  border-radius:10px;
  border:1px solid rgba(46,66,92,0.16);
  background:#ffffff;
  color:var(--text);
  padding:8px 10px;
  font-size:12px;
}
.field.small{padding:8px 9px}
.field.textarea{min-height:52px;resize:vertical}
.field.mono{font-family:var(--mono)}
.rung-actions{display:flex;gap:8px;flex-wrap:wrap;justify-content:flex-end}
.mini{
  border:1px solid rgba(46,66,92,0.16);
  border-radius:10px;
  background:linear-gradient(180deg,#ffffff,#edf2f7);
  color:#203246;
  padding:7px 9px;
  font-weight:700;
  font-size:12px;
}
.mini.danger{background:linear-gradient(180deg,rgba(255,126,151,0.14),rgba(255,126,151,0.08));color:#ffdbe3;border-color:rgba(255,126,151,0.22)}
.mini.accent{background:linear-gradient(180deg,rgba(47,127,217,0.14),rgba(47,127,217,0.08));color:#173456;border-color:rgba(47,127,217,0.22)}

.element-list{display:grid;gap:8px}
.element-card{
  display:grid;
  gap:10px;
  padding:10px;
  grid-template-columns:minmax(156px,180px) minmax(0,1fr) auto;
  align-items:start;
}
.element-card .field{width:100%}
.element-head{display:grid;gap:8px}
.element-chip{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  align-self:start;
  padding:6px 9px;
  border-radius:999px;
  border:1px solid rgba(47,127,217,0.16);
  background:rgba(47,127,217,0.08);
  color:#173456;
  font-size:11px;
  font-weight:800;
  letter-spacing:0.06em;
  text-transform:uppercase;
}
.element-body{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:8px}
.element-footer{display:flex;gap:8px;flex-wrap:wrap;justify-content:flex-end}

.inspect-block h3,.palette-title,.diag-title,.report-title,.trace-title{
  margin:0;
  font-size:13px;
}
.inspect-grid{
  display:grid;
  grid-template-columns:repeat(2,minmax(0,1fr));
  gap:8px;
}
.palette-btn{
  display:grid;
  gap:6px;
  padding:11px;
  text-align:left;
  color:var(--text);
  background:linear-gradient(180deg,#ffffff,#f6f9fc);
}
.palette-btn:hover{border-color:#8fb5e3;background:linear-gradient(180deg,#ffffff,#eef4fa)}
.palette-sub{color:var(--muted);font-size:12px;line-height:1.5}

.tag-grid,.watch-grid{
  display:grid;
  grid-template-columns:84px minmax(0,1fr) 76px auto;
  gap:8px;
  align-items:center;
}
.tag-grid .field,.watch-grid .field{padding:8px 9px}
.watch-row{display:grid;gap:8px}
.value-chip{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  min-width:64px;
  padding:6px 9px;
  border-radius:999px;
  border:1px solid rgba(46,66,92,0.14);
  background:#f7f9fc;
  font-family:var(--mono);
  font-size:12px;
  font-weight:800;
}
.value-chip.on{background:rgba(103,232,165,0.12);border-color:rgba(103,232,165,0.24);color:#14633f}
.value-chip.off{background:#f7f9fc;color:var(--muted)}
.value-chip.warn{background:rgba(255,206,107,0.12);border-color:rgba(255,206,107,0.24);color:#8d6400}

.watch-table,.diagnostics,.trace-list,.report-list{
  max-height:300px;
  overflow:auto;
  padding-right:2px;
}

.dock{
  display:grid;
  grid-template-columns:1.1fr 0.9fr 1.1fr;
  gap:10px;
  min-height:0;
}
.dock-panel{min-height:210px}

.statusbar{
  display:flex;
  justify-content:space-between;
  gap:12px;
  flex-wrap:wrap;
  align-items:center;
  border-radius:12px;
  border:1px solid rgba(46,66,92,0.14);
  background:linear-gradient(180deg,#f8fafc,#eef3f8);
  padding:9px 12px;
  color:#304355;
  font-size:12px;
}

.report-item.error{border-color:rgba(255,126,151,0.26);background:rgba(255,126,151,0.08)}
.report-item.warn{border-color:rgba(255,206,107,0.26);background:rgba(255,206,107,0.08)}
.report-item.ok{border-color:rgba(103,232,165,0.22);background:rgba(103,232,165,0.06)}
.diag-title,.report-title{display:flex;justify-content:space-between;gap:10px}

.empty{
  color:var(--muted);
  font-size:12px;
  padding:10px;
  border:1px dashed rgba(46,66,92,0.16);
  border-radius:12px;
  background:#ffffff;
}

@media (max-width: 1440px){
  .workspace{grid-template-columns:minmax(238px,270px) minmax(0,1fr) minmax(280px,312px)}
  .device-overview{grid-template-columns:repeat(2,minmax(0,1fr))}
}

@media (max-width: 1180px){
  .workspace{grid-template-columns:minmax(0,1fr)}
  .sidebar.right{grid-template-columns:repeat(3,minmax(0,1fr))}
  .sidebar.left{grid-template-columns:repeat(2,minmax(0,1fr))}
  .dock{grid-template-columns:1fr}
  .stage-tiles{grid-template-columns:repeat(2,minmax(0,1fr))}
  .stage-split{grid-template-columns:1fr}
}

@media (max-width: 1240px){
  .sidebar.right{grid-template-columns:repeat(2,minmax(0,1fr))}
}

@media (max-width: 880px){
  .app-shell{padding:6px; gap:6px}
  .titlebar,.toolbar,.statusbar{border-radius:11px}
  .titlebar{
    position:static;
    flex-direction:column;
    align-items:flex-start;
    gap:8px;
  }
  .top-status{
    width:100%;
    justify-content:flex-start;
  }
  .menu-bar{
    gap:4px;
    padding-bottom:2px;
    flex-wrap:wrap;
  }
  .menu-item{white-space:nowrap}
  .ribbon-tabs{
    padding-bottom:2px;
    flex-wrap:wrap;
  }
  .toolbar{
    justify-content:flex-start;
    align-items:flex-start;
  }
  .toolbar-group{
    width:100%;
  }
  .tool-btn{
    flex:1 1 auto;
  }
  .workspace{
    grid-template-columns:1fr;
  }
  .editor{order:1}
  .sidebar.left{order:2;grid-template-columns:1fr}
  .sidebar.right{order:3;grid-template-columns:1fr}
  .dock{grid-template-columns:1fr}
  .rung-head,.element-card,.tag-grid{grid-template-columns:1fr}
  .element-body,.inspect-grid{grid-template-columns:1fr}
  .device-overview{grid-template-columns:1fr}
  .stage-tiles{grid-template-columns:1fr}
  .stage-split{grid-template-columns:1fr}
  .tag-grid,.watch-grid{grid-template-columns:1fr}
  .element-card{grid-template-columns:1fr}
  .rung-actions,.element-footer{justify-content:flex-start}
  .network-canvas{overflow-x:auto}
  .network-canvas svg{min-width:760px}
}
