:root {
  --bg: #1a2226;
  --bg2: #212b30;
  --bg3: #2a363d;
  --fg: #d7e0e6;
  --fg-dim: #8aa0ac;
  --accent: #29aec6;
  --accent2: #37c55e;
  --danger: #d05a4e;
  --warn: #d8a13a;
  --router: #3aa0c9;
  --server: #5dbb63;
  --work: #c9a23a;
  --laptop: #b075c9;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

/* ---------- Splash screen ---------- */
#splash {
  position: fixed; inset: 0; z-index: 200;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(120% 120% at 50% 40%, #04152b 0%, #020611 70%);
}
#splash.hidden { display: none; }
#splash.closing { opacity: 0; transition: opacity .25s ease-in; }
#splash-img {
  max-width: min(92vw, 1280px); max-height: 88vh;
  border-radius: 10px;
  box-shadow: 0 18px 60px rgba(0,0,0,.65);
  user-select: none; -webkit-user-drag: none;
  animation: splash-in .3s ease-out;
}
@keyframes splash-in {
  from { opacity: 0; transform: scale(.985); }
  to { opacity: 1; transform: scale(1); }
}
#splash-close {
  position: absolute; top: 10px; right: 14px;
  background: rgba(13, 22, 28, .7); border: 1px solid #37444c; border-radius: 50%;
  color: var(--fg-dim); font-size: 22px; width: 40px; height: 40px;
  line-height: 1; cursor: pointer;
}
#splash-close:hover { color: var(--fg); border-color: var(--accent); }

html, body { height: 100%; overflow: hidden; }

body {
  font-family: "Segoe UI", system-ui, sans-serif;
  font-size: 13px;
  background: var(--bg);
  color: var(--fg);
}

#app { display: flex; flex-direction: column; height: 100vh; }

/* ---------- Toolbar ---------- */
#toolbar {
  display: flex; align-items: center; gap: 10px;
  height: 44px; padding: 0 12px;
  background: var(--bg2);
  border-bottom: 1px solid #000;
  flex: 0 0 auto;
}
.brand { font-weight: 700; font-size: 15px; color: var(--accent); margin-right: 6px; user-select: none; }
.brand span { color: var(--fg); font-weight: 400; }
.brand-version {
  margin-left: 6px; font-size: 10px; font-weight: 500;
  color: var(--bg); background: var(--accent);
  padding: 1px 6px; border-radius: 8px; vertical-align: middle;
}
.tb-group { display: flex; align-items: center; gap: 4px; }

#toolbar button {
  background: var(--bg3);
  border: 1px solid #37444c;
  color: var(--fg);
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}
#toolbar button:hover { background: var(--accent); color: #06222a; }
.speed-label { display: flex; align-items: center; gap: 6px; color: var(--fg-dim); font-size: 12px; }
#anim-speed { width: 80px; }
#anim-speed-val { min-width: 26px; }

.brand { font-weight: 700; font-size: 15px; color: var(--accent); margin-right: 6px; user-select: none; }

/* ---------- Main layout ---------- */
#main { display: flex; flex: 1 1 auto; min-height: 0; }

/* Palette */
#palette {
  width: 170px; flex: 0 0 auto;
  background: var(--bg2);
  border-right: 1px solid #000;
  padding: 10px;
  overflow-y: auto;
}
#palette h3 {
  font-size: 11px; text-transform: uppercase; letter-spacing: 1px;
  color: var(--fg-dim); margin: 10px 0 8px;
}
#palette h3:first-child { margin-top: 0; }
.palette-items { display: flex; flex-direction: column; gap: 6px; }
.palette-item {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px;
  background: var(--bg3);
  border: 1px solid #37444c;
  border-radius: 6px;
  cursor: grab;
  user-select: none;
}
.palette-item:hover { border-color: var(--accent); }
.palette-item span { font-size: 12px; }
.pi-icon { flex: 0 0 auto; }

/* Canvas */
#canvas-wrap { position: relative; flex: 1 1 auto; min-width: 0; background: var(--bg); }
#topology { position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: default; }

#device-menu {
  position: absolute;
  background: var(--bg3);
  border: 1px solid #37444c;
  border-radius: 6px;
  box-shadow: 0 4px 14px rgba(0,0,0,.5);
  padding: 4px;
  min-width: 140px;
  z-index: 50;
}
#device-menu button {
  display: block; width: 100%;
  background: none; border: 0; color: var(--fg);
  padding: 6px 10px; text-align: left; cursor: pointer;
  border-radius: 4px; font-size: 12px;
}
#device-menu button:hover { background: var(--accent); color: #06222a; }
#device-menu button.danger:hover { background: var(--danger); color: #fff; }

/* Log */
#logpanel {
  width: 260px; flex: 0 0 auto;
  background: var(--bg2);
  border-left: 1px solid #000;
  display: flex; flex-direction: column;
}
#logpanel h3 {
  font-size: 11px; text-transform: uppercase; letter-spacing: 1px;
  color: var(--fg-dim); padding: 10px 10px 6px;
}
#log {
  flex: 1; overflow-y: auto; padding: 0 10px 10px;
  font-family: Consolas, monospace; font-size: 11px; line-height: 1.55;
}

/* ---------- Collapsible side panels ---------- */
/* the panel name itself is the toggle: click "Devices" / "Event log" to collapse */
.panel-head {
  display: flex; align-items: center; gap: 6px;
  cursor: pointer; border-radius: 4px;
}
.panel-head h3 { flex: 1; }
.panel-head:hover h3 { color: var(--accent); }
#palette .panel-head { margin: 0 0 8px; }
#logpanel .panel-head { padding: 10px 10px 6px; }
#log-clear {
  background: none; border: 1px solid #37444c; color: var(--fg-dim);
  border-radius: 4px; cursor: pointer; font-size: 10px;
  padding: 2px 7px; text-transform: uppercase; letter-spacing: .5px;
}
#log-clear:hover { border-color: var(--danger); color: var(--danger); }
#palette .panel-head h3 { margin: 0; }
#logpanel .panel-head h3 { padding: 0; }
/* vertical rail shown only when the panel is collapsed */
.panel-rail {
  display: none;
  flex: 1; min-height: 0;
  align-items: center; justify-content: flex-start;
  padding: 10px 0; cursor: pointer;
  color: var(--fg-dim);
  writing-mode: vertical-rl; text-orientation: mixed;
  font-size: 11px; text-transform: uppercase; letter-spacing: 1px;
  user-select: none;
}
.panel-rail:hover { color: var(--accent); }

/* collapsed state: the panel shrinks to a thin rail (class on <html>) */
html.collapse-palette #palette,
html.collapse-log #logpanel {
  width: 28px; padding: 0; overflow: hidden;
}
html.collapse-palette #palette { display: flex; flex-direction: column; }
html.collapse-palette #palette .palette-items,
html.collapse-log #logpanel #log { display: none; }
html.collapse-palette #palette .panel-head,
html.collapse-log #logpanel .panel-head { display: none; }
html.collapse-palette #palette .panel-rail,
html.collapse-log #logpanel .panel-rail { display: flex; }
.log-line { word-break: break-word; }
.log-time { color: var(--fg-dim); margin-right: 4px; }
.log-ping { color: var(--accent2); }
.log-drop { color: var(--danger); }
.log-dhcp { color: var(--warn); }
.log-wifi { color: var(--laptop); }
.log-info { color: var(--fg); }
.log-dim { color: var(--fg-dim); }

/* ---------- Status footer ---------- */
#statusbar {
  flex: 0 0 auto;
  background: var(--bg2); border-top: 1px solid #000;
  padding: 3px 12px; font-size: 10px; color: var(--fg-dim);
  user-select: none;
  display: flex; align-items: center; justify-content: center; gap: 10px;
  position: relative;
}
#autosave-status {
  position: absolute; right: 12px;
  color: var(--fg-dim); font-family: Consolas, monospace;
  opacity: .85;
}
#autosave-status.as-saving { color: var(--accent); }
#autosave-status.as-ok { color: var(--accent2); }
#autosave-status.as-err { color: var(--danger); }

/* ---------- Windows (terminal / modal / help) ---------- */
.hidden { display: none !important; }

#terminal-win, #modal-win, #help-win, #browser-win {
  position: fixed;
  background: #0c1215;
  border: 1px solid #37444c;
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0,0,0,.6);
  z-index: 100;
  display: flex; flex-direction: column;
}
#terminal-win { width: 640px; height: 420px; left: 90px; top: 90px; }
#modal-win { width: 520px; left: 140px; top: 120px; }

/* maximized windows fill the viewport (geometry saved/restored in JS) */
.win-maximized {
  inset: 0 !important;
  width: 100vw !important;
  height: 100vh !important;
  border-radius: 0;
  border: none;
  box-shadow: none;
}
.win-maximized .rz-handle { display: none; }

/* resize handle (bottom-right corner) */
.rz-handle {
  position: absolute; right: 0; bottom: 0;
  width: 16px; height: 16px;
  cursor: nwse-resize;
  z-index: 10;
}
.rz-handle::before {
  content: ""; position: absolute; right: 4px; bottom: 4px;
  width: 8px; height: 8px;
  border-right: 2px solid #5a6c76; border-bottom: 2px solid #5a6c76;
  border-radius: 0 0 2px 0;
}

/* ---------- Router GUI ---------- */
.rg-tabs { display: flex; gap: 2px; flex-wrap: wrap; padding: 8px 8px 0; }
.rg-tab {
  background: var(--bg3); border: 1px solid #37444c; border-bottom: none;
  color: var(--fg-dim); padding: 6px 12px; cursor: pointer; font-size: 12px;
  border-radius: 6px 6px 0 0;
}
.rg-tab:hover { color: var(--fg); }
.rg-tab.active { color: #06222a; background: var(--accent); font-weight: 600; }
.rg-body { padding: 10px 14px 4px; border-top: 1px solid #37444c; margin: 0 8px; min-height: 120px; flex: 1; overflow-y: auto; }
#mw-body { display: flex; flex-direction: column; min-height: 0; }
/* bridge port checkboxes: grid (children immune to .cfg-row label flex-basis) */
.rg-ports-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 4px 10px; margin-bottom: 8px; }

.rg-table { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
.rg-table th {
  text-align: left; font-size: 10px; text-transform: uppercase; letter-spacing: .5px;
  color: var(--fg-dim); padding: 4px 6px; border-bottom: 1px solid #37444c;
}
.rg-table td { padding: 4px 6px; border-bottom: 1px solid #2a363d; vertical-align: middle; }
.rg-table input[type=text], .rg-table input[type=number], .rg-table select {
  background: var(--bg3); border: 1px solid #37444c; color: var(--fg);
  padding: 3px 6px; border-radius: 4px; font-size: 12px;
  font-family: Consolas, monospace; width: 100%;
}
.rg-table button, .rg-card button, .cfg-row button {
  background: var(--bg3); border: 1px solid #37444c; color: var(--fg);
  padding: 3px 10px; border-radius: 4px; cursor: pointer; font-size: 11px;
}
.rg-table button:hover, .rg-card button:hover, .cfg-row button:hover { border-color: var(--danger); color: var(--danger); }
.rg-mac { font-size: 9px; color: var(--fg-dim); font-family: Consolas, monospace; }

.rg-card {
  background: rgba(0,0,0,.18); border: 1px solid #2a363d; border-radius: 6px;
  padding: 8px; margin-bottom: 10px;
}
.rg-indent { margin-left: 18px; }
.rg-chk { display: flex; align-items: center; gap: 6px; font-size: 12px; cursor: pointer; }
.rg-static-line { font-family: Consolas, monospace; font-size: 11px; color: var(--fg-dim); margin: 2px 0; }
.danger-btn { color: var(--danger) !important; border-color: var(--danger) !important; }
.rg-prof-name, .rg-prof-key { background: var(--bg3) !important; border: 1px solid #37444c !important; color: var(--fg) !important; padding: 3px 6px !important; border-radius: 4px !important; font-family: Consolas !important; }

.tw-titlebar {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--bg3);
  padding: 6px 10px;
  border-bottom: 1px solid #000;
  border-radius: 8px 8px 0 0;
  cursor: move;
  user-select: none;
}
.win-maximized .tw-titlebar { border-radius: 0; cursor: default; }
#tw-title { font-size: 12px; color: var(--accent); font-weight: 600; }
.tw-btns button {
  background: none; border: 0; color: var(--fg-dim);
  font-size: 15px; cursor: pointer; padding: 0 5px; line-height: 1;
}
.tw-btns button:hover { color: var(--fg); }
.tw-btns .tw-max { font-size: 12px; padding: 1px 5px; }
.tw-btns .tw-max:hover { color: var(--accent); }
.tw-btns button:last-child:hover { color: var(--danger); }

#tw-screen {
  flex: 1; overflow-y: auto;
  font-family: Consolas, "Courier New", monospace;
  font-size: 13px; line-height: 1.45;
  padding: 8px 10px;
  white-space: pre-wrap; word-break: break-word;
  color: #cfe3d2;
  outline: none;
}
.tw-input-row {
  display: flex; align-items: center;
  padding: 6px 10px;
  border-top: 1px solid #1d262b;
  font-family: Consolas, monospace; font-size: 13px;
}
#tw-prompt { color: var(--accent); margin-right: 6px; white-space: pre; }
#tw-input {
  flex: 1; background: none; border: 0; outline: none;
  color: #cfe3d2; font-family: inherit; font-size: inherit;
}

#mw-body { padding: 12px 12px 0; flex: 1; overflow-y: auto; min-height: 0; }
#modal-win { display: flex; flex-direction: column; max-height: 85vh; }
#modal-win.win-maximized { max-height: 100vh; }
.cfg-row { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.cfg-row > label { flex: 0 0 150px; font-size: 12px; color: var(--fg-dim); }
.rg-chk, .cfg-row label.rg-chk { flex: 0 0 auto; }
.cfg-row input[type=text], .cfg-row input[type=number], .cfg-row select {
  flex: 1; background: var(--bg3); border: 1px solid #37444c;
  color: var(--fg); padding: 4px 6px; border-radius: 4px; font-size: 12px;
  font-family: Consolas, monospace;
}
.cfg-section { font-size: 11px; text-transform: uppercase; color: var(--accent); margin: 12px 0 6px; }
/* action buttons always visible at the bottom of the config window:
 * - openConfig/openApConfig/openWifiDialog: .cfg-btns is the last element of
 *   the scrollable #mw-body (flex column, no bottom padding) — sticky keeps it
 *   pinned flush to the window bottom while content scrolls.
 * - RouterGUI (.rg-foot): flex child of #mw-body AFTER the scrollable
 *   .rg-body (flex:1) — pinned by layout. */
.cfg-btns {
  display: flex; gap: 8px; justify-content: flex-end;
  margin-top: 14px;
  flex: 0 0 auto;
}
#mw-body > .cfg-btns:not(.rg-foot) {
  position: sticky; bottom: 0;
  margin-left: -12px; margin-right: -12px;
  padding: 10px 12px;
  background: var(--bg3);
  border-top: 1px solid #37444c;
  margin-top: auto;        /* flex column: push to the bottom when content is short */
}
/* breathing room between the last content row and the pinned footer */
#mw-body > *:not(.cfg-btns):last-child { margin-bottom: 10px; }
.rg-foot {
  padding: 10px 12px;
  background: var(--bg3);
  border-top: 1px solid #37444c;
  margin-bottom: 0;
  margin-left: -12px; margin-right: -12px;   /* span flush to the window edges (like the other modals' footer) */
}
.cfg-btns button {
  background: var(--bg3); border: 1px solid #37444c; color: var(--fg);
  padding: 6px 14px; border-radius: 4px; cursor: pointer;
}
.cfg-btns button.primary { background: var(--accent); color: #06222a; border-color: var(--accent); }
.cf-lease { font-size: 10px; color: var(--accent2); margin-left: 8px; font-family: Consolas, monospace; }
.dns-msg { font-size: 11px; color: var(--fg-dim); margin-left: 10px; }
.dns-msg-err { color: var(--danger); }

/* ---------- Browser ---------- */
#browser-win { width: 620px; height: 440px; left: 160px; top: 80px; }
.bw-toolbar {
  display: flex; gap: 4px; padding: 6px 8px;
  background: var(--bg3);
  border-bottom: 1px solid #000;
  align-items: center;
}
.bw-toolbar button {
  background: var(--bg2); border: 1px solid #37444c; color: var(--fg);
  width: 26px; height: 24px; border-radius: 4px; cursor: pointer; font-size: 12px;
}
.bw-toolbar button:hover { background: var(--accent); color: #06222a; }
#bw-url {
  flex: 1; background: var(--bg2); border: 1px solid #37444c;
  color: var(--fg); padding: 4px 8px; border-radius: 4px;
  font-size: 12px; font-family: Consolas, monospace; outline: none;
}
#bw-url:focus { border-color: var(--accent); }
#bw-go { width: auto !important; padding: 0 10px !important; }
#bw-viewport {
  flex: 1; overflow-y: auto;
  background: #f4f4f2; color: #222;
  font-family: "Segoe UI", Arial, sans-serif; font-size: 13px;
  padding: 18px 22px;
}
#bw-viewport h1 { font-size: 20px; margin: 0 0 10px; color: #1a2226; }
#bw-viewport h2 { font-size: 15px; margin: 14px 0 6px; color: #1a2226; }
#bw-viewport p { margin: 6px 0; line-height: 1.5; }
#bw-viewport a { color: #0b6e99; }
#bw-viewport .bw-err-title { font-size: 17px; font-weight: 600; color: #b33; margin-bottom: 8px; }
#bw-viewport .bw-err-detail { color: #555; font-size: 12px; font-family: Consolas, monospace; }
#bw-viewport .bw-loading { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; color: #777; gap: 10px; }
.bw-spinner {
  width: 26px; height: 26px; border-radius: 50%;
  border: 3px solid #ccc; border-top-color: var(--accent);
  animation: bw-spin 0.9s linear infinite;
}
@keyframes bw-spin { to { transform: rotate(360deg); } }
.bw-statusbar {
  background: var(--bg3); border-top: 1px solid #000;
  padding: 3px 10px; font-size: 10px; color: var(--fg-dim);
  font-family: Consolas, monospace;
}

#help-win { width: 460px; left: 200px; top: 100px; }
.hw-body { padding: 14px; flex: 1; overflow-y: auto; }
.hw-body h4 { color: var(--accent); margin: 10px 0 6px; font-size: 13px; }
.hw-body ul { margin-left: 18px; }
.hw-body li { margin-bottom: 4px; line-height: 1.4; }
.hw-body h4:first-of-type { margin-top: 14px; }
.hw-about {
  padding: 10px 12px; margin-bottom: 4px;
  background: var(--bg3);
  border: 1px solid #37444c; border-left: 3px solid var(--accent);
  border-radius: 6px; font-size: 12px; line-height: 1.55;
}
.hw-version { color: var(--accent); font-weight: 700; margin-bottom: 3px; }
.hw-about-desc { color: var(--fg-dim); margin-top: 4px; }

/* Scrollbars */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: var(--bg2); }
::-webkit-scrollbar-thumb { background: #3c4a52; border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #4a5b64; }