:root {
  --ink: #1a2421;
  --muted: #5c6b66;
  --paper: #f3efe6;
  --paper-2: #ebe4d6;
  --card: #fffdf8;
  --line: #d4cbb8;
  --accent: #1e6b6b;
  --accent-2: #c45c3a;
  --accent-soft: #dceceb;
  --ok: #2a7a4b;
  --warn: #b8860b;
  --radius: 10px;
  --shadow: 0 10px 30px rgba(26, 36, 33, 0.08);
  --font-display: "Sarabun", "Noto Sans Thai", sans-serif;
  --font-body: "Sarabun", "Noto Sans Thai", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background:
    radial-gradient(circle at top left, rgba(30, 107, 107, 0.08), transparent 40%),
    linear-gradient(180deg, var(--paper) 0%, var(--paper-2) 100%);
  color: var(--ink);
  font-family: var(--font-body);
  min-height: 100%;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.site-header {
  background: linear-gradient(120deg, #163f3f 0%, #1e6b6b 55%, #2a8a7a 100%);
  color: #f7f4ee;
  padding: 1.1rem 1.5rem;
  box-shadow: var(--shadow);
}

.site-header .brand {
  display: flex;
  align-items: baseline;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.site-header .brand-code {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.site-header .brand-title {
  font-size: 1.05rem;
  opacity: 0.95;
}

.site-header .brand-sub {
  width: 100%;
  font-size: 0.9rem;
  opacity: 0.8;
  margin-top: 0.15rem;
}

.container {
  width: min(1100px, calc(100% - 2rem));
  margin: 1.5rem auto 3rem;
}

.messages { list-style: none; padding: 0; margin: 0 0 1rem; }
.messages li {
  background: var(--accent-soft);
  border: 1px solid #b7d4d1;
  color: #134848;
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 0.5rem;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.4rem;
  margin-bottom: 1.25rem;
}

.card h1, .card h2, .card h3 {
  margin-top: 0;
  font-family: var(--font-display);
}

.card h1 { font-size: 1.45rem; }
.card h2 {
  font-size: 1.15rem;
  border-bottom: 1px solid var(--line);
  padding-bottom: 0.45rem;
  margin-bottom: 1rem;
}

.toolbar {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.steps {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 0.4rem;
  margin-bottom: 1.25rem;
}

.steps a, .steps span {
  display: block;
  text-align: center;
  padding: 0.55rem 0.4rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  font-size: 0.82rem;
  text-decoration: none;
}

.steps .active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  border: none;
  border-radius: 8px;
  padding: 0.65rem 1.1rem;
  font: inherit;
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  text-decoration: none;
}

.btn:hover { filter: brightness(1.05); text-decoration: none; }
.btn.secondary { background: #4a5a56; }
.btn.ghost {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
}
.btn.danger { background: var(--accent-2); }

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.9rem 1.1rem;
}

.form-grid .full { grid-column: 1 / -1; }

.field label {
  display: block;
  font-weight: 600;
  margin-bottom: 0.3rem;
  font-size: 0.95rem;
}

.field .help {
  color: var(--muted);
  font-size: 0.82rem;
  margin-top: 0.2rem;
}

.field input[type="text"],
.field input[type="number"],
.field input[type="date"],
.field select,
.field textarea,
.num-input,
.score-select,
.note-input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.55rem 0.7rem;
  font: inherit;
  background: #fff;
}

.field textarea, .note-input { resize: vertical; }

/* Speech-to-text mic control on note / problems textareas */
.stt-wrap {
  position: relative;
  width: 100%;
}

.stt-wrap textarea {
  width: 100%;
}

.stt-toolbar {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  margin-top: 0.35rem;
  flex-wrap: wrap;
}

.stt-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.28rem 0.7rem;
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  background: #f7f4ee;
  color: var(--ink, #1c2a26);
  line-height: 1.2;
}

.stt-btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.stt-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.stt-btn.is-listening {
  background: var(--accent-2, #c45c4a);
  border-color: var(--accent-2, #c45c4a);
  color: #fff;
  animation: stt-pulse 1.2s ease-in-out infinite;
}

.stt-icon {
  width: 1rem;
  height: 1rem;
  fill: currentColor;
  flex-shrink: 0;
}

.stt-status {
  font-size: 0.8rem;
  color: var(--muted);
}

@keyframes stt-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.72; }
}

.checkbox-list, .radio-list {
  display: grid;
  gap: 0.35rem;
  margin-top: 0.35rem;
}

.checkbox-list label, .radio-list label {
  font-weight: 400;
  display: flex;
  gap: 0.45rem;
  align-items: flex-start;
}

.section-block {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 1rem;
  overflow: hidden;
}

.section-head {
  padding: 0.7rem 1rem;
  color: #fff;
  font-weight: 700;
}

.question-row {
  padding: 0.9rem 1rem;
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr 180px;
  gap: 0.75rem 1rem;
  align-items: start;
}

.question-row .q-text {
  font-weight: 600;
  margin-bottom: 0.35rem;
}

.question-row .note-wrap { grid-column: 1 / -1; }

.metric-row {
  display: grid;
  grid-template-columns: 1.4fr 1fr 180px;
  gap: 0.75rem;
  padding: 0.85rem 0;
  border-bottom: 1px solid var(--line);
  align-items: end;
}

.metric-row .cat {
  color: var(--muted);
  font-size: 0.82rem;
}

.table {
  width: 100%;
  border-collapse: collapse;
}

.table th, .table td {
  border-bottom: 1px solid var(--line);
  padding: 0.65rem 0.4rem;
  text-align: left;
  vertical-align: top;
}

.table th { color: var(--muted); font-weight: 600; font-size: 0.9rem; }

.score-pill {
  display: inline-block;
  min-width: 3rem;
  text-align: center;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: #134848;
  font-weight: 700;
}

.charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.chart-box {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
}

.chart-box h3 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
  text-align: center;
}

.chart-box canvas {
  width: 100% !important;
  max-height: 360px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.8rem;
}

.summary-item {
  background: linear-gradient(180deg, #fff, #f7f3ea);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
}

.summary-item .label { color: var(--muted); font-size: 0.9rem; }
.summary-item .value {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--accent);
  margin-top: 0.2rem;
}

.empty {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--muted);
}

.r02-form-body {
  font-size: 0.98rem;
  line-height: 1.5;
}
.r02-form-line {
  display: flex;
  align-items: flex-end;
  gap: 0.6rem;
  margin-bottom: 0.85rem;
}
.r02-form-label {
  font-weight: 700;
  white-space: nowrap;
  color: var(--ink);
}
.r02-form-fill {
  flex: 1;
  border-bottom: 1px solid #333;
  min-height: 1.35em;
  padding: 0 0.25rem 0.1rem;
}
.r02-form-fill.narrow {
  flex: 0 0 auto;
  min-width: 1.6rem;
  text-align: center;
}
.r02-form-fill.month {
  flex: 0 0 5.2rem;
  min-width: 4.5rem;
}
.r02-form-fill.man-day-val {
  flex: 0 0 3.2rem;
  text-align: center;
}
.r02-form-label.man-day-unit {
  white-space: nowrap;
  flex-shrink: 0;
}

.r02-sign-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
.r02-sign-box {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 1rem 1.1rem 1.25rem;
  text-align: center;
  background: #fff;
}
.r02-sign-title {
  font-weight: 700;
  margin-bottom: 0.85rem;
  line-height: 1.35;
}
.r02-sign-title span {
  font-weight: 400;
  color: var(--muted);
  font-size: 0.92rem;
}
.r02-sign-label {
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
.r02-sign-line {
  width: min(85%, 260px);
  height: 2.4rem;
  margin: 0 auto 0.35rem;
  border-bottom: 1px solid #333;
}
.r02-sign-name {
  margin-top: 0.35rem;
  font-size: 1rem;
}
.r02-sign-date {
  margin-top: 0.65rem;
  color: var(--muted);
  font-size: 0.95rem;
}
@media (max-width: 860px) {
  .r02-sign-grid { grid-template-columns: 1fr; }
}
.r02-form-fill.block {
  display: block;
  width: 100%;
  margin-bottom: 0.45rem;
  flex: none;
}
.r02-form-meta {
  display: flex;
  flex-wrap: nowrap;
  align-items: flex-end;
  gap: 0.18rem 0.28rem;
  margin-bottom: 1rem;
  font-size: 0.86rem;
  overflow-x: auto;
}
.r02-form-meta-oneline .r02-form-label {
  letter-spacing: -0.02em;
}
.r02-form-people {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.25rem;
  margin-top: 0.5rem;
}
@media (max-width: 860px) {
  .r02-form-people { grid-template-columns: 1fr; }
}
.r02-doc-header {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.85rem 1rem 1rem;
  margin-bottom: 0.25rem;
}
.r02-doc-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 0.65rem;
}
.r02-doc-banner {
  color: #8a8a8a;
  font-size: 0.82rem;
  line-height: 1.35;
  max-width: 70%;
}
.r02-doc-badge {
  border: 2px solid #e07a2f;
  color: #1a1a1a;
  font-weight: 700;
  font-size: 0.95rem;
  padding: 0.28rem 0.7rem;
  white-space: nowrap;
  background: #fff;
}
.r02-doc-main {
  display: grid;
  grid-template-columns: minmax(140px, 200px) 1fr;
  gap: 1rem;
  align-items: center;
}
.r02-doc-logo img {
  width: 100%;
  max-height: 110px;
  object-fit: contain;
  display: block;
}
.r02-doc-titles {
  text-align: center;
}
.r02-doc-title {
  font-size: 1.25rem;
  font-weight: 700;
  line-height: 1.35;
  color: var(--ink);
}
.r02-doc-project,
.r02-doc-year {
  margin-top: 0.2rem;
  font-size: 1.02rem;
  font-weight: 600;
  line-height: 1.35;
}

@media (max-width: 860px) {
  .form-grid,
  .charts,
  .summary-grid,
  .question-row,
  .metric-row,
  .steps {
    grid-template-columns: 1fr;
  }
  .steps a, .steps span { border-radius: 8px; }
  .r02-doc-main { grid-template-columns: 1fr; text-align: center; }
  .r02-doc-logo img { max-width: 180px; margin: 0 auto; }
  .r02-doc-top { flex-direction: column; align-items: stretch; }
  .r02-doc-banner { max-width: 100%; }
  .r02-doc-badge { align-self: flex-end; }
}

/* Help / คู่มือ */
.help-layout {
  display: grid;
  grid-template-columns: minmax(200px, 240px) 1fr;
  gap: 1.25rem;
  align-items: start;
}

.help-toc {
  position: sticky;
  top: 1rem;
}

.help-toc-list {
  margin: 0;
  padding-left: 1.25rem;
  line-height: 1.7;
}

.help-toc-list ul {
  margin: 0.15rem 0 0.35rem;
  padding-left: 1.1rem;
  list-style: disc;
}

.help-body h3 {
  margin: 1.1rem 0 0.4rem;
  font-size: 1.05rem;
  color: var(--accent);
}

.help-toc-list a {
  color: var(--ink);
}

.help-toc-list a:hover {
  color: var(--accent);
}

.help-section h2 {
  margin-top: 0;
}

.help-body .card {
  margin-bottom: 1rem;
}

.help-body ul,
.help-body ol {
  line-height: 1.65;
}

@media (max-width: 860px) {
  .help-layout {
    grid-template-columns: 1fr;
  }
  .help-toc {
    position: static;
  }
}

/* Section heading with action button */
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-top: 1.25rem;
}
.section-head h2 {
  margin: 0;
}

/* Modal / popup */
body.modal-open {
  overflow: hidden;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
  padding: 0.75rem;
  background: rgba(26, 36, 33, 0.45);
}

.modal-overlay[hidden] {
  display: none !important;
}

.modal-dialog {
  width: 100%;
  max-width: none;
  height: calc(100vh - 1.5rem);
  max-height: none;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.25rem 1.25rem;
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  flex-shrink: 0;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.2rem;
}

.modal-close {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.1rem 0.35rem;
}

.modal-close:hover {
  color: var(--ink);
}

.modal-hint {
  color: var(--muted);
  margin: 0.55rem 0 0.75rem;
  font-size: 0.92rem;
  flex-shrink: 0;
}

.modal-textarea {
  display: block;
  width: 100% !important;
  max-width: none !important;
  box-sizing: border-box;
  align-self: stretch;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  height: auto;
  resize: none;
  font: inherit;
  line-height: 1.55;
  padding: 0.75rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  justify-content: flex-end;
  margin-top: 0.9rem;
  flex-shrink: 0;
}

.sortable-hint {
  color: var(--muted);
  font-size: 0.9rem;
  margin: -0.35rem 0 0.85rem;
}

.sortable-list {
  display: flex;
  flex-direction: column;
}

.sortable-item-head {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  margin-bottom: 0.65rem;
}

.sortable-handle {
  touch-action: none;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  border-radius: 8px;
  width: 2.15rem;
  height: 2.15rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
}

.sortable-handle:active {
  cursor: grabbing;
}

.sortable-grip {
  width: 0.72rem;
  height: 0.72rem;
  background-image: radial-gradient(currentColor 1.15px, transparent 1.3px);
  background-size: 0.36rem 0.36rem;
  background-position: center;
}

.sortable-item.is-dragging {
  opacity: 0.72;
  box-shadow: 0 12px 28px rgba(26, 36, 33, 0.16);
  z-index: 5;
}
