:root {
  --bg0: #0f1714;
  --bg1: #16211c;
  --bg2: #1d2b24;
  --panel: rgba(236, 244, 238, 0.94);
  --panel-soft: rgba(236, 244, 238, 0.08);
  --ink: #132019;
  --ink-muted: #4d6358;
  --line: rgba(19, 32, 25, 0.12);
  --accent: #1f8a5b;
  --accent-2: #c8f0d8;
  --warn: #b45309;
  --danger: #b42318;
  --ok: #067647;
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
  --radius: 18px;
  --font-ui: "Outfit", sans-serif;
  --font-display: "Fraunces", Georgia, serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: var(--font-ui);
  color: var(--ink);
  background:
    radial-gradient(1200px 700px at 10% -10%, rgba(31, 138, 91, 0.28), transparent 55%),
    radial-gradient(900px 500px at 100% 0%, rgba(200, 240, 216, 0.12), transparent 45%),
    linear-gradient(160deg, var(--bg0), var(--bg1) 45%, #101a16);
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: white;
  padding: 0.75rem 1.15rem;
  font-weight: 600;
}

button.ghost {
  background: transparent;
  color: inherit;
  border: 1px solid var(--line);
}

button.danger {
  color: var(--danger);
  border-color: rgba(180, 35, 24, 0.35);
}

button.danger:hover {
  background: rgba(180, 35, 24, 0.08);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.hidden {
  display: none !important;
}

.brand {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.35rem;
  letter-spacing: -0.02em;
}

.lede,
.user-email,
.results-meta,
.meta,
.empty-state {
  color: var(--ink-muted);
}

.error {
  color: var(--danger);
  margin: 0.5rem 0 0;
}

.success {
  color: var(--ok);
  margin: 0.5rem 0 0;
}

.tabs {
  display: inline-flex;
  gap: 0.35rem;
  padding: 0.25rem;
  border-radius: 999px;
  background: rgba(232, 243, 236, 0.08);
  border: 1px solid rgba(232, 243, 236, 0.14);
}

.tab {
  background: transparent;
  color: #d7ebe0;
  padding: 0.55rem 0.95rem;
}

.tab.active {
  background: var(--accent);
  color: white;
}

.tab-panel {
  display: grid;
  gap: 1rem;
}

.sites-layout {
  display: grid;
  grid-template-columns: minmax(260px, 340px) 1fr;
  gap: 1rem;
  min-height: 0;
}

.sites-list-pane,
.sites-editor-pane {
  min-height: 560px;
  padding: 1.15rem;
}

.pane-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.pane-toolbar-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.import-panel {
  display: grid;
  gap: 0.85rem;
  margin-bottom: 1.25rem;
  padding-bottom: 1.25rem;
  border-bottom: 1px solid var(--line);
}

.import-panel.hidden {
  display: none !important;
}

.import-help {
  margin: 0;
  color: var(--ink-muted);
  font-size: 0.92rem;
  line-height: 1.45;
}

.import-help code {
  font-size: 0.85em;
}

#import-json {
  width: 100%;
  min-height: 280px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.85rem;
  background: #0f1714;
  color: #e8f3ec;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85rem;
  line-height: 1.45;
  resize: vertical;
}

.pane-toolbar h2,
.pane-toolbar h3 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.25rem;
}

.sites-list {
  display: grid;
  gap: 0.5rem;
  overflow: auto;
  max-height: calc(100% - 3.5rem);
}

.site-item {
  width: 100%;
  text-align: left;
  border-radius: 14px;
  background: transparent;
  color: inherit;
  padding: 0.9rem;
  border: 1px solid transparent;
  display: grid;
  gap: 0.35rem;
}

.site-item:hover,
.site-item.active {
  background: #edf7f1;
  border-color: rgba(31, 138, 91, 0.25);
}

.site-item h3 {
  margin: 0;
  font-size: 1rem;
}

.site-item .url {
  margin: 0;
  font-size: 0.82rem;
  color: var(--ink-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.site-item .last-scraped {
  margin: 0;
  font-size: 0.8rem;
  color: var(--ink-muted);
}

.site-form,
.section-form {
  display: grid;
  gap: 1rem;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.85rem;
}

.form-grid label,
.section-form label {
  display: grid;
  gap: 0.35rem;
  font-size: 0.92rem;
  font-weight: 500;
}

.form-grid label.full {
  grid-column: 1 / -1;
}

.form-grid input,
.section-form input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.75rem 0.85rem;
  background: white;
  color: var(--ink);
}

.form-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.switch-label {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-weight: 500;
  color: var(--ink-muted);
}

.sections-panel {
  margin-top: 1.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--line);
}

.section-form {
  grid-template-columns: 1fr 1.4fr auto;
  align-items: end;
}

.sections-list {
  display: grid;
  gap: 0.55rem;
  margin-top: 0.75rem;
}

.section-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.75rem;
  align-items: center;
  padding: 0.75rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #f7fbf8;
}

.section-row strong {
  display: block;
}

.section-row span {
  font-size: 0.82rem;
  color: var(--ink-muted);
  word-break: break-all;
}

.section-row .row-actions {
  display: flex;
  gap: 0.4rem;
}

.section-row button {
  padding: 0.45rem 0.75rem;
  font-size: 0.85rem;
}

.crawl-test-panel {
  margin-top: 1.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--line);
}

.crawl-test-panel.hidden {
  display: none !important;
}

.crawl-test-block {
  display: grid;
  gap: 0.65rem;
  margin-bottom: 1.25rem;
}

.crawl-test-block h4 {
  margin: 0;
  font-size: 0.95rem;
  color: var(--ink-muted);
}

.crawl-test-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.65rem;
  align-items: end;
}

.crawl-test-form label {
  display: grid;
  gap: 0.35rem;
  font-size: 0.92rem;
  font-weight: 500;
}

.crawl-test-form input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.75rem 0.85rem;
  background: white;
  color: var(--ink);
}

.crawl-test-result {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #f7fbf8;
  padding: 0.85rem;
  max-height: 320px;
  overflow: auto;
}

.crawl-test-result.hidden {
  display: none !important;
}

.crawl-test-result ul {
  margin: 0.5rem 0 0;
  padding-left: 0;
  list-style: none;
}

.crawl-test-result li {
  margin-bottom: 0.35rem;
  word-break: break-all;
  font-size: 0.88rem;
}

.crawl-test-url-list li {
  display: flex;
  align-items: flex-start;
  gap: 0.45rem;
}

.crawl-url-pick {
  flex: 0 0 auto;
  width: 1.7rem;
  height: 1.7rem;
  padding: 0;
  border-radius: 6px;
  font-size: 1.05rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}

.crawl-url-text {
  flex: 1 1 auto;
  min-width: 0;
  padding-top: 0.2rem;
}

.crawl-test-result .article-preview img {
  max-width: 100%;
  max-height: 180px;
  border-radius: 10px;
  margin: 0.5rem 0;
}

.crawl-test-result .article-preview .body {
  white-space: pre-wrap;
  font-size: 0.9rem;
  line-height: 1.55;
  margin-top: 0.5rem;
}

#test-rewrite-form.test-rewrite-form {
  grid-template-columns: 1fr;
  align-items: stretch;
}

.test-rewrite-prompt-label textarea {
  width: 100%;
  min-height: 220px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.82rem;
  line-height: 1.45;
  resize: vertical;
}

.test-rewrite-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  justify-content: flex-end;
}

#test-rewrite-status {
  margin: 0.4rem 0 0;
}

/* Login */
.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.login-panel {
  width: min(420px, 100%);
  background: var(--panel);
  border-radius: 28px;
  padding: 2rem;
  box-shadow: var(--shadow);
}

.login-panel h1 {
  margin: 0.4rem 0 0.35rem;
  font-family: var(--font-display);
  font-size: 2rem;
}

.login-panel form {
  display: grid;
  gap: 0.9rem;
  margin-top: 1.4rem;
}

.login-panel label {
  display: grid;
  gap: 0.35rem;
  font-size: 0.92rem;
  font-weight: 500;
}

.login-panel input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.75rem 0.85rem;
  background: white;
  color: var(--ink);
}

/* Main */
.main-view {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 1rem;
  padding: 1.25rem;
  color: #e8f3ec;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 1rem;
}

.topbar h1 {
  margin: 0.2rem 0 0;
  font-family: var(--font-display);
  font-size: clamp(1.6rem, 3vw, 2.2rem);
}

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

.topbar .ghost,
.pager .ghost {
  color: #e8f3ec;
  border-color: rgba(232, 243, 236, 0.25);
}

.filters {
  display: grid;
  gap: 0.65rem;
  background: var(--panel-soft);
  border: 1px solid rgba(232, 243, 236, 0.12);
  border-radius: var(--radius);
  padding: 0.75rem 0.9rem;
}

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

.period-chip {
  background: rgba(232, 243, 236, 0.08);
  color: #d7ebe0;
  border: 1px solid rgba(232, 243, 236, 0.18);
  padding: 0.35rem 0.7rem;
  font-size: 0.82rem;
  font-weight: 500;
}

.period-chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.filters-row {
  display: flex;
  flex-wrap: nowrap;
  align-items: end;
  gap: 0.5rem;
  overflow-x: auto;
}

.filters-row label {
  display: grid;
  gap: 0.2rem;
  font-size: 0.75rem;
  font-weight: 500;
  color: #d7ebe0;
  min-width: 0;
}

.filters-row input,
.filters-row select {
  width: 100%;
  border: 1px solid rgba(232, 243, 236, 0.2);
  border-radius: 8px;
  padding: 0.35rem 0.45rem;
  background: white;
  color: var(--ink);
  font-size: 0.8rem;
  height: 2rem;
  line-height: 1.2;
}

.filters-row .filter-compact {
  flex: 0 0 auto;
}

.filters-row .filter-compact input[type="date"] {
  width: 8rem;
}

.filters-row .filter-compact select {
  width: 7.8rem;
}

.filters-row .filter-search {
  flex: 1 1 160px;
  min-width: 140px;
}

.results-meta {
  margin: 0;
  align-self: end;
  white-space: nowrap;
  font-size: 0.8rem;
  padding-bottom: 0.35rem;
  flex: 0 0 auto;
}

.content {
  display: grid;
  grid-template-columns: minmax(280px, 380px) 1fr;
  gap: 1rem;
  min-height: 0;
  align-items: start;
}

.list-pane,
.detail-pane {
  background: var(--panel);
  color: var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-height: 520px;
}

.list-pane {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#articles-tab .list-pane {
  position: sticky;
  top: 1rem;
  max-height: calc(100dvh - 2rem);
  align-self: start;
}

#articles-tab .detail-pane {
  overflow: visible;
  max-height: none;
}

.articles-list {
  flex: 1;
  overflow: auto;
  padding: 0.5rem;
}

.article-item {
  width: 100%;
  text-align: left;
  border-radius: 14px;
  background: transparent;
  color: inherit;
  padding: 0.9rem;
  border: 1px solid transparent;
  display: grid;
  gap: 0.35rem;
}

.article-item:hover,
.article-item.active {
  background: #edf7f1;
  border-color: rgba(31, 138, 91, 0.25);
}

.article-item h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.35;
}

.article-item .preview {
  margin: 0;
  font-size: 0.88rem;
  color: var(--ink-muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.meta-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
  font-size: 0.78rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  font-weight: 600;
  background: #e8efe9;
}

.badge.done {
  background: #dcfce7;
  color: var(--ok);
}

.badge.failed {
  background: #fee4e2;
  color: var(--danger);
}

.badge.sync-awaiting_rewrite {
  background: #e8efe9;
  color: var(--ink-muted);
}

.badge.sync-ready {
  background: #dbeafe;
  color: #1d4ed8;
}

.badge.sync-synced {
  background: #dcfce7;
  color: var(--ok);
}

.badge.sync-duplicate {
  background: #fee4e2;
  color: var(--danger);
}

.badge.category {
  background: #ede9fe;
  color: #5b21b6;
}

.category-picker {
  display: grid;
  gap: 0.35rem;
  font-size: 0.92rem;
  font-weight: 500;
  color: var(--ink-muted);
  max-width: 320px;
}

.category-picker select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.65rem 0.75rem;
  background: white;
  color: var(--ink);
}

.sync-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-top: 0.35rem;
}

.sync-actions .ghost {
  color: var(--ink);
  border-color: var(--line);
  padding: 0.45rem 0.8rem;
  font-size: 0.85rem;
}

.pager {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--line);
}

.pager .ghost {
  color: var(--ink);
  border-color: var(--line);
}

.detail-pane {
  padding: 1.35rem;
  overflow: auto;
}

.detail-pane.empty {
  display: grid;
  place-items: center;
}

.detail-header {
  display: grid;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.detail-header img {
  width: 100%;
  max-height: 280px;
  object-fit: cover;
  border-radius: 16px;
}

.detail-header h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(1.4rem, 2.4vw, 2rem);
  line-height: 1.2;
}

.detail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.detail-actions a {
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
}

.compare {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.compare section {
  border-top: 1px solid var(--line);
  padding-top: 1rem;
}

.compare h3 {
  margin: 0 0 0.75rem;
  font-size: 0.95rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-muted);
}

.compare-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.compare-heading h3 {
  margin: 0;
}

.rewritten-edit {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.rewritten-edit label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--ink-muted);
}

.rewritten-edit input,
.rewritten-edit textarea {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--bg, #fff);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.55rem 0.7rem;
}

.rewritten-edit textarea {
  resize: vertical;
  min-height: 8rem;
  white-space: pre-wrap;
  line-height: 1.55;
}

.ai-rewrite-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.75rem;
}

.ai-rewrite-model {
  flex: 1 1 14rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--ink-muted);
}

.ai-rewrite-model select {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--bg, #fff);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.55rem 0.7rem;
}

.ai-rewrite-bar button[data-action="ai-rewrite"] {
  flex: 0 0 auto;
}

.compare .body {
  white-space: pre-wrap;
  line-height: 1.65;
  font-size: 0.98rem;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(18, 18, 18, 0.55);
}

.modal-panel {
  position: relative;
  width: min(520px, 100%);
  max-height: min(90vh, 720px);
  overflow: auto;
  background: var(--surface, #fff);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.22);
  padding: 1.1rem 1.2rem 1.25rem;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.modal-header h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.25rem;
}

.modal-form {
  display: grid;
  gap: 0.85rem;
}

.modal-form label {
  display: grid;
  gap: 0.35rem;
  font-size: 0.9rem;
  color: var(--ink-muted);
}

.modal-form input {
  width: 100%;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  margin-top: 0.35rem;
}

.success {
  color: #1f7a3f;
  margin: 0;
}

.detail-actions button[data-action="push-article"] {
  background: var(--accent);
  color: #fff;
  border: none;
}

.queues-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.queues-toolbar-actions {
  display: flex;
  gap: 0.55rem;
  flex-wrap: wrap;
}

.queues-toolbar .ghost {
  color: #e8f3ec;
  border-color: rgba(232, 243, 236, 0.25);
}

.queues-dashboard {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
}

.queue-card {
  background: var(--panel);
  color: var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.15rem;
}

.queue-card header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.9rem;
}

.queue-card h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.15rem;
}

.queue-stat-list {
  display: grid;
  gap: 0.55rem;
}

.queue-stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 0.65rem;
  border-radius: 12px;
  background: #f7fbf8;
  border: 1px solid var(--line);
}

.badge.waiting,
.badge.delayed,
.badge.paused,
.badge.queued {
  background: #e0f2fe;
  color: #0369a1;
}

.badge.active,
.badge.completed {
  background: #dcfce7;
  color: var(--ok);
}

@media (max-width: 980px) {
  .filters-row {
    flex-wrap: wrap;
  }

  .content,
  .sites-layout,
  .queues-dashboard {
    grid-template-columns: 1fr;
  }

  .compare {
    grid-template-columns: 1fr;
  }

  .list-pane {
    min-height: 360px;
    max-height: 420px;
  }

  #articles-tab .list-pane {
    position: static;
    max-height: 420px;
  }

  .form-grid,
  .section-form,
  .crawl-test-form {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .topbar {
    flex-direction: column;
    align-items: start;
  }

  .topbar-actions {
    flex-wrap: wrap;
  }

  .filters-row .filter-search {
    flex-basis: 100%;
  }
}
