:root {
  --bg: #f2eee6;
  --bg-grad-top: #f8f3ea;
  --bg-grad-bot: #e7e0d4;
  --panel: rgba(248, 243, 234, 0.88);
  --panel-strong: rgba(252, 249, 243, 0.96);
  --ink: #222224;
  --muted: #6a6864;
  --line: rgba(54, 51, 45, 0.16);
  --line-soft: rgba(54, 51, 45, 0.09);
  --accent: #4a9dff;
  --accent-strong: #2f85ed;
  --danger: #d73a49;
  --radius: 14px;
}

body[data-theme="dark"] {
  --bg: #232932;
  --bg-grad-top: #2b333e;
  --bg-grad-bot: #1f252d;
  --panel: rgba(45, 53, 66, 0.88);
  --panel-strong: rgba(53, 62, 76, 0.95);
  --ink: #e7ebf2;
  --muted: #a6afbd;
  --line: rgba(205, 214, 226, 0.2);
  --line-soft: rgba(205, 214, 226, 0.11);
  --accent: #6eb3ff;
  --accent-strong: #4a99ef;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at -8% -18%, #ffffff 18%, transparent 60%),
    radial-gradient(800px 500px at 108% -15%, #ffffff 10%, transparent 52%),
    linear-gradient(180deg, var(--bg-grad-top), var(--bg-grad-bot));
  font-family: "SF Pro Text", "SF Pro Display", "Avenir Next", "Helvetica Neue", "Archivo", sans-serif;
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  transition: background 0.24s ease, color 0.2s ease;
}

.topbar {
  padding: 18px 24px 16px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.topbar-main {
  min-width: 0;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.menu-wrap {
  position: relative;
}

.tools-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 220px;
  background: var(--panel-strong);
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  box-shadow: 0 14px 26px rgba(12, 19, 31, 0.22);
  padding: 6px;
  display: grid;
  gap: 4px;
  z-index: 90;
}

.tools-menu[hidden] {
  display: none !important;
}

.tools-menu a {
  display: block;
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--ink);
  text-decoration: none;
  font-size: 0.84rem;
}

.tools-menu a:hover {
  background: rgba(22, 119, 255, 0.12);
}

.topbar h1 {
  margin: 0;
  font-size: 1.45rem;
  letter-spacing: -0.02em;
  font-weight: 700;
}

.topbar p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.site-footer {
  padding: 10px 24px 18px;
  text-align: center;
  color: var(--muted);
  font-size: 0.82rem;
}

.site-footer p {
  margin: 4px 0;
}

.site-footer a {
  color: var(--accent-strong);
  text-decoration: none;
  font-weight: 600;
}

.site-footer a:hover {
  text-decoration: underline;
}

.app-shell {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 10px;
  padding: 0 14px 14px;
  min-height: 0;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line-soft);
  border-radius: calc(var(--radius) + 2px);
  box-shadow:
    0 10px 30px rgba(21, 32, 51, 0.08),
    inset 0 1px 0 rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(14px);
  padding: 14px;
  overflow: auto;
  max-height: none;
  height: 100%;
}

h2 {
  margin: 8px 0 10px;
  font-size: 0.94rem;
  font-weight: 700;
  color: var(--ink);
  letter-spacing: 0.01em;
}

h3 {
  margin: 14px 0 8px;
  font-size: 0.86rem;
  font-weight: 700;
  color: var(--ink);
}

.field-row {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 9px;
}

.field-row.inline {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.field-grid {
  display: grid;
  gap: 8px;
}

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

label {
  font-size: 0.8rem;
  color: var(--muted);
  letter-spacing: 0.01em;
}

input,
select,
button {
  font: inherit;
}

input[type="text"],
input[type="number"],
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-strong);
  padding: 8px 10px;
  font-size: 0.9rem;
  color: var(--ink);
  transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

input[type="text"]:focus,
input[type="number"]:focus,
select:focus {
  border-color: rgba(22, 119, 255, 0.45);
  box-shadow: 0 0 0 3px rgba(22, 119, 255, 0.15);
  outline: none;
}

button {
  border: 1px solid transparent;
  border-radius: 10px;
  background: linear-gradient(180deg, var(--accent), var(--accent-strong));
  color: #fff;
  padding: 8px 11px;
  font-size: 0.84rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.14s ease, box-shadow 0.2s ease;
  box-shadow: 0 5px 14px rgba(22, 119, 255, 0.25);
}

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

button:active {
  transform: translateY(1px);
}

button.danger {
  background: linear-gradient(180deg, #e85a66, #cc3948);
  box-shadow: 0 5px 14px rgba(204, 57, 72, 0.22);
}

.palette {
  display: grid;
  gap: 7px;
}

.palette button {
  text-align: left;
}

.hint {
  margin: 8px 0 12px;
  font-size: 0.82rem;
  color: var(--muted);
}

.row-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.row-actions button {
  flex: 1;
}

.saved-list {
  list-style: none;
  margin: 0 0 10px;
  padding: 0;
  display: grid;
  gap: 7px;
}

.layers-list {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
  display: grid;
  gap: 7px;
}

.layer-item {
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  background: color-mix(in srgb, var(--panel), #ffffff 14%);
  padding: 7px;
  display: grid;
  gap: 6px;
}

.layer-item.active {
  border-color: rgba(22, 119, 255, 0.45);
  box-shadow: 0 0 0 2px rgba(22, 119, 255, 0.12);
}

.layer-name {
  width: 100%;
  text-align: left;
  padding: 6px 8px;
  font-size: 0.78rem;
  background: rgba(22, 119, 255, 0.11);
  color: var(--ink);
  border: 1px solid rgba(22, 119, 255, 0.2);
  box-shadow: none;
}

.layer-controls {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
}

.layer-move {
  min-width: 28px;
  padding: 4px 6px;
  background: linear-gradient(180deg, #e6eefb, #dbe7fb);
  color: var(--ink);
  border: 1px solid #c2d3ee;
  box-shadow: none;
}

.layer-move:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.layer-check {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 6px;
  border-radius: 8px;
  border: 1px solid var(--line-soft);
  font-size: 0.72rem;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.7);
}

.layer-check input {
  margin: 0;
}

.saved-list li {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 6px;
  align-items: center;
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.72);
  padding: 7px;
}

.saved-list button {
  padding: 5px 8px;
  box-shadow: none;
}

.saved-list .load-btn {
  background: linear-gradient(180deg, #25a36a, #1d8a58);
}

.saved-list .delete-btn {
  background: linear-gradient(180deg, #e85a66, #cc3948);
}

.status {
  font-size: 0.8rem;
  color: var(--muted);
}

.stage-panel {
  border: 1px solid var(--line-soft);
  border-radius: calc(var(--radius) + 2px);
  background:
    linear-gradient(155deg, color-mix(in srgb, var(--panel-strong), #ffffff 10%), color-mix(in srgb, var(--panel), #f5f8ff 8%)),
    linear-gradient(180deg, color-mix(in srgb, var(--bg-grad-top), #ffffff 5%), var(--bg-grad-bot));
  box-shadow:
    0 16px 42px rgba(18, 34, 58, 0.12),
    inset 0 1px 0 rgba(255, 255, 255, 0.9);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  min-height: 640px;
  position: relative;
  overflow: auto;
  padding: 16px 360px 24px 8px;
  height: 100%;
}

.stage-panel::before {
  content: "";
  position: absolute;
  width: 440px;
  height: 440px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(22, 119, 255, 0.09), transparent 70%);
  top: -180px;
  right: -140px;
  pointer-events: none;
}

#cardStageWrap {
  width: min(84vh, calc(100% - 16px));
  aspect-ratio: 5 / 7;
  max-height: none;
  z-index: 1;
  margin: 0;
  flex: 0 0 auto;
}

#cardStage {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 22px;
  border: 1px solid rgba(36, 55, 88, 0.24);
  background: color-mix(in srgb, var(--panel-strong), #ffffff 4%);
  overflow: hidden;
  box-shadow:
    0 26px 48px rgba(12, 24, 46, 0.18),
    0 2px 0 rgba(255, 255, 255, 0.9) inset;
}

#cardStage::after {
  content: "";
  position: absolute;
  inset: var(--safe-border-top, 7%) var(--safe-border-right, 10%) var(--safe-border-bottom, 7%) var(--safe-border-left, 10%);
  border: 1px dashed rgba(22, 119, 255, 0.5);
  border-radius: 6px;
  pointer-events: none;
  opacity: var(--safe-border-opacity, 0.85);
}

.tile {
  position: absolute;
  border: 1px solid rgba(31, 57, 96, 0.28);
  border-radius: 10px;
  background: color-mix(in srgb, var(--panel-strong), #ffffff 4%);
  overflow: hidden;
  user-select: none;
  box-shadow: 0 4px 16px rgba(18, 40, 75, 0.11);
  transition: box-shadow 0.14s ease, border-color 0.14s ease, transform 0.14s ease, opacity 0.14s ease;
}

.tile.selected {
  border-color: rgba(22, 119, 255, 0.95);
  box-shadow:
    0 0 0 2px rgba(22, 119, 255, 0.42),
    0 10px 24px rgba(22, 119, 255, 0.24);
  transform: translateY(-1px);
}

.tile.no-outline {
  border-color: transparent;
  box-shadow: none;
}

#cardStage:has(.tile.selected) .tile:not(.selected) {
  opacity: 0.66;
}

.tile-header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: 4px 8px;
  font-size: 0.66rem;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(180deg, rgba(22, 119, 255, 0.95), rgba(13, 98, 221, 0.95));
  z-index: 2;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.tile.selected .tile-header {
  background: linear-gradient(180deg, rgba(11, 101, 245, 0.98), rgba(8, 84, 214, 0.98));
}

.tile-delete {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 18px;
  height: 18px;
  border: 1px solid rgba(255, 255, 255, 0.55);
  border-radius: 9px;
  background: rgba(215, 58, 73, 0.95);
  color: #fff;
  font-size: 11px;
  line-height: 1;
  padding: 0;
  display: grid;
  place-items: center;
  z-index: 4;
  box-shadow: none;
}

.tile-delete:hover {
  filter: brightness(1.06);
}

.tile-content {
  position: absolute;
  inset: 22px 7px 7px;
  overflow: auto;
  outline: none;
  white-space: pre-wrap;
}

.tile.flavor .tile-content {
  font-style: italic;
  font-size: 0.9em;
}

.tile.main-image .tile-content,
.tile.icon .tile-content {
  inset: 22px 0 0;
}

.tile.card-background .tile-content {
  inset: 0;
}

.tile.card-background {
  border: none;
  border-radius: 0;
  box-shadow: none;
}

.tile.card-background .tile-content img {
  object-fit: cover;
}

.tile-content img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  display: block;
}

.tile-icon-value {
  position: absolute;
  z-index: 3;
  pointer-events: none;
  font-weight: 700;
  line-height: 1;
}

.tile-icon-value.overlay {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.tile-icon-value.left {
  top: 50%;
  left: 8px;
  transform: translateY(-50%);
}

.tile-icon-value.right {
  top: 50%;
  right: 8px;
  transform: translateY(-50%);
}

.resize-handle {
  position: absolute;
  width: 13px;
  height: 13px;
  right: 0;
  bottom: 0;
  background: linear-gradient(180deg, #4d93ff, #2d78ee);
  border-top-left-radius: 8px;
  cursor: nwse-resize;
}

.tile.selected .resize-handle {
  background: linear-gradient(180deg, #6fb1ff, #3287ff);
  width: 14px;
  height: 14px;
}

#cardStage.export-mode .tile-header,
#cardStage.export-mode .tile-delete,
#cardStage.export-mode .resize-handle {
  display: none !important;
}

#cardStage.export-mode .tile.main-image .tile-content,
#cardStage.export-mode .tile.icon .tile-content {
  inset: 0 !important;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.toolbar button {
  min-width: 34px;
  padding: 6px;
  box-shadow: none;
}

.tutorial-modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(17, 24, 39, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.preview-modal {
  position: fixed;
  inset: 0;
  z-index: 86;
  background: rgba(17, 24, 39, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.preview-modal[hidden] {
  display: none !important;
}

.preview-card {
  width: min(760px, 100%);
  max-height: 90vh;
  overflow: auto;
  background: var(--panel-strong);
  border: 1px solid var(--line-soft);
  border-radius: 14px;
  box-shadow: 0 22px 48px rgba(7, 15, 31, 0.35);
  padding: 16px;
}

.preview-card h2 {
  margin-top: 0;
}

.preview-image-wrap {
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  background: color-mix(in srgb, var(--panel), #ffffff 8%);
  padding: 10px;
  margin-bottom: 10px;
}

#previewImage {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 8px;
}

.tutorial-modal[hidden],
.tutorial-guide[hidden] {
  display: none !important;
}

.tutorial-card {
  width: min(620px, 100%);
  max-height: 86vh;
  overflow: auto;
  background: var(--panel-strong);
  border: 1px solid var(--line-soft);
  border-radius: 14px;
  box-shadow: 0 22px 48px rgba(7, 15, 31, 0.35);
  padding: 16px;
}

.tutorial-card h2 {
  margin-top: 0;
}

.tutorial-card p {
  margin: 0 0 12px;
  color: var(--muted);
}

.tutorial-guide {
  position: fixed;
  inset: 0;
  z-index: 85;
  pointer-events: none;
}

.tutorial-guide-card {
  position: fixed;
  width: min(320px, calc(100vw - 20px));
  background: color-mix(in srgb, var(--panel-strong), #ffffff 2%);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 16px 36px rgba(15, 22, 36, 0.28);
  padding: 12px;
  pointer-events: auto;
}

.tutorial-guide-step {
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

#tutorialGuideText {
  margin: 0 0 10px;
  color: var(--ink);
  font-size: 0.86rem;
  line-height: 1.4;
}

body[data-theme="dark"] .tutorial-modal {
  background: rgba(8, 12, 18, 0.56);
}

body[data-theme="dark"] .preview-modal {
  background: rgba(8, 12, 18, 0.56);
}

.tutorial-target {
  position: relative;
  z-index: 82 !important;
  box-shadow: 0 0 0 3px rgba(22, 119, 255, 0.35) !important;
  border-radius: 10px;
}

.floating-palette {
  position: fixed;
  top: 110px;
  right: 20px;
  width: min(340px, calc(100vw - 20px));
  max-height: calc(100vh - 126px);
  overflow: auto;
  z-index: 30;
  background: var(--panel);
  border: 1px solid var(--line-soft);
  border-radius: calc(var(--radius) + 2px);
  box-shadow:
    0 16px 34px rgba(18, 34, 58, 0.18),
    inset 0 1px 0 rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(14px);
  padding: 12px;
  transition: top 0.16s ease;
}

.floating-palette::before {
  content: "Selected Tile Inspector";
  display: block;
  font-size: 0.7rem;
  color: #5d6978;
  letter-spacing: 0.02em;
  margin-bottom: 8px;
}

@media (max-width: 1180px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .panel {
    max-height: none;
  }

  .stage-panel {
    min-height: 520px;
    padding: 16px 12px 180px;
  }

  .floating-palette {
    left: 10px !important;
    right: 10px;
    width: auto;
    top: auto !important;
    bottom: 10px;
    max-height: 52vh;
  }

  .topbar {
    flex-direction: column;
    align-items: flex-start;
  }
}
