:root {
  --reader-green: #102b28;
  --reader-green-soft: #173a36;
  --reader-gold: #b28a4a;
  --reader-paper: #f7f4ed;
  --reader-line: #ded8cc;
  --reader-ink: #202522;
  --reader-muted: #66706b;
}
* {
  box-sizing: border-box;
}
html,
body {
  height: 100%;
  margin: 0;
}
body.library-reader {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: grid;
  grid-template-rows: 68px minmax(0, 1fr) 70px;
  background: #1c2523;
  color: var(--reader-ink);
  font-family: Tahoma, Arial, sans-serif;
}
.library-reader button,
.library-reader input,
.library-reader textarea {
  font: inherit;
}
.library-reader button,
.library-reader a {
  -webkit-tap-highlight-color: transparent;
}
.library-reader-header {
  position: relative;
  z-index: 30;
  min-width: 0;
  padding: 8px 16px;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  background: var(--reader-green);
  color: #fff;
  box-shadow: 0 5px 18px rgba(0, 0, 0, 0.18);
}
.reader-back {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 10px;
  color: #fff;
}
.reader-back svg,
.library-reader-navigation svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.reader-identity {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}
.reader-monogram {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border: 1px solid rgba(178, 138, 74, 0.7);
  border-radius: 50%;
  color: #e1c58d;
  font-family: "Traditional Arabic", "Times New Roman", serif;
  font-size: 24px;
}
.reader-identity div {
  min-width: 0;
  display: grid;
  gap: 2px;
}
.reader-identity strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: "Traditional Arabic", "Times New Roman", serif;
  font-size: 18px;
  line-height: 1.2;
}
.reader-identity small {
  color: #aab9b5;
  font-size: 10px;
}
.reader-header-actions {
  display: flex;
  gap: 7px;
}
.reader-header-actions button {
  min-height: 44px;
  padding: 7px 13px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.05);
  color: #fff;
  cursor: pointer;
}
.reader-header-actions button:hover,
.reader-header-actions button[aria-expanded="true"] {
  border-color: rgba(178, 138, 74, 0.8);
  background: rgba(178, 138, 74, 0.13);
}
.library-reader-main {
  position: relative;
  min-height: 0;
  overflow: hidden;
  background: #202927;
}
.reader-paper-stage {
  width: 100%;
  height: 100%;
  padding: 24px;
  overflow: auto;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overscroll-behavior: contain;
  touch-action: pan-x pan-y;
  scrollbar-color: #66706b #202927;
}
.reader-paper-stage canvas,
.reader-paper-stage img {
  max-width: none;
  display: block;
  flex: 0 0 auto;
  background: #fff;
  border-radius: 2px;
  box-shadow: 0 18px 54px rgba(0, 0, 0, 0.34);
  user-select: none;
  -webkit-user-drag: none;
}
.reader-loading,
.reader-failure {
  position: absolute;
  z-index: 8;
  inset: 50% auto auto 50%;
  width: min(calc(100% - 32px), 380px);
  padding: 22px;
  display: grid;
  justify-items: center;
  gap: 8px;
  border: 1px solid rgba(255, 255, 255, 0.11);
  border-radius: 14px;
  background: rgba(16, 43, 40, 0.96);
  color: #fff;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  transform: translate(-50%, -50%);
}
.reader-loading[hidden],
.reader-failure[hidden] {
  display: none;
}
.reader-loading small,
.reader-failure p {
  margin: 0;
  color: #b8c4c0;
  font-size: 11px;
  line-height: 1.7;
}
.reader-loading-mark {
  width: 27px;
  height: 27px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--reader-gold);
  border-radius: 50%;
  animation: reader-spin 0.8s linear infinite;
}
.reader-failure strong {
  font-family: "Traditional Arabic", "Times New Roman", serif;
  font-size: 22px;
}
.reader-failure div {
  display: flex;
  gap: 8px;
}
.reader-failure button,
.reader-failure a {
  min-height: 44px;
  padding: 8px 14px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--reader-gold);
  border-radius: 9px;
  background: var(--reader-gold);
  color: var(--reader-green);
  font-weight: 700;
  text-decoration: none;
}
.reader-failure a {
  border-color: rgba(255, 255, 255, 0.18);
  background: transparent;
  color: #fff;
}
.library-reader-navigation {
  position: relative;
  z-index: 25;
  padding: 9px max(12px, env(safe-area-inset-right))
    calc(9px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  display: grid;
  grid-template-columns: minmax(150px, 220px) minmax(150px, 200px) minmax(
      150px,
      220px
    );
  justify-content: center;
  align-items: center;
  gap: 10px;
  border-top: 1px solid var(--reader-line);
  background: #fbfaf6;
  box-shadow: 0 -5px 18px rgba(0, 0, 0, 0.14);
}
.library-reader-navigation > button {
  min-height: 48px;
  padding: 7px 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  border: 1px solid var(--reader-green);
  border-radius: 10px;
  background: var(--reader-green);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}
.library-reader-navigation > button:disabled {
  border-color: #d8d3c9;
  background: #e9e5dc;
  color: #8a918e;
  cursor: not-allowed;
}
.reader-page-jump {
  min-height: 48px;
  padding: 5px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid var(--reader-line);
  border-radius: 10px;
  background: #fff;
  color: var(--reader-muted);
  font-size: 12px;
}
.reader-page-jump input {
  width: 56px;
  min-height: 36px;
  padding: 4px;
  border: 1px solid var(--reader-line);
  border-radius: 8px;
  background: var(--reader-paper);
  color: var(--reader-green);
  font-weight: 800;
  text-align: center;
}
.reader-page-jump output {
  min-width: 30px;
  color: var(--reader-green);
  font-weight: 700;
}
.reader-progress {
  position: fixed;
  z-index: 31;
  inset: auto 0 calc(70px + env(safe-area-inset-bottom));
  width: 100%;
  height: 3px;
  border: 0;
  accent-color: var(--reader-gold);
}
.reader-tools {
  position: fixed;
  z-index: 45;
  top: 76px;
  inset-inline-end: 16px;
  width: min(calc(100vw - 24px), 330px);
  padding: 10px;
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr) 48px;
  gap: 7px;
  border: 1px solid var(--reader-line);
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 18px 55px rgba(0, 0, 0, 0.24);
}
.reader-tools[hidden] {
  display: none;
}
.reader-tools button,
.reader-tools a {
  min-height: 44px;
  padding: 7px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--reader-line);
  border-radius: 8px;
  background: var(--reader-paper);
  color: var(--reader-green);
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
}
.reader-tools [data-reader-focus],
.reader-tools [data-reader-share],
.reader-tools [data-reader-copy-link],
.reader-tools a {
  grid-column: span 3;
}
.reader-tools [data-reader-focus],
.reader-tools [data-reader-share],
.reader-tools a {
  background: var(--reader-green);
  color: #fff;
}
.reader-tools > small {
  min-height: 18px;
  grid-column: span 3;
  color: #2f6e56;
  font-size: 11px;
  text-align: center;
}
.reader-panel {
  position: fixed;
  z-index: 55;
  inset-block: 0;
  inset-inline-end: 0;
  width: min(94vw, 470px);
  padding: 18px;
  overflow-y: auto;
  background: #fbfaf6;
  color: var(--reader-ink);
  box-shadow: -20px 0 65px rgba(0, 0, 0, 0.34);
}
.reader-panel[hidden],
.reader-backdrop[hidden] {
  display: none;
}
.reader-backdrop {
  position: fixed;
  z-index: 50;
  inset: 0;
  border: 0;
  background: rgba(5, 15, 13, 0.6);
}
.reader-panel-head {
  padding-bottom: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  border-bottom: 1px solid var(--reader-line);
}
.reader-panel-head > div {
  min-width: 0;
  display: grid;
  gap: 2px;
}
.reader-panel-head strong {
  font-family: "Traditional Arabic", "Times New Roman", serif;
  font-size: 25px;
  color: var(--reader-green);
}
.reader-panel-head small {
  color: var(--reader-muted);
  font-size: 11px;
}
.reader-panel-head > button {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  border: 1px solid var(--reader-line);
  border-radius: 50%;
  background: #fff;
  color: var(--reader-green);
  font-size: 22px;
  cursor: pointer;
}
.reader-search-context {
  padding: 11px 13px;
  border-radius: 8px;
  background: #eee9de;
  color: var(--reader-muted);
  font-size: 11px;
}
.reader-panel-section {
  padding-block: 20px;
  border-bottom: 1px solid var(--reader-line);
}
.reader-panel-section:last-child {
  border: 0;
}
.reader-panel-section h2 {
  margin: 0 0 12px;
  color: var(--reader-green);
  font-family: "Traditional Arabic", "Times New Roman", serif;
  font-size: 21px;
}
.reader-section-heading {
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.reader-section-heading h2 {
  margin: 0;
}
.reader-section-heading > button,
.reader-note-form > button {
  min-height: 42px;
  padding: 7px 11px;
  border: 1px solid var(--reader-green);
  border-radius: 8px;
  background: var(--reader-green);
  color: #fff;
  cursor: pointer;
}
.reader-chapter-list {
  max-height: 42vh;
  margin: 0;
  padding: 0;
  overflow: auto;
  list-style: none;
  border: 1px solid var(--reader-line);
  border-radius: 9px;
  background: #fff;
}
.reader-chapter-list li + li {
  border-top: 1px solid #eeeae2;
}
.reader-chapter-list button {
  width: 100%;
  min-height: 46px;
  padding: 9px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border: 0;
  background: #fff;
  color: var(--reader-ink);
  text-align: start;
  cursor: pointer;
}
.reader-chapter-list button:hover {
  background: var(--reader-paper);
}
.reader-chapter-list b {
  min-width: 30px;
  color: var(--reader-gold);
  font-size: 11px;
  text-align: center;
}
.reader-empty {
  margin: 0;
  color: var(--reader-muted);
  font-size: 11px;
}
.reader-bookmarks {
  display: grid;
  gap: 7px;
}
.reader-bookmarks > div {
  padding: 8px 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid var(--reader-line);
  border-radius: 8px;
  background: #fff;
}
.reader-bookmarks button,
.reader-notes button {
  min-height: 38px;
  border: 0;
  background: transparent;
  color: var(--reader-green);
  font-weight: 700;
  cursor: pointer;
}
.reader-remove {
  color: #a63d40 !important;
  font-size: 11px !important;
}
.reader-note-form {
  display: grid;
  gap: 9px;
}
.reader-note-form label {
  display: grid;
  gap: 6px;
  color: var(--reader-muted);
  font-size: 11px;
}
.reader-note-form textarea {
  width: 100%;
  resize: vertical;
  padding: 11px;
  border: 1px solid var(--reader-line);
  border-radius: 8px;
  background: #fff;
  color: var(--reader-ink);
  line-height: 1.7;
}
.reader-note-form small {
  min-height: 20px;
  color: #2f6e56;
  font-size: 11px;
}
.reader-notes {
  display: grid;
  gap: 8px;
}
.reader-notes article {
  padding: 11px;
  border: 1px solid var(--reader-line);
  border-radius: 9px;
  background: #fff;
}
.reader-notes article p {
  margin: 6px 0;
  color: var(--reader-ink);
  font-size: 12px;
  line-height: 1.7;
  white-space: pre-wrap;
}
.reader-panel-open {
  touch-action: none;
}
.library-reader :focus-visible {
  outline: 3px solid #d5b46f;
  outline-offset: 2px;
}
@keyframes reader-spin {
  to {
    transform: rotate(360deg);
  }
}
@media (max-width: 700px) {
  body.library-reader {
    height: 100svh;
    min-height: 100svh;
    max-height: 100svh;
    grid-template-rows:
      62px
      minmax(0, 1fr)
      calc(64px + env(safe-area-inset-bottom));
  }
  .library-reader-header {
    padding: 7px 9px;
    grid-template-columns: 42px minmax(0, 1fr) auto;
    gap: 7px;
  }
  .reader-back {
    width: 42px;
    height: 42px;
  }
  .reader-monogram,
  .reader-identity small {
    display: none;
  }
  .reader-identity strong {
    font-size: 15px;
  }
  .reader-header-actions {
    gap: 5px;
  }
  .reader-header-actions button {
    min-height: 42px;
    padding: 5px 8px;
    font-size: 11px;
  }
  .reader-paper-stage {
    padding: 9px 7px;
  }
  .reader-tools {
    grid-template-columns: 56px minmax(0, 1fr) 56px;
  }
  .library-reader-navigation {
    padding: 7px 8px calc(7px + env(safe-area-inset-bottom));
    grid-template-columns: minmax(72px, 1fr) minmax(108px, 1.2fr) minmax(
        72px,
        1fr
      );
    gap: 6px;
  }
  .library-reader-navigation > button {
    min-height: 50px;
    padding: 5px 7px;
    gap: 4px;
  }
  .library-reader-navigation > button span {
    font-size: 0;
  }
  .library-reader-navigation > button:first-child span::after {
    content: "السابق";
    font-size: 12px;
  }
  .library-reader-navigation > button:last-child span::after {
    content: "التالي";
    font-size: 12px;
  }
  .reader-page-jump {
    min-height: 50px;
    padding: 4px 5px;
    gap: 4px;
    font-size: 10px;
  }
  .reader-page-jump input {
    width: 42px;
    min-height: 38px;
  }
  .reader-progress {
    bottom: calc(64px + env(safe-area-inset-bottom));
  }
  .reader-tools {
    top: 68px;
    inset-inline: 8px;
    width: auto;
  }
  .reader-panel {
    width: 100%;
    padding: 14px;
    padding-top: max(14px, env(safe-area-inset-top));
  }
}
@media (max-width: 370px) {
  .reader-header-actions button {
    max-width: 78px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .reader-identity strong {
    font-size: 13px;
  }
  .library-reader-navigation {
    grid-template-columns: 72px minmax(100px, 1fr) 72px;
  }
  .library-reader-navigation > button svg {
    display: none;
  }
}
@media (prefers-reduced-motion: reduce) {
  .reader-loading-mark {
    animation-duration: 1.8s;
  }
  .library-reader * {
    scroll-behavior: auto !important;
  }
}
