:root {
  --bg-primary: #fdf6ec;
  --bg-sidebar: #1c1410;
  --bg-sidebar-hover: #2e1f14;
  --accent: #c0392b;
  --accent-soft: #e8d5c4;
  --text-primary: #1a0e08;
  --text-secondary: #5c3d2e;
  --text-sidebar: #e8d5c4;
  --text-sidebar-muted: #a38b78;
  --border: #d4b896;
  --reader-bg: #fffdf7;
  --r: 10px;
  --header-h: 56px;
  --sidebar-w: 280px;
  --font-ui: "Crimson Pro", "Noto Sans Telugu", Georgia, serif;
  --font-label: "Noto Sans Telugu", "Crimson Pro", sans-serif;
  --font-story: "Noto Sans Telugu", "Nirmala UI", Gautami, sans-serif;
  --font-story-serif: "Noto Serif Telugu", "Noto Sans Telugu", serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-ui);
  background: var(--bg-primary);
  color: var(--text-primary);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

#app[hidden] {
  display: none !important;
}

/* —— Top bar —— */
.app-top {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  height: var(--header-h);
  padding: 0 1rem 0 0.65rem;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  position: relative;
  z-index: 100;
  transition: box-shadow 0.2s ease;
}
.app-top.is-scrolled {
  box-shadow: 0 4px 20px rgba(28, 20, 16, 0.12);
}
.app-top__title {
  font-family: "Crimson Pro", Georgia, serif;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
  margin-right: auto;
  letter-spacing: 0.02em;
}
.app-top__profile {
  display: flex;
  align-items: center;
  gap: 0.5rem 0.75rem;
  flex-wrap: wrap;
}
.app-top__avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--text-secondary);
  font-size: 0.75rem;
  font-weight: 700;
  font-family: var(--font-label);
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.app-top__name {
  font-size: 0.92rem;
  color: var(--text-secondary);
  font-family: var(--font-label);
  max-width: 12rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.app-top__link {
  font: inherit;
  background: none;
  border: none;
  padding: 0.25rem 0;
  color: var(--accent);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.2em;
  cursor: pointer;
  font-size: 0.88rem;
  font-family: var(--font-label);
}
.app-top__link:hover {
  color: #922b1f;
}

.modal-input {
  width: 100%;
}

select,
input[type="text"],
input[type="password"] {
  font: inherit;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  min-height: 36px;
}

button.btn {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: 6px;
  padding: 0.45rem 0.8rem;
  min-height: 36px;
  font-weight: 500;
  background: var(--accent);
  color: #fff;
  font-family: var(--font-label);
}
button.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
button.btn--ghost {
  background: #fff;
  color: var(--text-primary);
  border: 1px solid var(--border);
}
button.btn--sm {
  min-height: 32px;
  padding: 0.25rem 0.5rem;
  font-size: 0.8rem;
}
button.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-primary);
  cursor: pointer;
  color: var(--text-secondary);
}
button.icon-btn:hover {
  background: #fff;
  border-color: var(--accent);
  color: var(--accent);
}

/* —— Shell —— */
.shell {
  display: flex;
  height: calc(100vh - var(--header-h));
  min-height: 0;
}

/* —— Dark sidebar —— */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  max-width: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg-sidebar);
  border-right: 1px solid rgba(255, 255, 255, 0.06);
  min-height: 0;
  color: var(--text-sidebar);
}

.sidebar__brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 0.9rem;
  background: linear-gradient(180deg, #251a14 0%, var(--bg-sidebar) 100%);
  border-bottom: 1px solid rgba(232, 213, 196, 0.12);
}
.sidebar__brand-text {
  font-family: var(--font-story);
  font-size: 1.5rem;
  line-height: 1;
  color: var(--accent-soft);
  opacity: 0.95;
  font-weight: 600;
}
.sidebar__brand-label {
  font-family: "Crimson Pro", serif;
  font-size: 0.8rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--text-sidebar-muted);
}

.sidebar__tabs {
  display: flex;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.sidebar__tab {
  flex: 1;
  padding: 0.65rem 0.5rem;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  font-family: var(--font-label);
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  color: var(--text-sidebar-muted);
  transition: color 0.2s ease, border-color 0.2s ease, background 0.2s ease;
}
.sidebar__tab:hover {
  color: var(--text-sidebar);
  background: var(--bg-sidebar-hover);
}
.sidebar__tab[aria-selected="true"] {
  color: var(--accent-soft);
  border-bottom-color: var(--accent);
  background: var(--bg-sidebar-hover);
}

.search-wrap {
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.search-pill {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: #fff;
  border-radius: 999px;
  padding: 0.35rem 0.75rem 0.35rem 0.6rem;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.06);
  transition: box-shadow 0.2s ease;
}
.search-pill:focus-within {
  box-shadow: 0 0 0 1px var(--accent), 0 0 16px rgba(192, 57, 43, 0.2);
}
.search-pill__icon {
  color: var(--text-secondary);
  display: flex;
  opacity: 0.7;
  flex-shrink: 0;
}
.search-pill__input {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  font: inherit;
  font-size: 0.88rem;
  font-family: var(--font-label);
  color: var(--text-primary);
  outline: none;
}
.search-pill__input::placeholder {
  color: #9a8a7c;
}

.category-tree {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem 0.6rem 1rem;
  -webkit-overflow-scrolling: touch;
}
.sidebar-placeholder {
  font-size: 0.85rem;
  color: var(--text-sidebar-muted);
  line-height: 1.45;
  margin: 0.5rem 0.25rem;
  font-family: var(--font-label);
}

details.cat-block {
  margin-bottom: 0.5rem;
  border: 1px solid rgba(232, 213, 196, 0.12);
  border-radius: 6px;
  background: var(--bg-sidebar-hover);
  overflow: hidden;
  interpolate-size: allow-keywords;
}
summary.cat-title {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  list-style: none;
  font-size: 0.68rem;
  font-weight: 700;
  font-family: var(--font-label);
  text-transform: uppercase;
  font-variant: small-caps;
  letter-spacing: 0.08em;
  color: #c17a5c;
  padding: 0.5rem 0.5rem 0.45rem 0.45rem;
  border-bottom: 1px solid transparent;
  margin: 0;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.2s ease, color 0.2s ease;
}
summary.cat-title::-webkit-details-marker {
  display: none;
}
summary.cat-title::before {
  content: "▸";
  display: block;
  font-size: 0.55rem;
  line-height: 1;
  width: 1.1em;
  text-align: center;
  flex-shrink: 0;
  color: var(--text-sidebar-muted);
  transition: transform 0.22s ease;
}
details.cat-block[open] summary.cat-title {
  border-bottom-color: rgba(232, 213, 196, 0.12);
  color: #d4896a;
}
details.cat-block[open] summary.cat-title::before {
  content: "▾";
  transform: none;
}
summary.cat-title:hover {
  color: #e8b5a0;
  background: rgba(0, 0, 0, 0.2);
}
details.cat-block .cat-children {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  padding: 0.2rem 0.2rem 0.45rem 0.25rem;
}

.story-row {
  text-align: left;
  width: 100%;
  font: inherit;
  font-size: 0.86rem;
  line-height: 1.4;
  font-family: var(--font-story);
  padding: 0.45rem 0.5rem 0.45rem 0.55rem;
  border: none;
  border-radius: 4px;
  background: transparent;
  cursor: pointer;
  color: var(--text-sidebar);
  border-left: 3px solid transparent;
  word-break: break-word;
  transition: background 0.2s ease, border-color 0.2s ease, font-weight 0.15s ease;
}
.story-row:hover {
  background: rgba(0, 0, 0, 0.25);
  border-left-color: var(--accent);
}
.story-row.is-active {
  background: rgba(192, 57, 43, 0.22);
  border-left-color: #e8a090;
  font-weight: 600;
  color: #fff5f0;
}
.story-row .meta {
  display: block;
  font-size: 0.68rem;
  color: var(--text-sidebar-muted);
  font-weight: 400;
  margin-top: 0.12rem;
}
mark.search-hit {
  background: rgba(255, 230, 120, 0.35);
  color: inherit;
  padding: 0 0.05em;
  border-radius: 2px;
}

/* —— Main reader —— */
.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
  background: var(--reader-bg);
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.04'/%3E%3C/svg%3E");
  -webkit-overflow-scrolling: touch;
  overflow: hidden;
}

.main__surface {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
}

.reader-progress {
  position: sticky;
  top: 0;
  z-index: 20;
  height: 3px;
  width: 100%;
  background: rgba(28, 20, 16, 0.08);
  flex-shrink: 0;
  pointer-events: none;
}
.reader-progress__bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.12s ease-out;
  border-radius: 0 1px 1px 0;
}

/* Bookmarks grid */
.bookmarks-panel {
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 0.5rem clamp(1rem, 3vw, 2.5rem) 3rem;
}
.bookmarks-panel__head {
  font-family: "Crimson Pro", serif;
  font-size: 1.35rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 1.25rem;
  letter-spacing: 0.02em;
}
.bookmarks-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
  list-style: none;
  margin: 0;
  padding: 0;
}
.bookmark-card {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  text-align: left;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1rem 0.85rem;
  box-shadow: 0 2px 12px rgba(28, 20, 16, 0.06);
  transition: box-shadow 0.2s ease, transform 0.2s ease, border-color 0.2s ease;
  cursor: pointer;
  min-height: 7rem;
  font: inherit;
  color: inherit;
}
.bookmark-card:hover,
.bookmark-card:focus-visible {
  box-shadow: 0 6px 24px rgba(28, 20, 16, 0.12);
  border-color: var(--accent);
  transform: translateY(-2px);
  outline: none;
}
.bookmark-card__tag {
  display: inline-block;
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 700;
  color: #c17a5c;
  margin-bottom: 0.4rem;
  font-family: var(--font-label);
}
.bookmark-card__title {
  font-family: var(--font-story);
  font-size: 1.02rem;
  line-height: 1.4;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 0.75rem;
  flex: 1;
}
.bookmark-card__cta {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--accent);
  font-family: var(--font-label);
  margin-top: auto;
  padding-top: 0.25rem;
  border-top: 1px solid var(--border);
}
.bookmarks-empty {
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.95rem;
  max-width: 24rem;
  margin: 2rem auto;
  line-height: 1.6;
  font-family: var(--font-ui);
}

.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  min-height: auto;
  padding: 1rem clamp(1rem, 3vw, 2.5rem) 2rem;
  text-align: center;
  color: var(--text-secondary);
}
.welcome__lede {
  max-width: 24rem;
  line-height: 1.6;
  font-size: 1.05rem;
  font-family: var(--font-ui);
}

/* Story article — full width of reader with modest gutters */
.story-article {
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 0.35rem clamp(1rem, 3vw, 2.5rem) 3rem;
  box-sizing: border-box;
  opacity: 1;
  transform: translateY(0);
  transition: opacity 0.35s ease, transform 0.35s ease;
}
.story-article.is-story-entering {
  opacity: 0;
  transform: translateY(12px);
}
.story-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.65rem;
  padding: 0 0 0.5rem;
  border-bottom: 1px solid var(--border);
  font-family: var(--font-label);
}
.story-breadcrumb {
  font-size: 0.8rem;
  color: var(--text-secondary);
  font-family: var(--font-label);
  margin-bottom: 0.3rem;
  line-height: 1.4;
}
.story-breadcrumb__sep {
  margin: 0 0.35em;
  opacity: 0.5;
}
.story-breadcrumb a {
  color: inherit;
  text-decoration: none;
  border-bottom: 1px solid transparent;
}
.story-breadcrumb a:hover {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
.story-header-block {
  margin-bottom: 0.85rem;
}
.story-title-block {
  position: relative;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 0.45rem;
}
.story-title-block h1 {
  flex: 1 1 14rem;
  margin: 0;
  font-size: 1.6rem;
  line-height: 1.3;
  font-weight: 700;
  font-family: var(--font-story);
  color: var(--text-primary);
}
.title-actions {
  display: flex;
  gap: 0.3rem;
  align-items: center;
  opacity: 0;
  transform: translateY(-2px);
  transition: opacity 0.2s ease;
  flex-shrink: 0;
}
.story-title-block:hover .title-actions,
.title-actions:focus-within {
  opacity: 1;
}
.icon-btn-ghost {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.1rem;
  height: 2.1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.7);
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s, background 0.2s;
}
.icon-btn-ghost:hover {
  color: var(--accent);
  border-color: var(--accent);
  background: #fff;
}
.story-title-rule {
  height: 2px;
  background: linear-gradient(90deg, var(--accent) 0%, var(--accent) 3rem, var(--border) 3rem, var(--border) 100%);
  border-radius: 1px;
  max-width: 100%;
}

.story-body {
  font-family: var(--font-story-serif);
  font-size: 1.15rem;
  line-height: 2.2;
  color: var(--text-primary);
  text-align: start;
  word-break: break-word;
  overflow-wrap: anywhere;
  padding-top: 0.5rem;
}
.story-body p {
  margin: 0 0 1.8rem;
}
.story-body p:first-of-type::first-letter {
  float: left;
  font-size: 4em;
  line-height: 0.85;
  margin: 0.05em 0.12em 0 0;
  font-family: var(--font-story-serif);
  color: var(--accent);
  font-weight: 600;
}
.story-addendum {
  margin-top: 1.5rem;
  padding-top: 1.25rem;
  border-top: 1px dashed var(--border);
}
.story-addendum .label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
  font-family: var(--font-label);
}
.story-addendum .body {
  font-family: var(--font-story-serif);
  color: var(--text-primary);
  line-height: 2;
  font-size: 1.05rem;
}
.story-addendum .body p {
  margin: 0 0 1.1em;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
}
.toggle-row input {
  width: 1.1rem;
  height: 1.1rem;
  accent-color: var(--accent);
  cursor: pointer;
}

/* Dialogs */
dialog.modal {
  border: none;
  padding: 0;
  max-width: min(100vw - 1.5rem, 420px);
  width: 100%;
  background: transparent;
  color: var(--text-primary);
}
dialog::backdrop {
  background: rgba(18, 12, 8, 0.45);
}
.modal__card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--r);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.15);
  padding: 1.15rem 1.2rem 1.2rem;
}
.modal__head h2 {
  font-family: "Crimson Pro", serif;
  margin: 0 0 0.3rem;
  font-size: 1.2rem;
}
.modal__lede {
  margin: 0;
  font-size: 0.88rem;
  color: var(--text-secondary);
  line-height: 1.45;
  font-family: var(--font-ui);
}
.modal__foot {
  display: flex;
  justify-content: flex-end;
  gap: 0.4rem;
  margin-top: 0.8rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--border);
}
.modal__foot--split {
  justify-content: space-between;
  flex-wrap: wrap;
}
.form-row {
  margin-bottom: 0.6rem;
}
.form-row label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 0.2rem;
  font-family: var(--font-label);
}
.error-msg {
  color: #8b1e1e;
  font-size: 0.85rem;
  margin: 0.3rem 0 0;
}
.profile-list {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  max-height: 200px;
  overflow-y: auto;
  margin: 0.5rem 0;
}
.profile-pick {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  text-align: left;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  font: inherit;
  cursor: pointer;
  color: var(--text-primary);
}
.profile-pick.is-selected {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.profile-pick__color {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

#menuToggle {
  display: none;
}
@media (max-width: 768px) {
  #menuToggle {
    display: inline-flex;
  }
  .shell {
    position: relative;
  }
  .sidebar {
    position: fixed;
    left: 0;
    top: var(--header-h);
    bottom: 0;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    max-width: min(90vw, 320px);
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .app-top {
    position: relative;
    z-index: 50;
  }
  .bookmarks-grid {
    grid-template-columns: 1fr;
  }
  .title-actions {
    opacity: 1;
  }
}

.status-toast {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%) translateY(200%);
  z-index: 2000;
  background: var(--bg-sidebar);
  color: var(--text-sidebar);
  padding: 0.5rem 0.9rem;
  border-radius: 8px;
  font-size: 0.88rem;
  font-family: var(--font-label);
  transition: transform 0.2s;
  max-width: 90vw;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}
.status-toast.is-on {
  transform: translateX(-50%) translateY(0);
}

@media (prefers-reduced-motion: reduce) {
  .story-article,
  .story-article.is-story-entering,
  .reader-progress__bar,
  .sidebar__tab,
  .story-row,
  .bookmark-card {
    transition: none;
  }
  .app-top.is-scrolled {
    box-shadow: none;
  }
}
