* { box-sizing: border-box; }
:root { color-scheme: light; font: clamp(11px, 1.45vh, 14px)/1.25 system-ui, sans-serif; }
html, body { height: 100%; overflow: hidden; }
body { margin: 0; background: #eceae3; color: #222; }
main, .layout { width: 100%; height: 100dvh; overflow: hidden; }
.layout { position: relative; }
.panel { min-width: 0; min-height: 0; overflow: hidden; background: white; border: 1px solid #ddd; border-radius: 8px; padding: 8px; }
.preview {
  position: absolute;
  inset: 0;
  border: 0;
  border-radius: 0;
  padding: 0;
  background: #f4f2ea;
}
#pattern { display: block; width: 100%; height: 100%; background: #f4f2ea; border: 0; cursor: grab; touch-action: none; }
#pattern.is-panning { cursor: grabbing; }
#pattern.is-editing, #pattern.is-editing.is-panning { cursor: default; }

.controls { position: absolute; top: 16px; left: 16px; z-index: 2; width: min(24rem, calc(100vw - 32px)); max-height: calc(100dvh - 32px); display: grid; grid-template-columns: 1fr; gap: 8px; align-content: start; overflow: auto; box-shadow: 0 14px 45px rgba(0,0,0,.18); }
.controls-handle { position: sticky; top: -8px; z-index: 1; margin: -8px -8px 0; padding: 8px; display: flex; gap: 6px; align-items: center; justify-content: space-between; border-bottom: 1px solid #e5e5e5; border-radius: 8px 8px 0 0; background: #fafafa; cursor: grab; font-weight: 800; user-select: none; touch-action: none; }
.controls-handle.is-dragging { cursor: grabbing; }
.header-actions, .preset-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.header-actions button { padding: 3px 6px; font-weight: 600; }
fieldset { min-width: 0; margin: 0; border: 0; padding: 0; }
legend { width: 100%; padding: 0 2px; font-weight: 800; }
.control-group { min-width: 0; min-height: 0; overflow: hidden; display: grid; gap: 5px; align-content: start; border: 1px solid #ececec; border-radius: 6px; padding: 6px; background: #fff; }
.control-group.is-collapsed > :not(legend) { display: none; }
.section-toggle { width: 100%; display: flex; justify-content: space-between; align-items: center; border: 0; padding: 0; background: transparent; font-weight: 800; text-align: left; }
.section-toggle::after { content: '▾'; }
.section-toggle[aria-expanded="false"]::after { content: '▸'; }
.field-grid { min-width: 0; display: flex; flex-wrap: wrap; gap: 6px 8px; align-items: end; align-content: start; }
.grid-offset-row { flex-basis: 100%; display: flex; gap: 6px 8px; align-items: end; }
.field-label { font-weight: 800; align-self: center; }
.field-label--wide { flex-basis: 100%; }
.field, label { min-width: 0; display: grid; gap: 2px; font-weight: 600; }
.field { width: max-content; max-width: 100%; }
.field--wide, .field--stacked { width: 100%; flex-basis: 100%; }
.field--upload { flex: 1 1 16rem; width: auto; }
.control-group--content-font .field--upload { width: 100%; }
.field--range { flex: 1 1 8rem; width: auto; }
.field--range input { min-width: 7rem; }
.field--stacked textarea { height: clamp(3.2rem, 8vh, 5.4rem); }
.field--check { display: flex; align-items: end; gap: .45em; padding-bottom: .35em; }
.field--check input { width: auto; }

input, textarea, select, button { font: inherit; }
textarea { min-height: 3em; resize: none; }
input, textarea, select { width: 100%; min-width: 0; padding: 4px 5px; border: 1px solid #bbb; border-radius: 4px; }
input[type="number"] { appearance: textfield; -moz-appearance: textfield; }
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
button { padding: 5px 8px; border: 1px solid #999; border-radius: 4px; background: #eee; cursor: pointer; }
.hint { color: #666; font-size: .85em; font-weight: 400; }
.warning-text { margin: 0; color: #9a3412; font-size: .9em; font-weight: 700; }
#status { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.nudge-panel { display: grid; gap: 5px; }
.nudge-pad { display: grid; grid-template-columns: repeat(3, 2rem); gap: 4px; width: max-content; }
.nudge-btn { min-width: 0; padding: 4px 0; }
.nudge-btn:disabled { opacity: .45; cursor: not-allowed; }
#editModeToggle.is-active { background: #dbeafe; border-color: #1e90ff; }

.stepper { display: grid; grid-template-columns: 1.45em 4.4ch 1.45em; width: max-content; max-width: 100%; }
.stepper .stepper-input { width: 4.4ch; text-align: center; padding-inline: 0; border-radius: 0; border-inline: 0; }
.stepper-btn { height: 1.9em; min-width: 0; padding: 0; border-color: #bbb; background: #f0f0f0; line-height: 1; }
.stepper-btn:first-child { border-radius: 4px 0 0 4px; }
.stepper-btn:last-child { border-radius: 0 4px 4px 0; }
.stepper-btn:hover { background: #e5e5e5; }

details { min-height: 0; margin-top: 0; overflow: hidden; }
details[open] { display: grid; grid-template-rows: auto auto minmax(0, 1fr); }
summary { cursor: pointer; font-weight: 700; }
.char-table { min-height: 0; max-height: min(24vh, 14rem); display: grid; gap: 4px; margin-top: 5px; overflow: auto; }
.char-row { display: grid; grid-template-columns: 3.2em 2.8em max-content max-content minmax(6rem, 1fr); gap: 4px; align-items: end; }
.char-row span { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.char-row input.color { min-width: 6rem; }

@media (max-height: 700px) {
  :root { font-size: 11px; }
  .controls, .field-grid { gap: 4px; }
  .panel, .control-group { padding: 5px; }
  .controls-handle { top: -5px; margin: -5px -5px 0; padding: 6px; }
  input, textarea, select { padding: 2px 4px; }
  textarea { min-height: 2.4em; }
  .field--stacked textarea { height: 3rem; }
  .hint:not(#status) { display: none; }
  details { display: none; }
}

@media (max-width: 420px) {
  .controls { top: 8px; left: 8px; width: calc(100vw - 16px); max-height: calc(100dvh - 16px); }
}
