/* ─── TAB BAR ─── */
:root {
  --tabbar-h: 38px;
}

.tab-bar {
  position: fixed;
  top: var(--topbar-h);
  left: var(--sidebar-w);
  right: 0;
  height: var(--tabbar-h);
  background: var(--nav-glass);
  backdrop-filter: blur(var(--nav-blur, 20px));
  -webkit-backdrop-filter: blur(var(--nav-blur, 20px));
  border-bottom: 1px solid var(--nav-stroke);
  display: flex;
  align-items: center;
  z-index: 9970;
  transition: left .25s cubic-bezier(.4,0,.2,1);
  padding: 0 4px;
  gap: 2px;
  overflow: hidden;
  /* Prevent backdrop-filter rendering bug on tablet/mobile */
  -webkit-transform: translate3d(0,0,0);
  transform: translate3d(0,0,0);
  will-change: transform;
}

body.sidebar-collapsed .tab-bar {
  left: var(--sidebar-w-collapsed);
}

/* When tab bar is visible, push content down and prevent parent scroll */
body.tabs-active {
  overflow: hidden;
  height: 100vh;
}
body.tabs-active .erp-content {
  margin-top: calc(var(--topbar-h) + var(--tabbar-h));
}

/* Tab bar inner: scrollable container for tabs */
.tab-bar-inner {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 1;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.tab-bar-inner::-webkit-scrollbar { display: none; }

/* Individual tab */
.tab-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  height: 30px;
  background: rgba(255,255,255,.04);
  border: 1px solid rgba(255,255,255,.04);
  border-radius: 8px 8px 0 0;
  color: rgba(234,240,255,.65);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  max-width: 180px;
  min-width: 80px;
  transition: all .15s;
  flex-shrink: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  position: relative;
}

.tab-item:hover {
  background: rgba(255,255,255,.08);
  color: rgba(234,240,255,.9);
}

.tab-item.active {
  background: rgba(79,124,255,.15);
  border-color: rgba(79,124,255,.2);
  color: #fff;
  font-weight: 600;
}

/* Accent underline on active tab */
.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 4px;
  right: 4px;
  height: 2px;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  border-radius: 2px 2px 0 0;
}

.tab-item .tab-icon {
  font-size: 11px;
  opacity: .7;
  flex-shrink: 0;
}

.tab-item.active .tab-icon {
  opacity: 1;
}

.tab-item .tab-title {
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

/* Close button */
.tab-item .tab-close {
  background: none;
  border: none;
  color: rgba(234,240,255,.35);
  font-size: 14px;
  padding: 0;
  cursor: pointer;
  line-height: 1;
  border-radius: 4px;
  transition: .15s;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
}
.tab-item .tab-close:hover {
  background: rgba(255,77,109,.25);
  color: #ffd6de;
}

/* Popout button (touch devices) */
.tab-item .tab-popout {
  background: none;
  border: none;
  color: rgba(234,240,255,.35);
  font-size: 11px;
  padding: 0;
  cursor: pointer;
  line-height: 1;
  border-radius: 4px;
  transition: .15s;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
}
.tab-item .tab-popout:hover {
  background: rgba(79,124,255,.25);
  color: #a5d8ff;
}

/* Drag & drop feedback */
.tab-item.dragging {
  opacity: 0.4;
}
.tab-item.drag-over {
  border-left: 2px solid var(--accent);
}

/* Pinned tab (no close button, compact) */
.tab-item.pinned .tab-close {
  display: none;
}
.tab-item.pinned {
  min-width: 40px;
  max-width: 40px;
  padding: 6px 0;
  justify-content: center;
}
.tab-item.pinned .tab-title {
  display: none;
}

/* New tab button */
.tab-bar-btn {
  background: none;
  border: none;
  color: rgba(234,240,255,.45);
  font-size: 13px;
  padding: 5px 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: .15s;
  flex-shrink: 0;
}
.tab-bar-btn:hover {
  background: rgba(255,255,255,.08);
  color: #eaf0ff;
}

/* ─── TAB CONTAINER (replaces .erp-content-inner in shell mode) ─── */
.tab-container {
  position: relative;
  width: 100%;
  height: calc(100vh - var(--topbar-h) - var(--tabbar-h));
  overflow: hidden;
}

.tab-container iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: none;
  background: #0a0e1a;
  display: none;
}

.tab-container iframe.active {
  display: block;
}

/* ─── EMBED MODE (inside iframe) ─── */
body.embed-mode {
  overflow-x: hidden;
}
body.embed-mode .erp-content {
  margin-left: 0 !important;
  margin-top: 0 !important;
}
body.embed-mode .erp-content-inner {
  max-width: 1400px;
  margin-left: auto;
  margin-right: auto;
  /* Ensure first section content doesn't clip at top on tablet */
  padding-top: 16px;
}
@media (max-width: 991px) {
  body.embed-mode .erp-content-inner {
    padding: 12px 10px 60px;
  }
}

/* ─── TABLET / MOBILE ─── */
@media (max-width: 991px) {
  .tab-bar {
    left: 0 !important;
    background: #0a0e1c !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }
  body.theme-light .tab-bar {
    background: rgba(255, 255, 255, .95) !important;
    border-bottom-color: rgba(0,0,0,.1);
  }
  body.theme-light .tab-item {
    background: rgba(0,0,0,.03);
    border-color: rgba(0,0,0,.06);
    color: rgba(30,30,60,.6);
  }
  body.theme-light .tab-item:hover {
    background: rgba(0,0,0,.06);
    color: rgba(30,30,60,.85);
  }
  body.theme-light .tab-item.active {
    background: rgba(79,124,255,.1);
    border-color: rgba(79,124,255,.2);
    color: #1a1a2e;
  }
  body.theme-light .tab-item .tab-close:hover {
    background: rgba(255,77,109,.15);
    color: #d32f2f;
  }
  /* Ensure content is pushed below both topbar + tabbar on tablet */
  body.tabs-active .erp-content {
    margin-left: 0 !important;
    margin-top: calc(var(--topbar-h) + var(--tabbar-h)) !important;
  }
  .tab-container {
    height: calc(100vh - var(--topbar-h) - var(--tabbar-h)) !important;
    /* Prevent iOS rubber-band scrolling issues */
    -webkit-overflow-scrolling: touch;
  }
}

/* ─── CONTEXT MENU ─── */
.tab-ctx-menu {
  position: fixed;
  z-index: 99999;
  background: rgba(15, 20, 40, .95);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(255,255,255,.1);
  border-radius: 8px;
  padding: 4px 0;
  min-width: 180px;
  box-shadow: 0 8px 32px rgba(0,0,0,.5);
  display: none;
}
.tab-ctx-menu.open { display: block; }
.tab-ctx-item {
  padding: 8px 16px;
  color: rgba(234,240,255,.8);
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
}
.tab-ctx-item:hover {
  background: rgba(79,124,255,.15);
  color: #fff;
}
.tab-ctx-item.danger:hover {
  background: rgba(255,77,109,.15);
  color: #ffd6de;
}
.tab-ctx-sep {
  height: 1px;
  background: rgba(255,255,255,.08);
  margin: 4px 0;
}
/* Light theme */
body.theme-light .tab-ctx-menu {
  background: rgba(255,255,255,.95);
  border-color: rgba(0,0,0,.1);
  box-shadow: 0 8px 32px rgba(0,0,0,.15);
}
body.theme-light .tab-ctx-item {
  color: rgba(30,30,60,.8);
}
body.theme-light .tab-ctx-item:hover {
  background: rgba(79,124,255,.1);
  color: #1a1a2e;
}
body.theme-light .tab-ctx-item.danger:hover {
  background: rgba(255,77,109,.1);
  color: #d32f2f;
}

@media (max-width: 600px) {
  .tab-item {
    max-width: 120px;
    min-width: 60px;
    padding: 6px 6px;
  }
  .tab-item .tab-title {
    font-size: 11px;
  }
}

/* ─── DETACHED / FLOATING TAB PANELS ─── */
.detached-panel {
  position: fixed;
  z-index: 9500;
  display: flex;
  flex-direction: column;
  background: #0f1225;
  border: 1px solid rgba(255,255,255,.12);
  border-radius: 12px;
  box-shadow: 0 8px 48px rgba(0,0,0,.6);
  overflow: hidden;
  min-width: 360px;
  min-height: 240px;
}
.detached-panel-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  min-height: 36px;
  background: linear-gradient(135deg, rgba(79,124,255,.28), rgba(168,130,255,.18));
  border-bottom: 2px solid rgba(255,255,255,.14);
  cursor: grab;
  flex-shrink: 0;
  user-select: none;
  position: relative;
}
.detached-panel-header::before {
  content: "⋮⋮";
  letter-spacing: -2px;
  color: rgba(255,255,255,.38);
  font-size: 14px;
  font-weight: 700;
  margin-right: 2px;
}
.detached-panel-header.dragging { cursor: grabbing; }
.detached-panel-title {
  flex: 1;
  font-size: 13px;
  font-weight: 700;
  color: #eaf0ff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.detached-panel-btns {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}
.dp-btn {
  width: 26px; height: 26px;
  border-radius: 6px; border: none;
  background: rgba(255,255,255,.06);
  color: rgba(234,240,255,.5);
  font-size: 11px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: .12s;
}
.dp-btn:hover { background: rgba(255,255,255,.12); color: #eaf0ff; }
.dp-btn.dp-close:hover { background: rgba(239,68,68,.25); color: #f87171; }
.detached-panel-body {
  flex: 1;
  overflow: hidden;
}
.detached-panel-body iframe {
  width: 100%; height: 100%; border: none;
  background: #0a0e1a;
}
/* Resize edges */
.dp-rz { position: absolute; z-index: 2; }
.dp-rz-n  { top: -3px; left: 10px; right: 10px; height: 6px; cursor: n-resize; }
.dp-rz-s  { bottom: -3px; left: 10px; right: 10px; height: 6px; cursor: s-resize; }
.dp-rz-w  { left: -3px; top: 10px; bottom: 10px; width: 6px; cursor: w-resize; }
.dp-rz-e  { right: -3px; top: 10px; bottom: 10px; width: 6px; cursor: e-resize; }
.dp-rz-nw { top: -3px; left: -3px; width: 14px; height: 14px; cursor: nw-resize; }
.dp-rz-ne { top: -3px; right: -3px; width: 14px; height: 14px; cursor: ne-resize; }
.dp-rz-sw { bottom: -3px; left: -3px; width: 14px; height: 14px; cursor: sw-resize; }
.dp-rz-se { bottom: -3px; right: -3px; width: 14px; height: 14px; cursor: se-resize; }

/* Light theme */
body.theme-light .detached-panel {
  background: #f5f6fa;
  border-color: rgba(0,0,0,.12);
  box-shadow: 0 8px 48px rgba(0,0,0,.15);
}
body.theme-light .detached-panel-header {
  background: linear-gradient(135deg, rgba(79,124,255,.1), rgba(46,229,157,.05));
  border-bottom-color: rgba(0,0,0,.08);
}
body.theme-light .detached-panel-title { color: #1a1a2e; }
body.theme-light .dp-btn { background: rgba(0,0,0,.05); color: rgba(30,30,60,.5); }
body.theme-light .dp-btn:hover { background: rgba(0,0,0,.1); color: #1a1a2e; }

@media (max-width: 768px) {
  .detached-panel { min-width: 280px; }
}
