:root {
  --bg0: #040608;
  --bg1: #08090f;
  --bg2: #0c0e16;
  --bg3: #10131d;
  --bg4: #151a27;
  --border: #1a2035;
  --border2: #232d47;
  --amber: #e8a020;
  --amber2: #c07010;
  --amber-dim: rgba(232,160,32,0.10);
  --cyan: #3a7bd5;
  --cyan-dim: rgba(58,123,213,0.08);
  --green: #00c97a;
  --green-dim: rgba(0,201,122,0.08);
  --red: #e04040;
  --red-dim: rgba(224,64,64,0.08);
  --purple: #8b6cf6;
  --text1: #e8edf8;
  --text2: #8a9bc0;
  --text3: #4a5878;
  --mono: 'IBM Plex Mono', monospace;
  --display: 'Bebas Neue', cursive;
  --body: 'Barlow', sans-serif;
  --topbar-h: 48px;
  --sidebar-w: 220px;
  --ai-panel-w: 340px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; background: var(--bg0); color: var(--text1); font-family: var(--body); }

/* ── TOPBAR ── */
#topbar {
  height: var(--topbar-h);
  background: var(--bg1);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 0;
  flex-shrink: 0;
  z-index: 1000;
  position: relative;
}
.logo-wrap { display: flex; align-items: center; gap: 10px; }
.logo-mark {
  width: 32px; height: 32px;
  background: linear-gradient(135deg, var(--amber), #c05010);
  border-radius: 7px;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--display); font-size: 19px; color: #000; flex-shrink: 0;
}
.logo-name { font-family: var(--display); font-size: 17px; letter-spacing: 2px; }
.logo-tag { font-family: var(--mono); font-size: 7px; color: var(--text3); letter-spacing: .14em; text-transform: uppercase; margin-top: 1px; }
.tb-sep { width: 1px; height: 24px; background: var(--border); margin: 0 14px; }

/* Status pills */
.tb-pills { display: flex; gap: 5px; }
.tb-pill {
  display: flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: 100px;
  font-family: var(--mono); font-size: 8px; border: 1px solid; cursor: default;
}
.tb-pill-dot { width: 4px; height: 4px; border-radius: 50%; animation: blink 2s infinite; }
@keyframes blink { 0%,100%{opacity:1;} 50%{opacity:.2;} }
.pill-g { border-color: rgba(0,201,122,.3); color: var(--green); background: var(--green-dim); }
.pill-g .tb-pill-dot { background: var(--green); }
.pill-a { border-color: rgba(232,160,32,.3); color: var(--amber); background: var(--amber-dim); }
.pill-a .tb-pill-dot { background: var(--amber); }
.pill-r { border-color: rgba(224,64,64,.3); color: var(--red); background: var(--red-dim); }
.pill-r .tb-pill-dot { background: var(--red); }

/* Topbar right controls */
.tb-right { display: flex; align-items: center; gap: 8px; margin-left: auto; }
#tb-clock { font-family: var(--mono); font-size: 10px; color: var(--text2); }
#tb-clock b { color: var(--amber); }
.tb-avatar {
  width: 26px; height: 26px; border-radius: 50%;
  background: linear-gradient(135deg, var(--purple), var(--cyan));
  display: flex; align-items: center; justify-content: center;
  font-size: 9px; font-weight: 700; cursor: pointer;
}
.tb-btn {
  display: flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: 4px;
  border: 1px solid var(--border2); background: transparent;
  color: var(--text2); font-family: var(--mono); font-size: 8px;
  cursor: pointer; transition: all .12s; letter-spacing: .05em; text-transform: uppercase;
  white-space: nowrap;
}
.tb-btn:hover { border-color: var(--amber); color: var(--amber); }
.tb-btn svg { width: 10px; height: 10px; flex-shrink: 0; }
.tb-btn-ai {
  border-color: rgba(58,123,213,.3); color: var(--cyan);
  background: var(--cyan-dim);
}
.tb-btn-ai:hover { border-color: var(--cyan); background: rgba(58,123,213,.14); }
.tb-btn-ai.active { background: rgba(58,123,213,.16); border-color: var(--cyan); }
.tb-btn-ai .tb-pill-dot { background: var(--cyan); animation: blink 2s infinite; }
.ws-badge {
  display: none; align-items: center; gap: 4px;
  padding: 2px 7px; border-radius: 100px;
  background: rgba(58,123,213,.08); border: 1px solid rgba(58,123,213,.2);
  font-family: var(--mono); font-size: 7px; color: var(--cyan);
}
.ws-badge.on { display: flex; }

/* ── APP LAYOUT ── */
#app {
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--topbar-h));
  overflow: hidden;
}
#app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── SHEET TABS (Workbook Style) ── */
#sheet-tabs {
  height: 34px;
  background: var(--bg1);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: stretch;
  padding: 0 8px;
  flex-shrink: 0;
  gap: 2px;
}
#sheet-tabs-list {
  display: flex;
  align-items: stretch;
  gap: 2px;
  overflow-x: auto;
  flex: 1;
}
#sheet-tabs-list::-webkit-scrollbar { height: 0; }
.sheet-tab {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  font-family: var(--mono);
  font-size: 10px;
  color: var(--text2);
  cursor: pointer;
  white-space: nowrap;
  margin-top: 4px;
  transition: all .12s;
  position: relative;
}
.sheet-tab:hover { color: var(--text1); background: var(--bg3); }
.sheet-tab.active {
  background: var(--bg0);
  color: var(--amber);
  border-color: var(--border2);
  margin-top: 0;
  border-bottom: 2px solid var(--bg0);
}
.sheet-tab-name {
  outline: none;
  min-width: 12px;
}
.sheet-tab-name[contenteditable="true"] {
  border-bottom: 1px dashed var(--amber);
}
.sheet-tab-close {
  width: 14px; height: 14px;
  border-radius: 3px;
  display: flex; align-items: center; justify-content: center;
  font-size: 10px;
  color: var(--text3);
  opacity: 0;
  transition: opacity .12s;
}
.sheet-tab:hover .sheet-tab-close { opacity: 1; }
.sheet-tab-close:hover { background: rgba(224,64,64,.2); color: var(--red); }
#sheet-add-btn {
  width: 28px;
  margin-top: 4px;
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  background: var(--bg2);
  color: var(--text3);
  font-size: 14px;
  cursor: pointer;
  transition: all .12s;
  flex-shrink: 0;
}
#sheet-add-btn:hover { background: var(--amber-dim); color: var(--amber); }

/* ── SIDEBAR ── */
#sidebar {
  width: var(--sidebar-w);
  background: var(--bg1);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
  z-index: 100;
}
#sidebar::-webkit-scrollbar { width: 3px; }
#sidebar::-webkit-scrollbar-thumb { background: var(--border2); }

.sb-header {
  padding: 10px 12px 6px;
  font-family: var(--mono); font-size: 7px;
  letter-spacing: .14em; text-transform: uppercase;
  color: var(--text3); flex-shrink: 0;
  border-bottom: 1px solid var(--border);
}

/* Section */
.sb-section { border-bottom: 1px solid var(--border); }
.sb-section-hdr {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px;
  cursor: pointer;
  user-select: none;
  transition: background .1s;
}
.sb-section-hdr:hover { background: var(--bg3); }
.sb-section-label {
  display: flex; align-items: center; gap: 7px;
  font-family: var(--mono); font-size: 9px;
  letter-spacing: .1em; text-transform: uppercase; font-weight: 600;
}
.sb-section-ico {
  width: 18px; height: 18px; border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  font-size: 10px; flex-shrink: 0;
}
.ico-intel { background: rgba(58,123,213,.12); color: var(--cyan); }
.ico-wealth { background: rgba(232,160,32,.12); color: var(--amber); }
.sb-chevron {
  font-size: 9px; color: var(--text3);
  transition: transform .2s;
}
.sb-section.open .sb-chevron { transform: rotate(90deg); }

.sb-items { display: none; padding: 3px 0 5px; }
.sb-section.open .sb-items { display: block; }

/* Module item */
.sb-item {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px 6px 14px;
  cursor: grab;
  transition: background .1s;
  user-select: none;
  position: relative;
}
.sb-item:hover { background: var(--bg3); }
.sb-item:active { cursor: grabbing; }
.sb-item.stub { opacity: .45; cursor: default; }
.sb-item.stub:hover { background: transparent; }
.sb-item-drag {
  width: 3px; height: 16px; border-radius: 2px;
  background: var(--border2); flex-shrink: 0;
  opacity: 0; transition: opacity .1s;
}
.sb-item:hover .sb-item-drag { opacity: 1; }
.sb-item-icon {
  font-size: 11px; flex-shrink: 0;
  width: 18px; text-align: center;
}
.sb-item-label {
  font-size: 11px; color: var(--text2);
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.sb-item:hover .sb-item-label { color: var(--text1); }
.sb-item-badge {
  font-family: var(--mono); font-size: 6px;
  padding: 1px 4px; border-radius: 2px;
  text-transform: uppercase; letter-spacing: .05em;
}
.badge-soon { background: var(--bg4); color: var(--text3); border: 1px solid var(--border2); }

/* Drag ghost */
#drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 9999;
  display: none;
  background: var(--bg3);
  border: 1px solid var(--amber);
  border-radius: 6px;
  padding: 6px 12px;
  font-family: var(--mono); font-size: 9px;
  color: var(--amber);
  opacity: .85;
  white-space: nowrap;
  box-shadow: 0 8px 24px rgba(0,0,0,.5);
}

/* ── CANVAS ── */
#canvas {
  flex: 1;
  position: relative;
  overflow: hidden;
  background:
    linear-gradient(rgba(26,32,53,.4) 1px, transparent 1px),
    linear-gradient(90deg, rgba(26,32,53,.4) 1px, transparent 1px),
    var(--bg0);
  background-size: 40px 40px;
}
#canvas-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
#canvas-empty.hidden { display: none; }
.canvas-empty-ico { font-size: 32px; margin-bottom: 12px; opacity: .3; }
.canvas-empty-title {
  font-family: var(--display); font-size: 18px;
  letter-spacing: 2px; color: var(--text3); margin-bottom: 6px;
}
.canvas-empty-sub {
  font-family: var(--mono); font-size: 9px;
  color: var(--text3); letter-spacing: .08em;
  text-transform: uppercase;
}

/* Canvas drag-over highlight */
#canvas.drag-over { background-color: rgba(232,160,32,.02); }

/* ── PANELS ── */
.panel {
  position: absolute;
  background: var(--bg2);
  border: 1px solid var(--border2);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,.5);
  min-width: 320px;
  min-height: 220px;
  transition: box-shadow .15s;
}
.panel.focused {
  border-color: rgba(232,160,32,.35);
  box-shadow: 0 12px 40px rgba(0,0,0,.6), 0 0 0 1px rgba(232,160,32,.08);
}
.panel.minimized .panel-body { display: none; }
.panel.minimized { min-height: 0; }

/* Panel header */
.panel-hdr {
  height: 36px;
  background: var(--bg1);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 10px;
  gap: 8px;
  cursor: move;
  flex-shrink: 0;
  user-select: none;
}
.panel-hdr-title {
  font-family: var(--mono); font-size: 8px;
  letter-spacing: .1em; text-transform: uppercase;
  color: var(--text2); flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.panel-platform-badge {
  font-family: var(--mono); font-size: 7px;
  padding: 1px 5px; border-radius: 3px;
  text-transform: uppercase; letter-spacing: .06em;
  flex-shrink: 0;
}
.badge-intel { background: rgba(58,123,213,.1); color: var(--cyan); border: 1px solid rgba(58,123,213,.2); }
.badge-wealth { background: rgba(232,160,32,.1); color: var(--amber); border: 1px solid rgba(232,160,32,.2); }
.panel-controls { display: flex; gap: 4px; flex-shrink: 0; }
.panel-ctrl {
  width: 18px; height: 18px; border-radius: 4px;
  border: none; background: var(--bg4);
  color: var(--text3); font-size: 9px;
  cursor: pointer; display: flex; align-items: center;
  justify-content: center; transition: all .1s;
}
.panel-ctrl:hover { background: var(--bg5, #212638); color: var(--text1); }
.panel-ctrl-close:hover { background: rgba(224,64,64,.2); color: var(--red); }
.panel-ctrl-min:hover { background: rgba(232,160,32,.15); color: var(--amber); }

/* Panel body */
.panel-body {
  flex: 1;
  overflow: hidden;
  position: relative;
}
.panel-body iframe {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}
.panel-body-stub {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  height: 100%;
  gap: 8px;
}
.stub-ico { font-size: 28px; opacity: .3; }
.stub-label {
  font-family: var(--mono); font-size: 9px;
  color: var(--text3); letter-spacing: .1em;
  text-transform: uppercase;
}

/* Resize handle */
.panel-resize {
  position: absolute;
  bottom: 0; right: 0;
  width: 16px; height: 16px;
  cursor: nwse-resize;
  opacity: 0;
  transition: opacity .1s;
}
.panel:hover .panel-resize { opacity: 1; }
.panel-resize::after {
  content: '';
  position: absolute;
  bottom: 4px; right: 4px;
  width: 6px; height: 6px;
  border-right: 2px solid var(--text3);
  border-bottom: 2px solid var(--text3);
  border-radius: 1px;
}

/* ── AI PANEL ── */
#ai-panel {
  width: 0; min-width: 0;
  background: var(--bg1);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  overflow: hidden;
  transition: width .22s cubic-bezier(.4,0,.2,1), min-width .22s;
  flex-shrink: 0;
}
#ai-panel.open { width: var(--ai-panel-w); min-width: var(--ai-panel-w); }

.ai-ph {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  flex-shrink: 0;
}
.ai-ph-title {
  font-family: var(--mono); font-size: 8px;
  letter-spacing: .12em; text-transform: uppercase;
  color: var(--cyan); display: flex; align-items: center; gap: 6px;
}
.ai-close {
  background: none; border: none; color: var(--text3);
  cursor: pointer; font-size: 13px; padding: 2px 4px;
  transition: color .1s;
}
.ai-close:hover { color: var(--text1); }

.ai-ctx {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2); flex-shrink: 0;
}
.ai-ctx-lbl {
  font-family: var(--mono); font-size: 7px;
  letter-spacing: .1em; text-transform: uppercase;
  color: var(--text3); margin-bottom: 5px;
}
.ai-ctx-chips { display: flex; flex-wrap: wrap; gap: 3px; }
.ai-chip {
  font-family: var(--mono); font-size: 7px;
  padding: 1px 5px; border-radius: 3px;
  border: 1px solid; background: var(--bg3);
}
.ai-chip-active { color: var(--amber); border-color: rgba(232,160,32,.25); }
.ai-chip-idle { color: var(--text3); border-color: var(--border); }

.ai-drop {
  margin: 8px 10px;
  border: 1px dashed var(--border2);
  border-radius: 6px; padding: 10px;
  text-align: center; cursor: pointer;
  transition: all .15s; flex-shrink: 0;
}
.ai-drop:hover, .ai-drop.over {
  border-color: var(--cyan); background: rgba(58,123,213,.04);
}
.ai-drop-ico { font-size: 15px; margin-bottom: 3px; }
.ai-drop-txt { font-size: 9px; color: var(--text2); }
.ai-drop-sub { font-family: var(--mono); font-size: 7px; color: var(--text3); margin-top: 1px; }

.ai-loaded {
  display: none; align-items: center; gap: 5px;
  margin: 0 10px 6px;
  background: rgba(58,123,213,.07);
  border: 1px solid rgba(58,123,213,.2);
  border-radius: 4px; padding: 4px 8px;
}
.ai-loaded.on { display: flex; }
.ai-loaded-name {
  font-family: var(--mono); font-size: 8px;
  color: var(--cyan); flex: 1;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.ai-loaded-rm {
  background: none; border: none; color: var(--text3);
  cursor: pointer; font-size: 11px;
}
.ai-loaded-rm:hover { color: var(--red); }

.ai-msgs {
  flex: 1; overflow-y: auto;
  padding: 8px 10px;
  display: flex; flex-direction: column; gap: 8px;
  scrollbar-width: thin; scrollbar-color: var(--border) transparent;
}
.ai-msgs::-webkit-scrollbar { width: 3px; }
.ai-msgs::-webkit-scrollbar-thumb { background: var(--border2); }
.ai-msg {
  max-width: 92%; padding: 8px 10px;
  border-radius: 7px; font-size: 9px; line-height: 1.6;
}
.ai-msg-u {
  background: var(--amber-dim);
  border: 1px solid rgba(232,160,32,.18);
  align-self: flex-end;
  border-radius: 7px 7px 2px 7px;
}
.ai-msg-a {
  background: var(--bg3);
  border: 1px solid var(--border2);
  align-self: flex-start;
  border-radius: 7px 7px 7px 2px;
}
.ai-msg-lbl {
  font-family: var(--mono); font-size: 7px;
  letter-spacing: .08em; text-transform: uppercase;
  margin-bottom: 2px;
}
.lbl-u { color: var(--amber); text-align: right; }
.lbl-a { color: var(--cyan); }

.ai-input-row {
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  display: flex; gap: 6px; flex-shrink: 0;
}
.ai-input {
  flex: 1;
  background: var(--bg3);
  border: 1px solid var(--border2);
  border-radius: 5px; padding: 6px 9px;
  color: var(--text1); font-size: 9px;
  font-family: var(--body); outline: none;
  resize: none; height: 32px;
  transition: border-color .12s;
}
.ai-input:focus { border-color: rgba(58,123,213,.35); }
.ai-send {
  padding: 0 12px; border-radius: 5px; border: none;
  background: linear-gradient(135deg, var(--cyan), #0088a0);
  color: #000; font-family: var(--mono); font-size: 8px;
  font-weight: 700; cursor: pointer;
  letter-spacing: .06em; text-transform: uppercase;
}
