:root {
  /* --- modern clean-minimal token system --- */
  --bg: #f7f8fa;            /* cool off-white app surface */
  --surface: #ffffff;        /* panels, dock, drawer */
  --surface-2: #f1f3f7;      /* recessed surface: inputs, chips, segmented tracks */
  --surface-3: #e9ecf2;      /* deeper recessed / hover */
  --line: #e6e8ee;           /* hairline borders */
  --line-2: #d6d9e1;         /* visible dividers */
  --text: #0f1115;           /* near-black primary text */
  --text-2: #565b66;         /* secondary text */
  --text-3: #8b9099;         /* muted captions, labels */
  --accent: #4f46e5;         /* indigo accent */
  --accent-2: #6366f1;       /* lighter accent / hover */
  --accent-soft: #eef0fe;    /* accent tint background */
  --danger: #dc2626;
  --r-sm: 8px;
  --r: 12px;
  --r-lg: 16px;
  --r-pill: 999px;
  --shadow-xs: 0 1px 2px rgba(15,17,21,0.04);
  --shadow-sm: 0 1px 3px rgba(15,17,21,0.06), 0 1px 2px rgba(15,17,21,0.04);
  --shadow-md: 0 4px 16px rgba(15,17,21,0.08), 0 1px 4px rgba(15,17,21,0.04);
  --shadow-lg: 0 12px 32px rgba(15,17,21,0.14), 0 2px 8px rgba(15,17,21,0.06);
  --shadow-dock: 0 8px 28px rgba(15,17,21,0.12), 0 2px 6px rgba(15,17,21,0.05);
  --shadow-drawer: -16px 0 48px rgba(15,17,21,0.16);
  --ease-spring: cubic-bezier(.34, 1.32, .64, 1);
  --ease-out: cubic-bezier(.4, 0, .2, 1);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 15px;
  font-weight: 400;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-feature-settings: "cv11", "ss01";
}

.app {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
}

/* --- floating brand mark (top-left) ------------------------------------- */
.brand-mark {
  position: fixed;
  top: max(18px, env(safe-area-inset-top));
  left: max(20px, env(safe-area-inset-left));
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 1px;
  pointer-events: none;
  opacity: 0;
  animation: fadeIn .5s var(--ease-out) .15s forwards;
}
.brand-name {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.4px;
  color: var(--text);
}
.ink-drop {
  width: 9px; height: 9px;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  box-shadow: 0 1px 3px rgba(79,70,229,0.35);
  flex: 0 0 auto;
}
.brand-tag {
  font-size: 11.5px;
  color: var(--text-3);
  letter-spacing: 0.1px;
  font-weight: 400;
}

@keyframes fadeIn { to { opacity: 1; } }

/* --- stage: canvas hero fills viewport ---------------------------------- */
.stage {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.canvas-wrap {
  flex: 1;
  padding: 0;
  display: block;
  position: relative;
  min-height: 0;
}
.layer {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  border-radius: 0;
}
#paperLayer {
  background: #fcfbf8;
}
#inkLayer { background: transparent; }
#paper {
  cursor: crosshair;
  touch-action: none;
  background: transparent;
}
#nibCursor {
  position: absolute;
  width: 9px; height: 9px;
  border: 1.5px solid rgba(15, 17, 21, 0.7);
  border-radius: 50%;
  pointer-events: none;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: opacity .12s, width .08s, height .08s;
  box-shadow: 0 0 0 1px rgba(255,255,255,.7);
  z-index: 5;
}
#nibCursor.visible { opacity: 0.95; }
#nibCursor.eraser { border-color: rgba(220, 38, 38, 0.9); border-style: dashed; }
.hint {
  position: absolute;
  left: 0; right: 0;
  bottom: 84px;
  padding: 0 18px;
  font-size: 12.5px;
  color: var(--text-3);
  text-align: center;
  font-weight: 400;
  pointer-events: none;
  opacity: 0;
  animation: fadeIn .5s var(--ease-out) .5s forwards;
}

/* --- floating dock (bottom-center) -------------------------------------- */
.dock {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  transform-origin: center bottom;
  z-index: 25;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px;
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  border: 1px solid var(--line);
  border-radius: var(--r-pill);
  box-shadow: var(--shadow-dock);
  max-width: calc(100vw - 24px);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  opacity: 1;
  transition: opacity .28s var(--ease-out), transform .28s var(--ease-out);
  animation: dockRise .5s var(--ease-spring) .1s;
}
.dock::-webkit-scrollbar { display: none; }

/* auto-hide while drawing so the canvas stays unobstructed */
.dock.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(16px);
  pointer-events: none;
}

@keyframes dockRise {
  from { opacity: 0; transform: translateX(-50%) translateY(20px); }
  to { opacity: 1; transform: translateX(-50%); }
}

/* --- segmented controls (tool / nib / paper) ---------------------------- */
.tool-switch, .nib-switch, .paper-switch {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--r-pill);
  overflow: hidden;
  padding: 3px;
  gap: 2px;
}
.tool-btn {
  background: transparent;
  border: none;
  color: var(--text-2);
  padding: 7px 13px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  border-radius: var(--r-pill);
  transition: background .18s var(--ease-out), color .18s var(--ease-out), box-shadow .18s var(--ease-out), transform .12s var(--ease-out);
}
.tool-btn .ico { font-size: 15px; line-height: 1; }
.tool-btn:hover { color: var(--text); }
.tool-btn.active {
  background: var(--surface);
  color: var(--accent);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
  border: none;
}

.dock-sep {
  display: block;
  width: 1px;
  height: 24px;
  background: var(--line-2);
  flex: 0 0 auto;
  margin: 0 2px;
}

.dock-ink {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--r-pill);
  padding: 6px 13px 6px 8px;
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, transform .12s;
}
.dock-ink:hover { background: var(--surface-3); }
.dock-ink:active { transform: scale(.97); }
.dock-swatch {
  width: 18px; height: 18px;
  box-shadow: inset 0 0 0 1px rgba(15,17,21,0.12);
}
.dock-ink-name { max-width: 110px; overflow: hidden; text-overflow: ellipsis; }
.dock-chev {
  color: var(--text-3);
  font-size: 11px;
  line-height: 1;
  margin-left: -2px;
}

.dock-actions { display: inline-flex; gap: 3px; }
.dock-btn {
  background: transparent;
  border: none;
  color: var(--text-2);
  padding: 7px 13px;
  border-radius: var(--r-pill);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  transition: background .15s, color .15s, transform .12s;
}
.dock-btn .ico { font-size: 15px; line-height: 1; }
.dock-btn:hover { background: var(--surface-2); color: var(--text); }
.dock-btn:active { transform: scale(.95); }
.dock-btn.primary {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  box-shadow: var(--shadow-xs);
}
.dock-btn.primary:hover { background: var(--accent-2); color: #fff; }

/* --- drawer / bottom sheet --------------------------------------------- */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(15, 17, 21, 0.36);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .28s var(--ease-out);
}
body.drawer-open .backdrop { opacity: 1; pointer-events: auto; }

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 390px;
  max-width: 92vw;
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  box-shadow: var(--shadow-drawer);
  transform: translateX(100%);
  transition: transform .34s var(--ease-out);
  overscroll-behavior: contain;
}
.drawer.open {
  transform: translateX(0);
  transition: transform .42s var(--ease-spring);
}

.sheet-handle {
  display: none; /* shown only on narrow screens */
  flex: 0 0 auto;
  align-items: center;
  justify-content: center;
  padding: 10px 0 4px;
  cursor: grab;
  touch-action: none;
}
.sheet-handle:active { cursor: grabbing; }
.sheet-handle span {
  display: block;
  width: 38px;
  height: 4px;
  border-radius: 3px;
  background: var(--line-2);
}

.drawer-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.drawer-title {
  margin: 0;
  font-family: "Inter", "Segoe UI", sans-serif;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.3px;
  color: var(--text);
}
.drawer-close {
  width: 32px; height: 32px;
  border: none;
  background: var(--surface-2);
  color: var(--text-2);
  border-radius: 50%;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background .15s, color .15s, transform .12s;
}
.drawer-close:hover { background: var(--surface-3); color: var(--text); }
.drawer-close:active { transform: scale(.9); }

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 20px 32px;
}
.drawer-body::-webkit-scrollbar { width: 8px; }
.drawer-body::-webkit-scrollbar-track { background: transparent; }
.drawer-body::-webkit-scrollbar-thumb { background: var(--line-2); border-radius: 4px; }
.drawer-body::-webkit-scrollbar-thumb:hover { background: #b8bcc6; }

/* --- flat panel sections (calm, no card-stacking noise) ---------------- */
.panel {
  margin-top: 28px;
}
.panel:first-child { margin-top: 8px; }
.panel-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 14px;
}
.panel-title {
  margin: 0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.2px;
  color: var(--text-2);
  text-transform: none;
}
.panel-sub {
  font-size: 11.5px;
  color: var(--text-3);
  font-weight: 400;
}

/* ink chips */
.ink-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.ink-chip {
  display: flex;
  align-items: center;
  gap: 9px;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  padding: 9px 10px;
  color: var(--text-2);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
  transition: background .15s, border-color .15s, color .15s, box-shadow .15s;
}
.ink-chip:hover { border-color: var(--line-2); color: var(--text); }
.ink-chip.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--text);
}
.chip-swatch {
  width: 16px; height: 16px;
  border-radius: 50%;
  flex: 0 0 auto;
  box-shadow: inset 0 0 0 1px rgba(15,17,21,0.10), 0 1px 2px rgba(15,17,21,0.08);
}
.chip-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.ink-meta {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 4px;
}
.ink-meta-name { font-weight: 600; color: var(--text); font-size: 14px; }
.ink-meta-desc { font-size: 12.5px; color: var(--text-3); line-height: 1.5; }

/* controls */
.ctrl { display: block; margin: 14px 0 0; }
.ctrl-row {
  display: flex; justify-content: space-between;
  font-size: 14px; color: var(--text-2); font-weight: 500;
  margin-bottom: 8px;
}
.ctrl .val {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.ctrl-hint {
  display: block;
  margin-top: 6px;
  font-size: 11.5px;
  color: var(--text-3);
  font-weight: 400;
}

/* nib / paper switches inside drawer (segmented, inherit from above) */
.nib-switch.disabled { opacity: 0.4; pointer-events: none; }
.nib-btn {
  background: transparent;
  border: none;
  color: var(--text-2);
  padding: 7px 11px;
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  border-radius: var(--r-pill);
  transition: background .15s, color .15s, box-shadow .15s;
}
.nib-btn:hover { color: var(--text); }
.nib-btn.active {
  background: var(--surface);
  color: var(--accent);
  font-weight: 600;
  box-shadow: var(--shadow-xs);
}
.paper-switch {
  width: 100%;
  flex-wrap: wrap;
  border-radius: var(--r-sm);
  padding: 4px;
}
.paper-btn {
  background: transparent;
  border: none;
  color: var(--text-2);
  padding: 7px 12px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border-radius: var(--r-sm);
  transition: background .15s, color .15s, box-shadow .15s;
}
.paper-btn:hover { color: var(--text); }
.paper-btn.active {
  background: var(--surface);
  color: var(--accent);
  font-weight: 600;
  box-shadow: var(--shadow-xs);
}

.ink-name-row {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}
.ink-name-row input[type="text"] {
  flex: 1;
  min-width: 0;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  padding: 10px 12px;
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
  font-weight: 500;
  transition: border-color .15s, box-shadow .15s, background .15s;
}
.ink-name-row input[type="text"]:focus {
  outline: none;
  background: var(--surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.tbtn.sm {
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  border-radius: var(--r-sm);
  border: none;
  cursor: pointer;
}
.tbtn.sm.primary {
  background: var(--accent);
  color: #fff;
  white-space: nowrap;
  box-shadow: var(--shadow-xs);
  transition: background .15s, transform .12s;
}
.tbtn.sm.primary:hover { background: var(--accent-2); }
.tbtn.sm.primary:active { transform: scale(.96); }

/* saved ink delete buttons */
.ink-chip .del {
  margin-left: auto;
  width: 20px; height: 20px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 5px;
  color: var(--text-3);
  font-size: 14px; line-height: 1;
  background: transparent; border: none; cursor: pointer;
  flex: 0 0 auto;
  transition: background .15s, color .15s;
}
.ink-chip .del:hover { background: rgba(220,38,38,.12); color: var(--danger); }

/* --- modern sliders ---------------------------------------------------- */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: var(--surface-3);
  outline: none;
  margin: 0;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--surface);
  border: 2px solid var(--accent);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform .12s var(--ease-out), box-shadow .12s;
}
input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.15);
  box-shadow: 0 0 0 6px var(--accent-soft), var(--shadow-sm);
}
input[type="range"]:active::-webkit-slider-thumb {
  transform: scale(1.1);
  box-shadow: 0 0 0 8px var(--accent-soft), var(--shadow-sm);
}
input[type="range"]::-moz-range-thumb {
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--surface); border: 2px solid var(--accent); cursor: pointer;
  box-shadow: var(--shadow-sm);
}

/* color editor */
.editor .swatch-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 4px;
}
.swatch-ctrl {
  display: flex; align-items: center; gap: 9px;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  padding: 8px 10px;
  font-size: 13px;
  color: var(--text-2);
  font-weight: 500;
  cursor: pointer;
  position: relative;
  transition: border-color .15s, background .15s;
}
.swatch-ctrl:hover { border-color: var(--line-2); background: var(--surface-3); }
.swatch-ctrl input[type="color"] {
  position: absolute; inset: 0; opacity: 0; cursor: pointer; border: none; padding: 0;
}
.swatch-ctrl .swatch {
  width: 16px; height: 16px; border-radius: 50%;
  flex: 0 0 auto;
  box-shadow: inset 0 0 0 1px rgba(15,17,21,0.10), 0 1px 2px rgba(15,17,21,0.08);
}

/* ink-rendered wordmark swash */
.brand-swash {
  display: block;
  width: 100%;
  height: 26px;
  margin: 0 0 14px;
}

/* --- tablet / phone: drawer becomes a bottom sheet --------------------- */
@media (max-width: 1100px) {
  .drawer {
    top: auto;
    left: 0;
    right: 0;
    width: auto;
    max-width: none;
    height: 86vh;
    height: 86dvh;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    box-shadow: 0 -16px 48px rgba(15,17,21,0.16);
    transform: translateY(100%);
  }
  .drawer.open { transform: translateY(0); }
  .sheet-handle { display: flex; }
  .brand-name { font-size: 17px; }
}

@media (max-width: 640px) {
  .dock { gap: 3px; padding: 5px; }
  .tool-btn, .dock-btn { padding: 8px 11px; }
  .tool-btn .lbl, .dock-btn .lbl { display: none; }
  .dock-ink-name { max-width: 76px; }
  .dock-sep { height: 20px; }
  .hint { font-size: 11.5px; bottom: 72px; }
  .brand-tag { display: none; }
  .drawer-body { padding: 8px 16px 28px; }
}

/* respect reduced motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}

/* keyboard focus */
.dock-btn:focus-visible,
.dock-ink:focus-visible,
.tool-btn:focus-visible,
.nib-btn:focus-visible,
.paper-btn:focus-visible,
.drawer-close:focus-visible,
.tbtn:focus-visible,
.ink-chip:focus-visible,
.swatch-ctrl:focus-within,
input[type="text"]:focus-visible,
.ink-name-row button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
