:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #d8dde3;
  --border-strong: #b6bdc6;
  --text: #1c2530;
  --text-muted: #5b6573;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --positive: #16a34a;
  --positive-bg: #ecfdf5;
  --concerning: #dc2626;
  --concerning-bg: #fef2f2;
  --moderate: #d97706;
  --moderate-bg: #fff7ed;
  --selected: #1d4ed8;
  --selected-bg: #dbeafe;
  --danger: #b91c1c;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 4px 12px rgba(15, 23, 42, 0.04);
  --radius: 8px;
  --radius-sm: 6px;
  --gap: 12px;
  --gap-sm: 6px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 {
  font-size: 18px;
  margin: 0;
  letter-spacing: -0.01em;
}
.tagline {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}
.topbar-controls {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  flex-wrap: wrap;
}
.control {
  display: flex;
  flex-direction: column;
  font-size: 11px;
  color: var(--text-muted);
}
.control select {
  font-size: 13px;
  padding: 6px 8px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  min-width: 160px;
}
.mic-btn {
  height: 34px;
  width: 38px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 16px;
  align-self: flex-end;
}
.mic-btn[aria-pressed="true"] {
  background: var(--concerning-bg);
  border-color: var(--concerning);
  color: var(--concerning);
}

/* Layout */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 460px;
  gap: 16px;
  padding: 16px;
  min-height: calc(100vh - 64px);
}
.input-pane {
  background: transparent;
  overflow: visible;
}
.output-pane {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: sticky;
  top: 80px;
  align-self: start;
  max-height: calc(100vh - 96px);
}
@media (max-width: 1000px) {
  .layout { grid-template-columns: 1fr; }
  .output-pane { position: static; max-height: none; }
}

/* Loading */
.loading {
  padding: 24px;
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  text-align: center;
  color: var(--text-muted);
}

/* Sections */
.section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.section[open] .section-summary {
  border-bottom: 1px solid var(--border);
}
.section-summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 16px;
  font-weight: 600;
  font-size: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  user-select: none;
}
.section-summary::-webkit-details-marker { display: none; }
.section-summary::before {
  content: "▸";
  color: var(--text-muted);
  margin-right: 8px;
  transition: transform 0.15s;
  display: inline-block;
}
.section[open] .section-summary::before {
  transform: rotate(90deg);
}
.section-title { flex: 1; }
.section-count {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 400;
  background: var(--surface-2);
  border-radius: 10px;
  padding: 2px 8px;
}
.section-count:empty { display: none; }
.section-body {
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Groups */
.group {
  border: none;
  margin: 0;
  padding: 0;
}
.group-legend {
  font-weight: 600;
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0 0 6px 0;
  display: flex;
  gap: 8px;
  align-items: baseline;
}
.group-hint {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text-muted);
  font-size: 11px;
}
.chip-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

/* Chips */
.chip {
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.1s, border-color 0.1s, transform 0.05s;
  font-family: inherit;
  text-align: left;
  line-height: 1.3;
}
.chip:hover {
  border-color: var(--accent);
}
.chip:active {
  transform: translateY(1px);
}
.chip[aria-pressed="true"] {
  background: var(--selected-bg);
  border-color: var(--selected);
  color: var(--selected);
  font-weight: 500;
}
.chip[data-tone="positive"] { border-color: #86efac; }
.chip[data-tone="positive"][aria-pressed="true"] { background: var(--positive-bg); border-color: var(--positive); color: var(--positive); }
.chip[data-tone="concerning"] { border-color: #fca5a5; }
.chip[data-tone="concerning"][aria-pressed="true"] { background: var(--concerning-bg); border-color: var(--concerning); color: var(--concerning); }
.chip[data-tone="moderate"] { border-color: #fdba74; }
.chip[data-tone="moderate"][aria-pressed="true"] { background: var(--moderate-bg); border-color: var(--moderate); color: var(--moderate); }

/* Numeric rows (exercises) */
.numeric-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 6px;
  font-size: 11px;
  color: var(--text-muted);
}
.numeric-row label {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.numeric-row input {
  font-size: 13px;
  width: 70px;
  padding: 4px 6px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
}
.numeric-row input[type="text"] { width: 90px; }

/* Free numerics (pain level, distance, TUG) */
.free-numerics {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  padding-top: 8px;
}
.free-numerics label {
  display: flex;
  flex-direction: column;
  font-size: 11px;
  color: var(--text-muted);
  gap: 2px;
}
.free-numerics input {
  font-size: 13px;
  width: 100px;
  padding: 4px 6px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
}

/* Output pane */
.output-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.output-header h2 {
  font-size: 14px;
  margin: 0;
}
.output-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 11px;
  color: var(--text-muted);
}
.word-count {
  background: var(--surface-2);
  padding: 2px 8px;
  border-radius: 10px;
}
.compliance {
  display: inline-block;
  min-width: 18px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 11px;
  cursor: default;
}
.compliance-neutral { background: var(--surface-2); color: var(--text-muted); }
.compliance-green { background: var(--positive-bg); color: var(--positive); }
.compliance-yellow { background: var(--moderate-bg); color: var(--moderate); }
.compliance-red { background: var(--concerning-bg); color: var(--concerning); cursor: pointer; }

.output-body {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  overflow-y: auto;
  white-space: pre-wrap;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  min-height: 240px;
}
.output-body:empty::before {
  content: "Select chips and click Generate.";
  color: var(--text-muted);
}
.output-body:focus { outline: 2px solid var(--accent); outline-offset: -2px; }

.compliance-detail {
  background: var(--concerning-bg);
  border: 1px solid var(--concerning);
  color: var(--concerning);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 12px;
}
.compliance-detail ul { margin: 4px 0 0 18px; padding: 0; }

.output-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.output-actions button {
  font-family: inherit;
  font-size: 12px;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.output-actions button:hover { background: var(--surface-2); }
.output-actions button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.output-actions button.primary:hover { background: #1d4ed8; }
.output-actions button.danger { color: var(--danger); }

/* History dialog */
dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 22px;
  min-width: 420px;
  max-width: 600px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.15);
}
dialog::backdrop { background: rgba(15,23,42,0.4); }
dialog h3 { margin: 0 0 4px 0; }
dialog .hint { font-size: 11px; color: var(--text-muted); margin: 0 0 12px 0; }
#history-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 320px;
  overflow-y: auto;
}
#history-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  gap: 8px;
}
#history-list li:last-child { border-bottom: none; }
#history-list .history-meta { font-size: 11px; color: var(--text-muted); }
#history-list button {
  font-size: 11px;
  padding: 4px 8px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  border-radius: 4px;
  cursor: pointer;
}
.dialog-actions { display: flex; justify-content: flex-end; margin-top: 14px; }

/* Hidden when payer model doesn't match */
[data-payer]:not(.match-payer) { display: none; }

/* Mic transcript indicator */
.mic-transcript {
  position: fixed;
  bottom: 16px;
  left: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  box-shadow: var(--shadow);
  max-width: 400px;
  font-size: 12px;
  color: var(--text-muted);
  z-index: 20;
}
.mic-transcript strong { color: var(--text); }
