/* =========================================================
   PostgreSQL 中文手册 - 三栏布局样式
   pg_layout.css
   支持 PC 端和移动端响应式布局
   ========================================================= */

/* --- Reset & Base ---------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: "Open Sans", Arial, "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", "STHeiti", SimSun, sans-serif;
  font-size: 14px;
  background: #FDFDFD;
  color: #353A3F;
}

/* --- Layout Skeleton ------------------------------------ */
#pg-layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* =========================================================
   TOP NAVBAR
   ========================================================= */
#pg-topbar {
  position: sticky;
  top: 0;
  z-index: 1000;
  background: #336791;
  color: #fff;
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
  width: 100%;
}

#pg-topbar-inner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 12px;
  padding: 6px 14px;
}

/* 版本链接区 */
#pg-topbar-left {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 6px;
  flex: 1 1 auto;
}

.pg-topbar-label {
  color: #bdd4e7;
  font-size: 0.82em;
  white-space: nowrap;
  margin-right: 4px;
}

.pg-ver-link {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 3px;
  font-size: 0.82em;
  color: #fff !important;
  text-decoration: none !important;
  border: 1px solid transparent;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}

.pg-ver-link:visited {
  color: #fff !important;
}

.pg-ver-link:hover {
  background: rgba(255,255,255,0.18);
  color: #fff !important;
  text-decoration: none !important;
}

.pg-ver-link.pg-ver-current {
  background: #2d7ab8;
  color: #fff;
  border-color: #5ba3d9;
  font-weight: bold;
}

/* 版本下拉框 */
.pg-ver-select {
  appearance: none;
  -webkit-appearance: none;
  background: rgba(255,255,255,0.15);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.35);
  border-radius: 4px;
  padding: 3px 28px 3px 8px;
  font-size: 0.82em;
  cursor: pointer;
  outline: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23ffffff'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 6px center;
  transition: background 0.15s, border-color 0.15s;
  min-width: 80px;
}

.pg-ver-select:hover {
  background-color: rgba(255,255,255,0.25);
  border-color: rgba(255,255,255,0.5);
}

.pg-ver-select:focus {
  border-color: #5ba3d9;
  box-shadow: 0 0 0 2px rgba(91,163,217,0.35);
}

.pg-ver-select option {
  background: #336791;
  color: #fff;
}

/* 右侧链接区 */
#pg-topbar-right {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 8px;
  flex: 0 0 auto;
}

#pg-topbar-right a {
  color: #fff;
  text-decoration: none;
  font-size: 0.82em;
  white-space: nowrap;
  padding: 2px 6px;
  border-radius: 3px;
  transition: background 0.15s, color 0.15s;
}

#pg-topbar-right a:visited {
  color: #fff;
}

#pg-topbar-right a:hover {
  background: rgba(255,255,255,0.18);
  color: #fff;
  text-decoration: none;
}

/* 搜索框 */
#pg-search-form {
  display: flex;
  align-items: center;
  gap: 0;
  flex: 0 0 auto;
}

#pg-search-form input[type="text"] {
  height: 28px;
  width: 200px;
  max-width: 40vw;
  padding: 0 8px;
  border: 1px solid #5ba3d9;
  border-right: none;
  border-radius: 3px 0 0 3px;
  background: rgba(255,255,255,0.12);
  color: #fff;
  font-size: 0.82em;
  outline: none;
}

#pg-search-form input[type="text"]::placeholder {
  color: rgba(255,255,255,0.45);
}

#pg-search-form input[type="text"]:focus {
  background: rgba(255,255,255,0.2);
}

#pg-search-form button {
  height: 28px;
  padding: 0 10px;
  background: #2d7ab8;
  color: #fff;
  border: 1px solid #5ba3d9;
  border-radius: 0 3px 3px 0;
  font-size: 0.82em;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}

#pg-search-form button:hover {
  background: #245f91;
}

/* =========================================================
   BODY AREA  =  SIDEBAR  +  CONTENT
   ========================================================= */
#pg-body {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* =========================================================
   SIDEBAR
   ========================================================= */
#pg-sidebar {
  width: 260px;
  min-width: 220px;
  max-width: 320px;
  flex-shrink: 0;
  background: #f4f7fb;
  border-right: 1px solid #d8e3ed;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.25s ease, min-width 0.25s ease, opacity 0.2s;
  position: relative;
}

#pg-sidebar.collapsed {
  width: 0 !important;
  min-width: 0 !important;
  opacity: 0;
  pointer-events: none;
  overflow: hidden;
}

/* 侧边栏标题栏 */
#pg-sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px 8px 14px;
  background: #5b8db8;
  color: #fff;
  font-size: 0.88em;
  font-weight: bold;
  flex-shrink: 0;
  white-space: nowrap;
  overflow: hidden;
}

#pg-sidebar-header span {
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 折叠按钮（在侧边栏标题栏内） */
#pg-sidebar-toggle-inner {
  background: transparent;
  border: none;
  color: rgba(255,255,255,0.8);
  cursor: pointer;
  font-size: 1.1em;
  padding: 0 4px;
  line-height: 1;
  flex-shrink: 0;
}

#pg-sidebar-toggle-inner:hover {
  color: #fff;
}

/* 目录导航滚动区 */
#pg-toc-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 6px 0 20px 0;
}

#pg-toc-scroll::-webkit-scrollbar {
  width: 5px;
}

#pg-toc-scroll::-webkit-scrollbar-thumb {
  background: #c0d0e0;
  border-radius: 3px;
}

/* TOC 列表 */
.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc-list .toc-list {
  /* 子级缩进 */
  padding-left: 14px;
  display: none; /* 默认折叠 */
  background: rgba(0,0,0,0.02);
}

.toc-list .toc-list.open {
  display: block;
}

.toc-item {
  position: relative;
}

/* 行容器 */
.toc-row {
  display: flex;
  align-items: center;
  cursor: pointer;
  border-radius: 0;
  transition: background 0.12s;
  user-select: none;
}

.toc-row:hover {
  background: #e4edf7;
}

.toc-row.active {
  background: #d0e4f5;
}

.toc-row.active > .toc-link {
  color: #0a5a99;
  font-weight: bold;
}

/* 展开/折叠箭头 */
.toc-arrow {
  flex-shrink: 0;
  width: 20px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7em;
  color: #7a9aba;
  transition: transform 0.2s;
}

.toc-arrow.open {
  transform: rotate(90deg);
}

.toc-arrow.leaf {
  color: transparent;
  cursor: default;
}

/* 目录链接 */
.toc-link {
  flex: 1;
  display: block;
  padding: 5px 12px 5px 2px;
  color: #2a4a6a;
  text-decoration: none;
  font-size: 0.87em;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toc-link:hover {
  text-decoration: none;
  color: #EC5800;
}

/* 一级节点样式（顶层目录：前言、I.教程...） */
.toc-list > .toc-item > .toc-row > .toc-link {
  font-weight: bold;
  font-size: 0.9em;
  color: #1a3f5c;
  padding-top: 7px;
  padding-bottom: 7px;
}

/* =========================================================
   SIDEBAR TOGGLE BUTTON  (浮动，在侧边栏收起时显示)
   ========================================================= */
#pg-sidebar-open-btn {
  display: none; /* 默认隐藏，由 JS 控制 */
  position: fixed;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  z-index: 900;
  background: #5b8db8;
  color: #fff;
  border: none;
  border-radius: 0 6px 6px 0;
  width: 22px;
  height: 60px;
  cursor: pointer;
  font-size: 0.9em;
  writing-mode: vertical-rl;
  letter-spacing: 2px;
  box-shadow: 2px 0 6px rgba(0,0,0,0.15);
  padding: 0;
  transition: background 0.15s;
}

#pg-sidebar-open-btn:hover {
  background: #4a7fa8;
}

/* =========================================================
   MAIN CONTENT
   ========================================================= */
#pg-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px 30px 40px 28px;
  min-width: 0;
}

/* 隐藏原有的导航头/尾（上一页/下一页 table） */
#pg-content .navheader,
#pg-content .navfooter,
#pg-content .NAVHEADER,
#pg-content .NAVFOOTER {
  display: none;
}

/* 正文内容样式修正 */
#pg-content h1, #pg-content h2 {
  color: #1a3f5c;
}

#pg-content .titlepage h2.title,
#pg-content .refnamediv h2 {
  color: #EC5800;
}

#pg-content pre, #pg-content .programlisting, #pg-content .screen {
  max-width: 100%;
  overflow-x: auto;
}

/* 表格响应式 */
#pg-content .table,
#pg-content .informaltable {
  overflow-x: auto;
  display: block;
  max-width: 100%;
}

/* =========================================================
   RESPONSIVE  — 移动端
   ========================================================= */
@media (max-width: 768px) {
  #pg-sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 800;
    /* 顶部导航高度由 JS 动态计算并设置 --topbar-height */
    top: var(--topbar-height, 48px);
    width: 80vw !important;
    max-width: 300px !important;
    box-shadow: 3px 0 12px rgba(0,0,0,0.2);
    opacity: 1;
    pointer-events: auto;
    transition: transform 0.28s ease, opacity 0.2s;
    transform: translateX(0);
  }

  #pg-sidebar.collapsed {
    width: 80vw !important;
    max-width: 300px !important;
    transform: translateX(-100%);
    opacity: 0;
    pointer-events: none;
  }

  /* 移动端蒙层 */
  #pg-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.35);
    z-index: 799;
  }

  #pg-overlay.visible {
    display: block;
  }

  #pg-sidebar-open-btn {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  #pg-content {
    padding: 14px 14px 40px 14px;
  }

  #pg-topbar-inner {
    flex-wrap: wrap;
    padding: 5px 8px;
    gap: 4px 8px;
  }

  #pg-search-form input[type="text"] {
    width: 130px;
  }
}

/* 非移动端：当侧边栏收起时，显示浮动展开按钮 */
@media (min-width: 769px) {
  #pg-sidebar.collapsed ~ * #pg-sidebar-open-btn,
  body.sidebar-collapsed #pg-sidebar-open-btn {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  #pg-overlay {
    display: none !important;
  }
}

/* =========================================================
   SCROLLBAR GLOBAL
   ========================================================= */
#pg-content::-webkit-scrollbar { width: 7px; }
#pg-content::-webkit-scrollbar-thumb {
  background: #c0d0e0;
  border-radius: 4px;
}

/* =========================================================
   PRINT
   ========================================================= */
@media print {
  #pg-topbar, #pg-sidebar, #pg-sidebar-open-btn { display: none !important; }
  #pg-content { padding: 0; }
}
