:root {
  color-scheme: light;
  --bg: #fff6fb;
  --paper: #fffefd;
  --surface: rgba(255, 255, 255, 0.84);
  --surface-strong: #ffffff;
  --ink: #403248;
  --muted: #8d7893;
  --line: #ecd4e7;
  --line-strong: #d7accb;
  --pink: #ff8fbc;
  --pink-strong: #ef5d9c;
  --blue: #96d8ff;
  --blue-strong: #48a7df;
  --lavender: #cdb6ff;
  --mint: #b9eddc;
  --yellow: #ffe49e;
  --danger: #ff6f8f;
  --shadow: 0 22px 70px rgba(198, 126, 177, 0.22);
  --radius: 12px;
  --cell: clamp(42px, 8.7vw, 64px);
  font-family: "Trebuchet MS", "Noto Sans Thai", "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  overflow-x: hidden;
  width: 100%;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 12% 8%, rgba(255, 228, 158, 0.68), transparent 26rem),
    radial-gradient(circle at 88% 18%, rgba(185, 237, 220, 0.66), transparent 24rem),
    linear-gradient(135deg, #fff6fb 0%, #f4f0ff 52%, #edfaff 100%);
  color: var(--ink);
}

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

button {
  cursor: pointer;
  touch-action: manipulation;
}

.app-shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: clamp(14px, 2.6vw, 30px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 12px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--pink), var(--lavender));
  color: white;
  font-size: 28px;
  box-shadow: 0 14px 34px rgba(239, 93, 156, 0.27);
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(24px, 4.2vw, 38px);
  line-height: 1;
  letter-spacing: 0;
}

.brand p,
.panel-head p {
  color: var(--muted);
  font-size: 14px;
  margin-top: 4px;
}

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

.icon-button,
.tiny-button,
.segmented button {
  min-height: 42px;
  border: 0;
  border-radius: var(--radius);
  padding: 0 14px;
  color: var(--ink);
  transition: transform 0.16s ease, box-shadow 0.16s ease, background 0.16s ease;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 800;
  min-width: 0;
  white-space: nowrap;
}

.icon-button:hover,
.tiny-button:hover,
.segmented button:hover {
  transform: translateY(-1px);
}

.icon-button.primary {
  background: linear-gradient(135deg, var(--pink), #ffaed0);
  color: white;
  box-shadow: 0 12px 28px rgba(239, 93, 156, 0.25);
}

.icon-button.soft,
.tiny-button {
  background: rgba(255, 255, 255, 0.84);
  border: 1px solid var(--line);
}

.icon-button.danger {
  background: linear-gradient(135deg, var(--danger), #ff9eb4);
  color: white;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 18px;
  align-items: start;
  min-width: 0;
}

.board-panel,
.setup-card,
.answers-card,
.save-card,
.notes-card {
  background: var(--surface);
  border: 1px solid rgba(236, 212, 231, 0.9);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}

.board-panel,
.side-panel {
  min-width: 0;
}

.board-panel {
  padding: clamp(14px, 2.2vw, 22px);
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.94), rgba(255, 249, 253, 0.86)),
    radial-gradient(circle at 20% 0%, rgba(255, 228, 238, 0.8), transparent 18rem);
}

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

h2 {
  font-size: 20px;
  line-height: 1.2;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(2, minmax(72px, 1fr));
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.72);
}

.segmented button {
  min-height: 36px;
  background: transparent;
  font-weight: 800;
  color: var(--muted);
}

.segmented button.active {
  background: linear-gradient(135deg, #ffffff, #ffe6f1);
  color: var(--pink-strong);
  box-shadow: 0 8px 20px rgba(239, 93, 156, 0.14);
}

.case-name-row {
  display: grid;
  grid-template-columns: auto minmax(140px, 1fr) auto auto;
  align-items: center;
  gap: 10px;
  margin: 18px 0 12px;
}

.case-name-row > input {
  min-width: 0;
}

.overview-button,
.lock-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 42px;
  border: 1px solid #f7bfd7;
  border-radius: 999px;
  padding: 0 15px;
  background: linear-gradient(135deg, #fff, #ffeaf4);
  color: var(--pink-strong);
  font-weight: 900;
  box-shadow: 0 10px 24px rgba(239, 93, 156, 0.12);
}

.lock-button {
  border-color: #d5c0ff;
  background: linear-gradient(135deg, #fff, #f3ecff);
  color: #7659b8;
}

.lock-button.is-locked {
  border-color: #ff9ec4;
  background: linear-gradient(135deg, #ff8fbc, #ffaed0);
  color: #fff;
}

label,
.category-label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.92);
  color: var(--ink);
  outline: 0;
}

input,
select {
  min-height: 42px;
  padding: 8px 11px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--pink);
  box-shadow: 0 0 0 4px rgba(255, 143, 188, 0.18);
}

.board-wrap {
  overflow: auto;
  padding: 16px 10px 10px;
  max-width: 100%;
  overscroll-behavior-x: contain;
  border: 1px solid rgba(236, 212, 231, 0.76);
  border-radius: 18px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.74), rgba(255, 255, 255, 0.42)),
    radial-gradient(circle at 0% 0%, rgba(255, 214, 231, 0.42), transparent 16rem);
}

.board-wrap.is-locked {
  border-color: rgba(255, 143, 188, 0.82);
  background:
    linear-gradient(180deg, rgba(255, 246, 251, 0.9), rgba(255, 255, 255, 0.56)),
    repeating-linear-gradient(-45deg, rgba(255, 143, 188, 0.08), rgba(255, 143, 188, 0.08) 8px, transparent 8px, transparent 18px);
}

.board-wrap.is-locked .cell:not(.blank) {
  pointer-events: none;
  opacity: 0.72;
}

.logic-board {
  display: grid;
  width: max-content;
  min-width: 100%;
  align-items: start;
  justify-content: start;
  padding: 10px 0 18px;
  filter: drop-shadow(0 12px 24px rgba(177, 122, 169, 0.12));
}

.col-headers,
.col-group-labels,
.board-row {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: var(--cell);
  gap: 2px;
}

.corner-space {
  width: calc(var(--cell) * 2.05);
}

.col-headers,
.col-group-labels {
  margin-left: calc(var(--cell) * 2.05);
}

.col-group-labels {
  align-items: end;
  margin-bottom: 2px;
}

.col-group {
  display: grid;
  place-items: center;
  min-height: 28px;
  color: #b061c9;
  font-size: 13px;
  font-weight: 900;
  text-align: center;
  border-radius: 999px 999px 6px 6px;
  background: linear-gradient(135deg, rgba(255, 235, 246, 0.95), rgba(242, 233, 255, 0.92));
  border: 1px solid rgba(211, 178, 242, 0.78);
  box-shadow: 0 8px 18px rgba(188, 132, 211, 0.12);
}

.top-item,
.row-item {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink);
  font-size: 12px;
  font-weight: 900;
  line-height: 1.15;
  text-align: center;
  word-break: break-word;
}

.top-item {
  height: 64px;
  padding: 4px;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  border-radius: 999px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.94), rgba(250, 241, 255, 0.92));
  border: 1px solid rgba(226, 201, 240, 0.8);
  color: #6f4a91;
}

.row-shell {
  display: grid;
  grid-template-columns: calc(var(--cell) * 2.05) auto;
  align-items: stretch;
}

.row-item {
  min-height: var(--cell);
  justify-content: flex-end;
  padding-right: 10px;
  flex-direction: column;
  gap: 2px;
  border-radius: 999px 8px 8px 999px;
  background: linear-gradient(135deg, rgba(246, 252, 255, 0.95), rgba(255, 241, 248, 0.9));
  border: 1px solid rgba(214, 226, 247, 0.7);
}

.row-group-title {
  color: var(--blue-strong);
  font-size: 11px;
  line-height: 1;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  border: 1px solid rgba(215, 172, 203, 0.68);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.82);
  color: transparent;
  display: grid;
  place-items: center;
  font-size: 23px;
  font-weight: 900;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.82);
  transition: transform 0.12s ease, background 0.12s ease, box-shadow 0.12s ease;
}

.cell:not(.blank):active {
  transform: scale(0.94);
}

.cell.group-edge-left {
  box-shadow:
    inset 3px 0 0 rgba(181, 126, 193, 0.56),
    inset 0 1px 0 rgba(255, 255, 255, 0.82);
}

.cell.group-edge-top {
  box-shadow:
    inset 0 3px 0 rgba(181, 126, 193, 0.56),
    inset 0 1px 0 rgba(255, 255, 255, 0.82);
}

.cell.group-edge-left.group-edge-top {
  box-shadow:
    inset 3px 0 0 rgba(181, 126, 193, 0.56),
    inset 0 3px 0 rgba(181, 126, 193, 0.56);
}

.cell.mark-yes {
  background: linear-gradient(135deg, #ffd7e2, #ffeaf0);
  color: #df3f70;
  box-shadow: 0 8px 18px rgba(223, 63, 112, 0.13);
}

.cell.mark-no {
  background: linear-gradient(135deg, #ead4c6, #f6e8df);
  color: #5a3323;
  box-shadow: 0 8px 18px rgba(90, 51, 35, 0.14);
}

.cell.mark-maybe {
  background: linear-gradient(135deg, #d9f1ff, #edf9ff);
  color: #2786c3;
  box-shadow: 0 8px 18px rgba(39, 134, 195, 0.12);
}

.cell.blank {
  border: 0;
  background: transparent;
  pointer-events: none;
}

.side-panel {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}

.setup-card,
.answers-card,
.save-card,
.notes-card {
  padding: 16px;
}

.setup-card.is-collapsed .preset-bank,
.setup-card.is-collapsed .setup-fields {
  display: none;
}

.setup-fields {
  display: grid;
  gap: 12px;
  margin-top: 12px;
}

.preset-bank {
  display: grid;
  gap: 9px;
  margin-top: 12px;
  padding: 12px;
  border: 1px solid rgba(236, 212, 231, 0.92);
  border-radius: var(--radius);
  background: rgba(255, 250, 253, 0.76);
}

.preset-add-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.preset-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 104px;
  overflow: auto;
}

.preset-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 30px;
  border: 1px solid #f4c5db;
  border-radius: 999px;
  padding: 0 8px 0 10px;
  background: #fff;
  color: var(--ink);
  font-size: 12px;
  font-weight: 800;
}

.preset-chip button {
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border: 0;
  border-radius: 50%;
  background: #fff0f6;
  color: var(--pink-strong);
  font-weight: 900;
}

.category-editor {
  border-top: 1px dashed var(--line-strong);
  padding-top: 12px;
}

.category-editor:first-child {
  border-top: 0;
  padding-top: 0;
}

.category-editor > label {
  display: grid;
  gap: 6px;
}

.item-list {
  display: grid;
  gap: 8px;
  margin-top: 9px;
}

.item-row {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr);
  gap: 8px;
  align-items: center;
}

.item-row span {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff0f6;
  color: var(--pink-strong);
  font-size: 12px;
}

.answer-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.save-library {
  display: grid;
  gap: 7px;
  margin-top: 12px;
  padding: 10px;
  border: 1px solid rgba(236, 212, 231, 0.82);
  border-radius: var(--radius);
  background: rgba(255, 250, 253, 0.72);
}

.save-load-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.answer-grid label {
  display: grid;
  gap: 6px;
}

.tiny-button {
  min-height: 34px;
  padding: 0 12px;
  color: var(--pink-strong);
  font-weight: 900;
}

.tiny-button.accent {
  background: #ecfff8;
  color: #278665;
  border-color: #bce8d8;
}

.notes-list {
  display: grid;
  gap: 8px;
  padding-left: 26px;
  margin: 12px 0 0;
}

.note-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 34px;
  gap: 8px;
  align-items: center;
}

.remove-note {
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: var(--danger);
  font-weight: 900;
}

.confirm-dialog {
  width: min(420px, calc(100vw - 28px));
  border: 0;
  border-radius: var(--radius);
  padding: 0;
  color: var(--ink);
  box-shadow: 0 26px 80px rgba(94, 53, 89, 0.28);
}

.confirm-dialog::backdrop {
  background: rgba(64, 50, 72, 0.28);
  backdrop-filter: blur(4px);
}

.confirm-dialog form {
  padding: 22px;
  background: linear-gradient(135deg, #fff, #fff1f8);
}

.confirm-dialog p {
  color: var(--muted);
  margin: 8px 0 18px;
}

.overview-dialog {
  width: min(1180px, calc(100vw - 32px));
  height: min(860px, calc(100dvh - 24px));
  border: 0;
  border-radius: 24px;
  padding: 0;
  background: transparent;
  color: var(--ink);
}

.overview-dialog::backdrop {
  background: rgba(64, 50, 72, 0.38);
  backdrop-filter: blur(8px);
}

.overview-sheet {
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  padding: clamp(8px, 1.3vw, 16px);
  overflow: hidden;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.98), rgba(255, 244, 250, 0.96)),
    radial-gradient(circle at 100% 0%, rgba(205, 182, 255, 0.28), transparent 24rem);
  border: 1px solid rgba(247, 191, 215, 0.92);
  border-radius: 24px;
  box-shadow: 0 32px 90px rgba(64, 50, 72, 0.28);
}

.overview-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 8px;
}

.overview-head > div {
  min-width: 0;
  flex: 1 1 auto;
}

.overview-head p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.overview-close {
  display: grid;
  place-items: center;
  flex: 0 0 34px;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: #fff;
  color: var(--pink-strong);
  font-size: 24px;
  line-height: 1;
  margin-right: 4px;
}

.overview-stage {
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  border-radius: 16px;
  background: #fff;
  border: 1px solid rgba(236, 212, 231, 0.82);
}

.overview-board {
  min-width: 0;
  padding: 4px;
  transform-origin: center;
  filter: none;
}

.overview-board .top-item,
.overview-board .row-item,
.overview-board .col-group {
  white-space: nowrap;
  word-break: normal;
  overflow: hidden;
  text-overflow: clip;
}

.overview-board .col-headers,
.overview-board .col-group-labels,
.overview-board .board-row {
  gap: 0;
}

.overview-board .top-item {
  height: calc(var(--cell) * 1.55);
  font-size: clamp(7px, calc(var(--cell) * 0.25), 11px);
  border-radius: 6px 6px 0 0;
  background: #fff;
  box-shadow: none;
  border-color: rgba(226, 201, 240, 0.7);
}

.overview-board .col-group {
  min-height: calc(var(--cell) * 0.58);
  font-size: clamp(7px, calc(var(--cell) * 0.27), 11px);
  border-radius: 7px 7px 0 0;
  background: #fff7fc;
  box-shadow: none;
}

.overview-board .row-item {
  font-size: clamp(7px, calc(var(--cell) * 0.24), 11px);
  padding-right: 5px;
  border-radius: 8px 0 0 8px;
  background: #f8fcff;
  box-shadow: none;
}

.overview-board .row-group-title {
  font-size: clamp(7px, calc(var(--cell) * 0.22), 10px);
}

.overview-board .cell {
  font-size: clamp(12px, calc(var(--cell) * 0.46), 22px);
  border-radius: 3px;
  pointer-events: none;
  background: #fff;
  box-shadow: none;
  transition: none;
}

.overview-board .cell.group-edge-left,
.overview-board .cell.group-edge-top,
.overview-board .cell.group-edge-left.group-edge-top {
  box-shadow: none;
  border-color: rgba(181, 126, 193, 0.64);
}

.overview-board .cell.mark-yes,
.overview-board .cell.mark-no,
.overview-board .cell.mark-maybe {
  box-shadow: none;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 5;
  border-radius: var(--radius);
  background: #403248;
  color: white;
  padding: 11px 14px;
  box-shadow: 0 16px 38px rgba(64, 50, 72, 0.24);
  animation: toastIn 2.2s ease forwards;
}

@keyframes toastIn {
  0%,
  100% {
    opacity: 0;
    transform: translateY(10px);
  }
  10%,
  85% {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 1040px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .side-panel {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .setup-card {
    grid-column: auto;
  }
}

@media (max-width: 720px) {
  :root {
    --cell: clamp(38px, 12.8vw, 52px);
  }

  .app-shell {
    width: calc(100% - 48px);
    padding: 12px;
  }

  .topbar,
  .panel-head {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions {
    display: flex;
    flex-wrap: nowrap;
    gap: 8px;
    width: calc(100vw - 72px);
    max-width: 100%;
    min-width: 0;
  }

  .icon-button {
    flex: 1 1 0;
    max-width: calc((100vw - 80px) / 2);
    padding: 0 10px;
    width: auto;
    font-size: 14px;
  }

  .side-panel,
  .answer-grid {
    grid-template-columns: 1fr;
  }

  .case-name-row {
    display: grid;
    grid-template-columns: 1fr;
    width: 100%;
  }

  .case-name-row label {
    grid-column: auto;
  }

  .case-name-row input {
    width: 100%;
  }

  .overview-button,
  .lock-button {
    width: 100%;
    padding: 0 12px;
  }

  .overview-button span:last-child,
  .lock-button span:last-child {
    display: inline;
  }

  .board-panel,
  .setup-card,
  .answers-card,
  .save-card,
  .notes-card {
    padding: 13px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}
