/* Level editor screen: inside the game page (index.html, Editor in the main menu) and on editor.html.
   Desktop layout: toolbar, tools | map | properties, status bar. All rules are scoped to .editor-screen,
   so they never touch the game page. */

.editor-screen *,
.editor-screen *::before,
.editor-screen *::after {
  box-sizing: border-box;
}

.editor-screen {
  --editor-background: #1d2330;
  --editor-panel: #232a39;
  --editor-panel-border: #343d50;
  --editor-control: #2d3546;
  --editor-control-hover: #3a4459;
  --editor-text: #e8ecf3;
  --editor-text-muted: #9aa4b8;
  --editor-accent: #3b82f6;
  --editor-danger: #ef4444;
  --editor-ok: #22c55e;
}

/* editor.html: the screen fills the page */
body.editor-page {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  background: #1d2330;
}

/* Covers the whole page; in the game page also the game and its overlays */
.editor-screen {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: var(--editor-background);
  color: var(--editor-text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 13px;
  line-height: normal;
  text-align: left;
  touch-action: auto;
  user-select: auto;
  -webkit-user-select: auto;
}

.editor-screen[hidden] {
  display: none;
}

.editor-screen button,
.editor-screen input,
.editor-screen select,
.editor-screen textarea {
  font: inherit;
  color: inherit;
}

.editor-screen button {
  padding: 5px 10px;
  border: 1px solid var(--editor-panel-border);
  border-radius: 6px;
  background: var(--editor-control);
  cursor: pointer;
}

.editor-screen button:hover:not(:disabled) {
  background: var(--editor-control-hover);
}

.editor-screen button:disabled {
  opacity: 0.45;
  cursor: default;
}

.editor-screen button:focus-visible,
.editor-screen input:focus-visible,
.editor-screen select:focus-visible,
.editor-screen textarea:focus-visible {
  outline: 2px solid var(--editor-accent);
  outline-offset: 1px;
}

/* ---- toolbar ---- */

.editor-screen .editor-toolbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--editor-panel-border);
  background: var(--editor-panel);
}

.editor-screen .editor-toolbar__title {
  font-weight: 700;
  font-size: 15px;
}

.editor-screen .editor-toolbar__group {
  display: flex;
  gap: 6px;
}

.editor-screen .editor-toolbar__primary {
  border-color: var(--editor-accent);
  background: var(--editor-accent);
  color: #ffffff;
  font-weight: 600;
}

.editor-screen .editor-toolbar__primary:hover:not(:disabled) {
  background: #2f6fd6;
}

.editor-screen .editor-toolbar__spacer {
  flex: 1;
}

.editor-screen .editor-toolbar__link {
  color: var(--editor-text-muted);
}

/* ---- main layout ---- */

.editor-screen .editor-layout {
  display: grid;
  grid-template-columns: 230px 1fr 290px;
  min-height: 0;
}

.editor-screen .editor-panel {
  overflow-y: auto;
  padding: 4px 12px 12px;
  background: var(--editor-panel);
}

.editor-screen .editor-panel:first-child {
  border-right: 1px solid var(--editor-panel-border);
}

.editor-screen .editor-panel:last-child {
  border-left: 1px solid var(--editor-panel-border);
}

.editor-screen .editor-view {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.editor-screen .editor-view canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.editor-screen .panel-section h2 {
  margin: 14px 0 8px;
  color: var(--editor-text-muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* ---- tools ---- */

.editor-screen .tool-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.editor-screen .tool-button {
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-align: left;
}

.editor-screen .tool-button--active {
  border-color: var(--editor-accent);
  background: rgba(59, 130, 246, 0.25);
}

.editor-screen kbd {
  padding: 0 5px;
  border: 1px solid var(--editor-panel-border);
  border-radius: 4px;
  color: var(--editor-text-muted);
  font-family: inherit;
  font-size: 11px;
}

.editor-screen .terrain-picker {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 8px;
}

.editor-screen .terrain-button {
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
}

.editor-screen .terrain-button--active {
  border-color: var(--editor-accent);
  background: rgba(59, 130, 246, 0.25);
}

.editor-screen .terrain-button__swatch {
  width: 16px;
  height: 16px;
  border: 1px solid rgba(0, 0, 0, 0.5);
  border-radius: 3px;
}

/* ---- form fields ---- */

.editor-screen .field {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin-bottom: 8px;
}

.editor-screen .field__label {
  color: var(--editor-text-muted);
  font-size: 12px;
}

.editor-screen .field input,
.editor-screen .field select,
.editor-screen .field textarea {
  width: 100%;
  padding: 5px 7px;
  border: 1px solid var(--editor-panel-border);
  border-radius: 6px;
  background: var(--editor-control);
}

.editor-screen .field textarea {
  resize: vertical;
}

.editor-screen .field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

/* ---- ground colors (map panel) ---- */

.editor-screen .panel-subtitle {
  margin: 14px 0 8px;
  color: var(--editor-text-muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.editor-screen .color-row {
  display: flex;
  gap: 6px;
}

.editor-screen .field .color-input {
  width: 40px;
  height: 28px;
  padding: 2px;
  cursor: pointer;
}

.editor-screen .color-reset {
  height: 28px;
  padding: 0 10px;
  font-size: 12px;
}

.editor-screen .owner-select {
  border-left: 6px solid var(--owner-color) !important;
}

.editor-screen .panel-button {
  width: 100%;
  margin-bottom: 8px;
}

.editor-screen .panel-button--danger {
  border-color: rgba(239, 68, 68, 0.6);
  color: #fca5a5;
}

.editor-screen .selection-title {
  margin: 0 0 8px;
  font-weight: 600;
}

.editor-screen .hint {
  margin: 0 0 8px;
  color: var(--editor-text-muted);
  line-height: 1.4;
}

.editor-screen .shortcut-list {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 10px;
  margin: 0;
  color: var(--editor-text-muted);
}

.editor-screen .shortcut-list dt {
  color: var(--editor-text);
}

.editor-screen .shortcut-list dd {
  margin: 0;
}

/* ---- problems ---- */

.editor-screen .problem-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.editor-screen .problem {
  margin-bottom: 6px;
  padding: 6px 8px;
  border-left: 3px solid var(--editor-danger);
  border-radius: 4px;
  background: rgba(239, 68, 68, 0.12);
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.editor-screen .problem--none {
  border-left-color: var(--editor-ok);
  background: rgba(34, 197, 94, 0.12);
}

/* ---- status bar ---- */

.editor-screen .editor-status {
  display: grid;
  grid-template-columns: 180px 1fr auto;
  gap: 12px;
  padding: 5px 12px;
  border-top: 1px solid var(--editor-panel-border);
  background: var(--editor-panel);
  color: var(--editor-text-muted);
  font-variant-numeric: tabular-nums;
}

.editor-screen .editor-status__message {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: var(--editor-text);
}

.editor-screen .editor-status__message--problem {
  color: #fca5a5;
}

/* ---- open dialog (map of Custom Maps or a file) ---- */

.editor-screen .editor-dialog {
  width: min(440px, calc(100vw - 32px));
  padding: 16px;
  border: 1px solid var(--editor-panel-border);
  border-radius: 10px;
  background: var(--editor-panel);
  color: var(--editor-text);
}

.editor-screen .editor-dialog::backdrop {
  background: rgba(10, 12, 18, 0.6);
}

.editor-screen .editor-dialog h2 {
  margin: 0 0 12px;
  font-size: 15px;
}

.editor-screen .editor-dialog__list {
  display: grid;
  gap: 6px;
  max-height: 50vh;
  margin: 0 0 12px;
  padding: 0;
  overflow-y: auto;
  list-style: none;
}

.editor-screen .editor-dialog__map {
  display: grid;
  width: 100%;
  gap: 2px;
  text-align: left;
}

.editor-screen .editor-dialog__detail,
.editor-screen .editor-dialog__note {
  color: var(--editor-text-muted);
  font-size: 12px;
}

.editor-screen .editor-dialog__actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* ---- buttons only needed without keyboard (compact layout) ---- */

.editor-screen .editor-toolbar__compact-only {
  display: none;
}

.editor-screen .editor-toolbar__panel.has-problems::after {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-left: 5px;
  border-radius: 50%;
  background: var(--editor-danger);
  vertical-align: middle;
}

.editor-screen .editor-toolbar__panel[aria-pressed="true"] {
  border-color: var(--editor-accent);
  background: rgba(59, 130, 246, 0.25);
}

.editor-screen .editor-view canvas {
  touch-action: none; /* pan and pinch are handled by the editor (editorTouch.js) */
}

.editor-screen .editor-panel {
  touch-action: pan-y; /* panels scroll with a finger */
}

/* ---- compact layout: phones in landscape and small windows ---- */

@media (max-width: 899px), (max-height: 499px) {
  .editor-screen {
    font-size: 12px;
  }

  .editor-screen button {
    padding: 6px 8px;
    min-height: 32px;
  }

  .editor-screen .editor-toolbar {
    gap: 6px;
    padding: max(4px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) 4px max(6px, env(safe-area-inset-left));
    overflow-x: auto;
    touch-action: pan-x;
  }

  .editor-screen .editor-toolbar__title,
  .editor-screen .editor-toolbar__spacer,
  .editor-screen .editor-toolbar__long {
    display: none;
  }

  .editor-screen .editor-toolbar__group {
    gap: 4px;
  }

  .editor-screen .editor-toolbar__compact-only {
    display: inline-block;
  }

  .editor-screen .editor-layout {
    position: relative;
    grid-template-columns: 104px 1fr;
  }

  .editor-screen .editor-panel {
    padding: 2px 6px 8px;
  }

  .editor-screen .editor-panel:first-child {
    padding-left: max(6px, env(safe-area-inset-left));
  }

  .editor-screen .panel-section--shortcuts,
  .editor-screen .tool-button kbd {
    display: none;
  }

  .editor-screen .tool-list {
    gap: 4px;
  }

  .editor-screen .tool-button {
    justify-content: center;
    text-align: center;
  }

  /* The right column becomes a drawer over the map, opened with the Panel button */
  .editor-screen .editor-panel--drawer {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 2;
    width: min(320px, 72vw);
    padding-right: max(8px, env(safe-area-inset-right));
    box-shadow: -8px 0 24px rgba(0, 0, 0, 0.45);
    transform: translateX(105%);
    transition: transform 0.2s ease;
  }

  .editor-screen.is-drawer-open .editor-panel--drawer {
    transform: none;
  }

  .editor-screen .editor-status {
    grid-template-columns: 1fr;
    padding: 3px max(8px, env(safe-area-inset-left)) max(3px, env(safe-area-inset-bottom));
  }

  .editor-screen .editor-status > :not(.editor-status__message) {
    display: none;
  }
}

/* ---- touch device held upright: the editor needs landscape ---- */

.editor-screen .portrait-note {
  display: none;
}

@media (pointer: coarse) and (orientation: portrait) {
  .editor-screen .portrait-note {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 24px;
    background: var(--editor-background);
    font-size: 17px;
    font-weight: 700;
    text-align: center;
  }

  .editor-screen .portrait-note p {
    margin: 0;
  }
}
