:root {
  --bg: #12100e;
  --bg-raised: #1b1815;
  --bg-sunken: #0d0b0a;
  --line: #322c26;
  --line-soft: #262119;
  --ink: #f2ece3;
  --ink-dim: #b3a897;
  --ink-faint: #7d7364;
  --accent: #d9a441;
  --accent-ink: #1a1409;
  --danger: #e06a5a;
  --radius: 10px;
  --shadow: 0 10px 30px rgb(0 0 0 / 45%);
  --font: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --font-ui: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a {
  color: inherit;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 13px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-raised);
  color: var(--ink);
  font-size: 14px;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}

.btn:hover:not(:disabled) {
  background: #262119;
  border-color: #463e33;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
  background: #e7b455;
  border-color: #e7b455;
}

.btn-quiet {
  background: transparent;
  border-color: transparent;
  color: var(--ink-dim);
}

.btn-quiet:hover:not(:disabled) {
  background: #241f19;
  border-color: transparent;
  color: var(--ink);
}

.btn-icon {
  padding: 7px 9px;
}

input[type="text"],
input[type="search"],
input[type="password"],
select {
  padding: 8px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-sunken);
  color: var(--ink);
  width: 100%;
}

input:focus-visible,
select:focus-visible,
button:focus-visible,
a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --- app shell ---------------------------------------------------------- */

.app {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100%;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line-soft);
  background: linear-gradient(#191612, #151310);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font);
  font-size: 19px;
  letter-spacing: 0.01em;
  text-decoration: none;
}

.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: 6px;
  background: linear-gradient(150deg, var(--accent), #a9761f);
  display: grid;
  place-items: center;
  color: var(--accent-ink);
  font-size: 14px;
  font-weight: 700;
}

.topbar-search {
  margin-left: auto;
  max-width: 300px;
  flex: 1 1 180px;
}

#menuBtn {
  display: none;
}

.topbar-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.body {
  display: grid;
  grid-template-columns: 264px 1fr;
  min-height: 0;
}

/* --- sidebar ------------------------------------------------------------ */

.sidebar {
  border-right: 1px solid var(--line-soft);
  padding: 16px 12px;
  overflow: auto;
  background: #151310;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 6px 8px;
  color: var(--ink-faint);
  font-size: 11px;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}

.tree,
.tree ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tree ul {
  margin-left: 13px;
  border-left: 1px solid var(--line-soft);
  padding-left: 6px;
}

.tree-row {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: 7px;
  padding: 3px 6px 3px 2px;
}

.tree-row:hover {
  background: #201b16;
}

.tree-row.is-active {
  background: #2a2219;
}

.tree-row.is-drop {
  outline: 1px dashed var(--accent);
}

.tree-toggle {
  width: 18px;
  height: 18px;
  display: grid;
  place-items: center;
  border: 0;
  background: none;
  color: var(--ink-faint);
  font-size: 10px;
  transition: transform 0.15s;
}

.tree-toggle[hidden] {
  display: none;
}

.tree-toggle.is-open {
  transform: rotate(90deg);
}

.tree-label {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 7px;
  border: 0;
  background: none;
  text-align: left;
  padding: 3px 2px;
  font-size: 14px;
  color: var(--ink-dim);
  min-width: 0;
}

.tree-row.is-active .tree-label {
  color: var(--ink);
}

.tree-label span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tree-count {
  color: var(--ink-faint);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.tree-menu {
  opacity: 0;
  border: 0;
  background: none;
  color: var(--ink-faint);
  padding: 2px 5px;
  border-radius: 5px;
}

.tree-row:hover .tree-menu,
.tree-menu:focus-visible {
  opacity: 1;
}

/* --- main --------------------------------------------------------------- */

.main {
  overflow: auto;
  padding: 22px 26px 60px;
  min-width: 0;
}

.crumbs {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  color: var(--ink-faint);
  font-size: 13px;
  margin-bottom: 4px;
  min-height: 20px;
}

.crumbs button {
  border: 0;
  background: none;
  color: var(--ink-dim);
  padding: 0;
}

.crumbs button:hover {
  color: var(--ink);
  text-decoration: underline;
}

.main-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

.main-head h1 {
  font-family: var(--font);
  font-weight: 500;
  font-size: 28px;
  margin: 0;
}

.main-head .meta {
  color: var(--ink-faint);
  font-size: 13px;
}

.section-label {
  color: var(--ink-faint);
  font-size: 11px;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  margin: 26px 0 10px;
}

.folder-strip {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
}

.folder-chip {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 11px 13px;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  background: var(--bg-raised);
  text-align: left;
  color: var(--ink-dim);
  min-width: 0;
}

.folder-chip:hover {
  border-color: var(--line);
  color: var(--ink);
}

.folder-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.shelf {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
  gap: 22px 18px;
}

.book {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.book-link {
  display: block;
  text-decoration: none;
  border-radius: 4px 8px 8px 4px;
}

.book-cover {
  position: relative;
  aspect-ratio: 1 / 1.414;
  border-radius: 3px 7px 7px 3px;
  background: #221d18 linear-gradient(100deg, rgb(0 0 0 / 45%) 0 5px, rgb(255 255 255 / 4%) 6px 9px, transparent 10px);
  border: 1px solid var(--line-soft);
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: transform 0.18s ease, box-shadow 0.18s ease;
  display: grid;
  place-items: center;
}

.book-link:hover .book-cover {
  transform: translateY(-3px);
  box-shadow: 0 16px 34px rgb(0 0 0 / 55%);
}

.book-cover canvas,
.book-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.book-cover .placeholder {
  color: var(--ink-faint);
  font-size: 12px;
  padding: 12px;
  text-align: center;
}

.book-title {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.3;
  color: var(--ink);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-meta {
  color: var(--ink-faint);
  font-size: 12px;
}

.book-actions {
  position: absolute;
  top: 7px;
  right: 7px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.15s;
}

.book:hover .book-actions,
.book-actions:focus-within {
  opacity: 1;
}

.book-actions button {
  border: 1px solid var(--line);
  background: rgb(12 10 8 / 85%);
  color: var(--ink-dim);
  border-radius: 6px;
  padding: 3px 7px;
  font-size: 12px;
  backdrop-filter: blur(3px);
}

.book-actions button:hover {
  color: var(--ink);
}

.empty {
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  padding: 46px 24px;
  text-align: center;
  color: var(--ink-faint);
}

.empty strong {
  display: block;
  color: var(--ink-dim);
  font-family: var(--font);
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 6px;
}

/* --- drop zone, dialogs, toasts ---------------------------------------- */

.dropveil {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  place-items: center;
  background: rgb(10 8 6 / 82%);
  backdrop-filter: blur(2px);
  font-family: var(--font);
  font-size: 22px;
  color: var(--ink);
}

.dropveil.is-on {
  display: grid;
}

dialog {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-raised);
  color: var(--ink);
  padding: 0;
  width: min(460px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgb(8 6 5 / 65%);
}

dialog form {
  padding: 20px;
  display: grid;
  gap: 14px;
}

dialog h2 {
  margin: 0;
  font-family: var(--font);
  font-weight: 500;
  font-size: 20px;
}

.field {
  display: grid;
  gap: 6px;
}

.field label {
  font-size: 12px;
  color: var(--ink-faint);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.field .hint {
  color: var(--ink-faint);
  font-size: 12px;
}

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

.is-error-text {
  color: var(--danger);
}

.filedrop {
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  padding: 22px;
  text-align: center;
  color: var(--ink-faint);
  background: var(--bg-sunken);
}

.filedrop.is-over {
  border-color: var(--accent);
  color: var(--ink-dim);
}

.progress {
  height: 4px;
  border-radius: 4px;
  background: var(--line-soft);
  overflow: hidden;
}

.progress span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.2s;
}

/* Upload finished, server still working - width no longer means anything. */
.progress.is-working span {
  width: 35% !important;
  transition: none;
  animation: crawl 1.2s ease-in-out infinite;
}

@keyframes crawl {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(320%);
  }
}

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: grid;
  gap: 8px;
  z-index: 60;
}

.toast {
  padding: 10px 14px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--bg-raised);
  box-shadow: var(--shadow);
  max-width: 340px;
  font-size: 14px;
}

.toast.is-error {
  border-color: var(--danger);
  color: #ffd7cf;
}

@media (max-width: 820px) {
  .body {
    grid-template-columns: 1fr;
  }

  #menuBtn {
    display: inline-flex;
  }

  .sidebar {
    display: none;
  }

  .sidebar.is-open {
    display: block;
    position: fixed;
    inset: 57px 0 0 0;
    z-index: 40;
  }

  .main {
    padding: 18px 16px 60px;
  }
}
