:root {
  --cms-black: #111513;
  --cms-white: #fffdf6;
  --cms-accent: #7cff7c;
  --cms-warn: #ffcf5a;
  --cms-border: rgba(17, 21, 19, .16);
}

.cms-toolbar,
.cms-panel,
.cms-toast {
  font-family: Manrope, Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.cms-toolbar {
  position: fixed;
  left: 50%;
  bottom: 18px;
  z-index: 99999;
  display: flex;
  align-items: center;
  gap: 10px;
  transform: translateX(-50%);
  padding: 10px;
  border: 1px solid rgba(255,255,255,.18);
  border-radius: 999px;
  background: rgba(17, 21, 19, .88);
  box-shadow: 0 20px 70px rgba(0,0,0,.28);
  color: var(--cms-white);
  backdrop-filter: blur(18px);
}

.cms-toolbar strong {
  padding: 0 8px;
  font-size: 13px;
  white-space: nowrap;
}

.cms-toolbar button,
.cms-panel button,
.cms-panel input,
.cms-panel textarea {
  font: inherit;
}

.cms-toolbar button,
.cms-panel button {
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  font-weight: 800;
}

.cms-toolbar button {
  padding: 10px 14px;
  background: rgba(255,255,255,.12);
  color: var(--cms-white);
}

.cms-toolbar button:hover {
  background: rgba(255,255,255,.2);
}

.cms-toolbar .cms-save {
  background: var(--cms-accent);
  color: var(--cms-black);
}

.cms-toolbar .cms-save[disabled] {
  opacity: .55;
  cursor: wait;
}

.cms-panel {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 99999;
  width: min(360px, calc(100vw - 36px));
  max-height: calc(100vh - 36px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  border: 1px solid rgba(255,255,255,.2);
  border-radius: 22px;
  background: rgba(17, 21, 19, .9);
  color: var(--cms-white);
  box-shadow: 0 24px 80px rgba(0,0,0,.32);
  backdrop-filter: blur(18px);
}

.cms-panel[hidden],
.cms-panel [hidden],
.cms-panel label[hidden] {
  display: none !important;
}

.cms-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.cms-panel-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.cms-panel-header-actions .cms-panel-save {
  padding: 9px 14px;
  border: 0;
  border-radius: 999px;
  background: var(--cms-accent);
  color: var(--cms-black);
  font-family: inherit;
  font-weight: 800;
  font-size: 13px;
  cursor: pointer;
}

.cms-panel-header-actions .cms-panel-save[disabled] {
  opacity: .55;
  cursor: wait;
}

.cms-panel-close {
  width: 30px;
  height: 30px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(255,255,255,.14);
  color: var(--cms-white);
  font-size: 18px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}

.cms-panel-close:hover {
  background: rgba(255,255,255,.22);
}

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

.cms-panel p {
  margin: 0 0 12px;
  color: rgba(255,255,255,.72);
  font-size: 12px;
  line-height: 1.45;
}

.cms-panel label {
  display: grid;
  gap: 6px;
  margin-top: 10px;
  color: rgba(255,255,255,.72);
  font-size: 12px;
  font-weight: 800;
}

.cms-panel input,
.cms-panel textarea {
  width: 100%;
  box-sizing: border-box;
  border: 1px solid rgba(255,255,255,.18);
  border-radius: 14px;
  background: rgba(255,255,255,.08);
  color: var(--cms-white);
  outline: none;
  padding: 10px 12px;
}

.cms-panel textarea {
  min-height: 94px;
  resize: vertical;
}

.cms-panel .cms-row {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.cms-history-fields {
  margin-top: 10px;
}

.cms-history-list {
  display: grid;
  gap: 8px;
  max-height: min(52vh, 460px);
  overflow: auto;
  padding-right: 2px;
}

.cms-history-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 16px;
  background: rgba(255,255,255,.07);
}

.cms-history-item strong {
  display: block;
  font-size: 13px;
  color: var(--cms-white);
}

.cms-history-item span,
.cms-history-empty {
  display: block;
  margin: 4px 0 0;
  color: rgba(255,255,255,.62);
  font-size: 11px;
  line-height: 1.35;
}

.cms-history-item button {
  padding: 8px 10px;
  font-size: 12px;
  background: rgba(255,255,255,.14);
  color: var(--cms-white);
}

.cms-history-item:first-child button {
  background: var(--cms-warn);
  color: var(--cms-black);
}

.cms-panel button {
  padding: 10px 12px;
  background: var(--cms-accent);
  color: var(--cms-black);
}

.cms-panel .cms-secondary {
  background: rgba(255,255,255,.12);
  color: var(--cms-white);
}

.cms-toast {
  position: fixed;
  left: 50%;
  top: 18px;
  z-index: 100000;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 12px 16px;
  border-radius: 999px;
  background: var(--cms-black);
  color: var(--cms-white);
  box-shadow: 0 14px 60px rgba(0,0,0,.24);
  font-size: 13px;
  font-weight: 800;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}

.cms-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(4px);
}

body.cms-editing [data-cms-text-key],
body.cms-editing [data-cms-image-key],
body.cms-editing [data-cms-link-key] {
  outline: 1.5px dashed rgba(124, 255, 124, .75);
  outline-offset: 4px;
}

body.cms-editing [data-cms-text-key]:hover,
body.cms-editing [data-cms-image-key]:hover,
body.cms-editing [data-cms-link-key]:hover,
body.cms-editing .cms-selected {
  outline: 3px solid var(--cms-accent) !important;
  outline-offset: 5px;
  cursor: text;
}

body.cms-editing [data-cms-image-key] {
  cursor: copy;
}

body.cms-editing [contenteditable="true"] {
  -webkit-user-modify: read-write-plaintext-only;
}

body.cms-editing [contenteditable="true"]:focus {
  outline: 3px solid var(--cms-warn) !important;
  outline-offset: 5px;
}

body.cms-editing a,
body.cms-editing button,
body.cms-editing .work-open {
  pointer-events: auto;
}

body.cms-editing .work-open {
  display: none;
}

body.cms-saving * {
  cursor: progress !important;
}

/* ----- Repeater cards ----- */

body.cms-editing [data-cms-card] {
  position: relative;
}

body.cms-editing [data-cms-card].cms-card-edit {
  outline: 1.5px dashed rgba(124, 255, 124, .55);
  outline-offset: 6px;
  transition: outline-color .15s ease, transform .15s ease, box-shadow .15s ease;
}

body.cms-editing [data-cms-card].cms-card-edit:hover {
  outline-color: var(--cms-accent);
}

body.cms-editing [data-cms-card].cms-card-dragging {
  opacity: .55;
  transform: scale(.985);
  box-shadow: 0 18px 40px rgba(0,0,0,.18);
}

body.cms-editing .cms-card-controls {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  border-radius: 999px;
  background: rgba(17, 21, 19, .92);
  color: var(--cms-white);
  font-family: Manrope, Inter, system-ui, sans-serif;
  font-size: 13px;
  box-shadow: 0 8px 24px rgba(0,0,0,.18);
  opacity: 0;
  transition: opacity .15s ease;
  pointer-events: none;
}

body.cms-editing [data-cms-card]:hover .cms-card-controls,
body.cms-editing [data-cms-card].cms-selected .cms-card-controls {
  opacity: 1;
  pointer-events: auto;
}

body.cms-editing .cms-card-drag {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  cursor: grab;
  user-select: none;
  font-weight: 800;
  letter-spacing: -1px;
  color: rgba(255,255,255,.85);
}

body.cms-editing .cms-card-drag:active {
  cursor: grabbing;
}

body.cms-editing .cms-card-delete {
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 80, 80, .9);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
  line-height: 1;
}

body.cms-editing .cms-card-delete:hover {
  background: #ff4444;
}

body.cms-editing [data-cms-card] [data-cms-field][data-cms-field-type="text"],
body.cms-editing [data-cms-card] [data-cms-field][data-cms-field-type="link"],
body.cms-editing [data-cms-card] [data-cms-field][data-cms-field-type="image"] {
  outline: 1px dashed rgba(124, 255, 124, .5);
  outline-offset: 3px;
}

body.cms-editing [data-cms-card] [data-cms-field]:hover,
body.cms-editing [data-cms-card] [data-cms-field].cms-selected {
  outline: 2px solid var(--cms-accent) !important;
  outline-offset: 4px;
}

body.cms-editing [data-cms-card] [data-cms-field-type="image"] {
  cursor: copy;
}

.cms-repeater-add {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  width: 100%;
  margin: 14px 0;
  padding: 14px 18px;
  border: 2px dashed rgba(124, 255, 124, .55);
  border-radius: 18px;
  background: rgba(124, 255, 124, .08);
  color: var(--cms-black);
  font-family: Manrope, Inter, system-ui, sans-serif;
  font-weight: 800;
  font-size: 14px;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease;
}

.cms-repeater-add small {
  color: rgba(17, 21, 19, .55);
  font-weight: 600;
  font-size: 11px;
}

.cms-repeater-add:hover {
  background: rgba(124, 255, 124, .18);
  border-color: var(--cms-accent);
}

body:not(.cms-editing) .cms-repeater-add {
  display: none !important;
}

body:not(.cms-editing) .cms-card-controls {
  display: none !important;
}

/* ----- Blocks panel ----- */

.cms-blocks-fields {
  margin-top: 10px;
  display: grid;
  gap: 8px;
}

.cms-blocks-list {
  display: grid;
  gap: 6px;
  max-height: min(52vh, 460px);
  overflow: auto;
  padding-right: 2px;
}

.cms-block-row {
  display: flex !important;
  align-items: center;
  gap: 10px;
  margin: 0 !important;
  padding: 10px 12px;
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 14px;
  background: rgba(255,255,255,.06);
  cursor: pointer;
}

.cms-block-row input[type="checkbox"] {
  width: 18px;
  height: 18px;
  margin: 0;
  cursor: pointer;
  accent-color: var(--cms-accent);
}

.cms-block-row span {
  display: grid;
  gap: 2px;
}

.cms-block-row strong {
  color: var(--cms-white);
  font-size: 13px;
}

.cms-block-row small {
  color: rgba(255,255,255,.55);
  font-size: 11px;
  font-weight: 600;
}

.cms-block-hidden-in-edit {
  position: relative;
  opacity: .35;
  filter: grayscale(.8);
}

.cms-block-hidden-in-edit::before {
  content: 'Спрятан на публичной странице';
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 4;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--cms-warn);
  color: var(--cms-black);
  font: 700 12px/1 Manrope, Inter, system-ui, sans-serif;
  letter-spacing: .02em;
  pointer-events: none;
}

/* ----- Media overlay ----- */

.cms-media-overlay {
  position: fixed;
  inset: 0;
  z-index: 100001;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(8, 10, 9, .72);
  backdrop-filter: blur(10px);
  font-family: Manrope, Inter, system-ui, sans-serif;
}

.cms-media-overlay[hidden] {
  display: none;
}

.cms-media-window {
  width: min(1100px, 100%);
  max-height: 88vh;
  display: grid;
  grid-template-rows: auto 1fr;
  border-radius: 24px;
  background: var(--cms-white);
  color: var(--cms-black);
  overflow: hidden;
  box-shadow: 0 32px 90px rgba(0,0,0,.4);
}

.cms-media-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--cms-border);
}

.cms-media-header strong {
  font-size: 16px;
  font-weight: 800;
}

.cms-media-header [data-cms-media-count] {
  flex: 1;
  color: rgba(17, 21, 19, .55);
  font-size: 12px;
  font-weight: 600;
}

.cms-media-header button {
  padding: 9px 14px;
  border: 0;
  border-radius: 999px;
  font-family: inherit;
  font-weight: 800;
  font-size: 12px;
  cursor: pointer;
}

.cms-media-upload {
  background: var(--cms-accent);
  color: var(--cms-black);
}

.cms-media-close {
  background: rgba(17, 21, 19, .12);
  color: var(--cms-black);
}

.cms-media-grid {
  padding: 18px 20px 24px;
  overflow: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 14px;
}

.cms-media-empty {
  grid-column: 1 / -1;
  padding: 40px 16px;
  text-align: center;
  color: rgba(17, 21, 19, .55);
  font-size: 13px;
}

.cms-media-empty code {
  padding: 2px 6px;
  border-radius: 6px;
  background: rgba(17, 21, 19, .08);
  font: 600 12px/1.2 ui-monospace, Menlo, monospace;
}

.cms-media-card {
  display: grid;
  grid-template-rows: 140px auto auto;
  gap: 6px;
  padding: 8px;
  border: 1px solid var(--cms-border);
  border-radius: 16px;
  background: #fff;
  transition: border-color .15s ease, transform .15s ease;
}

.cms-media-card:hover {
  border-color: var(--cms-accent);
  transform: translateY(-2px);
}

.cms-media-thumb {
  overflow: hidden;
  border-radius: 12px;
  background: rgba(17, 21, 19, .05);
  display: grid;
  place-items: center;
}

.cms-media-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.cms-media-meta {
  padding: 0 4px;
  display: grid;
  gap: 2px;
}

.cms-media-meta strong {
  font-size: 12px;
  font-weight: 800;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.cms-media-meta small {
  color: rgba(17, 21, 19, .5);
  font-size: 11px;
  font-weight: 600;
}

.cms-media-actions {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px;
  padding: 0 4px 4px;
}

.cms-media-actions button {
  padding: 7px 8px;
  border: 0;
  border-radius: 10px;
  background: rgba(17, 21, 19, .08);
  color: var(--cms-black);
  font: 700 11px/1.1 inherit;
  cursor: pointer;
}

.cms-media-actions .cms-media-delete-btn {
  background: rgba(255, 80, 80, .15);
  color: #c0312f;
}

.cms-media-actions .cms-media-delete-btn:hover {
  background: rgba(255, 80, 80, .25);
}

/* ----- Card up / down arrow buttons (touch-friendly reorder) ----- */

body.cms-editing .cms-card-up,
body.cms-editing .cms-card-down {
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(255,255,255,.18);
  color: var(--cms-white);
  font-weight: 800;
  font-size: 14px;
  cursor: pointer;
  line-height: 1;
}

body.cms-editing .cms-card-up:hover,
body.cms-editing .cms-card-down:hover {
  background: rgba(255,255,255,.32);
}

body.cms-editing .cms-card-up[disabled],
body.cms-editing .cms-card-down[disabled] {
  opacity: .35;
  cursor: not-allowed;
}

/* ----- Mobile / touch ----- */

@media (max-width: 720px) {
  .cms-toolbar {
    left: 8px;
    right: 8px;
    bottom: 8px;
    transform: none;
    justify-content: center;
    border-radius: 16px;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px;
  }

  .cms-toolbar strong {
    width: 100%;
    text-align: center;
    padding: 4px 0;
    font-size: 11px;
    letter-spacing: .05em;
    text-transform: uppercase;
    color: rgba(255,255,255,.55);
  }

  .cms-toolbar button {
    flex: 1 1 calc(33% - 6px);
    padding: 12px 8px;
    font-size: 13px;
    border-radius: 12px;
    min-height: 44px;
  }

  .cms-toolbar .cms-save {
    flex-basis: 100%;
    order: -1;
    font-size: 15px;
  }

  .cms-panel {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-height: 70vh;
    border-radius: 22px 22px 0 0;
    border: 0;
    border-top: 1px solid rgba(255,255,255,.18);
    padding: 18px 18px 28px;
    box-shadow: 0 -20px 60px rgba(0,0,0,.4);
  }

  body.cms-editing.cms-panel-open .cms-toolbar {
    display: none;
  }

  .cms-panel h3 {
    font-size: 17px;
    margin-bottom: 10px;
  }

  .cms-panel input,
  .cms-panel textarea {
    padding: 14px 14px;
    font-size: 16px; /* предотвращает зум iOS */
  }

  .cms-panel-actions button {
    min-height: 46px;
    font-size: 14px;
  }

  /* Карточки на тач: контролы всегда видны, крупнее */
  body.cms-editing .cms-card-controls {
    opacity: 1 !important;
    pointer-events: auto !important;
    gap: 8px;
    padding: 6px 8px;
    top: 6px;
    right: 6px;
  }

  body.cms-editing .cms-card-controls .cms-card-up,
  body.cms-editing .cms-card-controls .cms-card-down,
  body.cms-editing .cms-card-controls .cms-card-delete {
    width: 32px;
    height: 32px;
    font-size: 16px;
  }

  /* На тач-устройствах прячем "потянуть мышью" ⋮⋮ —
     им неудобно пользоваться, и есть кнопки ↑↓. */
  body.cms-editing .cms-card-drag {
    display: none;
  }

  .cms-repeater-add {
    padding: 18px;
    font-size: 15px;
    min-height: 56px;
  }

  /* Медиатека на мобиле — на весь экран */
  .cms-media-overlay {
    padding: 0;
  }

  .cms-media-window {
    width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
  }

  .cms-media-header {
    padding: 14px 16px;
    flex-wrap: wrap;
  }

  .cms-media-header strong {
    flex: 1 1 100%;
    margin-bottom: 4px;
  }

  .cms-media-grid {
    padding: 14px;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 10px;
  }

  .cms-media-card {
    grid-template-rows: 110px auto auto;
  }

  .cms-block-row {
    padding: 14px;
  }

  .cms-block-row input[type="checkbox"] {
    width: 22px;
    height: 22px;
  }
}

@media (hover: none) and (pointer: coarse) {
  /* На тач-устройстве (даже широкоэкранный планшет) карточные контролы всегда видны */
  body.cms-editing .cms-card-controls {
    opacity: 1;
    pointer-events: auto;
  }
}
