:root {
  color-scheme: light;
  --bg: #f4ecdf; --ink: #3b3227; --muted: #857662; --faint: #ab9c85;
  --panel: #fdfaf3; --border: #e3d9c4; --accent: #b3562e; --accent-ink: #fdf8ef;
  --danger: #a03123; --region: #c8342a; --note: #fff6cf; --note-border: #ecd98f;
  --shadow: 0 8px 28px rgba(93, 72, 44, .13);
  --serif: "Fraunces", "Iowan Old Style", Georgia, serif;
  --dots: radial-gradient(circle, rgba(120,99,72,.22) 1.2px, transparent 1.4px);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif; font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
body.board { overflow: hidden; overscroll-behavior: none; touch-action: manipulation; }
code { background: rgba(0,0,0,.05); padding: .1em .35em; border-radius: 4px; font-size: .9em; }

.btn {
  font: inherit; font-size: .9rem; color: var(--ink); cursor: pointer;
  background: var(--panel); border: 1px solid var(--border); border-radius: 999px; padding: .5rem 1.1rem;
  transition: border-color .15s, background .15s;
}
.btn:hover { border-color: var(--faint); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.05); }
.chip-btn {
  font: inherit; font-size: .82rem; color: var(--muted); cursor: pointer;
  background: color-mix(in srgb, var(--panel) 78%, transparent);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  border: 1px solid var(--border); border-radius: 999px; padding: .34rem .85rem;
  transition: color .15s, border-color .15s;
}
.chip-btn:hover { color: var(--ink); border-color: var(--faint); }
.chip-btn b { color: var(--ink); font-weight: 600; }
.chip-btn.on b { color: var(--accent); }
.toast {
  position: fixed; z-index: 60; left: 50%; transform: translateX(-50%);
  top: calc(max(1rem, env(safe-area-inset-top)) + 3.1rem);
  max-width: min(30rem, calc(100vw - 2rem));
  background: var(--panel); border: 1px solid var(--border); border-left: 3px solid var(--accent);
  border-radius: 10px; box-shadow: var(--shadow); padding: .6rem .95rem;
  font-size: .85rem; color: var(--ink); opacity: 0; pointer-events: none;
  transition: opacity .25s, translate .25s; translate: 0 -6px;
}
.toast.show { opacity: 1; translate: 0 0; }
.toast.error { border-left-color: var(--danger); }

/* ---------- plátno ---------- */
#stage {
  position: fixed; inset: 0; overflow: hidden; touch-action: none; cursor: default;
  background-color: var(--bg);
  background-image: var(--dots);
  background-size: var(--grid, 26px) var(--grid, 26px);
  background-position: var(--gx, 0) var(--gy, 0);
}
#stage.panning { cursor: grabbing; }
#stage.linking { cursor: crosshair; }
#stage.regioning .node.image .body { cursor: crosshair; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
#edges { position: absolute; left: 0; top: 0; width: 1px; height: 1px; overflow: visible; pointer-events: none; }
#nodes { position: absolute; left: 0; top: 0; }
#rubber { position: absolute; border: 1.5px dashed var(--region); background: rgba(200,52,42,.08); pointer-events: none; }

.edge { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; pointer-events: none; transition: opacity .25s; }
.edge.rel { stroke: var(--faint); stroke-dasharray: 5 5; }
.edge.temp { stroke: var(--accent); stroke-dasharray: 6 5; opacity: .7; }
.edge.sel { stroke: var(--ink); stroke-width: 2.5; }
.edge.dim { opacity: .12; }
.edge-hit { fill: none; stroke: transparent; stroke-width: 16; pointer-events: stroke; cursor: pointer; }
.arrhead { fill: var(--accent); }
.arrhead.sel { fill: var(--ink); }
.edge-label { font-size: 11px; fill: var(--muted); pointer-events: none; text-anchor: middle; paint-order: stroke; stroke: var(--bg); stroke-width: 4px; stroke-linejoin: round; transition: opacity .25s; }
.edge-label.dim { opacity: .12; }

/* ---------- položky ---------- */
.node {
  position: absolute; left: 0; top: 0; user-select: none; -webkit-user-select: none;
  border-radius: 10px; background: #fffdf7; border: 1px solid var(--border);
  box-shadow: 0 2px 10px rgba(93,72,44,.10);
  transition: box-shadow .15s, opacity .3s, filter .3s;
  cursor: grab;
}
.node:hover { box-shadow: 0 6px 22px rgba(93,72,44,.16); }
.node.dragging { cursor: grabbing; box-shadow: 0 16px 40px rgba(93,72,44,.22); z-index: 5; transition: none; }
.node.sel { outline: 2px solid var(--accent); outline-offset: 2px; }
.node.dim { opacity: .28; filter: saturate(.5); }
.node.focus { z-index: 4; box-shadow: 0 14px 40px rgba(93,72,44,.22); }
.node.pinned::after {
  content: ''; position: absolute; top: -7px; left: -7px; width: 14px; height: 14px; border-radius: 50%;
  background: var(--ink); border: 2px solid var(--bg);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='none' stroke='%23fdf8ef' stroke-width='2' stroke-linejoin='round'%3E%3Cpath d='M12 3l5 5-3 1-2.5 2.5V15l-2 2-2.5-4.5L3 17l4.5-4L3 10.5l2-2h3.5L11 6z'/%3E%3C/svg%3E");
  background-size: 9px; background-repeat: no-repeat; background-position: center;
}
.node .body { position: relative; overflow: hidden; border-radius: 9px 9px 0 0; }
.node.image .body img { display: block; width: 100%; height: auto; pointer-events: none; }
.node.image.uploading .body img { opacity: .5; }
.node.image.uploading .body::after { content: 'uploading…'; position: absolute; inset: auto 0 0 0; text-align: center; font-size: 11px; padding: 3px; background: rgba(253,250,243,.85); color: var(--muted); }
.node .caption {
  font-size: 12px; color: var(--muted); padding: 6px 10px; line-height: 1.35; min-height: 1.35em;
  white-space: pre-wrap; word-break: break-word; border-top: 1px solid var(--border); border-radius: 0 0 9px 9px;
}
.node .caption:empty::before { content: 'caption…'; color: var(--faint); font-style: italic; }
.node.image .caption:empty { display: none; }
.node.image.sel .caption:empty, .node.image.editing .caption:empty { display: block; }
.node .caption[contenteditable="plaintext-only"]:focus, .node .text:focus { outline: none; }
.node.editing { cursor: text; }
.node.editing .caption:empty::before { content: ''; }

.node.note { background: var(--note); border-color: var(--note-border); }
.node.note .text {
  padding: 12px 14px; font-size: 14px; line-height: 1.45; white-space: pre-wrap; word-break: break-word;
  min-height: 3.2em; font-family: var(--serif); color: var(--ink);
}
.node.note .text:empty::before { content: 'Note…'; color: var(--faint); font-style: italic; }
.node.note.editing { user-select: text; -webkit-user-select: text; }
.node.image.editing .caption { user-select: text; -webkit-user-select: text; }

.node.link { background: var(--panel); }
.node.link .lhead { display: flex; align-items: center; gap: 8px; padding: 10px 12px 6px; }
.node.link .lhead img { width: 16px; height: 16px; border-radius: 3px; }
.node.link .lhead .dom { font-size: 11px; color: var(--faint); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node.link .lhead a { color: var(--accent); text-decoration: none; font-size: 13px; padding: 2px 6px; border-radius: 6px; cursor: pointer; }
.node.link .lhead a:hover { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.node.link .ltitle { padding: 0 12px 10px; font-size: 14px; font-weight: 600; line-height: 1.35; white-space: pre-wrap; word-break: break-word; }
.node.link .ltitle:empty::before { content: 'link title…'; color: var(--faint); font-weight: 400; font-style: italic; }
.node.link .figma { display: inline-flex; gap: 2px; align-items: center; }
.node.link .figma i { width: 6px; height: 6px; border-radius: 50%; display: inline-block; }

/* oblasti v obrázku */
.region {
  position: absolute; border: 2px solid var(--region); border-radius: 3px; background: rgba(200,52,42,.07);
  cursor: pointer; transition: background .15s;
}
.region:hover, .region.sel { background: rgba(200,52,42,.16); }
.region.sel { box-shadow: 0 0 0 2px rgba(253,250,243,.9), 0 0 0 3.5px var(--region); }
.region .rlabel {
  position: absolute; left: -2px; bottom: 100%; margin-bottom: 3px; font-size: 10.5px; line-height: 1;
  background: var(--region); color: #fff; padding: 3px 6px; border-radius: 4px; white-space: nowrap; pointer-events: none;
}
.region .rlabel:empty { display: none; }

/* úchyty */
.handle {
  position: absolute; z-index: 3; opacity: 0; transition: opacity .15s, transform .15s;
}
.node:hover .handle, .node.sel .handle, .region:hover .handle, .region.sel .handle { opacity: 1; }
.handle.link {
  right: -9px; top: 50%; width: 16px; height: 16px; margin-top: -8px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--bg); cursor: crosshair; padding: 0;
}
.handle.link:hover { transform: scale(1.3); }
.region .handle.link { right: -8px; top: -8px; margin-top: 0; width: 13px; height: 13px; background: var(--region); }
.handle.resize {
  right: -4px; bottom: -4px; width: 12px; height: 12px; border-radius: 3px;
  background: var(--panel); border: 1.5px solid var(--faint); cursor: nwse-resize;
}
.handle.resize:hover { border-color: var(--ink); }
.node.dim .handle { display: none; }

/* ---------- HUD ---------- */
.hud { position: fixed; z-index: 10; pointer-events: none; }
.hud a, .hud button, .hud input, .hud label, .hud h1 { pointer-events: auto; }
.top-left { top: max(1rem, env(safe-area-inset-top)); left: 1.25rem; display: flex; align-items: center; gap: .8rem; max-width: calc(100vw - 30rem); }
.top-left h1 {
  font-family: var(--serif); font-style: italic; font-size: 1.2rem; font-weight: 600; margin: 0; letter-spacing: .005em;
  padding: .1rem .4rem; border-radius: 6px; border: 1px solid transparent; outline: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 30rem;
}
.top-left h1:hover, .top-left h1:focus { border-color: var(--border); background: color-mix(in srgb, var(--panel) 70%, transparent); }
.back { color: var(--muted); text-decoration: none; font-size: .85rem; white-space: nowrap; }
.back:hover { color: var(--ink); }
.status { color: var(--faint); font-size: .72rem; white-space: nowrap; }
.status.err { color: var(--danger); }
.top-right { top: max(1rem, env(safe-area-inset-top)); right: 1.25rem; display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; justify-content: flex-end; }

.toolbar {
  left: 50%; transform: translateX(-50%); bottom: max(1.25rem, env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: .15rem; padding: .3rem;
  background: color-mix(in srgb, var(--panel) 90%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
}
.toolbar .sep { width: 1px; height: 1.6rem; background: var(--border); margin: 0 .3rem; }
.tb {
  pointer-events: auto; display: flex; flex-direction: column; align-items: center; gap: 2px;
  font: inherit; font-size: .66rem; color: var(--muted); background: none; border: 0; border-radius: 10px;
  padding: .4rem .7rem; cursor: pointer; min-width: 4.2rem; transition: background .15s, color .15s;
}
.tb svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.tb:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 9%, transparent); color: var(--ink); }
.tb.on { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }
.tb:disabled { opacity: .35; cursor: default; }
.tb.danger:hover:not(:disabled) { color: var(--danger); }

.epop {
  z-index: 20; pointer-events: auto; display: grid; gap: .45rem;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); padding: .55rem; width: 15rem;
}
.epop .seg { display: flex; gap: .2rem; background: var(--bg); border: 1px solid var(--border); border-radius: 9px; padding: .18rem; }
.epop .seg button { flex: 1; font: inherit; font-size: 1rem; color: var(--muted); cursor: pointer; background: none; border: 0; border-radius: 7px; padding: .2rem .4rem; }
.epop .seg button.sel { background: var(--panel); color: var(--accent); font-weight: 600; box-shadow: 0 1px 3px rgba(93,72,44,.12); }
.epop input {
  font: inherit; font-size: .85rem; color: var(--ink); background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; padding: .4rem .6rem; outline: none; width: 100%;
}
.epop input:focus { border-color: var(--accent); }
.linkbtn { font: inherit; font-size: .76rem; color: var(--danger); background: none; border: 0; padding: 0; cursor: pointer; text-decoration: underline; justify-self: start; }

.empty {
  position: fixed; z-index: 5; inset: 0; display: grid; place-content: center; text-align: center;
  gap: .65rem; padding: 0 1.5rem; pointer-events: none; transition: opacity .35s;
}
.empty.gone { opacity: 0; }
.empty p { margin: 0; color: var(--muted); font-size: .95rem; max-width: 34rem; line-height: 1.55; }
.empty .big { font-family: var(--serif); font-style: italic; color: var(--ink); font-size: 2.2rem; font-weight: 500; margin-bottom: .35rem; }

.help {
  position: fixed; z-index: 5; right: 1.25rem; bottom: max(1.25rem, env(safe-area-inset-bottom));
  color: var(--faint); font-size: .72rem; pointer-events: none; text-align: right; line-height: 1.5;
}


.sheet {
  position: fixed; z-index: 40; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: min(34rem, calc(100vw - 2rem)); max-height: calc(100vh - 2rem); overflow: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow); padding: 1.5rem;
}
.sheet h2 { font-family: var(--serif); font-weight: 500; margin: 0 0 1rem; }
.sheet dl { display: grid; grid-template-columns: 6rem 1fr; gap: .6rem 1rem; margin: 0 0 1.2rem; font-size: .88rem; line-height: 1.5; }
.sheet dt { font-weight: 600; color: var(--accent); }
.sheet dd { margin: 0; color: var(--muted); }
.sheet dd b { color: var(--ink); }

.dropzone {
  position: fixed; z-index: 50; inset: .75rem; border: 2px dashed var(--accent); border-radius: 18px;
  background: rgba(244,236,223,.7); display: grid; place-content: center; pointer-events: none;
  font-family: var(--serif); font-style: italic; font-size: 1.6rem; color: var(--accent);
}

/* ---------- responzivita a dotyk (až na konci, ať přebije základní pravidla) ---------- */
@media (max-width: 900px) { .help { display: none; } }
@media (max-width: 640px) {
  .top-left { left: .75rem; gap: .5rem; max-width: calc(100vw - 1.5rem); }
  .top-left h1 { font-size: 1.05rem; max-width: 60vw; }
  .top-right { top: auto; left: .75rem; right: .75rem; justify-content: center; gap: .35rem;
               bottom: calc(max(1.25rem, env(safe-area-inset-bottom)) + 4.4rem); }
  .chip-btn { padding: .3rem .65rem; font-size: .78rem; }
  .chip-btn .pre { display: none; }
  .toolbar { width: calc(100vw - 1.5rem); justify-content: space-between; padding: .25rem; gap: 0; }
  .toolbar .sep { display: none; }
  .tb { min-width: 0; flex: 1; padding: .45rem .1rem; font-size: .6rem; }
  .epop { width: min(15rem, calc(100vw - 1.5rem)); }
  .empty { padding: 0 1.25rem 6rem; }
  .empty .big { font-size: 1.7rem; }
  .empty p { font-size: .88rem; }
  .sheet { padding: 1.1rem; border-radius: 12px; }
  .sheet dl { grid-template-columns: 1fr; gap: .15rem .5rem; }
  .sheet dt { margin-top: .6rem; }
}
/* dotyk: žádný hover — úchyty jen u vybrané položky, a větší, ať se dají trefit prstem */
@media (pointer: coarse) {
  .handle { opacity: 0; }
  .node:hover .handle, .region:hover .handle { opacity: 0; }
  .node.sel .handle, .region.sel .handle { opacity: 1; }
  .handle.link { width: 26px; height: 26px; margin-top: -13px; right: -14px; }
  .region .handle.link { width: 22px; height: 22px; right: -12px; top: -12px; margin-top: 0; }
  .handle.resize { width: 22px; height: 22px; right: -9px; bottom: -9px; border-radius: 5px; }
  .node .caption, .node.note .text, .node.link .ltitle { -webkit-user-select: none; user-select: none; }
  .node.editing .caption, .node.note.editing .text, .node.link.editing .ltitle { -webkit-user-select: text; user-select: text; }
  #scrollbtn, .hint-fine { display: none !important; }
}
@media (pointer: fine) { .hint-coarse { display: none !important; } }
