*,
*::before,
*::after {
  box-sizing: border-box;
}

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

body {
  font-family: var(--font-sans);
  color: var(--color-text);
  background: var(--color-bg);
}

a {
  color: var(--color-accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-rows: var(--topbar-height) 1fr var(--footer-height);
  grid-template-areas:
    "sidebar top"
    "sidebar main"
    "sidebar footer";
  height: 100vh;
  height: 100dvh;
  width: 100%;
  max-height: 100vh;
  max-height: 100dvh;
  position: relative;
  overflow: hidden;
}

.app-shell.right-open {
  grid-template-columns: var(--sidebar-width) 1fr var(--right-panel-width);
  grid-template-areas:
    "sidebar top right"
    "sidebar main right"
    "sidebar footer right";
}

/* —— Left sidebar —— */
.sidebar {
  grid-area: sidebar;
  background: var(--color-sidebar);
  color: var(--color-sidebar-text);
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  z-index: var(--z-sidebar);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.9rem 1rem;
  min-height: var(--topbar-height);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.brand-mark {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  clip-path: polygon(20% 10%, 80% 0%, 100% 70%, 50% 100%, 0% 70%);
}

.brand-text {
  font-weight: 700;
  font-size: 1.05rem;
  color: #fff;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.sidebar-nav {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0.75rem 0.5rem;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.22) transparent;
}

.sidebar-brand,
.sidebar-footer-note {
  flex-shrink: 0;
}

.nav-group-label {
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(197, 201, 214, 0.55);
  padding: 0.75rem 0.75rem 0.35rem;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.65rem 0.75rem;
  margin: 0.15rem 0;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: inherit;
  cursor: pointer;
  text-align: left;
}

.nav-item:hover {
  background: var(--color-sidebar-hover);
}

.nav-item.active {
  background: var(--color-sidebar-active);
  color: #fff;
}

.nav-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  opacity: 0.9;
}

.nav-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-sub {
  margin-left: 0.5rem;
  padding-left: 0.5rem;
  border-left: 1px solid rgba(255, 255, 255, 0.1);
  display: none;
}

.nav-module.expandable.open > .nav-sub {
  display: block;
}

.nav-module.expandable > .nav-parent .nav-label::after {
  content: " ▸";
  opacity: 0.55;
  font-size: 0.75em;
}

.nav-module.expandable.open > .nav-parent .nav-label::after {
  content: " ▾";
}

.nav-sub .nav-item {
  padding: 0.45rem 0.75rem;
  font-size: 0.9rem;
}

/* —— DB Modules section (catalog rows created in the Module / SubModule masters) —— */
.nav-divider {
  height: 1px;
  margin: 0.85rem 0.5rem 0.15rem;
  background: rgba(255, 255, 255, 0.16);
  flex-shrink: 0;
}

.nav-label {
  min-width: 0;
}

/* Admin-entered labels can be long: wrap them instead of cutting them off. */
.nav-item-db .nav-label {
  white-space: normal;
  overflow: visible;
  overflow-wrap: anywhere;
  line-height: 1.3;
}

.nav-db-badge {
  margin-left: auto;
  flex-shrink: 0;
  padding: 0.05rem 0.35rem;
  border: 1px solid rgba(255, 255, 255, 0.32);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  color: #e5e7eb;
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  line-height: 1.4;
}

.nav-item.active .nav-db-badge {
  border-color: rgba(255, 255, 255, 0.6);
  color: #fff;
}

/* —— Coming-soon placeholder for unavailable DB module actions —— */
.coming-soon {
  max-width: 560px;
  margin: 2rem auto;
  padding: 2rem 1.5rem;
  background: var(--color-surface);
  border: 1px dashed var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  text-align: center;
  color: var(--color-text);
  overflow-wrap: anywhere;
}

.tab-panel-frame > .coming-soon {
  margin: 1rem auto;
  width: calc(100% - 2rem);
  overflow: auto;
}

.coming-soon-icon {
  width: 40px;
  height: 40px;
  color: var(--color-muted);
}

.coming-soon-title {
  margin: 0.75rem 0 0.5rem;
  font-size: 1.15rem;
}

.coming-soon-name {
  margin: 0 0 0.75rem;
  font-weight: 600;
}

.coming-soon-url,
.coming-soon-reason {
  margin: 0.35rem 0 0;
  font-size: 0.85rem;
  color: var(--color-muted);
}

.coming-soon-url code {
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  background: var(--color-surface-muted);
  color: var(--color-text);
  word-break: break-all;
}

.sidebar-footer-note {
  padding: 0.75rem 1rem;
  font-size: 0.75rem;
  opacity: 0.55;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

/* —— Top bar —— */
.topbar {
  grid-area: top;
  background: var(--color-topbar);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0 1rem;
  z-index: 10;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--color-muted);
  cursor: pointer;
}

.btn-icon:hover {
  background: var(--color-surface-muted);
  color: var(--color-text);
}

.hamburger {
  display: none;
}

.search-wrap {
  flex: 1;
  max-width: 480px;
  position: relative;
}

.search-wrap input {
  width: 100%;
  height: 38px;
  padding: 0 0.9rem 0 2.4rem;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  background: var(--color-surface-muted);
  color: var(--color-text);
  outline: none;
}

.search-wrap input:focus {
  border-color: var(--color-accent);
  background: var(--color-surface);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.search-wrap .search-icon {
  position: absolute;
  left: 0.85rem;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  color: var(--color-muted);
  pointer-events: none;
}

.topbar-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.notif-wrap {
  position: relative;
}

.notif-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-danger);
  border: 2px solid var(--color-topbar);
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0.5rem 0.25rem 0.25rem;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--color-text);
  cursor: pointer;
}

.user-chip:hover {
  background: var(--color-surface-muted);
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--color-accent);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 0.75rem;
  font-weight: 700;
}

.user-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  line-height: 1.15;
}

.user-name {
  font-size: 0.85rem;
  font-weight: 600;
}

.user-role {
  font-size: 0.7rem;
  color: var(--color-muted);
}

/* —— Main / middle —— */
.main-area {
  grid-area: main;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.tab-bar {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.4rem 0.75rem 0;
  background: var(--color-bg);
  border-bottom: 1px solid var(--color-border);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  flex-shrink: 0;
}

.tab {
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
  gap: 0.4rem;
  max-width: 180px;
  padding: 0.45rem 0.65rem;
  border: 1px solid transparent;
  border-bottom: 0;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  background: transparent;
  color: var(--color-muted);
  cursor: pointer;
  white-space: nowrap;
}

.tab:hover {
  color: var(--color-text);
  background: var(--color-surface-muted);
}

.tab.active {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border);
  box-shadow: 0 1px 0 var(--color-surface);
  position: relative;
  z-index: 1;
}

.tab-title {
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 0.85rem;
}

.tab-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--color-muted);
  cursor: pointer;
  font-size: 0.85rem;
  line-height: 1;
  padding: 0;
}

.tab-close:hover {
  background: rgba(0, 0, 0, 0.08);
  color: var(--color-text);
}

.tab-panels {
  /* Sticky footers inside panels offset by this so they sit flush with the scrollport bottom. */
  --panel-pad-bottom: 1.5rem;
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--color-bg);
  padding: 1rem 1.25rem var(--panel-pad-bottom);
}

.tab-panel {
  display: none;
  min-height: 100%;
}

.tab-panel.active {
  display: block;
}

/* Frame tabs: the hosted page owns its own scroll region. */
.tab-panels.frame-active {
  padding: 0;
  overflow: hidden;
}

.tab-panel-frame.active {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.tab-frame {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  border: 0;
  display: block;
  background: var(--color-bg);
}

/* —— Right panel (hidden by default) —— */
.right-panel {
  grid-area: right;
  display: none;
  background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}

.app-shell.right-open .right-panel {
  display: flex;
}

.right-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--color-border);
  font-weight: 600;
}

.right-panel-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1rem;
  color: var(--color-muted);
  font-size: 0.9rem;
}

/* —— Footer —— */
.app-footer {
  grid-area: footer;
  background: var(--color-footer);
  border-top: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  font-size: 0.75rem;
  color: var(--color-muted);
}

/* —— Overlay for mobile sidebar —— */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: calc(var(--z-sidebar) - 1);
}

.sidebar-overlay.visible {
  display: block;
}

/* —— Global search palette —— */
.search-palette {
  display: none;
  position: fixed;
  inset: 0;
  z-index: var(--z-search);
  background: rgba(15, 23, 42, 0.4);
  align-items: flex-start;
  justify-content: center;
  padding-top: 12vh;
}

.search-palette.open {
  display: flex;
}

.search-palette-card {
  width: min(520px, 92vw);
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  border: 1px solid var(--color-border);
  overflow: hidden;
}

.search-palette-card input {
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--color-border);
  padding: 0.9rem 1rem;
  background: transparent;
  color: var(--color-text);
  outline: none;
}

.search-results {
  max-height: 280px;
  overflow: auto;
  margin: 0;
  padding: 0.35rem;
  list-style: none;
}

.search-results li button {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 0.65rem 0.75rem;
  border-radius: var(--radius-sm);
  color: var(--color-text);
  cursor: pointer;
}

.search-results li button:hover,
.search-results li button.active {
  background: var(--color-accent-soft);
}

.search-empty {
  padding: 1rem;
  color: var(--color-muted);
  font-size: 0.9rem;
}

/* —— Toast —— */
.toast {
  position: fixed;
  right: 1rem;
  bottom: 1.5rem;
  z-index: var(--z-toast);
  min-width: 220px;
  max-width: 360px;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  background: var(--color-text);
  color: #fff;
  box-shadow: var(--shadow-md);
  font-size: 0.9rem;
}

.toast.error {
  background: var(--color-danger);
}

.toast[hidden] {
  display: none !important;
}

/* —— User menu —— */
.user-menu {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 180px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  padding: 0.35rem;
  z-index: 20;
}

.user-menu.open {
  display: block;
}

.user-menu-wrap {
  position: relative;
}

.user-menu button {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 0.55rem 0.7rem;
  border-radius: 4px;
  color: var(--color-text);
  cursor: pointer;
}

.user-menu button:hover {
  background: var(--color-surface-muted);
}
