:root {
  color-scheme: dark;
  --bg: #101319;
  --panel: #171b22;
  --panel-2: #1d222b;
  --line: #2c3440;
  --text: #e8edf4;
  --muted: #929eae;
  --accent: #ef9f43;
  --accent-2: #ffc46f;
  --danger: #e26969;
  --shadow: 0 18px 45px rgb(0 0 0 / 24%);
  font-family: Inter, "Noto Sans JP", "Yu Gothic UI", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
}

button, input, select { font: inherit; }
button, label.button { user-select: none; }

.app-header {
  position: relative;
  z-index: 30;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 0 12px;
  border-bottom: 1px solid var(--line);
  background: #14181f;
}

.header-start, .brand, .file-actions, .button-row, .stage-toolbar, .status-bar,
.coordinates, .range-label, .check-row, .palette-tabs {
  display: flex;
  align-items: center;
}

.header-start { min-width: 0; gap: 8px; }
.brand { min-width: 0; }
.brand h1 {
  margin: 0;
  overflow: hidden;
  font-size: 16px;
  font-weight: 750;
  letter-spacing: .025em;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.file-actions { min-width: 0; gap: 7px; }
.file-actions [hidden] { display: none; }
.button-row { gap: 8px; }
.button-row.wrap { flex-wrap: wrap; }

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 34px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
}

.button:hover { border-color: #4b586a; background: #242b35; }
.button.primary { border-color: #d88931; background: var(--accent); color: #24170a; font-weight: 800; }
.button.primary:hover { background: var(--accent-2); }
.button.compact { min-height: 30px; padding: 0 10px; font-size: 12px; }
.button.wide { width: 100%; }
.install-button { margin-right: 5px; color: var(--muted); }
.save-button { min-width: 58px; }
.visibility-buttons { margin-top: 10px; }
.icon-button {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex: 0 0 auto;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel-2);
  color: var(--text);
  font-size: 17px;
  cursor: pointer;
}
.icon-button:hover { border-color: #4b586a; background: #242b35; }
.save-indicator {
  max-width: 190px;
  overflow: hidden;
  color: var(--muted);
  font-size: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.panel-toggle { display: none; }

.workspace {
  position: relative;
  display: grid;
  grid-template-columns: 214px minmax(320px, 1fr) 248px;
  grid-template-rows: minmax(0, 1fr);
  height: calc(100% - 56px);
  min-height: 0;
  overflow: hidden;
}

.tool-panel, .palette-panel {
  min-height: 0;
  background: var(--panel);
  overscroll-behavior: contain;
}

.tool-panel {
  overflow-x: hidden;
  overflow-y: auto;
  border-right: 1px solid var(--line);
}
.palette-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-left: 1px solid var(--line);
}

.panel-section { padding: 13px; border-bottom: 1px solid var(--line); }
.panel-section h2 {
  margin: 0 0 11px;
  color: var(--muted);
  font-size: 10px;
  letter-spacing: .15em;
  text-transform: uppercase;
}
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.section-heading h2 { margin-bottom: 10px; }
.active-tool-name {
  margin-bottom: 10px;
  padding: 3px 7px;
  border-radius: 10px;
  background: #392b1c;
  color: var(--accent-2);
  font-size: 10px;
  font-weight: 700;
}
.primary-settings { background: #151a21; }
.selection-settings, .unit-settings { display: none; }
body.tool-select .selection-settings,
body.tool-paste .selection-settings,
body.tool-unit .unit-settings,
body.tool-eyedropper .unit-settings { display: block; }
.disclosure { padding: 0; }
.disclosure > summary {
  position: relative;
  padding: 13px 30px 13px 13px;
  color: #c5cdd8;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .08em;
  cursor: pointer;
  list-style: none;
}
.disclosure > summary::-webkit-details-marker { display: none; }
.disclosure > summary::after {
  position: absolute;
  right: 14px;
  content: "›";
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  transform: rotate(90deg);
  transition: transform .16s ease;
}
.disclosure[open] > summary::after { transform: rotate(-90deg); }
.disclosure[open] > summary { padding-bottom: 9px; color: var(--text); }
.disclosure > :not(summary) { margin-right: 13px; margin-left: 13px; }
.disclosure > .button.wide { width: calc(100% - 26px); }
.disclosure > :last-child { margin-bottom: 13px; }
.disclosure > .section-note { margin-top: 9px; }

.tool-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
.tool-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding: 9px 5px 7px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel-2);
  color: var(--muted);
  cursor: pointer;
}
.tool-button:hover { color: var(--text); border-color: #4b586a; }
.tool-button.active { border-color: var(--accent); background: #392b1c; color: var(--accent-2); }
.tool-icon { height: 21px; font: 700 20px/1 ui-monospace, monospace; }
.tool-button span:last-child { font-size: 11px; }

.range-label { justify-content: space-between; color: var(--muted); font-size: 12px; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.check-row { gap: 8px; margin-top: 10px; color: #c5cdd8; font-size: 12px; }
.check-row input { accent-color: var(--accent); }

.size-grid, .dialog-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.size-grid label, .unit-settings > label, .dialog-grid label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 11px;
}

input[type="number"], input[type="text"], select {
  width: 100%;
  height: 32px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  outline: none;
  background: #11151b;
  color: var(--text);
}
input:focus, select:focus { border-color: var(--accent); }
.size-grid + .button { margin-top: 8px; }
.unit-settings > label { margin-top: 0; }
.unit-settings > .size-grid { margin-top: 8px; }
.section-note {
  margin: 9px 0 0;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.5;
}

.expand-grid {
  display: grid;
  grid-template-areas:
    ". north ."
    "west south east";
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.expand-grid label {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 10px;
  text-align: center;
}
.expand-grid .north { grid-area: north; }
.expand-grid .west { grid-area: west; }
.expand-grid .south { grid-area: south; }
.expand-grid .east { grid-area: east; }
.expand-grid + .button { margin-top: 8px; }

.map-stage {
  display: grid;
  grid-template-rows: 43px minmax(0, 1fr) 28px;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #0b0e12;
}
.stage-toolbar { justify-content: space-between; gap: 10px; padding: 0 12px; border-bottom: 1px solid var(--line); background: var(--panel); }
.stage-toolbar strong { font-size: 13px; }
.stage-toolbar #document-meta { margin-left: 10px; color: var(--muted); font-size: 11px; }
.document-info { min-width: 0; flex: 1; overflow: hidden; white-space: nowrap; }
.document-info strong { display: inline-block; max-width: 55%; overflow: hidden; text-overflow: ellipsis; vertical-align: middle; }
.history-actions { display: flex; gap: 5px; }
.history-actions .icon-button { width: 29px; height: 29px; font-size: 16px; }
.coordinates { gap: 8px; color: var(--muted); font: 11px ui-monospace, monospace; }
.coordinates span { min-width: 56px; padding: 4px 7px; border: 1px solid var(--line); border-radius: 4px; background: #11151b; }
.coordinates b { color: var(--accent-2); }

.canvas-scroller {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  background-color: #0c1015;
  background-image:
    linear-gradient(45deg, #11161d 25%, transparent 25%),
    linear-gradient(-45deg, #11161d 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #11161d 75%),
    linear-gradient(-45deg, transparent 75%, #11161d 75%);
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  background-size: 16px 16px;
}

#map-canvas {
  display: block;
  margin: 18px;
  background: white;
  box-shadow: var(--shadow);
  image-rendering: pixelated;
  cursor: crosshair;
  touch-action: none;
}
#map-canvas.is-panning { cursor: grabbing; }

.empty-hint {
  position: sticky;
  left: 50%;
  bottom: 22px;
  width: max-content;
  max-width: calc(100% - 40px);
  transform: translateX(-50%);
  padding: 8px 12px;
  border: 1px solid #3d4755;
  border-radius: 20px;
  background: rgb(17 22 29 / 90%);
  color: var(--muted);
  font-size: 11px;
  pointer-events: none;
}
.empty-hint.hidden { display: none; }

.status-bar { justify-content: space-between; padding: 0 11px; border-top: 1px solid var(--line); background: var(--panel); color: var(--muted); font-size: 10px; }

.palette-heading {
  display: none;
  align-items: center;
  justify-content: space-between;
  height: 43px;
  padding: 0 10px 0 13px;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
}
.panel-close { width: 28px; height: 28px; }
.palette-tabs { height: 43px; padding: 6px; gap: 5px; border-bottom: 1px solid var(--line); }
.palette-tab {
  flex: 1;
  height: 30px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.palette-tab.active { background: #2b313b; color: var(--text); font-weight: 700; }
.palette-import { display: grid; gap: 7px; padding: 9px; border-bottom: 1px solid var(--line); }
.palette-import .section-note { margin: 0 2px; }
.palette {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  align-content: start;
  gap: 5px;
  flex: 1;
  min-height: 0;
  padding: 9px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

.tool-panel,
.canvas-scroller,
.palette {
  scrollbar-color: #566274 #151a21;
  scrollbar-width: thin;
}

.tool-panel::-webkit-scrollbar,
.canvas-scroller::-webkit-scrollbar,
.palette::-webkit-scrollbar {
  width: 11px;
  height: 11px;
}

.tool-panel::-webkit-scrollbar-track,
.canvas-scroller::-webkit-scrollbar-track,
.palette::-webkit-scrollbar-track {
  background: #151a21;
}

.tool-panel::-webkit-scrollbar-thumb,
.canvas-scroller::-webkit-scrollbar-thumb,
.palette::-webkit-scrollbar-thumb {
  border: 3px solid #151a21;
  border-radius: 8px;
  background: #566274;
}
.chip-button {
  position: relative;
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  min-width: 0;
  padding: 3px;
  overflow: hidden;
  border: 1px solid #303844;
  border-radius: 5px;
  background: #11151b;
  cursor: pointer;
}
.chip-button:hover { border-color: #69778a; }
.chip-button.selected { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); background: #332719; }
.chip-button canvas, .chip-button img { max-width: 100%; max-height: 100%; image-rendering: pixelated; }
.selection-info { display: grid; gap: 3px; padding: 10px 12px; border-top: 1px solid var(--line); background: #14181f; }
.selection-info span { color: var(--muted); font-size: 9px; letter-spacing: .1em; }
.selection-info strong { overflow: hidden; font: 11px ui-monospace, monospace; text-overflow: ellipsis; white-space: nowrap; }

dialog {
  width: min(360px, calc(100% - 30px));
  padding: 20px;
  border: 1px solid #3a4552;
  border-radius: 11px;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgb(0 0 0 / 62%); }
dialog h2 { margin: 0 0 18px; font-size: 17px; }
dialog h3 { margin: 0 0 8px; font-size: 13px; }
.dialog-message { color: #c5cdd8; font-size: 13px; line-height: 1.7; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.material-manager-section { padding: 16px 0 14px; border-top: 1px solid var(--line); }
.material-manager-section:first-of-type { border-top: 0; padding-top: 0; }
.material-manager-section .dialog-message { margin: 0 0 13px; }
.material-import-section { padding-bottom: 17px; }
.material-import-section .dialog-message { margin-top: -3px; }
.material-manager-section + .material-manager-section h3 { margin-bottom: 6px; }
.material-manager-section .button-row { gap: 6px; }
.material-manager-section code { color: var(--accent-2); }
.panel-backdrop {
  position: absolute;
  z-index: 15;
  display: none;
  inset: 0;
  padding: 0;
  border: 0;
  background: rgb(5 7 10 / 64%);
  cursor: default;
}

@media (max-width: 1100px) {
  .panel-toggle { display: inline-grid; }
  .workspace { grid-template-columns: 204px minmax(280px, 1fr); }
  .palette-panel {
    position: absolute;
    z-index: 20;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(280px, calc(100% - 48px));
    box-shadow: -18px 0 40px rgb(0 0 0 / 35%);
    transform: translateX(105%);
    transition: transform .18s ease;
  }
  body.palette-panel-open .palette-panel { transform: translateX(0); }
  body.palette-panel-open .panel-backdrop { display: block; }
  .palette-heading { display: flex; }
}

@media (min-width: 761px) and (max-width: 1100px) {
  body.tools-panel-collapsed .workspace { grid-template-columns: minmax(280px, 1fr); }
  body.tools-panel-collapsed .tool-panel { display: none; }
}

@media (max-width: 760px) {
  .app-header { gap: 6px; padding: 0 8px; }
  .brand h1 { font-size: 13px; }
  .save-indicator { display: none; }
  .file-actions { gap: 5px; }
  .file-actions .button { min-height: 32px; padding: 0 9px; font-size: 11px; }
  .workspace { display: block; height: calc(100% - 56px); }
  .map-stage { width: 100%; height: 100%; }
  .tool-panel {
    position: absolute;
    z-index: 20;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(250px, calc(100% - 48px));
    box-shadow: 18px 0 40px rgb(0 0 0 / 35%);
    transform: translateX(-105%);
    transition: transform .18s ease;
  }
  body.tools-panel-open .tool-panel { transform: translateX(0); }
  body.tools-panel-open .panel-backdrop,
  body.palette-panel-open .panel-backdrop { display: block; }
  .coordinates span { min-width: 45px; }
  .status-bar span:last-child { display: none; }
  .palette { grid-template-columns: repeat(4, 1fr); }
}

@media (max-width: 480px) {
  .brand h1 { max-width: 82px; font-size: 12px; }
  .file-actions .button { padding: 0 8px; }
  .stage-toolbar #document-meta { display: none; }
  .coordinates { gap: 4px; }
  .coordinates span { min-width: 40px; padding: 4px 5px; }
  .status-bar { font-size: 9px; }
}

@media (max-width: 420px) {
  .brand h1 { display: none; }
}
