/* NewsMind — frontend styles */
/* NewsMind — reading-room system (apprun.icu family).
   Warm paper, ink, hairline rules, press-blue accent. Default = paper
   (light); [data-theme="dark"] = night paper. Same family as the
   landing catalog: no gradients, no glows, no glass. */
:root {
  --bg: #f3f0e9;
  --bg-alt: #faf7f0;
  --bg-elev: #ece8df;
  --border: rgba(21,21,21,.16);
  --border-strong: rgba(21,21,21,.32);
  --text: #151515;
  --text-dim: #6b6862;
  --accent: #1f5fa8;
  --accent-dim: #1a4f8c;
  --accent-btn: #1f5fa8;  /* button fill: >=4.5:1 with paper text */
  --accent-wash: rgba(31,95,168,.10);
  --danger: #b03a2e;
  --success: #2f7d5c;
  --warning: #a9711f;
  --world: #3f7fd6;
  --economy: #d98a2b;
  --science: #3f8f5f;
  --tech: #8f5fbf;
  --politics: #cf6a3f;
  --sports: #2f9db8;
  --health: #c0507f;
  --entertainment: #b59a1f;
  --radius: 6px;
  --font-display: 'Fraunces', Georgia, serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, Menlo, monospace;
  --shadow: 0 12px 32px rgba(60,45,20,.13);
  --graph-edge: rgba(21,21,21,.30);
  --graph-halo: rgba(31,95,168,.16);
}

[data-theme="dark"] {
  --bg: #14120e;
  --bg-alt: #1b1915;
  --bg-elev: #24211a;
  --border: rgba(240,236,227,.12);
  --border-strong: rgba(240,236,227,.24);
  --text: #f0ece3;
  --text-dim: #a9a498;
  --accent: #7fb0e8;
  --accent-dim: #5d8fc4;
  --accent-btn: #1f5fa8;
  --accent-wash: rgba(127,176,232,.12);
  --danger: #d98a72;
  --success: #93b08b;
  --warning: #d9b45c;
  --world: #7fb0e8;
  --economy: #e6b063;
  --science: #93b08b;
  --tech: #c79be8;
  --politics: #e08a72;
  --sports: #67c8d8;
  --health: #e88ab0;
  --entertainment: #e2c96b;
  --radius: 6px;
  --font-display: 'Fraunces', Georgia, serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, Menlo, monospace;
  --shadow: 0 18px 48px rgba(0,0,0,.34);
  --graph-edge: rgba(240,236,227,.20);
  --graph-halo: rgba(127,176,232,.16);
}

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

body {
  font-family: 'Inter Tight', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
  height: 100vh;
}

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

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: var(--bg-alt);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .logo { font-size: 22px; color: var(--accent); display: inline-flex; }
.brand .logo svg { display: block; }
.brand .title { font-family: var(--font-display); font-weight: 600; font-size: 18px; letter-spacing: -0.01em; }
.brand .tagline { color: var(--text-dim); font-size: 12px; }
.topbar-actions { display: flex; align-items: center; gap: 12px; }
.status { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-dim); }
.status.active { color: var(--success); }
.status.error { color: var(--danger); }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }

/* Buttons */
.btn {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}
.btn:hover { background: var(--border); border-color: var(--accent-dim); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-primary { background: var(--accent-btn); border-color: var(--accent-btn); color: #fff; }
.btn-primary:hover { background: var(--accent-dim); }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-block { width: 100%; margin-top: 8px; }
.btn-toggle.active { background: var(--accent-btn); border-color: var(--accent-btn); color: #fff; }

/* Layout */
.layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 280px;
  background: var(--bg-alt);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  flex-shrink: 0;
}
.sidebar-section { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.sidebar-section h3 { font-size: 11px; font-family: var(--font-mono); text-transform: uppercase; letter-spacing: 0.14em; color: var(--text-dim); margin-bottom: 10px; }
.feed-controls { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.input {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  width: 100%;
}
.input:focus { outline: none; border-color: var(--accent); }
.feed-list { max-height: 200px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.feed-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}
.feed-item:hover { background: var(--bg-elev); }
.feed-item.selected { background: var(--accent-wash); color: var(--accent); }
.feed-item input[type="checkbox"] { accent-color: var(--accent); }
.feed-item .feed-name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat-val { display: block; font-size: 16px; font-weight: 600; margin-top: 2px; }

.memory-actions { display: flex; gap: 6px; margin-top: 8px; }
.memory-actions .btn { flex: 1; }

/* Threshold Slider */
.threshold-control { display: flex; flex-direction: column; gap: 6px; }
.slider {
  -webkit-appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: var(--border);
  outline: none;
}
.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}
.threshold-label { text-align: center; font-size: 12px; color: var(--text-dim); }
.slider-label { display: block; font-size: 11px; color: var(--text-dim); margin-bottom: 4px; }
.slider-label span { color: var(--text); font-weight: 600; }

/* Feed Health */
.feed-health-summary { display: flex; gap: 8px; margin-bottom: 10px; }
.feed-health-stat { flex: 1; text-align: center; padding: 6px; background: var(--bg); border-radius: var(--radius); }
.feed-health-stat-val { font-size: 18px; font-weight: 700; }
.feed-health-stat-label { font-size: 9px; color: var(--text-dim); text-transform: uppercase; }
.feed-health-stat.success .feed-health-stat-val { color: var(--success); }
.feed-health-stat.failed .feed-health-stat-val { color: var(--danger); }
.feed-health-list { max-height: 150px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.feed-health-item { display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-radius: 4px; font-size: 11px; background: var(--bg); }
.feed-health-item.ok { color: var(--success); }
.feed-health-item.fail { color: var(--danger); }
.feed-health-item .feed-name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.feed-health-item .feed-count {
  font-family: var(--font-mono); color: var(--text-dim); font-size: 10px; }

.cat-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.cat-chip {
  padding: 3px 8px;
  border-radius: 12px;
  font-size: 11px;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg);
  transition: all 0.15s;
}
.cat-chip.active { background: var(--accent-btn); border-color: var(--accent-btn); color: #fff; }
.cat-chip:hover { border-color: var(--accent); }

.memory-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stat { background: var(--bg); padding: 8px; border-radius: var(--radius); text-align: center; }
.stat-label { display: block; font-size: 10px; color: var(--text-dim); text-transform: uppercase; }
.stat-val { display: block; font-size: 16px; font-weight: 600; margin-top: 2px; }

.memory-actions { display: flex; gap: 6px; margin-top: 8px; }
.memory-actions .btn { flex: 1; }

/* Threshold Slider */
.threshold-control { display: flex; flex-direction: column; gap: 6px; }
.slider {
  -webkit-appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: var(--border);
  outline: none;
}
.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}
.threshold-label { text-align: center; font-size: 12px; color: var(--text-dim); }

/* Feed Health */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: var(--bg-alt);
  border-bottom: 1px solid var(--border);
}
.toolbar-spacer { flex: 1; }
.view-toggle { display: flex; gap: 0; }
.view-toggle .btn { border-radius: 0; }
.view-toggle .btn:first-child { border-radius: var(--radius) 0 0 var(--radius); }
.view-toggle .btn:last-child { border-radius: 0 var(--radius) var(--radius) 0; }

/* Progress */
.progress-bar {
  height: 28px;
  background: var(--bg-alt);
  border-bottom: 1px solid var(--border);
  position: relative;
  overflow: hidden;
  display: none;
}
.progress-bar.visible { display: block; }
.progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.3s;
}
.progress-text {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: var(--text);
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}

/* Views */
.view-container { flex: 1; position: relative; overflow: hidden; }
.view { position: absolute; top: 0; left: 0; right: 0; bottom: 0; display: none; }
.view.active { display: block; }
.view-graph { display: none; flex-direction: column; }
.view-graph.active { display: flex; }

#graph-canvas { flex: 1; width: 100%; cursor: grab; }
#graph-canvas:active { cursor: grabbing; }
.graph-legend {
  display: flex;
  gap: 16px;
  padding: 8px 16px;
  background: var(--bg-alt);
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}
.legend-item { display: flex; align-items: center; gap: 5px; font-size: 11px; color: var(--text-dim); }
.legend-item .dot { width: 10px; height: 10px; border-radius: 50%; }
.dot-world { background: var(--world); }
.dot-economy { background: var(--economy); }
.dot-science { background: var(--science); }
.dot-tech { background: var(--tech); }
.dot-politics { background: var(--politics); }
.dot-sports { background: var(--sports); }
.dot-health { background: var(--health); }
.dot-entertainment { background: var(--entertainment); }
.graph-hint { padding: 6px 16px; font-size: 11px; color: var(--text-dim); background: var(--bg-alt); border-top: 1px solid var(--border); text-align: center; }

.loss-curve-container {
  padding: 8px 16px;
  background: var(--bg-alt);
  border-top: 1px solid var(--border);
}
.loss-curve-container canvas {
  width: 100%;
  height: 80px;
  display: block;
}

/* Article list */
.article-list { overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; height: 100%; }
.article-card {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  cursor: pointer;
  transition: all 0.15s;
}
.article-card:hover { border-color: var(--accent); transform: translateX(2px); }
.article-card h4 { font-size: 14px; margin-bottom: 4px; }
.article-card .article-meta { font-size: 11px; color: var(--text-dim); display: flex; gap: 10px; }
.article-card .article-snippet { font-size: 12px; color: var(--text-dim); margin-top: 6px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.article-controls { display: flex; gap: 8px; padding: 12px 16px 0; }
.article-controls .input { flex: 1; }

/* Clusters */
.cluster-grid { overflow-y: auto; padding: 16px; display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; height: 100%; align-content: start; }
.cluster-card {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}
.cluster-card h4 { font-size: 13px; margin-bottom: 8px; display: flex; align-items: center; gap: 8px; }
.cluster-card .cluster-terms { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.cluster-card .cluster-term { font-size: 11px; padding: 2px 6px; background: var(--bg-elev); border-radius: 4px; }
.cluster-card .cluster-count { font-size: 11px; color: var(--text-dim); }
.cluster-card { cursor: pointer; transition: all 0.15s; }
.cluster-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.cluster-article { padding: 4px 0; border-bottom: 1px solid var(--border); }
.cluster-article a { color: var(--text); text-decoration: none; }
.cluster-article a:hover { color: var(--accent); }

/* Detail panel */
.detail-panel {
  position: fixed;
  top: 0; right: -420px;
  width: 400px;
  height: 100vh;
  background: var(--bg-alt);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 100;
  transition: right 0.25s;
  display: flex;
  flex-direction: column;
}
.detail-panel.open { right: 0; }
.detail-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.detail-header h3 { font-size: 14px; }
.detail-body { flex: 1; overflow-y: auto; padding: 16px; font-size: 13px; }
.detail-body .detail-row { margin-bottom: 12px; }
.detail-body .detail-label { font-size: 11px; color: var(--text-dim); text-transform: uppercase; margin-bottom: 2px; }
.detail-body a { color: var(--accent); }

/* Toast */
.toast-container { position: fixed; bottom: 20px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }

.training-summary {
  position: fixed;
  bottom: -300px;
  left: 50%;
  transform: translateX(-50%);
  width: 500px;
  max-height: 300px;
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: var(--shadow);
  z-index: 150;
  transition: bottom 0.3s;
  display: flex;
  flex-direction: column;
}
.training-summary.open { bottom: 0; }
.summary-header { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.summary-header h3 { font-size: 14px; }
.summary-body { flex: 1; overflow-y: auto; padding: 16px; font-size: 12px; }
.summary-stat { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--border); }
.summary-stat-label { color: var(--text-dim); }
.summary-stat-val { font-weight: 600; }
.toast {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 16px;
  font-size: 13px;
  box-shadow: var(--shadow);
  animation: slideIn 0.2s;
  max-width: 320px;
}
.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--danger); }
.toast.info { border-left: 3px solid var(--accent); }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }

/* Mobile Responsive */
@media (max-width: 768px) {
  .topbar { padding: 8px 12px; }
  .brand .tagline { display: none; }
  .brand .title { font-size: 16px; }
  
  .layout { flex-direction: column; }
  
  .sidebar {
    width: 100%;
    max-height: 40vh;
    border-right: none;
    border-bottom: 1px solid var(--border);
    flex-direction: row;
    overflow-x: auto;
  }
  
  .sidebar-section {
    min-width: 200px;
    border-bottom: none;
    border-right: 1px solid var(--border);
    padding: 10px;
  }
  
  .feed-list { max-height: 100px; }
  .feed-health-list { max-height: 80px; }
  
  .toolbar { padding: 8px 10px; flex-wrap: wrap; gap: 6px; }
  .toolbar-spacer { display: none; }
  .view-toggle { width: 100%; justify-content: center; }
  
  .view-container { height: 60vh; }
  
  .graph-legend { gap: 8px; padding: 6px 10px; }
  .legend-item { font-size: 10px; }
  
  .cluster-grid { grid-template-columns: 1fr; }
  
  .detail-panel { width: 100%; right: -100%; }
  .detail-panel.open { right: 0; }
}

@media (max-width: 480px) {
  .sidebar { max-height: 35vh; }
  .sidebar-section { min-width: 150px; }
  .view-container { height: 65vh; }
  .btn { padding: 5px 10px; font-size: 12px; }
  .brand .logo { font-size: 18px; }
  .brand .title { font-size: 14px; }
}

/* =========================================================================
   NewsMind v1.1 additions — loader, empty states, server-down, polish
   ========================================================================= */

.btn-sm { padding: 4px 10px; font-size: 11px; }

.feed-select-actions {
  display: flex; align-items: center; gap: 6px; margin-top: 6px;
}
.feed-select-actions .btn-sm { padding: 3px 8px; }
.feed-count {
  margin-left: auto; font-size: 10px; color: var(--text-dim);
  background: var(--bg); padding: 2px 7px; border-radius: 10px;
  font-variant-numeric: tabular-nums;
}

/* Progress shimmer */
.progress-fill { position: relative; overflow: hidden; }
.progress-fill::after {
  content: ""; position: absolute; top: 0; left: 0; bottom: 0; width: 40%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.35), transparent);
  animation: shimmer 1.1s infinite;
}
@keyframes shimmer { 0% { transform: translateX(-100%); } 100% { transform: translateX(320%); } }

/* Train button loading state */
.btn-primary.loading { position: relative; color: transparent !important; pointer-events: none; }
.btn-primary.loading::after {
  content: ""; position: absolute; top: 50%; left: 50%;
  width: 14px; height: 14px; margin: -7px 0 0 -7px;
  border: 2px solid rgba(255,255,255,0.4); border-top-color: #fff;
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Overlay loader (covers the view area during fetch/train) */
.overlay-loader {
  position: absolute; inset: 0; z-index: 40;
  display: none; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px; text-align: center; padding: 20px;
  background: rgba(21,21,21,0.6); /* fallback if color-mix unsupported */
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
}
.overlay-loader.visible { display: flex; }
.loader-graph { width: 54px; height: 54px; margin-bottom: 14px; position: relative; }
.loader-ring {
  position: absolute; inset: 0; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
.loader-ring::after {
  content: ""; position: absolute; inset: 10px; border-radius: 50%;
  border: 3px solid transparent; border-bottom-color: var(--accent);
  animation: spin 1.2s linear infinite reverse;
}
.loader-dots { display: flex; gap: 9px; margin-bottom: 6px; }
.loader-dots span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--accent);
  animation: neuronpulse 1.2s ease-in-out infinite;
}
.loader-dots span:nth-child(2) { animation-delay: 0.18s; }
.loader-dots span:nth-child(3) { animation-delay: 0.36s; }
@keyframes neuronpulse {
  0%,100% { transform: scale(0.6); opacity: 0.4; }
  50% { transform: scale(1.2); opacity: 1; }
}
.loader-title { font-size: 14px; font-weight: 600; color: var(--text); }
.loader-sub { font-size: 12px; color: var(--text-dim); min-height: 16px; max-width: 360px; }

/* Empty states */
.empty-state {
  position: absolute; inset: 0; z-index: 5;
  display: none; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; text-align: center; padding: 24px; color: var(--text-dim);
}
.empty-state.show { display: flex; }
.empty-state .es-icon { font-size: 36px; opacity: 0.55; }
.empty-state .es-title { font-size: 14px; color: var(--text); font-weight: 600; }
.empty-state .es-sub { font-size: 12px; max-width: 340px; line-height: 1.6; }
.empty-state .es-sub b { color: var(--text); }

/* Server-down modal */
.server-down {
  position: fixed; inset: 0; z-index: 1000;
  display: none; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.72); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.server-down.show { display: flex; }
.server-down-card {
  background: var(--bg-alt); border: 1px solid var(--border); border-radius: 12px;
  padding: 28px 32px; max-width: 380px; text-align: center; box-shadow: 0 12px 40px rgba(0,0,0,0.5);
}
.server-down-card .sd-icon { font-size: 38px; color: var(--warning); margin-bottom: 8px; }
.server-down-card h3 { font-size: 16px; margin-bottom: 10px; }
.server-down-card p { font-size: 13px; color: var(--text-dim); margin-bottom: 8px; line-height: 1.5; }
.server-down-card .sd-hint { margin-bottom: 16px; }
.server-down-card code {
  background: var(--bg); padding: 2px 6px; border-radius: 4px; font-size: 12px; color: var(--text);
}
.server-down-card #sd-url { color: var(--text); font-variant-numeric: tabular-nums; }

/* Toast refinements */
.toast { transition: opacity 0.3s, transform 0.3s; }
.toast-container { max-width: 340px; }
