/* Map page: full-viewport canvas under a fixed header + tag filter bar. */
html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}






/* Tag Filter Bar */
#tag-filter-bar {
  position: fixed;
  top: var(--header-total);
  left: 0;
  right: 0;
  height: 36px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  gap: 8px;
  z-index: 950;
  overflow-x: auto;
  scrollbar-width: none;
}

#tag-filter-bar::-webkit-scrollbar {
  display: none;
}

.filter-pills {
  display: flex;
  align-items: center;
  gap: 5px;
}

.filter-pill {
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--border-color);
  background: var(--bg-elevated);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}

.filter-pill:hover {
  color: var(--text-main);
}

.filter-pill.active {
  background: var(--accent-primary);
  border-color: var(--accent-primary);
  color: #ffffff;
}

.map-stats-pill {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  font-weight: 600;
}

/* ================= CANVAS & NODES ================= */
#canvas-container {
  position: fixed;
  top: calc(var(--header-total) + 36px);
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
  touch-action: none;
  cursor: grab;
  background-image: radial-gradient(var(--grid-dot) 1.25px, transparent 1.25px);
  background-size: 24px 24px;
}

#canvas-container.grabbing {
  cursor: grabbing;
}

/* IMPORTANT: No will-change: transform or translate3d so vector text stays crisp on zoom */
#world {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  transform-origin: 0 0;
  -webkit-font-smoothing: antialiased;
  text-rendering: geometricPrecision;
}

#edges-svg {
  position: absolute;
  top: -10000px;
  left: -10000px;
  width: 20000px;
  height: 20000px;
  pointer-events: none;
  overflow: visible;
}

.edge-primary {
  fill: none;
  stroke: var(--edge-solid);
  stroke-width: 2;
  stroke-linecap: round;
  transition: opacity 0.2s, stroke 0.2s, stroke-width 0.2s;
}

.edge-primary.highlighted {
  stroke: var(--accent-primary);
  stroke-width: 2.8;
}

.edge-lateral {
  fill: none;
  stroke-width: 2.2;
  stroke-dasharray: 6 5;
  stroke-linecap: round;
  transition: opacity 0.2s, stroke-width 0.2s;
}

#nodes-layer {
  position: absolute;
  top: 0;
  left: 0;
}

/* Reverted Clean Circular Primary Kanji Node */
.node-primary {
  position: absolute;
  width: 64px;
  height: 64px;
  margin-left: -32px;
  margin-top: -32px;
  border-radius: 50%;
  background: var(--node-bg);
  border: 2.5px solid #3b82f6;
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  transition: opacity 0.2s, transform 0.12s ease-out;
  -webkit-font-smoothing: antialiased;
  text-rendering: geometricPrecision;
}

.node-primary:hover {
  transform: scale(1.06);
  z-index: 30;
}

.node-primary.selected {
  z-index: 40;
  transform: scale(1.08);
}

.node-primary.dimmed {
  opacity: 0.28;
}

.node-primary-char {
  font-size: 28px;
  font-weight: 800;
  line-height: 1.05;
  color: var(--text-main);
}

.node-primary-reading {
  font-size: 9px;
  font-weight: 600;
  color: var(--text-muted);
  line-height: 1;
  margin-top: 2px;
  max-width: 54px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Reverted Clean Capsule Secondary Compound Node */
.node-secondary {
  position: absolute;
  transform: translate(-50%, -50%);
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--node-bg);
  border: 2px solid #10b981;
  box-shadow: var(--shadow-sm);
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  transition: opacity 0.2s, box-shadow 0.15s;
  -webkit-font-smoothing: antialiased;
  text-rendering: geometricPrecision;
  z-index: 15;
}

.node-secondary:hover {
  box-shadow: var(--shadow-md);
  z-index: 35;
}

.node-secondary.selected {
  z-index: 45;
}

.node-secondary.dimmed {
  opacity: 0.25;
}

.node-secondary-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.node-secondary-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.node-secondary-label {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-main);
  line-height: 1.2;
}

.node-secondary-reading {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.15;
  margin-top: 1px;
}

.hidden-node {
  display: none !important;
}

/* ================= FLOATING CONTROLS ================= */
#link-mode-bar {
  position: fixed;
  top: calc(var(--header-total) + 48px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-surface);
  border: 1.5px solid #f59e0b;
  border-radius: 12px;
  padding: 8px 14px;
  box-shadow: var(--shadow-lg);
  display: none;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  z-index: 980;
  max-width: calc(100vw - 24px);
}

#link-mode-bar.visible {
  display: flex;
}

.link-type-chip {
  padding: 4px 9px;
  border-radius: 999px;
  border: 1px solid var(--border-color);
  background: var(--bg-elevated);
  color: var(--text-main);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.link-type-chip.active {
  color: #ffffff;
  border-color: transparent;
}

#zoom-controls {
  position: fixed;
  bottom: 16px;
  left: 16px;
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 4px;
  box-shadow: var(--shadow-md);
  z-index: 900;
}

.zoom-btn {
  width: 30px;
  height: 28px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-main);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.zoom-btn:hover {
  background: var(--bg-elevated);
}

#zoom-level-text {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
  padding: 0 6px;
  min-width: 44px;
  text-align: center;
  cursor: pointer;
}

/* ================= INSPECTOR PANEL ================= */
#inspector-panel {
  position: fixed;
  top: calc(var(--header-total) + 36px);
  right: 0;
  bottom: 0;
  width: 350px;
  max-width: 100vw;
  background: var(--bg-surface);
  border-left: 1px solid var(--border-color);
  box-shadow: var(--shadow-lg);
  z-index: 990;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.22s ease;
}

#inspector-panel.open {
  transform: translateX(0);
}

.inspector-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
}

.inspector-badge {
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--bg-elevated);
  color: var(--text-muted);
}

.inspector-body {
  padding: 14px 16px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.kanji-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: 8px;
  border: 1.5px solid var(--accent-primary);
  background: rgba(37, 99, 235, 0.08);
  color: var(--text-main);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.kanji-chip:hover {
  background: rgba(37, 99, 235, 0.18);
}

.hint-chip {
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px dashed var(--accent-primary);
  background: transparent;
  color: var(--accent-primary);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
}

.hint-chip:hover {
  background: rgba(37, 99, 235, 0.1);
}

.tag-toggle {
  padding: 4px 9px;
  border-radius: 999px;
  border: 1px solid var(--border-color);
  background: var(--bg-elevated);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.tag-toggle.active {
  background: var(--accent-secondary);
  border-color: var(--accent-secondary);
  color: #ffffff;
}

.color-swatches {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.swatch-btn {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
}

.swatch-btn.active {
  border-color: var(--text-main);
  transform: scale(1.12);
}

.checklist-box {
  max-height: 155px;
  overflow-y: auto;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: var(--bg-elevated);
  padding: 4px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.checklist-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 8px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  gap: 6px;
}

.checklist-item:hover {
  background: var(--bg-surface);
}

.dict-links {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.dict-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 5px 9px;
  border-radius: 7px;
  border: 1px solid var(--border-color);
  background: var(--bg-elevated);
  color: var(--text-main);
  text-decoration: none;
  font-size: 11px;
  font-weight: 600;
}

.dict-link:hover {
  border-color: var(--accent-primary);
  color: var(--accent-primary);
}

.danger-zone {
  border-top: 1px solid var(--border-color);
  padding-top: 12px;
  margin-top: 4px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

/* ================= SMALL UTILITIES (replacing old inline styles) ================= */
.link-mode-title {
  font-size: 12px;
  font-weight: 800;
  color: #d97706;
}
#link-mode-status {
  font-size: 12px;
  font-weight: 600;
}
.label-input {
  font-size: 20px;
  font-weight: 800;
}
.color-picker {
  width: 26px;
  height: 24px;
  border: none;
  background: transparent;
  cursor: pointer;
}
.field-select.compact {
  width: auto;
  padding: 2px 6px;
  font-size: 11px;
}
.link-target {
  font-weight: 700;
  cursor: pointer;
}
.link-search-row {
  display: flex;
  gap: 6px;
  margin: 6px 0 4px;
}
.link-search-row .field-input {
  flex: 1;
}
.add-hint {
  font-size: 11px;
  color: var(--accent-primary);
  font-weight: 700;
}
.muted.pad {
  padding: 6px;
  font-size: 11px;
}
.kanji-chip.small {
  font-size: 11px;
  padding: 2px 7px;
  cursor: default;
}

/* ================= SUMMARY VIEW (phone inspector, read-first) ================= */
.summary-hero {
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
}
.summary-label {
  font-size: 34px;
  font-weight: 800;
  line-height: 1.1;
}
.summary-reading {
  font-size: 17px;
  font-weight: 600;
  color: var(--text-muted);
}
.summary-notes {
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  background: var(--bg-elevated);
  border-radius: 10px;
  padding: 8px 12px;
}
.summary-actions {
  display: flex;
  gap: 8px;
}
.summary-actions .tb-btn {
  flex: 1;
  justify-content: center;
  min-height: 44px;
  font-size: 14px;
}
.link-chip {
  border-width: 1.5px;
  border-style: solid;
}

/* Phone-only map controls are hidden on desktop. */
#m-search,
#fab-wrap,
.sheet-grab {
  display: none;
}

/* ================= PHONES ================= */
@media (max-width: 699px) {
  .map-actions,
  .map-stats-pill {
    display: none;
  }
  .brand-title {
    font-size: 16px;
  }
  .mobile-actions {
    display: flex;
    gap: 6px;
  }

  #tag-filter-bar {
    padding: 0 10px;
  }
  .filter-pill {
    padding: 5px 11px;
    font-size: 12px;
  }

  #canvas-container {
    bottom: var(--tabbar-h);
  }

  /* Bigger touch targets for word capsules without changing how they look. */
  .node-secondary::after {
    content: '';
    position: absolute;
    inset: -10px -6px;
  }

  #zoom-controls {
    left: 12px;
    bottom: calc(var(--tabbar-h) + 12px);
    padding: 3px;
  }
  #zoom-level-text {
    display: none;
  }
  .zoom-btn {
    width: 40px;
    height: 38px;
    font-size: 18px;
  }

  #fab-wrap {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 10px;
    position: fixed;
    right: max(16px, env(safe-area-inset-right, 0px));
    bottom: calc(var(--tabbar-h) + 16px);
    z-index: 950;
  }
  #fab {
    width: 58px;
    height: 58px;
    border-radius: 18px;
    border: none;
    background: var(--accent-primary);
    color: #fff;
    font-size: 30px;
    font-weight: 400;
    box-shadow: 0 8px 22px rgba(37, 99, 235, 0.38);
    cursor: pointer;
    transition: transform 0.15s;
  }
  #fab.open {
    transform: rotate(45deg);
  }
  #fab-menu {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 8px;
  }
  #fab-menu[hidden] {
    display: none;
  }
  .fab-option {
    padding: 12px 16px;
    border-radius: 14px;
    border: 1px solid var(--border-color);
    background: var(--bg-surface);
    color: var(--text-main);
    font: inherit;
    font-size: 15px;
    font-weight: 700;
    box-shadow: var(--shadow-md);
    cursor: pointer;
  }

  /* Search overlay with tappable results */
  #m-search:not([hidden]) {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: var(--header-total);
    left: 0;
    right: 0;
    bottom: calc(var(--tabbar-h));
    z-index: 1005;
    background: var(--bg-page);
  }
  .keyboard-open #m-search:not([hidden]) {
    bottom: var(--kb-offset);
  }
  .m-search-bar {
    display: flex;
    gap: 8px;
    padding: 10px 12px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-color);
  }
  #m-search-input {
    flex: 1;
    min-width: 0;
    padding: 9px 12px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    background: var(--bg-elevated);
    color: var(--text-main);
    outline: none;
  }
  .m-search-results {
    flex: 1;
    overflow-y: auto;
    padding: 6px 0;
  }
  .m-result {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    min-height: 54px;
    padding: 8px 16px;
    border: none;
    border-bottom: 1px solid var(--border-color);
    background: transparent;
    color: var(--text-main);
    font: inherit;
    text-align: left;
    cursor: pointer;
  }
  .m-result-label {
    font-size: 19px;
    font-weight: 800;
  }
  .m-result-sub {
    font-size: 13px;
    color: var(--text-muted);
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .m-result-kind {
    font-size: 11px;
    font-weight: 700;
    color: var(--text-muted);
    background: var(--bg-elevated);
    border-radius: 999px;
    padding: 2px 8px;
  }

  /* Inspector: bottom sheet with a peek and a full state. */
  #inspector-panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-width: none;
    height: 88dvh;
    border-left: none;
    border-top: 1px solid var(--border-color);
    border-radius: 18px 18px 0 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    transform: translateY(100%);
    z-index: 1001;
  }
  #inspector-panel.open {
    transform: translateY(44dvh);
  }
  #inspector-panel.open.full {
    transform: translateY(0);
  }
  .inspector-header {
    position: relative;
    padding: 16px 16px 10px;
    touch-action: none;
  }
  .sheet-grab {
    display: block;
    position: absolute;
    top: 6px;
    left: 50%;
    width: 38px;
    height: 5px;
    margin-left: -19px;
    border-radius: 999px;
    background: var(--border-color);
  }
  /* Only the visible part of the sheet scrolls in the peek state. */
  #inspector-panel.open:not(.full) .inspector-body {
    max-height: calc(44dvh - 60px);
  }

  .kanji-chip,
  .dict-link,
  .hint-chip,
  .tag-toggle {
    min-height: 36px;
  }
  .kanji-chip {
    font-size: 15px;
    padding: 6px 12px;
  }
}

/* Coarse pointers anywhere (tablets too) get larger hit areas for kanji nodes. */
@media (pointer: coarse) {
  .node-primary::after {
    content: '';
    position: absolute;
    inset: -8px;
    border-radius: 50%;
  }
}

/* ================= MEANINGS ================= */
.summary-meaning {
  font-size: 15px;
  line-height: 1.55;
}
.field-textarea.short {
  min-height: 48px;
}
.dict-status a {
  color: inherit;
}
.hint-chip.active {
  background: var(--accent-primary);
  color: #fff;
}

/* ================= HEADER ACTIONS (desktop & tablet) ================= */
@media (min-width: 700px) {
  .map-actions {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
  }
  .mobile-actions {
    display: none;
  }
}
#btn-view-menu.active {
  color: #b45309;
  border-color: #f59e0b;
  background: rgba(245, 158, 11, 0.12);
}
.zoom-btn .icon {
  width: 17px;
  height: 17px;
}
.link-mode-title {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

#fab .icon {
  width: 28px;
  height: 28px;
}
.fab-option {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.fab-option .icon {
  width: 18px;
  height: 18px;
  color: var(--accent-primary);
}
.dict-link .icon,
.summary-actions .icon {
  width: 15px;
  height: 15px;
}
.dict-status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.dict-status .icon {
  width: 14px;
  height: 14px;
}

/* Tablets: desktop layout with touch-sized targets */
@media (min-width: 700px) and (pointer: coarse) {
  #inspector-panel {
    width: 390px;
  }
  .filter-pill {
    padding: 7px 13px;
    font-size: 13px;
  }
  #tag-filter-bar {
    height: 46px;
  }
  #canvas-container,
  #inspector-panel {
    top: calc(var(--header-total) + 46px);
  }
  .kanji-chip,
  .dict-link,
  .hint-chip,
  .tag-toggle {
    min-height: 40px;
    font-size: 14px;
  }
  .zoom-btn {
    width: 44px;
    height: 42px;
  }
  .node-secondary::after {
    content: '';
    position: absolute;
    inset: -10px -6px;
  }
}

/* Pinned nodes: a small pin badge (automatic layout won't move them) */
.pin-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
  pointer-events: none;
}
.pin-badge .icon {
  width: 11px;
  height: 11px;
}
.node-secondary.pinned .pin-badge {
  top: -8px;
  right: -4px;
}
