:root {
  --library-text: #17233a;
  --library-muted: #70819a;
  --library-border: rgba(148, 163, 184, .22);
  --library-panel: rgba(255, 255, 255, .86);
  --library-accent: #347ff0;
  --library-accent-soft: #edf5ff;
  --library-danger: #dc5d55;
}

body.mzpi-library-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #edf6fd 0%, #e8f2fb 52%, #eaf4fc 100%) !important;
  color: var(--library-text);
}

body.mzpi-library-page .gateway-footer { display: none !important; }
body.mzpi-library-page .mzpi-library > .library-heading { display: none !important; }

.mzpi-library {
  width: min(1350px, calc(100vw - 340px));
  min-height: calc(100vh - 100px);
  margin-left: max(300px, calc(260px + (100vw - 1610px) / 2));
  padding: 100px 32px 64px;
}

.library-browser {
  max-width: 1350px;
  margin: 0 auto;
}

.library-refresh,
.library-back,
.library-view-switch button,
.library-search button,
.library-error button,
.library-selection button,
.library-modal button {
  border: 0;
  cursor: pointer;
  font: inherit;
}

.library-refresh,
.library-back {
  display: inline-flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--library-border);
  border-radius: 12px;
  background: rgba(255, 255, 255, .72);
  color: #41536f;
  font-size: .82rem;
  font-weight: 700;
}

.library-refresh:hover,
.library-back:not(:disabled):hover {
  border-color: rgba(52, 127, 240, .32);
  background: #fff;
  color: var(--library-accent);
}

.library-back:disabled { cursor: not-allowed; opacity: .42; }

.library-browser {
  position: relative;
  min-width: 0;
  display: flex;
  height: clamp(480px, calc(100vh - 190px), 760px);
  min-height: 0;
  flex-direction: column;
  padding: 20px;
  border: 1px solid rgba(255, 255, 255, .6);
  border-radius: 18px;
  background: var(--library-panel);
  box-shadow: 0 12px 32px rgba(36, 70, 115, .07);
}

.library-browser__topline {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 42px;
}

.library-breadcrumb {
  display: flex;
  flex: 1 1 auto;
  align-items: center;
  min-width: 0;
  gap: 4px;
  overflow-x: auto;
  scrollbar-width: none;
}

.library-breadcrumb::-webkit-scrollbar { display: none; }

.library-breadcrumb button {
  flex: 0 0 auto;
  max-width: 180px;
  overflow: hidden;
  padding: 6px 7px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--library-muted);
  cursor: pointer;
  font: inherit;
  font-size: .88rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.library-breadcrumb button:hover,
.library-breadcrumb button.is-current { background: var(--library-accent-soft); color: var(--library-accent); }
.library-breadcrumb i { flex: 0 0 auto; color: #a4b1c0; font-size: .62rem; }

.library-view-switch {
  display: inline-flex;
  flex: 0 0 auto;
  padding: 3px;
  border: 1px solid var(--library-border);
  border-radius: 10px;
  background: rgba(255, 255, 255, .78);
}

.library-view-switch button {
  display: grid;
  width: 30px;
  height: 28px;
  place-items: center;
  border-radius: 7px;
  background: transparent;
  color: #8190a4;
}

.library-view-switch button.is-active { background: #e7f0ff; color: var(--library-accent); }

.library-search {
  display: flex;
  align-items: center;
  gap: 10px;
  width: min(560px, 100%);
  min-height: 44px;
  margin: 18px 0 14px;
  padding: 0 6px 0 15px;
  border: 1px solid var(--library-border);
  border-radius: 12px;
  background: rgba(255, 255, 255, .78);
}

.library-search:focus-within { border-color: rgba(52, 127, 240, .42); box-shadow: 0 0 0 3px rgba(52, 127, 240, .09); }
.library-search > i { color: var(--library-accent); }
.library-search input { flex: 1 1 auto; min-width: 0; height: 40px; border: 0; outline: 0; background: transparent; color: var(--library-text); font: inherit; font-size: .88rem; }
.library-search input::placeholder { color: #95a3b6; }
.library-search button { flex: 0 0 auto; min-height: 32px; padding: 0 13px; border-radius: 8px; background: var(--library-accent); color: #fff; font-size: .78rem; font-weight: 700; }
.library-search button:hover { background: #216ae0; }

.library-directory-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; min-height: 38px; margin: 0; }
.library-directory-actions:empty { display: none; }
.library-directory-actions button { display: inline-flex; align-items: center; justify-content: center; gap: 7px; min-height: 38px; padding: 0 11px; border: 1px solid rgba(52, 127, 240, .16); border-radius: 10px; background: #f4f8ff; color: #3974cc; cursor: pointer; font: inherit; font-size: .76rem; font-weight: 700; }
.library-directory-actions button:hover { border-color: rgba(52, 127, 240, .36); background: #e8f1ff; }
.library-directory-actions button.is-active { border-color: rgba(52, 127, 240, .3); background: #e7f0ff; color: var(--library-accent); }
.library-directory-actions button i,
.library-refresh i,
.library-view-switch button i { display: inline-flex; align-items: center; line-height: 1; }

.library-notice { min-height: 0; margin: 0 0 12px; color: var(--library-muted); font-size: .82rem; }
.library-notice:empty { display: none; }
.library-notice.is-warning { color: #a36b0e; }

.library-loading,
.library-empty,
.library-error {
  display: flex;
  min-height: 240px;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 10px;
  padding: 30px;
  color: var(--library-muted);
  text-align: center;
}

.library-loading {
  position: absolute;
  z-index: 2;
  inset: 110px 20px 20px;
  min-height: 0;
  border-radius: 12px;
  background: rgba(255, 255, 255, .68);
  backdrop-filter: blur(3px);
  pointer-events: none;
}

.library-loading[hidden],
.library-empty[hidden],
.library-error[hidden],
.library-selection[hidden],
.library-modal[hidden],
.library-menu[hidden] { display: none !important; }

.library-loading i { color: var(--library-accent); animation: library-spin 1s linear infinite; }
.library-empty > i { color: #9db5d8; font-size: 2rem; }
.library-empty strong,
.library-error strong { color: var(--library-text); }
.library-error > i { color: #e09b53; font-size: 1.7rem; }
.library-error button { margin-top: 6px; padding: 8px 14px; border-radius: 9px; background: #eaf2ff; color: var(--library-accent); font-weight: 700; }

.library-items {
  min-height: 0;
  flex: 1 1 auto;
  display: grid;
  align-content: start;
  gap: 12px;
  overflow-y: auto;
  scroll-behavior: auto;
  padding: 2px 4px 6px;
  scrollbar-color: rgba(112, 129, 154, .34) transparent;
  scrollbar-width: thin;
}

.library-items.is-grid { grid-template-columns: repeat(auto-fill, minmax(178px, 1fr)); }
.library-items.is-list { grid-template-columns: 1fr; gap: 7px; }

.library-item {
  position: relative;
  display: flex;
  min-width: 0;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, .62);
  color: inherit;
  cursor: pointer;
  text-align: left;
  transition: border-color .18s ease, background-color .18s ease, transform .18s ease, box-shadow .18s ease;
}

.library-items.is-grid .library-item { min-height: 122px; flex-direction: column; align-items: center; padding: 16px; border-radius: 14px; }
.library-items.is-list .library-item { align-items: center; min-height: 62px; padding: 10px 16px; border-radius: 11px; }
.library-item:hover,
.library-item.is-selected { border-color: rgba(52, 127, 240, .3); background: #fff; box-shadow: 0 7px 18px rgba(48, 82, 128, .08); }
.library-item:hover { transform: translateY(-1px); }

.library-item__icon { display: grid; flex: 0 0 auto; width: 42px; height: 42px; place-items: center; border-radius: 11px; background: #edf4ff; color: #5e84c9; font-size: 1.3rem; }
.library-item.is-folder .library-item__icon { background: #fff4d8; color: #e9ab35; }
.library-item.is-image .library-item__icon { background: #e8f8f4; color: #38a987; }
.library-item.is-pdf .library-item__icon { background: #fff0f0; color: #dc6464; }
.library-item.is-office .library-item__icon { background: #ebf3ff; color: #4780df; }
.library-items.is-grid .library-item__icon { margin-bottom: 0; }
.library-items.is-list .library-item__icon { margin-right: 12px; }

.library-item__content { min-width: 0; }
.library-items.is-grid .library-item__content { width: 100%; margin-top: 7px; text-align: center; }
.library-items.is-list .library-item__content { flex: 1 1 auto; }
.library-item__name { display: -webkit-box; overflow: hidden; color: var(--library-text); font-size: .9rem; font-weight: 700; line-height: 1.45; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
.library-items.is-list .library-item__name { -webkit-line-clamp: 1; }
.library-item__select { position: absolute; top: 10px; right: 10px; display: none; width: 22px; height: 22px; place-items: center; border: 1px solid #b7c5d7; border-radius: 50%; background: rgba(255, 255, 255, .92); color: transparent; font-size: .66rem; }
.library-items.is-selection-mode .library-item__select { display: grid; }
.library-item.is-selected .library-item__select { border-color: var(--library-accent); background: var(--library-accent); color: #fff; }
.library-selection {
  position: fixed;
  z-index: 180;
  right: max(28px, calc((100vw - 1350px) / 2));
  bottom: 24px;
  left: max(calc(var(--sidebar-width) + 28px), calc((100vw - 1350px + var(--sidebar-width)) / 2));
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 58px;
  padding: 10px 14px 10px 18px;
  border: 1px solid rgba(148, 163, 184, .2);
  border-radius: 15px;
  background: rgba(255, 255, 255, .94);
  box-shadow: 0 14px 34px rgba(28, 54, 90, .16);
  backdrop-filter: blur(12px);
}

.library-selection__file { display: flex; flex: 1 1 auto; align-items: center; min-width: 0; gap: 9px; color: var(--library-text); font-size: .84rem; font-weight: 700; }
.library-selection__file i { color: var(--library-accent); }
.library-selection__file span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.library-selection__actions { display: inline-flex; flex: 0 0 auto; gap: 8px; }
.library-selection__actions button { min-height: 34px; padding: 0 11px; border-radius: 9px; background: #edf4ff; color: var(--library-accent); font-size: .76rem; font-weight: 700; }
.library-selection__actions button:hover { background: #dceaff; }
.library-selection__close { display: grid; flex: 0 0 auto; width: 32px; height: 32px; place-items: center; border-radius: 9px; background: #f3f6fa; color: #8190a4; }

.library-modal { position: fixed; z-index: 3000; inset: 0; display: grid; place-items: center; padding: 28px; }
.library-modal__backdrop { position: absolute; inset: 0; background: rgba(8, 18, 34, .68); }
.library-modal__dialog { position: relative; z-index: 1; display: flex; width: min(960px, 100%); max-height: min(86vh, 900px); flex-direction: column; overflow: hidden; border-radius: 16px; background: #fff; box-shadow: 0 24px 70px rgba(0, 0, 0, .34); }
.library-modal__dialog header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 13px 16px; border-bottom: 1px solid #edf0f5; }
.library-modal__dialog h2 { overflow: hidden; margin: 0; font-size: .95rem; text-overflow: ellipsis; white-space: nowrap; }
.library-modal__dialog header button { display: grid; width: 32px; height: 32px; place-items: center; border-radius: 8px; background: #f2f5fa; color: #536276; }
.library-modal__dialog img { display: block; width: 100%; min-height: 0; object-fit: contain; }
.library-modal__dialog img[hidden],
.library-modal__dialog iframe[hidden] { display: none; }

.library-preview-modal { padding: 22px; }
.library-preview-modal__dialog { width: min(1120px, 100%); height: min(82vh, 820px); max-height: none; }
.library-preview-modal__header { flex: 0 0 auto; }
.library-preview-modal__header > div:first-child { min-width: 0; }
.library-preview-modal__header p { margin: 0 0 4px; color: var(--library-muted); font-size: .72rem; font-weight: 700; }
.library-preview-modal__header h2 { max-width: min(720px, 64vw); }
.library-preview-modal__header-actions { display: inline-flex; flex: 0 0 auto; gap: 8px; }
.library-preview-stage { position: relative; display: grid; flex: 1 1 auto; min-height: 0; place-items: center; overflow: auto; padding: 20px; background: #eef2f6; }
.library-preview-stage img { max-width: 100%; max-height: 100%; transition: transform .2s ease; }
.library-preview-stage iframe { width: 100%; height: 100%; min-height: 420px; border: 0; background: #fff; transform-origin: top center; transition: transform .2s ease; }
.library-preview-toolbar { display: flex; flex: 0 0 auto; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-top: 1px solid #edf0f5; background: #fff; }
.library-preview-toolbar__group { display: inline-flex; align-items: center; gap: 7px; }
.library-preview-toolbar button { display: inline-flex; min-width: 34px; height: 34px; align-items: center; justify-content: center; gap: 7px; padding: 0 10px; border: 1px solid #e1e7ef; border-radius: 9px; background: #f8fafc; color: #4f627b; font-size: .78rem; font-weight: 700; }
.library-preview-toolbar button:hover { border-color: rgba(52, 127, 240, .4); background: #eef5ff; color: var(--library-accent); }
.library-preview-toolbar__group:first-child button { padding: 0; }
.library-preview-toolbar__group:first-child button[hidden] { display: none; }
.library-preview-toolbar__group > span { min-width: 48px; color: #65758a; font-size: .76rem; font-weight: 700; text-align: center; }

.library-upload-modal__dialog { width: min(960px, 100%); }
.library-upload-modal__body { min-height: 0; overflow: auto; padding: 24px 16px 20px; }
.library-upload-dropzone { padding: 25px 20px 20px; border: 2px dashed #d6dfed; border-radius: 13px; background: #f9fbfe; text-align: center; transition: border-color .18s ease, background-color .18s ease; }
.library-upload-dropzone.is-dragover { border-color: var(--library-accent); background: #eef6ff; }
.library-upload-dropzone > i { display: grid; width: 58px; height: 58px; margin: 0 auto 8px; place-items: center; border-radius: 50%; background: #edf5ff; color: var(--library-accent); font-size: 1.35rem; }
.library-upload-dropzone p { margin: 0 0 14px; color: #63738a; font-size: .92rem; }
.library-upload-dropzone p button { padding: 0; border: 0; background: transparent; color: var(--library-accent); cursor: pointer; font: inherit; font-weight: 700; }
.library-upload-dropzone__actions { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; }
.library-upload-dropzone__actions button { display: inline-flex; align-items: center; gap: 7px; min-height: 36px; padding: 0 14px; border-radius: 9px; background: #edf5ff; color: var(--library-accent); font-size: .78rem; font-weight: 700; }
.library-upload-dropzone button[hidden] { display: none; }
.library-upload-dropzone__actions button:last-child { background: #fff4e9; color: #e77735; }
.library-upload-dropzone small { display: block; margin-top: 13px; overflow: hidden; color: #9aa8ba; font-size: .72rem; text-overflow: ellipsis; white-space: nowrap; }
.library-upload-summary { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 52px; color: #63738a; font-size: .78rem; }
.library-upload-summary button { padding: 5px 9px; border: 1px solid var(--library-border); border-radius: 7px; background: #fff; color: #718198; font-size: .72rem; }
.library-upload-tasks { display: flex; min-height: 94px; max-height: 260px; flex-direction: column; gap: 7px; overflow: auto; padding: 2px; }
.library-upload-task { display: grid; grid-template-columns: minmax(0, 1fr) 160px 42px; align-items: center; gap: 12px; padding: 8px 10px; border: 1px solid #edf1f6; border-radius: 8px; background: #fff; }
.library-upload-task__name { overflow: hidden; color: #40516a; font-size: .76rem; text-overflow: ellipsis; white-space: nowrap; }
.library-upload-task__progress { height: 6px; overflow: hidden; border-radius: 99px; background: #edf1f6; }
.library-upload-task__progress span { display: block; height: 100%; border-radius: inherit; background: var(--library-accent); transition: width .15s ease; }
.library-upload-task__status { color: #8492a5; font-size: .7rem; text-align: right; }
.library-upload-task.is-done .library-upload-task__progress span { background: #38ad82; }
.library-upload-task.is-done .library-upload-task__status { color: #29936b; }
.library-upload-task.is-error .library-upload-task__progress span { background: var(--library-danger); }
.library-upload-task.is-error .library-upload-task__status { color: var(--library-danger); }

.library-menu { position: fixed; z-index: 5000; min-width: 172px; max-height: calc(100vh - 16px); overflow: auto; padding: 6px; border: 1px solid var(--library-border); border-radius: 11px; background: #fff; box-shadow: 0 12px 28px rgba(20, 43, 75, .2); }
.library-menu button { display: flex; width: 100%; align-items: center; gap: 8px; padding: 9px 10px; border: 0; border-radius: 7px; background: transparent; color: #41536f; cursor: pointer; font: inherit; font-size: .8rem; text-align: left; }
.library-menu button:hover { background: #f0f6ff; color: var(--library-accent); }

body.dark.mzpi-library-page { background: #0b1120 !important; }
body.dark .library-empty strong,
body.dark .library-error strong,
body.dark .library-item__name,
body.dark .library-selection__file { color: #e7edf7; }
body.dark .library-notice { color: #9eacc1; }
body.dark .library-browser,
body.dark .library-item { border-color: rgba(148, 163, 184, .14); background: rgba(30, 41, 59, .78); }
body.dark .library-loading { background: rgba(15, 23, 42, .62); }
body.dark .library-item:hover,
body.dark .library-item.is-selected { background: #26354a; }
body.dark .library-refresh,
body.dark .library-back,
body.dark .library-view-switch,
body.dark .library-search { border-color: rgba(148, 163, 184, .2); background: rgba(30, 41, 59, .78); color: #cfdaea; }
body.dark .library-search input { color: #e2e8f0; }
body.dark .library-selection { border-color: rgba(148, 163, 184, .2); background: rgba(30, 41, 59, .96); }
body.dark .library-modal__dialog,
body.dark .library-menu { background: #1e293b; }
body.dark .library-modal__dialog header { border-color: rgba(148, 163, 184, .16); }
body.dark .library-modal__dialog h2,
body.dark .library-menu button { color: #d8e2ee; }
body.dark .library-preview-stage { background: #111b29; }
body.dark .library-preview-toolbar { border-color: rgba(148, 163, 184, .16); background: #1e293b; }
body.dark .library-preview-toolbar button { border-color: rgba(148, 163, 184, .18); background: #26354a; color: #cbd7e5; }
body.dark .library-preview-toolbar__group > span { color: #b2c0d2; }
body.dark .library-upload-dropzone { border-color: rgba(148, 163, 184, .24); background: rgba(15, 23, 42, .48); }
body.dark .library-upload-dropzone p,
body.dark .library-upload-task__name { color: #d8e2ee; }
body.dark .library-upload-task,
body.dark .library-upload-summary button { border-color: rgba(148, 163, 184, .16); background: #26354a; }

@media (max-width: 1100px) {
  .mzpi-library { width: 100%; margin: 0; padding: 42px clamp(18px, 5vw, 40px) 76px; }
  .library-selection { right: 24px; left: 24px; }
}

/* At 150% browser zoom the shared .8 page scale leaves 20% of the physical
   viewport outside the scaled coordinate space. Recenter only the library
   panel in that remaining desktop space; the navigation stays anchored. */
@media (min-width: 1101px) and (max-width: 1280px) {
  body.mzpi-library-page.desktop-page-scale .mzpi-library {
    transform: translateX(12.5vw);
  }
}

@media (max-width: 700px) {
  .mzpi-library { min-height: 100vh; padding: 28px 14px 88px; }
  .library-refresh { width: 38px; min-width: 38px; padding: 0; }
  .library-refresh span { display: none; }
  .library-browser { height: calc(100vh - 126px); min-height: 420px; padding: 13px; border-radius: 14px; }
  .library-loading { inset: 94px 13px 13px; }
  .library-browser__topline { flex-wrap: wrap; gap: 8px; }
  .library-back { min-height: 34px; padding: 0 10px; font-size: .75rem; }
  .library-breadcrumb { order: 3; width: 100%; }
  .library-breadcrumb button { max-width: 132px; font-size: .8rem; }
  .library-view-switch { margin-left: auto; }
  .library-search { width: 100%; margin: 14px 0 10px; }
  .library-items.is-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; }
  .library-items.is-grid .library-item { min-height: 112px; padding: 14px; border-radius: 12px; }
  .library-item__icon { width: 36px; height: 36px; font-size: 1.1rem; }
  .library-item__name { font-size: .82rem; }
  .library-selection { right: 12px; bottom: calc(env(safe-area-inset-bottom) + 12px); left: 12px; gap: 8px; min-height: 54px; padding: 8px 10px; border-radius: 13px; }
  .library-selection__actions { gap: 5px; }
  .library-selection__actions button { min-height: 32px; padding: 0 8px; font-size: .7rem; }
  .library-selection__close { width: 30px; height: 30px; }
  .library-modal { padding: 14px; }
  .library-modal__dialog { max-height: 82vh; border-radius: 14px; }
  .library-preview-modal { padding: 0; }
  .library-preview-modal__dialog { width: 100%; height: 100%; max-height: none; border-radius: 0; }
  .library-preview-stage { padding: 10px; }
  .library-preview-stage iframe { min-height: 360px; }
  .library-preview-toolbar { padding: 10px; }
  .library-preview-toolbar button span { display: none; }
  .library-upload-modal__body { padding: 16px 12px; }
  .library-upload-task { grid-template-columns: minmax(0, 1fr) 58px; gap: 8px; }
  .library-upload-task__progress { grid-column: 1 / 2; }
  .library-upload-task__status { grid-column: 2 / 3; grid-row: 1 / span 2; }
}

@keyframes library-spin { to { transform: rotate(360deg); } }
