:root {
  color-scheme: light dark;
  --bg: #f5f7fb;
  --panel: #ffffff;
  --text: #1b1f24;
  --muted: #667085;
  --border: #cfd6df;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --cell-size: 34px;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151b;
    --panel: #1a2029;
    --text: #edf2f7;
    --muted: #aab3c0;
    --border: #3a4554;
    --accent: #60a5fa;
    --accent-text: #0f172a;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
}

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

button,
input,
select,
textarea {
  border: 1px solid var(--border);
  border-radius: 7px;
}

button {
  cursor: pointer;
  padding: 0.55rem 0.75rem;
  background: var(--panel);
  color: var(--text);
}

button:hover {
  border-color: var(--accent);
}

button.active,
button.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

button:disabled {
  opacity: 0.45;
  cursor: default;
}

input,
select,
textarea {
  width: 100%;
  padding: 0.5rem 0.6rem;
  background: var(--panel);
  color: var(--text);
}

textarea {
  resize: vertical;
}

.site-header {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.sidebar-toggle {
  flex: 0 0 auto;
  white-space: nowrap;
}

.save-status {
  display: inline-flex;
  align-items: center;
  min-height: 2rem;
  padding: 0.25rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.85rem;
  white-space: nowrap;
}

.save-status.saved {
  color: var(--muted);
}

.save-status.unsaved {
  border-color: #b45309;
  color: #b45309;
}

.site-header h1,
.site-header p {
  margin: 0;
}

.site-header h1 {
  font-size: 1.35rem;
}

.site-header p {
  margin-top: 0.2rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.app {
  position: relative;
  display: grid;
  flex: 1 1 auto;
  grid-template-columns: 290px minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
  transition: grid-template-columns 180ms ease;
}

.app.sidebar-closed {
  grid-template-columns: 0 minmax(0, 1fr);
}

.sidebar {
  width: 290px;
  min-width: 290px;
  height: 100%;
  overflow-y: auto;
  padding: 1rem;
  border-right: 1px solid var(--border);
  background: var(--panel);
  transform: translateX(0);
  transition: transform 180ms ease;
}

.app.sidebar-closed .sidebar {
  transform: translateX(-100%);
  pointer-events: none;
}

.panel {
  padding-bottom: 1rem;
  margin-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.panel:last-child {
  border-bottom: 0;
}

.panel h2,
.workspace h2 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
}

.panel label {
  display: block;
  margin-bottom: 0.7rem;
  font-size: 0.9rem;
}

.panel label input,
.panel label select,
.panel label textarea {
  margin-top: 0.3rem;
}

.field-row,
.history-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
}

.tool-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
}

.history-row {
  margin-top: 0.6rem;
}

.hint {
  margin: 0.6rem 0 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.palette {
  display: grid;
  gap: 0.4rem;
}

.palette-button {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr);
  align-items: center;
  gap: 0.55rem;
  text-align: left;
}

.palette-button.selected {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.swatch {
  width: 22px;
  height: 22px;
  border: 1px solid #808080;
  border-radius: 3px;
}

.terrain-edit,
.terrain-add {
  margin-top: 0.8rem;
}

.terrain-edit {
  padding-top: 0.8rem;
  border-top: 1px dashed var(--border);
}

.terrain-edit summary,
.terrain-add summary {
  cursor: pointer;
  margin-bottom: 0.7rem;
}

#updateTerrain {
  width: 100%;
}

.file-label {
  display: block;
  margin-top: 0.7rem;
}

.file-label input {
  margin-top: 0.35rem;
}

#showJson {
  width: 100%;
  margin-top: 0.5rem;
}

#downloadJson,
#createMap {
  width: 100%;
}

.workspace {
  min-width: 0;
  height: 100%;
  overflow: auto;
  padding: 1rem;
}

.workspace-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 0.8rem;
}

.workspace-header h2,
.workspace-header p {
  margin: 0;
}

.workspace-header p {
  margin-top: 0.2rem;
  color: var(--muted);
}

.selected-info {
  color: var(--muted);
  font-size: 0.9rem;
}

.map-scroll {
  overflow: auto;
  padding-bottom: 0.5rem;
}

.map-grid {
  display: grid;
  width: max-content;
  user-select: none;
  touch-action: none;
}

.coord,
.tile {
  width: var(--cell-size);
  height: var(--cell-size);
}

.coord {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  background: var(--panel);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.coord.corner {
  background: transparent;
  border: 0;
}

.tile {
  position: relative;
  border-right: 1px solid rgba(110, 120, 130, 0.28);
  border-bottom: 1px solid rgba(110, 120, 130, 0.28);
  cursor: crosshair;
}

.tile.rect-start::after {
  content: "";
  position: absolute;
  inset: 2px;
  border: 2px dashed var(--accent);
  pointer-events: none;
}

.marker {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  text-shadow:
    1px 1px 0 rgba(255,255,255,.8),
    -1px -1px 0 rgba(255,255,255,.8),
    1px -1px 0 rgba(255,255,255,.8),
    -1px 1px 0 rgba(255,255,255,.8);
  pointer-events: none;
}

.marker.enemy,
.marker.reinforcement,
.marker.boss {
  color: #dc2626;
}

.marker.player {
  color: #1d4ed8;
}

.marker.ally {
  color: #059669;
}

.marker.item,
.marker.note {
  color: #7c3aed;
}

.legend-panel {
  margin-top: 1rem;
}

.legend-table-wrap {
  overflow-x: auto;
}

table {
  border-collapse: collapse;
  min-width: 520px;
  background: var(--panel);
}

th,
td {
  border: 1px solid var(--border);
  padding: 0.55rem 0.7rem;
  text-align: left;
}

th {
  font-weight: 700;
}

.legend-swatch {
  width: 44px;
  height: 24px;
  border: 1px solid #808080;
}

dialog {
  width: min(900px, calc(100vw - 2rem));
  max-height: calc(100vh - 2rem);
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.45);
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}

#jsonPreview {
  overflow: auto;
  max-height: 70vh;
  padding: 0.8rem;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 0.85rem;
  background: var(--bg);
}

@media (max-width: 820px) {
  .site-header {
    padding: 0.75rem 1rem;
  }

  .site-header p {
    display: none;
  }

  .app,
  .app.sidebar-closed {
    display: block;
  }

  .sidebar {
    position: absolute;
    z-index: 20;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(290px, 86vw);
    min-width: 0;
    border-right: 1px solid var(--border);
    box-shadow: 8px 0 24px rgba(0, 0, 0, 0.18);
  }

  .app.sidebar-closed .sidebar {
    transform: translateX(-100%);
    box-shadow: none;
  }

  .workspace {
    width: 100%;
  }
}


.series-dialog {
  width: min(460px, calc(100vw - 2rem));
}

.series-dialog-body {
  display: grid;
  gap: 1rem;
}

.series-dialog-body h2,
.series-dialog-body p {
  margin: 0;
}

.series-dialog-body label {
  display: grid;
  gap: 0.4rem;
}

.series-dialog-body .primary {
  width: 100%;
}

@media (prefers-color-scheme: dark) {
  .save-status.unsaved {
    border-color: #f59e0b;
    color: #f59e0b;
  }
}

@media (max-width: 560px) {
  .save-status {
    display: none;
  }
}
