:root {
  --bg: #0f1524;
  --bg-soft: #141c2d;
  --panel: #1b2438;
  --panel-2: #202b42;
  --text: #edf2ff;
  --muted: #9bb0da;
  --border: #33445f;
  --accent: #4ea4ff;
  --accent-2: #67b7ff;
  --gold: #f2c14e;
  --danger: #ff6b6b;
  --radius: 12px;
  --shadow: 0 14px 30px rgba(0, 0, 0, 0.28);

  --reader-font-size: 1.28rem;
  --reader-line-height: 2.05;
}

html[data-theme="light"] {
  --bg: #f5efe3;
  --bg-soft: #efe6d6;
  --panel: #fffaf2;
  --panel-2: #f8f0e4;
  --text: #2b241a;
  --muted: #6f6355;
  --border: #dacbb6;
  --accent: #0f6ccf;
  --accent-2: #1d7ce2;
  --gold: #c98f09;
  --danger: #ce3f3f;
  --shadow: 0 10px 22px rgba(58, 48, 32, 0.15);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: radial-gradient(1200px 700px at 90% -10%, #213055 0%, transparent 55%),
    radial-gradient(900px 500px at -20% 110%, #21365e 0%, transparent 55%), var(--bg);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}

html[data-theme="light"] body {
  background: radial-gradient(1200px 700px at 90% -10%, #e4d7bf 0%, transparent 55%),
    radial-gradient(900px 500px at -20% 110%, #e7dcc8 0%, transparent 55%), var(--bg);
}

.hidden {
  display: none !important;
}

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.88rem;
  line-height: 1;
  cursor: pointer;
  transition: 0.18s ease;
  touch-action: manipulation;
}

.btn:hover,
.btn:focus-visible {
  outline: none;
  border-color: var(--accent);
  background: var(--panel-2);
}

.btn.accent {
  background: linear-gradient(180deg, var(--accent-2), var(--accent));
  border-color: transparent;
  color: #fff;
  font-weight: 600;
}

.btn.secondary {
  background: transparent;
}

.loading-screen {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  gap: 12px;
  z-index: 1000;
}

.spinner {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

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

.loading-text {
  color: var(--muted);
  font-size: 0.9rem;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  backdrop-filter: blur(8px);
  background: color-mix(in srgb, var(--bg-soft) 82%, transparent);
  border-bottom: 1px solid var(--border);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 10px;
}

.topbar-title {
  text-align: center;
  font-weight: 700;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.library-view,
.reader-view {
  max-width: 1120px;
  margin: 0 auto;
  padding: 14px 14px 100px;
}

.library-header h1 {
  margin: 0 0 8px;
  font-size: 1.6rem;
}

.library-header p {
  margin: 0;
  color: var(--muted);
}

.library-actions {
  margin-top: 12px;
  display: flex;
  gap: 8px;
}

.library-header code {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px 5px;
}

.library-grid {
  margin-top: 18px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}

.book-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: linear-gradient(180deg, var(--panel), color-mix(in srgb, var(--panel) 80%, #000 20%));
  box-shadow: var(--shadow);
  padding: 14px;
  cursor: pointer;
  transition: transform 0.18s ease, border-color 0.18s ease;
}

.book-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
}

.book-title {
  margin: 0 0 8px;
  font-size: 1.03rem;
  font-weight: 700;
}

.book-meta {
  color: var(--muted);
  font-size: 0.8rem;
  display: grid;
  gap: 4px;
}

.book-lang {
  display: inline-block;
  font-size: 0.72rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 8px;
  margin-bottom: 10px;
}

.book-progress {
  margin-top: 12px;
  height: 6px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--panel-2) 70%, #000 30%);
  overflow: hidden;
}

.book-progress > div {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
}

.book-resume {
  margin-top: 9px;
  color: var(--gold);
  font-size: 0.76rem;
  font-weight: 600;
}

.toolbar {
  margin: 8px 0 10px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: color-mix(in srgb, var(--bg-soft) 90%, #000 10%);
  padding: 10px;
  display: grid;
  gap: 10px;
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.toolbar-row.wrap {
  flex-wrap: wrap;
}

.page-info,
.zoom-value,
.search-count {
  color: var(--muted);
  font-size: 0.82rem;
}

.search-input {
  min-width: 180px;
  flex: 1;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.9rem;
}

.search-input:focus-visible {
  outline: none;
  border-color: var(--accent);
}

.reader-container {
  display: grid;
  gap: 14px;
}

.paragraph,
.bi-source,
.bi-target {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  padding: 16px;
}

.paragraph {
  font-size: var(--reader-font-size);
  line-height: var(--reader-line-height);
  word-spacing: 1.8px;
  text-wrap: pretty;
  position: relative;
}

.paragraph[dir="rtl"],
.bi-source[dir="rtl"],
.bi-target[dir="rtl"] {
  font-family: "Amiri", "Noto Naskh Arabic", serif;
}

.paragraph-index {
  position: absolute;
  inset-inline-start: 8px;
  top: 6px;
  color: var(--muted);
  font-size: 0.66rem;
}

.paragraph.highlight {
  border-color: var(--accent);
}

.word {
  border-radius: 6px;
  padding: 0 2px;
  cursor: pointer;
}

.word:hover {
  background: color-mix(in srgb, var(--accent) 35%, transparent);
}

.word.selected {
  background: color-mix(in srgb, var(--gold) 42%, transparent);
}

.bi-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.bi-source,
.bi-target {
  position: relative;
  min-height: 64px;
}

.bi-target {
  color: color-mix(in srgb, var(--text) 76%, var(--muted) 24%);
  font-size: calc(var(--reader-font-size) * 0.73);
  line-height: 1.7;
}

.bi-row:hover .bi-source,
.bi-row:hover .bi-target {
  border-color: var(--accent);
}

.bi-loading {
  color: var(--muted);
  font-style: italic;
}

.popup {
  position: fixed;
  z-index: 80;
  max-width: min(92vw, 460px);
  min-width: min(82vw, 340px);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  background: var(--panel-2);
  padding: 14px;
}

.popup-close {
  position: absolute;
  top: 6px;
  inset-inline-end: 8px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.2rem;
  cursor: pointer;
}

.popup-source {
  margin-bottom: 10px;
  padding-inline-end: 18px;
  font-size: 1.12rem;
}

.popup-source[dir="rtl"] {
  font-family: "Amiri", "Noto Naskh Arabic", serif;
}

.popup-translation {
  border-top: 1px solid var(--border);
  padding-top: 10px;
  min-height: 38px;
  line-height: 1.5;
}

.popup-translation[dir="rtl"] {
  font-family: "Amiri", "Noto Naskh Arabic", serif;
  font-size: 1.04rem;
}

.popup-actions {
  margin-top: 12px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.toast {
  position: fixed;
  z-index: 90;
  top: calc(env(safe-area-inset-top, 0px) + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--gold);
  color: #1f1a12;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 0.82rem;
  font-weight: 600;
  box-shadow: var(--shadow);
}

.empty {
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 18px;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 760px) {
  .library-view,
  .reader-view {
    padding-inline: 10px;
  }

  .toolbar {
    position: sticky;
    top: calc(env(safe-area-inset-top, 0px) + 58px);
    z-index: 30;
  }

  .btn {
    min-height: 40px;
  }

  .bi-row {
    grid-template-columns: 1fr;
  }

  .paragraph,
  .bi-source,
  .bi-target {
    padding: 14px;
  }
}
