@font-face {
  font-family: "Excalifont";
  src: url("/fonts/excalifont.woff2") format("woff2");
  font-display: swap;
}

:root {
  --bg: #fdfcf8;
  --surface: #ffffff;
  --surface-2: #f4f3ee;
  --ink: #1e1e1e;
  --muted: #6b6e76;
  --line: #d9d7cf;
  --strong-line: #1e1e1e;
  --accent: #6965db;
  --accent-ink: #ffffff;
  --accent-soft: #e3e2fe;
  --danger: #e03131;
  --warn: #e8590c;
  --text: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  --hand: "Excalifont", system-ui, "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  --mono: ui-monospace, "Cascadia Code", Consolas, "Noto Sans Mono CJK TC", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161618;
    --surface: #1e1e21;
    --surface-2: #26262a;
    --ink: #e3e3e8;
    --muted: #a0a3ab;
    --line: #34343a;
    --strong-line: #8a8a92;
    --accent: #a8a5ff;
    --accent-ink: #121212;
    --accent-soft: #2c2a52;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 var(--text);
}
button, select, input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- 頂列 ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  min-height: 48px;
  flex-wrap: wrap;
  position: relative;
  z-index: 20;
}
.brand { font-family: var(--hand); font-size: 24px; color: var(--ink); text-decoration: none; margin-right: 8px; }
.divider { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.spacer { flex: 1; }

.tool, .button, .menu > summary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 32px;
  height: 32px;
  padding: 0 10px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
}
.tool { font-size: 17px; padding: 0 6px; }
.tool:hover, .menu > summary:hover, .menu[open] > summary { background: var(--surface-2); }
.tool:disabled { opacity: 0.35; cursor: default; background: transparent; }
.button { border-color: var(--line); background: var(--surface); text-decoration: none; }
.button:hover { border-color: var(--strong-line); }
.button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.button.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
.zoom { display: inline-flex; align-items: center; }
.zoom-value { min-width: 56px; font-size: 13px; }

.select { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); }
select {
  height: 30px;
  padding: 0 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.menu { position: relative; }
.menu > summary { list-style: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-list {
  position: absolute;
  top: 38px;
  left: 0;
  min-width: 250px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.14);
}
.menu-list button {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  width: 100%;
  padding: 7px 10px;
  text-align: left;
  background: none;
  border: 0;
  border-radius: 6px;
  cursor: pointer;
}
.menu-list button:hover { background: var(--accent-soft); }
.menu-list hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
kbd { font: 12px var(--mono); color: var(--muted); }

.save-status { color: var(--muted); font-size: 12px; margin-right: 6px; }
.save-status[data-kind="error"] { color: var(--danger); }

/* ---------- 工作區 ---------- */
.workspace { flex: 1; display: flex; min-height: 0; }
.viewport {
  position: relative;
  flex: 1;
  overflow: hidden;
  cursor: default;
  outline: none;
  touch-action: none;
}
.viewport.panning { cursor: grabbing; }
.viewport.dragging, .viewport.picking { cursor: crosshair; }
.world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.map-layer svg, .overlay { display: block; }
.overlay { position: absolute; left: 0; top: 0; pointer-events: none; }
.map-layer [data-id] { cursor: pointer; }

.inline-editor {
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
  pointer-events: none;
  resize: none;
  overflow: hidden;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--ink);
  font-family: var(--hand);
  line-height: 1.25;
  z-index: 5;
  caret-color: var(--accent);
}
.inline-editor.editing {
  opacity: 1;
  pointer-events: auto;
  background: var(--surface);
  border: 2px solid var(--accent);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.15);
}
.viewport.dark .inline-editor.editing { background: #1e1e21; color: #e3e3e8; }

/* ---------- 屬性面板 ---------- */
.inspector {
  width: 284px;
  flex-shrink: 0;
  overflow-y: auto;
  padding: 14px 16px 24px;
  background: var(--surface);
  border-left: 1px solid var(--line);
}
.inspector h2 { font: 400 20px var(--hand); margin: 0 0 8px; }
.panel-section { margin: 14px 0; }
.panel-section h3 { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin: 0 0 6px; }
.field { width: 100%; padding: 6px 8px; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; }
.field:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.field.note { resize: vertical; }
.field:disabled { opacity: 0.5; }
.check { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; cursor: pointer; }
.muted { color: var(--muted); }

.icon-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.icon-cell {
  display: grid;
  min-width: 0;
  padding: 0;
  place-items: center;
  aspect-ratio: 1;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  color: var(--muted);
}
.icon-cell:hover { border-color: var(--strong-line); }
.icon-cell.active { border-color: var(--accent); background: var(--accent-soft); }
.color-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.color-cell { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--line); cursor: pointer; padding: 0; }
.color-cell.auto { background: conic-gradient(#e03131, #f08c00, #2f9e44, #1971c2, #6741d9, #e03131); }
.color-cell.active { box-shadow: 0 0 0 2px var(--accent); }
.color-custom { width: 28px; height: 24px; padding: 0; border: 1px solid var(--line); border-radius: 6px; background: none; cursor: pointer; }

/* ---------- 大綱面板 ---------- */
.outline-panel {
  width: 340px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px;
  background: var(--surface);
  border-right: 1px solid var(--line);
}
.outline-panel h2 { font: 400 20px var(--hand); margin: 0; }
.outline-panel p { margin: 0; font-size: 12px; }
#outline-text {
  flex: 1;
  width: 100%;
  resize: none;
  padding: 10px;
  font: 13px/1.7 var(--mono);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  tab-size: 2;
}

.diagnostics { list-style: none; margin: 0; padding: 0; font-size: 12px; max-height: 180px; overflow: auto; }
.diagnostics li { padding: 3px 8px; margin-bottom: 3px; border-left: 3px solid var(--warn); background: color-mix(in srgb, var(--warn) 8%, transparent); }
.diagnostics li.error { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }

/* ---------- 對話框 ---------- */
.dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 24px 60px rgb(0 0 0 / 0.25);
}
.dialog.wide { width: min(880px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgb(0 0 0 / 0.35); }
.dialog form, .dialog > .dialog-body { padding: 0 20px 20px; }
.dialog form > p { margin: 0 0 8px; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px 8px; }
.dialog form .dialog-head { padding: 16px 0 8px; }
.dialog-head h2 { font: 400 24px var(--hand); margin: 0; }
.icon-button { background: none; border: 0; font-size: 18px; cursor: pointer; color: var(--muted); }
#paste-text { width: 100%; padding: 10px; font: 13px/1.6 var(--mono); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; resize: vertical; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 12px; }
.row.end { justify-content: flex-end; }

.tabs { display: flex; gap: 4px; padding: 0 20px; border-bottom: 1px solid var(--line); }
.tab { padding: 8px 12px; background: none; border: 0; border-bottom: 2px solid transparent; cursor: pointer; color: var(--muted); }
.tab.active { color: var(--ink); border-color: var(--accent); }
.dialog-body { overflow: auto; max-height: calc(100vh - 170px); padding-top: 16px !important; }
.pane h3 { font-size: 13px; color: var(--muted); margin: 16px 0 6px; }
.pane h3:first-child { margin-top: 0; }
.keys { border-collapse: collapse; width: 100%; }
.keys td { padding: 5px 8px; border-bottom: 1px solid var(--line); }
.keys td:first-child { width: 44%; }
.keys kbd { display: inline-block; padding: 1px 6px; font-size: 12px; color: var(--ink); background: var(--surface-2); border: 1px solid var(--line); border-radius: 5px; }
.plain { margin: 0; padding-left: 18px; }
.steps { margin: 0; padding-left: 20px; display: grid; gap: 6px; }
.steps strong { display: block; }
.steps span { color: var(--muted); }
.prompt { white-space: pre-wrap; max-height: 300px; overflow: auto; margin: 12px 0 0; padding: 12px; font: 12px/1.6 var(--mono); background: var(--bg); border-radius: 8px; }
.compare { width: 100%; border-collapse: collapse; margin: 8px 0 16px; font-size: 13px; }
.compare th, .compare td { text-align: left; padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.compare thead th { color: var(--muted); font-weight: 600; font-size: 12px; }
.cards { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.card { padding: 14px; border: 1px solid var(--line); border-radius: 10px; min-width: 0; }
.card h3 { margin-top: 0 !important; color: var(--ink) !important; font-size: 15px !important; }
.copy-row { display: flex; gap: 6px; align-items: center; margin: 6px 0; }
.copy-row code { flex: 1; min-width: 0; overflow-x: auto; white-space: nowrap; padding: 6px 8px; font: 12px var(--mono); background: var(--bg); border-radius: 6px; }

/* ---------- 提示 ---------- */
.loading { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); font: 22px var(--hand); color: var(--muted); z-index: 50; }
.mode-hint { position: fixed; top: 64px; left: 50%; transform: translateX(-50%); margin: 0; padding: 6px 14px; background: var(--accent); color: var(--accent-ink); border-radius: 999px; z-index: 30; }
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  max-width: calc(100vw - 32px);
  opacity: 0;
  pointer-events: none;
  padding: 10px 18px;
  color: var(--accent-ink);
  background: var(--accent);
  border-radius: 999px;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 60;
  overflow-wrap: anywhere;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 900px) {
  .inspector { display: none; }
  .select > span, .divider { display: none; }
  .outline-panel { position: absolute; inset: 48px 0 0 0; width: auto; z-index: 15; }
  .cards { grid-template-columns: 1fr; }
}
.map-layer [data-toggle], .map-layer [data-relation] { cursor: pointer; }

/* ---------- 帳號 ---------- */
.dialog.narrow { width: min(400px, calc(100vw - 32px)); }
.account-form { display: flex; flex-direction: column; gap: 10px; }
.account-form .dialog-head { padding: 16px 0 0; }
.account-form p { margin: 0; }
.stack { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.stack .field { color: var(--ink); font-size: 15px; padding: 8px 10px; }
.button.wide { width: 100%; height: 38px; margin-top: 4px; }
.button.small { height: 28px; padding: 0 10px; font-size: 13px; }
.notice { padding: 8px 10px; background: var(--accent-soft); border-radius: 8px; font-size: 13px; }
.form-error { color: var(--danger); font-size: 13px; }
.links { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.link { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font-size: 13px; }
.account-menu > summary { max-width: 180px; overflow: hidden; text-overflow: ellipsis; border: 1px solid var(--line); }
.menu-list.right { left: auto; right: 0; }
.menu-note { padding: 4px 10px 6px; font-size: 12px; margin: 0; overflow-wrap: anywhere; }
.maps-actions { padding: 0 20px 8px; }
.maps-body { padding: 0 20px 20px; max-height: 60vh; overflow: auto; }
.maps-list { list-style: none; margin: 0; padding: 0; }
.maps-list li { display: flex; align-items: center; gap: 8px; border-bottom: 1px solid var(--line); }
.map-open { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 10px 6px; background: none; border: 0; text-align: left; cursor: pointer; border-radius: 6px; }
.map-open:hover { background: var(--accent-soft); }
.map-open strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.map-open span { font-size: 12px; }
.lang-select { height: 32px; padding: 0 4px; }

/* ---------- 播放動畫 ---------- */
.player { position: absolute; left: 0; top: 0; display: block; }
.viewport.playing { cursor: pointer; }
.viewport.playing .map-layer, .viewport.playing .overlay { visibility: hidden; }
