:root {
  color-scheme: dark;
  --bg: #11110f;
  --panel: #454743;
  --panel-dark: #282826;
  --panel-darker: #181917;
  --line: #111;
  --line-soft: #30302e;
  --text: #eee6d0;
  --muted: #bcb4a4;
  --field: #a7a7a1;
  --field-text: #111;
  --accent: #f2a12a;
  --accent-strong: #ffc24f;
  --danger: #ff7468;
  --danger-soft: rgba(255, 116, 104, 0.14);
  --editor-min-width: 400px;
  --editor-max-width: 860px;
  --preview-column-min: 430px;
  --shadow-inset: inset 0 1px 0 rgba(255, 255, 255, 0.1), inset 0 -1px 0 rgba(0, 0, 0, 0.5);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow: hidden;
  color: var(--text);
  background: var(--bg);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  min-height: 32px;
  border: 2px solid var(--line);
  border-radius: 2px;
  background: linear-gradient(#777872, #4b4c47);
  color: #111;
  font-weight: 900;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.32);
  cursor: pointer;
}

button:hover {
  filter: brightness(1.08);
}

button:disabled {
  cursor: not-allowed;
  filter: grayscale(0.4);
  opacity: 0.55;
}

button.primary {
  background: linear-gradient(#79d76f, #43aa46);
  color: #0d1b0b;
}

button.danger {
  color: #2a0704;
  background: linear-gradient(#ff958b, #d95449);
}

input,
select,
textarea {
  width: 100%;
  height: 30px;
  border: 2px solid var(--line);
  border-radius: 2px;
  background: var(--field);
  color: var(--field-text);
  font-weight: 800;
}

input,
select {
  padding: 0 8px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  outline: none;
}

input[type="checkbox"] {
  width: 17px;
  height: 17px;
  padding: 0;
  accent-color: var(--accent);
}

input[type="range"] {
  height: 24px;
  padding: 0;
  accent-color: var(--accent);
}

.main,
.single-screen,
.panel.active {
  min-width: 0;
  min-height: 0;
  height: 100vh;
}

.panel {
  display: none;
}

.panel.active {
  display: block;
}

.main.single-screen {
  min-height: 100vh;
}

#visualPanel {
  background: #20201e;
}

.mapgen-window {
  display: grid;
  grid-template-rows: 36px auto minmax(0, 1fr);
  height: 100vh;
  border-top: 2px solid var(--line);
  background: #242422;
}

.mapgen-titlebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-width: 0;
  border-bottom: 2px solid var(--line);
  background:
    linear-gradient(90deg, rgba(0, 0, 0, 0.28), transparent 42%),
    repeating-linear-gradient(90deg, #3c3d3a 0, #3c3d3a 2px, #30312f 2px, #30312f 5px);
}

.mapgen-title-group {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.mapgen-title {
  padding: 0 0 0 12px;
  color: var(--text);
  font-size: 18px;
  font-weight: 900;
  text-shadow: 0 1px 0 #000;
  white-space: nowrap;
}

.factorio-version {
  overflow: hidden;
  max-width: min(420px, 42vw);
  padding: 3px 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(0, 0, 0, 0.22);
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.factorio-version.update {
  border-color: rgba(255, 194, 79, 0.46);
  color: var(--accent-strong);
}

.factorio-version.error {
  border-color: rgba(255, 116, 104, 0.38);
  color: #ffb8af;
}

.mapgen-status {
  overflow: hidden;
  padding: 0 12px;
  color: var(--muted);
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mapgen-toolbar {
  display: grid;
  grid-template-columns: minmax(360px, 780px) minmax(300px, max-content);
  align-items: center;
  justify-content: start;
  gap: 6px;
  padding: 4px 6px;
  border-bottom: 2px solid var(--line);
  background: #252423;
}

.toolbar-group,
.seed-box,
.preview-tools {
  min-height: 34px;
  border: 2px solid var(--line);
  background: #3d3e3a;
  box-shadow: var(--shadow-inset);
}

.toolbar-label {
  color: var(--text);
  font-size: 12px;
  font-weight: 900;
  white-space: nowrap;
}

.edit-preset-group {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  width: min(100%, 780px);
  min-width: 0;
  padding: 0 6px;
}

.preset-box {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.preset-box select {
  height: 30px;
  min-width: 0;
  font-weight: 900;
}

.preset-box select option {
  background: var(--field);
  color: var(--field-text);
}

.seed-box {
  display: grid;
  grid-template-columns: auto minmax(92px, 150px) auto auto;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
}

.seed-box > label:first-child,
.preview-tools > label,
.preview-tools .check-field span {
  color: var(--text);
  font-weight: 900;
}

.seed-reroll {
  min-height: 28px;
  padding: 0 8px;
  white-space: nowrap;
}

.seed-random {
  white-space: nowrap;
}

.check-field {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 30px;
}

.check-field span {
  color: var(--text);
  font-size: 13px;
  font-weight: 800;
}

.preview-tools {
  display: grid;
  grid-template-columns: auto 110px 104px minmax(96px, auto);
  align-items: center;
  gap: 8px;
  padding: 0 6px;
}

.preview-button {
  min-width: 96px;
  min-height: 30px;
  padding: 0 14px;
  background: linear-gradient(#ffc24f, #e38a1f);
  color: #17100a;
}

.mapgen-body {
  display: grid;
  grid-template-columns:
    minmax(var(--editor-min-width), min(var(--editor-max-width), 42vw))
    minmax(var(--preview-column-min), 1fr);
  min-height: 0;
}

.mapgen-controls {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-height: 0;
  border-right: 3px solid var(--line);
  background: #2a2928;
}

.mapgen-subtabs {
  display: flex;
  gap: 2px;
  padding: 12px 10px 0;
  border-bottom: 2px solid var(--line);
  background: #282625;
}

.mapgen-subtab {
  min-height: 40px;
  padding: 0 22px;
  border: 2px solid var(--line);
  border-bottom: 0;
  border-radius: 5px 5px 0 0;
  background: linear-gradient(#d8d6d0, #8f8f88);
  color: #111;
}

.mapgen-subtab.active {
  background: linear-gradient(#ffc24f, #e38a1f);
  color: #111;
}

.mapgen-subpanel {
  display: none;
  grid-row: 2;
  min-height: 0;
  overflow: auto;
  padding: 8px;
  border-top: 2px solid #4e4f4a;
  background: var(--panel);
}

.mapgen-subpanel.active {
  display: block;
}

.mapgen-editor-actions {
  display: flex;
  flex-wrap: wrap;
  grid-row: 3;
  justify-content: flex-end;
  gap: 6px;
  padding: 8px;
  border-top: 2px solid var(--line);
  background: #282625;
}

.mapgen-editor-actions button {
  min-height: 30px;
  padding: 0 10px;
}

.table-caption {
  display: grid;
  grid-template-columns: minmax(180px, 1.5fr) minmax(110px, 1fr) minmax(110px, 1fr) minmax(110px, 1fr);
  align-items: center;
  min-height: 42px;
  border: 1px solid #222;
  border-bottom: 0;
  background: #3d3f3b;
  color: #ddd7c8;
  font-weight: 900;
}

.table-caption span {
  padding: 0 10px;
  border-left: 1px solid #222;
}

.table-caption span:first-child {
  border-left: 0;
}

#terrainSubpanel .table-caption,
#enemySubpanel .table-caption,
#advancedSubpanel .table-caption {
  grid-template-columns: minmax(180px, 1.5fr) minmax(110px, 1fr);
}

.advanced-groups {
  display: grid;
  gap: 14px;
}

.field-grid,
.control-stack,
.autoplace-grid {
  display: grid;
  gap: 0;
}

.field,
.slider-field {
  display: grid;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.field label,
.slider-field label {
  color: #ded4c2;
  font-size: 13px;
  font-weight: 900;
}

.slider-field {
  grid-template-columns: minmax(120px, 1fr) minmax(130px, 2fr) 82px;
}

.slider-field input[type="number"] {
  text-align: right;
}

.autoplace-grid .preset-field {
  grid-template-columns: minmax(180px, 1.5fr) minmax(0, 3fr);
  min-height: 54px;
  padding: 8px 10px;
  border: 1px solid #222;
  border-top: 0;
  background: #484a46;
}

.autoplace-row {
  display: grid;
  grid-template-columns: minmax(180px, 1.5fr) minmax(0, 3fr);
  gap: 0;
  min-height: 58px;
  border: 1px solid #222;
  border-top: 0;
  background: #484a46;
}

.resource-name {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 0 12px;
  border-right: 1px solid #222;
  font-weight: 900;
}

.resource-name span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.swatch {
  flex: 0 0 auto;
  width: 18px;
  height: 18px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 4px;
}

.resource-controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0;
  min-width: 0;
}

.resource-controls .slider-field {
  grid-template-columns: minmax(0, 1fr) 64px;
  min-height: 58px;
  padding: 0 10px;
  border-left: 1px solid #222;
}

.resource-controls .slider-field:first-child {
  border-left: 0;
}

.resource-controls .slider-field label {
  display: none;
}

.factorio-control-stack .slider-field,
.factorio-control-stack .field,
.factorio-control-stack .preset-field,
.factorio-control-stack .preset-slider-field,
.factorio-field-grid .field,
.factorio-field-grid .check-field,
.factorio-field-grid .preset-field,
.factorio-field-grid .preset-slider-field,
.space-age-group .preset-field {
  min-height: 54px;
  margin: 0;
  padding: 8px 10px;
  border: 1px solid #222;
  border-top: 0;
  background: #484a46;
}

.factorio-control-stack .slider-field {
  grid-template-columns: minmax(180px, 1.5fr) minmax(120px, 1fr) 82px;
}

.factorio-control-stack .field,
.factorio-control-stack .preset-field,
.factorio-control-stack .preset-slider-field,
.factorio-field-grid .field,
.factorio-field-grid .preset-field,
.factorio-field-grid .preset-slider-field,
.space-age-group .preset-field {
  grid-template-columns: minmax(180px, 1.5fr) minmax(120px, 1fr);
}

.space-age-controls {
  display: grid;
  gap: 12px;
}

.space-age-group {
  display: grid;
  gap: 0;
  border-bottom: 1px solid #222;
}

.space-age-group-header {
  display: flex;
  align-items: center;
  min-height: 36px;
  padding: 0 10px;
  border: 1px solid #222;
  border-top: 0;
  background: #3d3f3b;
}

.space-age-group-header h3 {
  margin: 0;
  color: #ddd7c8;
  font-size: 13px;
  font-weight: 900;
}

.preset-field,
.preset-slider-field {
  display: grid;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.preset-field > label,
.preset-slider-field > label {
  color: #ded4c2;
  font-size: 13px;
  font-weight: 900;
}

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

.slider-input-row,
.paired-inputs {
  display: grid;
  grid-template-columns: minmax(100px, 1fr) 82px;
  gap: 8px;
  min-width: 0;
}

.paired-inputs {
  grid-template-columns: repeat(2, minmax(82px, 1fr));
}

.preset-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  min-width: 0;
}

.tooltip-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.tooltip-mark {
  display: inline-grid;
  flex: 0 0 auto;
  place-items: center;
  width: 15px;
  height: 15px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 50%;
  color: var(--muted);
  cursor: help;
  font-size: 10px;
  font-weight: 900;
  line-height: 1;
}

.tooltip-mark:focus-visible {
  outline: 2px solid var(--accent-strong);
  outline-offset: 2px;
}

.check-field .tooltip-mark,
.resource-name .tooltip-mark {
  margin-left: 2px;
}

.preset-button {
  min-height: 24px;
  padding: 0 8px;
  font-size: 11px;
  line-height: 1;
}

.preset-button.active {
  background: linear-gradient(#ffc24f, #e38a1f);
  color: #17100a;
}

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

.mapgen-preview-pane {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  padding: 8px;
  background: var(--panel-darker);
}

.mapgen-preview-pane .preview-tools {
  flex: 0 0 auto;
  width: min(100%, max(var(--preview-size, 768px), 414px));
  margin-bottom: 8px;
}

.preview-scroll {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  max-width: 100%;
  max-height: 100%;
  overflow: auto;
}

.preview-frame {
  display: block;
  width: var(--preview-size, 768px);
  height: var(--preview-size, 768px);
  min-height: 0;
  overflow: hidden;
  position: relative;
  background: #102326;
  box-shadow: 0 0 0 3px var(--line);
}

.preview-frame img {
  display: none;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  object-fit: fill;
}

.preview-frame img.updating {
  filter: brightness(0.72) saturate(0.9);
}

.preview-empty {
  display: grid;
  place-items: center;
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  min-height: 100%;
  color: var(--muted);
  font-size: 14px;
}

.preview-empty.updating {
  display: grid;
  grid-auto-flow: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: #ffe0a3;
  background: rgba(13, 14, 12, 0.48);
  font-weight: 700;
  letter-spacing: 0;
}

.preview-empty.updating::after {
  content: "";
  width: 22px;
  height: 22px;
  margin-left: 0;
  border: 3px solid rgba(255, 224, 163, 0.25);
  border-top-color: #ffb23c;
  border-radius: 50%;
  animation: preview-spin 0.8s linear infinite;
}

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

.preview-status {
  flex: 0 0 auto;
  width: min(100%, max(var(--preview-size, 768px), 414px));
  min-height: 20px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}

.preview-status.error {
  max-height: 180px;
  overflow: auto;
  padding: 10px 12px;
  border: 1px solid rgba(255, 116, 104, 0.34);
  border-radius: 4px;
  background: var(--danger-soft);
  color: #ffb8af;
  white-space: pre-wrap;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  max-width: min(520px, calc(100vw - 44px));
  padding: 12px 12px 12px 14px;
  border: 2px solid var(--line-soft);
  border-radius: 4px;
  background: #20241d;
  color: var(--text);
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.34);
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: opacity 150ms ease, transform 150ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.toast.error {
  border-color: #ff2f2f;
  background: #2b0f0d;
  color: #ffd3cf;
  box-shadow: 0 0 0 2px rgba(255, 47, 47, 0.32), 0 18px 42px rgba(0, 0, 0, 0.42);
}

.toast-message {
  min-width: 0;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.toast-close {
  min-height: 28px;
  padding: 0 8px;
  border-color: rgba(255, 255, 255, 0.28);
  background: rgba(255, 255, 255, 0.08);
  color: inherit;
  white-space: nowrap;
}

.toast.error .toast-close {
  border-color: rgba(255, 211, 207, 0.55);
  background: rgba(255, 47, 47, 0.18);
}

@media (max-width: 1180px) {
  body {
    overflow: auto;
  }

  .single-screen,
  .panel.active,
  .mapgen-window {
    height: auto;
    min-height: 100vh;
  }

  .mapgen-toolbar {
    grid-template-columns: minmax(0, 1fr) minmax(300px, auto);
  }

  .mapgen-editor-actions {
    justify-content: flex-start;
  }

  .mapgen-body {
    grid-template-columns: 1fr;
  }

  .mapgen-controls {
    border-right: 0;
    border-bottom: 3px solid var(--line);
  }

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

@media (max-width: 680px) {
  .mapgen-window {
    grid-template-rows: auto auto minmax(0, 1fr);
  }

  .mapgen-titlebar {
    align-items: flex-start;
    flex-direction: column;
    padding: 6px 0;
  }

  .mapgen-title-group {
    align-items: flex-start;
    flex-direction: column;
    gap: 4px;
    width: 100%;
  }

  .factorio-version {
    max-width: calc(100vw - 24px);
    margin-left: 12px;
  }

  .mapgen-toolbar,
  .edit-preset-group,
  .seed-box,
  .preview-tools,
  .table-caption,
  #terrainSubpanel .table-caption,
  #enemySubpanel .table-caption,
  #advancedSubpanel .table-caption,
  .autoplace-grid .preset-field,
  .autoplace-row,
  .resource-controls,
  .resource-controls .slider-field,
  .factorio-control-stack .slider-field,
  .factorio-control-stack .field,
  .factorio-control-stack .preset-field,
  .factorio-control-stack .preset-slider-field,
  .factorio-field-grid .field,
  .factorio-field-grid .preset-field,
  .factorio-field-grid .preset-slider-field,
  .space-age-group .preset-field {
    grid-template-columns: 1fr;
  }

  .slider-input-row,
  .paired-inputs {
    grid-template-columns: 1fr;
  }

  .mapgen-editor-actions {
    flex-direction: column;
  }

  .mapgen-editor-actions button {
    width: 100%;
  }

  .mapgen-subtabs {
    overflow-x: auto;
  }

  .mapgen-subtab {
    min-width: 120px;
    padding: 0 14px;
  }
}

.session-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
  padding-right: 8px;
}

.current-user {
  color: var(--text);
  font-size: 13px;
  font-weight: 900;
  white-space: nowrap;
}

.small-button {
  min-height: 26px;
  padding: 0 10px;
  font-size: 12px;
}

.auth-overlay,
.admin-overlay,
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  place-items: center;
  padding: 22px;
  background: rgba(8, 8, 7, 0.82);
}

.auth-overlay.active,
.admin-overlay:not([hidden]),
.modal-overlay:not([hidden]) {
  display: grid;
}

.auth-dialog,
.admin-dialog,
.modal-dialog {
  width: min(100%, 420px);
  border: 2px solid var(--line);
  background: #33332f;
  box-shadow: 0 18px 52px rgba(0, 0, 0, 0.48);
}

.auth-dialog,
.modal-dialog {
  display: grid;
  gap: 12px;
  padding: 18px;
}

.modal-dialog {
  width: min(100%, 460px);
}

.auth-dialog h1,
.admin-dialog h2,
.admin-dialog h3,
.modal-dialog h2 {
  margin: 0;
  color: var(--text);
}

.auth-dialog label,
.modal-dialog label {
  display: grid;
  gap: 5px;
  color: var(--text);
  font-size: 13px;
  font-weight: 900;
}

.form-error {
  min-height: 18px;
  color: #ffb8af;
  font-size: 13px;
}

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

.guest-user {
  color: var(--muted);
}


.admin-dialog {
  display: grid;
  gap: 12px;
  width: min(100%, 980px);
  max-height: min(760px, calc(100vh - 44px));
  overflow: auto;
  padding: 14px;
}

.modal-header,
.modal-actions,
.admin-header,
.admin-create,
.user-row {
  display: grid;
  align-items: center;
  gap: 8px;
}

.modal-header,
.admin-header {
  grid-template-columns: minmax(0, 1fr) auto;
}

.modal-actions {
  grid-template-columns: minmax(0, 1fr) auto;
  justify-items: end;
}

.admin-create {
  grid-template-columns: minmax(140px, 1fr) minmax(140px, 1fr) auto auto;
  padding: 10px;
  border: 1px solid #222;
  background: #454743;
}

.admin-section {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid #222;
  background: #454743;
}

.admin-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.factorio-admin-status {
  display: grid;
  gap: 4px;
}

.factorio-admin-line {
  display: grid;
  grid-template-columns: 140px minmax(0, 1fr);
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
}

.factorio-admin-line span {
  font-weight: 900;
}

.factorio-admin-line strong {
  overflow-wrap: anywhere;
  color: var(--text);
}

.factorio-admin-line.update strong {
  color: var(--accent-strong);
}

.factorio-admin-line.error strong {
  color: #ffb8af;
}

.admin-list,
.audit-list {
  display: grid;
  gap: 6px;
}

.user-row {
  grid-template-columns: minmax(140px, 1fr) auto minmax(140px, 1fr) auto auto;
  padding: 8px;
  border: 1px solid #222;
  background: #484a46;
}

.audit-row {
  display: grid;
  gap: 4px;
  padding: 8px 10px;
  border: 1px solid #222;
  background: #282826;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

.audit-row span {
  overflow-wrap: anywhere;
  color: var(--muted);
}

@media (max-width: 760px) {
  .session-bar,
  .modal-header,
  .modal-actions,
  .admin-create,
  .user-row,
  .factorio-admin-line {
    grid-template-columns: 1fr;
  }

  .session-bar {
    align-items: stretch;
    width: 100%;
    padding: 0 8px;
  }
}
