/* Translation Reader — hand-written mobile-first stylesheet.
   Replaces the vendored utility CSS and the never-linked legacy stylesheet. */

:root {
  --bg: #f6f5f1;
  --fg: #21201c;
  --muted: #706c63;
  --card: #ffffff;
  --line: #e2dfd6;
  --accent: #33684b;
  --header-bg: rgba(246, 245, 241, 0.92);
  --sent-t: #33506e;
  --error-bg: #f9e7e7;
  --error-fg: #8c3030;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131210;
    --fg: #e8e5dd;
    --muted: #9a958a;
    --card: #1c1b17;
    --line: #2d2b25;
    --accent: #86b89a;
    --header-bg: rgba(19, 18, 16, 0.92);
    --sent-t: #9cb9d9;
    --error-bg: #3a2121;
    --error-fg: #eeb4b4;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

/* The `hidden` attribute must win over any component display rule */
[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 1rem;
  line-height: 1.5;
}

/* ---------- Sticky app header (safe-area aware) ---------- */

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  padding:
    calc(0.5rem + env(safe-area-inset-top))
    calc(0.75rem + env(safe-area-inset-right))
    0.5rem
    calc(0.75rem + env(safe-area-inset-left));
  background: var(--header-bg);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}

.app-header .back {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: var(--card);
  color: var(--fg);
  text-decoration: none;
  font-size: 1.05rem;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
}

.app-header h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
}

.app-header .book-title {
  flex: 1 1 auto;
  min-width: 8rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-header .controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-left: auto;
}

.app-header select,
.app-header button {
  font: inherit;
  font-size: 0.8rem;
  line-height: 1;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: var(--card);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
}

.app-header button {
  cursor: pointer;
}

.app-header #reveal-all {
  border-color: var(--accent);
  color: var(--accent);
}

/* ---------- Layout ---------- */

main {
  max-width: 42rem;
  margin: 0 auto;
  padding: 1rem 1rem calc(3rem + env(safe-area-inset-bottom));
}

.status {
  padding: 0.75rem 1rem;
  border: 1px solid var(--line);
  border-radius: 0.6rem;
  background: var(--card);
  color: var(--muted);
  font-size: 0.95rem;
}

.status.error {
  background: var(--error-bg);
  border-color: transparent;
  color: var(--error-fg);
}

/* ---------- Library ---------- */

#book-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.book-group {
  margin: 0.25rem 0 0 0.15rem;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.book-row {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0.85rem 1rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 0.6rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.book-row:active {
  border-color: var(--accent);
}

.book-row:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.book-row .book-title {
  font-size: 1.02rem;
  font-weight: 600;
}

.book-row .book-meta {
  color: var(--muted);
  font-size: 0.83rem;
}

.book-row .badge {
  align-self: flex-start;
  margin-top: 0.2rem;
  padding: 0.05rem 0.5rem;
  border: 1px solid var(--accent);
  border-radius: 999px;
  color: var(--accent);
  font-size: 0.7rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.book-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.45rem;
}

.book-actions button {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 999px;
  padding: 0.2rem 0.7rem;
  font-size: 0.78rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.book-actions button:active {
  border-color: var(--accent);
  color: var(--accent);
}

.job-progress {
  color: var(--muted);
  font-size: 0.8rem;
}

.job-progress.error {
  color: var(--error-fg);
}

.inline-edit {
  font: inherit;
  font-weight: 600;
  width: 100%;
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--accent);
  border-radius: 0.4rem;
  padding: 0.25rem 0.5rem;
}

.translate-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.5rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--line);
}

.translate-form input {
  font: inherit;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 0.4rem;
  background: var(--bg);
  color: var(--fg);
}

/* ---------- Reader ---------- */

#reader {
  font-family: Georgia, "Iowan Old Style", "Times New Roman", serif;
  font-size: 1.05rem;
  line-height: 1.75;
  /* Backward pages are prepended with manual scroll compensation;
     disable native scroll anchoring so it doesn't double-correct. */
  overflow-anchor: none;
}

#reader .block {
  margin: 0 0 1rem;
}

#reader .block.heading {
  font-weight: 700;
  line-height: 1.3;
  margin: 1.4em 0 0.8em;
}

h1.block.heading { font-size: 1.6rem; }
h2.block.heading { font-size: 1.4rem; }
h3.block.heading { font-size: 1.25rem; }
h4.block.heading { font-size: 1.15rem; }
h5.block.heading { font-size: 1.1rem; }
h6.block.heading { font-size: 1.05rem; }

.sent {
  display: block;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.sent-o {
  display: block;
}

.sent-t {
  color: var(--sent-t);
}

/* Spoiler: translation is blurred in place; revealing never shifts the page.
   content-visibility keeps off-screen blurred nodes off the GPU. */
.sent-t { display:block; filter:blur(5px); opacity:.6; user-select:none;
          transition:filter .18s ease, opacity .18s ease; }
.sent.revealed .sent-t,
.reveal-all .sent-t { filter:none; opacity:1; user-select:text; }
.block { content-visibility:auto; contain-intrinsic-size:auto 120px; }

#reader img.block {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1.5rem auto;
}

#sentinel,
#top-sentinel {
  height: 1px;
  overflow-anchor: none;
}

/* ---------- Bookmarks & toast ---------- */

.bookmark-scrim {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}
.bookmark-scrim.open {
  opacity: 1;
  pointer-events: auto;
}

.bookmark-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  width: min(22rem, 90vw);
  display: flex;
  flex-direction: column;
  background: var(--card);
  border-left: 1px solid var(--line);
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.15);
  transform: translateX(105%);
  visibility: hidden;
  transition: transform 0.25s ease, visibility 0.25s;
}
.bookmark-panel.open {
  transform: none;
  visibility: visible;
}

.bookmark-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--line);
}
.bookmark-panel-head h2 {
  margin: 0;
  font-size: 1rem;
}
#bookmark-close {
  font: inherit;
  font-size: 1rem;
  line-height: 1;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: none;
  color: var(--fg);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.bookmark-list {
  flex: 1;
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
}

.bookmark-item {
  display: flex;
  align-items: stretch;
  border-bottom: 1px solid var(--line);
}

.bookmark-jump {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.6rem 0.75rem;
  background: none;
  border: none;
  color: var(--fg);
  font: inherit;
  text-align: left;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.bookmark-jump:active {
  background: var(--bg);
}

.bookmark-label {
  font-weight: 600;
}

.bookmark-meta {
  font-size: 0.75rem;
  color: var(--muted);
}

.bookmark-delete {
  align-self: center;
  margin-right: 0.5rem;
  width: 2rem;
  height: 2rem;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.bookmark-delete:active {
  color: var(--fg);
}

.bookmark-empty {
  margin: 0;
  padding: 1rem;
  color: var(--muted);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(1rem + env(safe-area-inset-bottom));
  z-index: 60;
  transform: translate(-50%, 0.5rem);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  background: var(--fg);
  color: var(--bg);
  padding: 0.5rem 0.9rem;
  border-radius: 999px;
  font-size: 0.85rem;
}
.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- Ask AI chat ---------- */

.chat-scrim {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}
.chat-scrim.open {
  opacity: 1;
  pointer-events: auto;
}

.chat-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  width: min(22rem, 90vw);
  display: flex;
  flex-direction: column;
  background: var(--card);
  border-left: 1px solid var(--line);
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.15);
  transform: translateX(105%);
  visibility: hidden;
  transition: transform 0.25s ease, visibility 0.25s;
}
.chat-panel.open {
  transform: none;
  visibility: visible;
}

.chat-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--line);
}
.chat-panel-head h2 {
  margin: 0;
  font-size: 1rem;
}
.chat-panel-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
#chat-close {
  font: inherit;
  font-size: 1rem;
  line-height: 1;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: none;
  color: var(--fg);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
#chat-reset {
  font: inherit;
  font-size: 0.8rem;
  line-height: 1;
  padding: 0.3rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: none;
  color: var(--fg);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.chat-context {
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  font-size: 0.8rem;
  color: var(--muted);
}
.chat-context-text {
  flex: 1;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
#chat-context-clear {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0 0.25rem;
}

.chat-messages {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  overflow-y: auto;
  padding: 0.75rem;
}

.chat-msg {
  max-width: 85%;
  padding: 0.5rem 0.75rem;
  border-radius: 0.75rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 0.925rem;
  line-height: 1.45;
}
.chat-msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: var(--bg);
  border-bottom-right-radius: 0.25rem;
}
.chat-msg.assistant {
  align-self: flex-start;
  background: var(--bg);
  border: 1px solid var(--line);
  border-bottom-left-radius: 0.25rem;
}
.chat-msg.error {
  background: var(--error-bg);
  color: var(--error-fg);
  border-color: transparent;
}

.chat-empty {
  margin: 0;
  color: var(--muted);
  font-size: 0.875rem;
  text-align: center;
}

.chat-input-row {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem;
  border-top: 1px solid var(--line);
}
#chat-input {
  flex: 1;
  min-width: 0;
  font: inherit;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: var(--bg);
  color: var(--fg);
}
#chat-input:disabled {
  opacity: 0.6;
}
#chat-send {
  font: inherit;
  padding: 0.5rem 0.9rem;
  border: none;
  border-radius: 0.5rem;
  background: var(--accent);
  color: var(--bg);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
#chat-send:disabled {
  opacity: 0.6;
  cursor: default;
}

.chat-context-view {
  margin: 0;
  padding: 0.4rem 1rem;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  font-size: 0.75rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.selection-chip {
  position: fixed;
  z-index: 60;
  transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  font-size: 0.8rem;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  cursor: pointer;
  white-space: nowrap;
}

/* ---------- Auth bar ---------- */

.auth-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0.4rem 0.75rem;
  background: var(--header-bg);
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.78rem;
}

.auth-bar button {
  font: inherit;
  line-height: 1;
  padding: 0.25rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 0.4rem;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.auth-bar button:active {
  border-color: var(--accent);
  color: var(--accent);
}

/* ---------- Auth pages ---------- */

.auth-form {
  max-width: 340px;
  margin: 12vh auto 0;
  padding: 0 1rem;
  display: grid;
  gap: 0.75rem;
}

.auth-form h1 {
  margin: 0 0 0.25rem;
  font-size: 1.3rem;
}

.auth-field {
  display: grid;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.auth-form input {
  width: 100%;
  font: inherit;
  padding: 0.55rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: var(--card);
  color: var(--fg);
}

.auth-form input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.auth-form button[type="submit"] {
  font: inherit;
  line-height: 1;
  padding: 0.6rem;
  border: none;
  border-radius: 0.5rem;
  background: var(--accent);
  color: var(--bg);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.auth-error {
  margin: 0;
  font-size: 0.83rem;
  color: var(--error-fg);
}

.auth-alt,
.auth-hint {
  margin: 0;
  font-size: 0.83rem;
  color: var(--muted);
}

.auth-alt a {
  color: var(--accent);
}
