@font-face {
  font-family: "Excalifont";
  src: url("fonts/Excalifont-Latin.woff2") format("woff2");
  unicode-range: U+20-7E, U+A0-A3, U+A5-A6, U+A8-AB, U+AD-B1, U+B4, U+B6-B8, U+BA-FF, U+131, U+152-153, U+2BC,
    U+2C6, U+2DA, U+2DC, U+304, U+308, U+2013-2014, U+2018-201A, U+201C-201E, U+2020, U+2022, U+2024-2026, U+2030,
    U+2039-203A, U+20AC, U+2122, U+2212;
  font-display: swap;
}
@font-face {
  font-family: "Excalifont";
  src: url("fonts/Excalifont-LatinExt.woff2") format("woff2");
  unicode-range: U+100-130, U+132-137, U+139-149, U+14C-151, U+154-17E, U+192, U+1FC-1FF, U+218-21B, U+237,
    U+1E80-1E85, U+1EF2-1EF3, U+2113;
  font-display: swap;
}
@font-face {
  font-family: "Comic Shanns";
  src: url("fonts/ComicShanns.woff2") format("woff2");
  font-display: swap;
}

:root {
  --hand: "Excalifont", "Segoe Print", "Bradley Hand", cursive;
  --mono: "Comic Shanns", ui-monospace, monospace;
  --ui: system-ui, -apple-system, "Segoe UI", sans-serif;

  --canvas: #ffffff;
  --grid: #dee2e6;
  --ink: #1e1e1e;
  --muted: #868e96;
  --island: #ffffff;
  --island-line: #ececf4;
  --hover: #f1f0ff;
  --primary: #6965db;
  --primary-ink: #ffffff;
  --primary-soft: #e0dfff;
  --primary-strong: #5b57d1;
  --tip-bg: #1e1e1e;
  --tip-ink: #ffffff;
  --danger: #e03131;
  --shadow: 0 7px 14px rgba(0, 0, 0, 0.05), 0 0 3px rgba(0, 0, 0, 0.08), 0 0 1px rgba(0, 0, 0, 0.17);
  --radius: 10px;
  --world-filter: none;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --canvas: #121212;
    --grid: #2b2b33;
    --ink: #e3e3e8;
    --muted: #8f8f99;
    --island: #232329;
    --island-line: #2e2d39;
    --hover: #2e2d39;
    --primary: #a8a5ff;
    --primary-ink: #121212;
    --primary-soft: #403e6a;
    --primary-strong: #bbb8ff;
    --tip-bg: #e3e3e8;
    --tip-ink: #121212;
    --danger: #ff8787;
    --shadow: 0 7px 14px rgba(0, 0, 0, 0.35), 0 0 1px rgba(255, 255, 255, 0.12);
    --world-filter: invert(93%) hue-rotate(180deg);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --canvas: #121212;
  --grid: #2b2b33;
  --ink: #e3e3e8;
  --muted: #8f8f99;
  --island: #232329;
  --island-line: #2e2d39;
  --hover: #2e2d39;
  --primary: #a8a5ff;
  --primary-ink: #121212;
  --primary-soft: #403e6a;
  --primary-strong: #bbb8ff;
  --tip-bg: #e3e3e8;
  --tip-ink: #121212;
  --danger: #ff8787;
  --shadow: 0 7px 14px rgba(0, 0, 0, 0.35), 0 0 1px rgba(255, 255, 255, 0.12);
  --world-filter: invert(93%) hue-rotate(180deg);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--canvas);
  color: var(--ink);
  font-family: var(--hand);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
kbd {
  font-family: var(--ui);
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 4px;
  border: 1px solid var(--island-line);
  color: var(--muted);
}
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* ---------- canvas ---------- */
#board {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}
#board.panning { cursor: grabbing; }
.grid-dot { fill: var(--grid); }
#world { filter: var(--world-filter); }
#world text { font-family: var(--hand); }
#world .mono { font-family: var(--mono); }

.br { cursor: pointer; transition: opacity 0.25s; }
.br .hit { stroke: transparent; stroke-width: 18; fill: none; pointer-events: stroke; }
.br .dot { pointer-events: all; }
.br:hover .label { text-decoration: underline; text-decoration-thickness: 1.5px; text-underline-offset: 3px; }
#world.dim-search .br.miss { opacity: 0.12; }
#world.has-sel .br:not(.sel) { opacity: 0.16; }
#world.far .label, #world.far .sticky { display: none; }
#world.has-sel .trunk-dot:not(.sel-end) { opacity: 0.5; }

.drawing .ink path {
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  animation: draw 0.9s ease-out forwards;
  animation-delay: var(--d, 0s);
}
.drawing .fade {
  opacity: 0;
  animation: fade 0.4s ease-out forwards;
  animation-delay: calc(var(--d, 0s) + 0.5s);
}
@keyframes draw { to { stroke-dashoffset: 0; } }
@keyframes fade { to { opacity: 1; } }

.fresh .ink path { stroke-dasharray: 1; stroke-dashoffset: 1; animation: draw 1.4s ease-out forwards; }
.fresh .glow { animation: glow 2.4s ease-out 3; transform-box: fill-box; transform-origin: center; }
@keyframes glow {
  0% { opacity: 0.9; transform: scale(0.6); }
  100% { opacity: 0; transform: scale(2.4); }
}
.glow { opacity: 0; }

/* ---------- islands ---------- */
.island {
  background: var(--island);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.tool {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  display: inline-grid;
  place-items: center;
  font-size: 18px;
  line-height: 1;
}
.tool:hover { background: var(--hover); }
.tool:focus-visible, .chip:focus-visible, .live:focus-visible, .help:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.top-left {
  position: fixed;
  top: 16px;
  left: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 14px 4px 4px;
  z-index: 3;
  max-width: calc(100vw - 32px);
}
.title { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; }
.title strong { font-size: 20px; font-weight: 400; white-space: nowrap; }
.title span { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.menu {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  width: 240px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-family: var(--ui);
  font-size: 14px;
}
.menu > a, .menu > button {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  text-decoration: none;
  text-align: left;
}
.menu > a:hover, .menu > button:hover { background: var(--hover); }
.menu hr { border: 0; border-top: 1px solid var(--island-line); margin: 6px 0; width: 100%; }
.theme-row { display: flex; align-items: center; gap: 4px; padding: 4px 10px; }
.theme-row span { flex: 1; }
.theme-row button {
  width: 32px;
  height: 30px;
  border-radius: 6px;
  border: 1px solid var(--island-line);
  background: transparent;
}
.theme-row button[aria-pressed="true"] { background: var(--primary-soft); border-color: var(--primary); }

.toolbar {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px;
  z-index: 2;
  max-width: calc(100vw - 32px);
}
.filters { display: flex; gap: 2px; }
.chip {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 10px 0 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  font-size: 15px;
  white-space: nowrap;
}
.chip:hover { background: var(--hover); }
.chip[aria-pressed="true"] { background: var(--primary-soft); }
.chip[aria-pressed="false"] { color: var(--muted); }
.chip[aria-pressed="false"] svg { opacity: 0.35; }
.chip .n { font-family: var(--ui); font-size: 11px; color: var(--muted); }
.chip .key {
  position: absolute;
  right: 3px;
  bottom: 1px;
  font-family: var(--ui);
  font-size: 9px;
  color: var(--muted);
}
.divider { width: 1px; align-self: stretch; margin: 6px 4px; background: var(--island-line); }
.search {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px;
  height: 36px;
  border-radius: 8px;
  color: var(--muted);
}
.search:focus-within { box-shadow: 0 0 0 1px var(--primary); }
.search input {
  width: 170px;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font-size: 15px;
}

.live {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 44px;
  padding: 0 16px;
  border: 0;
  border-radius: var(--radius);
  background: var(--primary);
  color: var(--primary-ink);
  font-size: 16px;
  box-shadow: var(--shadow);
}
.live:hover { background: var(--primary-strong); }
.pulse {
  position: relative;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #40c057;
  box-shadow: 0 0 0 2px var(--primary-ink);
}
.pulse::after {
  content: "";
  position: absolute;
  inset: -2px;
  border-radius: 50%;
  border: 2px solid #40c057;
  animation: ping 2s ease-out infinite;
}
.live.syncing .pulse { background: #fab005; }
.live.syncing .pulse::after { border-color: #fab005; animation-duration: 0.8s; }
.live.offline .pulse { background: #fa5252; }
.live.offline .pulse::after { animation: none; }
@keyframes ping {
  from { transform: scale(1); opacity: 0.9; }
  to { transform: scale(2.6); opacity: 0; }
}

.zoom {
  position: fixed;
  left: 16px;
  bottom: 16px;
  display: flex;
  align-items: center;
  padding: 2px;
  z-index: 2;
}
.zoom-level {
  min-width: 58px;
  height: 36px;
  border: 0;
  background: transparent;
  font-family: var(--ui);
  font-size: 13px;
  border-radius: 8px;
}
.zoom-level:hover { background: var(--hover); }

.help {
  position: fixed;
  right: 16px;
  bottom: 16px;
  width: 40px;
  height: 40px;
  border: 0;
  font-size: 20px;
  z-index: 2;
}
.legend {
  position: fixed;
  right: 16px;
  bottom: 66px;
  width: 300px;
  padding: 14px 16px;
  z-index: 3;
}
.legend h2 { margin: 0 0 8px; font-size: 20px; font-weight: 400; }
.legend ul { list-style: none; padding: 0; margin: 0 0 10px; display: grid; gap: 6px; }
.legend li { display: flex; align-items: center; gap: 10px; font-size: 15px; }
.legend p { font-family: var(--ui); font-size: 12px; line-height: 1.6; margin: 0; }

/* ---------- side panel ---------- */
.panel {
  position: fixed;
  top: 76px;
  right: 16px;
  bottom: 70px;
  width: 340px;
  padding: 16px 16px 12px;
  overflow: auto;
  z-index: 2;
  overscroll-behavior: contain;
}
.panel .close { position: absolute; top: 8px; right: 8px; font-size: 14px; }
.panel h2 {
  margin: 0 30px 4px 0;
  font-size: 22px;
  font-weight: 400;
  line-height: 1.15;
  overflow-wrap: anywhere;
}
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 10px;
  border-radius: 999px;
  border: 1.5px solid currentColor;
  font-size: 14px;
}
.facts { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: 10px 0 12px; font-family: var(--ui); font-size: 13px; }
.facts b { font-weight: 600; }
.pr-card {
  display: block;
  margin: 0 0 14px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1.5px solid var(--island-line);
  color: inherit;
  text-decoration: none;
}
.pr-card:hover { border-color: var(--primary); }
.pr-card .num { color: var(--primary); }
.pr-card .t { display: block; font-family: var(--ui); font-size: 14px; margin-top: 2px; }
.pr-card .labels { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.pr-card .labels span {
  font-family: var(--ui);
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 999px;
  border: 1px solid var(--island-line);
}
.links { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.links a {
  font-size: 14px;
  color: var(--primary);
  text-decoration: none;
  border-bottom: 1.5px dashed currentColor;
}
.panel h3 { font-size: 16px; font-weight: 400; margin: 8px 0 6px; color: var(--muted); }
.commits { list-style: none; padding: 0; margin: 0; display: grid; gap: 2px; }
.commits li {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 10px;
  padding: 6px 8px;
  border-radius: 6px;
}
.commits li:hover { background: var(--hover); }
.commits a { grid-row: span 2; font-family: var(--mono); font-size: 12px; color: var(--primary); text-decoration: none; }
.commits .m { font-family: var(--ui); font-size: 13px; overflow-wrap: anywhere; }
.commits .w { font-family: var(--ui); font-size: 11px; color: var(--muted); }

/* ---------- toasts & tooltip ---------- */
.toasts {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  gap: 8px;
  z-index: 4;
  pointer-events: none;
  width: min(460px, calc(100vw - 32px));
}
.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-radius: var(--radius);
  background: var(--island);
  box-shadow: var(--shadow);
  font-size: 16px;
  animation: toast-in 0.35s cubic-bezier(0.2, 1.4, 0.4, 1);
  cursor: pointer;
  max-width: 100%;
}
.toast .sw { flex: none; width: 12px; height: 12px; border-radius: 50%; border: 1.5px solid var(--ink); }
.toast span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toast.out { animation: toast-out 0.3s ease-in forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateY(14px) rotate(-1deg); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px); } }

.tip {
  position: fixed;
  z-index: 5;
  max-width: 320px;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--tip-bg);
  color: var(--tip-ink);
  font-family: var(--ui);
  font-size: 12px;
  line-height: 1.4;
  pointer-events: none;
}
.tip .sha { font-family: var(--mono); opacity: 0.75; margin-right: 6px; }
.tip .w { opacity: 0.7; display: block; margin-top: 2px; }

/* ---------- boot & lock ---------- */
.boot {
  position: fixed;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 6px;
  z-index: 6;
  background: var(--canvas);
  transition: opacity 0.4s;
}
.boot p { margin: 0; font-size: 22px; }
.boot.done { opacity: 0; pointer-events: none; }
#boot-doodle, .lock-doodle { filter: var(--world-filter); }

.lock {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(-0.6deg);
  width: min(380px, calc(100vw - 32px));
  padding: 28px 28px 22px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 7;
}
.lock-frame { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; z-index: -1; filter: var(--world-filter); }
.lock-doodle { align-self: center; }
.lock h1 { margin: 0; font-size: 28px; font-weight: 400; text-align: center; }
.lock p { margin: 0; font-family: var(--ui); font-size: 14px; line-height: 1.5; color: var(--muted); text-align: center; }
.lock label { font-size: 16px; margin-top: 6px; }
.lock input {
  height: 44px;
  padding: 0 12px;
  border-radius: 8px;
  border: 1.5px solid var(--ink);
  background: var(--island);
  font-family: var(--mono);
  font-size: 16px;
}
.lock input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.btn-primary {
  height: 44px;
  border: 0;
  border-radius: 8px;
  background: var(--primary);
  color: var(--primary-ink);
  font-size: 18px;
}
.btn-primary:hover { background: var(--primary-strong); }
.lock .lock-msg { color: var(--danger); min-height: 1.5em; }
.lock.shake { animation: shake 0.4s; }
@keyframes shake {
  20%, 60% { transform: translate(calc(-50% - 8px), -50%) rotate(-0.6deg); }
  40%, 80% { transform: translate(calc(-50% + 8px), -50%) rotate(-0.6deg); }
}

/* ---------- small screens ---------- */
@media (max-width: 1280px) {
  .chip .label-text { display: none; }
}
@media (max-width: 900px) {
  .toolbar {
    top: auto;
    bottom: 16px;
    left: 16px;
    right: 72px;
    transform: none;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .toolbar::-webkit-scrollbar { display: none; }
  .zoom { bottom: 72px; }
  .search input { width: 140px; }
  .chip .label-text { display: none; }
  .chip { padding: 0 14px 0 10px; }
  .toasts { bottom: 128px; }
  .legend { bottom: 66px; }
  .panel {
    top: auto;
    left: 16px;
    right: 16px;
    bottom: 72px;
    width: auto;
    max-height: 55vh;
  }
  .live { height: 40px; padding: 0 12px; }
}
@media (max-width: 520px) {
  .title span { display: none; }
  #live-text { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-delay: 0s !important; }
}
