:root {
  color-scheme: light;
  --bg: #eef1f5;
  --surface: #ffffff;
  --surface-2: #f7f9fb;
  --ink: #18212f;
  --muted: #64748b;
  --line: #d9e0e8;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --teal: #0f766e;
  --amber: #b45309;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 4px 14px rgba(15, 23, 42, 0.05);
  font-family: "SF Pro Text", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

body.dragging::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(37, 99, 235, 0.08);
  border: 3px dashed var(--accent);
  border-radius: 12px;
  pointer-events: none;
}

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

button {
  cursor: pointer;
}

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: rgba(255, 255, 255, 0.94);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
}

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

.brand-mark {
  width: 42px;
  height: 42px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
}

.brand-mark svg {
  width: 22px;
  height: 22px;
}

.brand-text h1 {
  margin: 0;
  font-size: 18px;
  line-height: 1.25;
  letter-spacing: 0;
}

.brand-text p {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--muted);
}

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

.btn {
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 14px;
  border: 1px solid transparent;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}

.btn:active {
  transform: translateY(1px);
}

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

.btn svg {
  width: 17px;
  height: 17px;
  flex: none;
}

.btn.primary {
  background: var(--accent);
  color: var(--accent-ink);
}

.btn.primary:not(:disabled):hover {
  background: #1d4ed8;
}

.btn.teal {
  background: var(--teal);
  color: #fff;
}

.btn.teal:not(:disabled):hover {
  background: #115e59;
}

.btn.ghost {
  background: var(--surface);
  border-color: var(--line);
  color: var(--ink);
}

.btn.ghost:not(:disabled):hover {
  background: var(--surface-2);
  border-color: #c4cdd8;
}

.layout {
  flex: 1;
  width: 100%;
  max-width: 1440px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  grid-template-columns: 292px minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

.sidebar,
.workspace {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.sidebar {
  position: sticky;
  top: 78px;
  max-height: calc(100dvh - 94px);
}

.tool-nav {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  padding: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.tool-btn {
  height: 44px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  text-align: left;
}

.tool-btn svg {
  width: 17px;
  height: 17px;
  flex: none;
}

.tool-btn:hover {
  background: var(--surface-2);
  color: var(--ink);
}

.tool-btn.active {
  background: #e8f0fe;
  border-color: #bcd3fb;
  color: var(--accent);
}

.files-panel,
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.files-panel {
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.panel-head h2 {
  margin: 0;
  font-size: 15px;
  letter-spacing: 0;
}

.panel-sub {
  margin: 3px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.count-badge {
  min-width: 26px;
  height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 7px;
  border-radius: 999px;
  background: #e8f0fe;
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
}

.file-list {
  flex: 1;
  min-height: 120px;
  overflow: auto;
  padding: 8px;
}

.file-item {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) 24px;
  gap: 8px;
  align-items: center;
  width: 100%;
  padding: 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  text-align: left;
}

.file-item:hover {
  background: var(--surface-2);
}

.file-item.active {
  background: #e8f0fe;
  border-color: #bcd3fb;
}

.file-thumb {
  width: 34px;
  height: 42px;
  object-fit: cover;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #f1f5f9;
}

.file-thumb-placeholder {
  width: 34px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #f1f5f9;
  color: var(--muted);
}

.file-thumb-placeholder svg {
  width: 17px;
  height: 17px;
}

.file-info {
  min-width: 0;
}

.file-name {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 600;
}

.file-meta {
  display: block;
  margin-top: 2px;
  font-size: 11px;
  color: var(--muted);
}

.icon-btn {
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
}

.icon-btn:hover {
  background: #fee2e2;
  color: var(--danger);
}

.icon-btn svg {
  width: 15px;
  height: 15px;
}

.empty-hint {
  margin: 0;
  padding: 16px;
  font-size: 12px;
  line-height: 1.6;
  color: var(--muted);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) minmax(320px, 0.85fr);
  align-items: start;
}

body.editing .workspace {
  grid-template-columns: minmax(0, 1fr);
}

body.editing .preview-panel {
  display: none;
}

body.editing .action-panel {
  width: 100%;
  min-height: 0;
}

.preview-panel {
  min-height: 420px;
  display: flex;
  flex-direction: column;
}

.preview-body {
  min-height: 360px;
  max-height: calc(100dvh - 250px);
  overflow: auto;
  padding: 14px;
}

.empty-state {
  min-height: 360px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px;
  border: 2px dashed #c7d2de;
  border-radius: 8px;
  background: var(--surface-2);
}

.empty-icon {
  width: 58px;
  height: 58px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: #e8f0fe;
  color: var(--accent);
  margin-bottom: 14px;
}

.empty-icon svg {
  width: 28px;
  height: 28px;
}

.empty-state h3 {
  margin: 0 0 6px;
  font-size: 16px;
}

.empty-state p {
  max-width: 340px;
  margin: 0;
  font-size: 13px;
  line-height: 1.6;
  color: var(--muted);
}

.badge-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.badge {
  height: 24px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 0 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.badge.accent {
  background: #e8f0fe;
  border-color: #bcd3fb;
  color: var(--accent);
}

.page-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 12px;
}

.page-tile {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.page-tile:hover {
  border-color: #a8b8ca;
}

.page-tile.selected {
  border-color: var(--accent);
  background: #f4f8ff;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.12);
}

.page-tile canvas {
  width: 100%;
  height: auto;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #fff;
}

.page-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}

.page-meta input {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
}

.image-preview {
  max-width: 100%;
  max-height: 70vh;
  display: block;
  margin: 0 auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.action-panel {
  min-height: 420px;
  padding: 18px;
}

.tool-panel.hidden {
  display: none;
}

.tool-head {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 16px;
}

.tool-head > svg {
  width: 22px;
  height: 22px;
  flex: none;
  color: var(--accent);
  margin-top: 2px;
}

.tool-head h3 {
  margin: 0;
  font-size: 16px;
  letter-spacing: 0;
}

.tool-head p {
  margin: 4px 0 0;
  font-size: 13px;
  line-height: 1.5;
  color: var(--muted);
}

.tool-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.action-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.action-grid .btn {
  width: 100%;
  padding: 0 8px;
}

.overlay-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.editor-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.editor-page-label {
  min-width: 70px;
  text-align: center;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}

.toolbar-divider {
  width: 1px;
  height: 22px;
  margin: 0 4px;
  background: var(--line);
}

.editor-controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 10px;
}

.editor-controls label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.editor-stage {
  position: relative;
  width: 100%;
  max-height: calc(100dvh - 260px);
  min-height: 420px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.editor-stage canvas {
  display: block;
  max-width: none;
}

.editor-stage.ready {
  width: min(100%, 900px);
}

.text-edit-box {
  position: absolute;
  min-width: 24px;
  min-height: 16px;
  padding: 1px 4px;
  border: 1px dashed rgba(37, 99, 235, 0.55);
  background: rgba(255, 255, 255, 0.25);
  color: #111;
  line-height: 1.2;
  cursor: text;
  outline: none;
  word-break: break-word;
}

.text-edit-box:hover {
  border-color: var(--accent);
  background: rgba(37, 99, 235, 0.08);
}

.text-edit-box.selected {
  border-style: solid;
  background: rgba(37, 99, 235, 0.14);
}

.drag-handle {
  position: absolute;
  top: -7px;
  left: -7px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--accent);
  cursor: move;
}

.delete-box {
  position: absolute;
  top: -10px;
  right: -8px;
  width: 18px;
  height: 18px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  cursor: pointer;
}

.delete-box svg {
  width: 11px;
  height: 11px;
}

.overlay-grid label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.color-input {
  width: 100%;
  height: 40px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  cursor: pointer;
}

.panel-hint {
  margin: 10px 0 0;
  font-size: 13px;
  color: var(--muted);
}

.merge-list,
.image-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 230px;
  overflow: auto;
}

.merge-row,
.image-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-2);
}

.order-index {
  width: 22px;
  height: 22px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 5px;
  background: var(--ink);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
}

.row-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 600;
}

.row-meta {
  flex: none;
  font-size: 11px;
  color: var(--muted);
}

.mini-btn {
  width: 28px;
  height: 28px;
  flex: none;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--surface);
  color: var(--muted);
}

.mini-btn:hover:not(:disabled) {
  background: #e8f0fe;
  color: var(--accent);
  border-color: #bcd3fb;
}

.mini-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.mini-btn svg {
  width: 14px;
  height: 14px;
}

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

.radio,
.check {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-2);
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  cursor: pointer;
}

.radio input,
.check input {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
}

.text-input,
.select-input {
  width: 100%;
  height: 40px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
}

.text-input:focus,
.select-input:focus,
.text-output:focus {
  outline: 2px solid rgba(37, 99, 235, 0.35);
  border-color: var(--accent);
}

.field-row {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.field-row label {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

.text-output {
  width: 100%;
  min-height: 180px;
  margin-top: 12px;
  padding: 12px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--ink);
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
}

.toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  max-width: min(92vw, 520px);
  padding: 11px 16px;
  border-radius: 7px;
  background: var(--ink);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.24);
}

.toast.error {
  background: var(--danger);
}

.toast.warn {
  background: var(--amber);
}

.loading-mask {
  min-height: 180px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
}

.spinner {
  width: 26px;
  height: 26px;
  border: 3px solid #dbe2ea;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.btn-spinner {
  width: 15px;
  height: 15px;
  flex: none;
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

@media (max-width: 1024px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    max-height: none;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .tool-nav {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .preview-body {
    max-height: 60vh;
  }
}

@media (max-width: 640px) {
  .topbar {
    padding: 10px 12px;
  }

  .brand-text p {
    display: none;
  }

  .layout {
    padding: 12px;
  }

  .tool-nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .btn span {
    display: none;
  }

  .btn {
    width: 40px;
    padding: 0;
  }

  .field-row {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .overlay-grid {
    grid-template-columns: 1fr;
  }

  .editor-controls {
    grid-template-columns: 1fr;
  }
}
