/* ==========================================================================
 * Block 1 of 2 — Reader three-pane shell (wide)
 * Description: Fixed viewport; per-pane scroll; Links dropdown on main toolbar
 * Version: 1.e
 * Revised: 12Jul26
 * ========================================================================== */

/* Lock document scroll — only panel interiors scroll */
html:has(body.page-reader),
body.page-reader {
  width: 100%;
  height: 100%;
  max-height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

body.page-reader {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  /* Fallback when :has() unsupported */
  position: fixed;
  inset: 0;
}

.reader-container {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  width: 100%;
  height: 100%;
  max-height: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.left-panel {
  display: flex;
  flex-direction: column;
  flex: 0 0 var(--left-width, 360px);
  width: var(--left-width, 360px);
  min-width: var(--left-width, 320px);
  max-width: var(--left-width, 420px);
  min-height: 0;
  height: 100%;
  border-right: 1px solid var(--border);
  overflow: hidden;
  z-index: 2;
  background: var(--bg);
}

.middle-panel,
.right-panel {
  display: flex;
  flex-direction: column;
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
}

.middle-panel {
  border-right: 1px solid var(--border);
  position: relative;
  z-index: 1;
}

.right-panel {
  z-index: 1;
}

.main-toolbar-stack {
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  min-width: 0;
  width: 100%;
  background: var(--toolbar-chrome, var(--bg));
  /* Borders live on each .panel-toolbar so height matches context + hypo */
  border-bottom: none;
}

/*
 * Single standard for main, context, and alignment with Hypothesis top bar:
 * fixed 40px border-box including 1px bottom edge (same total as hypo chrome).
 */
.panel-toolbar {
  height: var(--toolbar-height);
  min-height: var(--toolbar-height);
  max-height: var(--toolbar-height);
  background: var(--toolbar-chrome, var(--bg));
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 8px;
  gap: 4px;
  flex-shrink: 0;
  min-width: 0;
  width: 100%;
  box-sizing: border-box;
  /* visible so Links dropdown is not clipped */
  overflow: visible;
}

.main-toolbar-stack > .panel-toolbar + .panel-toolbar {
  /* Second row: top hairline only if needed — bottom border already present */
  border-top: none;
}

.toolbar-breadcrumb {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.15rem;
  overflow: hidden;
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
}
.toolbar-breadcrumb .crumb {
  color: var(--text-muted);
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 12rem;
}
.toolbar-breadcrumb .crumb:hover {
  color: var(--accent);
  text-decoration: underline;
}
.toolbar-breadcrumb .crumb.is-current {
  color: var(--text);
  font-weight: 500;
  pointer-events: none;
}
.toolbar-breadcrumb .crumb-sep {
  flex-shrink: 0;
  opacity: 0.55;
  user-select: none;
  padding: 0 0.1rem;
}
.toolbar-breadcrumb-empty {
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Vertical rule between toolbar clusters (CSS only — no inline SVG) */
.toolbar-divider {
  flex: 0 0 auto;
  align-self: stretch;
  width: 1px;
  margin: 6px 2px;
  background: var(--border);
  pointer-events: none;
}

/* Search + counter + prev/next stay as one cluster */
.toolbar-search-group {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 22rem;
}
.toolbar-search-group > .toolbar-btn {
  flex-shrink: 0;
}
.toolbar-search-group .search-counter {
  flex-shrink: 0;
}

.toolbar-search-wrap {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  min-width: 0;
  flex: 1 1 auto;
}
.toolbar-search-wrap .toolbar-search {
  flex: 1 1 auto;
  min-width: 5rem;
  width: 100%;
  max-width: none;
}

/* Hide native search cancel — we use our own clear control (avoid double X) */
.toolbar-search::-webkit-search-cancel-button,
.toolbar-search::-webkit-search-decoration {
  -webkit-appearance: none;
  appearance: none;
  display: none;
}

.toolbar-btn-clear {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
}
.toolbar-btn-clear.is-hidden {
  display: none;
}

/*
 * Consult selection cluster (left-aligned, tight):
 *   selected_term · clear (×) · provider icons
 * Term sizes to content and may grow/shrink; long terms ellipsize.
 * Providers sit immediately after the term (not pushed to the far right).
 */
.context-selection-wrap {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 2px;
  min-width: 0;
  flex: 1 1 auto;
  overflow: hidden;
}
.context-selection-wrap .context-selection {
  flex: 0 1 auto;
  min-width: 0;
  max-width: calc(100% - 10rem); /* room for clear + provider icons */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.context-selection-wrap .toolbar-btn-clear {
  flex: 0 0 auto;
}
.context-selection-wrap .context-provider-buttons {
  flex: 0 0 auto;
  gap: 0;
}

/* Keep Links dropdown above content */
.main-toolbar-stack {
  position: relative;
  z-index: 30;
  overflow: visible;
}
#main-toolbar-row2 {
  overflow: visible;
  z-index: 40;
}
#context-toolbar {
  overflow: hidden;
}

.toolbar-menu {
  position: relative;
  flex-shrink: 0;
}

.toolbar-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 40;
  min-width: 11.5rem;
  padding: 0.5rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.18);
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.toolbar-dropdown.is-hidden {
  display: none;
}

.toolbar-dropdown-row {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.toolbar-dropdown-label {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}

.toolbar-book-title {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
  font-weight: 600;
}

.toolbar-search {
  height: 28px;
  border: none;
  border-radius: 6px;
  padding: 0 0.5rem;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  box-sizing: border-box;
}

/* Keep text-size clear of Hypothesis chrome on narrow layouts (extra right pad) */
html[data-layout='narrow'] #main-toolbar {
  padding-left: 8px;
  padding-right: 48px; /* 8px base + 40px */
}
html[data-layout='narrow'] #btn-font-cycle {
  margin-left: 0;
}
.toolbar-search:focus {
  outline: none;
}
.toolbar-search:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Keep font control on the trailing edge of row 1 */
#main-toolbar > .toolbar-divider + #btn-font-cycle {
  margin-left: 0;
  flex-shrink: 0;
}
#main-toolbar > .toolbar-search-group + .toolbar-divider {
  margin-left: auto;
}

.search-counter {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* Only the book column scrolls */
.middle-content {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.context-iframe-container {
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

/* ==========================================================================
 * Block 2 of 2 — Narrow mode + viewport alert
 * Description: Main fills viewport; zero research links; limited-experience overlay
 * Version: 1.b
 * Revised: 10Jul26
 * ========================================================================== */

html[data-layout='narrow'] .right-panel {
  display: none !important;
}

html[data-layout='narrow'] .left-panel {
  display: none !important;
}

html[data-layout='narrow'] .middle-panel {
  border-right: none;
  flex: 1 1 100%;
  width: 100%;
  max-width: 100%;
}

html[data-layout='narrow'] #links-menu {
  display: none !important;
}

html[data-layout='narrow'] .book-root a[data-link-provider],
html[data-layout='narrow'] .book-root a[href^='l:'],
html[data-layout='narrow'] .book-root a[href^='w:'],
html[data-layout='narrow'] .book-root a[href^='d:'],
html[data-layout='narrow'] .book-root a[href^='m:'] {
  color: inherit !important;
  text-decoration: none !important;
  pointer-events: none !important;
  cursor: text !important;
  border: none !important;
  font-weight: inherit !important;
}

.viewport-alert {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  background: rgba(0, 0, 0, 0.55);
}

.viewport-alert.is-hidden {
  display: none;
}

.viewport-card {
  max-width: 26rem;
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.5rem 1.35rem;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}

.viewport-card h2 {
  margin: 0 0 0.5rem;
  font-size: 1.15rem;
}

.viewport-card p {
  margin: 0 0 1rem;
  color: var(--text-muted);
  font-size: 0.95rem;
  line-height: 1.5;
}
.viewport-hypo-tip {
  font-size: 0.88rem !important;
  color: var(--text) !important;
}

.viewport-dismiss {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 2.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.viewport-dismiss:hover {
  filter: brightness(1.05);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.provider-toggle.is-off {
  opacity: 0.4;
}

.density-chip {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  min-width: 2.25rem;
  text-align: center;
}
