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

body {
  background: #0a0e17;
  color: #e0e0e0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
}

/* --- Header / Search --- */

body > .back-link {
  display: block;
  padding: 8px 8px 0;
}

header {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: 8px;
  background: #0a0e17;
}

.logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 18px;
  font-weight: 800;
  color: #4a7cff;
  text-decoration: none;
  flex-shrink: 0;
  line-height: 1;
}

.logo-beta {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 4.5px;
  text-transform: uppercase;
  opacity: 0.6;
}

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

.header-row form {
  flex: 1;
}

.settings-link {
  font-size: 20px;
  color: #9aa0ab;
  text-decoration: none;
  padding: 6px;
  line-height: 1;
}

#search-form {
  position: relative;
}

.search-clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #6a6f79;
  font-size: 18px;
  cursor: pointer;
  padding: 4px 6px;
  line-height: 1;
}

.search-clear:hover {
  color: #e0e0e0;
}

/* --- Autocomplete Dropdown --- */

.ac-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 20;
  background: #141922;
  border: 1px solid #2a2f3a;
  border-top: none;
  border-radius: 0 0 8px 8px;
  max-height: 240px;
  overflow-y: auto;
}

.ac-dropdown:empty {
  display: none;
}

.ac-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 8px 12px;
  background: none;
  border: none;
  color: #e0e0e0;
  font-size: 14px;
  cursor: pointer;
  text-align: left;
}

.ac-item:hover {
  background: #1e2636;
}

.ac-count {
  font-size: 11px;
  color: #6a6f79;
  flex-shrink: 0;
  margin-left: 8px;
}

#search-form input[type="search"] {
  width: 100%;
  padding: 10px 32px 10px 14px;
  border: 1px solid #2a2f3a;
  border-radius: 8px;
  background: #141922;
  color: #e0e0e0;
  font-size: 16px;
  outline: none;
}

#search-form input[type="search"]:focus {
  border-color: #4a7cff;
}

/* --- Tag Chips --- */

.chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 8px 0 4px;
  scrollbar-width: none;
}

.chips::-webkit-scrollbar {
  display: none;
}

.chip {
  flex-shrink: 0;
  padding: 5px 12px;
  border: 1px solid #2a2f3a;
  border-radius: 16px;
  background: #141922;
  color: #9aa0ab;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}

.chip:active {
  background: #1e2636;
  color: #e0e0e0;
}

button.chip-sort {
  border-color: rgba(74, 124, 255, 0.3);
  color: #4a7cff;
}

button.chip-filter {
  border-color: rgba(242, 172, 8, 0.3);
  color: #f2ac08;
}

button.chip-content {
  border-color: rgba(221, 0, 221, 0.3);
  color: #dd00dd;
}

button.chip-fav {
  border-color: rgba(235, 64, 52, 0.3);
  color: #eb4034;
}

/* --- Grid --- */

#grid {
  padding: 4px;
}

.card-batch {
  columns: 1;
  column-gap: 4px;
}

@media (min-width: 600px) {
  .card-batch { columns: 2; }
}

@media (min-width: 900px) {
  .card-batch { columns: 3; }
}

@media (min-width: 1200px) {
  .card-batch { columns: 4; }
}

@media (min-width: 1600px) {
  .card-batch { columns: 5; }
}

/* --- Post Cards --- */

.card {
  position: relative;
  border-radius: 6px;
  overflow: hidden;
  background: #141922;
  break-inside: avoid;
  margin-bottom: 4px;
}

.card a {
  display: block;
  text-decoration: none;
  color: inherit;
}

.card-img-wrap {
  position: relative;
  width: 100%;
  overflow: hidden;
  background: #0d1117;
}

.card-img-wrap img {
  display: block;
  width: 100%;
  height: auto;
  object-fit: cover;
}

/* --- Badges --- */

.badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  text-transform: uppercase;
}

.rating-e {
  background: #c0392b;
  color: #fff;
}

.rating-q {
  background: #d4a017;
  color: #000;
}

.rating-s {
  background: #27ae60;
  color: #fff;
}

.play-overlay {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 48px;
  height: 48px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  padding-left: 3px;
  pointer-events: none;
}

/* --- Card Info Bar --- */

.card-info {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 6px;
  font-size: 12px;
  color: #8a8f99;
}

.card-score,
.card-favs {
  white-space: nowrap;
}

.card-favs {
  color: #c0392b;
}

.card-animated {
  margin-left: auto;
  font-size: 10px;
  color: #dd00dd;
  white-space: nowrap;
}

/* --- Infinite Scroll Sentinel --- */

.sentinel {
  height: 1px;
}

/* --- Error --- */

.grid-empty {
  text-align: center;
  padding: 60px 16px;
  color: #6a6f79;
  font-size: 15px;
}

.grid-error {
  grid-column: 1 / -1;
  text-align: center;
  padding: 40px 16px;
  color: #666;
  font-size: 14px;
}

/* --- Post Detail Page --- */

.post-page {
  max-width: 900px;
  margin: 0 auto;
  padding: 12px;
}

.post-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.back-link {
  display: inline-block;
  color: #4a7cff;
  text-decoration: none;
  font-size: 14px;
  padding: 8px 0;
  margin-bottom: 8px;
}

.back-link:hover {
  text-decoration: underline;
}

.post-media {
  position: relative;
  width: 100%;
  border-radius: 8px;
  overflow: hidden;
  background: #0d1117;
  margin-bottom: 12px;
}

/* --- Tap Navigation Zones --- */

.tap-zone {
  position: absolute;
  top: 0;
  bottom: 0;
  z-index: 2;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.nav-row {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}

.tap-zone-prev {
  left: 0;
  width: 20%;
}

.tap-zone-next {
  right: 0;
  width: 20%;
}

.tap-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  font-size: 32px;
  color: rgba(255, 255, 255, 0.8);
  pointer-events: none;
  opacity: 0;
  text-shadow: 0 1px 4px rgba(0,0,0,0.6);
}

.tap-zone-prev .tap-arrow {
  left: 12px;
}

.tap-zone-next .tap-arrow {
  right: 12px;
}

.tap-arrow.flash {
  animation: arrowFlash 0.35s ease-out;
}

@keyframes arrowFlash {
  0%   { opacity: 0.9; transform: translateY(-50%) scale(1.1); }
  100% { opacity: 0;   transform: translateY(-50%) scale(1); }
}


.post-media img,
.post-media video {
  display: block;
  width: 100%;
  height: auto;
}

/* --- Post Info Bar --- */

.post-info {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid #1e2636;
  margin-bottom: 12px;
  font-size: 13px;
  color: #9aa0ab;
}

.info-favs {
  color: #c0392b;
}

.info-file {
  color: #6a6f79;
}

.info-link {
  margin-left: auto;
  color: #4a7cff;
  text-decoration: none;
  font-size: 12px;
}

.info-link:hover {
  text-decoration: underline;
}

/* --- Post Tags --- */

.post-tags {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.tag-group h3 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
  font-weight: 600;
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.tag {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 12px;
  background: #1e2636;
  color: #c0c4cc;
  text-decoration: none;
}

/* Tag category colors */
.tag-group-artist h3 { color: #f2ac08; }
.tag-artist { background: rgba(242, 172, 8, 0.15); color: #f2ac08; }

.tag-group-character h3 { color: #00aa00; }
.tag-character { background: rgba(0, 170, 0, 0.15); color: #00aa00; }

.tag-group-copyright h3 { color: #dd00dd; }
.tag-copyright { background: rgba(221, 0, 221, 0.15); color: #dd00dd; }

.tag-group-species h3 { color: #ed5d1f; }
.tag-species { background: rgba(237, 93, 31, 0.15); color: #ed5d1f; }

.tag-group-general h3 { color: #4a7cff; }
.tag-general { background: rgba(74, 124, 255, 0.15); color: #4a7cff; }

.tag-group-meta h3 { color: #888; }
.tag-meta { background: rgba(136, 136, 136, 0.15); color: #888; }

/* --- Bottom nav mode --- */

body[data-navpos="bottom"] header {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  top: auto;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-top: 1px solid #1e2636;
}

body[data-navpos="bottom"] header .chips {
  order: -1;
  padding: 0;
}

body[data-navpos="bottom"] #grid {
  padding-bottom: 120px;
}

/* --- Right-hand holding mode (controls align right for right thumb) --- */

body[data-hand="right"] .card-info {
  flex-direction: row-reverse;
}

body[data-hand="right"] .chips {
  flex-direction: row-reverse;
}

body[data-hand="right"] .post-info {
  flex-direction: row-reverse;
}

body[data-hand="right"] .post-info .info-link {
  margin-left: 0;
  margin-right: auto;
}

body[data-hand="right"] .tag-group h3 {
  text-align: right;
}

body[data-hand="right"] .tag-list {
  justify-content: flex-end;
}

body[data-hand="right"] .back-link {
  display: block;
  text-align: right;
}

/* --- Settings Page --- */

.settings-page {
  max-width: 500px;
  margin: 0 auto;
  padding: 12px;
}

.settings-page h1 {
  font-size: 20px;
  margin-bottom: 20px;
}

.setting-group {
  margin-bottom: 24px;
}

.setting-group h2 {
  font-size: 14px;
  margin-bottom: 4px;
}

.setting-desc {
  font-size: 12px;
  color: #6a6f79;
  margin-bottom: 10px;
}

.hand-toggle {
  display: flex;
  gap: 8px;
}

.hand-btn {
  flex: 1;
  padding: 10px;
  border: 1px solid #2a2f3a;
  border-radius: 8px;
  background: #141922;
  color: #9aa0ab;
  font-size: 14px;
  cursor: pointer;
}

.hand-btn.active {
  border-color: #4a7cff;
  color: #e0e0e0;
  background: #1a2640;
}

.credentials-form {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.credentials-form label {
  font-size: 12px;
  color: #9aa0ab;
}

.credentials-form input[type="text"],
.credentials-form input[type="password"] {
  padding: 10px;
  border: 1px solid #2a2f3a;
  border-radius: 8px;
  background: #141922;
  color: #e0e0e0;
  font-size: 14px;
  font-family: inherit;
}

.credentials-actions {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}

.credentials-actions button {
  padding: 10px 24px;
}

textarea:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.back-btn {
  display: block;
  width: 100%;
  padding: 14px;
  border: 1px solid #2a2f3a;
  border-radius: 8px;
  background: #141922;
  color: #4a7cff;
  font-size: 15px;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  margin-bottom: 8px;
}

.back-btn:active {
  background: #1e2636;
}

.fav-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 8px;
}

.quality-row {
  display: flex;
  gap: 6px;
}

.quality-row .fav-btn {
  flex: 1;
}

.fav-btn {
  width: 100%;
  padding: 14px;
  border: 1px solid rgba(235, 64, 52, 0.3);
  border-radius: 8px;
  background: #141922;
  color: #eb4034;
  font-size: 16px;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  box-sizing: border-box;
}

.fav-btn.active {
  border-color: #d4a017;
  background: rgba(212, 160, 23, 0.15);
}

.quality-btn {
  color: #d4a017;
  border-color: rgba(212, 160, 23, 0.3);
}

#save-btn {
  color: #27ae60;
  border-color: rgba(39, 174, 96, 0.3);
}

.fav-btn.faved {
  border-color: #eb4034;
  background: rgba(235, 64, 52, 0.15);
}

.fav-btn:disabled {
  cursor: default;
  opacity: 0.7;
}

.unfav-btn {
  padding: 10px;
  font-size: 13px;
  color: #6a6f79;
  border-color: #2a2f3a;
}

.unfav-btn:hover {
  color: #eb4034;
  border-color: rgba(235, 64, 52, 0.3);
}

.nav-btn {
  flex: 1;
  color: #4a7cff;
  border-color: rgba(74, 124, 255, 0.3);
}
