:root {
  color-scheme: light;
  --bg: #f7f3ea;
  --panel: #fffaf1;
  --text: #191713;
  --muted: #756f64;
  --line: #ded6c8;
  --accent: #26706a;
  --accent-text: #ffffff;
  --shadow: 0 18px 50px rgb(38 31 21 / 14%);
  --app-viewport-height: 100dvh;
  --app-viewport-top: 0px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #151515;
  --panel: #20201e;
  --text: #f4efe4;
  --muted: #b5ac9d;
  --line: #3a3833;
  --accent: #78b7a6;
  --accent-text: #10211d;
  --shadow: 0 18px 50px rgb(0 0 0 / 35%);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100svh;
  background: var(--bg);
  color: var(--text);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

button,
input {
  font: inherit;
}

input,
textarea {
  user-select: text;
  -webkit-user-select: text;
  -webkit-touch-callout: default;
}

button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  min-height: 44px;
  padding: 0 14px;
}

.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
}

.icon-button {
  width: 44px;
  padding: 0;
  font-size: 24px;
  display: grid;
  place-items: center;
  line-height: 1;
}

.chevron-left::before {
  content: "";
  width: 11px;
  height: 11px;
  border-left: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(45deg) translate(1px, -1px);
}

.search-icon::before {
  content: "";
  width: 17px;
  height: 17px;
  border: 2px solid currentColor;
  border-radius: 50%;
  transform: translate(-2px, -2px);
}

.search-icon::after {
  content: "";
  width: 10px;
  height: 2px;
  background: currentColor;
  border-radius: 999px;
  transform: translate(9px, 9px) rotate(45deg);
}

.search-icon::before,
.search-icon::after {
  grid-area: 1 / 1;
}

.hidden {
  display: none !important;
}

.login-view {
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: max(20px, env(safe-area-inset-top)) 20px max(20px, env(safe-area-inset-bottom));
}

.login-panel {
  width: min(100%, 360px);
  display: grid;
  gap: 12px;
}

.login-panel h1,
.topbar h1 {
  margin: 0;
  font-size: 32px;
  letter-spacing: 0;
}

input {
  width: 100%;
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  padding: 0 12px;
}

.error {
  color: #b42318;
}

.library-view {
  min-height: 100dvh;
  padding: max(16px, env(safe-area-inset-top)) 16px max(22px, env(safe-area-inset-bottom));
}

.topbar,
.readerbar,
.sheet-head,
.search-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.topbar {
  justify-content: space-between;
  margin-bottom: 18px;
}

.actions {
  display: flex;
  gap: 8px;
}

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 18px 14px;
}

.book-card {
  position: relative;
  display: grid;
  gap: 7px;
  border: 0;
  background: transparent;
  padding: 0;
  text-align: left;
  min-width: 0;
  touch-action: manipulation;
  -webkit-touch-callout: none;
}

.book-card.favorite .cover::after {
  content: "★";
  position: absolute;
  top: 7px;
  right: 7px;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgb(25 23 19 / 78%);
  color: #ffd166;
  font-size: 16px;
}

.cover {
  position: relative;
  aspect-ratio: 2 / 3;
  display: grid;
  place-items: center;
  overflow: hidden;
  border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 18%, var(--panel));
  box-shadow: var(--shadow);
}

.context-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: transparent;
}

.context-menu {
  position: fixed;
  min-width: 190px;
  display: grid;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.context-menu button {
  border: 0;
  border-radius: 0;
  justify-content: start;
  text-align: left;
  background: transparent;
}

.context-menu button + button {
  border-top: 1px solid var(--line);
}

.danger-action {
  color: #b42318;
}

.cover img,
.result-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.book-title,
.book-author {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-title {
  font-weight: 700;
}

.book-author,
.empty,
.muted,
#readerAuthor,
#readerProgress {
  color: var(--muted);
}

.reader-view {
  position: relative;
  height: 100dvh;
  overflow: hidden;
}

.readerbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 4;
  min-width: 0;
  padding: max(8px, env(safe-area-inset-top)) 10px 8px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  backdrop-filter: blur(14px);
  overflow: hidden;
  transition: opacity 180ms ease, transform 180ms ease;
}

.reader-view.chrome-hidden .readerbar {
  opacity: 0;
  pointer-events: none;
  transform: translateY(-100%);
}

.readerbar div {
  min-width: 0;
  display: grid;
  flex: 1;
}

.readerbar strong,
.readerbar span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.text-icon {
  font-size: 20px;
  font-family: Georgia, serif;
  font-weight: 800;
}

.reader-stage {
  position: relative;
  min-height: 0;
  height: 100dvh;
  overflow: hidden;
  background: var(--panel);
}

#readerHost {
  width: 100%;
  height: 100%;
  min-height: 320px;
}

#readerHost iframe {
  background: var(--panel);
}

.reader-nav {
  position: absolute;
  top: 0;
  bottom: 0;
  z-index: 2;
  width: 28%;
  min-height: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: transparent;
  padding: 0;
  touch-action: manipulation;
  user-select: none;
}

.reader-nav:active {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

.reader-nav-prev {
  left: 0;
}

.reader-nav-center {
  left: 28%;
  width: 44%;
  z-index: 3;
}

.reader-nav-next {
  right: 0;
}

.reader-error {
  max-width: 680px;
  margin: 24px auto;
  padding: 0 16px;
  color: #b42318;
}

.pdf-frame {
  width: 100%;
  height: 100%;
  border: 0;
}

.epub-frame {
  width: 100%;
  height: 100%;
  border: 0;
  background: var(--panel);
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 45%);
  display: grid;
  align-items: end;
  z-index: 10;
}

.search-overlay {
  display: block;
  background: rgb(0 0 0 / 32%);
}

.sheet {
  max-height: min(88dvh, 820px);
  overflow: auto;
  background: var(--panel);
  border-radius: 8px 8px 0 0;
  padding: 14px 14px max(18px, env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
}

.search-sheet {
  position: absolute;
  inset: var(--app-viewport-top) 0 auto;
  width: 100%;
  height: var(--app-viewport-height);
  max-height: none;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  overflow: hidden;
  border-radius: 0;
  padding: 0;
  box-shadow: none;
}

.sheet-head {
  justify-content: space-between;
  margin-bottom: 12px;
}

.sheet h2 {
  margin: 0;
}

.settings-sheet {
  display: grid;
  gap: 16px;
}

.setting-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

.setting-row input[type="range"] {
  grid-column: 1 / -1;
  min-height: 32px;
  padding: 0;
}

.setting-row output {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.segmented {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.segmented button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.search-row {
  margin: 0;
}

.search-dock {
  z-index: 1;
  display: grid;
  gap: 8px;
  padding: 10px 12px max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 96%, transparent);
  backdrop-filter: blur(16px);
}

.search-dock .search-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 44px;
}

.search-results {
  min-height: 0;
  overflow-y: auto;
  align-content: start;
  padding: max(14px, env(safe-area-inset-top)) 12px 14px;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.check {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 14px;
}

.check input {
  width: 18px;
  min-height: 18px;
}

.results {
  display: grid;
  gap: 10px;
}

.search-empty {
  min-height: 42svh;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 10px;
  color: var(--muted);
  text-align: center;
}

.search-empty strong {
  color: var(--text);
  font-size: 18px;
}

.search-empty p {
  max-width: 280px;
  margin: 0;
}

.search-empty button {
  margin-top: 4px;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
}

.search-more-row {
  display: grid;
  padding: 8px 0 2px;
}

.search-more-row button {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 800;
}

.search-more-row button:disabled {
  opacity: 0.65;
}

.result {
  display: grid;
  grid-template-columns: 52px 1fr auto;
  gap: 10px;
  align-items: center;
  min-width: 0;
  width: 100%;
  min-height: auto;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
}

.result:active {
  opacity: 0.72;
}

.result-cover {
  width: 52px;
  aspect-ratio: 2 / 3;
  display: block;
  overflow: hidden;
  border-radius: 6px;
  background: var(--bg);
}

.result-main {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.result-main strong,
.result-main small {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.result-badges {
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.result-badge {
  max-width: 100%;
  min-height: 22px;
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0 8px;
  font-size: 11px;
  font-weight: 800;
  line-height: 1;
  white-space: nowrap;
}

.result-badge-epub {
  background: #c9fbff;
  color: #064b52;
}

.result-badge-pdf {
  background: #ffe0df;
  color: #8f1d18;
}

.result-badge-language {
  background: color-mix(in srgb, var(--accent) 20%, var(--panel));
  color: var(--text);
}

.result-badge-size {
  background: #fff3bf;
  color: #5c4300;
}

.result-badge-downloads {
  background: color-mix(in srgb, #9b8cff 24%, var(--panel));
  color: #2d235d;
}

.result-chevron {
  color: var(--muted);
  font-size: 26px;
}

.fullscreen-overlay {
  align-items: stretch;
  background: var(--panel);
}

.detail-sheet {
  width: 100%;
  max-height: 100dvh;
  border-radius: 0;
  padding: max(12px, env(safe-area-inset-top)) 14px max(20px, env(safe-area-inset-bottom));
  box-shadow: none;
}

.detail-head {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--panel);
  padding-bottom: 8px;
}

.detail-cover-wrap {
  display: grid;
  place-items: center;
  margin: 12px 0 18px;
}

.detail-cover {
  width: min(58vw, 260px);
  aspect-ratio: 2 / 3;
  object-fit: cover;
  border-radius: 8px;
  background: var(--bg);
  box-shadow: var(--shadow);
}

.detail-cover-empty {
  display: block;
}

.detail-title {
  margin: 0;
  font-size: 26px;
  line-height: 1.15;
  letter-spacing: 0;
}

.detail-author {
  margin: 8px 0 16px;
  color: var(--muted);
}

.detail-import-form {
  display: grid;
  gap: 10px;
  margin-bottom: 18px;
}

.detail-link {
  margin-bottom: 18px;
  min-height: 44px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
}

.detail-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  margin: 0;
}

.detail-list dt {
  color: var(--muted);
  font-size: 13px;
  margin-top: 10px;
}

.detail-list dd {
  margin: 0;
  overflow-wrap: anywhere;
  line-height: 1.35;
}

.detail-list a {
  color: var(--accent);
}

.detail-json {
  max-height: 360px;
  overflow: auto;
  margin: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
  white-space: pre-wrap;
}

@media (min-width: 760px) {
  .library-view {
    padding-inline: max(28px, calc((100vw - 1040px) / 2));
  }

  .sheet {
    width: min(720px, calc(100vw - 32px));
    margin: 0 auto;
  }

  .search-sheet {
    left: 50%;
    right: auto;
    width: min(720px, calc(100vw - 32px));
    transform: translateX(-50%);
    border-radius: 8px 8px 0 0;
  }
}
