:root {
  --bg-main: #0c0f14;
  --bg-card: #141820;
  --bg-subtle: #1c222e;
  --bg-hover: #242c3b;
  --bg-active: #2b3648;
  --border: #263042;
  --border-focus: #4f8cff;
  
  --text-primary: #f0f4fc;
  --text-secondary: #9daec6;
  --text-muted: #627289;
  
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-light: rgba(59, 130, 246, 0.15);
  
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --deleted-bg: rgba(239, 68, 68, 0.08);
  --deleted-border: rgba(239, 68, 68, 0.3);
  
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
}

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

body {
  background: var(--bg-main);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* Header */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.85rem 1.5rem;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  gap: 1rem;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--accent);
  text-decoration: none;
  cursor: pointer;
}

.logo h1 {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text-primary);
}

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

.import-thread-form {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.import-thread-input {
  position: relative;
  display: flex;
  align-items: center;
}

.import-thread-input input {
  width: 7.5rem;
  background: var(--bg-main);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 0.4rem 0.55rem 0.4rem 1.35rem;
  font-size: 0.85rem;
}

.import-thread-input input:focus {
  outline: none;
  border-color: var(--border-focus);
}

.thread-id-prefix {
  position: absolute;
  left: 0.55rem;
  color: var(--text-muted);
  pointer-events: none;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* Scrape dropdown */
.scrape-dropdown {
  position: relative;
  display: inline-flex;
}

.scrape-dropdown:has(button:disabled) {
  opacity: 0.5;
}

.scrape-dropdown:has(button:disabled) button {
  cursor: not-allowed;
  opacity: 1;
}

.scrape-dropdown .scrape-dropdown-btn {
  border-radius: var(--radius-sm) 0 0 var(--radius-sm);
  width: 13rem;
  justify-content: center;
  white-space: nowrap;
}

.scrape-dropdown .scrape-dropdown-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.6rem;
  margin-left: -1px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  cursor: pointer;
  transition: background 0.15s ease;
}

.scrape-dropdown-toggle:hover:not(:disabled) {
  background: var(--accent-hover);
}

.scrape-dropdown-menu {
  display: none;
  position: absolute;
  top: calc(100% + 0.35rem);
  right: 0;
  min-width: 10rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  z-index: 100;
  overflow: hidden;
}

.scrape-dropdown.open .scrape-dropdown-menu {
  display: block;
}

.scrape-dropdown-item {
  display: block;
  width: 100%;
  padding: 0.5rem 0.75rem;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 0.82rem;
  text-align: left;
  cursor: pointer;
  transition: background 0.1s ease;
}

.scrape-dropdown-item:hover {
  background: var(--bg-hover);
}

.scrape-dropdown-item.active {
  color: var(--accent);
  font-weight: 600;
}

.control-label {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.input-with-unit {
  position: relative;
  display: flex;
  align-items: center;
}

.input-with-unit input {
  background: var(--bg-main);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 0.4rem 2rem 0.4rem 0.65rem;
  width: 5.5rem;
  font-size: 0.85rem;
}

.input-with-unit input:focus {
  outline: none;
  border-color: var(--border-focus);
}

.input-with-unit .unit {
  position: absolute;
  right: 0.5rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  pointer-events: none;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.45rem 0.9rem;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, opacity 0.15s ease;
}

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

.btn-primary {
  background: var(--accent);
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--bg-subtle);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* Stats Overview Bar */
.stats-overview {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.6rem 1.5rem;
  background: rgba(20, 24, 32, 0.7);
  border-bottom: 1px solid var(--border);
  font-size: 0.82rem;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.stat-card {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--bg-subtle);
  padding: 0.3rem 0.7rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.stat-label {
  color: var(--text-muted);
}

.stat-val {
  font-weight: 600;
  color: var(--text-primary);
  font-family: 'JetBrains Mono', monospace;
}

.text-warning {
  color: var(--warning) !important;
}

.stat-status-panel {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: auto;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.status-indicator-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success);
  flex-shrink: 0;
}

.status-indicator-dot.running {
  background: var(--warning);
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(1.2); }
  100% { opacity: 1; transform: scale(1); }
}

/* Main Layout */
.app-main {
  display: grid;
  grid-template-columns: 420px 1fr;
  flex: 1;
  overflow: hidden;
}

/* Sidebar */
.sidebar-panel {
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

.search-box {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  color: var(--text-muted);
}

.search-box input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-size: 0.88rem;
}

.search-box input:focus {
  outline: none;
}

.filter-bar {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.1);
}

.filter-row {
  display: flex;
  width: 100%;
  gap: 0.5rem;
}

.filter-row-import {
  justify-content: flex-end;
}

.filter-bar select {
  flex: 1;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  height: 1.8rem;
  padding: 0.35rem 0.5rem;
  font-size: 0.8rem;
}

.filter-bar select:focus {
  outline: none;
  border-color: var(--border-focus);
}

.filter-bar .tag-import-limit {
  flex: 0 0 5.8rem;
}

.filter-bar .import-tag-btn {
  flex: 0 0 auto;
  height: 1.8rem;
  padding: 0.35rem 0.6rem;
}

.filter-bar .ts-wrapper,
.global-filter-controls .ts-wrapper {
  flex: 1 1 0;
  min-width: 0;
  --ts-pr-min: 2rem;
  color: var(--text-primary);
  font-size: 0.8rem;
}

.filter-bar .ts-control,
.global-filter-controls .ts-control {
  min-height: 1.8rem;
  height: 1.8rem;
  align-items: center;
  padding: 0 0.5rem;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: none;
  color: var(--text-primary);
  font-size: 0.8rem;
  line-height: 1.5;
}

.filter-bar .ts-wrapper.single.input-active .ts-control,
.global-filter-controls .ts-wrapper.single.input-active .ts-control {
  background: var(--bg-subtle);
  color: var(--text-primary);
}

.filter-bar .ts-control > input,
.global-filter-controls .ts-control > input {
  min-width: 0;
  height: 1.25rem;
  line-height: 1.25rem;
  color: var(--text-primary) !important;
  -webkit-text-fill-color: var(--text-primary) !important;
  caret-color: var(--accent);
}

.filter-bar .ts-control .item,
.global-filter-controls .ts-control .item {
  line-height: 1.25rem;
}

.filter-bar .ts-control > input::placeholder,
.global-filter-controls .ts-control > input::placeholder {
  color: var(--text-muted);
  opacity: 1;
}

.filter-bar .ts-wrapper.input-active .ts-control > input,
.global-filter-controls .ts-wrapper.input-active .ts-control > input {
  left: auto;
  opacity: 1;
  position: static;
}

.filter-bar .focus .ts-control,
.global-filter-controls .focus .ts-control,
.filter-bar .dropdown-active .ts-control,
.global-filter-controls .dropdown-active .ts-control {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 2px var(--accent-light);
}

.filter-bar .ts-dropdown,
.global-filter-controls .ts-dropdown {
  z-index: 30;
  margin-top: 0.25rem;
  overflow: hidden;
  background: var(--bg-card);
  border: 1px solid var(--border-focus);
  border-radius: var(--radius-sm);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
  color: var(--text-secondary);
  font-size: 0.8rem;
}

.filter-bar .ts-dropdown-content,
.global-filter-controls .ts-dropdown-content {
  max-height: 19rem;
}

.filter-bar .ts-dropdown .option,
.global-filter-controls .ts-dropdown .option,
.filter-bar .ts-dropdown .no-results,
.global-filter-controls .ts-dropdown .no-results {
  padding: 0.45rem 0.6rem;
}

.filter-bar .ts-dropdown .active,
.global-filter-controls .ts-dropdown .active {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.filter-bar .tag-option-row,
.global-filter-controls .tag-option-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.filter-bar .tag-option-count,
.global-filter-controls .tag-option-count {
  flex-shrink: 0;
  color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.7rem;
}

.filter-bar .ts-dropdown .active .tag-option-count,
.global-filter-controls .ts-dropdown .active .tag-option-count {
  color: var(--text-secondary);
}

.filter-bar .ts-dropdown .no-results,
.global-filter-controls .ts-dropdown .no-results {
  color: var(--text-muted);
}

.filter-bar .ts-control::after,
.global-filter-controls .ts-control::after {
  content: '';
  position: absolute;
  top: 50%;
  right: 0.75rem;
  width: 0;
  height: 0;
  margin-top: -2px;
  border-style: solid;
  border-width: 5px 5px 0;
  border-color: var(--text-muted) transparent transparent;
  pointer-events: none;
}

.filter-bar .dropdown-active .ts-control::after,
.global-filter-controls .dropdown-active .ts-control::after {
  margin-top: -3px;
  border-width: 0 5px 5px;
  border-color: transparent transparent var(--text-muted);
}

.thread-list-container {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.thread-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.thread-item-btn {
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  border-left: 3px solid transparent;
  border-bottom: 1px solid var(--border);
  padding: 0.85rem 1rem;
  color: var(--text-primary);
  cursor: pointer;
  transition: background 0.12s ease;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.thread-item-btn:hover {
  background: var(--bg-subtle);
}

.thread-item-btn.active {
  background: var(--bg-active);
  border-left: 3px solid var(--accent);
}

.item-top-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.5rem;
}

.thread-item-title {
  font-size: 0.9rem;
  font-weight: 500;
  line-height: 1.35;
  color: var(--text-primary);
}

.thread-item-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.76rem;
  color: var(--text-muted);
  flex-wrap: wrap;
}

.tag-pill {
  background: var(--bg-main);
  border: 1px solid var(--border);
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  color: var(--text-secondary);
}

.tag-pill-btn {
  font: inherit;
  cursor: pointer;
  line-height: 1.4;
}

.tag-pill-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.tag-pill-btn.active {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
}

.load-more-wrapper {
  padding: 0.75rem 1rem;
  text-align: center;
}

.load-more-wrapper .btn {
  width: 100%;
}

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.12rem 0.45rem;
  border-radius: 4px;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.badge.db-mode {
  background: var(--accent-light);
  color: var(--accent);
}

.badge.active {
  background: rgba(16, 185, 129, 0.15);
  color: #34d399;
}

.badge.saved {
  background: rgba(59, 130, 246, 0.15);
  color: #60a5fa;
}

.badge.deleted {
  background: rgba(239, 68, 68, 0.15);
  color: #f87171;
}

.badge.op {
  background: rgba(16, 185, 129, 0.15);
  color: #34d399;
}

.badge.sage {
  background: rgba(139, 92, 246, 0.15);
  color: #a78bfa;
}

/* Viewer Panel */
.viewer-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-main);
  position: relative;
}

.thread-viewer {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.placeholder-notice {
  text-align: center;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.placeholder-notice svg {
  stroke: var(--text-muted);
  margin-bottom: 0.5rem;
}

.thread-header-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 1.25rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.thread-sticky-bar {
  position: sticky;
  top: -0.75rem;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.65rem 0.85rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.25);
}

.thread-sticky-summary {
  display: flex;
  align-items: baseline;
  gap: 0.55rem;
  flex-shrink: 0;
}

.thread-sticky-eyebrow {
  color: var(--text-primary);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.thread-sticky-bar .filter-result-count {
  margin-left: 0;
  white-space: nowrap;
}

.thread-sticky-bar .thread-filters-bar {
  flex: 1;
  min-width: 0;
  padding-top: 0;
  border-top: none;
}

.thread-header-card h2 {
  font-size: 1.25rem;
  font-weight: 700;
  line-height: 1.35;
}

.thread-title-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
}

.thread-title-row .item-top-row {
  flex: 1;
  min-width: 0;
}

.thread-update-btn {
  flex-shrink: 0;
  white-space: nowrap;
}

.thread-update-btn.is-loading .btn-icon {
  animation: spin 0.9s linear infinite;
}

.thread-update-btn:focus-visible {
  outline: 2px solid var(--border-focus);
  outline-offset: 2px;
}

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

.thread-header-meta {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.82rem;
  color: var(--text-secondary);
  flex-wrap: wrap;
}

.external-link {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
}

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

.thread-filters-bar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}

.thread-filters-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
}

.filter-toggle-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.3rem 0.65rem;
  font-size: 0.78rem;
  font-weight: 500;
  border-radius: var(--radius-sm);
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s ease;
  user-select: none;
}

.filter-toggle-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--text-muted);
}

.filter-toggle-btn.active {
  background: var(--accent-light);
  border-color: var(--accent);
  color: #93c5fd;
}

.filter-toggle-btn.deleted-filter.active {
  background: var(--deleted-bg);
  border-color: var(--danger);
  color: #f87171;
}

.filter-toggle-btn .filter-badge {
  font-size: 0.72rem;
  padding: 0.05rem 0.35rem;
  border-radius: 10px;
  background: var(--bg-card);
  color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
}

.filter-toggle-btn.active .filter-badge {
  background: rgba(255, 255, 255, 0.15);
  color: var(--text-primary);
}

.filter-reset-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  padding: 0.25rem 0.55rem;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.15s ease;
}

.filter-reset-btn:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
  border-color: var(--text-secondary);
}

.filter-result-count {
  margin-left: auto;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
}

.message-load-btn {
  flex-shrink: 0;
  white-space: nowrap;
}

.message-load-sentinel {
  min-height: 2.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 0.76rem;
  font-family: 'JetBrains Mono', monospace;
}

.message-load-sentinel[hidden] {
  display: none;
}

.message-load-error {
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--deleted-border);
  border-radius: var(--radius-sm);
  color: #f87171;
  font-size: 0.78rem;
}

.scroll-jump-controls {
  position: absolute;
  right: 1rem;
  bottom: 1rem;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.35rem;
  pointer-events: none;
}

.scroll-jump-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  width: 2rem;
  height: 2rem;
  padding: 0;
  justify-content: center;
  color: var(--text-muted);
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 50%;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.22);
  pointer-events: auto;
  cursor: pointer;
  opacity: 0.72;
  transition: background 0.15s ease, color 0.15s ease, opacity 0.15s ease;
}

.scroll-jump-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.scroll-jump-btn:disabled:hover {
  background: var(--bg-subtle);
  color: var(--text-muted);
  opacity: 0.3;
}

.scroll-jump-btn:hover {
  background: var(--bg-hover);
  color: var(--text-secondary);
  opacity: 1;
}

.scroll-jump-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.scroll-jump-icon {
  flex-shrink: 0;
}

.empty-filter-notice {
  text-align: center;
  padding: 3rem 1.5rem;
  background: var(--bg-card);
  border: 1px dashed var(--border);
  border-radius: var(--radius-md);
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}

.empty-filter-notice p {
  font-size: 0.9rem;
}

/* Message Cards */
.message-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  transition: border-color 0.2s ease, background 0.2s ease;
}

.message-card.highlight {
  border-color: var(--accent);
  background: rgba(59, 130, 246, 0.08);
}

.message-card.deleted {
  background: var(--deleted-bg);
  border-color: var(--deleted-border);
}

.message-header-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.82rem;
  color: var(--text-secondary);
  flex-wrap: wrap;
}

.message-index {
  font-weight: 700;
  color: var(--text-primary);
  font-family: 'JetBrains Mono', monospace;
}

.message-id {
  color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
}

.message-author {
  font-weight: 600;
  color: var(--text-primary);
}

.message-time {
  color: var(--text-muted);
  margin-left: auto;
  font-size: 0.78rem;
}

.message-body {
  font-size: 0.92rem;
  line-height: 1.55;
  color: var(--text-primary);
  word-break: break-word;
}

.message-body .unkfunc,
.message-body .quote,
.message-body blockquote {
  color: #86efac;
  display: block;
}

.message-body .reply-ref,
.message-body a.post-reply-link {
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
  cursor: pointer;
}

.message-body .reply-ref:hover,
.message-body a.post-reply-link:hover {
  text-decoration: underline;
}

/* Media Cards Grid */
.media-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 0.25rem;
}

.media-thumbnail-card {
  display: flex;
  flex-direction: column;
  width: 220px;
  height: 220px;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.media-preview {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #000;
  position: relative;
}

.thumb-preview-btn {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  display: block;
  overflow: hidden;
  position: relative;
  line-height: 0;
  width: 100%;
  height: 100%;
}

.thumb-preview-btn img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  transition: transform 0.2s ease;
}

.thumb-preview-btn:hover img {
  transform: scale(1.1);
}

.media-video-player {
  width: 100%;
  height: auto;
  max-height: 100%;
  display: block;
  background: #000;
  border-radius: 0;
  object-fit: contain;
  object-position: center;
}

.media-preview.is-empty {
  display: flex;
  align-items: center;
  justify-content: center;
}

.media-placeholder {
  display: none;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: var(--text-secondary);
  font-size: 0.8rem;
}

.media-preview.is-empty .media-placeholder {
  display: flex;
}

.media-preview.is-empty .thumb-preview-btn,
.media-preview.is-empty img {
  display: none;
}

.media-caption-bar {
  padding: 0.35rem 0.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  background: rgba(0, 0, 0, 0.2);
  flex-shrink: 0;
}

.media-file-link {
  color: var(--text-secondary);
  font-size: 0.75rem;
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.media-file-link:hover {
  color: var(--accent);
  text-decoration: underline;
}

/* Lightbox Modal */
.lightbox-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.lightbox-modal[hidden] {
  display: none;
}

.lightbox-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(4px);
}

.lightbox-dialog {
  position: relative;
  z-index: 1001;
  max-width: 90vw;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.lightbox-close {
  position: absolute;
  top: -2.5rem;
  right: 0;
  background: transparent;
  border: none;
  color: white;
  cursor: pointer;
  opacity: 0.8;
  transition: opacity 0.15s;
}

.lightbox-close:hover {
  opacity: 1;
}

.lightbox-media-container {
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: 90vw;
  max-height: 80vh;
}

.lightbox-media-container img,
.lightbox-media-container video {
  max-width: 90vw;
  max-height: 80vh;
  object-fit: contain;
  border-radius: var(--radius-sm);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.6);
}

.lightbox-image-holder {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: min(45vw, 480px);
  min-height: min(35vh, 360px);
}

.lightbox-image-holder img {
  max-width: 90vw;
  max-height: 80vh;
  object-fit: contain;
  border-radius: var(--radius-sm);
}

.lightbox-preview {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  filter: blur(8px);
  transform: scale(1.04);
  opacity: 0.85;
  box-shadow: none;
}

.lightbox-full {
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.6);
  opacity: 0;
  transition: opacity 0.25s ease;
}

.lightbox-full.is-loaded {
  opacity: 1;
}

.lightbox-spinner {
  position: absolute;
  z-index: 3;
  width: 44px;
  height: 44px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: lightbox-spin 0.8s linear infinite;
}

.lightbox-spinner-behind {
  background: rgba(0, 0, 0, 0.45);
  box-shadow: 0 0 14px rgba(0, 0, 0, 0.45);
}

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

.lightbox-error {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 4;
  max-width: 80%;
  color: #fff;
  background: rgba(180, 32, 32, 0.92);
  padding: 0.6rem 1.1rem;
  border-radius: var(--radius-sm);
  font-size: 0.92rem;
  text-align: center;
}

.lightbox-footer {
  display: flex;
  align-items: center;
  gap: 1rem;
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.lightbox-action-btn {
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
  background: var(--bg-card);
  padding: 0.25rem 0.75rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.lightbox-action-btn:hover {
  background: var(--bg-hover);
}

/* Nav View Switcher Tabs */
.nav-view-tabs {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  background: var(--bg-main);
  padding: 0.25rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  margin-left: 0.5rem;
}

.nav-tab {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-secondary);
  background: transparent;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.nav-tab:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.nav-tab.active {
  background: var(--bg-card);
  color: var(--text-primary);
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

/* Global Messages Main View */
.global-messages-main {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
  background: var(--bg-main);
  position: relative;
}

.global-toolbar {
  display: flex;
  flex-direction: column;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
}

.global-search-box {
  border-bottom: 1px solid var(--border);
  padding: 0.65rem 1.5rem;
}

.global-filter-controls {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 1.5rem;
  background: rgba(0, 0, 0, 0.1);
  flex-wrap: wrap;
}

.global-filter-controls select {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  height: 1.8rem;
  padding: 0.35rem 0.65rem;
  font-size: 0.8rem;
}

.global-filter-controls .ts-wrapper {
  flex: 0 1 15rem;
  width: 15rem;
  max-width: 100%;
}

.global-filter-controls select:focus {
  outline: none;
  border-color: var(--border-focus);
}

.global-messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.global-messages-feed {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  max-width: 960px;
  width: 100%;
  margin: 0 auto;
}

.thread-source-link {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--bg-main);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.15rem 0.5rem;
  font-size: 0.74rem;
  font-weight: 500;
  color: var(--accent);
  cursor: pointer;
  transition: all 0.12s ease;
  max-width: 320px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-source-link:hover {
  background: var(--accent-light);
  border-color: var(--accent);
}

@media (max-width: 900px) {
  .app-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .header-controls {
    width: 100%;
    flex-wrap: wrap;
  }

  .app-main {
    grid-template-columns: 1fr;
  }
  .sidebar-panel {
    max-height: 45vh;
  }
  .global-messages-container {
    padding: 1rem;
  }

  .thread-title-row {
    flex-wrap: wrap;
  }

  .thread-update-btn {
    margin-left: auto;
  }

  .thread-sticky-bar {
    align-items: stretch;
    flex-direction: column;
    gap: 0.55rem;
  }

  .thread-sticky-summary {
    justify-content: space-between;
  }

  .thread-sticky-bar .thread-filters-bar {
    flex: initial;
  }

  .scroll-jump-controls {
    right: 0.85rem;
    bottom: 0.85rem;
  }
}

/* Reply Tooltip */
.reply-tooltip {
  position: fixed;
  z-index: 500;
  max-width: 420px;
  min-width: 240px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  font-size: 0.85rem;
  overflow: clip;
}

.reply-tooltip.hidden {
  display: none;
}

.reply-tooltip-header {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.78rem;
  color: var(--text-secondary);
  background: rgba(0, 0, 0, 0.15);
}

.reply-tooltip-header .tt-id {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  color: var(--accent);
}

.reply-tooltip-header .tt-author {
  font-weight: 600;
  color: var(--text-primary);
}

.reply-tooltip-header .tt-time {
  margin-left: auto;
  color: var(--text-muted);
  font-size: 0.72rem;
}

.reply-tooltip-body {
  padding: 0.6rem 0.75rem;
  max-height: 240px;
  overflow-y: auto;
  line-height: 1.5;
  color: var(--text-primary);
  word-break: break-word;
}

.reply-tooltip-body .unkfunc,
.reply-tooltip-body .quote,
.reply-tooltip-body blockquote {
  color: #86efac;
  display: block;
}

.reply-tooltip-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  color: var(--text-muted);
  font-size: 0.8rem;
  gap: 0.5rem;
}

.reply-tooltip-loading::before {
  content: '';
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: lightbox-spin 0.7s linear infinite;
}

.reply-tooltip-error {
  padding: 0.75rem;
  color: #f87171;
  font-size: 0.8rem;
  text-align: center;
}

.tt-media-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.5rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}

.tt-media {
  max-width: 180px;
  max-height: 140px;
  border-radius: 4px;
  object-fit: contain;
  display: block;
  background: #000;
}

/* Gallery View */
.gallery-main {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
  background: var(--bg-main);
  position: relative;
}

.gallery-container {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.gallery-feed {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
}

.gallery-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.gallery-card:hover {
  border-color: var(--accent);
  box-shadow: 0 2px 12px rgba(59, 130, 246, 0.15);
}

.gallery-card.deleted {
  background: var(--deleted-bg);
  border-color: var(--deleted-border);
}

.gallery-card.deleted:hover {
  border-color: var(--danger);
  box-shadow: 0 2px 12px rgba(239, 68, 68, 0.15);
}

.gallery-card-media {
  aspect-ratio: 1;
  background: var(--bg-subtle);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}

.gallery-card-media.is-empty {
  align-items: center;
  justify-content: center;
}

.gallery-card-media.is-empty::after {
  content: 'No preview';
  font-size: 0.75rem;
  color: var(--text-muted);
}

.gallery-card-media img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: var(--bg-subtle);
  padding: 0.5rem;
  transition: transform 0.2s ease;
}

.gallery-card:hover .gallery-card-media img {
  transform: scale(1.03);
}

.gallery-card-media video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: var(--bg-subtle);
}

.gallery-video {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.gallery-thumb-btn {
  display: flex;
  width: 100%;
  height: 100%;
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 0;
}

.gallery-card-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.5rem 0.65rem;
  min-height: 2.2rem;
}

.gallery-card-filename {
  font-size: 0.75rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

.gallery-card-thread {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--accent);
  background: var(--bg-main);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.1rem 0.4rem;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.12s ease;
}

.gallery-card-thread:hover {
  background: var(--accent-light);
  border-color: var(--accent);
}

@media (max-width: 900px) {
  .gallery-container {
    padding: 1rem;
  }

  .gallery-feed {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 0.75rem;
  }
}
