/* ============================================================
   Custom properties
   ============================================================ */
:root {
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;

  --bg: #f7f6f3;
  --surface: #ffffff;
  --surface-hover: #f4f4f2;

  --border: #e8e7e4;
  --border-strong: #d3d2cf;

  --text: #1a1a18;
  --text-secondary: #787774;
  --text-muted: #b5b3af;

  --accent: #2d6ae0;
  --accent-hover: #2259c4;
  --accent-bg: #edf2fb;

  --radius-sm: 4px;
  --radius: 6px;
  --radius-lg: 10px;

  --header-height: 52px;
  --content-width: 820px;
  --sop-width: 760px;
}

/* ============================================================
   Reset + base
   ============================================================ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html { font-size: 14px; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font-family: inherit; cursor: pointer; }
input, textarea { font-family: inherit; }

/* ============================================================
   App header
   ============================================================ */
.app-header {
  position: sticky;
  top: 0;
  z-index: 200;
  height: var(--header-height);
  background: rgba(255,255,255,0.9);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.header-inner {
  max-width: var(--content-width);
  margin: 0 auto;
  padding: 0 24px;
  height: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
}

.app-logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
}

.logo-mark {
  width: 24px;
  height: 24px;
  background: var(--text);
  color: #fff;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  flex-shrink: 0;
}

.app-divider {
  width: 1px;
  height: 16px;
  background: var(--border-strong);
}

.app-subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 400;
}

/* ============================================================
   Layout shells
   ============================================================ */
#app { min-height: calc(100vh - var(--header-height)); }

.list-view,
.sop-view {
  padding: 32px 24px 64px;
}

.list-view-inner {
  max-width: var(--content-width);
  margin: 0 auto;
}

.sop-view-inner {
  max-width: var(--sop-width);
  margin: 0 auto;
}

/* ============================================================
   Search bar
   ============================================================ */
.search-bar-wrap {
  margin-bottom: 20px;
}

.search-bar {
  position: relative;
  display: flex;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  height: 40px;
  padding: 0 12px;
  gap: 8px;
  transition: border-color 0.15s;
  margin-bottom: 10px;
}

.search-bar:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(45,106,224,0.12);
}

.search-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  color: var(--text-muted);
}

.search-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-size: 14px;
  color: var(--text);
}

.search-input::placeholder { color: var(--text-muted); }

.search-clear {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  line-height: 1;
  padding: 0 2px;
  display: flex;
  align-items: center;
  transition: color 0.1s;
}

.search-clear:hover { color: var(--text); }

/* ============================================================
   Filter pills
   ============================================================ */
.filters {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.filters-label {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
  margin-right: 2px;
}

.filter-pill-wrap {
  position: relative;
}

.filter-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 28px;
  padding: 0 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 99px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, color 0.15s;
  white-space: nowrap;
}

.filter-pill:hover {
  border-color: var(--border-strong);
  color: var(--text);
}

.filter-pill--active {
  background: var(--accent-bg);
  border-color: var(--accent);
  color: var(--accent);
}

.filter-pill--open {
  border-color: var(--accent);
}

.pill-chevron {
  width: 12px;
  height: 12px;
  flex-shrink: 0;
  opacity: 0.6;
}

.filter-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 300;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,0.08), 0 1px 4px rgba(0,0,0,0.04);
  min-width: 160px;
  max-height: 280px;
  overflow-y: auto;
  padding: 4px;
}

.filter-option {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 6px 10px;
  background: none;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  transition: background 0.1s;
}

.filter-option:hover { background: var(--surface-hover); }

.filter-option--active {
  color: var(--accent);
  font-weight: 500;
}

.filter-option--check {
  cursor: pointer;
}

.filter-option input[type="checkbox"] {
  width: 13px;
  height: 13px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.clear-filters-btn {
  background: none;
  border: none;
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0 4px;
  margin-left: 2px;
  transition: color 0.1s;
}

.clear-filters-btn:hover { color: var(--text-secondary); }

/* ============================================================
   Results list
   ============================================================ */
.results-meta {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
  margin-bottom: 8px;
  padding-left: 2px;
}

.results-list {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  overflow: hidden;
}

.sop-row {
  display: grid;
  grid-template-columns: 68px 1fr auto;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 16px;
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  text-align: left;
  cursor: pointer;
  transition: background 0.1s;
}

.sop-row:last-child { border-bottom: none; }
.sop-row:hover { background: var(--surface-hover); }

.sop-row-id {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  letter-spacing: 0.04em;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.sop-row-id:not(:empty) {
  display: inline-flex;
  align-items: center;
  height: 20px;
  padding: 0 7px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.sop-row-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
}

.sop-row-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
}

/* ============================================================
   Status pills
   ============================================================ */
.status-pill {
  display: inline-flex;
  align-items: center;
  height: 22px;
  padding: 0 8px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  letter-spacing: 0.01em;
}

.status-pill--sm {
  height: 20px;
  font-size: 11px;
  padding: 0 7px;
}

/* ============================================================
   Tags / meta pills
   ============================================================ */
.tag {
  display: inline-flex;
  align-items: center;
  height: 20px;
  padding: 0 7px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 11px;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
}

.tag--more {
  color: var(--text-muted);
  font-style: italic;
}

.recurrence {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

.meta-pill {
  display: inline-flex;
  align-items: center;
  height: 24px;
  padding: 0 9px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
}

.meta-pill--tool {
  background: #f0f4ff;
  border-color: #c8d8f8;
  color: #2d5cb8;
}

/* ============================================================
   Loading / empty / error
   ============================================================ */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 64px 24px;
  color: var(--text-muted);
  font-size: 14px;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

@keyframes spin { to { transform: rotate(360deg); } }

.loading-sop {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 120px 24px;
  flex-direction: column;
  gap: 16px;
  color: var(--text-muted);
}

.empty-state,
.error-state {
  padding: 64px 24px;
  text-align: center;
  color: var(--text-secondary);
}

.empty-state p,
.error-state p {
  margin-bottom: 16px;
  font-size: 14px;
}

/* ============================================================
   Buttons
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  white-space: nowrap;
  text-decoration: none !important;
}

.btn svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.55; cursor: not-allowed; }

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

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

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border-color: transparent;
}

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

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 0;
  margin-bottom: 24px;
  transition: color 0.1s;
}

.back-btn:hover { color: var(--text-secondary); }

.back-btn svg {
  width: 14px;
  height: 14px;
}

/* ============================================================
   SOP viewer — header
   ============================================================ */
.sop-header {
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--border);
}

.sop-header-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.sop-id-badge {
  display: inline-flex;
  align-items: center;
  height: 22px;
  padding: 0 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 11px;
  font-weight: 700;
  color: var(--text-secondary);
  letter-spacing: 0.05em;
  font-variant-numeric: tabular-nums;
}

.sop-title {
  font-size: 26px;
  font-weight: 700;
  line-height: 1.25;
  color: var(--text);
  margin-bottom: 16px;
  letter-spacing: -0.01em;
}

.sop-attrs {
  display: flex;
  flex-wrap: wrap;
  gap: 0;
  margin-bottom: 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
}

.attr-item {
  display: flex;
  flex-direction: column;
  padding: 10px 16px;
  border-right: 1px solid var(--border);
  min-width: 100px;
}

.attr-item:last-child { border-right: none; }

.attr-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 3px;
}

.attr-value {
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
}

.framework-badge {
  display: inline-flex;
  align-items: center;
  padding: 1px 7px;
  background: #fef3e2;
  color: #8a5a00;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 600;
}

.sop-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* ============================================================
   Change request form
   ============================================================ */
.change-request-form {
  margin-top: 14px;
  padding: 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.change-request-form textarea {
  width: 100%;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 13px;
  color: var(--text);
  background: var(--surface);
  outline: none;
  line-height: 1.5;
  transition: border-color 0.15s;
}

.change-request-form textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(45,106,224,0.1);
}

.change-request-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.comment-feedback {
  margin-top: 10px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
}

.comment-feedback--success {
  background: #eaf3de;
  color: #3b6d11;
}

.comment-feedback--error {
  background: #fcebeb;
  color: #a32d2d;
}

/* ============================================================
   SOP sections
   ============================================================ */
.sop-sections {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sop-section {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}

.sop-section summary { list-style: none; }
.sop-section summary::-webkit-details-marker { display: none; }

.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  cursor: pointer;
  user-select: none;
  transition: background 0.1s;
}

.section-header:hover { background: var(--surface-hover); }

.section-chevron {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  color: var(--text-muted);
  transition: transform 0.15s;
}

details.sop-section[open] > summary .section-chevron {
  transform: rotate(90deg);
}

.section-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.section-header--steps {
  cursor: default;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.section-header--steps .section-title {
  font-size: 13px;
  font-weight: 600;
}

.section-content {
  padding: 16px;
}

.sop-section--steps .section-content {
  padding: 0;
}

.empty-section {
  color: var(--text-muted);
  font-size: 13px;
  font-style: italic;
}

/* ============================================================
   Steps
   ============================================================ */
.steps-list {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.step-card {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
}

.step-card:last-child { border-bottom: none; }

.step-num {
  flex-shrink: 0;
  width: 48px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 18px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: 0.01em;
  border-right: 1px solid var(--border);
}

.step-body {
  flex: 1;
  padding: 14px 16px;
  min-width: 0;
}

.step-text {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  line-height: 1.5;
  margin-bottom: 0;
}

.step-children {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.step-intro {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.step-intro ul,
.step-intro ol {
  list-style: none;
  padding: 0;
  margin: 0;
}

.step-intro li {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.6;
  margin-bottom: 2px;
}

.step-intro .notion-p {
  color: var(--text-secondary);
}

/* ============================================================
   Notion block rendering
   ============================================================ */
.notion-p {
  font-size: 14px;
  color: var(--text);
  line-height: 1.6;
  margin-bottom: 6px;
}

.notion-p:last-child { margin-bottom: 0; }

.notion-spacer { height: 8px; }

.notion-h1 {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  margin: 16px 0 8px;
  line-height: 1.3;
}

.notion-h2 {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  margin: 12px 0 6px;
  line-height: 1.35;
}

.notion-h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 10px 0 4px;
}

.section-content ul,
.section-content ol,
.step-children ul,
.step-children ol {
  padding-left: 20px;
  margin: 4px 0;
}

.section-content li,
.step-children li {
  font-size: 14px;
  color: var(--text);
  line-height: 1.6;
  margin-bottom: 3px;
}

.notion-todo {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 3px 0;
  cursor: default;
}

.notion-todo input[type="checkbox"] {
  margin-top: 2px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.notion-todo span {
  font-size: 14px;
  color: var(--text);
  line-height: 1.5;
}

.notion-callout {
  display: flex;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  font-size: 13px;
  line-height: 1.5;
}

.callout-icon {
  font-size: 15px;
  flex-shrink: 0;
  line-height: 1.4;
}

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

.callout-check {
  background: #eaf3de;
  border-color: #c3dfa4;
  color: #3b6d11;
}

.callout-warning {
  background: #fff8e6;
  border-color: #f5d97a;
  color: #7a5200;
}

.callout-tip {
  background: #fff0e6;
  border-color: #f5c4a0;
  color: #7a3200;
}

.callout-info {
  background: #f0f4ff;
  border-color: #c8d8f8;
  color: #1e3a6e;
}

.notion-quote {
  border-left: 3px solid var(--border-strong);
  padding: 4px 0 4px 14px;
  color: var(--text-secondary);
  font-style: italic;
  font-size: 14px;
  margin: 8px 0;
}

.notion-code {
  background: #f4f4f2;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
  font-size: 12.5px;
  line-height: 1.6;
  overflow-x: auto;
  color: var(--text);
  margin: 8px 0;
}

.inline-code {
  font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
  font-size: 12px;
  background: #f0f0ee;
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 1px 4px;
  color: #c0392b;
}

.notion-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 12px 0;
}

.notion-toggle {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin: 4px 0;
}

.notion-toggle summary {
  list-style: none;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
}

.notion-toggle summary::-webkit-details-marker { display: none; }
.notion-toggle summary:hover { background: var(--surface-hover); }

.toggle-children {
  padding: 4px 10px 8px 20px;
}

.notion-image {
  margin: 8px 0;
}

.notion-image img {
  max-width: 100%;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.notion-image figcaption {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
  text-align: center;
}

.notion-table-wrap { overflow-x: auto; margin: 8px 0; }

.notion-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.notion-table th,
.notion-table td {
  padding: 7px 12px;
  border: 1px solid var(--border);
  text-align: left;
  line-height: 1.4;
}

.notion-table th {
  background: var(--bg);
  font-weight: 600;
  color: var(--text-secondary);
}

.notion-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin: 8px 0;
}

/* ============================================================
   Responsive
   ============================================================ */
@media (max-width: 640px) {
  .list-view, .sop-view { padding: 20px 16px 48px; }

  .sop-row {
    grid-template-columns: 56px 1fr;
    grid-template-rows: auto auto;
  }

  .sop-row-meta {
    grid-column: 1 / -1;
    flex-wrap: wrap;
  }

  .sop-attrs {
    flex-direction: column;
  }

  .attr-item {
    border-right: none;
    border-bottom: 1px solid var(--border);
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 8px 14px;
  }

  .attr-item:last-child { border-bottom: none; }

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

  .sop-title { font-size: 20px; }
}
