:root {
  --bg: #eef2f3;
  --panel: #ffffff;
  --panel-soft: #f8fafb;
  --line: #d6dee3;
  --line-strong: #b6c4cc;
  --text: #182027;
  --muted: #66737d;
  --accent: #087f8c;
  --accent-dark: #05626c;
  --danger: #b8433f;
  --danger-dark: #8e2e2b;
  --focus: rgba(8, 127, 140, 0.2);
  --shadow: 0 12px 34px rgba(18, 32, 42, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

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

.app-shell {
  min-height: 100vh;
  padding: 16px;
}

.topbar,
.meta-bar,
.pane {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.topbar {
  align-items: center;
  display: flex;
  gap: 16px;
  justify-content: space-between;
  padding: 14px;
}

.brand {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  min-width: 220px;
}

h1,
h2 {
  margin: 0;
}

h1 {
  font-size: 22px;
  line-height: 1.2;
}

h2 {
  font-size: 16px;
  line-height: 1.25;
}

.status-badge,
.small-metric,
.editor-type {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
}

.status-badge {
  background: #edf7f8;
  border: 1px solid #c5e6ea;
  border-radius: 999px;
  color: var(--accent-dark);
  padding: 4px 9px;
  white-space: nowrap;
}

.status-badge.dirty {
  background: #fff6df;
  border-color: #ecd28b;
  color: #7a5a00;
}

.header-actions {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.saved-select {
  min-width: 220px;
}

.button,
.icon-button,
select,
input {
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  min-height: 36px;
}

select,
input {
  background: #ffffff;
  color: var(--text);
  padding: 8px 10px;
}

input:focus,
select:focus,
button:focus-visible {
  box-shadow: 0 0 0 4px var(--focus);
  outline: none;
}

.button {
  align-items: center;
  background: var(--panel-soft);
  color: var(--text);
  display: inline-flex;
  justify-content: center;
  padding: 8px 12px;
}

.button:hover,
.icon-button:hover,
.context-menu button:hover {
  filter: brightness(0.98);
}

.button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.button.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #ffffff;
}

.button.neutral {
  background: #f3f6f7;
}

.icon-button {
  align-items: center;
  background: #ffffff;
  color: var(--text);
  display: inline-flex;
  flex: 0 0 auto;
  height: 36px;
  justify-content: center;
  padding: 0;
  width: 36px;
}

.primary-icon {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.meta-bar {
  display: grid;
  gap: 12px;
  grid-template-columns: minmax(220px, 0.55fr) minmax(260px, 1fr);
  margin-top: 12px;
  padding: 12px;
}

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

.meta-bar span,
.field-label {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.2;
}

.workspace {
  display: grid;
  gap: 12px;
  grid-template-columns: minmax(440px, 1.15fr) minmax(380px, 0.85fr);
  height: calc(100vh - 148px);
  min-height: 560px;
  margin-top: 12px;
}

.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.pane-header {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 10px;
  justify-content: space-between;
  min-height: 58px;
  padding: 12px;
}

.pane-header > div:first-child {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.preview-frame {
  background: #e8ecef;
  border: 0;
  flex: 1 1 auto;
  min-height: 320px;
  width: 100%;
}

.structure-header {
  align-items: start;
}

.add-controls {
  align-items: center;
  display: flex;
  gap: 7px;
}

.add-controls select {
  max-width: 180px;
}

.tree {
  flex: 1 1 auto;
  list-style: none;
  margin: 0;
  overflow: auto;
  padding: 10px;
}

.tree ol {
  border-left: 1px solid var(--line);
  list-style: none;
  margin: 0 0 0 17px;
  padding: 3px 0 3px 12px;
}

.tree li {
  margin: 2px 0;
}

.tree-item {
  align-items: center;
  background: #ffffff;
  border: 1px solid transparent;
  border-radius: 6px;
  display: grid;
  gap: 8px;
  grid-template-columns: 28px minmax(0, 1fr) auto;
  min-height: 38px;
  padding: 4px 6px;
}

.tree-item:hover {
  background: #f4f8f9;
  border-color: var(--line);
}

.tree-item.selected {
  background: #e7f5f7;
  border-color: #83cbd2;
}

.tree-toggle {
  align-items: center;
  background: #f2f5f6;
  border: 1px solid var(--line);
  border-radius: 4px;
  color: var(--muted);
  display: inline-flex;
  height: 26px;
  justify-content: center;
  padding: 0;
  width: 26px;
}

.tree-toggle.empty {
  background: transparent;
  border-color: transparent;
}

.node-main {
  min-width: 0;
}

.node-title {
  align-items: center;
  display: flex;
  gap: 7px;
  min-width: 0;
}

.node-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.node-type {
  background: #eef1f2;
  border-radius: 4px;
  color: #3d4a53;
  flex: 0 0 auto;
  font-size: 11px;
  padding: 2px 5px;
}

.node-detail {
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.node-actions {
  display: flex;
  gap: 4px;
}

.node-actions button {
  align-items: center;
  background: #f7f9fa;
  border: 1px solid var(--line);
  border-radius: 4px;
  color: var(--text);
  display: inline-flex;
  height: 26px;
  justify-content: center;
  padding: 0;
  width: 26px;
}

.node-actions .danger-mini {
  color: var(--danger);
}

.empty-tree {
  color: var(--muted);
  padding: 18px 8px;
  text-align: center;
}

.context-menu {
  background: #ffffff;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  box-shadow: var(--shadow);
  min-width: 180px;
  padding: 6px;
  position: fixed;
  z-index: 40;
}

.context-menu button {
  background: transparent;
  border: 0;
  border-radius: 5px;
  color: var(--text);
  display: block;
  min-height: 32px;
  padding: 7px 10px;
  text-align: left;
  width: 100%;
}

.danger-text {
  color: var(--danger) !important;
}

.modal {
  align-items: center;
  background: rgba(10, 18, 24, 0.48);
  display: flex;
  inset: 0;
  justify-content: center;
  padding: 18px;
  position: fixed;
  z-index: 50;
}

.modal[hidden],
.context-menu[hidden] {
  display: none;
}

.modal-panel {
  background: #ffffff;
  border-radius: 8px;
  box-shadow: var(--shadow);
  max-height: min(760px, 92vh);
  max-width: 780px;
  overflow: hidden;
  width: min(780px, 100%);
}

.modal-header,
.modal-actions {
  align-items: center;
  display: flex;
  gap: 10px;
  justify-content: space-between;
  padding: 14px;
}

.modal-header {
  border-bottom: 1px solid var(--line);
}

.modal-header > div {
  min-width: 0;
}

.modal-actions {
  border-top: 1px solid var(--line);
  justify-content: flex-end;
}

.editor-fields {
  display: grid;
  gap: 12px;
  max-height: calc(92vh - 168px);
  overflow: auto;
  padding: 14px;
}

.field-group {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.field-group-title {
  background: var(--panel-soft);
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  padding: 8px 10px;
}

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

.field {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.field input[type="text"] {
  width: 100%;
}

.field-file {
  align-items: center;
  display: flex;
  gap: 8px;
}

.field-file input[type="file"] {
  min-height: 34px;
  width: 100%;
}

.toast {
  background: #182027;
  border-radius: 8px;
  bottom: 18px;
  box-shadow: var(--shadow);
  color: #ffffff;
  left: 50%;
  opacity: 0;
  padding: 10px 14px;
  pointer-events: none;
  position: fixed;
  transform: translate(-50%, 12px);
  transition: opacity 160ms ease, transform 160ms ease;
  z-index: 60;
}

.toast.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (max-width: 980px) {
  .app-shell {
    padding: 10px;
  }

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

  .header-actions {
    justify-content: flex-start;
  }

  .saved-select {
    min-width: 100%;
    width: 100%;
  }

  .meta-bar {
    grid-template-columns: 1fr;
  }

  .workspace {
    grid-template-columns: 1fr;
    height: auto;
  }

  .preview-pane {
    min-height: 520px;
  }

  .structure-pane {
    min-height: 520px;
  }
}

@media (max-width: 620px) {
  .header-actions .button,
  .header-actions select {
    width: 100%;
  }

  .pane-header {
    align-items: stretch;
    flex-direction: column;
  }

  .add-controls {
    display: grid;
    grid-template-columns: 1fr 36px 36px;
  }

  .add-controls select {
    max-width: none;
  }

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

  .modal-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .modal-actions .button {
    width: 100%;
  }
}
