:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #1d232b;
  --muted: #6a7280;
  --line: #d9dee7;
  --brand: #246b55;
  --brand-dark: #185341;
  --accent: #c7782a;
  --danger: #b23b3b;
  --shadow: 0 12px 30px rgba(28, 38, 52, 0.08);
  --ui-scale: 1;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: "Microsoft JhengHei", "Noto Sans TC", Arial, sans-serif;
}

main {
  font-size: calc(16px * var(--ui-scale));
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  min-height: calc(38px * var(--ui-scale));
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 8px 12px;
  cursor: pointer;
}

button:focus,
input:focus,
select:focus,
textarea:focus {
  outline: 2px solid rgba(36, 107, 85, 0.25);
  outline-offset: 2px;
}

input,
select,
textarea {
  width: 100%;
  min-height: calc(38px * var(--ui-scale));
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 8px 10px;
}

textarea {
  min-height: 38px;
  resize: vertical;
}

.auto-grow {
  overflow: hidden;
  line-height: 1.5;
  resize: vertical;
}

label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.875em;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 22px 28px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.topbar h1,
.topbar p {
  margin: 0;
}

.eyebrow {
  color: var(--brand);
  font-size: 13px;
  font-weight: 700;
}

.topbar-action {
  min-width: 96px;
}

.shell {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  gap: 20px;
  padding: 20px;
}

.sidebar,
.empty,
.workspace {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.sidebar {
  align-self: start;
  display: grid;
  gap: 12px;
  padding: 16px;
}

.primary {
  background: var(--brand);
  color: #fff;
}

.primary:hover {
  background: var(--brand-dark);
}

.secondary {
  background: #eef3f1;
  color: var(--brand-dark);
  border-color: #d2dfda;
}

.danger {
  background: #fff2f2;
  color: var(--danger);
  border-color: #f0cccc;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.small {
  min-height: calc(32px * var(--ui-scale));
  padding: 6px 10px;
}

.empty {
  min-height: 420px;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 12px;
  text-align: center;
  padding: 40px;
}

.empty h2,
.empty p {
  margin: 0;
}

.empty p {
  color: var(--muted);
}

.workspace {
  overflow: hidden;
}

.form-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 14px;
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.form-title-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
}

.title-field {
  min-width: 0;
}

.tabs {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px 0;
}

.tab {
  background: transparent;
  color: var(--muted);
  border-bottom: 3px solid transparent;
  border-radius: 0;
}

.tab.active {
  color: var(--brand-dark);
  border-bottom-color: var(--brand);
}

.font-scale-control {
  display: flex;
  align-items: center;
  width: min(220px, 35vw);
  margin: 0 0 0 auto;
}

.font-scale-control input {
  width: 100%;
  min-height: 26px;
  padding: 0;
  accent-color: var(--brand);
}

.secondary.active {
  background: var(--brand);
  color: #fff;
  border-color: var(--brand);
}

.pane {
  padding: 16px;
}

.band {
  padding: 18px 0;
  border-top: 1px solid var(--line);
}

.pane .band:first-child {
  border-top: 0;
  padding-top: 0;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.section-title h2,
.section-title h3 {
  margin: 0;
}

.compact {
  margin-top: 18px;
}

.field-list,
.rows-editor,
.entry-grid {
  display: grid;
  gap: 10px;
}

.signature-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(180px, 1fr));
  gap: 10px;
}

.muted-text,
.empty-cell {
  color: var(--muted);
}

.empty-cell {
  text-align: center;
}

.field-card {
  display: grid;
  grid-template-columns: 42px minmax(160px, 1fr) 140px minmax(180px, 1fr) 76px;
  gap: 10px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
}

.drag-handle {
  min-height: 38px;
  padding: 0;
  background: #fff;
  color: var(--muted);
  border-color: var(--line);
  cursor: grab;
}

.drag-handle:active {
  cursor: grabbing;
}

.field-card.dragging,
.check-row.dragging {
  opacity: 0.55;
}

.field-card.drag-over,
.check-row.drag-over {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(36, 107, 85, 0.14);
}

.field-options[hidden] {
  display: none;
}

.check-row {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(180px, 1fr) 76px;
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.table-wrap {
  overflow-x: auto;
}

.entry-table {
  width: 100%;
  min-width: 780px;
  border-collapse: collapse;
  background: #fff;
}

.entry-table th,
.entry-table td {
  border: 1px solid var(--line);
  padding: 8px;
  vertical-align: top;
}

.entry-table th {
  background: #eef3f1;
  text-align: left;
}

.question-pager {
  display: grid;
  gap: 12px;
}

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

.question-menu {
  position: relative;
}

.question-menu summary {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  padding: 6px 10px;
  border: 1px solid #d2dfda;
  border-radius: 6px;
  background: #eef3f1;
  color: var(--brand-dark);
  cursor: pointer;
  list-style: none;
}

.question-menu summary::-webkit-details-marker {
  display: none;
}

.question-list {
  display: none;
}

.question-menu[open] .question-list {
  position: absolute;
  left: 0;
  top: 40px;
  z-index: 5;
  display: grid;
  gap: 4px;
  width: min(320px, calc(100vw - 56px));
  max-height: min(420px, 70vh);
  overflow-y: auto;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--shadow);
}

.question-list button {
  width: 100%;
  min-height: 36px;
  border-color: transparent;
  background: transparent;
  color: var(--ink);
  text-align: left;
}

.question-list button:hover,
.question-list button.active {
  background: #eef3f1;
  color: var(--brand-dark);
}

.question-count {
  color: var(--muted);
  font-size: 0.875em;
  line-height: calc(34px * var(--ui-scale));
  white-space: nowrap;
}

.question-card {
  display: grid;
  gap: 14px;
  min-height: 280px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  touch-action: pan-y;
}

.question-card h3 {
  margin: 0;
  font-size: 1.25em;
}

.question-static,
.question-answers {
  display: grid;
  gap: 10px;
}

.question-static > div {
  display: grid;
  gap: 4px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f8faf9;
}

.question-static span {
  color: var(--muted);
  font-size: 0.8125em;
}

.question-static strong {
  font-weight: 600;
  line-height: 1.5;
}

.question-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.sheet {
  max-width: 920px;
  margin: 0 auto;
  background: #fff;
  border: 1px solid #222;
  padding: 22px;
}

.sheet h2 {
  text-align: center;
  margin: 0 0 14px;
}

.sheet table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 16px;
}

.sheet th,
.sheet td {
  border: 1px solid #222;
  padding: 8px;
  min-height: 38px;
}

.sheet th {
  background: #f2f2f2;
}

.sheet .kv-table {
  table-layout: fixed;
}

.sheet .kv-table th {
  width: calc((var(--label-chars, 6) * 1.15em) + 18px);
  max-width: calc((8 * 1.15em) + 18px);
  text-align: center;
  white-space: normal;
  word-break: break-all;
  overflow-wrap: anywhere;
}

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(29, 35, 43, 0.42);
  padding: 20px;
  z-index: 20;
}

.modal-panel {
  width: min(420px, 100%);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 18px;
}

.modal-panel h2 {
  margin: 0 0 14px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 14px;
}

.error-text {
  margin: 8px 0 0;
  color: var(--danger);
  font-size: 0.875em;
}

.hidden {
  display: none !important;
}

@media (max-width: 840px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

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

  .form-head,
  .form-title-row,
  .field-card,
  .check-row,
  .signature-grid {
    grid-template-columns: 1fr;
  }
}
