:root {
  color-scheme: light;
  --safe-area-max-inset-bottom: env(safe-area-max-inset-bottom, 0px);
  --yellow-50: #fff9d6;
  --yellow-100: #fff1a8;
  --yellow-300: #ffd84d;
  --yellow-500: #f6c600;
  --yellow-700: #b88700;
  --ink: #1e1e1e;
  --muted: #646464;
  --surface: #ffffff;
  --surface-alt: #f6f7f8;
  --line: #ded7bf;
  --accent: #176b87;
  --danger: #b33636;
  --shadow: 0 18px 48px rgba(51, 43, 14, 0.12);
  --radius: 8px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --yellow-50: #342d12;
  --yellow-100: #4b3f12;
  --yellow-300: #caa72b;
  --yellow-500: #f0c533;
  --yellow-700: #ffe073;
  --ink: #f4f0df;
  --muted: #c5bea3;
  --surface: #191814;
  --surface-alt: #202124;
  --line: #4c432a;
  --accent: #72c7df;
  --danger: #ff8d8d;
  --shadow: 0 18px 48px rgba(0, 0, 0, 0.28);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--surface-alt);
}

body {
  min-width: 320px;
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(246, 198, 0, 0.12), transparent 280px),
    var(--surface-alt);
  font-family: Inter, Roboto, "Segoe UI", system-ui, -apple-system, sans-serif;
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
}

a {
  color: inherit;
  text-decoration: none;
}

img {
  display: block;
  max-width: 100%;
}

.app-shell {
  min-height: 100vh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 64px;
  padding: 10px clamp(16px, 4vw, 48px);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(18px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  overflow: hidden;
  background: #fff7d1;
  border-radius: var(--radius);
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.08);
}

.brand-mark img {
  width: 100%;
  height: 100%;
}

.brand strong,
.brand small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brand strong {
  font-size: 1rem;
}

.brand small {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.78rem;
}

.topbar-actions,
.reader-controls,
.button-row,
.tag-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.search-container {
  position: relative;
}

.search-container input {
  min-height: 42px;
  width: 200px;
  padding: 0 16px;
  color: var(--ink);
  background-color: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  outline: none;
  transition: width 0.2s ease, border-color 0.2s ease;
}

.search-container input:focus {
  width: 260px;
  border-color: var(--yellow-500);
}

.desktop-only {
  display: block;
}

.mobile-only {
  display: none;
}

@media (max-width: 620px) {
  .desktop-only {
    display: none;
  }
  .mobile-only {
    display: inline-flex;
  }
  .mobile-only.icon-button {
    display: inline-grid;
  }
}

.search-dialog {
  width: 100%;
  max-width: 100%;
  margin: 0;
  margin-bottom: auto;
  padding: 16px clamp(16px, 4vw, 48px);
  border: none;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.search-dialog::backdrop {
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
}

.search-dialog-form {
  width: 100%;
}

.search-dialog-header {
  display: flex;
  gap: 12px;
  align-items: center;
}

.search-dialog-header input {
  flex: 1;
  min-height: 42px;
  padding: 0 16px;
  color: var(--ink);
  background-color: var(--surface-alt);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  outline: none;
}

.search-dialog-header input:focus {
  border-color: var(--yellow-500);
}

.password-dialog {
  width: min(420px, calc(100% - 32px));
  margin: auto;
  padding: 32px;
  border: none;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--ink);
}

.password-dialog::backdrop {
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

.password-dialog-form {
  display: grid;
  gap: 14px;
  text-align: center;
}

.password-dialog-icon {
  display: grid;
  place-items: center;
  margin: 0 auto;
  width: 56px;
  height: 56px;
  background: var(--yellow-50);
  border-radius: 50%;
}

.password-dialog-icon svg {
  width: 28px;
  height: 28px;
  stroke: var(--yellow-500);
}

.password-dialog-form h2 {
  margin: 0;
  font-size: 1.3rem;
}

.password-dialog-form p {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.password-dialog-form input {
  min-height: 44px;
  padding: 0 16px;
  color: var(--ink);
  background-color: var(--surface-alt);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  outline: none;
  font: inherit;
  text-align: center;
  letter-spacing: 0.1em;
}

.password-dialog-form input:focus {
  border-color: var(--yellow-500);
}

.password-error {
  color: var(--danger) !important;
  font-weight: 700;
  font-size: 0.86rem !important;
}

.password-dialog-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.encrypted-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  color: var(--yellow-700);
  background: var(--yellow-50);
  border: 1px solid var(--yellow-300);
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.encrypted-badge svg {
  width: 12px;
  height: 12px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.decrypt-loading {
  display: grid;
  place-items: center;
  padding: 48px 16px;
  color: var(--muted);
  font-weight: 700;
}

.decrypt-loading .spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--line);
  border-top-color: var(--yellow-500);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin-bottom: 12px;
}

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

.app {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 28px clamp(16px, 4vw, 48px) 56px;
}

.app:focus {
  outline: none;
}

.manga-hero {
  display: grid;
  grid-template-columns: minmax(220px, 320px) minmax(0, 1fr);
  gap: clamp(20px, 5vw, 48px);
  align-items: center;
  padding: clamp(22px, 5vw, 48px) 0 34px;
}

.eyebrow,
.status,
.meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.eyebrow,
.status {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 800;
}

.manga-summary h1,
.reader-info h1,
.not-found h1 {
  margin: 8px 0;
  font-size: clamp(2rem, 5vw, 4rem);
  line-height: 1;
}

.manga-summary .description {
  max-width: 700px;
  margin: 18px 0 0;
  color: var(--muted);
  font-size: 1.02rem;
  line-height: 1.65;
}

select {
  appearance: none;
  min-height: 44px;
  width: 100%;
  color: var(--ink);
  background-color: var(--surface);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' viewBox='0 0 24 24' fill='none' stroke='%23646464' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-position: right 14px center;
  background-repeat: no-repeat;
  background-size: 16px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 42px 0 12px;
  outline: none;
}

:root[data-theme="dark"] select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' viewBox='0 0 24 24' fill='none' stroke='%23c5bea3' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
}

select:focus,
button:focus-visible,
a:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--yellow-500) 55%, transparent);
  outline-offset: 2px;
}

.cover-panel {
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.cover-panel img {
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  background: var(--yellow-100);
  border-radius: calc(var(--radius) - 2px);
}

.manga-summary {
  display: grid;
  min-width: 0;
  gap: 16px;
}

.library-heading {
  display: grid;
  gap: 10px;
  padding: clamp(22px, 5vw, 48px) 0 18px;
}

.library-heading h1 {
  margin: 0;
  font-size: clamp(2rem, 5vw, 4rem);
  line-height: 1;
}

.library-heading .description {
  max-width: 680px;
  margin: 0;
  color: var(--muted);
  font-size: 1.02rem;
  line-height: 1.65;
}

.manga-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 14px;
}

.manga-card {
  display: grid;
  grid-template-columns: 120px minmax(0, 1fr);
  gap: 14px;
  min-width: 0;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(51, 43, 14, 0.06);
}

.manga-card-cover img {
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  background: var(--yellow-100);
  border-radius: calc(var(--radius) - 2px);
}

.manga-card-main {
  display: grid;
  align-content: start;
  min-width: 0;
  gap: 10px;
}

.manga-card h2 {
  margin: 0;
  font-size: 1.25rem;
  line-height: 1.15;
}

.manga-card .tag-row {
  gap: 6px;
}

.chapter-section {
  display: grid;
  gap: 14px;
  padding-top: 22px;
  border-top: 1px solid var(--line);
}

.section-heading {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
}

.section-heading h2 {
  margin: 6px 0 0;
  font-size: clamp(1.45rem, 3vw, 2rem);
  line-height: 1.1;
}

.section-heading span {
  color: var(--muted);
  font-weight: 800;
}

.chapter-list {
  display: grid;
  gap: 10px;
}

.chapter-row {
  display: grid;
  grid-template-columns: 54px minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  min-height: 72px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(51, 43, 14, 0.06);
}

.chapter-row.current {
  border-color: var(--yellow-500);
  box-shadow: inset 4px 0 0 var(--yellow-500), 0 8px 24px rgba(51, 43, 14, 0.08);
}

.chapter-number {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  color: #1d1a12;
  background: var(--yellow-500);
  border-radius: var(--radius);
  font-weight: 900;
}

.chapter-main {
  min-width: 0;
}

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

.chapter-main small,
.chapter-pages {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.86rem;
}

.chapter-pages {
  font-weight: 800;
  white-space: nowrap;
}

.tag-row span {
  min-height: 28px;
  padding: 5px 9px;
  color: color-mix(in srgb, var(--accent) 78%, var(--ink));
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 22%, transparent);
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
}

.primary-button,
.text-button,
.icon-button,
.segmented button,
.page-step {
  display: inline-grid;
  place-items: center;
  min-height: 42px;
  border: 0;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 800;
}

.primary-button {
  padding: 0 16px;
  color: #1d1a12;
  background: var(--yellow-500);
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.14);
}

.text-button {
  padding: 0 12px;
  color: var(--accent);
  background: transparent;
}

.icon-button {
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  padding: 0;
  color: var(--ink);
  background: var(--surface-alt);
  border: 1px solid var(--line);
  font-size: 1.25rem;
  line-height: 1;
  text-align: center;
}

.icon-button:disabled,
.icon-button[aria-disabled="true"] {
  cursor: not-allowed;
  opacity: 0.38;
}

.icon-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.reader-view {
  width: 100%;
  padding-top: 16px;
  padding-bottom: 112px;
}

.reader-toolbar {
  position: fixed;
  right: 12px;
  bottom: max(12px, env(safe-area-inset-bottom));
  left: 12px;
  z-index: 10;
  display: grid;
  grid-template-columns: auto minmax(180px, 1fr) auto auto;
  gap: 12px;
  align-items: center;
  width: min(calc(100% - 24px), 1180px);
  max-width: 1180px;
  margin: 0 auto;
  padding: 12px;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
  transition: transform 180ms ease, opacity 180ms ease;
  will-change: transform;
}

body.reader-toolbar-hidden .reader-toolbar {
  opacity: 0;
  pointer-events: none;
  transform: translateY(calc(100% + 24px));
}

.chapter-title {
  min-width: 0;
}

.chapter-title strong,
.chapter-title span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chapter-title span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.86rem;
}

.reader-controls select {
  width: auto;
  max-width: 220px;
}

.segmented {
  display: flex;
  min-height: 42px;
  padding: 3px;
  background: var(--surface-alt);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.segmented button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 34px;
  padding: 0 10px;
  color: var(--muted);
  background: transparent;
}

.segmented svg {
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.segmented button.active {
  color: #1d1a12;
  background: var(--yellow-500);
}

.reader-info {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
  max-width: 980px;
  scroll-margin-top: 84px;
  margin: 0 auto 22px;
}

.reader-info h1 {
  font-size: clamp(1.75rem, 4.5vw, 3rem);
}

#page-counter {
  margin: 0;
  color: var(--muted);
  font-weight: 800;
}

.reader-mode-vertical #page-counter {
  display: none;
}

.site-footer {
  margin-top: 32px;
  padding-top: 18px;
  color: var(--muted);
  border-top: 1px solid var(--line);
  font-size: 0.9rem;
  font-weight: 800;
  text-align: center;
}

.pages {
  display: grid;
  gap: 14px;
  justify-items: center;
  margin: 0 auto;
}

.width-compact .pages,
.width-compact .reader-info {
  max-width: 820px;
}

.width-comfortable .pages,
.width-comfortable .reader-info {
  max-width: 980px;
}

.width-wide .pages,
.width-wide .reader-info {
  max-width: 1220px;
}

@media (min-width: 841px) {
  .reader-format-4koma.reader-mode-vertical .reader-info {
    max-width: 760px;
  }

  .reader-format-4koma.reader-mode-vertical .pages {
    grid-template-columns: 1fr;
    max-width: 760px;
    gap: 16px;
  }

  .reader-format-4koma.reader-mode-vertical .manga-page {
    width: auto;
    max-width: 100%;
    max-height: calc(100vh - 84px);
    min-height: 0;
    object-fit: contain;
  }

  .reader-format-4koma.reader-mode-single .reader-info {
    max-width: 760px;
  }

  .reader-format-4koma.reader-mode-single .single-page {
    max-width: 920px;
  }

  .reader-format-4koma.reader-mode-single .single-page-frame {
    width: fit-content;
    max-width: 100%;
    justify-self: center;
    justify-items: center;
  }

  .reader-format-4koma.reader-mode-single .single-page-frame .manga-page {
    justify-self: center;
    width: auto;
    max-width: 100%;
    max-height: calc(100vh - 150px);
    min-height: 0;
    object-fit: contain;
  }

  .width-compact .single-page {
    max-width: 980px;
  }

  .width-compact .single-page-frame {
    width: fit-content;
    max-width: 100%;
    justify-self: center;
    justify-items: center;
  }

  .width-compact .single-page-frame .manga-page {
    justify-self: center;
    width: auto;
    max-width: 100%;
    max-height: calc(100vh - 140px);
    object-fit: contain;
  }
}

.manga-page {
  width: 100%;
  min-height: 320px;
  scroll-margin-top: 84px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.reader-format-webtoon.reader-mode-vertical .pages {
  gap: 0;
  line-height: 0;
}

.reader-format-webtoon.reader-mode-vertical .manga-page {
  min-height: 0;
  margin-top: -1px;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

.reader-format-webtoon.reader-mode-vertical .manga-page:first-child {
  margin-top: 0;
}

.single-page {
  position: relative;
  grid-template-columns: 64px minmax(0, 1fr) 64px;
  align-items: center;
  max-width: 1080px;
}

.single-page-frame {
  position: relative;
  display: grid;
  align-items: start;
  grid-column: 2;
  width: 100%;
  transition: opacity 120ms ease;
}

.single-page-frame .manga-page {
  grid-area: 1 / 1;
  align-self: start;
  justify-self: stretch;
  width: 100%;
  height: auto;
}

.single-page-frame.loading-next {
  opacity: 0.94;
}

.page-tap-zone {
  display: none;
}

.page-step {
  position: sticky;
  top: 50vh;
  width: 52px;
  height: 72px;
  color: #1d1a12;
  background: var(--yellow-500);
  font-size: 2rem;
}

.page-step:disabled,
.page-tap-zone:disabled {
  cursor: not-allowed;
  opacity: 0.35;
}

.page-step.prev {
  grid-column: 1;
}

.page-step.next {
  grid-column: 3;
}

.chapter-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  max-width: 980px;
  margin: 24px auto 0;
}

.final-home {
  margin-top: 12px;
}

.empty,
.not-found {
  padding: 32px 0;
  color: var(--muted);
}

@media (max-width: 840px) {
  .manga-hero {
    grid-template-columns: 1fr;
  }

  .cover-panel {
    width: min(280px, 100%);
  }

  .reader-toolbar {
    right: 0;
    bottom: calc(env(safe-area-inset-bottom, 0px) - var(--safe-area-max-inset-bottom));
    left: 0;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 8px;
    width: 100%;
    max-width: none;
    min-height: calc(62px + var(--safe-area-max-inset-bottom));
    margin: 0;
    padding: 10px;
    padding-bottom: calc(10px + var(--safe-area-max-inset-bottom));
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: 0;
  }

  body.reader-toolbar-hidden .reader-toolbar {
    opacity: 1;
    pointer-events: auto;
    transform: none;
  }

  .reader-toolbar .chapter-title {
    display: none;
  }

  .reader-controls {
    display: grid;
    grid-column: 2;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: stretch;
    gap: 8px;
  }

  .reader-controls select {
    min-height: 40px;
    width: 100%;
    max-width: none;
    padding-right: 30px;
    background-position: right 10px center;
    background-size: 14px 14px;
  }

  #width-select {
    display: none;
  }

  .segmented {
    grid-column: 2;
    grid-row: 1;
    min-width: 84px;
    min-height: 40px;
    padding: 2px;
  }

  .segmented button {
    width: 38px;
    min-height: 34px;
    padding: 0;
  }

  .chapter-jump {
    width: 40px;
    height: 40px;
  }

  .mode-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  .reader-view {
    padding-bottom: calc(104px + var(--safe-area-max-inset-bottom));
  }
}

@media (max-width: 620px) {
  .topbar {
    padding-inline: 12px;
    transition: transform 180ms ease, box-shadow 180ms ease;
    will-change: transform;
  }

  body.topbar-hidden .topbar {
    transform: translateY(calc(-100% - 1px));
    box-shadow: none;
  }

  .app {
    padding-inline: 12px;
  }

  .manga-hero {
    padding-top: 18px;
  }

  .chapter-row {
    grid-template-columns: 44px minmax(0, 1fr);
    align-items: start;
  }

  .manga-card {
    grid-template-columns: 92px minmax(0, 1fr);
  }

  .chapter-number {
    width: 36px;
    height: 36px;
  }

  .chapter-pages {
    grid-column: 2;
  }

  .reader-info {
    align-items: start;
    flex-direction: column;
  }

  .single-page {
    grid-template-columns: 1fr;
  }

  .single-page-frame {
    grid-column: 1 / -1;
    grid-row: 1;
  }

  .page-step {
    display: none;
  }

  .page-tap-zone {
    position: absolute;
    top: 0;
    bottom: 0;
    z-index: 2;
    display: block;
    width: 38%;
    min-height: 0;
    padding: 0;
    background: transparent;
    border: 0;
    border-radius: 0;
    cursor: pointer;
  }

  .tap-prev {
    left: 0;
  }

  .tap-next {
    right: 0;
  }
}
