:root {
  --bg: #f8f8f6;
  --surface: #ffffff;
  --surface-2: #f1f1ef;
  --border: #e3e3e0;
  --text: #1f2328;
  --text-2: #6b7280;
  --text-3: #9aa0a8;
  --accent: #3b6cf6;
  --accent-soft: rgba(59, 108, 246, 0.12);
  --danger: #ef4444;
  --font: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --sidebar-w: 236px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: 48px 1fr;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
#topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; margin-right: 8px; font-weight: 650; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: linear-gradient(135deg, #4f6bf5, #a855f7);
  color: #fff;
}
.spacer { flex: 1; }
.bar-group { display: flex; align-items: center; gap: 2px; padding: 2px; border-radius: 10px; background: var(--surface-2); }
.ibtn {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
}
.ibtn:hover:not(:disabled) { background: #fff; color: var(--text); box-shadow: 0 1px 2px rgba(16, 24, 40, 0.08); }
.ibtn:disabled { opacity: 0.35; cursor: default; }
.zoom-label {
  min-width: 52px;
  height: 30px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.zoom-label:hover { background: #fff; }

/* ---------- sidebar ---------- */
#sidebar {
  grid-row: 2;
  grid-column: 1;
  overflow-y: auto;
  padding: 12px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 4;
  scrollbar-width: thin;
}
.panel + .panel { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }
.panel-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin: 0 0 8px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-2);
}
.panel-hint { font-weight: 400; color: var(--text-3); }
.tool-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; }
.tool {
  display: grid;
  place-items: center;
  width: 100%;
  aspect-ratio: 1;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
}
.tool:hover { background: var(--surface-2); color: var(--text); }
.tool.is-active { background: var(--accent); color: #fff; box-shadow: 0 2px 8px rgba(59, 108, 246, 0.35); }
.swatches { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
.swatch {
  width: 100%;
  aspect-ratio: 1;
  padding: 0;
  border: 2px solid var(--surface);
  border-radius: 50%;
  outline: 1px solid rgba(0, 0, 0, 0.1);
  cursor: pointer;
}
.swatch.is-active { outline: 2px solid var(--accent); }
.segmented { display: flex; gap: 2px; margin-top: 10px; padding: 2px; border-radius: 8px; background: var(--surface-2); }
.segmented button { flex: 1; height: 28px; border: 0; border-radius: 6px; background: transparent; color: var(--text-2); cursor: pointer; }
.segmented button:hover { color: var(--text); }
.segmented button.is-active { background: #fff; color: var(--text); box-shadow: 0 1px 2px rgba(16, 24, 40, 0.1); }
.widget-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tile {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  min-height: 68px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  cursor: grab;
  text-align: left;
  touch-action: none;
  transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease;
}
.tile:hover { border-color: var(--accent); box-shadow: 0 4px 14px rgba(16, 24, 40, 0.08); transform: translateY(-1px); }
.tile.is-pressed { cursor: grabbing; }
.tile.is-dragging { opacity: 0.4; }
.tile-icon { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; background: var(--accent-soft); color: var(--accent); }
.tile-name { font-size: 12.5px; font-weight: 500; }
.tile-ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 100;
  margin: 0;
  border-color: var(--accent);
  box-shadow: 0 12px 32px rgba(16, 24, 40, 0.22);
  pointer-events: none;
}

/* ---------- canvas ---------- */
#stage { grid-row: 2; grid-column: 2; position: relative; overflow: hidden; }
#canvas { position: absolute; inset: 0; display: block; touch-action: none; user-select: none; -webkit-user-select: none; }
#overlay { position: absolute; left: 0; top: 0; width: 0; height: 0; transform-origin: 0 0; pointer-events: none; }
#drop-preview {
  position: absolute;
  left: 0;
  top: 0;
  display: none;
  border: 1.5px dashed var(--accent);
  border-radius: 12px;
  background: var(--accent-soft);
}
#drop-preview.is-on { display: block; }
.text-editor {
  position: absolute;
  left: 0;
  top: 0;
  display: none;
  padding: 0;
  border: 0;
  outline: 1.5px dashed var(--accent);
  outline-offset: 5px;
  background: transparent;
  color: inherit;
  resize: none;
  overflow: hidden;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: var(--font);
  pointer-events: auto;
}

/* ---------- widgets ---------- */
.node {
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  flex-direction: column;
  transform-origin: 0 0;
  pointer-events: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 6px 18px rgba(16, 24, 40, 0.07);
  user-select: none;
  -webkit-user-select: none;
}
.node--dragging { box-shadow: 0 14px 36px rgba(16, 24, 40, 0.22); }
.node--selected { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft), 0 6px 18px rgba(16, 24, 40, 0.08); }
.node-bar {
  display: flex;
  flex: none;
  align-items: center;
  gap: 6px;
  height: 26px;
  padding: 0 4px 0 6px;
  border-bottom: 1px solid var(--border);
  border-radius: 12px 12px 0 0;
  color: var(--text-3);
  cursor: grab;
  touch-action: none;
}
.node--dragging .node-bar { cursor: grabbing; }
.node-grip { display: grid; place-items: center; color: #b4b8bf; }
.node-title { flex: 1; font-size: 11.5px; font-weight: 500; }
.node-del {
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--text-3);
  cursor: pointer;
  opacity: 0;
}
.node:hover .node-del, .node--selected .node-del, .node-del:focus-visible { opacity: 1; }
.node-del:hover { background: #fee2e2; color: var(--danger); }
.node-body {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  border-radius: 0 0 12px 12px;
  overflow: hidden;
}
.node-resize { position: absolute; right: 0; bottom: 0; width: 18px; height: 18px; cursor: nwse-resize; touch-action: none; }
.node-resize::after {
  content: "";
  position: absolute;
  right: 5px;
  bottom: 5px;
  width: 6px;
  height: 6px;
  border-right: 1.5px solid #b4b8bf;
  border-bottom: 1.5px solid #b4b8bf;
  border-radius: 0 0 2px;
}
/* shared bits */
.btn {
  height: 28px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  font-size: 12.5px;
}
.btn:hover { border-color: #cfd3da; background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #335de6; border-color: #335de6; }
.btn-row { display: flex; gap: 6px; }
.btn-row .btn { flex: 1; }
.chip-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px; }
.chip {
  height: 22px;
  padding: 0 8px;
  border: 0;
  border-radius: 11px;
  background: var(--surface-2);
  color: var(--text-2);
  cursor: pointer;
  font-size: 11.5px;
}
.chip:hover { background: #e6e8ec; color: var(--text); }
.chip.is-active { background: var(--accent); color: #fff; }
.big-time, .big-number { font: 600 30px/1.1 var(--mono); font-variant-numeric: tabular-nums; text-align: center; }
.widget-hint { font-size: 11.5px; color: var(--text-3); }
/* note */
.node[data-type="note"] .node-body { padding: 8px 8px 6px; gap: 4px; }
.note-input {
  flex: 1;
  width: 100%;
  padding: 2px 4px;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  color: #3f3f46;
  font: 500 14px/1.5 var(--font);
  user-select: text;
  -webkit-user-select: text;
}
.note-colors { display: flex; gap: 6px; padding: 2px 4px; }
.note-color {
  width: 14px;
  height: 14px;
  padding: 0;
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 50%;
  cursor: pointer;
  opacity: 0.55;
}
.note-color:hover, .note-color.is-active { opacity: 1; }
.note-color.is-active { outline: 2px solid rgba(0, 0, 0, 0.55); outline-offset: 1px; }
/* todo */
.todo-list { display: flex; flex: 1; flex-direction: column; gap: 2px; min-height: 0; overflow-y: auto; scrollbar-width: thin; }
.todo-item { display: flex; flex: none; align-items: center; gap: 6px; height: 26px; border-radius: 6px; }
.todo-item:hover { background: var(--surface-2); }
.todo-check {
  display: grid;
  flex: none;
  place-items: center;
  width: 16px;
  height: 16px;
  padding: 0;
  border: 1.5px solid #c4c8cf;
  border-radius: 5px;
  background: #fff;
  color: transparent;
  cursor: pointer;
}
.todo-item.is-done .todo-check { background: var(--accent); border-color: var(--accent); color: #fff; }
.todo-text {
  flex: 1;
  min-width: 0;
  height: 24px;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  font: 13px var(--font);
  user-select: text;
  -webkit-user-select: text;
}
.todo-item.is-done .todo-text { color: var(--text-3); text-decoration: line-through; }
.todo-del {
  display: grid;
  flex: none;
  place-items: center;
  width: 18px;
  height: 18px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--text-3);
  cursor: pointer;
  opacity: 0;
}
.todo-item:hover .todo-del { opacity: 1; }
.todo-del:hover { background: #fee2e2; color: var(--danger); }
.todo-add {
  height: 28px;
  padding: 0 8px;
  border: 1px dashed #cfd3da;
  border-radius: 6px;
  outline: none;
  background: transparent;
  color: var(--text);
  font: 13px var(--font);
  user-select: text;
  -webkit-user-select: text;
}
.todo-add:focus { border-color: var(--accent); border-style: solid; }
.todo-add::placeholder, .note-input::placeholder, .todo-text::placeholder { color: #b4b8bf; }
.todo-progress { display: flex; flex: none; align-items: center; gap: 8px; }
.todo-track { flex: 1; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.todo-progress-fill { height: 100%; background: var(--accent); border-radius: 3px; transition: width 0.2s ease; }
.todo-progress-label { font-size: 11.5px; color: var(--text-3); font-variant-numeric: tabular-nums; white-space: nowrap; }
/* stopwatch / timer / clock */
.node[data-type="stopwatch"] .node-body { justify-content: center; }
.ring-wrap { position: relative; display: grid; place-items: center; }
.ring { width: 124px; height: 124px; transform: rotate(-90deg); }
.ring-track { fill: none; stroke: var(--surface-2); stroke-width: 9; }
.ring-value { fill: none; stroke: var(--accent); stroke-width: 9; stroke-linecap: round; transition: stroke-dashoffset 0.2s linear; }
.ring-time { position: absolute; font: 600 26px/1 var(--mono); font-variant-numeric: tabular-nums; }
.ring-time.is-over { animation: pulse 0.5s ease 3; }
@keyframes pulse { 50% { transform: scale(1.14); color: var(--danger); } }
.clock-wrap { display: grid; place-items: center; }
.clock { width: 124px; height: 124px; }
.clock-face { fill: #fff; stroke: #d9dce1; stroke-width: 2; }
.clock-tick { stroke: #c4c8cf; stroke-width: 2; stroke-linecap: round; }
.clock-hand { stroke: var(--text); stroke-width: 3.5; stroke-linecap: round; transform-origin: 60px 60px; }
.clock-minute { stroke-width: 2.5; }
.clock-second { stroke: var(--danger); stroke-width: 1.5; }
.clock-label { color: var(--text-2); font: 500 12.5px var(--font); font-variant-numeric: tabular-nums; text-align: center; }
/* dice */
.dice-tray { display: grid; place-items: center; }
.die {
  position: relative;
  width: 92px;
  height: 92px;
  padding: 0;
  border: 0;
  border-radius: 18px;
  background: #fff;
  box-shadow: inset 0 0 0 1.5px #d9dce1, inset 0 -5px 0 #eef0f3, 0 4px 10px rgba(16, 24, 40, 0.1);
  cursor: pointer;
}
.die:active { transform: scale(0.97); }
.die.is-rolling { animation: shake 0.12s linear infinite; }
@keyframes shake { 0%, 100% { transform: rotate(-5deg); } 50% { transform: rotate(5deg); } }
.pip { position: absolute; width: 13px; height: 13px; margin: -6.5px 0 0 -6.5px; border-radius: 50%; background: var(--text); }
/* palette */
.palette-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.palette-swatch {
  width: 100%;
  aspect-ratio: 1;
  padding: 0;
  border: 2px solid #fff;
  border-radius: 8px;
  outline: 1px solid rgba(0, 0, 0, 0.12);
  cursor: pointer;
}
.palette-swatch:hover { outline-color: rgba(0, 0, 0, 0.3); }
.palette-swatch.is-active { outline: 2px solid var(--accent); outline-offset: 1px; }
.palette-custom { display: flex; align-items: center; gap: 6px; color: var(--text-2); font-size: 12.5px; cursor: pointer; }
.palette-custom input { width: 24px; height: 24px; padding: 0; border: 0; border-radius: 6px; background: none; cursor: pointer; }
.palette-custom input::-webkit-color-swatch-wrapper { padding: 0; }
.palette-custom input::-webkit-color-swatch { border: 1px solid rgba(0, 0, 0, 0.12); border-radius: 6px; }
/* counter */
.counter-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.btn-round { display: grid; place-items: center; width: 36px; height: 36px; padding: 0; border: 0; border-radius: 50%; cursor: pointer; }
.btn-round-minus { background: var(--surface-2); color: var(--text); }
.btn-round-minus:hover { background: #e6e8ec; }
.btn-round-plus { background: var(--accent); color: #fff; }
.btn-round-plus:hover { background: #335de6; }
.counter-foot { display: flex; align-items: center; justify-content: space-between; }
/* sticker */
.sticker {
  flex: 1;
  padding: 0;
  border: 0;
  background: transparent;
  font-size: 56px;
  line-height: 1;
  cursor: pointer;
}
.sticker:active { transform: scale(0.94); }
.sticker-tray {
  position: absolute;
  inset: 4px;
  z-index: 2;
  display: none;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 4px;
  padding: 8px;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 4px 16px rgba(16, 24, 40, 0.18);
}
.sticker-tray.is-open { display: flex; }
.sticker-option { width: 36px; height: 36px; border: 0; border-radius: 6px; background: transparent; font-size: 20px; cursor: pointer; }
.sticker-option:hover, .sticker-option.is-active { background: var(--accent-soft); }

/* ---------- hint, toasts, help ---------- */
.hint {
  position: fixed;
  left: calc(50% + var(--sidebar-w) / 2);
  bottom: 18px;
  z-index: 6;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: calc(100vw - var(--sidebar-w) - 32px);
  padding: 8px 8px 8px 14px;
  border-radius: 999px;
  background: rgba(31, 35, 40, 0.88);
  color: #fff;
  font-size: 12.5px;
  box-shadow: 0 8px 24px rgba(16, 24, 40, 0.2);
}
.hint kbd { padding: 2px 6px; border-radius: 5px; background: rgba(255, 255, 255, 0.16); font: 500 11px var(--mono); }
.hint-close {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  cursor: pointer;
}
.toasts {
  position: fixed;
  left: calc(50% + var(--sidebar-w) / 2);
  bottom: 64px;
  z-index: 7;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}
.toast {
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--text);
  color: #fff;
  font-size: 13px;
  box-shadow: 0 8px 24px rgba(16, 24, 40, 0.25);
  animation: rise 0.18s ease;
}
.toast.is-leaving { opacity: 0; transition: opacity 0.4s; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.help {
  max-width: 440px;
  width: calc(100vw - 32px);
  padding: 0;
  border: 0;
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 64px rgba(16, 24, 40, 0.3);
}
.help::backdrop { background: rgba(16, 24, 40, 0.35); }
.help-card { display: flex; flex-direction: column; gap: 14px; padding: 20px; }
.help-card h2 { margin: 0; font-size: 16px; }
.help-card .btn { align-self: flex-end; padding: 0 14px; }
.keys { display: grid; grid-template-columns: 1fr auto; gap: 8px 16px; margin: 0; font-size: 13px; }
.keys dt { color: var(--text-2); }
.keys dd { margin: 0; color: var(--text); font: 12px var(--mono); text-align: right; }

/* ---------- narrow screens: tools and widgets become a bottom strip ---------- */
@media (max-width: 760px) {
  :root { --sidebar-w: 0px; }
  body { grid-template-columns: 1fr; grid-template-rows: 48px 1fr auto; }
  #sidebar {
    grid-row: 3;
    grid-column: 1;
    display: flex;
    gap: 12px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 8px 10px;
    border-right: 0;
    border-top: 1px solid var(--border);
  }
  .panel { flex: none; }
  .panel + .panel { margin: 0; padding: 0; border: 0; }
  .panel-title { display: none; }
  #style-panel { order: 3; }
  #topbar { gap: 4px; padding: 0 8px; }
  .bar-group { gap: 0; }
  #btn-zoom-in, #btn-zoom-out { display: none; } /* pinch zooms; the label resets to 100% */
  .tool-grid { grid-template-columns: repeat(5, 34px); gap: 2px; }
  .widget-grid { display: flex; gap: 6px; }
  .tile { flex-direction: row; align-items: center; min-height: 46px; padding: 6px 10px 6px 6px; gap: 6px; }
  .tile-icon { width: 28px; height: 28px; }
  #style-panel { display: flex; align-items: center; }
  .swatches { display: flex; align-items: center; }
  .swatch { width: 22px; }
  .segmented { margin: 0 0 0 6px; }
  .segmented button { flex: none; padding: 0 8px; }
  #stage { grid-row: 2; grid-column: 1; }
  .brand-name, .hint { display: none; }
  .toasts { bottom: 18px; }
}
