* {
  box-sizing: border-box;
}

:root {
  --bg: #1b1b1e;
  --bg-elevated: #2a2a2e;
  --bg-hover: #34343a;
  --fg: #e6e6e6;
  --fg-muted: #999;
  --accent: #4a9eff;
  --accent-hover: #6bb0ff;
  --border: #3a3a40;
  --error: #ff6b6b;
  --chip-embedded-bg: #3a3a40;
  --chip-embedded-fg: #fff;
  --chip-suggested-bg: #1e3a5c;
  --chip-suggested-fg: #fff;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  min-height: 100vh;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  color: var(--accent-hover);
}

.loading {
  padding: 2rem;
  text-align: center;
  color: var(--fg-muted);
}

.error {
  padding: 0.75rem;
  background: rgba(255, 107, 107, 0.1);
  border: 1px solid var(--error);
  border-radius: 4px;
  color: var(--error);
  margin: 0.5rem 0;
  font-size: 0.9rem;
}

.info {
  padding: 0.75rem;
  background: rgba(74, 158, 255, 0.1);
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--accent-hover);
  margin: 0.5rem 0;
  font-size: 0.9rem;
}

.link-btn {
  background: none;
  border: none;
  color: var(--fg-muted);
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0;
  align-self: center;
  text-decoration: underline;
}

.link-btn:hover:not(:disabled) {
  color: var(--accent);
}

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

.account-page {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  max-width: 480px;
  margin: 0 auto;
}

.account-card h2 {
  margin: 0;
  font-size: 1.4rem;
}

.account-card h3 {
  margin: 0.5rem 0 0 0;
  font-size: 1rem;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.account-hint {
  margin: -0.25rem 0 0 0;
  font-size: 0.85rem;
}

/* ---------- Comments ---------- */

.detail-page {
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.comments-section {
  max-width: 720px;
  margin: 0 auto;
  width: 100%;
}

.comments-section h3 {
  margin: 0 0 0.75rem 0;
  font-size: 1rem;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.comment-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.comment-thread {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.comment {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.6rem 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.comment-reply {
  margin-left: 1.5rem;
  background: transparent;
  border-left: 2px solid var(--border);
  border-top: none;
  border-right: none;
  border-bottom: none;
  border-radius: 0;
  padding: 0.4rem 0.8rem;
}

.comment-meta {
  display: flex;
  gap: 0.5rem;
  align-items: baseline;
  font-size: 0.85rem;
}

.comment-author {
  font-weight: 600;
  color: var(--fg);
}

.comment-time {
  color: var(--fg-muted);
  font-size: 0.75rem;
}

.comment-body {
  white-space: pre-wrap;
  word-wrap: break-word;
}

.comment-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 0.1rem;
}

.comment-actions .link-btn {
  font-size: 0.8rem;
  align-self: auto;
}

.comment-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.comment-form textarea {
  padding: 0.5rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--fg);
  font: inherit;
  font-size: 0.95rem;
  resize: vertical;
  min-height: 2.5rem;
}

.comment-form textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.comment-form-actions {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}

.comment-form-actions button[type="submit"] {
  padding: 0.4rem 1rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
}

.comment-form-actions button[type="submit"]:hover:not(:disabled) {
  background: var(--accent-hover);
}

.comment-form-actions button[type="submit"]:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.comment-reply-form {
  margin-left: 1.5rem;
  margin-top: 0.25rem;
}

.comment-new {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.muted {
  color: var(--fg-muted);
}

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

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  font-weight: 600;
  font-size: 1.1rem;
  color: var(--fg);
}

.brand:hover {
  color: var(--accent);
}

.nav {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.nav-link {
  font-size: 0.9rem;
  color: var(--fg-muted);
}

.nav-link:hover {
  color: var(--fg);
}

.logout {
  background: none;
  border: none;
  cursor: pointer;
  font: inherit;
  padding: 0;
}

.main {
  padding: 1.5rem;
  max-width: 1400px;
  margin: 0 auto;
}

/* ---------- Login ---------- */

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.login-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2rem;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.login-card h1 {
  margin: 0;
  font-size: 1.5rem;
}

.login-card .subtitle {
  margin: 0;
  color: var(--fg-muted);
  font-size: 0.9rem;
}

.login-card label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.85rem;
  color: var(--fg-muted);
}

.login-card input {
  padding: 0.6rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--fg);
  font-size: 1rem;
}

.login-card input:focus {
  outline: none;
  border-color: var(--accent);
}

.login-card button,
.add-tag button,
.load-more button {
  padding: 0.6rem 1rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
}

.login-card button:hover:not(:disabled),
.add-tag button:hover:not(:disabled),
.load-more button:hover:not(:disabled) {
  background: var(--accent-hover);
}

.login-card button:disabled,
.add-tag button:disabled,
.load-more button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Gallery ---------- */

.gallery-layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 2rem;
}

@media (max-width: 480px) {
  .header {
    padding: 0.75rem 1rem;
  }
  .nav {
    gap: 0.5rem;
  }
  .nav-link {
    font-size: 0.85rem;
  }
  .main {
    padding: 1rem;
  }
}

.tag-sidebar {
  position: sticky;
  top: 5rem;
  align-self: start;
}

.tag-sidebar h2 {
  margin: 0 0 0.75rem 0;
  font-size: 1rem;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.tag-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  max-height: 60vh;
  overflow-y: auto;
}

.tag-list a {
  display: flex;
  justify-content: space-between;
  padding: 0.4rem 0.6rem;
  border-radius: 4px;
  font-size: 0.9rem;
}

.tag-list a:hover {
  background: var(--bg-hover);
}

.tag-list .count {
  color: var(--fg-muted);
  font-size: 0.8rem;
}

@media (max-width: 900px) {
  .gallery-layout {
    grid-template-columns: 1fr;
  }
  .tag-sidebar {
    position: static;
    top: auto;
    align-self: auto;
    margin-bottom: 0.5rem;
  }
  .tag-sidebar h2 {
    margin-bottom: 0.5rem;
  }
  .tag-list {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 0.4rem;
    max-height: none;
    overflow: visible;
  }
  .tag-list a {
    display: inline-flex;
    justify-content: flex-start;
    gap: 0.35rem;
    padding: 0.3rem 0.7rem;
    background: var(--bg-elevated);
    border-radius: 999px;
    font-size: 0.85rem;
    white-space: nowrap;
  }
  .tag-list a:hover {
    background: var(--bg-hover);
  }
  .thumb-tag {
    font-size: 0.75rem;
    padding: 0.2rem 0.6rem;
  }
}

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

.thumb-card {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.thumb {
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
  background: var(--bg-elevated);
  border-radius: 4px;
  display: block;
}

.comment-pip {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  line-height: 1.2;
  pointer-events: none;
  z-index: 1;
}

.thumb-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.thumb-tag {
  font-size: 0.7rem;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  background: var(--bg-elevated);
  color: #fff;
  white-space: nowrap;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thumb-tag:hover {
  background: var(--bg-hover);
  color: #fff;
}

.thumb-tag-more {
  background: transparent;
  color: var(--fg-muted);
  font-style: italic;
}

.thumb-tag-more:hover {
  color: var(--fg);
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.2s;
}

.thumb:hover img {
  transform: scale(1.04);
}

.thumb-placeholder {
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, var(--bg-elevated), var(--bg-hover), var(--bg-elevated));
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.load-more {
  text-align: center;
  margin: 2rem 0;
}

/* ---------- Image Detail ---------- */

.detail {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 2rem;
}

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

.detail-image {
  background: var(--bg-elevated);
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 400px;
}

.detail-image img {
  width: 100%;
  height: auto;
  display: block;
  max-height: 80vh;
  object-fit: contain;
}

.detail-meta {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.back-link {
  font-size: 0.9rem;
  color: var(--fg-muted);
}

.back-link:hover {
  color: var(--fg);
}

.detail-meta h2 {
  margin: 0;
  font-size: 1.2rem;
}

.tag-section h3 {
  margin: 0 0 0.5rem 0;
  font-size: 0.8rem;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.tag-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.chip {
  padding: 0.25rem 0.7rem;
  border-radius: 999px;
  font-size: 0.85rem;
}

.chip-embedded {
  background: var(--chip-embedded-bg);
  color: var(--chip-embedded-fg);
}

.chip-embedded:hover {
  background: var(--bg-hover);
}

.chip-suggested {
  background: var(--chip-suggested-bg);
  color: var(--chip-suggested-fg);
}

.chip-suggested:hover {
  background: #2a4a6e;
}

.add-tag {
  display: flex;
  gap: 0.5rem;
}

.add-tag input {
  flex: 1;
  padding: 0.5rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--fg);
  font-size: 0.95rem;
}

.add-tag input:focus {
  outline: none;
  border-color: var(--accent);
}

.add-tag button {
  padding: 0.5rem 1rem;
  font-size: 0.95rem;
}

/* ---------- Tag View ---------- */

.image-id {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  color: var(--fg-muted);
  padding: 0.4rem 0.6rem;
  background: var(--bg-elevated);
  border-radius: 4px;
}

.image-id code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  color: var(--fg);
  word-break: break-all;
  flex: 1;
}

.copy-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--fg-muted);
  font-size: 0.7rem;
  padding: 0.15rem 0.4rem;
  border-radius: 3px;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.copy-btn:hover {
  color: var(--fg);
  border-color: var(--accent);
}

.admin-actions {
  display: flex;
  gap: 0.5rem;
}

.admin-actions button {
  flex: 1;
  padding: 0.45rem 0.6rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--fg);
  font-size: 0.9rem;
  border-radius: 4px;
  cursor: pointer;
}

.admin-actions button:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: var(--accent);
}

.admin-actions button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.tag-view h2 {
  margin: 0.5rem 0 0.25rem 0;
}

.tag-view .muted {
  margin: 0 0 1rem 0;
  font-size: 0.9rem;
}
