:root {
  --bg-0: #eee8df;
  --bg-1: #e6dfd2;
  --bg-2: #c8bca9;
  --ink: #201710;
  --ink-soft: #4b4036;
  --panel: rgba(255, 249, 239, 0.86);
  --panel-edge: rgba(75, 54, 36, 0.28);
  --accent: #9d5a2a;
  --accent-2: #5d3a1f;
  --danger: #8a1e1e;
  --shadow: 0 18px 34px rgba(35, 18, 4, 0.2);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  font-family: "Source Sans 3", serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 10%, rgba(255, 255, 255, 0.6), transparent 34%),
    radial-gradient(circle at 84% 88%, rgba(157, 90, 42, 0.13), transparent 32%),
    linear-gradient(145deg, var(--bg-0), var(--bg-1), var(--bg-2));
}

.app-shell {
  --left-width: 320px;
  --right-width: 360px;
  display: grid;
  grid-template-columns: var(--left-width) 8px minmax(520px, 1fr) 8px var(--right-width);
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas:
    "menu menu menu menu menu"
    "left splitL stage splitR right";
  gap: 8px;
  height: 100%;
  padding: 8px;
}

.app-shell.left-collapsed {
  grid-template-columns: 44px 0 minmax(520px, 1fr) 8px var(--right-width);
}

.app-shell.right-collapsed {
  grid-template-columns: var(--left-width) 8px minmax(520px, 1fr) 0 44px;
}

.app-shell.left-collapsed.right-collapsed {
  grid-template-columns: 44px 0 minmax(520px, 1fr) 0 44px;
}

.sidebar-splitter {
  grid-row: 2;
  width: 8px;
  cursor: col-resize;
  border-radius: 6px;
  background: linear-gradient(180deg, rgba(153, 102, 61, 0.25), rgba(107, 72, 44, 0.35));
  box-shadow: inset 0 0 0 1px rgba(74, 46, 25, 0.25);
}

.sidebar-splitter:hover {
  background: linear-gradient(180deg, rgba(188, 122, 72, 0.45), rgba(131, 85, 50, 0.45));
}

.sidebar-splitter-left {
  grid-area: splitL;
}

.sidebar-splitter-right {
  grid-area: splitR;
}

.menu-bar {
  grid-area: menu;
  display: flex;
  align-items: center;
  gap: 2px;
  border: 1px solid var(--panel-edge);
  border-radius: 8px;
  background: linear-gradient(180deg, #fdfaf3, #efe3d1);
  box-shadow: var(--shadow);
  padding: 2px;
}

.menu-item {
  position: relative;
}

.menu-title {
  border: 1px solid transparent;
  background: transparent;
  border-radius: 6px;
  color: var(--ink);
  padding: 3px 8px;
  font-size: 0.9rem;
  cursor: pointer;
  font: inherit;
}

.menu-title:hover,
.menu-item:focus-within .menu-title {
  border-color: rgba(98, 67, 42, 0.35);
  background: rgba(255, 255, 255, 0.62);
}

.menu-drop {
  position: absolute;
  top: calc(100% + 2px);
  left: 0;
  min-width: 220px;
  border: 1px solid rgba(85, 58, 35, 0.35);
  border-radius: 7px;
  background: #fff8ec;
  box-shadow: 0 12px 24px rgba(26, 16, 8, 0.2);
  padding: 3px;
  display: none;
  z-index: 60;
}

.menu-item:hover .menu-drop,
.menu-item:focus-within .menu-drop {
  display: block;
}

.menu-drop button {
  width: 100%;
  text-align: left;
  white-space: nowrap;
  border: none;
  background: transparent;
  color: var(--ink);
  border-radius: 6px;
  padding: 5px 7px;
  font-size: 0.9rem;
  font: inherit;
  cursor: pointer;
}

.menu-drop button:hover {
  background: #f0e0ca;
}

.panel {
  grid-row: 2;
  border: 1px solid var(--panel-edge);
  border-radius: 12px;
  background: var(--panel);
  backdrop-filter: blur(2px);
  box-shadow: 0 12px 28px rgba(35, 18, 4, 0.16);
  padding: 9px;
  overflow-y: auto;
}

.left-panel {
  grid-area: left;
}

.right-panel {
  grid-area: right;
}

.panel.collapsed {
  padding: 8px 4px;
  overflow: hidden;
}

.panel.collapsed .tool-section,
.panel.collapsed h1,
.panel.collapsed h2,
.panel.collapsed h3,
.panel.collapsed #iconSources,
.panel.collapsed #backgroundList,
.panel.collapsed #layersList {
  display: none;
}

.panel-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.panel-toggle-btn {
  min-width: 30px;
  padding: 5px 8px;
}

h1,
h2,
h3 {
  margin: 0 0 7px;
  font-family: "Cinzel", serif;
  letter-spacing: 0.04em;
}

h1 {
  font-size: 1.32rem;
}

h2 {
  font-size: 1rem;
}

h3 {
  font-size: 0.95rem;
}

.tool-section {
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px dashed rgba(63, 42, 26, 0.25);
}

.collapsible-section {
  border: 1px solid rgba(92, 64, 39, 0.22);
  border-radius: 10px;
  padding: 0;
  margin-bottom: 7px;
  background: rgba(255, 251, 244, 0.65);
}

.collapsible-section summary {
  list-style: none;
  cursor: pointer;
  font-family: "Cinzel", serif;
  font-size: 0.88rem;
  letter-spacing: 0.02em;
  padding: 6px 8px;
  border-radius: 10px;
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.collapsible-section summary::-webkit-details-marker {
  display: none;
}

.collapsible-section summary::after {
  content: "+";
  color: var(--ink-soft);
  font-weight: 700;
}

.collapsible-section[open] summary::after {
  content: "-";
}

.collapsible-section .section-body {
  padding: 6px 8px 8px;
  border-top: 1px dashed rgba(63, 42, 26, 0.25);
}

.thumb-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.thumb-btn {
  border: 1px solid rgba(77, 51, 30, 0.35);
  border-radius: 10px;
  background: #f8f3eb;
  padding: 3px;
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.12s ease;
}

.thumb-btn:hover {
  transform: translateY(-1px);
  border-color: var(--accent);
}

.thumb-btn.is-active {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.thumb-btn img {
  width: 100%;
  height: 62px;
  object-fit: cover;
  display: block;
  border-radius: 8px;
}

.field-row {
  margin-bottom: 7px;
}

.field-row.two-col {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

label {
  display: block;
  margin-bottom: 3px;
  color: var(--ink-soft);
  font-size: 0.82rem;
}

input,
textarea,
select {
  width: 100%;
  padding: 6px 7px;
  border-radius: 8px;
  border: 1px solid rgba(90, 62, 34, 0.35);
  background: #fffefb;
  color: var(--ink);
  font-size: 0.9rem;
  font: inherit;
}

textarea {
  resize: vertical;
}

.btn {
  border: 1px solid rgba(70, 46, 24, 0.35);
  background: #f7ebdd;
  color: var(--ink);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 0.88rem;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.12s ease;
}

.btn:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.btn-primary {
  background: linear-gradient(180deg, #b87039, #9d5a2a);
  color: #fff;
  border-color: #8a4f26;
}

.btn-danger {
  background: linear-gradient(180deg, #a53a3a, #8a1e1e);
  color: #fff;
  border-color: #711515;
}

.layers-toolbar,
.mode-row {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-bottom: 6px;
}

.compact-row {
  margin-bottom: 6px;
}

.layers-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.layer-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto auto;
  gap: 4px;
  align-items: center;
}

.layer-row .layer-select,
.layer-row .layer-toggle,
.layer-row .layer-lock,
.layer-row .layer-delete {
  border: 1px solid rgba(76, 53, 33, 0.35);
  border-radius: 7px;
  background: #f6eee2;
  color: var(--ink);
  padding: 3px 7px;
  cursor: pointer;
}

.layer-row .layer-toggle,
.layer-row .layer-lock,
.layer-row .layer-delete {
  width: 30px;
  padding: 3px 0;
  text-align: center;
}

.layer-row .layer-delete {
  color: #8a1e1e;
  font-weight: 700;
}

.layer-row .layer-select {
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.layer-row.is-active .layer-select {
  box-shadow: inset 0 0 0 1px var(--accent);
  border-color: var(--accent);
}

.stage-wrap {
  grid-area: stage;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.top-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.zoom-label {
  min-width: 52px;
  text-align: center;
  color: var(--ink-soft);
  font-weight: 600;
}

.stage-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: 1px solid var(--panel-edge);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: var(--shadow);
  padding: 10px 12px;
}

.stage-title {
  font-family: "Cinzel", serif;
  font-size: 1rem;
  margin-right: 8px;
}

.stage-subtitle {
  color: var(--ink-soft);
  font-size: 0.9rem;
}

.map-viewport {
  position: relative;
  flex: 1;
  border: 1px solid rgba(70, 47, 28, 0.35);
  border-radius: 16px;
  overflow: auto;
  min-height: 520px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.25), var(--shadow);
  background: linear-gradient(135deg, #eadfcd, #d7c4a7);
}

.select-tool-btn {
  position: sticky;
  top: 10px;
  left: 10px;
  z-index: 40;
  margin: 8px;
  padding: 4px 8px;
  border: 1px solid rgba(74, 50, 28, 0.42);
  border-radius: 8px;
  background: rgba(255, 251, 243, 0.92);
  box-shadow: 0 8px 18px rgba(34, 19, 8, 0.2);
  color: var(--ink);
  font-weight: 600;
  font-size: 0.84rem;
  cursor: pointer;
}

.select-tool-btn:hover {
  border-color: var(--accent);
  background: rgba(255, 247, 233, 0.98);
}

.select-tool-btn.is-active {
  border-color: #2f7a45;
  background: linear-gradient(180deg, #e7f6ea, #d7efdd);
  box-shadow: 0 10px 20px rgba(25, 72, 42, 0.22);
}

.map-stage {
  position: relative;
  width: 1600px;
  height: 1000px;
  margin: 12px;
  background-size: cover;
  background-position: top left;
  background-repeat: no-repeat;
  transform-origin: top left;
  box-shadow: 0 8px 20px rgba(35, 18, 4, 0.22);
}

.map-layers {
  position: absolute;
  inset: 0;
}

.map-layer {
  position: absolute;
  transform-origin: center center;
  user-select: none;
}

.map-layer.is-hidden {
  display: none;
}

.map-layer.is-selected {
  outline: 1px dashed rgba(157, 90, 42, 0.9);
  outline-offset: 2px;
}

.map-layer.locked {
  opacity: 0.8;
}

.map-layer img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  pointer-events: none;
}

.layer-text {
  min-width: 16px;
  white-space: pre;
  line-height: 1.1;
  pointer-events: none;
}

.layer-text-canvas {
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
}

.check-row {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  color: var(--ink);
}

.check-row input {
  width: auto;
  margin: 0;
}

.map-layer svg {
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
}

.transform-handle,
.rotate-handle {
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1px solid #fff;
  background: var(--accent);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35);
  display: none;
}

.transform-handle {
  right: -7px;
  bottom: -7px;
  cursor: nwse-resize;
}

.rotate-handle {
  left: calc(50% - 6px);
  top: -18px;
  cursor: grab;
}

.map-layer.is-selected .transform-handle,
.map-layer.is-selected .rotate-handle {
  display: block;
}

.icon-sources .thumb-btn img {
  object-fit: contain;
  background: linear-gradient(160deg, #fbf8f4, #ebe3d6);
}

.cutter-section {
  border-bottom: none;
  margin-bottom: 0;
}

#cutCanvas {
  border: 1px solid rgba(83, 58, 33, 0.35);
  border-radius: 10px;
  background: linear-gradient(45deg, #f8f3eb 25%, #f1e7d8 25%, #f1e7d8 50%, #f8f3eb 50%, #f8f3eb 75%, #f1e7d8 75%);
  background-size: 24px 24px;
}

.cut-canvas-wrap {
  overflow: auto;
  max-height: 280px;
  border-radius: 10px;
  border: 1px solid rgba(83, 58, 33, 0.2);
  margin-bottom: 8px;
  background: rgba(255, 255, 255, 0.3);
}

.cut-zoom-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 6px;
  align-items: center;
  margin-bottom: 6px;
}

.cut-hint {
  margin-bottom: 6px;
  color: var(--ink-soft);
  font-size: 0.82rem;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(31, 19, 10, 0.42);
  backdrop-filter: blur(2px);
}

.modal-backdrop[hidden] {
  display: none;
}

.modal-card {
  width: min(460px, 100%);
  border: 1px solid rgba(92, 64, 39, 0.34);
  border-radius: 14px;
  background: linear-gradient(180deg, #fff9ef, #f4e8d7);
  box-shadow: 0 22px 45px rgba(31, 18, 8, 0.28);
  padding: 14px;
}

.modal-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.license-status {
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 0.84rem;
  border: 1px solid rgba(70, 46, 24, 0.28);
  background: rgba(255, 255, 255, 0.55);
  color: var(--ink-soft);
}

.license-status.is-valid {
  border-color: rgba(43, 115, 69, 0.45);
  background: rgba(226, 244, 233, 0.9);
  color: #1f5f36;
}

.license-status.is-invalid {
  border-color: rgba(138, 30, 30, 0.35);
  background: rgba(249, 234, 234, 0.9);
  color: #7a2121;
}

.license-info {
  margin: 0;
  color: var(--ink-soft);
  font-size: 0.86rem;
  line-height: 1.35;
}

.license-link {
  width: fit-content;
  color: var(--accent-2);
  font-weight: 700;
  text-decoration: none;
  border-bottom: 1px dotted rgba(93, 58, 31, 0.55);
}

.license-link:hover {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

@media (max-width: 1280px) {
  .app-shell {
    --left-width: 290px;
    --right-width: 320px;
    grid-template-columns: var(--left-width) 8px minmax(420px, 1fr) 8px var(--right-width);
  }

  .app-shell.right-collapsed {
    grid-template-columns: var(--left-width) 8px minmax(420px, 1fr) 0 44px;
  }
}

@media (max-width: 1024px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto auto;
    grid-template-areas:
      "menu"
      "left"
      "stage"
      "right";
    height: auto;
    min-height: 100%;
  }

  .sidebar-splitter {
    display: none;
  }

  .panel,
  .stage-header,
  .map-viewport {
    min-height: auto;
  }

  .map-viewport {
    min-height: 460px;
  }

  .top-toolbar {
    justify-content: flex-start;
  }
}
