:root {
  --bg: #f5f6f4;
  --text: #1b1f23;
  --muted: #687076;
  --line: #d9ded7;
  --panel-0: #eef4f8;
  --panel-1: #f3f0e8;
  --panel-2: #edf4ed;
  --panel-3: #f5eef1;
  --panel-4: #eef1f6;
  --panel-5: #f1f4ea;
  --danger: #b42318;
  --accent: #226a52;
  --accent-dark: #184f3d;
  --warning: #d99a00;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  min-width: 360px;
  margin: 0;
  color: var(--text);
  background: var(--bg);
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled,
select:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.page {
  min-height: 100vh;
}

.login-view {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(100%, 360px);
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 18px 60px rgb(35 38 35 / 10%);
}

.login-panel h1,
.topbar h1 {
  margin: 0;
  font-size: 24px;
  line-height: 1.2;
}

.field {
  display: grid;
  gap: 8px;
  margin: 24px 0 16px;
  color: var(--muted);
  font-size: 14px;
}

.field input,
.edit-input,
.level-input {
  width: 100%;
  min-width: 0;
  height: 36px;
  border: 1px solid #b9c2bd;
  border-radius: 6px;
  padding: 0 10px;
  color: var(--text);
  background: #fff;
  outline: none;
}

.field input:focus,
.edit-input:focus,
.level-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgb(34 106 82 / 16%);
}

.primary-button,
.secondary-button,
.level-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 36px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0 14px;
  white-space: nowrap;
}

.primary-button {
  width: 100%;
  color: #fff;
  background: var(--accent);
}

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

.secondary-button,
.level-action {
  color: var(--text);
  border-color: #cfd8d2;
  background: #fff;
}

.save-button.is-dirty:not(:disabled) {
  color: #3d2b00;
  border-color: var(--warning);
  background: #ffd875;
}

.save-button.is-dirty:not(:disabled):hover {
  border-color: #bf8700;
  background: #ffc94a;
}

.fold-button {
  min-height: 52px;
  padding: 0 20px;
  font-size: 16px;
}

.collapse-all-button::before,
.expand-all-button::before {
  content: "";
  width: 15px;
  height: 15px;
  flex: 0 0 auto;
  border: 1.5px solid currentColor;
  border-radius: 3px;
  background: linear-gradient(currentColor 0 0) center / 8px 1.5px no-repeat;
}

.expand-all-button::before {
  background:
    linear-gradient(currentColor 0 0) center / 8px 1.5px no-repeat,
    linear-gradient(currentColor 0 0) center / 1.5px 8px no-repeat;
}

.json-download-button::before,
.json-upload-button::before,
.ai-prompt-button::before,
.add-backup-button::before,
.backups-button::before,
.save-button::before,
.logout-button::before {
  content: "";
  display: inline-block;
  width: 18px;
  height: 18px;
  flex: 0 0 auto;
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
}

.json-download-button::before {
  background-image: url("icons/download.svg");
}

.json-upload-button::before {
  background-image: url("icons/upload.svg");
}

.ai-prompt-button::before {
  background-image: url("icons/ai.svg");
}

.add-backup-button::before {
  background-image: url("icons/add-backup.svg");
}

.backups-button::before {
  background-image: url("icons/backups.svg");
}

.save-button::before {
  background-image: url("icons/save.svg");
}

.logout-button::before {
  background-image: url("icons/logout.svg");
}

.warning-reset-button::before {
  content: "!";
  display: inline-grid;
  width: 16px;
  height: 16px;
  flex: 0 0 auto;
  place-items: center;
  border: 1.5px solid currentColor;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
}

.secondary-button:hover,
.level-action:hover {
  border-color: #9aa8a1;
}

.form-error,
.edit-error {
  min-height: 18px;
  margin: 8px 0 0;
  color: var(--danger);
  font-size: 13px;
}

.app-view {
  min-height: 100vh;
  padding: 18px;
}

.topbar {
  position: sticky;
  z-index: 5;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin: -18px -18px 18px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  background: rgb(245 246 244 / 94%);
  backdrop-filter: blur(10px);
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.save-status {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.transfer-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: max-content;
  max-width: 100%;
  margin: 0 0 12px;
  padding: 10px 12px;
  border: 1px solid #c7d6ce;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 4px 18px rgb(25 33 28 / 8%);
}

.transfer-text {
  color: #26312c;
  font-size: 14px;
  line-height: 1.35;
}

.tree-root {
  overflow-x: auto;
  padding-bottom: 24px;
}

.branch {
  --branch-width: 360px;
  --branch-content-width: calc(var(--branch-width) - 22px);
  display: inline-block;
  width: var(--branch-width);
  min-width: var(--branch-width);
  padding: 10px;
  border: 1px solid rgb(92 112 97 / 14%);
  border-radius: 8px;
  vertical-align: top;
  background: var(--panel-0);
}

.branch[data-level="1"] {
  background: var(--panel-1);
}

.branch[data-level="2"] {
  background: var(--panel-2);
}

.branch[data-level="3"] {
  background: var(--panel-3);
}

.branch[data-level="4"] {
  background: var(--panel-4);
}

.branch[data-level="5"] {
  background: var(--panel-5);
}

.branch-header {
  display: grid;
  align-items: start;
  gap: 8px;
  min-height: 38px;
  margin-bottom: 8px;
}

.branch-title {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
}

.level-title-wrap {
  width: 100%;
  min-width: 0;
  padding-bottom: 8px;
  border-bottom: 1px solid rgb(92 112 97 / 16%);
}

.level-title-line {
  display: flex;
  align-items: center;
  gap: 7px;
  width: 100%;
  min-width: 0;
}

.branch-duplicate-icon {
  cursor: help;
}

.level-title-line .level-editor {
  flex: 1 1 auto;
  min-width: 0;
}

.level-title-button {
  display: block;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 100%;
  min-height: 30px;
  overflow: hidden;
  border: 0;
  border-radius: 5px;
  padding: 3px 6px;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
  background: transparent;
  font-size: 13px;
  font-weight: 700;
  text-align: left;
  text-transform: uppercase;
}

.level-title-button:hover {
  background: rgb(34 106 82 / 8%);
}

.level-editor {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) auto auto;
  align-items: center;
  gap: 4px;
}

.level-input {
  height: 32px;
  font-size: 14px;
}

.selection-branch-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
}

.selection-type-select,
.display-type-select,
.default-value-select,
.range-type-select,
.range-step-input {
  width: max-content;
  max-width: 100%;
}

.selection-toggle,
.required-toggle,
.range-setting-field,
.range-step-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.2;
  white-space: nowrap;
}

.selection-toggle input,
.required-toggle input {
  width: 14px;
  height: 14px;
  margin: 0;
}

.level-error,
.branch-warning {
  margin: 4px 0 0;
  color: var(--danger);
  font-size: 12px;
}

.branch-warning {
  margin: -2px 0 8px;
}

.branch-actions {
  display: grid;
  flex: 0 0 auto;
  justify-items: end;
  gap: 6px;
}

.branch-json-actions,
.branch-transfer-actions {
  display: grid;
  gap: 6px;
  width: 100%;
}

.fold-actions {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
}

.reset-warnings-button,
.branch-json-button,
.branch-transfer-button,
.branch-delete-button {
  min-height: 32px;
  padding: 0 8px;
  font-size: 12px;
}

.rows {
  display: grid;
  gap: 8px;
}

.add-node-button {
  display: grid;
  width: var(--branch-content-width);
  max-width: 100%;
  min-height: 42px;
  place-items: center;
  border: 1px dashed #aebbb4;
  border-radius: 8px;
  color: var(--accent);
  background: rgb(255 255 255 / 62%);
  font-size: 22px;
  line-height: 1;
}

.add-node-button:hover {
  border-color: var(--accent);
  background: #fff;
}

.add-node-button.drop-target {
  border-color: var(--accent);
  background: #fff;
  box-shadow: inset 0 0 0 2px rgb(34 106 82 / 18%);
}

.tree-row {
  display: flex;
  align-items: start;
  gap: 12px;
  width: max-content;
}

.node-cell {
  flex: 0 0 var(--branch-content-width);
  width: var(--branch-content-width);
  min-width: 0;
}

.node-card {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr) 86px;
  align-items: center;
  gap: 6px;
  min-height: 42px;
  padding: 5px;
  border: 1px solid rgb(93 103 96 / 16%);
  border-radius: 8px;
  background: rgb(255 255 255 / 82%);
  box-shadow: 0 1px 0 rgb(25 33 28 / 4%);
}

.node-card[draggable="true"] {
  cursor: grab;
}

.node-card.drag-source {
  opacity: 0.46;
}

.tree-row.drop-before > .node-cell > .node-card {
  box-shadow:
    0 -3px 0 var(--accent),
    0 1px 0 rgb(25 33 28 / 4%);
}

.tree-row.drop-after > .node-cell > .node-card {
  box-shadow:
    0 3px 0 var(--accent),
    0 1px 0 rgb(25 33 28 / 4%);
}

.node-card.transfer-source {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgb(34 106 82 / 18%);
}

.node-card.transfer-target {
  border-color: #b8cfc3;
  background: rgb(255 255 255 / 94%);
}

.node-main {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.node-text {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  max-width: 100%;
  min-height: 30px;
  overflow-wrap: anywhere;
  border-radius: 5px;
  padding: 4px 6px;
  line-height: 1.25;
}

.node-text:hover {
  background: rgb(34 106 82 / 8%);
}

.custom-input-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: max-content;
  max-width: 100%;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.2;
}

.custom-input-toggle input {
  width: 14px;
  height: 14px;
  margin: 0;
}

.node-actions {
  display: flex;
  flex: 0 0 auto;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: flex-end;
  max-width: 116px;
}

.child-level-menu-wrap {
  position: relative;
  display: inline-flex;
}

.child-level-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 40;
  display: grid;
  gap: 4px;
  min-width: 190px;
  padding: 6px;
  border: 1px solid #cfd8d2;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 12px 30px rgb(27 31 35 / 16%);
}

.child-level-menu button {
  display: flex;
  align-items: center;
  min-height: 32px;
  border: 0;
  border-radius: 6px;
  padding: 0 10px;
  color: var(--text);
  background: transparent;
  text-align: left;
  white-space: nowrap;
}

.child-level-menu button:hover {
  background: rgb(34 106 82 / 9%);
}

.icon-button {
  display: inline-grid;
  width: 26px;
  height: 26px;
  place-items: center;
  border: 1px solid transparent;
  border-radius: 6px;
  color: #26312c;
  background: transparent;
  line-height: 1;
}

.collapse-button {
  width: 52px;
  height: 52px;
  font-size: 22px;
}

.icon-button:hover {
  border-color: #c7d1cb;
  background: #fff;
}

.icon-button.danger {
  color: var(--danger);
}

.icon-button.confirm {
  color: var(--accent);
}

.collapse-placeholder {
  width: 52px;
  height: 52px;
}

.hidden-children-icon,
.duplicate-icon {
  display: inline-grid;
  width: 20px;
  height: 20px;
  flex: 0 0 auto;
  place-items: center;
  border-radius: 50%;
  font-size: 13px;
  line-height: 1;
}

.hidden-children-icon {
  color: #33433a;
  background: rgb(34 106 82 / 12%);
}

.duplicate-icon {
  color: #3d2b00;
  border: 0;
  background: #ffd875;
  cursor: pointer;
}

.children-cell {
  flex: 0 0 auto;
  min-width: 0;
  margin-right: -10px;
}

.child-branches {
  display: grid;
  gap: 8px;
  width: max-content;
  min-width: 360px;
}

.editing-wrap {
  display: grid;
  gap: 3px;
}

.empty-state {
  display: grid;
  min-height: 42px;
  place-items: center;
  border: 1px dashed rgb(104 112 118 / 36%);
  border-radius: 8px;
  color: var(--muted);
  background: rgb(255 255 255 / 52%);
  font-size: 14px;
}

.saving-overlay {
  position: fixed;
  z-index: 80;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgb(245 246 244 / 72%);
  backdrop-filter: blur(2px);
}

.saving-panel {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 0 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: #fff;
  box-shadow: 0 18px 60px rgb(35 38 35 / 16%);
  font-weight: 700;
}

.saving-spinner {
  width: 22px;
  height: 22px;
  border: 3px solid #d7dfda;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: saving-spin 0.8s linear infinite;
}

@keyframes saving-spin {
  to {
    transform: rotate(360deg);
  }
}

.modal-backdrop {
  position: fixed;
  z-index: 50;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgb(27 31 35 / 38%);
}

.modal-dialog {
  width: min(100%, 560px);
  max-height: min(720px, calc(100vh - 48px));
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 20px;
  background: #fff;
  box-shadow: 0 22px 70px rgb(20 28 24 / 22%);
}

.backups-modal {
  width: min(100%, 820px);
}

.modal-title {
  margin: 0;
  font-size: 22px;
  line-height: 1.2;
}

.modal-subtitle {
  margin: 8px 0 18px;
  color: var(--muted);
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.duplicate-modal-form {
  display: grid;
  gap: 14px;
}

.duplicate-options {
  display: grid;
  gap: 8px;
  max-height: 360px;
  overflow: auto;
}

.duplicate-option {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: start;
  gap: 10px;
  padding: 10px;
  border: 1px solid #d7dfda;
  border-radius: 8px;
  background: #f9fbf9;
  cursor: pointer;
}

.duplicate-option:hover {
  border-color: #afbeb6;
  background: #fff;
}

.duplicate-option input {
  margin-top: 3px;
}

.duplicate-option span {
  overflow-wrap: anywhere;
  line-height: 1.35;
}

.modal-error {
  min-height: 18px;
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.backups-list {
  display: grid;
  gap: 8px;
  max-height: min(540px, calc(100vh - 250px));
  overflow: auto;
  padding-right: 4px;
}

.backup-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border: 1px solid #d7dfda;
  border-radius: 8px;
  background: #f9fbf9;
}

.backup-info {
  display: grid;
  min-width: 0;
  gap: 5px;
}

.backup-name {
  min-width: 0;
  overflow: hidden;
  color: #26312c;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: ui-monospace, "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 13px;
  font-weight: 700;
}

.backup-meta {
  color: var(--muted);
  font-size: 13px;
}

.backup-item-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.backup-action-button {
  min-height: 32px;
  padding: 0 10px;
  font-size: 13px;
}

.danger-action-button {
  color: var(--danger);
}

.empty-backups {
  margin: 0;
  padding: 18px;
  border: 1px dashed #c5cec8;
  border-radius: 8px;
  color: var(--muted);
  background: #f9fbf9;
  text-align: center;
}

.modal-primary-button {
  width: auto;
}

@media (max-width: 700px) {
  .app-view {
    padding: 12px;
  }

  .topbar {
    margin: -12px -12px 12px;
    padding: 12px;
  }

  .tree-row {
    align-items: stretch;
    flex-direction: column;
    gap: 8px;
    width: 100%;
  }

  .node-cell {
    flex-basis: auto;
    width: 100%;
  }

  .children-cell {
    margin-right: 0;
    padding-left: 18px;
  }

  .transfer-panel {
    align-items: stretch;
    flex-direction: column;
    width: 100%;
  }

  .backup-item {
    grid-template-columns: 1fr;
  }

  .backup-item-actions {
    justify-content: stretch;
  }

  .backup-action-button {
    flex: 1 1 140px;
  }
}
