:root {
  --navy: #123f67;
  --ink: #162332;
  --muted: #53677d;
  --line: #b9c5d1;
  --panel: #ffffff;
  --page: #eaf1f6;
  --red-rule: #d02222;

  --p1: #fff1b0;
  --p2: #ffe39a;
  --p3: #ffd27a;
  --p4: #ffc066;
  --p5: #ffa64d;
  --p6: #ff8d42;
  --p7: #ff7445;
  --p8: #f35a52;
  --p9: #eb4b63;
  --p10: #df3c70;
  --neutral: #9ea6af;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--page);
  color: var(--ink);
  font-family: Arial, Helvetica, sans-serif;
}

button {
  font: inherit;
  color: inherit;
  background: #fff;
  border: 1px solid #8799aa;
  min-height: 44px;
  cursor: pointer;
  touch-action: manipulation;
}

button:focus-visible {
  outline: 3px solid #4b86b8;
  outline-offset: 2px;
}

.app-shell {
  width: min(1400px, calc(100% - 32px));
  margin: 22px auto;
}

.toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  padding: 16px;
  background: #fff;
  border: 1px solid var(--line);
}

.segmented,
.zoom-controls {
  display: flex;
  align-items: stretch;
}

.segmented .seg-btn + .seg-btn,
.zoom-controls button + button {
  border-left: 0;
}

.seg-btn,
.zoom-controls button {
  padding: 0 16px;
  font-weight: 700;
}

.seg-btn.active {
  background: var(--navy);
  color: #fff;
  border-color: var(--navy);
}

#zoomLabel {
  display: grid;
  place-items: center;
  min-width: 66px;
  padding: 0 10px;
  background: #fff;
  border-top: 1px solid #8799aa;
  border-bottom: 1px solid #8799aa;
  color: var(--muted);
}

.red-rule {
  height: 4px;
  margin: 14px 0 22px;
  background: var(--red-rule);
}

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

.palette-panel,
.map-card {
  background: var(--panel);
  border: 1px solid var(--line);
}

.palette-panel {
  padding: 16px;
}

.palette-panel h2 {
  margin: 0 0 12px;
  font-size: 22px;
}

.palette-panel h3 {
  margin: 0 0 8px;
  font-size: 16px;
}

.pain-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 50px;
}

.pain-row span {
  line-height: 1.15;
}

.swatch {
  width: 46px;
  min-width: 46px;
  height: 46px;
  min-height: 46px;
  padding: 0;
  font-weight: 700;
  border: 1px solid #7c8b98;
}

.swatch.active {
  outline: 3px solid var(--navy);
  outline-offset: 2px;
}

.neutral { background: var(--neutral); color: transparent; }
.p1 { background: var(--p1); }
.p2 { background: var(--p2); }
.p3 { background: var(--p3); }
.p4 { background: var(--p4); }
.p5 { background: var(--p5); }
.p6 { background: var(--p6); }
.p7 { background: var(--p7); }
.p8 { background: var(--p8); }
.p9 { background: var(--p9); }
.p10 { background: var(--p10); }

.side-divider {
  height: 1px;
  background: var(--line);
  margin: 14px 0;
}

.brush-buttons {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
}

.brush-btn {
  width: 100%;
  min-height: 38px;
  font-weight: 700;
}

.brush-btn.active {
  background: var(--navy);
  color: #fff;
  border-color: var(--navy);
}

.wide-control {
  width: 100%;
  font-weight: 700;
}

#eraseBtn.active {
  background: #7b2630;
  color: #fff;
  border-color: #7b2630;
}

.tool-status {
  margin: 8px 0 0;
  padding: 8px;
  border: 1px solid var(--line);
  background: #f7f9fb;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.3;
}

.tool-status.erase-on {
  background: #fff2f3;
  border-color: #c9757d;
  color: #6f1f27;
  font-weight: 700;
}

.edit-controls {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-top: 8px;
}

.edit-controls button {
  padding: 0 8px;
  font-weight: 700;
}

.map-card {
  padding: 16px;
  min-width: 0;
}

.map-heading h1 {
  margin: 0;
  font-size: 28px;
}

.map-heading p,
.help-text {
  color: var(--muted);
}

.map-heading p {
  margin: 2px 0 14px;
  font-size: 17px;
}

.viewport {
  position: relative;
  height: min(76vh, 780px);
  min-height: 560px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: #fff;
  touch-action: none;
  overscroll-behavior: contain;
}

.stage {
  position: absolute;
  left: 0;
  top: 0;
  width: 900px;
  height: 700px;
  transform-origin: 0 0;
  user-select: none;
  -webkit-user-select: none;
}

#bodyImage,
#drawCanvas {
  position: absolute;
  left: 200px;
  top: 0;
  width: 500px;
  height: 700px;
}

#bodyImage {
  object-fit: contain;
  pointer-events: none;
  -webkit-user-drag: none;
}

#drawCanvas {
  z-index: 2;
  touch-action: none;
  cursor: crosshair;
}

.side-label {
  position: absolute;
  top: 74px;
  font-weight: 700;
  letter-spacing: 1px;
  color: #2e3f51;
  pointer-events: none;
}

.left-label { left: 92px; }
.right-label { right: 92px; }

.help-text {
  margin: 12px 0 0;
  font-size: 16px;
}

@media (max-width: 820px) {
  .app-shell {
    width: min(100% - 12px, 760px);
    margin: 8px auto 18px;
  }

  .toolbar {
    gap: 6px;
    padding: 10px;
  }

  .seg-btn,
  .zoom-controls button {
    padding: 0 10px;
    min-height: 40px;
  }

  #zoomLabel {
    min-width: 52px;
    font-size: 14px;
  }

  .red-rule {
    margin: 10px 0 12px;
  }

  .workspace {
    grid-template-columns: 96px minmax(0, 1fr);
    gap: 8px;
  }

  .palette-panel {
    padding: 8px;
  }

  .palette-panel h2 {
    font-size: 18px;
    margin-bottom: 8px;
  }

  .palette-panel h3 {
    font-size: 13px;
  }

  .pain-row {
    min-height: 43px;
    gap: 6px;
  }

  .pain-row span {
    display: none;
  }

  .swatch {
    width: 40px;
    min-width: 40px;
    height: 40px;
    min-height: 40px;
  }

  .brush-btn,
  .wide-control,
  .edit-controls button {
    min-height: 36px;
    padding: 0 4px;
    font-size: 12px;
  }

  .tool-status {
    font-size: 11px;
    padding: 6px;
  }

  .edit-controls {
    grid-template-columns: 1fr;
    gap: 5px;
  }

  .map-card {
    padding: 8px;
  }

  .map-heading h1 {
    font-size: 22px;
  }

  .map-heading p,
  .help-text {
    font-size: 13px;
  }

  .viewport {
    height: 72vh;
    min-height: 500px;
  }
}

@media (max-width: 520px) {
  .toolbar {
    display: grid;
    grid-template-columns: max-content max-content;
    justify-content: start;
  }

  .zoom-controls {
    grid-column: 1 / -1;
  }

  .workspace {
    grid-template-columns: 82px minmax(0, 1fr);
    gap: 6px;
  }

  .palette-panel {
    padding: 6px;
  }

  .swatch {
    width: 36px;
    min-width: 36px;
    height: 36px;
    min-height: 36px;
  }

  .pain-row {
    min-height: 39px;
  }

  .tool-status {
    display: none;
  }
}


.icon-btn {
  min-width: 44px;
  padding: 0 12px;
  font-size: 24px;
  line-height: 1;
  font-weight: 700;
}

.icon-btn:active {
  transform: translateY(1px);
}


.undo-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 14px;
  font-weight: 700;
  white-space: nowrap;
}

.undo-btn img {
  width: 22px;
  height: 22px;
  object-fit: contain;
  display: block;
}

@media (max-width: 820px) {
  .undo-btn {
    padding: 0 10px;
    gap: 6px;
  }

  .undo-btn img {
    width: 20px;
    height: 20px;
  }
}


/* Improved mobile toolbar responsiveness */
@media (max-width: 700px) {
  .toolbar {
    gap: 4px;
    padding: 8px;
  }

  .toolbar .segmented,
  .toolbar .zoom-controls {
    flex-wrap: nowrap;
  }

  .seg-btn,
  .zoom-controls button,
  .undo-btn {
    min-height: 36px;
    padding: 0 8px;
    font-size: 13px;
  }

  #zoomLabel {
    min-width: 44px;
    padding: 0 6px;
    font-size: 12px;
  }

  .undo-btn {
    gap: 4px;
  }

  .undo-btn img {
    width: 17px;
    height: 17px;
  }
}

@media (max-width: 520px) {
  .toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px;
    padding: 6px;
  }

  .segmented,
  .zoom-controls {
    display: flex;
    flex-wrap: nowrap;
  }

  .seg-btn,
  .zoom-controls button,
  .undo-btn {
    min-height: 34px;
    padding: 0 7px;
    font-size: 12px;
  }

  #zoomLabel {
    min-width: 40px;
    padding: 0 5px;
    font-size: 11px;
  }

  .undo-btn {
    gap: 3px;
  }

  .undo-btn img {
    width: 16px;
    height: 16px;
  }
}

@media (max-width: 390px) {
  .toolbar {
    gap: 3px;
    padding: 5px;
  }

  .seg-btn,
  .zoom-controls button,
  .undo-btn {
    min-height: 32px;
    padding: 0 6px;
    font-size: 11px;
  }

  #zoomLabel {
    min-width: 36px;
    font-size: 10px;
  }

  .undo-btn img {
    width: 15px;
    height: 15px;
  }
}


/* Mobile scrolling:
   - the page can scroll normally when the gesture starts outside the drawing canvas
   - gestures that begin on the drawing canvas remain reserved for drawing/pinch zoom */
@media (max-width: 820px) {
  .viewport {
    touch-action: pan-y;
    overscroll-behavior-y: auto;
  }

  #drawCanvas {
    touch-action: none;
  }
}


/* Conditional mobile touch behavior:
   the drawing canvas itself does not capture touches.
   JS only cancels a touch when it starts on the actual body. */
#drawCanvas {
  pointer-events: none;
}

@media (max-width: 820px) {
  .viewport {
    touch-action: auto;
    overscroll-behavior-y: auto;
  }
}


.participant-status {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid #8799aa;
  background: #f7f9fb;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.participant-status.valid {
  background: #eef7f0;
  border-color: #7ca889;
  color: #285f36;
}

.participant-status.invalid {
  background: #fff2f3;
  border-color: #c9757d;
  color: #6f1f27;
}

.submit-btn {
  min-height: 44px;
  padding: 0 18px;
  border-color: var(--navy);
  background: var(--navy);
  color: #fff;
  font-weight: 700;
}

.submit-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.submit-btn.submitting {
  opacity: 0.75;
  cursor: wait;
}

.app-message {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(10, 24, 38, 0.48);
}

.app-message[hidden] {
  display: none;
}

.app-message-card {
  width: min(440px, 100%);
  padding: 22px;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 18px 48px rgba(0,0,0,0.22);
}

.app-message-card h2 {
  margin: 0 0 8px;
  font-size: 22px;
}

.app-message-card p {
  margin: 0 0 18px;
  line-height: 1.45;
}

.app-message-card button {
  padding: 0 16px;
  font-weight: 700;
}

@media (max-width: 700px) {
  .participant-status {
    min-height: 34px;
    padding: 0 7px;
    font-size: 11px;
  }

  .submit-btn {
    min-height: 34px;
    padding: 0 10px;
    font-size: 12px;
  }
}

@media (max-width: 390px) {
  .participant-status {
    max-width: 118px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}


.submit-reminder {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-top: 18px;
  padding: 18px 20px;
  background: #fff;
  border: 1px solid var(--line);
  border-top: 4px solid var(--navy);
}

.submit-reminder-text h2 {
  margin: 0 0 4px;
  font-size: 20px;
}

.submit-reminder-text p {
  margin: 0;
  color: var(--muted);
  line-height: 1.4;
}

.submit-btn-bottom {
  flex: 0 0 auto;
  min-width: 190px;
}

@media (max-width: 700px) {
  .submit-reminder {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    padding: 14px;
  }

  .submit-reminder-text h2 {
    font-size: 18px;
  }

  .submit-reminder-text p {
    font-size: 14px;
  }

  .submit-btn-bottom {
    width: 100%;
    min-height: 44px;
    font-size: 14px;
  }
}


/* Extra-compact mobile top toolbar */
@media (max-width: 700px) {
  .toolbar {
    gap: 3px;
    padding: 6px;
  }

  .toolbar .seg-btn,
  .toolbar .zoom-controls button,
  .toolbar .undo-btn,
  .toolbar .submit-btn,
  .participant-status {
    min-height: 32px;
    padding-left: 6px;
    padding-right: 6px;
    font-size: 11px;
  }

  #zoomLabel {
    min-width: 36px;
    padding: 0 4px;
    font-size: 10px;
  }

  .undo-btn {
    gap: 3px;
  }

  .undo-btn img {
    width: 14px;
    height: 14px;
  }

  .participant-status {
    max-width: 150px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

@media (max-width: 420px) {
  .toolbar {
    gap: 2px;
    padding: 5px;
  }

  .toolbar .seg-btn,
  .toolbar .zoom-controls button,
  .toolbar .undo-btn,
  .toolbar .submit-btn,
  .participant-status {
    min-height: 30px;
    padding-left: 5px;
    padding-right: 5px;
    font-size: 10px;
  }

  #zoomLabel {
    min-width: 34px;
    font-size: 9px;
  }

  .participant-status {
    max-width: 130px;
  }

  .undo-btn img {
    width: 13px;
    height: 13px;
  }
}


.toolbar-bottom-row {
  flex-basis: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
}

.toolbar-bottom-row .participant-status {
  flex: 1 1 auto;
  max-width: none;
}

.toolbar-bottom-row .submit-btn {
  flex: 0 0 auto;
}

@media (max-width: 700px) {
  .toolbar-bottom-row {
    gap: 6px;
    margin-top: 3px;
    padding-top: 6px;
  }

  .toolbar-bottom-row .participant-status {
    min-width: 0;
    max-width: none;
    font-size: 10px;
  }

  .toolbar-bottom-row .submit-btn {
    font-size: 10px;
    padding-left: 7px;
    padding-right: 7px;
  }
}

@media (max-width: 420px) {
  .toolbar-bottom-row {
    gap: 5px;
  }

  .toolbar-bottom-row .participant-status,
  .toolbar-bottom-row .submit-btn {
    min-height: 30px;
    font-size: 9.5px;
  }
}
