:root {
  --bg: #f5f5f7;
  --bg-elevated: #ffffff;
  --bg-elevated-2: #fbfbfd;
  --text: #1d1d1f;
  --text-secondary: #6e6e73;
  --text-tertiary: #98989d;
  --accent: #0071e3;
  --accent-hover: #0077ed;
  --tfjs-accent: #ff6f00;
  --tfjs-accent-2: #ffa726;
  --border: rgba(0, 0, 0, 0.08);
  --shadow-sm: 0 2px 10px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.12);
  --radius-lg: 20px;
  --radius-md: 12px;
  --code-bg: #1d1f21;
  --code-text: #e6e6e6;
  --sidebar-w: 300px;
  --good: #30a46c;
  --warn: #d9822b;
  --bad: #e5484d;
  --series-actual: #2a78d6;
  --series-forecast: #eb6834;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-elevated: #1c1c1e;
    --bg-elevated-2: #131315;
    --text: #f5f5f7;
    --text-secondary: #98989d;
    --text-tertiary: #6e6e73;
    --border: rgba(255, 255, 255, 0.12);
    --shadow-sm: 0 2px 10px rgba(0, 0, 0, 0.5);
    --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.6);
    --code-bg: #0d0e10;
    --series-actual: #3987e5;
    --series-forecast: #d95926;
  }
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
    "Inter", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 60;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px max(20px, env(safe-area-inset-right)) 12px max(20px, env(safe-area-inset-left));
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  background: color-mix(in srgb, var(--bg) 72%, transparent);
  border-bottom: 1px solid var(--border);
  height: 56px;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 600; font-size: 15px; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 8px;
  background: linear-gradient(135deg, var(--tfjs-accent), var(--tfjs-accent-2));
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 11px; font-weight: 700; flex-shrink: 0;
}
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.ghost-link { color: var(--text-secondary); font-size: 14px; text-decoration: none; }
.ghost-link:hover { color: var(--text); }
.sidebar-toggle {
  display: none; width: 34px; height: 34px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--bg-elevated); color: var(--text);
  align-items: center; justify-content: center; cursor: pointer;
}

/* ---------- Layout ---------- */
.app-shell { display: flex; min-height: calc(100vh - 56px); }

.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--bg-elevated-2);
  overflow-y: auto;
  height: calc(100vh - 56px);
  position: sticky; top: 56px;
  padding: 16px 12px 40px;
}
.sidebar-search {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: 999px; padding: 9px 14px; margin-bottom: 14px;
}
.sidebar-search svg { flex-shrink: 0; color: var(--text-tertiary); }
.sidebar-search input {
  border: none; background: none; outline: none; font-size: 14px;
  color: var(--text); width: 100%; font-family: inherit;
}

.nav-category { margin-bottom: 2px; }
.nav-category-btn {
  width: 100%; display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 10px; border: none; background: none;
  color: var(--text); font-size: 13.5px; font-weight: 600; text-align: left;
  cursor: pointer; font-family: inherit;
}
.nav-category-btn:hover { background: var(--bg-elevated); }
.nav-category-btn .cat-icon { flex-shrink: 0; color: var(--tfjs-accent); display: flex; }
.nav-category-btn .cat-count {
  margin-left: auto; font-size: 11px; font-weight: 500; color: var(--text-tertiary);
  background: var(--bg-elevated); padding: 1px 7px; border-radius: 999px;
}
.nav-category-btn .chevron { transition: transform 0.18s ease; color: var(--text-tertiary); flex-shrink: 0; }
.nav-category.open .chevron { transform: rotate(90deg); }

.nav-topics { list-style: none; margin: 2px 0 8px; padding: 0; display: none; }
.nav-category.open .nav-topics { display: block; }
.nav-topics li { margin: 1px 0; }
.nav-topics a {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 14px 7px 38px; border-radius: 8px;
  font-size: 13.5px; color: var(--text-secondary);
}
.nav-topics a:hover { background: var(--bg-elevated); color: var(--text); }
.nav-topics a.active { background: var(--accent); color: #fff; }
.nav-topics a .live-dot {
  width: 6px; height: 6px; border-radius: 50%; background: var(--good); flex-shrink: 0;
}
.nav-topics a.active .live-dot { background: #fff; }

.sidebar-hidden-msg { padding: 10px 14px; font-size: 13px; color: var(--text-tertiary); display: none; }

/* ---------- Content ---------- */
.content-area { flex: 1; min-width: 0; }
.content-inner { max-width: 820px; margin: 0 auto; padding: 40px 28px 100px; }

/* Home / landing */
.hero {
  display: flex; align-items: center; gap: 40px; padding: 20px 0 44px;
  border-bottom: 1px solid var(--border); margin-bottom: 36px; flex-wrap: wrap;
}
.hero-text { flex: 1; min-width: 260px; }
.hero-text .eyebrow { font-size: 13px; font-weight: 600; color: var(--tfjs-accent); margin: 0 0 10px; letter-spacing: 0.02em; }
.hero-text h1 { font-size: clamp(28px, 4vw, 38px); margin: 0 0 14px; letter-spacing: -0.01em; line-height: 1.1; }
.hero-text p { font-size: 16px; color: var(--text-secondary); line-height: 1.6; margin: 0 0 20px; max-width: 540px; }
.hero-image { width: 220px; flex-shrink: 0; }
.hero-image img, .hero-image svg { width: 100%; height: auto; display: block; }
.hero-stats { display: flex; gap: 22px; flex-wrap: wrap; }
.hero-stat strong { display: block; font-size: 22px; }
.hero-stat span { font-size: 12.5px; color: var(--text-secondary); }

.section-heading { display: flex; align-items: baseline; justify-content: space-between; margin: 0 0 14px; gap: 12px; flex-wrap: wrap; }
.section-heading h3 { margin: 0; }
.section-heading p { margin: 0; font-size: 13px; color: var(--text-tertiary); }

.category-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; margin-bottom: 40px; }
.category-card {
  background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-md);
  padding: 16px; display: flex; align-items: center; gap: 12px; transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.category-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-sm); }
.category-card .cat-icon { color: var(--tfjs-accent); flex-shrink: 0; }
.category-card .cat-name { font-size: 13.5px; font-weight: 600; }
.category-card .cat-count { font-size: 12px; color: var(--text-tertiary); }

.project-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.project-card {
  background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-md);
  padding: 18px; display: flex; flex-direction: column; gap: 8px;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.project-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-sm); }
.project-card .pc-top { display: flex; align-items: center; justify-content: space-between; }
.project-card .cat-icon { color: var(--tfjs-accent); }
.project-card .pc-title { font-size: 14.5px; font-weight: 600; margin: 0; }
.project-card .pc-summary { font-size: 12.5px; color: var(--text-secondary); line-height: 1.5; margin: 0; }

.badge {
  display: inline-flex; align-items: center; gap: 5px; font-size: 10.5px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.04em; padding: 3px 9px; border-radius: 999px;
}
.badge-live { background: color-mix(in srgb, var(--good) 16%, transparent); color: var(--good); }
.badge-doc { background: color-mix(in srgb, var(--text-tertiary) 18%, transparent); color: var(--text-secondary); }
.badge .live-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--good); }

/* Topic / project page */
.breadcrumb { font-size: 13px; color: var(--text-tertiary); margin-bottom: 10px; }
.breadcrumb a:hover { color: var(--accent); }
.topic-title-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.topic-title { font-size: clamp(26px, 4vw, 34px); margin: 0; letter-spacing: -0.01em; line-height: 1.15; }
.topic-summary { font-size: 16px; color: var(--text-secondary); line-height: 1.6; margin: 0 0 28px; }

.topic-body { font-size: 16px; line-height: 1.75; }
.topic-body h3 { font-size: 19px; margin: 32px 0 12px; letter-spacing: -0.005em; }
.topic-body p { margin: 0 0 16px; }
.topic-body ul, .topic-body ol { margin: 0 0 16px; padding-left: 22px; }
.topic-body li { margin-bottom: 6px; }
.topic-body strong { font-weight: 600; }
.topic-body code {
  font-family: "SF Mono", "SFMono-Regular", ui-monospace, Menlo, monospace;
  background: var(--bg-elevated-2); border: 1px solid var(--border);
  padding: 2px 6px; border-radius: 6px; font-size: 0.88em;
}
.topic-body pre {
  background: var(--code-bg); color: var(--code-text); border-radius: var(--radius-md);
  padding: 18px 20px; overflow-x: auto; margin: 0 0 20px; box-shadow: var(--shadow-sm);
}
.topic-body pre code {
  background: none; border: none; padding: 0; font-size: 13.5px; line-height: 1.6; color: inherit;
}
.pill-row { display: flex; gap: 8px; flex-wrap: wrap; margin: 0 0 20px; }
.pill {
  font-size: 12px; font-weight: 600; padding: 5px 12px; border-radius: 999px;
  background: var(--bg-elevated-2); border: 1px solid var(--border); color: var(--text-secondary);
}

.topic-nav { display: flex; justify-content: space-between; gap: 16px; margin-top: 48px; padding-top: 24px; border-top: 1px solid var(--border); }
.topic-nav-link {
  flex: 1; max-width: 48%; display: flex; flex-direction: column; gap: 4px;
  padding: 14px 16px; border: 1px solid var(--border); border-radius: var(--radius-md);
  background: var(--bg-elevated);
}
.topic-nav-link:hover { border-color: var(--accent); }
.topic-nav-link.next { text-align: right; margin-left: auto; }
.topic-nav-link .nav-dir { font-size: 11.5px; color: var(--text-tertiary); text-transform: uppercase; letter-spacing: 0.04em; }
.topic-nav-link .nav-title { font-size: 14px; font-weight: 600; }

.not-found-box { text-align: center; padding: 80px 20px; color: var(--text-secondary); }

/* ---------- Demo panel (shared by all live demos) ---------- */
.demo-panel {
  background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 22px; margin: 0 0 32px; box-shadow: var(--shadow-sm);
}
.demo-panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.demo-panel-head h4 { margin: 0; font-size: 15px; }
.status-pill {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600;
  padding: 4px 10px; border-radius: 999px; background: var(--bg-elevated-2); color: var(--text-secondary);
  border: 1px solid var(--border);
}
.status-pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-tertiary); }
.status-pill.ok .dot { background: var(--good); }
.status-pill.busy .dot { background: var(--warn); animation: pulse 1.2s ease-in-out infinite; }
.status-pill.err .dot { background: var(--bad); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

.btn {
  display: inline-flex; align-items: center; gap: 7px; font-size: 13.5px; font-weight: 600;
  padding: 9px 16px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--bg-elevated-2); color: var(--text); cursor: pointer; font-family: inherit;
  transition: transform 0.1s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--tfjs-accent); border-color: var(--tfjs-accent); color: #fff; }
.btn-primary:hover { background: #e86300; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }

.media-frame {
  position: relative; width: 100%; max-width: 480px; aspect-ratio: 4/3;
  background: #000; border-radius: var(--radius-md); overflow: hidden; margin: 0 auto 16px;
}
.media-frame video, .media-frame canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.media-frame .media-placeholder {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; color: rgba(255,255,255,0.6); font-size: 13px; text-align: center; padding: 20px;
}
.media-frame .media-placeholder svg { opacity: 0.7; }

.permission-notice {
  display: flex; gap: 10px; align-items: flex-start; padding: 12px 14px; border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--tfjs-accent) 10%, transparent); border: 1px solid color-mix(in srgb, var(--tfjs-accent) 30%, transparent);
  font-size: 13px; color: var(--text-secondary); margin-bottom: 14px;
}
.permission-notice svg { color: var(--tfjs-accent); flex-shrink: 0; margin-top: 1px; }

.class-bucket-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; margin-bottom: 14px; }
.class-bucket {
  border: 1px solid var(--border); border-radius: var(--radius-md); padding: 12px; background: var(--bg-elevated-2);
  display: flex; flex-direction: column; gap: 8px;
}
.class-bucket-name { display: flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 600; }
.class-bucket-name input {
  border: none; background: none; font-weight: 600; font-size: 13px; color: var(--text);
  font-family: inherit; width: 100%; outline: none; border-bottom: 1px dashed transparent;
}
.class-bucket-name input:focus { border-bottom-color: var(--border); }
.class-bucket-count { font-size: 11px; color: var(--text-tertiary); }
.class-bucket .btn { width: 100%; justify-content: center; padding: 7px 10px; font-size: 12.5px; }

.predict-result {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px; border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--good) 10%, transparent); border: 1px solid color-mix(in srgb, var(--good) 30%, transparent);
  margin-bottom: 14px;
}
.predict-result strong { font-size: 15px; }
.predict-result span { font-size: 12.5px; color: var(--text-secondary); }

.prob-bar-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.prob-bar-label { width: 92px; flex-shrink: 0; font-size: 13px; font-weight: 600; text-align: right; text-transform: capitalize; }
.prob-bar-track { flex: 1; height: 20px; border-radius: 999px; background: var(--bg-elevated-2); border: 1px solid var(--border); overflow: hidden; }
.prob-bar-fill { height: 100%; background: linear-gradient(90deg, var(--tfjs-accent), var(--tfjs-accent-2)); border-radius: 999px 0 0 999px; transition: width 0.12s ease; }
.prob-bar-fill.top { background: linear-gradient(90deg, var(--good), #57c98a); }
.prob-bar-pct { width: 42px; flex-shrink: 0; font-size: 12px; color: var(--text-secondary); }

.style-thumb-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 8px; margin-bottom: 14px; }
.style-thumb {
  aspect-ratio: 1; border-radius: 10px; overflow: hidden; border: 2px solid transparent; cursor: pointer; padding: 0;
  background: var(--bg-elevated-2); display: block; width: 100%;
}
.style-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.style-thumb.active { border-color: var(--tfjs-accent); }

.log-line { font-size: 12px; color: var(--text-tertiary); margin-top: 10px; }
.demo-note { font-size: 12.5px; color: var(--text-tertiary); margin-top: 12px; line-height: 1.6; }

.field-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.field-row input[type="text"] {
  flex: 1; min-width: 160px; font-size: 13.5px; padding: 9px 14px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--bg-elevated-2); color: var(--text); font-family: inherit;
}
.field-row input[type="text"]:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.chart-wrap { width: 100%; max-width: 640px; margin: 0 auto; }
.chart-wrap canvas { width: 100%; height: 220px; display: block; }
.chart-legend { display: flex; gap: 18px; justify-content: center; margin-top: 10px; flex-wrap: wrap; }
.chart-legend-item { display: flex; align-items: center; gap: 7px; font-size: 12.5px; color: var(--text-secondary); }
.chart-legend-swatch { width: 20px; height: 3px; border-radius: 2px; flex-shrink: 0; }

.textarea-field {
  width: 100%; min-height: 96px; resize: vertical; font-family: inherit; font-size: 13.5px;
  line-height: 1.6; padding: 12px 14px; border-radius: var(--radius-md); border: 1px solid var(--border);
  background: var(--bg-elevated-2); color: var(--text); margin-bottom: 12px;
}
.textarea-field:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.autocomplete-preview {
  font-family: inherit; font-size: 14px; line-height: 1.7; padding: 14px 16px; border-radius: var(--radius-md);
  border: 1px solid var(--border); background: var(--bg-elevated-2); margin-bottom: 12px; min-height: 60px; white-space: pre-wrap;
}
.autocomplete-preview .suggestion { color: var(--text-tertiary); font-style: italic; }
.training-progress { height: 6px; border-radius: 999px; background: var(--bg-elevated-2); border: 1px solid var(--border); overflow: hidden; margin-bottom: 10px; }
.training-progress-fill { height: 100%; background: linear-gradient(90deg, var(--tfjs-accent), var(--tfjs-accent-2)); width: 0%; transition: width 0.2s ease; }

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  .sidebar-toggle { display: flex; }
  .sidebar {
    position: fixed; top: 56px; left: 0; bottom: 0; z-index: 55;
    transform: translateX(-100%); transition: transform 0.22s ease;
    box-shadow: var(--shadow-lg); width: min(86vw, 320px);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay {
    display: none; position: fixed; inset: 56px 0 0 0; background: rgba(0,0,0,0.35); z-index: 54;
  }
  .sidebar-overlay.open { display: block; }
  .content-inner { padding: 28px 18px 80px; }
  .hero { flex-direction: column-reverse; text-align: center; gap: 20px; }
  .hero-text p { max-width: none; }
  .hero-stats { justify-content: center; }
  .topic-nav { flex-direction: column; }
  .topic-nav-link { max-width: none; }
}

.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
