:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-hover: #f1f5f9;
  --border: #e2e8f0;
  --border-light: #f1f5f9;
  --text: #1e293b;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --primary-light: #eef2ff;
  --primary-border: #c7d2fe;
  --success: #10b981;
  --success-light: #ecfdf5;
  --success-border: #a7f3d0;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --danger-light: #fef2f2;
  --danger-border: #fecaca;
  --warning-light: #fffbeb;
  --warning-border: #fde68a;
  --warning-text: #92400e;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 6px rgba(0,0,0,.05), 0 2px 4px rgba(0,0,0,.04);
  --shadow-lg: 0 10px 25px rgba(0,0,0,.08), 0 4px 10px rgba(0,0,0,.04);
  --sidebar-width: 220px;
  --transition: .15s ease;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

/* Layout */
.app {
  display: flex;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 12px;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  z-index: 10;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px 20px;
  border-bottom: 1px solid var(--border-light);
  margin-bottom: 16px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--primary);
}

.health-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: background var(--transition);
  flex-shrink: 0;
}
.health-dot.ok { background: var(--success); box-shadow: 0 0 6px rgba(16,185,129,.4); }
.health-dot.err { background: var(--danger); }

.nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.nav-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: .9rem;
  color: var(--text-secondary);
  transition: all var(--transition);
  text-align: left;
  font-family: inherit;
}

.nav-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.nav-btn.active {
  background: var(--primary-light);
  color: var(--primary);
  font-weight: 600;
}

.nav-btn svg { flex-shrink: 0; }

/* Main */
.main {
  flex: 1;
  margin-left: var(--sidebar-width);
  padding: 32px 40px;
  max-width: 960px;
}

#tab-content h2 {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 6px;
  color: var(--text);
}

.page-desc {
  color: var(--text-secondary);
  font-size: .9rem;
  margin-bottom: 28px;
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: var(--shadow);
}

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

.card-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}

/* Forms */
.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: .82rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: .03em;
}

.form-hint {
  font-size: .78rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.form-row {
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.form-row .form-group {
  flex: 1;
  margin-bottom: 0;
}

input[type="text"],
input[type="number"],
textarea,
select {
  width: 100%;
  padding: 9px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: .9rem;
  font-family: inherit;
  color: var(--text);
  background: var(--surface);
  transition: border-color var(--transition), box-shadow var(--transition);
}

input::placeholder, textarea::placeholder { color: var(--text-muted); }

input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99,102,241,.12);
}

textarea { resize: vertical; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 18px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: .88rem;
  font-weight: 500;
  font-family: inherit;
  transition: all var(--transition);
  white-space: nowrap;
}

.btn:hover { background: var(--primary-hover); transform: translateY(-1px); box-shadow: var(--shadow); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }

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

.btn-danger { background: var(--danger); }
.btn-danger:hover { background: var(--danger-hover); }

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  padding: 6px 10px;
}
.btn-ghost:hover { background: var(--surface-hover); color: var(--text); transform: none; box-shadow: none; }

.btn-sm {
  padding: 5px 12px;
  font-size: .8rem;
}

.btn-icon {
  padding: 6px;
  background: transparent;
  color: var(--text-muted);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  transition: all var(--transition);
}
.btn-icon:hover { background: var(--surface-hover); color: var(--text); }

.btn-group {
  display: flex;
  gap: 8px;
}

/* Tables */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: .88rem;
}

th, td {
  padding: 10px 14px;
  text-align: left;
}

thead th {
  font-size: .78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

tbody tr {
  border-bottom: 1px solid var(--border-light);
  transition: background var(--transition);
}
tbody tr:hover { background: var(--surface-hover); }
tbody tr:last-child { border-bottom: none; }

.uuid {
  font-family: "SF Mono", Monaco, "Cascadia Code", Consolas, monospace;
  font-size: .8rem;
  color: var(--text-muted);
  cursor: pointer;
}
.uuid:hover { color: var(--primary); }
.uuid-inline {
  font-family: "SF Mono", Monaco, "Cascadia Code", Consolas, monospace;
  font-size: .75rem;
  color: var(--text-muted);
  cursor: pointer;
  transition: color var(--transition);
}
.uuid-inline:hover { color: var(--primary); }

/* Badges */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: .75rem;
  font-weight: 600;
  letter-spacing: .02em;
}

.badge-ready { background: var(--success-light); color: #059669; border: 1px solid var(--success-border); }
.badge-pending { background: var(--warning-light); color: var(--warning-text); border: 1px solid var(--warning-border); }
.badge-error { background: var(--danger-light); color: #dc2626; border: 1px solid var(--danger-border); }

/* Result displays */
.result {
  margin-top: 14px;
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  font-family: "SF Mono", Monaco, "Cascadia Code", Consolas, monospace;
  font-size: .82rem;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 400px;
  overflow-y: auto;
  display: none;
}

.result.success {
  display: block;
  background: var(--success-light);
  border: 1px solid var(--success-border);
  color: #065f46;
}

.result.error {
  display: block;
  background: var(--danger-light);
  border: 1px solid var(--danger-border);
  color: #991b1b;
}

/* Detection result cards */
.detect-result-card {
  border-radius: var(--radius);
  padding: 20px;
  margin-top: 16px;
  display: flex;
  align-items: flex-start;
  gap: 16px;
  animation: slideIn .25s ease;
}

.detect-result-card.on-topic {
  background: var(--success-light);
  border: 1px solid var(--success-border);
}

.detect-result-card.off-topic {
  background: var(--danger-light);
  border: 1px solid var(--danger-border);
}

.detect-icon {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 1.2rem;
}

.on-topic .detect-icon { background: var(--success-border); }
.off-topic .detect-icon { background: var(--danger-border); }

.detect-info { flex: 1; }
.detect-label { font-size: 1rem; font-weight: 700; margin-bottom: 4px; }
.on-topic .detect-label { color: #065f46; }
.off-topic .detect-label { color: #991b1b; }

.detect-meta {
  font-size: .82rem;
  color: var(--text-secondary);
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-top: 6px;
}

.detect-meta span { display: flex; align-items: center; gap: 4px; }

/* Classify result cards */
.classify-result-card {
  background: var(--primary-light);
  border: 1px solid var(--primary-border);
}

.classify-icon {
  background: var(--primary-border);
  font-size: 1.1rem;
}

.classify-label {
  color: var(--primary) !important;
}

.classify-batch-item {
  border-color: var(--primary-border) !important;
  background: var(--primary-light) !important;
}

.classify-batch-status {
  color: var(--primary) !important;
}

/* Keyword match result cards */
.kw-result-card {
  background: var(--warning-light);
  border: 1px solid var(--warning-border);
}

.kw-result-card.kw-miss {
  background: var(--surface-hover);
  border: 1px solid var(--border);
}

.kw-icon {
  background: var(--warning-border);
}

.kw-miss .kw-icon {
  background: var(--border);
}

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

.kw-miss .kw-label {
  color: var(--text-muted) !important;
}

.kw-batch-item.kw-hit {
  border-color: var(--warning-border) !important;
  background: var(--warning-light) !important;
}

.kw-batch-item.kw-miss {
  border-color: var(--border) !important;
  background: var(--surface) !important;
}

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

.kw-miss .kw-batch-status {
  color: var(--text-muted) !important;
}

/* Batch results */
.batch-results { margin-top: 16px; display: flex; flex-direction: column; gap: 10px; }

.batch-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 18px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  animation: slideIn .25s ease;
}

.batch-item.off-topic { border-color: var(--danger-border); background: var(--danger-light); }
.batch-item.on-topic { border-color: var(--success-border); background: var(--success-light); }

.batch-item-status {
  font-size: .78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  white-space: nowrap;
}
.batch-item.on-topic .batch-item-status { color: #059669; }
.batch-item.off-topic .batch-item-status { color: #dc2626; }

.batch-item-text { flex: 1; font-size: .88rem; color: var(--text); }
.batch-item-detail { font-size: .78rem; color: var(--text-muted); white-space: nowrap; }

.batch-summary {
  display: flex;
  gap: 20px;
  padding: 14px 18px;
  background: var(--surface-hover);
  border-radius: var(--radius-sm);
  font-size: .85rem;
  color: var(--text-secondary);
  margin-top: 8px;
}

.batch-summary strong { color: var(--text); }

/* Checkbox group actions */
.checkbox-group-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.btn-link {
  background: none;
  border: none;
  color: var(--primary);
  font-size: .82rem;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  transition: background var(--transition);
}

.btn-link:hover {
  background: var(--primary-light);
}

/* Checkbox group */
.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 6px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: .88rem;
  cursor: pointer;
  padding: 6px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--surface);
  transition: all var(--transition);
  user-select: none;
}

.checkbox-label:hover { border-color: var(--primary-border); background: var(--primary-light); }
.checkbox-label:has(input:checked) {
  background: var(--primary-light);
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 500;
}

.checkbox-label input { display: none; }

/* Empty state */
.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
}

.empty-state svg { margin-bottom: 12px; opacity: .4; }
.empty-state p { font-size: .92rem; }

/* Loading */
.spinner {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .6s linear infinite;
}

.loading-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 32px;
  color: var(--text-muted);
  font-size: .88rem;
}

/* Toast */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 1000;
  display: flex;
  flex-direction: column-reverse;
  gap: 8px;
}

.toast {
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  font-size: .88rem;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  animation: slideUp .3s ease;
  max-width: 380px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.toast.success { background: #065f46; color: #ecfdf5; }
.toast.error { background: #991b1b; color: #fef2f2; }
.toast.info { background: #1e3a5f; color: #eff6ff; }

.toast-exit { animation: slideOut .2s ease forwards; }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn .15s ease;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 28px;
  width: 90%;
  max-width: 480px;
  max-height: 85vh;
  overflow-y: auto;
  animation: scaleIn .2s ease;
}

.modal h3 {
  font-size: 1.1rem;
  margin-bottom: 20px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 24px;
}

/* Group cards */
.group-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
  margin-top: 16px;
}

.group-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  cursor: default;
  transition: all var(--transition);
  position: relative;
}

.group-card:hover { border-color: var(--primary-border); box-shadow: var(--shadow-md); }

.group-card-name {
  font-weight: 600;
  font-size: .95rem;
  margin-bottom: 4px;
}

.group-card-desc {
  font-size: .84rem;
  color: var(--text-secondary);
  margin-bottom: 12px;
  min-height: 1.2em;
}

.group-card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.group-card-meta {
  font-size: .78rem;
  color: var(--text-muted);
}

.group-card-actions {
  display: flex;
  gap: 4px;
}

/* Group search */
.group-search-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.group-search-wrap svg {
  position: absolute;
  left: 11px;
  color: var(--text-muted);
  pointer-events: none;
}

.group-search-wrap input {
  padding-left: 35px;
  width: 220px;
  font-size: .88rem;
}

/* Category list items */
.category-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 10px;
  transition: all var(--transition);
}

.category-item:hover { border-color: var(--primary-border); }

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

.category-item-name {
  font-weight: 600;
  font-size: .92rem;
  display: flex;
  align-items: center;
  gap: 10px;
}

.category-item-body {
  display: flex;
  gap: 24px;
  font-size: .84rem;
  color: var(--text-secondary);
}

.category-item-examples {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.example-tag {
  display: inline-block;
  padding: 3px 10px;
  background: var(--surface-hover);
  border-radius: 14px;
  font-size: .78rem;
  color: var(--text-secondary);
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.example-more {
  font-size: .78rem;
  color: var(--text-muted);
  padding: 3px 10px;
  cursor: pointer;
}
.example-more:hover { color: var(--primary); }

/* Tag input for batch */
.tag-input-wrap {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  min-height: 44px;
  cursor: text;
  transition: border-color var(--transition), box-shadow var(--transition);
  background: var(--surface);
}

.tag-input-wrap:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99,102,241,.12);
}

.tag-list {
  display: contents;
}

.batch-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px 4px 12px;
  background: var(--primary-light);
  border: 1px solid var(--primary-border);
  border-radius: 16px;
  font-size: .84rem;
  color: var(--primary);
  max-width: 100%;
  animation: slideIn .15s ease;
}

.batch-tag-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 320px;
}

.batch-tag-remove {
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0 2px;
  border-radius: 50%;
  color: var(--primary);
  opacity: .6;
  transition: opacity var(--transition);
}

.batch-tag-remove:hover {
  opacity: 1;
}

.tag-input {
  flex: 1;
  min-width: 180px;
  border: none !important;
  padding: 4px 6px !important;
  font-size: .88rem;
  background: transparent !important;
  box-shadow: none !important;
}

.tag-input:focus {
  outline: none;
  box-shadow: none !important;
}

/* Animations */
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes slideIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
@keyframes slideUp { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: translateY(0); } }
@keyframes slideOut { to { opacity: 0; transform: translateY(16px); } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes scaleIn { from { opacity: 0; transform: scale(.95); } to { opacity: 1; transform: scale(1); } }

/* Responsive */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    bottom: 0;
    top: auto;
    left: 0;
    right: 0;
    width: 100%;
    height: auto;
    flex-direction: row;
    padding: 8px 12px;
    border-right: none;
    border-top: 1px solid var(--border);
    z-index: 20;
    background: var(--surface);
  }

  .sidebar-header { display: none; }

  .nav {
    flex-direction: row;
    width: 100%;
    justify-content: space-around;
  }

  .nav-btn {
    flex-direction: column;
    gap: 2px;
    font-size: .72rem;
    padding: 6px 12px;
  }

  .main {
    margin-left: 0;
    padding: 20px 16px 80px;
    max-width: 100%;
  }

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

  .card-header:has(.group-search-wrap) { flex-wrap: wrap; gap: 10px; }
  .group-search-wrap input { width: 100%; }
  .group-search-wrap { width: 100%; order: 1; }

  .form-row { flex-direction: column; }
  .form-row .form-group { width: 100%; }
}
