/* ==========================================================================
   Clarity Studio — design tokens (source of truth)
   Calm light operational UI. Mobile-first; desktop sidebar ≥ 900px.
   Do NOT hardcode colors/spacing elsewhere — use these tokens only.
   ========================================================================== */

:root {
  /* surfaces */
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-soft: #eef1f5;
  --surface-hover: #f2f4f7;

  /* text */
  --text: #1b2330;
  --text-muted: #5c667a;
  --text-faint: #8b94a6;

  /* lines */
  --border: #e2e6ec;
  --border-strong: #cdd4dd;

  /* semantic */
  --primary: #0f766e;        /* dominant action */
  --primary-strong: #115e59; /* hover / pressed */
  --primary-soft: #e0f2f1;   /* selected tab / active state tint */
  --info: #0284c7;           /* working / neutral progress */
  --info-soft: #e0f2fe;
  --success: #16a34a;        /* ready / safe */
  --success-soft: #dcfce7;
  --warn: #b45309;           /* needs a decision / costs money / stale */
  --warn-soft: #fef3c7;
  --danger: #dc2626;         /* broken / destructive only */
  --danger-soft: #fee2e2;

  /* radius */
  --radius-lg: 14px;
  --radius-md: 10px;
  --radius-sm: 7px;
  --radius-full: 999px;

  /* spacing (4px scale) */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;
  --sp-8: 32px;

  /* elevation — intentionally minimal */
  --shadow-1: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow-2: 0 4px 12px rgba(16, 24, 40, 0.06);

  /* type */
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
          "Noto Sans Thai", "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas,
               "Liberation Mono", monospace;
  --text-sm: 13px;
  --text-md: 15px;
  --text-lg: 18px;

  /* layout */
  --topbar-h: 56px;
  --tabbar-h: 62px;
  --sidebar-w: 232px;
  --content-max: 720px;
}

/* ============================ reset / base =============================== */
* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--text-md);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body { padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); }

h1, h2, h3, p { margin: 0; }
button { font-family: inherit; }
a { color: var(--primary); text-decoration: none; }

::selection { background: var(--primary-soft); }

/* visible focus — required on every interactive element */
:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* ============================ topbar ===================================== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  height: var(--topbar-h);
  padding: 0 var(--sp-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-title {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  font-size: var(--text-lg);
  font-weight: 700;
  letter-spacing: -0.01em;
}

.topbar-title .brand { color: var(--primary); }
.topbar-meta {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  color: var(--text-muted);
  font-size: var(--text-sm);
}

/* ============================ navigation ================================= */
/* mobile: bottom tab bar */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.tabbar a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: 44px;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 500;
  -webkit-tap-highlight-color: transparent;
}

.tabbar a svg { width: 22px; height: 22px; }

.tabbar a[aria-current="page"] {
  color: var(--primary);
  font-weight: 600;
}

/* desktop: left sidebar (also used for the episode detail split later) */
.sidebar {
  display: none;
}

@media (min-width: 900px) {
  body { padding-bottom: 0; }
  .tabbar { display: none; }

  .sidebar {
    position: fixed;
    inset: var(--topbar-h) auto 0 0;
    width: var(--sidebar-w);
    display: flex;
    flex-direction: column;
    gap: var(--sp-1);
    padding: var(--sp-4) var(--sp-3);
    background: var(--surface);
    border-right: 1px solid var(--border);
  }

  .sidebar a {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    min-height: 44px;
    padding: 0 var(--sp-3);
    border-radius: var(--radius-md);
    color: var(--text-muted);
    font-weight: 500;
  }

  .sidebar a svg { width: 20px; height: 20px; }

  .sidebar a:hover { background: var(--surface-hover); }

  .sidebar a[aria-current="page"] {
    background: var(--primary-soft);
    color: var(--primary);
    font-weight: 600;
  }

  .topbar { padding-left: calc(var(--sidebar-w) + var(--sp-4)); }
  .view { margin-left: var(--sidebar-w); }
}

/* ============================ view / page ================================ */
.view {
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto;
  padding: var(--sp-4) var(--sp-4) var(--sp-8);
}

.page-head { margin-bottom: var(--sp-4); }

.page-head h1 {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.015em;
}

.page-head .sub { color: var(--text-muted); font-size: var(--text-sm); margin-top: 2px; }

.page-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--sp-2);
  margin-top: var(--sp-3);
  font-size: var(--text-sm);
  color: var(--text-muted);
}

/* ============================ section card =============================== */
.section-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-1);
  margin-bottom: var(--sp-4);
  overflow: hidden;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--border);
}

.section-head h2 {
  font-size: 15px;
  font-weight: 600;
}

.section-head .count {
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-family: var(--font-mono);
}

.section-body { padding: var(--sp-3) var(--sp-4); }

/* ============================ task row =================================== */
.task-row {
  display: flex;
  align-items: stretch;
  gap: var(--sp-3);
  padding: var(--sp-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  cursor: pointer;
  color: inherit;
  text-decoration: none;
  transition: background 0.12s ease;
}

.task-row + .task-row { margin-top: var(--sp-2); }
.task-row:hover { background: var(--surface-hover); }

.task-icon {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: var(--radius-md);
  background: var(--surface-soft);
  color: var(--text-muted);
}

.task-icon svg { width: 20px; height: 20px; }
.task-icon.skill-duang  { color: #7c5f2a; background: #f6efdd; }
.task-icon.skill-ghost  { color: #4b5563; background: #eceff3; }
.task-icon.skill-linetold { color: #b45309; background: #fdf0e2; }
.task-icon.skill-ponder { color: #0f766e; background: #e0f2f1; }
.task-icon.skill-factshort { color: #0369a1; background: #e0f2fe; }

.task-body { flex: 1 1 auto; min-width: 0; }

.task-title {
  font-size: 15px;
  font-weight: 600;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.task-need { color: var(--text-muted); font-size: var(--text-sm); margin-top: 2px; }
.task-why { color: var(--text-faint); font-size: 12px; margin-top: 3px; }

.task-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--sp-2);
  margin-top: var(--sp-2);
}

.task-side {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--sp-2);
  color: var(--text-faint);
}

/* CTA lives inside the card body, full-width on mobile, right-aligned wide */
.task-cta {
  display: flex;
  justify-content: stretch;
  margin-top: var(--sp-3);
}
.task-cta .btn { width: 100%; }

@media (min-width: 900px) {
  .task-cta { justify-content: flex-end; }
  .task-cta .btn { width: auto; }
}

.task-icon.run { color: var(--info); background: var(--info-soft); }

.section-foot {
  padding: var(--sp-3) var(--sp-4);
  border-top: 1px solid var(--border);
  color: var(--text-faint);
  font-size: 12px;
}

/* collapsible backlog fold */
.section-card.backlog summary {
  list-style: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.section-card.backlog summary::-webkit-details-marker { display: none; }
.section-card.backlog summary h2 { color: var(--text-muted); }
.section-card.backlog[open] summary h2 { color: var(--text); }
.section-card.backlog summary::after {
  content: "";
  margin-left: auto;
  width: 18px; height: 18px;
  background: var(--chevron, url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%235c667a' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><path d='M9 6l6 6-6 6'/></svg>")) center/contain no-repeat;
  transition: transform 0.15s ease;
}
.section-card.backlog[open] summary::after { transform: rotate(90deg); }

/* ============================ chips / badges ============================= */
.status-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 9px;
  border-radius: var(--radius-full);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.status-chip::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.status-chip.ok    { color: var(--success); background: var(--success-soft); }
.status-chip.run   { color: var(--info); background: var(--info-soft); }
.status-chip.wait  { color: var(--warn); background: var(--warn-soft); }
.status-chip.down  { color: var(--danger); background: var(--danger-soft); }
.status-chip.quiet { color: var(--text-muted); background: var(--surface-soft); }

.cost-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 9px;
  border-radius: var(--radius-full);
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 600;
}

.cost-badge.free { color: var(--success); background: var(--success-soft); }
.cost-badge.paid { color: var(--warn); background: var(--warn-soft); }

.skill-chip {
  display: inline-flex;
  align-items: center;
  padding: 1px 8px;
  border-radius: var(--radius-full);
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--surface-soft);
}

/* ============================ buttons ==================================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 var(--sp-4);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.btn:active { transform: translateY(1px); }

.btn svg { width: 17px; height: 17px; }

/* dominant action — one per screen */
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-strong); }

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

.btn-ghost {
  background: transparent;
  color: var(--primary);
  min-height: 36px;
  padding: 0 var(--sp-3);
}
.btn-ghost:hover { background: var(--primary-soft); }

.btn[disabled] { opacity: 0.5; cursor: default; }

/* ============================ progress =================================== */
.progress {
  height: 8px;
  border-radius: var(--radius-full);
  background: var(--surface-soft);
  overflow: hidden;
}

.progress .bar {
  height: 100%;
  border-radius: var(--radius-full);
  background: var(--info);
  transition: width 0.4s ease;
}

.progress.live .bar { animation: pulse-bar 1.6s ease-in-out infinite; }

@keyframes pulse-bar {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

/* ============================ states ===================================== */
.state-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-8) var(--sp-4);
  text-align: center;
  color: var(--text-muted);
}

.state-box svg { width: 34px; height: 34px; color: var(--text-faint); }
.state-box .state-title { font-weight: 600; color: var(--text); font-size: 15px; }
.state-box .state-hint { font-size: var(--text-sm); max-width: 34ch; }

.skeleton {
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, var(--surface-soft) 25%, var(--surface-hover) 50%, var(--surface-soft) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s infinite linear;
}

@keyframes shimmer { to { background-position: -200% 0; } }

/* ============================ action bar (sticky CTA) ==================== */
.action-bar {
  position: sticky;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  z-index: 10;
  display: flex;
  gap: var(--sp-3);
  padding: var(--sp-3) 0;
  background: linear-gradient(180deg, transparent, var(--bg) 30%);
}

.action-bar .btn { flex: 1; }

@media (min-width: 900px) {
  .action-bar { bottom: 0; }
}

/* ============================ back link ================================== */
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-weight: 600;
  margin-bottom: var(--sp-3);
  min-height: 32px;
}
.back-link svg { width: 16px; height: 16px; }
.back-link:hover { color: var(--primary); }

/* ============================ episode workspace ========================== */
/* freshness strip under the header */
.progress-strip { display: flex; align-items: center; gap: var(--sp-3); margin-bottom: var(--sp-4); }
.ps-track { flex: 1; height: 6px; border-radius: var(--radius-full); background: var(--surface-soft); overflow: hidden; }
.ps-bar { height: 100%; border-radius: var(--radius-full); background: var(--success); transition: width 0.4s ease; }
.ps-label { font-size: 12px; color: var(--text-muted); white-space: nowrap; }

/* blocker card = the single dominant CTA holder */
.blocker { border-color: var(--border-strong); }
.blocker-need { font-size: 16px; font-weight: 600; line-height: 1.4; }

/* task group rows */
.group-list { padding: var(--sp-3) var(--sp-4); }
.task-row.group.current { background: var(--primary-soft); border-color: var(--primary); }
.task-icon.group-current { background: var(--primary); color: #fff; }

/* scene health minimap */
.scene-map {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
}
.scene-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
}
.scene-cell.tone-ok   { color: var(--success); background: var(--success-soft); }
.scene-cell.tone-warn { color: var(--warn); background: var(--warn-soft); }
.scene-cell.tone-bad  { color: var(--danger); background: var(--danger-soft); }

.legend { display: flex; flex-wrap: wrap; gap: var(--sp-4); margin-top: var(--sp-3); font-size: 12px; color: var(--text-muted); }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.ok   { background: var(--success); }
.dot.warn { background: var(--warn); }
.dot.bad  { background: var(--danger); }

.thumb-preview { width: 100%; border-radius: var(--radius-md); border: 1px solid var(--border); }

.readiness-chips { display: flex; flex-wrap: wrap; gap: var(--sp-3); }
.readiness-chips .flex { gap: var(--sp-1); }

.cost-row { display: flex; align-items: baseline; justify-content: space-between; }

/* desktop split: left = overview+tasks, right = outputs/detail */
.episode-grid { display: grid; gap: var(--sp-4); }
.episode-detail .section-card:last-child { margin-bottom: 0; }

@media (min-width: 1024px) {
  .episode-grid { grid-template-columns: minmax(0, 1fr) 300px; align-items: start; }
}

/* ============================ login / auth =============================== */
.auth-mode .tabbar, .auth-mode .sidebar { display: none; }

.auth-card {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
  max-width: 340px;
  margin: 12vh auto 0;
  padding: var(--sp-6) var(--sp-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
}

.auth-brand {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 700;
  color: var(--primary);
}
.auth-brand svg { width: 20px; height: 20px; }

.pw-input {
  min-height: 48px;
  padding: 0 var(--sp-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: 16px;
  background: var(--surface);
  color: var(--text);
}
.pw-input:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ============================ misc utilities ============================= */
.mono { font-family: var(--font-mono); }
.muted { color: var(--text-muted); }
.faint { color: var(--text-faint); }
.flex { display: flex; align-items: center; gap: var(--sp-2); }
.stack > * + * { margin-top: var(--sp-2); }
.divider { height: 1px; background: var(--border); margin: var(--sp-3) 0; }
