:root {
  --bg: #f3f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --border: #dbe3ee;
  --primary: #2563eb;
  --primary-soft: #eff6ff;
  --success: #0f766e;
  --success-soft: #ecfdf5;
  --danger: #b91c1c;
  --danger-soft: #fef2f2;
  --warning: #b45309;
  --warning-soft: #fffbeb;
  --mental: #047857;
  --mental-soft: #ecfdf5;
  --shadow: 0 8px 28px rgba(15, 23, 42, 0.08);
  --radius: 18px;
}

* { box-sizing: border-box; }

html {
  scroll-behavior: smooth;
  background: var(--bg);
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button, select, input, textarea { font: inherit; }
button { touch-action: manipulation; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 50;
  padding-top: env(safe-area-inset-top);
  background: rgba(11, 18, 32, 0.96);
  backdrop-filter: blur(12px);
  color: #fff;
}

.header-inner {
  max-width: 820px;
  margin: 0 auto;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.header-inner h1 {
  margin: 2px 0 0;
  font-size: 1.25rem;
  line-height: 1.2;
}

.eyebrow {
  margin: 0;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.app-header .eyebrow { color: #94a3b8; }

.app-shell {
  width: min(100%, 820px);
  margin: 0 auto;
  padding: 14px 12px calc(28px + env(safe-area-inset-bottom));
}

.card, .system-panel, .mental-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 14px;
}

.card { padding: 16px; }

.intro-card h2,
.output-card h2,
.system-panel h3,
.mental-card h2,
.mental-card h3 {
  margin: 4px 0 8px;
}

.intro-card p {
  margin: 8px 0 0;
  line-height: 1.5;
  color: #334155;
}

.note {
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--warning-soft);
  color: #7c2d12;
  font-size: 0.9rem;
  line-height: 1.45;
}

.system-nav {
  position: sticky;
  top: calc(67px + env(safe-area-inset-top));
  z-index: 40;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 9px 2px 10px;
  margin-bottom: 6px;
  background: linear-gradient(var(--bg) 85%, rgba(243, 246, 251, 0));
  scrollbar-width: none;
}

.system-nav::-webkit-scrollbar { display: none; }

.assessment-progress {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 2px 2px;
  color: var(--muted);
  font-size: .82rem;
  font-weight: 800;
}

.progress-abnormal {
  color: var(--danger);
}

.system-chip {
  flex: 0 0 auto;
  min-height: 42px;
  padding: 8px 13px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: #334155;
  font-weight: 800;
  cursor: pointer;
}

.system-status {
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  margin-left: 5px;
  border-radius: 999px;
  background: #e2e8f0;
  color: #64748b;
  font-size: .72rem;
  line-height: 1;
  vertical-align: middle;
}

.system-status:empty { display: none; }

.system-chip.is-started .system-status {
  background: #fef3c7;
  color: #92400e;
}

.system-chip.is-reviewed .system-status {
  background: #ccfbf1;
  color: #047857;
}

.system-chip.has-abnormal .system-status {
  background: #fee2e2;
  color: var(--danger);
}

.system-chip.active .system-status {
  background: rgba(255,255,255,.18);
  color: #fff;
}

.system-chip.active {
  background: var(--text);
  border-color: var(--text);
  color: #fff;
}

.system-chip.has-abnormal {
  border-color: #ef4444;
}

.system-chip.has-abnormal:not(.active) {
  background: var(--danger-soft);
  color: var(--danger);
}

.system-chip.has-selection:not(.active):not(.has-abnormal) {
  background: var(--success-soft);
  color: var(--success);
  border-color: #99f6e4;
}

.system-panel { overflow: hidden; }

.system-panel-header { padding: 16px 16px 10px; }

.system-panel-header h3 {
  margin: 4px 0 0;
  font-size: 1.2rem;
}

.system-panel-body { padding: 0 16px 16px; }

.quick-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(145px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}

.btn {
  min-height: 46px;
  padding: 10px 14px;
  border: 0;
  border-radius: 12px;
  font-weight: 800;
  cursor: pointer;
}

.btn-primary { background: var(--primary); color: #fff; }
.btn-secondary { background: #e2e8f0; color: #0f172a; }

.btn-ghost {
  border: 1px solid rgba(255,255,255,.22);
  background: rgba(255,255,255,.08);
  color: #fff;
}

.btn-small {
  min-height: 38px;
  padding: 7px 11px;
  font-size: 0.86rem;
}

.btn-normal {
  background: var(--success-soft);
  color: var(--success);
  border: 1px solid #99f6e4;
}

.btn-clear-system {
  background: var(--surface-2);
  color: #475569;
  border: 1px solid var(--border);
}

.btn-review {
  background: #fff;
  color: #475569;
  border: 1px solid var(--border);
}

.btn-review.is-reviewed {
  background: var(--success-soft);
  color: var(--success);
  border-color: #99f6e4;
}

.section-label {
  margin: 15px 0 8px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .06em;
}

.section-label:first-child { margin-top: 4px; }

.findings-grid {
  display: grid;
  gap: 8px;
}

.finding-wrap {
  border: 1px solid var(--border);
  border-radius: 13px;
  background: #fff;
  overflow: hidden;
}

.finding-wrap.selected {
  border-color: #93c5fd;
  background: var(--primary-soft);
}

.finding-wrap.abnormal.selected {
  border-color: #fca5a5;
  background: var(--danger-soft);
  order: -1;
}

.finding-wrap.has-incomplete,
.mental-option-wrap.has-incomplete {
  border-color: #f59e0b;
  box-shadow: 0 0 0 2px rgba(245, 158, 11, .12);
}

.required-marker {
  margin-left: 4px;
  color: var(--warning);
  font-size: .72rem;
  font-weight: 800;
}

.finding-option {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 50px;
  padding: 11px 12px;
  cursor: pointer;
  user-select: none;
}

.finding-option input {
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--primary);
  flex: 0 0 auto;
}

.finding-label {
  font-weight: 700;
  line-height: 1.25;
}

.finding-detail {
  padding: 0 12px 12px 42px;
}

.detail-row {
  margin-top: 8px;
}

.detail-row label {
  display: block;
  margin-bottom: 6px;
  font-size: .82rem;
  font-weight: 800;
  color: #475569;
}

.detail-row select,
.detail-row input {
  width: 100%;
  min-height: 44px;
  padding: 9px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  background: #fff;
  font-size: 16px;
}

.custom-finding { margin-top: 14px; }

.custom-finding label {
  display: block;
  margin-bottom: 7px;
  font-weight: 800;
  font-size: 0.9rem;
}

.custom-finding input {
  width: 100%;
  min-height: 48px;
  padding: 11px 12px;
  border: 1px solid #cbd5e1;
  border-radius: 12px;
  font-size: 16px;
}

.suggestion-chips {
  display: flex;
  gap: 7px;
  overflow-x: auto;
  margin: 0 0 8px;
  padding: 1px 1px 4px;
  scrollbar-width: none;
}

.suggestion-hint {
  margin: -2px 0 7px;
  color: var(--muted);
  font-size: .78rem;
  line-height: 1.35;
}

.suggestion-chips::-webkit-scrollbar { display: none; }

.suggestion-chip {
  flex: 0 0 auto;
  min-height: 36px;
  padding: 7px 10px;
  border: 1px solid #bfdbfe;
  border-radius: 999px;
  background: var(--primary-soft);
  color: #1d4ed8;
  font-size: .8rem;
  font-weight: 800;
  cursor: pointer;
}

.mental-card {
  overflow: hidden;
  border-color: #a7f3d0;
}

.mental-header {
  padding: 18px 16px;
  background: linear-gradient(135deg, #064e3b, #047857);
  color: #fff;
}

.mental-header .eyebrow { color: #a7f3d0; }

.mental-header p:last-child {
  margin: 8px 0 0;
  color: #d1fae5;
  line-height: 1.45;
}

.mental-body { padding: 16px; }

.mental-step {
  border: 1px solid #d1fae5;
  border-radius: 16px;
  background: #fbfffd;
  margin-bottom: 12px;
  overflow: hidden;
}

.mental-step-head {
  padding: 13px 14px;
  background: var(--mental-soft);
  border-bottom: 1px solid #d1fae5;
}

.mental-step-head h3 {
  margin: 2px 0 0;
  font-size: 1rem;
}

.mental-step-body { padding: 12px; }

.mental-review-button {
  width: 100%;
  margin-top: 12px;
}

.mental-grid {
  display: grid;
  gap: 8px;
}

.mental-option-wrap {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  overflow: hidden;
}

.mental-option-wrap.selected {
  border-color: #6ee7b7;
  background: #ecfdf5;
}

.mental-option-wrap.abnormal.selected {
  border-color: #fca5a5;
  background: var(--danger-soft);
  order: -1;
}

.mental-option {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 10px 11px;
  cursor: pointer;
}

.mental-option input {
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--mental);
}

.mental-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
  margin: 10px 0 14px;
}

.btn-mental {
  background: var(--mental);
  color: #fff;
}

.btn-mental-secondary {
  background: var(--mental-soft);
  color: #065f46;
  border: 1px solid #a7f3d0;
}

.review-box {
  margin: 0 0 12px;
  padding: 13px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface-2);
}

.review-box h3 {
  margin: 3px 0 9px;
  font-size: .98rem;
}

.review-groups {
  display: grid;
  gap: 8px;
}

.review-line {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  line-height: 1.35;
  font-size: .88rem;
}

.review-label {
  flex: 0 0 auto;
  font-weight: 800;
  min-width: 72px;
}

.review-value {
  color: #475569;
}

.review-value.abnormal {
  color: var(--danger);
  font-weight: 800;
}

.review-value.incomplete {
  color: var(--warning);
  font-weight: 800;
}

.modal-warning {
  margin-bottom: 12px;
  padding: 10px 12px;
  border: 1px solid #fcd34d;
  border-radius: 12px;
  background: var(--warning-soft);
  color: #92400e;
  font-size: .88rem;
  line-height: 1.4;
}

.output-heading {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}

.count-badge {
  flex: 0 0 auto;
  padding: 6px 9px;
  border-radius: 999px;
  background: #e2e8f0;
  color: #475569;
  font-size: 0.78rem;
  font-weight: 800;
}

#outputText {
  width: 100%;
  min-height: 240px;
  margin-top: 8px;
  padding: 13px;
  border: 1px solid #cbd5e1;
  border-radius: 13px;
  resize: vertical;
  background: #fff;
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}

.output-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-top: 10px;
}

.status {
  min-height: 1.25em;
  margin: 9px 0 0;
  color: var(--success);
  font-weight: 700;
  font-size: 0.9rem;
}

.app-footer {
  width: min(100%, 820px);
  margin: 0 auto;
  padding: 0 16px calc(18px + env(safe-area-inset-bottom));
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.4;
  text-align: center;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: end center;
  padding: 16px 12px calc(16px + env(safe-area-inset-bottom));
  background: rgba(15, 23, 42, .58);
  backdrop-filter: blur(4px);
}

.modal {
  width: min(100%, 520px);
  padding: 18px;
  border-radius: 20px;
  background: #fff;
  box-shadow: 0 20px 60px rgba(15,23,42,.3);
}

.modal h2 {
  margin: 4px 0 8px;
  font-size: 1.2rem;
}

.modal p {
  margin: 0 0 14px;
  color: #475569;
  line-height: 1.5;
}

.confirm-check {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  margin-bottom: 14px;
}

.confirm-check input {
  width: 20px;
  height: 20px;
  margin-top: 2px;
  accent-color: var(--primary);
}

.modal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.hidden { display: none !important; }

:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.35);
  outline-offset: 2px;
}

@media (min-width: 640px) {
  .app-shell { padding: 20px 16px 36px; }
  .findings-grid, .mental-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .output-actions { grid-template-columns: 1fr 1fr; }
  .modal-backdrop { place-items: center; }
}
